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