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