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