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