Annotation of loncom/homework/default_homework.lcpm, revision 1.77
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.77 ! albertel 4: # $Id: default_homework.lcpm,v 1.76 2004/03/22 21:05:15 albertel Exp $
1.42 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.20 harris41 28: #
1.76 albertel 29:
1.25 albertel 30: #init some globals
1.38 albertel 31: $hidden::RANDOMINIT=0;
1.22 ng 32: $pi=atan2(1,1)*4;
33: $rad2deg=180.0/$pi;
34: $deg2rad=$pi/180.0;
1.44 matthew 35: $"=' ';
1.3 albertel 36:
1.7 albertel 37: sub caparesponse_check {
1.74 albertel 38: my ($answer,$response)=@_;
1.73 albertel 39: #not properly used yet: calc
40: #not to be used: $ans_fmt
1.74 albertel 41: my $type=$LONCAPA::CAPAresponse_args{'type'};
42: my $tol=$LONCAPA::CAPAresponse_args{'tol'};
43: my $sig=$LONCAPA::CAPAresponse_args{'sig'};
44: my $ans_fmt=$LONCAPA::CAPAresponse_args{'ans_fmt'};
45: my $unit=$LONCAPA::CAPAresponse_args{'unit'};
46: my $calc=$LONCAPA::CAPAresponse_args{'calc'};
47: my $samples=$LONCAPA::CAPAresponse_args{'samples'};
1.73 albertel 48:
49: my $tol_type=''; # gets it's value from whether tol has a % or not done
50: my $sig_lbound=''; #done
51: my $sig_ubound=''; #done
52:
53:
54: #type's definitons come from capaParser.h
55: my $message='';
56: #remove leading and trailing whitespace
57: if (!defined($response)) {
58: $response='';
59: }
60: if ($response=~ /^\s|\s$/) {
61: $response=~ s:^\s+|\s+$::g;
62: $message .="Removed ws now :$response:\n";
63: } else {
64: $message .="no ws in :$response:\n";
65: }
1.76 albertel 66: if ($type eq 'cs' || $type eq 'ci' || $type eq 'mc') {
67: #for string answers make surec all places spaces occur, there is
68: #really only 1 space, in both the answer and the response
69: $answer=~s/ +/ /g;
70: $response=~s/ +/ /g;
71: }
1.73 albertel 72: if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
73:
74: if ($type eq '' ) {
1.74 albertel 75: $message .= "Didn't find a type :$type: defaulting\n";
1.73 albertel 76: if ( $answer eq ($answer *1.0)) { $type = 2;
77: } else { $type = 3; }
78: } else {
79: if ($type eq 'cs') { $type = 4; }
80: elsif ($type eq 'ci') { $type = 3 }
81: elsif ($type eq 'mc') { $type = 5; }
82: elsif ($type eq 'fml') { $type = 8; }
83: elsif ($type eq 'subj') { $type = 7; }
84: elsif ($type eq 'float') { $type = 2; }
85: elsif ($type eq 'int') { $type = 1; }
86: else { return "ERROR: Unknown type of answer: $type" }
87: }
88:
89: my $points;
90: my $id_list;
91: #formula type setup the sample points
92: if ($type eq '8') {
93: ($id_list,$points)=split(/@/,$samples);
94: $message.="Found :$id_list:$points: points in $samples\n";
95: }
96: if ($tol eq '') {
97: $tol=0.0;
98: $tol_type=1; #TOL_ABSOLUTE
99: } else {
100: if ($tol =~ /%$/) {
101: chop $tol;
102: $tol_type=2; #TOL_PERCENTAGE
103: } else {
104: $tol_type=1; #TOL_ABSOLUTE
105: }
106: }
107:
108: if ($sig eq '') {
109: $sig_lbound = 0; #SIG_LB_DEFAULT
110: $sig_ubound =15; #SIG_UB_DEFAULT
111: } else {
112: ($sig_lbound,$sig_ubound) = split /,/,$sig;
113: if (!defined($sig_lbound)) {
114: $sig_lbound = 0; #SIG_LB_DEFAULT
115: $sig_ubound =15; #SIG_UB_DEFAULT
116: }
117: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
118: }
119: my $reterror="";
120: my $result = &caparesponse_capa_check_answer($response,$answer,$type,
121: $tol_type,$tol,
122: $sig_lbound,$sig_ubound,
123: $ans_fmt,$unit,$calc,$id_list,
124: $points,$external::randomseed,
125: \$reterror);
126:
127: if ($result == '1') { $result='EXACT_ANS'; }
128: elsif ($result == '2') { $result='APPROX_ANS'; }
129: elsif ($result == '3') { $result='SIG_FAIL'; }
130: elsif ($result == '4') { $result='UNIT_FAIL'; }
131: elsif ($result == '5') { $result='NO_UNIT'; }
132: elsif ($result == '6') { $result='UNIT_OK'; }
133: elsif ($result == '7') { $result='INCORRECT'; }
134: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
135: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
136: elsif ($result =='10') { $result='SUB_RECORDED'; }
137: elsif ($result =='11') { $result='BAD_FORMULA'; }
1.77 ! albertel 138: elsif ($result =='13') { $result='UNIT_INVALID_INSTRUCTOR'; }
! 139: elsif ($result =='141') { $result='UNIT_INVALID_STUDENT'; }
! 140: elsif ($result =='142') { $result='UNIT_INVALID_STUDENT'; }
! 141: elsif ($result =='143') { $result='UNIT_INVALID_STUDENT'; }
! 142: elsif ($result =='15') { $result='UNIT_IRRECONCIBLE'; }
1.73 albertel 143: else {$result = "ERROR: Unknown Result:$result:$@:";}
144:
1.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)=@_;
483: my $dollarmode;
484: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
485: $fmt=~s/e/E/g;
486: my $result=sprintf('%.'.$fmt,$value);
487: $result=~s/(E[+-]*)0/$1/;
488: if ($dollarmode) {$result=&dollarmode($result);}
489: return $result;
1.46 albertel 490: }
491:
1.75 albertel 492: sub chemparse {
493: my ($reaction) = @_;
494: my @tokens = split(/(\s\+|\->|<=>)/,$reaction);
495: my $formula = '';
496: foreach my $token (@tokens) {
497: if ($token eq '->' ) {
498: $formula .= '<m>\ensuremath{\rightarrow}</m> ';
499: next;
500: }
501: if ($token eq '<=>') {
502: if ($external::target eq 'web' &&
503: &EXT('request.browser.unicode')) {
1.76 albertel 504: $formula .= '⇌ ';
1.75 albertel 505: } else {
506: $formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
507: '<=$gt; ');
508: }
509: next;
510: }
511: $token =~ /^\s*(\d*)(.*)/;
512: $formula .= $1 if ($1 > 1); # stoichiometric coefficient
513:
514: my $molecule = $2;
515: # subscripts
516: $molecule =~ s|(?<=[a-zA-Z\[\s])(\d+)|<sub>$1</sub>|g;
517: # superscripts
518: $molecule =~ s|\^(\d*[+\-]*)|<sup>$1</sup>|g;
519: # strip whitespace
520: $molecule =~ s/\s*//g;
521: # forced space
522: $molecule =~ s/_/ /g;
523: $formula .= $molecule.' ';
524: }
525: # get rid of trailing space
526: $formula =~ s/(\Q${nbsp}\E| )$//;
527:
528: return &xmlparse($formula);
529: }
530:
1.46 albertel 531: sub prettyprint {
1.73 albertel 532: my ($value,$fmt,$target)=@_;
533: my $result;
534: my $dollarmode;
535: if (!$target) { $target = $external::target; }
1.75 albertel 536: if ($fmt =~ /chem/i) { return(&chemparse($value)); }
1.73 albertel 537: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
538: if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
539: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
540: my $frac=$1;
541: if ($dollarmode) { $frac=&dollarformat($frac); }
542: my $exponent=$2;
543: $exponent=~s/^\+0*//;
544: $exponent=~s/^-0*/-/;
545: $exponent=~s/^-0*/-/;
546: if ($exponent eq '-') { undef($exponent); }
547: if ($exponent) {
548: if ($target eq 'web') {
549: $result=$frac.'×10<sup>'.$exponent.'</sup>';
550: } elsif ($target eq 'tex') {
551: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
552: } else {
553: $result=$value;
554: }
555: } else {
556: $result=$frac;
557: }
558: } else {
1.48 albertel 559: $result=$value;
1.73 albertel 560: if ($dollarmode) { $result=&dollarformat($result,$target); }
1.46 albertel 561: }
1.73 albertel 562: return $result;
1.48 albertel 563: }
564:
565: sub dollarformat {
1.73 albertel 566: my ($number,$target) = @_;
567: if (!$target) { $target = $external::target; }
568: if ($number =~ /\./) {
569: while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
570: $number = $1.','.$2.$3;
571: }
572: } else {
573: while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
574: $number = $1.','.$2.$3;
575: }
576: }
577: if ($target eq 'tex') {
578: $number='\$'.$number; #' stupid emacs
579: } else {
580: $number='$'.$number; #' stupid emacs
581: }
582: return $number;
1.2 albertel 583: }
1.5 albertel 584:
585: sub map {
1.27 ng 586: my ($phrase,$dest,$source)=@_;
1.51 albertel 587: my @oldseed=&random_get_seed();
1.27 ng 588: my @seed = &random_seed_from_phrase($phrase);
589: &random_set_seed(@seed);
590: my $destct = scalar(@$dest);
1.28 ng 591: if (!$source) {
592: my @output;
593: my @idx = &math_random_permuted_index($destct);
594: my $ctr = 0;
595: while ($ctr < $destct) {
596: $output[$ctr] = $$dest[$idx[$ctr]];
1.27 ng 597: $ctr++;
1.28 ng 598: }
1.51 albertel 599: &random_set_seed(@oldseed);
1.28 ng 600: return @output;
1.27 ng 601: } else {
1.28 ng 602: my $num = scalar(@$source);
603: my @idx = &math_random_permuted_index($num);
604: my $ctr = 0;
605: my $tot = $num;
606: $tot = $destct if $destct < $num;
607: if (ref($$dest[0])) {
608: while ($ctr < $tot) {
609: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
610: $ctr++;
611: }
612: } else {
613: while ($ctr < $tot) {
614: $$dest[$ctr] = $$source[$idx[$ctr]];
615: $ctr++;
616: }
617: }
1.27 ng 618: }
1.56 albertel 619: &random_set_seed(@oldseed);
1.51 albertel 620: return '';
1.27 ng 621: }
622:
623: sub rmap {
624: my ($phrase,$dest,$source)=@_;
1.51 albertel 625: my @oldseed=&random_get_seed();
1.27 ng 626: my @seed = &random_seed_from_phrase($phrase);
627: &random_set_seed(@seed);
628: my $destct = scalar(@$dest);
1.28 ng 629: if (!$source) {
630: my @idx = &math_random_permuted_index($destct);
631: my $ctr = 0;
632: my @r_idx;
633: while ($ctr < $destct) {
634: $r_idx[$idx[$ctr]] = $ctr;
635: $ctr++;
636: }
637: my @output;
638: $ctr = 0;
639: while ($ctr < $destct) {
640: $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27 ng 641: $ctr++;
1.28 ng 642: }
1.51 albertel 643: &random_set_seed(@oldseed);
1.28 ng 644: return @output;
1.27 ng 645: } else {
1.28 ng 646: my $num = scalar(@$source);
647: my @idx = &math_random_permuted_index($num);
648: my $ctr = 0;
649: my $tot = $num;
650: $tot = $destct if $destct < $num;
651: my @r_idx;
1.27 ng 652: while ($ctr < $tot) {
1.28 ng 653: $r_idx[$idx[$ctr]] = $ctr;
1.27 ng 654: $ctr++;
1.28 ng 655: }
656: $ctr = 0;
657: if (ref($$dest[0])) {
658: while ($ctr < $tot) {
659: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
660: $ctr++;
661: }
662: } else {
663: while ($ctr < $tot) {
664: $$dest[$ctr] = $$source[$r_idx[$ctr]];
665: $ctr++;
666: }
667: }
1.6 albertel 668: }
1.51 albertel 669: &random_set_seed(@oldseed);
670: return '';
1.5 albertel 671: }
1.22 ng 672:
1.23 ng 673: sub capa_id { return }
674:
675: sub problem { return }
676:
1.22 ng 677: sub name{
1.73 albertel 678: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
679: $fullname = "" if $fullname eq ", ";
680: $fullname =~ s/\%2d/-/g;
681: return $fullname;
1.22 ng 682: }
683:
684: sub student_number {
1.73 albertel 685: my $id = &EXT('environment.id');
686: $id = '' if $id eq "";
687: return $id;
1.22 ng 688: }
689:
690: sub class {
1.73 albertel 691: my $course = &EXT('course.description');
692: $course = '' if $course eq "";
693: return $course;
1.22 ng 694: }
695:
696: sub sec {
1.73 albertel 697: my $sec = &EXT('request.course.sec');
698: $sec = '' if $sec eq "";
699: return $sec;
1.22 ng 700: }
701:
1.23 ng 702: sub open_date {
1.73 albertel 703: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
704: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
705: my @hm = split(/:/,$dc[3]);
706: my $ampm = " am";
707: if ($hm[0] > 12) {
708: $hm[0]-=12;
709: $ampm = " pm";
710: }
711: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 712: }
713:
714: sub due_date {
1.73 albertel 715: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
716: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
717: my @hm = split(/:/,$dc[3]);
718: my $ampm = " am";
719: if ($hm[0] > 12) {
720: $hm[0]-=12;
721: $ampm = " pm";
722: }
723: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 724: }
725:
726: sub answer_date {
1.73 albertel 727: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
728: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
729: my @hm = split(/:/,$dc[3]);
730: my $ampm = " am";
731: if ($hm[0] > 12) {
732: $hm[0]-=12;
733: $ampm = " pm";
734: }
735: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.24 ng 736: }
737:
738: sub array_moments {
1.73 albertel 739: my @input=@_;
740: my (@output,$N);
741: $N=scalar (@input);
742: $output[0]=$N;
743: if ($N <= 1) {
744: $output[1]=$input[0];
745: $output[1]="Input array not defined" if ($N == 0);
746: $output[2]="variance undefined for N<=1";
747: $output[3]="skewness undefined for N<=1";
748: $output[4]="kurtosis undefined for N<=1";
749: return @output;
750: }
751: my $sum=0;
752: foreach my $line (@input) {
753: $sum+=$line;
754: }
755: $output[1] = $sum/$N;
756: my ($x,$sdev,$var,$skew,$kurt) = 0;
757: foreach my $line (@input) {
758: $x=$line-$output[1];
759: $var+=$x**2;
760: $skew+=$x**3;
761: $kurt+=$x**4;
762: }
763: $output[2]=$var/($N-1);
764: $sdev=CORE::sqrt($output[2]);
765: if ($sdev == 0) {
766: $output[3]="inf-variance=0";
767: $output[4]="inf-variance=0";
768: return @output;
769: }
770: $output[3]=$skew/($sdev**3*$N);
771: $output[4]=$kurt/($sdev**4*$N)-3;
1.24 ng 772: return @output;
773: }
1.5 albertel 774:
775: sub choose {
1.73 albertel 776: my $num = $_[0];
777: return $_[$num];
1.5 albertel 778: }
1.23 ng 779:
1.49 albertel 780: # expiremental idea
781: sub proper_path {
1.73 albertel 782: my ($path)=@_;
783: if ( $external::target eq "tex" ) {
784: return '/home/httpd/html'.$path;
785: } else {
786: return $path;
787: }
1.49 albertel 788: }
1.23 ng 789:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>