Annotation of loncom/homework/default_homework.lcpm, revision 1.70
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.70 ! albertel 4: # $Id: default_homework.lcpm,v 1.69 2004/02/18 22:49:32 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].
1.70 ! albertel 47: ';return ($__LC__type,$__LC__tol,$__LC__sig,$__LC__ans_fmt,$__LC__unit,$__LC__calc,$__LC__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.70 ! albertel 168: my $type =eval $expr.';return $__LC__type;';
! 169: $result.="Got type :$type:\n";
1.31 albertel 170: if ($type ne '' && $#list > 0) {
171: (@responselist)=split /,/,$response;
172: } else {
173: (@responselist)=($response);
174: }
1.15 albertel 175: my $unit='';
1.52 albertel 176: $result.="Initial final response :$responselist['-1']:\n";
1.64 albertel 177: if ($type eq '' || $type eq 'float') {
1.31 albertel 178: #for numerical problems split off the unit
179: if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
180: $responselist['-1']=$1;
181: $unit=$2;
182: }
1.15 albertel 183: }
1.52 albertel 184: $result.="Final final response :$responselist['-1']:\n";
185: $result.=":$#list: answers\n";
1.14 albertel 186: $unit=~s/\s//;
187: my $i=0;
188: my $awards='';
189: for ($i=0; $i<@list;$i++) {
1.52 albertel 190: $result.="trying answer :$list[$i]:\n";
1.57 albertel 191: my $thisanswer=$list[$i];
1.64 albertel 192: # $thisanswer=~ s/\\/\\\\/g;
193: # $thisanswer =~ s/\'/\\\'/g;
194: $result.="trying answer :$thisanswer:\n";
1.19 albertel 195: if ($unit eq '') {
1.64 albertel 196: $aresult=&caparesponse_check($thisanswer,$responselist[$i],
197: $expr);
1.19 albertel 198: } else {
1.64 albertel 199: $aresult=&caparesponse_check($thisanswer,$responselist[$i]." $unit",
200: $expr);
1.19 albertel 201: }
1.15 albertel 202: my ($temp)=split /:/, $aresult;
1.14 albertel 203: $awards.="$temp,";
1.15 albertel 204: $result.=$aresult;
1.14 albertel 205: }
206: chop $awards;
1.52 albertel 207: return "$awards:\n$result";
1.7 albertel 208: }
209:
1.4 albertel 210: sub tex {
211: if ( $external::target eq "tex" ) {
1.58 albertel 212: return $_[0];
1.4 albertel 213: } else {
1.58 albertel 214: return $_[1];
1.4 albertel 215: }
216: }
217:
1.24 ng 218: sub var_in_tex {
219: if ( $external::target eq "tex" ) {
1.58 albertel 220: return $_[0];
1.24 ng 221: } else {
222: return "";
223: }
224: }
225:
1.4 albertel 226: sub web {
227: if ( $external::target eq "tex" ) {
1.58 albertel 228: return $_[1];
1.4 albertel 229: } else {
1.43 albertel 230: if ( $external::target eq "web" || $external::target eq "answer") {
1.58 albertel 231: return $_[2];
1.26 ng 232: } else {
1.58 albertel 233: return $_[0];
1.4 albertel 234: }
235: }
236: }
237:
1.24 ng 238: sub html {
239: if ( $external::target eq "web" ) {
1.26 ng 240: return shift;
1.24 ng 241: }
242: }
243:
1.1 harris41 244: sub hinton {
245: return 0;
246: }
247:
248: sub random {
1.61 albertel 249: my ($start,$end,$step)=@_;
250: if ( ! $hidden::RANDOMINIT ) {
251: if ($external::randomseed == 0) { $external::randomseed=1; }
252: if ($external::randomseed =~/,/) {
253: my ($num1,$num2)=split(/,/,$seed);
254: &random_set_seed(abs($num1),abs($num2));
255: } else {
256: &random_set_seed(1,int(abs($external::randomseed)));
257: }
258: &math_random_uniform();
259: $hidden::RANDOMINIT=1;
260: }
261: if (!defined($step)) { $step=1; }
262: my $num=1+int(($end-$start)/$step);
263: my $result=$start + int(&math_random_uniform() * $num)*$step;
264: return $result;
1.1 harris41 265: }
266:
1.26 ng 267: sub random_normal {
268: my ($item_cnt,$seed,$av,$std_dev) = @_;
1.51 albertel 269: my @oldseed=&random_get_seed();
1.26 ng 270: my @retArray;
271: &random_set_seed_from_phrase($seed);
272: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
1.51 albertel 273: &random_set_seed(@oldseed);
1.26 ng 274: return @retArray;
275: }
276:
277: sub random_beta {
278: my ($item_cnt,$seed,$aa,$bb) = @_;
1.51 albertel 279: my @oldseed=&random_get_seed();
1.26 ng 280: my @retArray;
281: &random_set_seed_from_phrase($seed);
282: @retArray=&math_random_beta($item_cnt,$aa,$bb);
1.51 albertel 283: &random_set_seed(@oldseed);
1.26 ng 284: return @retArray;
285: }
286:
287: sub random_gamma {
288: my ($item_cnt,$seed,$a,$r) = @_;
1.51 albertel 289: my @oldseed=&random_get_seed();
1.26 ng 290: my @retArray;
291: &random_set_seed_from_phrase($seed);
292: @retArray=&math_random_gamma($item_cnt,$a,$r);
1.51 albertel 293: &random_set_seed(@oldseed);
1.26 ng 294: return @retArray;
295: }
296:
297: sub random_exponential {
298: my ($item_cnt,$seed,$av) = @_;
1.51 albertel 299: my @oldseed=&random_get_seed();
1.26 ng 300: my @retArray;
301: &random_set_seed_from_phrase($seed);
302: @retArray=&math_random_exponential($item_cnt,$av);
1.51 albertel 303: &random_set_seed(@oldseed);
1.26 ng 304: return @retArray;
305: }
306:
307: sub random_poisson {
308: my ($item_cnt,$seed,$mu) = @_;
1.51 albertel 309: my @oldseed=&random_get_seed();
1.26 ng 310: my @retArray;
311: &random_set_seed_from_phrase($seed);
312: @retArray=&math_random_poisson($item_cnt,$mu);
1.51 albertel 313: &random_set_seed(@oldseed);
1.26 ng 314: return @retArray;
315: }
316:
317: sub random_chi {
318: my ($item_cnt,$seed,$df) = @_;
1.51 albertel 319: my @oldseed=&random_get_seed();
1.26 ng 320: my @retArray;
321: &random_set_seed_from_phrase($seed);
322: @retArray=&math_random_chi_square($item_cnt,$df);
1.51 albertel 323: &random_set_seed(@oldseed);
1.26 ng 324: return @retArray;
325: }
326:
327: sub random_noncentral_chi {
328: my ($item_cnt,$seed,$df,$nonc) = @_;
1.51 albertel 329: my @oldseed=&random_get_seed();
1.26 ng 330: my @retArray;
331: &random_set_seed_from_phrase($seed);
332: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
1.51 albertel 333: &random_set_seed(@oldseed);
1.26 ng 334: return @retArray;
335: }
336:
337: sub random_f {
338: my ($item_cnt,$seed,$dfn,$dfd) = @_;
1.51 albertel 339: my @oldseed=&random_get_seed();
1.26 ng 340: my @retArray;
341: &random_set_seed_from_phrase($seed);
342: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
1.51 albertel 343: &random_set_seed(@oldseed);
1.26 ng 344: return @retArray;
345: }
346:
347: sub random_noncentral_f {
348: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
1.51 albertel 349: my @oldseed=&random_get_seed();
1.26 ng 350: my @retArray;
351: &random_set_seed_from_phrase($seed);
352: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
1.51 albertel 353: &random_set_seed(@oldseed);
1.26 ng 354: return @retArray;
355: }
356:
357: sub random_multivariate_normal {
1.33 ng 358: my ($item_cnt,$seed,$mean,$covar) = @_;
1.51 albertel 359: my @oldseed=&random_get_seed();
1.26 ng 360: &random_set_seed_from_phrase($seed);
1.33 ng 361: @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.51 albertel 362: &random_set_seed(@oldseed);
1.26 ng 363: return @retArray;
364: }
365:
366: sub random_multinomial {
367: my ($item_cnt,$seed,@p) = @_;
1.51 albertel 368: my @oldseed=&random_get_seed();
1.26 ng 369: my @retArray;
370: &random_set_seed_from_phrase($seed);
371: @retArray=&math_random_multinomial($item_cnt,@p);
1.51 albertel 372: &random_set_seed(@oldseed);
1.26 ng 373: return @retArray;
374: }
375:
376: sub random_permutation {
377: my ($seed,@inArray) = @_;
1.51 albertel 378: my @oldseed=&random_get_seed();
1.26 ng 379: my @retArray;
380: &random_set_seed_from_phrase($seed);
381: @retArray=&math_random_permutation(@inArray);
1.51 albertel 382: &random_set_seed(@oldseed);
1.26 ng 383: return @retArray;
384: }
385:
386: sub random_uniform {
387: my ($item_cnt,$seed,$low,$high) = @_;
1.51 albertel 388: my @oldseed=&random_get_seed();
1.26 ng 389: my @retArray;
390: &random_set_seed_from_phrase($seed);
391: @retArray=&math_random_uniform($item_cnt,$low,$high);
1.51 albertel 392: &random_set_seed(@oldseed);
1.26 ng 393: return @retArray;
394: }
395:
396: sub random_uniform_integer {
397: my ($item_cnt,$seed,$low,$high) = @_;
1.51 albertel 398: my @oldseed=&random_get_seed();
1.26 ng 399: my @retArray;
400: &random_set_seed_from_phrase($seed);
401: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
1.51 albertel 402: &random_set_seed(@oldseed);
1.26 ng 403: return @retArray;
404: }
405:
406: sub random_binomial {
407: my ($item_cnt,$seed,$nt,$p) = @_;
1.51 albertel 408: my @oldseed=&random_get_seed();
1.26 ng 409: my @retArray;
410: &random_set_seed_from_phrase($seed);
411: @retArray=&math_random_binomial($item_cnt,$nt,$p);
1.51 albertel 412: &random_set_seed(@oldseed);
1.26 ng 413: return @retArray;
414: }
415:
416: sub random_negative_binomial {
417: my ($item_cnt,$seed,$ne,$p) = @_;
1.51 albertel 418: my @oldseed=&random_get_seed();
1.26 ng 419: my @retArray;
420: &random_set_seed_from_phrase($seed);
421: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
1.51 albertel 422: &random_set_seed(@oldseed);
1.26 ng 423: return @retArray;
424: }
425:
1.23 ng 426: sub abs { abs(shift) }
427: sub sin { sin(shift) }
428: sub cos { cos(shift) }
429: sub exp { exp(shift) }
430: sub int { int(shift) }
431: sub log { log(shift) }
432: sub atan2 { atan2($_[0],$_[1]) }
433: sub sqrt { sqrt(shift) }
434:
1.59 albertel 435: sub tan { CORE::sin($_[0]) / CORE::cos($_[0]) }
1.21 harris41 436: #sub atan { atan2($_[0], 1); }
437: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
438: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.22 ng 439:
1.59 albertel 440: sub log10 { CORE::log($_[0])/CORE::log(10); }
1.22 ng 441:
1.20 harris41 442: sub factorial {
1.59 albertel 443: my $input = CORE::int(shift);
1.20 harris41 444: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
445: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
446: return 1 if $input == 0;
447: my $result = 1;
448: for (my $i=2; $i<=$input; $i++) { $result *= $i }
449: return $result;
450: }
451:
452: sub sgn {
453: return -1 if $_[0] < 0;
454: return 0 if $_[0] == 0;
455: return 1 if $_[0] > 0;
456: }
457:
458: sub min {
459: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
460: return shift @sorted;
461: }
462:
463: sub max {
464: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
465: return pop @sorted;
466: }
1.1 harris41 467:
1.20 harris41 468: sub roundto {
469: my ($input,$n) = @_;
470: return sprintf('%.'.$n.'f',$input);
471: }
472:
473: sub to_string {
474: my ($input,$n) = @_;
1.26 ng 475: return sprintf($input) if $n eq "";
476: $n = '.'.$n if $n !~ /^\./;
1.20 harris41 477: return sprintf('%'.$n,$input) if $n ne "";
478: }
479:
480: sub sub_string {
481: my ($str,$start,$len) = @_;
482: return substr($str,$start-1,$len);
483: }
1.1 harris41 484:
485: sub pow {return $_[0] ** $_[1]; }
1.59 albertel 486: sub ceil {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
487: sub floor {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
1.27 ng 488: #sub floor {return int($_[0]); }
1.1 harris41 489:
1.2 albertel 490: sub format {
491: my ($value,$fmt)=@_;
1.48 albertel 492: my $dollarmode;
493: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
1.67 albertel 494: $fmt=~s/e/E/g;
1.48 albertel 495: my $result=sprintf('%.'.$fmt,$value);
1.50 albertel 496: $result=~s/(E[+-]*)0/$1/;
1.48 albertel 497: if ($dollarmode) {$result=&dollarmode($result);}
498: return $result;
1.46 albertel 499: }
500:
501: sub prettyprint {
1.69 albertel 502: my ($value,$fmt,$target)=@_;
1.46 albertel 503: my $result;
1.48 albertel 504: my $dollarmode;
1.69 albertel 505: if (!$target) { $target = $external::target; }
1.48 albertel 506: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
1.46 albertel 507: if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
1.68 ng 508: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
1.46 albertel 509: my $frac=$1;
1.48 albertel 510: if ($dollarmode) { $frac=&dollarformat($frac); }
1.46 albertel 511: my $exponent=$2;
512: $exponent=~s/^\+0*//;
1.50 albertel 513: $exponent=~s/^-0*/-/;
1.65 albertel 514: $exponent=~s/^-0*/-/;
515: if ($exponent eq '-') { undef($exponent); }
1.48 albertel 516: if ($exponent) {
1.69 albertel 517: if ($target eq 'web') {
1.48 albertel 518: $result=$frac.'×10<sup>'.$exponent.'</sup>';
1.69 albertel 519: } elsif ($target eq 'tex') {
1.48 albertel 520: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
521: } else {
522: $result=$value;
523: }
1.46 albertel 524: } else {
1.48 albertel 525: $result=$frac;
1.46 albertel 526: }
527: } else {
528: $result=$value;
1.69 albertel 529: if ($dollarmode) { $result=&dollarformat($result,$target); }
1.46 albertel 530: }
531: return $result;
1.48 albertel 532: }
533:
534: sub dollarformat {
1.69 albertel 535: my ($number,$target) = @_;
536: if (!$target) { $target = $external::target; }
1.48 albertel 537: if ($number =~ /\./) {
538: while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
539: $number = $1.','.$2.$3;
540: }
541: } else {
542: while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
543: $number = $1.','.$2.$3;
544: }
545: }
1.69 albertel 546: if ($target eq 'tex') {
1.48 albertel 547: $number='\$'.$number; #' stupid emacs
548: } else {
549: $number='$'.$number; #' stupid emacs
550: }
551: return $number;
1.2 albertel 552: }
1.5 albertel 553:
554: sub map {
1.27 ng 555: my ($phrase,$dest,$source)=@_;
1.51 albertel 556: my @oldseed=&random_get_seed();
1.27 ng 557: my @seed = &random_seed_from_phrase($phrase);
558: &random_set_seed(@seed);
559: my $destct = scalar(@$dest);
1.28 ng 560: if (!$source) {
561: my @output;
562: my @idx = &math_random_permuted_index($destct);
563: my $ctr = 0;
564: while ($ctr < $destct) {
565: $output[$ctr] = $$dest[$idx[$ctr]];
1.27 ng 566: $ctr++;
1.28 ng 567: }
1.51 albertel 568: &random_set_seed(@oldseed);
1.28 ng 569: return @output;
1.27 ng 570: } else {
1.28 ng 571: my $num = scalar(@$source);
572: my @idx = &math_random_permuted_index($num);
573: my $ctr = 0;
574: my $tot = $num;
575: $tot = $destct if $destct < $num;
576: if (ref($$dest[0])) {
577: while ($ctr < $tot) {
578: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
579: $ctr++;
580: }
581: } else {
582: while ($ctr < $tot) {
583: $$dest[$ctr] = $$source[$idx[$ctr]];
584: $ctr++;
585: }
586: }
1.27 ng 587: }
1.56 albertel 588: &random_set_seed(@oldseed);
1.51 albertel 589: return '';
1.27 ng 590: }
591:
592: sub rmap {
593: my ($phrase,$dest,$source)=@_;
1.51 albertel 594: my @oldseed=&random_get_seed();
1.27 ng 595: my @seed = &random_seed_from_phrase($phrase);
596: &random_set_seed(@seed);
597: my $destct = scalar(@$dest);
1.28 ng 598: if (!$source) {
599: my @idx = &math_random_permuted_index($destct);
600: my $ctr = 0;
601: my @r_idx;
602: while ($ctr < $destct) {
603: $r_idx[$idx[$ctr]] = $ctr;
604: $ctr++;
605: }
606: my @output;
607: $ctr = 0;
608: while ($ctr < $destct) {
609: $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27 ng 610: $ctr++;
1.28 ng 611: }
1.51 albertel 612: &random_set_seed(@oldseed);
1.28 ng 613: return @output;
1.27 ng 614: } else {
1.28 ng 615: my $num = scalar(@$source);
616: my @idx = &math_random_permuted_index($num);
617: my $ctr = 0;
618: my $tot = $num;
619: $tot = $destct if $destct < $num;
620: my @r_idx;
1.27 ng 621: while ($ctr < $tot) {
1.28 ng 622: $r_idx[$idx[$ctr]] = $ctr;
1.27 ng 623: $ctr++;
1.28 ng 624: }
625: $ctr = 0;
626: if (ref($$dest[0])) {
627: while ($ctr < $tot) {
628: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
629: $ctr++;
630: }
631: } else {
632: while ($ctr < $tot) {
633: $$dest[$ctr] = $$source[$r_idx[$ctr]];
634: $ctr++;
635: }
636: }
1.6 albertel 637: }
1.51 albertel 638: &random_set_seed(@oldseed);
639: return '';
1.5 albertel 640: }
1.22 ng 641:
1.23 ng 642: sub capa_id { return }
643:
644: sub problem { return }
645:
1.22 ng 646: sub name{
647: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
648: $fullname = "" if $fullname eq ", ";
1.26 ng 649: $fullname =~ s/\%2d/-/g;
1.22 ng 650: return $fullname;
651: }
652:
653: sub student_number {
654: my $id = &EXT('environment.id');
655: $id = '' if $id eq "";
656: return $id;
657: }
658:
659: sub class {
660: my $course = &EXT('course.description');
661: $course = '' if $course eq "";
662: return $course;
663: }
664:
665: sub sec {
666: my $sec = &EXT('request.course.sec');
1.23 ng 667: $sec = '' if $sec eq "";
1.22 ng 668: return $sec;
669: }
670:
1.23 ng 671: sub open_date {
672: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
1.24 ng 673: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
674: my @hm = split(/:/,$dc[3]);
675: my $ampm = " am";
676: if ($hm[0] > 12) {
677: $hm[0]-=12;
678: $ampm = " pm";
679: }
680: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 681: }
682:
683: sub due_date {
684: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
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];
1.23 ng 694: }
695:
696: sub answer_date {
697: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
1.24 ng 698: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
699: my @hm = split(/:/,$dc[3]);
700: my $ampm = " am";
701: if ($hm[0] > 12) {
702: $hm[0]-=12;
703: $ampm = " pm";
704: }
705: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
706: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
707: }
708:
709: sub array_moments {
710: my @input=@_;
711: my (@output,$N);
712: $N=scalar (@input);
713: $output[0]=$N;
714: if ($N <= 1) {
715: $output[1]=$input[0];
1.28 ng 716: $output[1]="Input array not defined" if ($N == 0);
1.24 ng 717: $output[2]="variance undefined for N<=1";
718: $output[3]="skewness undefined for N<=1";
719: $output[4]="kurtosis undefined for N<=1";
720: return @output;
721: }
722: my $sum=0;
723: foreach my $line (@input) {
724: $sum+=$line;
725: }
726: $output[1] = $sum/$N;
727: my ($x,$sdev,$var,$skew,$kurt) = 0;
728: foreach my $line (@input) {
729: $x=$line-$output[1];
730: $var+=$x**2;
731: $skew+=$x**3;
732: $kurt+=$x**4;
733: }
734: $output[2]=$var/($N-1);
1.59 albertel 735: $sdev=CORE::sqrt($output[2]);
1.24 ng 736: if ($sdev == 0) {
737: $output[3]="inf-variance=0";
738: $output[4]="inf-variance=0";
739: return @output;
740: }
741: $output[3]=$skew/($sdev**3*$N);
742: $output[4]=$kurt/($sdev**4*$N)-3;
743: return @output;
744: }
1.5 albertel 745:
746: sub choose {
747: my $num = $_[0];
748: return $_[$num];
749: }
1.23 ng 750:
1.49 albertel 751: # expiremental idea
752: sub proper_path {
753: my ($path)=@_;
754: if ( $external::target eq "tex" ) {
755: return '/home/httpd/html'.$path;
756: } else {
757: return $path;
758: }
759: }
1.23 ng 760:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>