Annotation of loncom/homework/default_homework.lcpm, revision 1.82
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.82 ! albertel 4: # $Id: default_homework.lcpm,v 1.81 2004/05/18 23:12:15 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.82 ! albertel 145: 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 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: }
1.82 ! albertel 175: $result.="Final final response :$responselist['-1']:$unit:\n";
1.73 albertel 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)=@_;
1.81 albertel 483: my ($dollarmode,$commamode,$alwaysperiod,$options);
484: if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; }
485: #if ($options =~ /\$/) { $dollamode=1; }
486: #if ($options =~ /,/) { $commamode=1; }
1.82 ! albertel 487: if ($options =~ /\./) { $alwaysperiod=1; }
1.73 albertel 488: $fmt=~s/e/E/g;
489: my $result=sprintf('%.'.$fmt,$value);
1.81 albertel 490: if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
1.73 albertel 491: $result=~s/(E[+-]*)0/$1/;
1.81 albertel 492: #if ($dollarmode) {$result=&dollarformat($result);}
493: #if ($commamode) {$result=&commaformat($result);}
1.73 albertel 494: return $result;
1.46 albertel 495: }
496:
1.75 albertel 497: sub chemparse {
498: my ($reaction) = @_;
499: my @tokens = split(/(\s\+|\->|<=>)/,$reaction);
500: my $formula = '';
501: foreach my $token (@tokens) {
502: if ($token eq '->' ) {
503: $formula .= '<m>\ensuremath{\rightarrow}</m> ';
504: next;
505: }
506: if ($token eq '<=>') {
507: if ($external::target eq 'web' &&
508: &EXT('request.browser.unicode')) {
1.76 albertel 509: $formula .= '⇌ ';
1.75 albertel 510: } else {
511: $formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
512: '<=$gt; ');
513: }
514: next;
515: }
516: $token =~ /^\s*(\d*)(.*)/;
517: $formula .= $1 if ($1 > 1); # stoichiometric coefficient
518:
519: my $molecule = $2;
520: # subscripts
1.78 albertel 521: $molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
1.75 albertel 522: # superscripts
523: $molecule =~ s|\^(\d*[+\-]*)|<sup>$1</sup>|g;
524: # strip whitespace
525: $molecule =~ s/\s*//g;
526: # forced space
527: $molecule =~ s/_/ /g;
528: $formula .= $molecule.' ';
529: }
530: # get rid of trailing space
531: $formula =~ s/(\Q${nbsp}\E| )$//;
532:
533: return &xmlparse($formula);
534: }
535:
1.46 albertel 536: sub prettyprint {
1.73 albertel 537: my ($value,$fmt,$target)=@_;
538: my $result;
539: if (!$target) { $target = $external::target; }
1.75 albertel 540: if ($fmt =~ /chem/i) { return(&chemparse($value)); }
1.81 albertel 541: my ($dollarmode,$commamode,$alwaysperiod,$options);
542: if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; }
543: if ($options =~ /\$/) { $dollamode=1; }
544: if ($options =~ /,/) { $commamode=1; }
545: if ($options =~ /\./) { $alwaysperiod=1; }
1.73 albertel 546: if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
1.81 albertel 547: if ($alwaysperiod && $fmt eq '0f') {
548: if ($target eq 'tex') {
549: $value .='\\ensuremath{.}';
550: } else {
551: $value .='.';
552: }
553: }
1.73 albertel 554: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
555: my $frac=$1;
556: if ($dollarmode) { $frac=&dollarformat($frac); }
1.80 albertel 557: if ($commamode) { $frac=&commaformat($frac); }
1.73 albertel 558: my $exponent=$2;
559: $exponent=~s/^\+0*//;
560: $exponent=~s/^-0*/-/;
561: $exponent=~s/^-0*/-/;
562: if ($exponent eq '-') { undef($exponent); }
563: if ($exponent) {
564: if ($target eq 'web') {
565: $result=$frac.'×10<sup>'.$exponent.'</sup>';
566: } elsif ($target eq 'tex') {
567: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
568: } else {
569: $result=$value;
570: }
571: } else {
572: $result=$frac;
573: }
574: } else {
1.48 albertel 575: $result=$value;
1.73 albertel 576: if ($dollarmode) { $result=&dollarformat($result,$target); }
1.80 albertel 577: if ($commamode) { $result=&commaformat($result,$target); }
1.46 albertel 578: }
1.73 albertel 579: return $result;
1.48 albertel 580: }
581:
1.80 albertel 582: sub commaformat {
1.73 albertel 583: my ($number,$target) = @_;
584: if ($number =~ /\./) {
585: while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
586: $number = $1.','.$2.$3;
587: }
588: } else {
589: while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
590: $number = $1.','.$2.$3;
591: }
592: }
1.80 albertel 593: return $number;
594: }
595:
596: sub dollarformat {
597: my ($number,$target) = @_;
598: if (!$target) { $target = $external::target; }
599: $number=&commaformat($number,$target);
1.73 albertel 600: if ($target eq 'tex') {
601: $number='\$'.$number; #' stupid emacs
602: } else {
603: $number='$'.$number; #' stupid emacs
604: }
605: return $number;
1.2 albertel 606: }
1.5 albertel 607:
608: sub map {
1.27 ng 609: my ($phrase,$dest,$source)=@_;
1.51 albertel 610: my @oldseed=&random_get_seed();
1.27 ng 611: my @seed = &random_seed_from_phrase($phrase);
612: &random_set_seed(@seed);
613: my $destct = scalar(@$dest);
1.28 ng 614: if (!$source) {
615: my @output;
616: my @idx = &math_random_permuted_index($destct);
617: my $ctr = 0;
618: while ($ctr < $destct) {
619: $output[$ctr] = $$dest[$idx[$ctr]];
1.27 ng 620: $ctr++;
1.28 ng 621: }
1.51 albertel 622: &random_set_seed(@oldseed);
1.28 ng 623: return @output;
1.27 ng 624: } else {
1.28 ng 625: my $num = scalar(@$source);
626: my @idx = &math_random_permuted_index($num);
627: my $ctr = 0;
628: my $tot = $num;
629: $tot = $destct if $destct < $num;
630: if (ref($$dest[0])) {
631: while ($ctr < $tot) {
632: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
633: $ctr++;
634: }
635: } else {
636: while ($ctr < $tot) {
637: $$dest[$ctr] = $$source[$idx[$ctr]];
638: $ctr++;
639: }
640: }
1.27 ng 641: }
1.56 albertel 642: &random_set_seed(@oldseed);
1.51 albertel 643: return '';
1.27 ng 644: }
645:
646: sub rmap {
647: my ($phrase,$dest,$source)=@_;
1.51 albertel 648: my @oldseed=&random_get_seed();
1.27 ng 649: my @seed = &random_seed_from_phrase($phrase);
650: &random_set_seed(@seed);
651: my $destct = scalar(@$dest);
1.28 ng 652: if (!$source) {
653: my @idx = &math_random_permuted_index($destct);
654: my $ctr = 0;
655: my @r_idx;
656: while ($ctr < $destct) {
657: $r_idx[$idx[$ctr]] = $ctr;
658: $ctr++;
659: }
660: my @output;
661: $ctr = 0;
662: while ($ctr < $destct) {
663: $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27 ng 664: $ctr++;
1.28 ng 665: }
1.51 albertel 666: &random_set_seed(@oldseed);
1.28 ng 667: return @output;
1.27 ng 668: } else {
1.28 ng 669: my $num = scalar(@$source);
670: my @idx = &math_random_permuted_index($num);
671: my $ctr = 0;
672: my $tot = $num;
673: $tot = $destct if $destct < $num;
674: my @r_idx;
1.27 ng 675: while ($ctr < $tot) {
1.28 ng 676: $r_idx[$idx[$ctr]] = $ctr;
1.27 ng 677: $ctr++;
1.28 ng 678: }
679: $ctr = 0;
680: if (ref($$dest[0])) {
681: while ($ctr < $tot) {
682: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
683: $ctr++;
684: }
685: } else {
686: while ($ctr < $tot) {
687: $$dest[$ctr] = $$source[$r_idx[$ctr]];
688: $ctr++;
689: }
690: }
1.6 albertel 691: }
1.51 albertel 692: &random_set_seed(@oldseed);
693: return '';
1.5 albertel 694: }
1.22 ng 695:
1.23 ng 696: sub capa_id { return }
697:
698: sub problem { return }
699:
1.22 ng 700: sub name{
1.73 albertel 701: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
702: $fullname = "" if $fullname eq ", ";
703: $fullname =~ s/\%2d/-/g;
704: return $fullname;
1.22 ng 705: }
706:
707: sub student_number {
1.73 albertel 708: my $id = &EXT('environment.id');
709: $id = '' if $id eq "";
710: return $id;
1.22 ng 711: }
712:
713: sub class {
1.73 albertel 714: my $course = &EXT('course.description');
715: $course = '' if $course eq "";
716: return $course;
1.22 ng 717: }
718:
719: sub sec {
1.73 albertel 720: my $sec = &EXT('request.course.sec');
721: $sec = '' if $sec eq "";
722: return $sec;
1.22 ng 723: }
724:
1.23 ng 725: sub open_date {
1.73 albertel 726: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
727: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
728: my @hm = split(/:/,$dc[3]);
729: my $ampm = " am";
730: if ($hm[0] > 12) {
731: $hm[0]-=12;
732: $ampm = " pm";
733: }
734: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 735: }
736:
737: sub due_date {
1.73 albertel 738: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
739: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
740: my @hm = split(/:/,$dc[3]);
741: my $ampm = " am";
742: if ($hm[0] > 12) {
743: $hm[0]-=12;
744: $ampm = " pm";
745: }
746: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 747: }
748:
749: sub answer_date {
1.73 albertel 750: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
751: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
752: my @hm = split(/:/,$dc[3]);
753: my $ampm = " am";
754: if ($hm[0] > 12) {
755: $hm[0]-=12;
756: $ampm = " pm";
757: }
758: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.24 ng 759: }
760:
761: sub array_moments {
1.73 albertel 762: my @input=@_;
763: my (@output,$N);
764: $N=scalar (@input);
765: $output[0]=$N;
766: if ($N <= 1) {
767: $output[1]=$input[0];
768: $output[1]="Input array not defined" if ($N == 0);
769: $output[2]="variance undefined for N<=1";
770: $output[3]="skewness undefined for N<=1";
771: $output[4]="kurtosis undefined for N<=1";
772: return @output;
773: }
774: my $sum=0;
775: foreach my $line (@input) {
776: $sum+=$line;
777: }
778: $output[1] = $sum/$N;
779: my ($x,$sdev,$var,$skew,$kurt) = 0;
780: foreach my $line (@input) {
781: $x=$line-$output[1];
782: $var+=$x**2;
783: $skew+=$x**3;
784: $kurt+=$x**4;
785: }
786: $output[2]=$var/($N-1);
787: $sdev=CORE::sqrt($output[2]);
788: if ($sdev == 0) {
789: $output[3]="inf-variance=0";
790: $output[4]="inf-variance=0";
791: return @output;
792: }
793: $output[3]=$skew/($sdev**3*$N);
794: $output[4]=$kurt/($sdev**4*$N)-3;
1.24 ng 795: return @output;
796: }
1.5 albertel 797:
798: sub choose {
1.73 albertel 799: my $num = $_[0];
800: return $_[$num];
1.5 albertel 801: }
1.23 ng 802:
1.49 albertel 803: # expiremental idea
804: sub proper_path {
1.73 albertel 805: my ($path)=@_;
806: if ( $external::target eq "tex" ) {
807: return '/home/httpd/html'.$path;
808: } else {
809: return $path;
810: }
1.49 albertel 811: }
1.23 ng 812:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>