Annotation of loncom/homework/default_homework.lcpm, revision 1.69
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.69 ! albertel 4: # $Id: default_homework.lcpm,v 1.68 2003/10/23 13:56:07 ng 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: #
29: # Guy Albertelli
30: #
31: # 05/25/2001 H. K. Ng
1.24 ng 32: # 05/31/2001 H. K. Ng
1.44 matthew 33: # 12/21/2001 Matthew
1.20 harris41 34: #
1.25 albertel 35: #init some globals
1.38 albertel 36: $hidden::RANDOMINIT=0;
1.22 ng 37: $pi=atan2(1,1)*4;
38: $rad2deg=180.0/$pi;
39: $deg2rad=$pi/180.0;
1.44 matthew 40: $"=' ';
1.3 albertel 41:
1.7 albertel 42: sub caparesponse_check {
1.36 albertel 43: #not properly used yet: calc
44: #not to be used: $ans_fmt
1.64 albertel 45: my ($type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
46: eval $_[2].
47: ';return ($type,$tol,$sig,$ans_fmt,$unit,$calc,$samples);';
1.36 albertel 48:
1.10 albertel 49: my $tol_type=''; # gets it's value from whether tol has a % or not done
1.19 albertel 50: my $sig_lbound=''; #done
51: my $sig_ubound=''; #done
1.64 albertel 52: my ($answer,$response,$expr)=@_;
1.31 albertel 53:
1.32 albertel 54:
1.8 albertel 55: #type's definitons come from capaParser.h
1.31 albertel 56: my $message='';
1.32 albertel 57: #remove leading and trailing whitespace
58: if ($response=~ /^\s|\s$/) {
59: $response=~ s:^\s+|\s+$::g;
1.52 albertel 60: $message .="Removed ws now :$response:\n";
1.32 albertel 61: } else {
1.52 albertel 62: $message .="no ws in :$response:\n";
1.32 albertel 63: }
1.62 albertel 64:
65: if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
1.32 albertel 66:
1.8 albertel 67: if ($type eq '' ) {
1.52 albertel 68: $message .= "Didn't find a type :$type:$expr: defaulting\n";
1.8 albertel 69: if ( $answer eq ($answer *1.0)) { $type = 2;
70: } else { $type = 3; }
71: } else {
72: if ($type eq 'cs') { $type = 4;
73: } elsif ($type eq 'ci') { $type = 3;
74: } elsif ($type eq 'mc') { $type = 5;
75: } elsif ($type eq 'fml') { $type = 8;
1.9 albertel 76: } elsif ($type eq 'subj') { $type = 7;
1.41 albertel 77: } elsif ($type eq 'float') { $type = 2;
78: } elsif ($type eq 'int') { $type = 1;
1.9 albertel 79: } else { return "ERROR: Unknown type of answer: $type" }
1.8 albertel 80: }
81:
1.39 albertel 82: my $points;
83: my $id_list;
84: #formula type setup the sample points
85: if ($type eq '8') {
86: ($id_list,$points)=split(/@/,$samples);
1.61 albertel 87: $message.="Found :$id_list:$points: points in $samples\n";
1.39 albertel 88: }
1.10 albertel 89: if ($tol eq '') {
90: $tol=0.0;
91: $tol_type=1; #TOL_ABSOLUTE
92: } else {
93: if ($tol =~ /%$/) {
1.12 albertel 94: chop $tol;
1.10 albertel 95: $tol_type=2; #TOL_PERCENTAGE
96: } else {
97: $tol_type=1; #TOL_ABSOLUTE
98: }
99: }
1.12 albertel 100:
101: if ($sig eq '') {
102: $sig_lbound = 0; #SIG_LB_DEFAULT
103: $sig_ubound =15; #SIG_UB_DEFAULT
104: } else {
105: ($sig_lbound,$sig_ubound) = split /,/,$sig;
1.53 albertel 106: if (!defined($sig_lbound)) {
1.45 albertel 107: $sig_lbound = 0; #SIG_LB_DEFAULT
108: $sig_ubound =15; #SIG_UB_DEFAULT
109: }
1.53 albertel 110: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1.12 albertel 111: }
1.7 albertel 112: my $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.10 albertel 113: $tol_type,$tol,
1.7 albertel 114: $sig_lbound,$sig_ubound,
1.39 albertel 115: $ans_fmt,$unit,$calc,$id_list,
116: $points,$external::randomseed);
1.9 albertel 117:
118: if ($result == '1') { $result='EXACT_ANS'; }
119: elsif ($result == '2') { $result='APPROX_ANS'; }
120: elsif ($result == '3') { $result='SIG_FAIL'; }
121: elsif ($result == '4') { $result='UNIT_FAIL'; }
122: elsif ($result == '5') { $result='NO_UNIT'; }
123: elsif ($result == '6') { $result='UNIT_OK'; }
124: elsif ($result == '7') { $result='INCORRECT'; }
125: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
126: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
127: elsif ($result =='10') { $result='SUB_RECORDED'; }
128: elsif ($result =='11') { $result='BAD_FORMULA'; }
129: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.13 albertel 130: else {$result = "ERROR: Unknown Result:$result:$@:";}
1.9 albertel 131:
1.52 albertel 132: return "$result:\nError $error:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message$expr";
1.14 albertel 133: }
134:
1.37 albertel 135: sub get_array_args {
136: my ($expr,$arg)=@_;
1.30 albertel 137: # do these first, because who knows what varname the instructor might have used
138: # but it probably isn't $CAPARESPONSE_CHECK_LIST_answer
1.63 albertel 139: my $CAPARESPONSE_CHECK_LIST_answer = eval $expr.';return $'.$arg; #' stupid emacs
1.64 albertel 140: my $GET_ARRAY_ARGS_result="expr is $expr\narg is $arg\nfirst answer guess is $CAPARESPONSE_CHECK_LIST_answer\n";
1.40 albertel 141: my @GET_ARRAY_ARGS_list;
1.39 albertel 142: if ($CAPARESPONSE_CHECK_LIST_answer =~ /^\s*[\$\@]/) {
1.40 albertel 143: (@GET_ARRAY_ARGS_list) = eval $CAPARESPONSE_CHECK_LIST_answer;
1.39 albertel 144: }
1.52 albertel 145: $GET_ARRAY_ARGS_result.="error:$@:\n";
1.31 albertel 146: # if the eval fails just use what is in the answer exactly
1.60 albertel 147: if (!(@GET_ARRAY_ARGS_list) || !defined($GET_ARRAY_ARGS_list[0])) {
1.52 albertel 148: $GET_ARRAY_ARGS_result.="list zero is undefined\n";
1.40 albertel 149: $GET_ARRAY_ARGS_list[0]=$CAPARESPONSE_CHECK_LIST_answer;
1.31 albertel 150: }
1.40 albertel 151: return $GET_ARRAY_ARGS_result,@GET_ARRAY_ARGS_list;
1.37 albertel 152: }
153:
154: sub caparesponse_check_list {
155: my ($response,$expr)=@_;
1.64 albertel 156: # $expr =~ s/\\/\\\\/g;
157: # $expr =~ s/\'/\\\'/g;
158: # my ($result,@list) = &get_array_args($expr,'answer');
159: # $expr =~ s/\\\'/\'/g;
160: # $expr =~ s/\\\\/\\/g;
161: my ($result,@list);
162: @list=@CAPARESPONSE_CHECK_LIST_answer;
1.15 albertel 163: my $aresult='';
1.14 albertel 164: my $current_answer;
1.39 albertel 165: my $answers=join(':',@list);
1.52 albertel 166: $result.="Got response :$answers:\n";
1.31 albertel 167: my @responselist;
1.64 albertel 168: my $type =eval $expr.';return $type;';
1.31 albertel 169: if ($type ne '' && $#list > 0) {
170: (@responselist)=split /,/,$response;
171: } else {
172: (@responselist)=($response);
173: }
1.15 albertel 174: my $unit='';
1.52 albertel 175: $result.="Initial final response :$responselist['-1']:\n";
1.64 albertel 176: if ($type eq '' || $type eq 'float') {
1.31 albertel 177: #for numerical problems split off the unit
178: if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
179: $responselist['-1']=$1;
180: $unit=$2;
181: }
1.15 albertel 182: }
1.52 albertel 183: $result.="Final final response :$responselist['-1']:\n";
184: $result.=":$#list: answers\n";
1.14 albertel 185: $unit=~s/\s//;
186: my $i=0;
187: my $awards='';
188: for ($i=0; $i<@list;$i++) {
1.52 albertel 189: $result.="trying answer :$list[$i]:\n";
1.57 albertel 190: my $thisanswer=$list[$i];
1.64 albertel 191: # $thisanswer=~ s/\\/\\\\/g;
192: # $thisanswer =~ s/\'/\\\'/g;
193: $result.="trying answer :$thisanswer:\n";
1.19 albertel 194: if ($unit eq '') {
1.64 albertel 195: $aresult=&caparesponse_check($thisanswer,$responselist[$i],
196: $expr);
1.19 albertel 197: } else {
1.64 albertel 198: $aresult=&caparesponse_check($thisanswer,$responselist[$i]." $unit",
199: $expr);
1.19 albertel 200: }
1.15 albertel 201: my ($temp)=split /:/, $aresult;
1.14 albertel 202: $awards.="$temp,";
1.15 albertel 203: $result.=$aresult;
1.14 albertel 204: }
205: chop $awards;
1.52 albertel 206: return "$awards:\n$result";
1.7 albertel 207: }
208:
1.4 albertel 209: sub tex {
210: if ( $external::target eq "tex" ) {
1.58 albertel 211: return $_[0];
1.4 albertel 212: } else {
1.58 albertel 213: return $_[1];
1.4 albertel 214: }
215: }
216:
1.24 ng 217: sub var_in_tex {
218: if ( $external::target eq "tex" ) {
1.58 albertel 219: return $_[0];
1.24 ng 220: } else {
221: return "";
222: }
223: }
224:
1.4 albertel 225: sub web {
226: if ( $external::target eq "tex" ) {
1.58 albertel 227: return $_[1];
1.4 albertel 228: } else {
1.43 albertel 229: if ( $external::target eq "web" || $external::target eq "answer") {
1.58 albertel 230: return $_[2];
1.26 ng 231: } else {
1.58 albertel 232: return $_[0];
1.4 albertel 233: }
234: }
235: }
236:
1.24 ng 237: sub html {
238: if ( $external::target eq "web" ) {
1.26 ng 239: return shift;
1.24 ng 240: }
241: }
242:
1.1 harris41 243: sub hinton {
244: return 0;
245: }
246:
247: sub random {
1.61 albertel 248: my ($start,$end,$step)=@_;
249: if ( ! $hidden::RANDOMINIT ) {
250: if ($external::randomseed == 0) { $external::randomseed=1; }
251: if ($external::randomseed =~/,/) {
252: my ($num1,$num2)=split(/,/,$seed);
253: &random_set_seed(abs($num1),abs($num2));
254: } else {
255: &random_set_seed(1,int(abs($external::randomseed)));
256: }
257: &math_random_uniform();
258: $hidden::RANDOMINIT=1;
259: }
260: if (!defined($step)) { $step=1; }
261: my $num=1+int(($end-$start)/$step);
262: my $result=$start + int(&math_random_uniform() * $num)*$step;
263: return $result;
1.1 harris41 264: }
265:
1.26 ng 266: sub random_normal {
267: my ($item_cnt,$seed,$av,$std_dev) = @_;
1.51 albertel 268: my @oldseed=&random_get_seed();
1.26 ng 269: my @retArray;
270: &random_set_seed_from_phrase($seed);
271: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
1.51 albertel 272: &random_set_seed(@oldseed);
1.26 ng 273: return @retArray;
274: }
275:
276: sub random_beta {
277: my ($item_cnt,$seed,$aa,$bb) = @_;
1.51 albertel 278: my @oldseed=&random_get_seed();
1.26 ng 279: my @retArray;
280: &random_set_seed_from_phrase($seed);
281: @retArray=&math_random_beta($item_cnt,$aa,$bb);
1.51 albertel 282: &random_set_seed(@oldseed);
1.26 ng 283: return @retArray;
284: }
285:
286: sub random_gamma {
287: my ($item_cnt,$seed,$a,$r) = @_;
1.51 albertel 288: my @oldseed=&random_get_seed();
1.26 ng 289: my @retArray;
290: &random_set_seed_from_phrase($seed);
291: @retArray=&math_random_gamma($item_cnt,$a,$r);
1.51 albertel 292: &random_set_seed(@oldseed);
1.26 ng 293: return @retArray;
294: }
295:
296: sub random_exponential {
297: my ($item_cnt,$seed,$av) = @_;
1.51 albertel 298: my @oldseed=&random_get_seed();
1.26 ng 299: my @retArray;
300: &random_set_seed_from_phrase($seed);
301: @retArray=&math_random_exponential($item_cnt,$av);
1.51 albertel 302: &random_set_seed(@oldseed);
1.26 ng 303: return @retArray;
304: }
305:
306: sub random_poisson {
307: my ($item_cnt,$seed,$mu) = @_;
1.51 albertel 308: my @oldseed=&random_get_seed();
1.26 ng 309: my @retArray;
310: &random_set_seed_from_phrase($seed);
311: @retArray=&math_random_poisson($item_cnt,$mu);
1.51 albertel 312: &random_set_seed(@oldseed);
1.26 ng 313: return @retArray;
314: }
315:
316: sub random_chi {
317: my ($item_cnt,$seed,$df) = @_;
1.51 albertel 318: my @oldseed=&random_get_seed();
1.26 ng 319: my @retArray;
320: &random_set_seed_from_phrase($seed);
321: @retArray=&math_random_chi_square($item_cnt,$df);
1.51 albertel 322: &random_set_seed(@oldseed);
1.26 ng 323: return @retArray;
324: }
325:
326: sub random_noncentral_chi {
327: my ($item_cnt,$seed,$df,$nonc) = @_;
1.51 albertel 328: my @oldseed=&random_get_seed();
1.26 ng 329: my @retArray;
330: &random_set_seed_from_phrase($seed);
331: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
1.51 albertel 332: &random_set_seed(@oldseed);
1.26 ng 333: return @retArray;
334: }
335:
336: sub random_f {
337: my ($item_cnt,$seed,$dfn,$dfd) = @_;
1.51 albertel 338: my @oldseed=&random_get_seed();
1.26 ng 339: my @retArray;
340: &random_set_seed_from_phrase($seed);
341: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
1.51 albertel 342: &random_set_seed(@oldseed);
1.26 ng 343: return @retArray;
344: }
345:
346: sub random_noncentral_f {
347: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
1.51 albertel 348: my @oldseed=&random_get_seed();
1.26 ng 349: my @retArray;
350: &random_set_seed_from_phrase($seed);
351: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
1.51 albertel 352: &random_set_seed(@oldseed);
1.26 ng 353: return @retArray;
354: }
355:
356: sub random_multivariate_normal {
1.33 ng 357: my ($item_cnt,$seed,$mean,$covar) = @_;
1.51 albertel 358: my @oldseed=&random_get_seed();
1.26 ng 359: &random_set_seed_from_phrase($seed);
1.33 ng 360: @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.51 albertel 361: &random_set_seed(@oldseed);
1.26 ng 362: return @retArray;
363: }
364:
365: sub random_multinomial {
366: my ($item_cnt,$seed,@p) = @_;
1.51 albertel 367: my @oldseed=&random_get_seed();
1.26 ng 368: my @retArray;
369: &random_set_seed_from_phrase($seed);
370: @retArray=&math_random_multinomial($item_cnt,@p);
1.51 albertel 371: &random_set_seed(@oldseed);
1.26 ng 372: return @retArray;
373: }
374:
375: sub random_permutation {
376: my ($seed,@inArray) = @_;
1.51 albertel 377: my @oldseed=&random_get_seed();
1.26 ng 378: my @retArray;
379: &random_set_seed_from_phrase($seed);
380: @retArray=&math_random_permutation(@inArray);
1.51 albertel 381: &random_set_seed(@oldseed);
1.26 ng 382: return @retArray;
383: }
384:
385: sub random_uniform {
386: my ($item_cnt,$seed,$low,$high) = @_;
1.51 albertel 387: my @oldseed=&random_get_seed();
1.26 ng 388: my @retArray;
389: &random_set_seed_from_phrase($seed);
390: @retArray=&math_random_uniform($item_cnt,$low,$high);
1.51 albertel 391: &random_set_seed(@oldseed);
1.26 ng 392: return @retArray;
393: }
394:
395: sub random_uniform_integer {
396: my ($item_cnt,$seed,$low,$high) = @_;
1.51 albertel 397: my @oldseed=&random_get_seed();
1.26 ng 398: my @retArray;
399: &random_set_seed_from_phrase($seed);
400: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
1.51 albertel 401: &random_set_seed(@oldseed);
1.26 ng 402: return @retArray;
403: }
404:
405: sub random_binomial {
406: my ($item_cnt,$seed,$nt,$p) = @_;
1.51 albertel 407: my @oldseed=&random_get_seed();
1.26 ng 408: my @retArray;
409: &random_set_seed_from_phrase($seed);
410: @retArray=&math_random_binomial($item_cnt,$nt,$p);
1.51 albertel 411: &random_set_seed(@oldseed);
1.26 ng 412: return @retArray;
413: }
414:
415: sub random_negative_binomial {
416: my ($item_cnt,$seed,$ne,$p) = @_;
1.51 albertel 417: my @oldseed=&random_get_seed();
1.26 ng 418: my @retArray;
419: &random_set_seed_from_phrase($seed);
420: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
1.51 albertel 421: &random_set_seed(@oldseed);
1.26 ng 422: return @retArray;
423: }
424:
1.23 ng 425: sub abs { abs(shift) }
426: sub sin { sin(shift) }
427: sub cos { cos(shift) }
428: sub exp { exp(shift) }
429: sub int { int(shift) }
430: sub log { log(shift) }
431: sub atan2 { atan2($_[0],$_[1]) }
432: sub sqrt { sqrt(shift) }
433:
1.59 albertel 434: sub tan { CORE::sin($_[0]) / CORE::cos($_[0]) }
1.21 harris41 435: #sub atan { atan2($_[0], 1); }
436: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
437: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.22 ng 438:
1.59 albertel 439: sub log10 { CORE::log($_[0])/CORE::log(10); }
1.22 ng 440:
1.20 harris41 441: sub factorial {
1.59 albertel 442: my $input = CORE::int(shift);
1.20 harris41 443: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
444: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
445: return 1 if $input == 0;
446: my $result = 1;
447: for (my $i=2; $i<=$input; $i++) { $result *= $i }
448: return $result;
449: }
450:
451: sub sgn {
452: return -1 if $_[0] < 0;
453: return 0 if $_[0] == 0;
454: return 1 if $_[0] > 0;
455: }
456:
457: sub min {
458: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
459: return shift @sorted;
460: }
461:
462: sub max {
463: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
464: return pop @sorted;
465: }
1.1 harris41 466:
1.20 harris41 467: sub roundto {
468: my ($input,$n) = @_;
469: return sprintf('%.'.$n.'f',$input);
470: }
471:
472: sub to_string {
473: my ($input,$n) = @_;
1.26 ng 474: return sprintf($input) if $n eq "";
475: $n = '.'.$n if $n !~ /^\./;
1.20 harris41 476: return sprintf('%'.$n,$input) if $n ne "";
477: }
478:
479: sub sub_string {
480: my ($str,$start,$len) = @_;
481: return substr($str,$start-1,$len);
482: }
1.1 harris41 483:
484: sub pow {return $_[0] ** $_[1]; }
1.59 albertel 485: sub ceil {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
486: sub floor {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
1.27 ng 487: #sub floor {return int($_[0]); }
1.1 harris41 488:
1.2 albertel 489: sub format {
490: my ($value,$fmt)=@_;
1.48 albertel 491: my $dollarmode;
492: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
1.67 albertel 493: $fmt=~s/e/E/g;
1.48 albertel 494: my $result=sprintf('%.'.$fmt,$value);
1.50 albertel 495: $result=~s/(E[+-]*)0/$1/;
1.48 albertel 496: if ($dollarmode) {$result=&dollarmode($result);}
497: return $result;
1.46 albertel 498: }
499:
500: sub prettyprint {
1.69 ! albertel 501: my ($value,$fmt,$target)=@_;
1.46 albertel 502: my $result;
1.48 albertel 503: my $dollarmode;
1.69 ! albertel 504: if (!$target) { $target = $external::target; }
1.48 albertel 505: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
1.46 albertel 506: if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
1.68 ng 507: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
1.46 albertel 508: my $frac=$1;
1.48 albertel 509: if ($dollarmode) { $frac=&dollarformat($frac); }
1.46 albertel 510: my $exponent=$2;
511: $exponent=~s/^\+0*//;
1.50 albertel 512: $exponent=~s/^-0*/-/;
1.65 albertel 513: $exponent=~s/^-0*/-/;
514: if ($exponent eq '-') { undef($exponent); }
1.48 albertel 515: if ($exponent) {
1.69 ! albertel 516: if ($target eq 'web') {
1.48 albertel 517: $result=$frac.'×10<sup>'.$exponent.'</sup>';
1.69 ! albertel 518: } elsif ($target eq 'tex') {
1.48 albertel 519: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
520: } else {
521: $result=$value;
522: }
1.46 albertel 523: } else {
1.48 albertel 524: $result=$frac;
1.46 albertel 525: }
526: } else {
527: $result=$value;
1.69 ! albertel 528: if ($dollarmode) { $result=&dollarformat($result,$target); }
1.46 albertel 529: }
530: return $result;
1.48 albertel 531: }
532:
533: sub dollarformat {
1.69 ! albertel 534: my ($number,$target) = @_;
! 535: if (!$target) { $target = $external::target; }
1.48 albertel 536: if ($number =~ /\./) {
537: while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
538: $number = $1.','.$2.$3;
539: }
540: } else {
541: while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
542: $number = $1.','.$2.$3;
543: }
544: }
1.69 ! albertel 545: if ($target eq 'tex') {
1.48 albertel 546: $number='\$'.$number; #' stupid emacs
547: } else {
548: $number='$'.$number; #' stupid emacs
549: }
550: return $number;
1.2 albertel 551: }
1.5 albertel 552:
553: sub map {
1.27 ng 554: my ($phrase,$dest,$source)=@_;
1.51 albertel 555: my @oldseed=&random_get_seed();
1.27 ng 556: my @seed = &random_seed_from_phrase($phrase);
557: &random_set_seed(@seed);
558: my $destct = scalar(@$dest);
1.28 ng 559: if (!$source) {
560: my @output;
561: my @idx = &math_random_permuted_index($destct);
562: my $ctr = 0;
563: while ($ctr < $destct) {
564: $output[$ctr] = $$dest[$idx[$ctr]];
1.27 ng 565: $ctr++;
1.28 ng 566: }
1.51 albertel 567: &random_set_seed(@oldseed);
1.28 ng 568: return @output;
1.27 ng 569: } else {
1.28 ng 570: my $num = scalar(@$source);
571: my @idx = &math_random_permuted_index($num);
572: my $ctr = 0;
573: my $tot = $num;
574: $tot = $destct if $destct < $num;
575: if (ref($$dest[0])) {
576: while ($ctr < $tot) {
577: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
578: $ctr++;
579: }
580: } else {
581: while ($ctr < $tot) {
582: $$dest[$ctr] = $$source[$idx[$ctr]];
583: $ctr++;
584: }
585: }
1.27 ng 586: }
1.56 albertel 587: &random_set_seed(@oldseed);
1.51 albertel 588: return '';
1.27 ng 589: }
590:
591: sub rmap {
592: my ($phrase,$dest,$source)=@_;
1.51 albertel 593: my @oldseed=&random_get_seed();
1.27 ng 594: my @seed = &random_seed_from_phrase($phrase);
595: &random_set_seed(@seed);
596: my $destct = scalar(@$dest);
1.28 ng 597: if (!$source) {
598: my @idx = &math_random_permuted_index($destct);
599: my $ctr = 0;
600: my @r_idx;
601: while ($ctr < $destct) {
602: $r_idx[$idx[$ctr]] = $ctr;
603: $ctr++;
604: }
605: my @output;
606: $ctr = 0;
607: while ($ctr < $destct) {
608: $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27 ng 609: $ctr++;
1.28 ng 610: }
1.51 albertel 611: &random_set_seed(@oldseed);
1.28 ng 612: return @output;
1.27 ng 613: } else {
1.28 ng 614: my $num = scalar(@$source);
615: my @idx = &math_random_permuted_index($num);
616: my $ctr = 0;
617: my $tot = $num;
618: $tot = $destct if $destct < $num;
619: my @r_idx;
1.27 ng 620: while ($ctr < $tot) {
1.28 ng 621: $r_idx[$idx[$ctr]] = $ctr;
1.27 ng 622: $ctr++;
1.28 ng 623: }
624: $ctr = 0;
625: if (ref($$dest[0])) {
626: while ($ctr < $tot) {
627: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
628: $ctr++;
629: }
630: } else {
631: while ($ctr < $tot) {
632: $$dest[$ctr] = $$source[$r_idx[$ctr]];
633: $ctr++;
634: }
635: }
1.6 albertel 636: }
1.51 albertel 637: &random_set_seed(@oldseed);
638: return '';
1.5 albertel 639: }
1.22 ng 640:
1.23 ng 641: sub capa_id { return }
642:
643: sub problem { return }
644:
1.22 ng 645: sub name{
646: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
647: $fullname = "" if $fullname eq ", ";
1.26 ng 648: $fullname =~ s/\%2d/-/g;
1.22 ng 649: return $fullname;
650: }
651:
652: sub student_number {
653: my $id = &EXT('environment.id');
654: $id = '' if $id eq "";
655: return $id;
656: }
657:
658: sub class {
659: my $course = &EXT('course.description');
660: $course = '' if $course eq "";
661: return $course;
662: }
663:
664: sub sec {
665: my $sec = &EXT('request.course.sec');
1.23 ng 666: $sec = '' if $sec eq "";
1.22 ng 667: return $sec;
668: }
669:
1.23 ng 670: sub open_date {
671: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
1.24 ng 672: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
673: my @hm = split(/:/,$dc[3]);
674: my $ampm = " am";
675: if ($hm[0] > 12) {
676: $hm[0]-=12;
677: $ampm = " pm";
678: }
679: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 680: }
681:
682: sub due_date {
683: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
1.24 ng 684: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
685: my @hm = split(/:/,$dc[3]);
686: my $ampm = " am";
687: if ($hm[0] > 12) {
688: $hm[0]-=12;
689: $ampm = " pm";
690: }
691: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
692: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
1.23 ng 693: }
694:
695: sub answer_date {
696: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
1.24 ng 697: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
698: my @hm = split(/:/,$dc[3]);
699: my $ampm = " am";
700: if ($hm[0] > 12) {
701: $hm[0]-=12;
702: $ampm = " pm";
703: }
704: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
705: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
706: }
707:
708: sub array_moments {
709: my @input=@_;
710: my (@output,$N);
711: $N=scalar (@input);
712: $output[0]=$N;
713: if ($N <= 1) {
714: $output[1]=$input[0];
1.28 ng 715: $output[1]="Input array not defined" if ($N == 0);
1.24 ng 716: $output[2]="variance undefined for N<=1";
717: $output[3]="skewness undefined for N<=1";
718: $output[4]="kurtosis undefined for N<=1";
719: return @output;
720: }
721: my $sum=0;
722: foreach my $line (@input) {
723: $sum+=$line;
724: }
725: $output[1] = $sum/$N;
726: my ($x,$sdev,$var,$skew,$kurt) = 0;
727: foreach my $line (@input) {
728: $x=$line-$output[1];
729: $var+=$x**2;
730: $skew+=$x**3;
731: $kurt+=$x**4;
732: }
733: $output[2]=$var/($N-1);
1.59 albertel 734: $sdev=CORE::sqrt($output[2]);
1.24 ng 735: if ($sdev == 0) {
736: $output[3]="inf-variance=0";
737: $output[4]="inf-variance=0";
738: return @output;
739: }
740: $output[3]=$skew/($sdev**3*$N);
741: $output[4]=$kurt/($sdev**4*$N)-3;
742: return @output;
743: }
1.5 albertel 744:
745: sub choose {
746: my $num = $_[0];
747: return $_[$num];
748: }
1.23 ng 749:
1.49 albertel 750: # expiremental idea
751: sub proper_path {
752: my ($path)=@_;
753: if ( $external::target eq "tex" ) {
754: return '/home/httpd/html'.$path;
755: } else {
756: return $path;
757: }
758: }
1.23 ng 759:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>