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