Annotation of loncom/homework/default_homework.lcpm, revision 1.64
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.64 ! albertel 4: # $Id: default_homework.lcpm,v 1.63 2003/06/07 03:52:39 albertel 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; }
493: my $result=sprintf('%.'.$fmt,$value);
1.50 albertel 494: $result=~s/(E[+-]*)0/$1/;
1.48 albertel 495: if ($dollarmode) {$result=&dollarmode($result);}
496: return $result;
1.46 albertel 497: }
498:
499: sub prettyprint {
500: my ($value,$fmt)=@_;
501: my $result;
1.48 albertel 502: my $dollarmode;
503: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
1.46 albertel 504: if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
505: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
506: my $frac=$1;
1.48 albertel 507: if ($dollarmode) { $frac=&dollarformat($frac); }
1.46 albertel 508: my $exponent=$2;
509: $exponent=~s/^\+0*//;
1.50 albertel 510: $exponent=~s/^-0*/-/;
1.48 albertel 511: if ($exponent) {
512: if ($external::target eq 'web') {
513: $result=$frac.'×10<sup>'.$exponent.'</sup>';
514: } elsif ($external::target eq 'tex') {
515: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
516: } else {
517: $result=$value;
518: }
1.46 albertel 519: } else {
1.48 albertel 520: $result=$frac;
1.46 albertel 521: }
522: } else {
523: $result=$value;
1.48 albertel 524: if ($dollarmode) { $result=&dollarformat($result); }
1.46 albertel 525: }
526: return $result;
1.48 albertel 527: }
528:
529: sub dollarformat {
530: my ($number) = @_;
531: if ($number =~ /\./) {
532: while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
533: $number = $1.','.$2.$3;
534: }
535: } else {
536: while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
537: $number = $1.','.$2.$3;
538: }
539: }
540: if ($external::target eq 'tex') {
541: $number='\$'.$number; #' stupid emacs
542: } else {
543: $number='$'.$number; #' stupid emacs
544: }
545: return $number;
1.2 albertel 546: }
1.5 albertel 547:
548: sub map {
1.27 ng 549: my ($phrase,$dest,$source)=@_;
1.51 albertel 550: my @oldseed=&random_get_seed();
1.27 ng 551: my @seed = &random_seed_from_phrase($phrase);
552: &random_set_seed(@seed);
553: my $destct = scalar(@$dest);
1.28 ng 554: if (!$source) {
555: my @output;
556: my @idx = &math_random_permuted_index($destct);
557: my $ctr = 0;
558: while ($ctr < $destct) {
559: $output[$ctr] = $$dest[$idx[$ctr]];
1.27 ng 560: $ctr++;
1.28 ng 561: }
1.51 albertel 562: &random_set_seed(@oldseed);
1.28 ng 563: return @output;
1.27 ng 564: } else {
1.28 ng 565: my $num = scalar(@$source);
566: my @idx = &math_random_permuted_index($num);
567: my $ctr = 0;
568: my $tot = $num;
569: $tot = $destct if $destct < $num;
570: if (ref($$dest[0])) {
571: while ($ctr < $tot) {
572: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
573: $ctr++;
574: }
575: } else {
576: while ($ctr < $tot) {
577: $$dest[$ctr] = $$source[$idx[$ctr]];
578: $ctr++;
579: }
580: }
1.27 ng 581: }
1.56 albertel 582: &random_set_seed(@oldseed);
1.51 albertel 583: return '';
1.27 ng 584: }
585:
586: sub rmap {
587: my ($phrase,$dest,$source)=@_;
1.51 albertel 588: my @oldseed=&random_get_seed();
1.27 ng 589: my @seed = &random_seed_from_phrase($phrase);
590: &random_set_seed(@seed);
591: my $destct = scalar(@$dest);
1.28 ng 592: if (!$source) {
593: my @idx = &math_random_permuted_index($destct);
594: my $ctr = 0;
595: my @r_idx;
596: while ($ctr < $destct) {
597: $r_idx[$idx[$ctr]] = $ctr;
598: $ctr++;
599: }
600: my @output;
601: $ctr = 0;
602: while ($ctr < $destct) {
603: $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27 ng 604: $ctr++;
1.28 ng 605: }
1.51 albertel 606: &random_set_seed(@oldseed);
1.28 ng 607: return @output;
1.27 ng 608: } else {
1.28 ng 609: my $num = scalar(@$source);
610: my @idx = &math_random_permuted_index($num);
611: my $ctr = 0;
612: my $tot = $num;
613: $tot = $destct if $destct < $num;
614: my @r_idx;
1.27 ng 615: while ($ctr < $tot) {
1.28 ng 616: $r_idx[$idx[$ctr]] = $ctr;
1.27 ng 617: $ctr++;
1.28 ng 618: }
619: $ctr = 0;
620: if (ref($$dest[0])) {
621: while ($ctr < $tot) {
622: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
623: $ctr++;
624: }
625: } else {
626: while ($ctr < $tot) {
627: $$dest[$ctr] = $$source[$r_idx[$ctr]];
628: $ctr++;
629: }
630: }
1.6 albertel 631: }
1.51 albertel 632: &random_set_seed(@oldseed);
633: return '';
1.5 albertel 634: }
1.22 ng 635:
1.23 ng 636: sub capa_id { return }
637:
638: sub problem { return }
639:
1.22 ng 640: sub name{
641: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
642: $fullname = "" if $fullname eq ", ";
1.26 ng 643: $fullname =~ s/\%2d/-/g;
1.22 ng 644: return $fullname;
645: }
646:
647: sub student_number {
648: my $id = &EXT('environment.id');
649: $id = '' if $id eq "";
650: return $id;
651: }
652:
653: sub class {
654: my $course = &EXT('course.description');
655: $course = '' if $course eq "";
656: return $course;
657: }
658:
659: sub sec {
660: my $sec = &EXT('request.course.sec');
1.23 ng 661: $sec = '' if $sec eq "";
1.22 ng 662: return $sec;
663: }
664:
1.23 ng 665: sub open_date {
666: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
1.24 ng 667: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
668: my @hm = split(/:/,$dc[3]);
669: my $ampm = " am";
670: if ($hm[0] > 12) {
671: $hm[0]-=12;
672: $ampm = " pm";
673: }
674: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 675: }
676:
677: sub due_date {
678: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
1.24 ng 679: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
680: my @hm = split(/:/,$dc[3]);
681: my $ampm = " am";
682: if ($hm[0] > 12) {
683: $hm[0]-=12;
684: $ampm = " pm";
685: }
686: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
687: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
1.23 ng 688: }
689:
690: sub answer_date {
691: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
1.24 ng 692: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
693: my @hm = split(/:/,$dc[3]);
694: my $ampm = " am";
695: if ($hm[0] > 12) {
696: $hm[0]-=12;
697: $ampm = " pm";
698: }
699: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
700: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
701: }
702:
703: sub array_moments {
704: my @input=@_;
705: my (@output,$N);
706: $N=scalar (@input);
707: $output[0]=$N;
708: if ($N <= 1) {
709: $output[1]=$input[0];
1.28 ng 710: $output[1]="Input array not defined" if ($N == 0);
1.24 ng 711: $output[2]="variance undefined for N<=1";
712: $output[3]="skewness undefined for N<=1";
713: $output[4]="kurtosis undefined for N<=1";
714: return @output;
715: }
716: my $sum=0;
717: foreach my $line (@input) {
718: $sum+=$line;
719: }
720: $output[1] = $sum/$N;
721: my ($x,$sdev,$var,$skew,$kurt) = 0;
722: foreach my $line (@input) {
723: $x=$line-$output[1];
724: $var+=$x**2;
725: $skew+=$x**3;
726: $kurt+=$x**4;
727: }
728: $output[2]=$var/($N-1);
1.59 albertel 729: $sdev=CORE::sqrt($output[2]);
1.24 ng 730: if ($sdev == 0) {
731: $output[3]="inf-variance=0";
732: $output[4]="inf-variance=0";
733: return @output;
734: }
735: $output[3]=$skew/($sdev**3*$N);
736: $output[4]=$kurt/($sdev**4*$N)-3;
737: return @output;
738: }
1.5 albertel 739:
740: sub choose {
741: my $num = $_[0];
742: return $_[$num];
743: }
1.23 ng 744:
1.49 albertel 745: # expiremental idea
746: sub proper_path {
747: my ($path)=@_;
748: if ( $external::target eq "tex" ) {
749: return '/home/httpd/html'.$path;
750: } else {
751: return $path;
752: }
753: }
1.23 ng 754:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>