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