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