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