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