Annotation of loncom/homework/default_homework.lcpm, revision 1.148
1.42 albertel 1: # The LearningOnline Network with CAPA
1.1 harris41 2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
1.42 albertel 3: #
1.148 ! raeburn 4: # $Id: default_homework.lcpm,v 1.147 2010/01/06 16:59:30 www Exp $
1.42 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.20 harris41 28: #
1.76 albertel 29:
1.25 albertel 30: #init some globals
1.38 albertel 31: $hidden::RANDOMINIT=0;
1.22 ng 32: $pi=atan2(1,1)*4;
33: $rad2deg=180.0/$pi;
34: $deg2rad=$pi/180.0;
1.44 matthew 35: $"=' ';
1.116 albertel 36: use strict;
37: {
38: my $n = 0;
39: my $total = 0;
40: my $num_left = 0;
41: my @order;
42: my $type;
43:
44: sub init_permutation {
45: my ($size,$requested_type) = @_;
46: @order = (0..$size-1);
47: $n = $size;
48: $type = $requested_type;
49: if ($type eq 'ordered') {
50: $total = $num_left = 1;
51: } elsif ($type eq 'unordered') {
52: $total = $num_left = &factorial($size);
53: } else {
54: die("Unkown type: $type");
55: }
56: }
57:
58: sub get_next_permutation {
59: if ($num_left == $total) {
60: $num_left--;
1.117 albertel 61: return \@order;
1.116 albertel 62: }
63:
64: # Find largest index j with a[j] < a[j+1]
65:
66: my $j = scalar(@order) - 2;
67: while ($order[$j] > $order[$j+1]) {
68: $j--;
69: }
70:
71: # Find index k such that a[k] is smallest integer
72: # greater than a[j] to the right of a[j]
73:
74: my $k = scalar(@order) - 1;
75: while ($order[$j] > $order[$k]) {
76: $k--;
77: }
78:
79: # Interchange a[j] and a[k]
80:
81: @order[($k,$j)] = @order[($j,$k)];
82:
83: # Put tail end of permutation after jth position in increasing order
84:
85: my $r = scalar(@order) - 1;
86: my $s = $j + 1;
87:
88: while ($r > $s) {
89: @order[($s,$r)]=@order[($r,$s)];
90: $r--;
91: $s++;
92: }
93:
94: $num_left--;
1.117 albertel 95: return(\@order);
1.116 albertel 96: }
97:
98: sub get_permutations_left {
99: return $num_left;
100: }
101: }
1.3 albertel 102:
1.91 albertel 103: sub check_commas {
104: my ($response)=@_;
105: #print("$response ");
106: my @numbers=split(',',$response);
107: #print(" numbers ".join('-',@numbers)." ");
108: if (scalar(@numbers) > 1) {
109: #print(" numbers[0] ".$numbers[0]." ");
110: if (length($numbers[0]) > 3 || length($numbers[0]) == 0) { return -1; }
111: shift(@numbers);
112: #print(" numbers ".scalar(@numbers)." ");
113: while (scalar(@numbers) > 1) {
114: #print(" numbers ".join('-',@numbers)." ");
115: if (length($numbers[0]) != 3) { return -2; }
116: shift(@numbers);
117: }
118: my ($number)=split('\.',$numbers[0]);
119: #print(" number ".$number." ");
120: #print(" numbers[0] ".$numbers[0]." ");
121: if (length($number) != 3) { return -3; }
122: } else {
123: my ($number)=split('\.',$numbers[0]);
124: if (length($number) > 3) { return -4; }
125: }
126: return 1;
127: }
128:
1.117 albertel 129:
1.7 albertel 130: sub caparesponse_check {
1.74 albertel 131: my ($answer,$response)=@_;
1.73 albertel 132: #not properly used yet: calc
133: #not to be used: $ans_fmt
1.74 albertel 134: my $type=$LONCAPA::CAPAresponse_args{'type'};
135: my $tol=$LONCAPA::CAPAresponse_args{'tol'};
136: my $sig=$LONCAPA::CAPAresponse_args{'sig'};
1.88 albertel 137: my $ans_fmt=$LONCAPA::CAPAresponse_args{'format'};
1.74 albertel 138: my $unit=$LONCAPA::CAPAresponse_args{'unit'};
139: my $calc=$LONCAPA::CAPAresponse_args{'calc'};
140: my $samples=$LONCAPA::CAPAresponse_args{'samples'};
1.73 albertel 141:
142: my $tol_type=''; # gets it's value from whether tol has a % or not done
143: my $sig_lbound=''; #done
144: my $sig_ubound=''; #done
145:
146:
147: #type's definitons come from capaParser.h
1.116 albertel 148:
1.73 albertel 149: #remove leading and trailing whitespace
150: if (!defined($response)) {
151: $response='';
152: }
153: if ($response=~ /^\s|\s$/) {
154: $response=~ s:^\s+|\s+$::g;
1.119 albertel 155: &LONCAPA_INTERNAL_DEBUG("Removed ws now :$response:");
1.73 albertel 156: }
1.116 albertel 157:
1.117 albertel 158: #&LONCAPA_INTERNAL_DEBUG(" type is $type ");
1.100 albertel 159: if ($type eq 'cs' || $type eq 'ci') {
1.130 www 160: #for string answers make sure all places spaces occur, there is
1.76 albertel 161: #really only 1 space, in both the answer and the response
162: $answer=~s/ +/ /g;
163: $response=~s/ +/ /g;
1.100 albertel 164: } elsif ($type eq 'mc') {
165: $answer=~s/[\s,]//g;
166: $response=~s/[\s,]//g;
1.76 albertel 167: }
1.91 albertel 168: if ($type eq 'float' && $unit=~/\$/) {
1.126 www 169: if ($response!~/^\$|\$$/) { return ('NO_UNIT', undef); }
1.88 albertel 170: $response=~s/\$//g;
171: }
1.91 albertel 172: if ($type eq 'float' && $unit=~/\,/ && (&check_commas($response)<0)) {
173: return "COMMA_FAIL:";
174: }
1.88 albertel 175: $ans_fmt=~s/\W//g;
1.91 albertel 176: $unit=~s/[\$,]//g;
177: if ($type eq 'float') { $response=~s/,//g; }
1.88 albertel 178:
1.117 albertel 179: if (length($response) > 500) { return ('TOO_LONG',undef); }
1.73 albertel 180:
181: if ($type eq '' ) {
1.119 albertel 182: &LONCAPA_INTERNAL_DEBUG("Didn't find a type :$type: defaulting");
1.73 albertel 183: if ( $answer eq ($answer *1.0)) { $type = 2;
184: } else { $type = 3; }
185: } else {
1.107 albertel 186: if ($type eq 'cs') { $type = 4; }
1.73 albertel 187: elsif ($type eq 'ci') { $type = 3 }
188: elsif ($type eq 'mc') { $type = 5; }
189: elsif ($type eq 'fml') { $type = 8; }
1.107 albertel 190: elsif ($type eq 'math') { $type = 9; }
1.73 albertel 191: elsif ($type eq 'subj') { $type = 7; }
192: elsif ($type eq 'float') { $type = 2; }
193: elsif ($type eq 'int') { $type = 1; }
1.117 albertel 194: else { return ('ERROR', "Unknown type of answer: $type") }
1.73 albertel 195: }
196:
197: my $points;
198: my $id_list;
199: #formula type setup the sample points
200: if ($type eq '8') {
201: ($id_list,$points)=split(/@/,$samples);
1.119 albertel 202: &LONCAPA_INTERNAL_DEBUG("Found :$id_list:$points: points in $samples");
1.73 albertel 203: }
204: if ($tol eq '') {
205: $tol=0.0;
206: $tol_type=1; #TOL_ABSOLUTE
207: } else {
208: if ($tol =~ /%$/) {
209: chop $tol;
210: $tol_type=2; #TOL_PERCENTAGE
211: } else {
212: $tol_type=1; #TOL_ABSOLUTE
213: }
214: }
215:
1.85 albertel 216: ($sig_ubound,$sig_lbound)=&LONCAPA_INTERNAL_get_sigrange($sig);
217:
1.73 albertel 218: my $reterror="";
1.107 albertel 219: my $result;
1.142 raeburn 220: if (($type eq '9') || ($type eq '8')) {
221: if ($response=~/\=/) {
222: return ('BAD_FORMULA','Please submit just an expression, not an equation.');
1.143 riegler 223: } elsif ($response =~ /\,/ and $response !~ /^\s*\{.*\}\s*$/) {
1.142 raeburn 224: return ('BAD_FORMULA');
225: }
226: }
1.107 albertel 227: if ($type eq '9') {
1.108 www 228: $result = &maxima_check(&maxima_cas_formula_fix($response),&maxima_cas_formula_fix($answer),\$reterror);
1.107 albertel 229: } else {
1.109 albertel 230: if ($type eq '8') { # fml type
231: $response = &capa_formula_fix($response);
232: $answer = &capa_formula_fix($answer);
233: }
234: $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.73 albertel 235: $tol_type,$tol,
236: $sig_lbound,$sig_ubound,
237: $ans_fmt,$unit,$calc,$id_list,
238: $points,$external::randomseed,
239: \$reterror);
1.107 albertel 240: }
1.73 albertel 241: if ($result == '1') { $result='EXACT_ANS'; }
242: elsif ($result == '2') { $result='APPROX_ANS'; }
243: elsif ($result == '3') { $result='SIG_FAIL'; }
244: elsif ($result == '4') { $result='UNIT_FAIL'; }
245: elsif ($result == '5') { $result='NO_UNIT'; }
246: elsif ($result == '6') { $result='UNIT_OK'; }
247: elsif ($result == '7') { $result='INCORRECT'; }
248: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
249: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
250: elsif ($result =='10') { $result='SUB_RECORDED'; }
251: elsif ($result =='11') { $result='BAD_FORMULA'; }
1.94 albertel 252: elsif ($result =='12' && !$response) { $result='MISSING_ANSWER'; }
253: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.77 albertel 254: elsif ($result =='13') { $result='UNIT_INVALID_INSTRUCTOR'; }
255: elsif ($result =='141') { $result='UNIT_INVALID_STUDENT'; }
256: elsif ($result =='142') { $result='UNIT_INVALID_STUDENT'; }
257: elsif ($result =='143') { $result='UNIT_INVALID_STUDENT'; }
258: elsif ($result =='15') { $result='UNIT_IRRECONCIBLE'; }
1.73 albertel 259: else {$result = "ERROR: Unknown Result:$result:$@:";}
260:
1.119 albertel 261: &LONCAPA_INTERNAL_DEBUG("RetError $reterror: Answer $answer: Response $response: type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|");
262: &LONCAPA_INTERNAL_DEBUG(" $answer $response $result ");
1.139 raeburn 263: return ($result,$reterror);
1.37 albertel 264: }
265:
1.73 albertel 266:
1.37 albertel 267: sub caparesponse_check_list {
1.119 albertel 268: my $responses=$LONCAPA::CAPAresponse_args{'response'};
1.105 albertel 269: &LONCAPA_INTERNAL_DEBUG("args ".join(':',%LONCAPA::CAPAresponse_args));
1.74 albertel 270: my $type = $LONCAPA::CAPAresponse_args{'type'};
1.133 www 271: my $answerunit=$LONCAPA::CAPAresponse_args{'unit'};
272: &LONCAPA_INTERNAL_DEBUG("Got type :$type: answer unit :$answerunit:\n");
1.119 albertel 273:
274: my $num_input_lines =
275: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}});
1.120 albertel 276:
277: if ($type ne '' ) {
1.119 albertel 278: if (scalar(@$responses) < $num_input_lines) {
1.105 albertel 279: return 'MISSING_ANSWER';
280: }
1.119 albertel 281: if (scalar(@$responses) > $num_input_lines) {
282: return 'EXTRA_ANSWER';
283: }
284:
285: }
286:
287: foreach my $which (0..($num_input_lines-1)) {
288: my $answer_size =
289: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}[$which]});
290: if ($type ne ''
291: && $answer_size > 1) {
292: $responses->[$which]=[split(/,/,$responses->[$which])];
293: } else {
294: $responses->[$which]=[$responses->[$which]];
295: }
296: }
297: foreach my $which (0..($num_input_lines-1)) {
298: my $answer_size =
299: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}[$which]});
300: my $response_size =
301: scalar(@{$responses->[$which]});
302: if ($answer_size > $response_size) {
303: return 'MISSING_ANSWER';
304: }
305: if ($answer_size < $response_size) {
1.105 albertel 306: return 'EXTRA_ANSWER';
307: }
1.73 albertel 308: }
1.119 albertel 309:
310: &LONCAPA_INTERNAL_DEBUG("Initial final response :$responses->[0][-1]:");
1.105 albertel 311: my $unit;
1.140 raeburn 312: if ($type eq 'float' || $type eq '') {
1.73 albertel 313: #for numerical problems split off the unit
1.128 www 314: # if ( $responses->[0][-1]=~ /(.*[^\s])\s+([^\s]+)/ ) {
1.132 www 315: if ( $responses->[0][-1]=~ /^([\d\.\,\s\$]*(?:(?:[xX\*]10[\^\*]*|[eE]*)[\+\-]*\d*)*(?:^|\S)\d+)([\$\s\w\^\*\/\(\)\+\-]*[^\d\.\s\,][\$\s\w\^\*\/\(\)\+\-]*)$/ ) {
1.119 albertel 316: $responses->[0][-1]=$1;
1.128 www 317: $unit=&capa_formula_fix($2);
318: &LONCAPA_INTERNAL_DEBUG("Found unit :$unit:");
1.73 albertel 319: }
320: }
1.119 albertel 321: &LONCAPA_INTERNAL_DEBUG("Final final response :$responses->[0][-1]:$unit:");
1.73 albertel 322: $unit=~s/\s//;
1.133 www 323: foreach my $response (@$responses) {
324: foreach my $element (@$response) {
1.138 raeburn 325: if (($type eq 'float') || (($type eq '') && ($unit ne ''))) {
326: $element =~ s/\s//g;
327: }
1.133 www 328: my $appendunit=$unit;
1.147 www 329: # Deal with percentages
330: # unit is unit entered by student, answerunit is unit by author
331: # Deprecated: divide answer by 100 if student entered percent,
332: # but author did not. Too much confusion
1.146 www 333: # if (($unit=~/\%/) && ($answerunit ne '%')) {
334: # $element=$element/100;
335: # $appendunit=~s/\%//;
336: # }
1.147 www 337: # Author entered percent, student did not
338: if (($unit!~/\%/) && ($answerunit=~/\%/)) {
339: $element=$element*100;
340: $appendunit='%'.$appendunit;
341: }
342: # Zero does not need a dimension
1.133 www 343: if (($element==0) && ($unit!~/\w/) && ($answerunit=~/\w/)) {
344: $appendunit=$answerunit;
345: }
1.148 ! raeburn 346: if ($appendunit ne '') {
! 347: $element .= " $appendunit";
! 348: }
1.133 www 349: &LONCAPA_INTERNAL_DEBUG("Made response element :$element:");
350: }
1.119 albertel 351: }
352:
1.117 albertel 353: foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
354: if (!defined($thisanswer)) {
355: return ('ERROR','answer was undefined');
356: }
357: }
358:
359:
1.121 albertel 360: # &LONCAPA_INTERNAL_DEBUG(&LONCAPA_INTERNAL_Dumper($responses));
1.117 albertel 361: my %memoized;
362: if ($LONCAPA::CAPAresponse_answer->{'type'} eq 'ordered') {
1.119 albertel 363: for (my $i=0; $i<scalar(@$responses);$i++) {
1.117 albertel 364: my $answer = $LONCAPA::CAPAresponse_answer->{'answers'}[$i];
1.119 albertel 365: my $response = $responses->[$i];
1.117 albertel 366: my $key = "$answer\0$response";
1.119 albertel 367: my (@awards,@msgs);
368: for (my $j=0; $j<scalar(@$response); $j++) {
369: my ($award,$msg) = &caparesponse_check($answer->[$j],
370: $response->[$j]);
371: push(@awards,$award);
372: push(@msgs, $msg);
373: }
374: my ($award,$msg) =
375: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
376: $memoized{$key} = [$award,$msg];
1.117 albertel 377: }
378: } else {
1.119 albertel 379: #FIXME broken with unorder responses where one is a <value>
380: # and the other is a <vector> (need to delay parse til
381: # inside the loop?)
382: foreach my $response (@$responses) {
383: my $response_size = scalar(@{$response});
1.117 albertel 384: foreach my $answer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
385: my $key = "$answer\0$response";
1.119 albertel 386: my $answer_size = scalar(@{$answer});
387: my ($award,$msg);
388: if ($answer_size > $response_size) {
389: $award = 'MISSING_ANSWER';
390: } elsif ($answer_size < $response_size) {
391: $award = 'EXTRA_ANSWER';
392: } else {
393: my (@awards,@msgs);
394: for (my $j=0; $j<scalar(@$response); $j++) {
395: my ($award,$msg) = &caparesponse_check($answer->[$j],
396: $response->[$j]);
397: push(@awards,$award);
398: push(@msgs, $msg);
399: }
400: ($award,$msg) =
401: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
402: }
403: $memoized{$key} = [$award,$msg];
1.117 albertel 404: }
405: }
406: }
407:
1.116 albertel 408: my ($final_award,$final_msg);
1.119 albertel 409: &init_permutation(scalar(@$responses),
1.116 albertel 410: $LONCAPA::CAPAresponse_answer->{'type'});
1.117 albertel 411:
1.118 albertel 412: # possible FIXMEs
413: # - significant time is spent calling non-safe space routine
414: # from safe space
415: # - early outs could be possible with classifying awards is to stratas
416: # and stopping as so as hitting the top strata
417: # - some early outs also might be possible with check ing the
418: # memoized hash of results (is correct even possible? etc.)
419:
1.117 albertel 420: my (@final_awards,@final_msg);
1.116 albertel 421: while( &get_permutations_left() ) {
1.117 albertel 422: my $order = &get_next_permutation();
1.116 albertel 423: my (@awards, @msgs, $i);
424: foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
1.119 albertel 425: my $key = "$thisanswer\0".$responses->[$order->[$i]];
1.117 albertel 426: push(@awards,$memoized{$key}[0]);
427: push(@msgs,$memoized{$key}[1]);
1.116 albertel 428: $i++;
1.119 albertel 429:
1.116 albertel 430: }
1.119 albertel 431: &LONCAPA_INTERNAL_DEBUG(" all awards ".join(':',@awards));
432:
1.116 albertel 433: my ($possible_award,$possible_msg) =
434: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
1.119 albertel 435: &LONCAPA_INTERNAL_DEBUG(" pos awards ".$possible_award);
1.117 albertel 436: push(@final_awards,$possible_award);
437: push(@final_msg,$possible_msg);
1.73 albertel 438: }
1.117 albertel 439:
1.119 albertel 440: &LONCAPA_INTERNAL_DEBUG(" all final_awards ".join(':',@final_awards));
1.117 albertel 441: my ($final_award,$final_msg) =
442: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@final_awards,\@final_msg,undef,1);
1.116 albertel 443: return ($final_award,$final_msg);
1.7 albertel 444: }
445:
1.124 www 446: sub cas {
1.137 www 447: my ($system,$input,$library)=@_;
1.124 www 448: my $output;
1.145 www 449: my $dump;
1.124 www 450: if ($system eq 'maxima') {
1.137 www 451: $output=&maxima_eval($input,$library);
1.144 www 452: } elsif ($system eq 'R') {
1.145 www 453: ($output,$dump)=&r_eval($input,$library,0);
1.137 www 454: } else {
455: $output='Error: unrecognized CAS';
1.124 www 456: }
457: return $output;
458: }
459:
1.145 www 460: sub cas_hashref {
461: my ($system,$input,$library)=@_;
462: if ($system eq 'maxima') {
463: return 'Error: unsupported CAS';
464: } elsif ($system eq 'R') {
465: return &r_eval($input,$library,1);
466: } else {
467: return 'Error: unrecognized CAS';
468: }
469: }
470:
471: #
472: # cas_hashref_entry takes a list of indices and gets the entry in a hash generated by Rreturn.
473: # Call: cas_hashref_entry(Rvalue, index1, index2, ...) where Rvalue is a hash returned by Rreturn.
474: # Rentry will return the first scalar value it encounters (ignoring excess indices).
475: # If an invalid key is given, it returns undef.
476: #
477: sub cas_hashref_entry {
478: return &Rentry(@_);
479: }
480:
481: #
482: # cas_hashref_array takes a list of indices and gets a column array from a hash generated by Rreturn.
483: # Call: cas_hashref_array(Rvalue, index1, index2, ...) where Rvalue is a hash returned by Rreturn.
484: # If an invalid key is given, it returns undef.
485: #
486: sub cas_hashref_array {
487: return &Rarray(@_);
488: }
489:
1.4 albertel 490: sub tex {
1.73 albertel 491: if ( $external::target eq "tex" ) {
492: return $_[0];
493: } else {
494: return $_[1];
495: }
1.4 albertel 496: }
497:
1.24 ng 498: sub var_in_tex {
1.73 albertel 499: if ( $external::target eq "tex" ) {
500: return $_[0];
501: } else {
502: return "";
503: }
1.24 ng 504: }
505:
1.4 albertel 506: sub web {
1.73 albertel 507: if ( $external::target eq "tex" ) {
508: return $_[1];
1.26 ng 509: } else {
1.73 albertel 510: if ( $external::target eq "web" || $external::target eq "answer") {
511: return $_[2];
512: } else {
513: return $_[0];
514: }
1.4 albertel 515: }
516: }
517:
1.24 ng 518: sub html {
1.73 albertel 519: if ( $external::target eq "web" ) {
520: return shift;
521: }
1.24 ng 522: }
523:
1.1 harris41 524: sub hinton {
1.73 albertel 525: return 0;
1.1 harris41 526: }
527:
528: sub random {
1.61 albertel 529: my ($start,$end,$step)=@_;
530: if ( ! $hidden::RANDOMINIT ) {
531: if ($external::randomseed == 0) { $external::randomseed=1; }
532: if ($external::randomseed =~/,/) {
1.84 albertel 533: my ($num1,$num2)=split(/,/,$external::randomseed);
534: &random_set_seed(1,abs($num1));
535: } elsif ($external::randomseed =~/:/) {
536: my ($num1,$num2)=split(/:/,$external::randomseed);
1.61 albertel 537: &random_set_seed(abs($num1),abs($num2));
538: } else {
539: &random_set_seed(1,int(abs($external::randomseed)));
540: }
541: &math_random_uniform();
542: $hidden::RANDOMINIT=1;
543: }
544: if (!defined($step)) { $step=1; }
545: my $num=1+int(($end-$start)/$step);
546: my $result=$start + int(&math_random_uniform() * $num)*$step;
547: return $result;
1.1 harris41 548: }
549:
1.26 ng 550: sub random_normal {
1.73 albertel 551: my ($item_cnt,$seed,$av,$std_dev) = @_;
552: my @oldseed=&random_get_seed();
553: my @retArray;
554: &random_set_seed_from_phrase($seed);
555: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
556: &random_set_seed(@oldseed);
557: return @retArray;
1.26 ng 558: }
559:
560: sub random_beta {
1.73 albertel 561: my ($item_cnt,$seed,$aa,$bb) = @_;
562: my @oldseed=&random_get_seed();
563: my @retArray;
564: &random_set_seed_from_phrase($seed);
565: @retArray=&math_random_beta($item_cnt,$aa,$bb);
566: &random_set_seed(@oldseed);
567: return @retArray;
1.26 ng 568: }
569:
570: sub random_gamma {
1.73 albertel 571: my ($item_cnt,$seed,$a,$r) = @_;
572: my @oldseed=&random_get_seed();
573: my @retArray;
574: &random_set_seed_from_phrase($seed);
575: @retArray=&math_random_gamma($item_cnt,$a,$r);
576: &random_set_seed(@oldseed);
577: return @retArray;
1.26 ng 578: }
579:
580: sub random_exponential {
1.73 albertel 581: my ($item_cnt,$seed,$av) = @_;
582: my @oldseed=&random_get_seed();
583: my @retArray;
584: &random_set_seed_from_phrase($seed);
585: @retArray=&math_random_exponential($item_cnt,$av);
586: &random_set_seed(@oldseed);
587: return @retArray;
1.26 ng 588: }
589:
590: sub random_poisson {
1.73 albertel 591: my ($item_cnt,$seed,$mu) = @_;
592: my @oldseed=&random_get_seed();
593: my @retArray;
594: &random_set_seed_from_phrase($seed);
595: @retArray=&math_random_poisson($item_cnt,$mu);
596: &random_set_seed(@oldseed);
597: return @retArray;
1.26 ng 598: }
599:
600: sub random_chi {
1.73 albertel 601: my ($item_cnt,$seed,$df) = @_;
602: my @oldseed=&random_get_seed();
603: my @retArray;
604: &random_set_seed_from_phrase($seed);
605: @retArray=&math_random_chi_square($item_cnt,$df);
606: &random_set_seed(@oldseed);
607: return @retArray;
1.26 ng 608: }
609:
610: sub random_noncentral_chi {
1.73 albertel 611: my ($item_cnt,$seed,$df,$nonc) = @_;
612: my @oldseed=&random_get_seed();
613: my @retArray;
614: &random_set_seed_from_phrase($seed);
615: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
616: &random_set_seed(@oldseed);
617: return @retArray;
1.26 ng 618: }
619:
620: sub random_f {
1.73 albertel 621: my ($item_cnt,$seed,$dfn,$dfd) = @_;
622: my @oldseed=&random_get_seed();
623: my @retArray;
624: &random_set_seed_from_phrase($seed);
625: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
626: &random_set_seed(@oldseed);
627: return @retArray;
1.26 ng 628: }
629:
630: sub random_noncentral_f {
1.73 albertel 631: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
632: my @oldseed=&random_get_seed();
633: my @retArray;
634: &random_set_seed_from_phrase($seed);
635: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
636: &random_set_seed(@oldseed);
637: return @retArray;
1.26 ng 638: }
639:
640: sub random_multivariate_normal {
1.73 albertel 641: my ($item_cnt,$seed,$mean,$covar) = @_;
642: my @oldseed=&random_get_seed();
643: &random_set_seed_from_phrase($seed);
1.87 albertel 644: my @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.73 albertel 645: &random_set_seed(@oldseed);
646: return @retArray;
1.26 ng 647: }
648:
649: sub random_multinomial {
1.73 albertel 650: my ($item_cnt,$seed,@p) = @_;
651: my @oldseed=&random_get_seed();
652: my @retArray;
653: &random_set_seed_from_phrase($seed);
1.87 albertel 654: my @retArray=&math_random_multinomial($item_cnt,@p);
1.73 albertel 655: &random_set_seed(@oldseed);
656: return @retArray;
1.26 ng 657: }
658:
659: sub random_permutation {
1.73 albertel 660: my ($seed,@inArray) = @_;
661: my @oldseed=&random_get_seed();
662: my @retArray;
663: &random_set_seed_from_phrase($seed);
664: @retArray=&math_random_permutation(@inArray);
665: &random_set_seed(@oldseed);
666: return @retArray;
1.26 ng 667: }
668:
669: sub random_uniform {
1.73 albertel 670: my ($item_cnt,$seed,$low,$high) = @_;
671: my @oldseed=&random_get_seed();
672: my @retArray;
673: &random_set_seed_from_phrase($seed);
674: @retArray=&math_random_uniform($item_cnt,$low,$high);
675: &random_set_seed(@oldseed);
676: return @retArray;
1.26 ng 677: }
678:
679: sub random_uniform_integer {
1.73 albertel 680: my ($item_cnt,$seed,$low,$high) = @_;
681: my @oldseed=&random_get_seed();
682: my @retArray;
683: &random_set_seed_from_phrase($seed);
684: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
685: &random_set_seed(@oldseed);
686: return @retArray;
1.26 ng 687: }
688:
689: sub random_binomial {
1.73 albertel 690: my ($item_cnt,$seed,$nt,$p) = @_;
691: my @oldseed=&random_get_seed();
692: my @retArray;
693: &random_set_seed_from_phrase($seed);
694: @retArray=&math_random_binomial($item_cnt,$nt,$p);
695: &random_set_seed(@oldseed);
696: return @retArray;
1.26 ng 697: }
698:
699: sub random_negative_binomial {
1.73 albertel 700: my ($item_cnt,$seed,$ne,$p) = @_;
701: my @oldseed=&random_get_seed();
702: my @retArray;
703: &random_set_seed_from_phrase($seed);
704: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
705: &random_set_seed(@oldseed);
706: return @retArray;
1.26 ng 707: }
708:
1.103 albertel 709: sub abs { CORE::abs(shift) }
710: sub sin { CORE::sin(shift) }
711: sub cos { CORE::cos(shift) }
712: sub exp { CORE::exp(shift) }
713: sub int { CORE::int(shift) }
714: sub log { CORE::log(shift) }
715: sub atan2 { CORE::atan2($_[0],$_[1]) }
716: sub sqrt { CORE::sqrt(shift) }
1.23 ng 717:
1.59 albertel 718: sub tan { CORE::sin($_[0]) / CORE::cos($_[0]) }
1.21 harris41 719: #sub atan { atan2($_[0], 1); }
720: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
721: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.22 ng 722:
1.59 albertel 723: sub log10 { CORE::log($_[0])/CORE::log(10); }
1.22 ng 724:
1.20 harris41 725: sub factorial {
1.59 albertel 726: my $input = CORE::int(shift);
1.20 harris41 727: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
728: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
729: return 1 if $input == 0;
730: my $result = 1;
731: for (my $i=2; $i<=$input; $i++) { $result *= $i }
732: return $result;
733: }
734:
735: sub sgn {
736: return -1 if $_[0] < 0;
737: return 0 if $_[0] == 0;
738: return 1 if $_[0] > 0;
739: }
740:
741: sub min {
742: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
743: return shift @sorted;
744: }
745:
746: sub max {
747: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
748: return pop @sorted;
749: }
1.1 harris41 750:
1.20 harris41 751: sub roundto {
752: my ($input,$n) = @_;
753: return sprintf('%.'.$n.'f',$input);
754: }
755:
756: sub to_string {
757: my ($input,$n) = @_;
1.26 ng 758: return sprintf($input) if $n eq "";
759: $n = '.'.$n if $n !~ /^\./;
1.20 harris41 760: return sprintf('%'.$n,$input) if $n ne "";
761: }
762:
763: sub sub_string {
764: my ($str,$start,$len) = @_;
765: return substr($str,$start-1,$len);
766: }
1.1 harris41 767:
768: sub pow {return $_[0] ** $_[1]; }
1.59 albertel 769: sub ceil {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
770: sub floor {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
1.27 ng 771: #sub floor {return int($_[0]); }
1.1 harris41 772:
1.2 albertel 773: sub format {
1.73 albertel 774: my ($value,$fmt)=@_;
1.81 albertel 775: my ($dollarmode,$commamode,$alwaysperiod,$options);
776: if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; }
777: #if ($options =~ /\$/) { $dollamode=1; }
778: #if ($options =~ /,/) { $commamode=1; }
1.82 albertel 779: if ($options =~ /\./) { $alwaysperiod=1; }
1.99 ng 780: my $result;
1.97 albertel 781: if ($fmt=~/s$/i) {
782: $result=&format_significant_figures($value,$fmt);
783: } else {
784: $fmt=~s/e/E/g;
1.99 ng 785: $result=sprintf('%.'.$fmt,$value);
1.97 albertel 786: if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
787: $result=~s/(E[+-]*)0/$1/;
788: }
1.81 albertel 789: #if ($dollarmode) {$result=&dollarformat($result);}
790: #if ($commamode) {$result=&commaformat($result);}
1.73 albertel 791: return $result;
1.46 albertel 792: }
793:
1.75 albertel 794: sub chemparse {
795: my ($reaction) = @_;
1.96 albertel 796: my @tokens = split(/(\s\+|\->|<=>|<\-|\.)/,$reaction);
1.75 albertel 797: my $formula = '';
798: foreach my $token (@tokens) {
799: if ($token eq '->' ) {
800: $formula .= '<m>\ensuremath{\rightarrow}</m> ';
801: next;
802: }
1.96 albertel 803: if ($token eq '<-' ) {
804: $formula .= '<m>\ensuremath{\leftarrow}</m> ';
805: next;
806: }
1.75 albertel 807: if ($token eq '<=>') {
808: if ($external::target eq 'web' &&
809: &EXT('request.browser.unicode')) {
1.76 albertel 810: $formula .= '⇌ ';
1.75 albertel 811: } else {
812: $formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
1.95 albertel 813: '<=> ');
1.75 albertel 814: }
815: next;
816: }
1.96 albertel 817: if ($token eq '.') {
818: $formula =~ s/(\ \;| )$//;
819: $formula .= '·';
820: next;
821: }
822: $token =~ /^\s*([\d|\/]*(?:&frac\d\d)?)(.*)/;
1.90 albertel 823: $formula .= $1 if ($1 ne '1'); # stoichiometric coefficient
1.75 albertel 824:
825: my $molecule = $2;
826: # subscripts
1.78 albertel 827: $molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
1.75 albertel 828: # superscripts
829: $molecule =~ s|\^(\d*[+\-]*)|<sup>$1</sup>|g;
830: # strip whitespace
831: $molecule =~ s/\s*//g;
832: # forced space
833: $molecule =~ s/_/ /g;
1.96 albertel 834: $molecule =~ s/-/−/g;
1.75 albertel 835: $formula .= $molecule.' ';
836: }
837: # get rid of trailing space
1.87 albertel 838: $formula =~ s/(\ \;| )$//;
1.75 albertel 839: return &xmlparse($formula);
840: }
841:
1.46 albertel 842: sub prettyprint {
1.73 albertel 843: my ($value,$fmt,$target)=@_;
844: my $result;
845: if (!$target) { $target = $external::target; }
1.75 albertel 846: if ($fmt =~ /chem/i) { return(&chemparse($value)); }
1.81 albertel 847: my ($dollarmode,$commamode,$alwaysperiod,$options);
848: if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; }
1.86 albertel 849: if ($options =~ /\$/) { $dollarmode=1; }
1.81 albertel 850: if ($options =~ /,/) { $commamode=1; }
851: if ($options =~ /\./) { $alwaysperiod=1; }
1.97 albertel 852: if ($fmt=~/s$/i) {
853: $value=&format_significant_figures($value,$fmt);
854: } elsif ($fmt) {
855: $value=sprintf('%.'.$fmt,$value);
856: }
1.81 albertel 857: if ($alwaysperiod && $fmt eq '0f') {
858: if ($target eq 'tex') {
859: $value .='\\ensuremath{.}';
860: } else {
861: $value .='.';
862: }
863: }
1.73 albertel 864: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
865: my $frac=$1;
866: if ($dollarmode) { $frac=&dollarformat($frac); }
1.80 albertel 867: if ($commamode) { $frac=&commaformat($frac); }
1.73 albertel 868: my $exponent=$2;
869: $exponent=~s/^\+0*//;
870: $exponent=~s/^-0*/-/;
871: $exponent=~s/^-0*/-/;
872: if ($exponent eq '-') { undef($exponent); }
873: if ($exponent) {
874: if ($target eq 'web') {
875: $result=$frac.'×10<sup>'.$exponent.'</sup>';
876: } elsif ($target eq 'tex') {
877: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
878: } else {
879: $result=$value;
880: }
881: } else {
882: $result=$frac;
883: }
884: } else {
1.48 albertel 885: $result=$value;
1.86 albertel 886: if ($dollarmode) { $result=&dollarformat($result,$target); }
887: elsif ($commamode) { $result=&commaformat($result,$target); }
1.46 albertel 888: }
1.73 albertel 889: return $result;
1.48 albertel 890: }
891:
1.80 albertel 892: sub commaformat {
1.73 albertel 893: my ($number,$target) = @_;
894: if ($number =~ /\./) {
1.102 albertel 895: while ($number =~ /([^0-9]*)([0-9]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
896: $number = $1.$2.','.$3.$4;
1.73 albertel 897: }
898: } else {
1.102 albertel 899: while ($number =~ /^([^0-9]*)([0-9]+)([^,][^,][^,])([,0-9]*)$/) {
900: $number = $1.$2.','.$3.$4;
1.73 albertel 901: }
902: }
1.80 albertel 903: return $number;
904: }
905:
906: sub dollarformat {
907: my ($number,$target) = @_;
908: if (!$target) { $target = $external::target; }
909: $number=&commaformat($number,$target);
1.73 albertel 910: if ($target eq 'tex') {
911: $number='\$'.$number; #' stupid emacs
912: } else {
913: $number='$'.$number; #' stupid emacs
914: }
915: return $number;
1.2 albertel 916: }
1.5 albertel 917:
1.97 albertel 918: # format of form ns or nS where n is an integer
919: sub format_significant_figures {
920: my ($number,$format) = @_;
921: return '0' if ($number == 0);
922: # extract number of significant figures needed
923: my ($sig) = ($format =~ /(\d+)s/i);
924: # arbitrary choice - suggestions ?? or throw error message?
925: $sig = 3 if ($sig eq '');
926: # save the minus sign
927: my $sign = ($number < 0) ? '-' : '';
928: $number = abs($number);
929: # needed to correct for a number greater than 1 (or
930: my $power = ($number < 1) ? 0 : 1;
931: # could round up. Take the integer part of log10.
932: my $x10 = int(log($number)/log(10));
933: # find number with values left of decimal pt = # of sign figs.
934: my $xsig = $number*10**($sig-$x10-$power);
935: # get just digits left of decimal pt - also rounds off correctly
936: my $xint = sprintf('%.0f',$xsig);
937: # save any trailing zero's
938: my ($zeros) = ($xint =~ /(0+)$/);
939: # return number to original magnitude
940: my $numSig = $xint*10**($x10-$sig+$power);
941: # insert trailing zero's if have decimal point
942: $numSig =~ s/^(\d+)\.(\d+)(\e?(.*)?)$/$1\.$2$zeros$3/;
1.98 albertel 943: # put a decimal pt for number ending with 0 and length = # of sig fig
944: $numSig.='.' if (length($numSig) == $sig && $numSig =~ /0$/);
945: if (length($numSig) < $sig) {
946: $numSig.='.'.substr($zeros,0,($sig-length($numSig)));
947: }
1.97 albertel 948: # return number with sign
949: return $sign.$numSig;
950:
951: }
952:
1.5 albertel 953: sub map {
1.27 ng 954: my ($phrase,$dest,$source)=@_;
1.51 albertel 955: my @oldseed=&random_get_seed();
1.27 ng 956: my @seed = &random_seed_from_phrase($phrase);
957: &random_set_seed(@seed);
958: my $destct = scalar(@$dest);
1.28 ng 959: if (!$source) {
960: my @output;
961: my @idx = &math_random_permuted_index($destct);
962: my $ctr = 0;
963: while ($ctr < $destct) {
964: $output[$ctr] = $$dest[$idx[$ctr]];
1.27 ng 965: $ctr++;
1.28 ng 966: }
1.51 albertel 967: &random_set_seed(@oldseed);
1.28 ng 968: return @output;
1.27 ng 969: } else {
1.28 ng 970: my $num = scalar(@$source);
971: my @idx = &math_random_permuted_index($num);
972: my $ctr = 0;
973: my $tot = $num;
974: $tot = $destct if $destct < $num;
975: if (ref($$dest[0])) {
976: while ($ctr < $tot) {
977: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
978: $ctr++;
979: }
980: } else {
981: while ($ctr < $tot) {
982: $$dest[$ctr] = $$source[$idx[$ctr]];
983: $ctr++;
984: }
985: }
1.27 ng 986: }
1.56 albertel 987: &random_set_seed(@oldseed);
1.51 albertel 988: return '';
1.27 ng 989: }
990:
991: sub rmap {
992: my ($phrase,$dest,$source)=@_;
1.51 albertel 993: my @oldseed=&random_get_seed();
1.27 ng 994: my @seed = &random_seed_from_phrase($phrase);
995: &random_set_seed(@seed);
996: my $destct = scalar(@$dest);
1.28 ng 997: if (!$source) {
998: my @idx = &math_random_permuted_index($destct);
999: my $ctr = 0;
1000: my @r_idx;
1001: while ($ctr < $destct) {
1002: $r_idx[$idx[$ctr]] = $ctr;
1003: $ctr++;
1004: }
1005: my @output;
1006: $ctr = 0;
1007: while ($ctr < $destct) {
1008: $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27 ng 1009: $ctr++;
1.28 ng 1010: }
1.51 albertel 1011: &random_set_seed(@oldseed);
1.28 ng 1012: return @output;
1.27 ng 1013: } else {
1.28 ng 1014: my $num = scalar(@$source);
1015: my @idx = &math_random_permuted_index($num);
1016: my $ctr = 0;
1017: my $tot = $num;
1018: $tot = $destct if $destct < $num;
1019: my @r_idx;
1.27 ng 1020: while ($ctr < $tot) {
1.28 ng 1021: $r_idx[$idx[$ctr]] = $ctr;
1.27 ng 1022: $ctr++;
1.28 ng 1023: }
1024: $ctr = 0;
1025: if (ref($$dest[0])) {
1026: while ($ctr < $tot) {
1027: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
1028: $ctr++;
1029: }
1030: } else {
1031: while ($ctr < $tot) {
1032: $$dest[$ctr] = $$source[$r_idx[$ctr]];
1033: $ctr++;
1034: }
1035: }
1.6 albertel 1036: }
1.51 albertel 1037: &random_set_seed(@oldseed);
1038: return '';
1.5 albertel 1039: }
1.22 ng 1040:
1.23 ng 1041: sub capa_id { return }
1042:
1043: sub problem { return }
1044:
1.22 ng 1045: sub name{
1.73 albertel 1046: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
1047: $fullname = "" if $fullname eq ", ";
1048: $fullname =~ s/\%2d/-/g;
1049: return $fullname;
1.22 ng 1050: }
1051:
1052: sub student_number {
1.73 albertel 1053: my $id = &EXT('environment.id');
1054: $id = '' if $id eq "";
1055: return $id;
1.22 ng 1056: }
1057:
1058: sub class {
1.73 albertel 1059: my $course = &EXT('course.description');
1060: $course = '' if $course eq "";
1061: return $course;
1.22 ng 1062: }
1063:
1.112 www 1064: sub firstname {
1065: my $firstname = &EXT('environment.firstname');
1066: $firstname = '' if $firstname eq "";
1067: return $firstname;
1068: }
1069:
1070: sub lastname {
1071: my $lastname = &EXT('environment.lastname');
1072: $lastname = '' if $lastname eq "";
1073: return $lastname;
1074: }
1075:
1.22 ng 1076: sub sec {
1.73 albertel 1077: my $sec = &EXT('request.course.sec');
1078: $sec = '' if $sec eq "";
1079: return $sec;
1.22 ng 1080: }
1081:
1.136 www 1082: sub submission {
1083: my ($partid,$responseid,$subnumber)=@_;
1084: my $sub='';
1085: if ($subnumber) { $sub=$subnumber.':'; }
1086: return &EXT('user.resource.'.$sub.'resource.'.$partid.'.'.$responseid.'.submission');
1087: }
1088:
1089: sub currentpart {
1090: return $external::part;
1091: }
1092:
1.135 www 1093: sub eval_time {
1094: my ($timestamp)=@_;
1095: unless ($timestamp) { return ''; }
1096: return &locallocaltime($timestamp);
1097: }
1098:
1.23 ng 1099: sub open_date {
1.134 www 1100: my ($partid)=@_;
1101: unless ($partid) { $partid=0; }
1.135 www 1102: return &eval_time(&EXT('resource.'.$partid.'.opendate'));
1.23 ng 1103: }
1104:
1.134 www 1105: sub due_date {
1106: my ($partid)=@_;
1107: unless ($partid) { $partid=0; }
1.135 www 1108: return &eval_time(&EXT('resource.'.$partid.'.duedate'));
1.23 ng 1109: }
1110:
1111: sub answer_date {
1.134 www 1112: my ($partid)=@_;
1113: unless ($partid) { $partid=0; }
1.135 www 1114: return &eval_time(&EXT('resource.'.$partid.'.answerdate'));
1.24 ng 1115: }
1116:
1.136 www 1117: sub open_date_epoch {
1118: my ($partid)=@_;
1119: unless ($partid) { $partid=0; }
1120: return &EXT('resource.'.$partid.'.opendate');
1121: }
1122:
1123: sub due_date_epoch {
1124: my ($partid)=@_;
1125: unless ($partid) { $partid=0; }
1126: return &EXT('resource.'.$partid.'.duedate');
1127: }
1128:
1129: sub answer_date_epoch {
1130: my ($partid)=@_;
1131: unless ($partid) { $partid=0; }
1132: return &EXT('resource.'.$partid.'.answerdate');
1133: }
1134:
1.24 ng 1135: sub array_moments {
1.73 albertel 1136: my @input=@_;
1137: my (@output,$N);
1138: $N=scalar (@input);
1139: $output[0]=$N;
1140: if ($N <= 1) {
1141: $output[1]=$input[0];
1142: $output[1]="Input array not defined" if ($N == 0);
1143: $output[2]="variance undefined for N<=1";
1144: $output[3]="skewness undefined for N<=1";
1145: $output[4]="kurtosis undefined for N<=1";
1146: return @output;
1147: }
1148: my $sum=0;
1149: foreach my $line (@input) {
1150: $sum+=$line;
1151: }
1152: $output[1] = $sum/$N;
1153: my ($x,$sdev,$var,$skew,$kurt) = 0;
1154: foreach my $line (@input) {
1155: $x=$line-$output[1];
1156: $var+=$x**2;
1157: $skew+=$x**3;
1158: $kurt+=$x**4;
1159: }
1160: $output[2]=$var/($N-1);
1161: $sdev=CORE::sqrt($output[2]);
1162: if ($sdev == 0) {
1163: $output[3]="inf-variance=0";
1164: $output[4]="inf-variance=0";
1165: return @output;
1166: }
1167: $output[3]=$skew/($sdev**3*$N);
1168: $output[4]=$kurt/($sdev**4*$N)-3;
1.24 ng 1169: return @output;
1170: }
1.5 albertel 1171:
1172: sub choose {
1.73 albertel 1173: my $num = $_[0];
1174: return $_[$num];
1.5 albertel 1175: }
1.23 ng 1176:
1.101 albertel 1177: #&sum1(1,$x,sub { &sum1($_[0],2*$_[0], sub { fact($_[0])**2 })});
1178: #sub sum1 {
1179: # my ($start,$end,$sub)=@_;
1180: # my $sum=0;
1181: # for (my $i=$start;$i<=$end;$i++) {
1182: # $sum+=&$sub($i);
1183: # }
1184: # return $sum
1185: #}
1186:
1187: #&sum2('a',1,$x,'&sum2(\'b\',$a,2*$a, \'&factorial($b)**2\')');
1188: #sub sum2 {
1189: # my ($varname,$start,$end,$line)=@_;
1190: # my $sum=0;
1191: # for (my $i=$start;$i<=$end;$i++) {
1192: # my $func=sub {
1193: # eval("\$".$varname."=$i");
1194: # eval($line);
1195: # };
1196: # $sum+=&$func($i);
1197: # }
1198: # return $sum
1199: #}
1200:
1.49 albertel 1201: # expiremental idea
1202: sub proper_path {
1.73 albertel 1203: my ($path)=@_;
1204: if ( $external::target eq "tex" ) {
1205: return '/home/httpd/html'.$path;
1206: } else {
1207: return $path;
1208: }
1.49 albertel 1209: }
1.23 ng 1210:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>