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.44 2001/12/21 21:37:26 matthew 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: # Guy Albertelli
30: #
31: # 05/25/2001 H. K. Ng
32: # 05/31/2001 H. K. Ng
33: # 12/21/2001 Matthew
34: #
35: #init some globals
36: $hidden::RANDOMINIT=0;
37: $pi=atan2(1,1)*4;
38: $rad2deg=180.0/$pi;
39: $deg2rad=$pi/180.0;
40: $"=' ';
41:
42: sub caparesponse_check {
43: #not properly used yet: calc
44: #not to be used: $ans_fmt
45: my ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
46: eval $_[1].
47: ';return ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples);';
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: my ($response,$expr)=@_;
53:
54:
55: #type's definitons come from capaParser.h
56: my $message='';
57: #remove leading and trailing whitespace
58: if ($response=~ /^\s|\s$/) {
59: $response=~ s:^\s+|\s+$::g;
60: $message .="Removed ws now :$response:<br />";
61: } else {
62: $message .="no ws in :$response:<br />";
63: }
64:
65: if ($type eq '' ) {
66: $message .= "Didn't find a type :$type:$expr: defaulting<br />";
67: if ( $answer eq ($answer *1.0)) { $type = 2;
68: } else { $type = 3; }
69: } else {
70: if ($type eq 'cs') { $type = 4;
71: } elsif ($type eq 'ci') { $type = 3;
72: } elsif ($type eq 'mc') { $type = 5;
73: } elsif ($type eq 'fml') { $type = 8;
74: } elsif ($type eq 'subj') { $type = 7;
75: } elsif ($type eq 'float') { $type = 2;
76: } elsif ($type eq 'int') { $type = 1;
77: } else { return "ERROR: Unknown type of answer: $type" }
78: }
79:
80: my $points;
81: my $id_list;
82: #formula type setup the sample points
83: if ($type eq '8') {
84: ($id_list,$points)=split(/@/,$samples);
85: $message.="Found :$points: points<br />";
86: }
87: if ($tol eq '') {
88: $tol=0.0;
89: $tol_type=1; #TOL_ABSOLUTE
90: } else {
91: if ($tol =~ /%$/) {
92: chop $tol;
93: $tol_type=2; #TOL_PERCENTAGE
94: } else {
95: $tol_type=1; #TOL_ABSOLUTE
96: }
97: }
98:
99: if ($sig eq '') {
100: $sig_lbound = 0; #SIG_LB_DEFAULT
101: $sig_ubound =15; #SIG_UB_DEFAULT
102: } else {
103: ($sig_lbound,$sig_ubound) = split /,/,$sig;
104: }
105: my $result = &caparesponse_capa_check_answer($response,$answer,$type,
106: $tol_type,$tol,
107: $sig_lbound,$sig_ubound,
108: $ans_fmt,$unit,$calc,$id_list,
109: $points,$external::randomseed);
110:
111: if ($result == '1') { $result='EXACT_ANS'; }
112: elsif ($result == '2') { $result='APPROX_ANS'; }
113: elsif ($result == '3') { $result='SIG_FAIL'; }
114: elsif ($result == '4') { $result='UNIT_FAIL'; }
115: elsif ($result == '5') { $result='NO_UNIT'; }
116: elsif ($result == '6') { $result='UNIT_OK'; }
117: elsif ($result == '7') { $result='INCORRECT'; }
118: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
119: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
120: elsif ($result =='10') { $result='SUB_RECORDED'; }
121: elsif ($result =='11') { $result='BAD_FORMULA'; }
122: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
123: else {$result = "ERROR: Unknown Result:$result:$@:";}
124:
125: return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br /> type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|<br />$message$expr";
126: }
127:
128: sub get_array_args {
129: my ($expr,$arg)=@_;
130: # do these first, because who knows what varname the instructor might have used
131: # but it probably isn't $CAPARESPONSE_CHECK_LIST_answer
132: my $CAPARESPONSE_CHECK_LIST_answer = eval $expr.';return $'.$arg; #'
133: my $GET_ARRAY_ARGS_result;
134: my @GET_ARRAY_ARGS_list;
135: if ($CAPARESPONSE_CHECK_LIST_answer =~ /^\s*[\$\@]/) {
136: (@GET_ARRAY_ARGS_list) = eval $CAPARESPONSE_CHECK_LIST_answer;
137: }
138: $GET_ARRAY_ARGS_result.="error:$@:<br />";
139: # if the eval fails just use what is in the answer exactly
140: if (!defined(@GET_ARRAY_ARGS_list) || !defined($GET_ARRAY_ARGS_list[0])) {
141: $GET_ARRAY_ARGS_result.="list zero is undefined<br />";
142: $GET_ARRAY_ARGS_list[0]=$CAPARESPONSE_CHECK_LIST_answer;
143: }
144: return $GET_ARRAY_ARGS_result,@GET_ARRAY_ARGS_list;
145: }
146:
147: sub caparesponse_check_list {
148: my ($response,$expr)=@_;
149: my $result;
150: my ($result,@list) = &get_array_args($expr,'answer');
151: my $aresult='';
152: my $current_answer;
153: my $answers=join(':',@list);
154: $result.="Got response :$answers:<br />";
155: my @responselist;
156: my $type =eval $expr.';return $answer;';
157: if ($type ne '' && $#list > 0) {
158: (@responselist)=split /,/,$response;
159: } else {
160: (@responselist)=($response);
161: }
162: my $unit='';
163: $result.="Initial final response :$responselist['-1']:<br />";
164: if ($type eq '') {
165: #for numerical problems split off the unit
166: if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
167: $responselist['-1']=$1;
168: $unit=$2;
169: }
170: }
171: $result.="Final final response :$responselist['-1']:<br />";
172: $result.=":$#list: answers<br />";
173: $unit=~s/\s//;
174: my $i=0;
175: my $awards='';
176: for ($i=0; $i<@list;$i++) {
177: $result.="trying answer :$list[$i]:<br />";
178: if ($unit eq '') {
179: $aresult=&caparesponse_check($responselist[$i],
180: $expr.';my $answer=\''.$list[$i].'\';');
181: } else {
182: $aresult=&caparesponse_check($responselist[$i]." $unit",
183: $expr.';my $answer=\''.$list[$i].'\';');
184: }
185: my ($temp)=split /:/, $aresult;
186: $awards.="$temp,";
187: $result.=$aresult;
188: }
189: chop $awards;
190: return "$awards:<br />$result";
191: }
192:
193: sub tex {
194: if ( $external::target eq "tex" ) {
195: return @_[0];
196: } else {
197: return @_[1];
198: }
199: }
200:
201: sub var_in_tex {
202: if ( $external::target eq "tex" ) {
203: return @_[0];
204: } else {
205: return "";
206: }
207: }
208:
209: sub web {
210: if ( $external::target eq "tex" ) {
211: return @_[1];
212: } else {
213: if ( $external::target eq "web" || $external::target eq "answer") {
214: return @_[2];
215: } else {
216: return @_[0];
217: }
218: }
219: }
220:
221: sub html {
222: if ( $external::target eq "web" ) {
223: return shift;
224: }
225: }
226:
227: sub problem {
228: return '1';
229: }
230:
231: sub hinton {
232: return 0;
233: }
234:
235: sub random {
236: my ($start,$end,$step)=@_;
237: if ( ! $hidden::RANDOMINIT ) {
238: srand($external::randomseed);
239: $hidden::RANDOMINIT=1;
240: }
241: my $num=1+int(($end-$start)/$step);
242: my $result=$start + int(rand() * $num)*$step;
243: return $result;
244: }
245:
246: sub random_normal {
247: my ($item_cnt,$seed,$av,$std_dev) = @_;
248: my @retArray;
249: &random_set_seed_from_phrase($seed);
250: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
251: return @retArray;
252: }
253:
254: sub random_beta {
255: my ($item_cnt,$seed,$aa,$bb) = @_;
256: my @retArray;
257: &random_set_seed_from_phrase($seed);
258: @retArray=&math_random_beta($item_cnt,$aa,$bb);
259: return @retArray;
260: }
261:
262: sub random_gamma {
263: my ($item_cnt,$seed,$a,$r) = @_;
264: my @retArray;
265: &random_set_seed_from_phrase($seed);
266: @retArray=&math_random_gamma($item_cnt,$a,$r);
267: return @retArray;
268: }
269:
270: sub random_exponential {
271: my ($item_cnt,$seed,$av) = @_;
272: my @retArray;
273: &random_set_seed_from_phrase($seed);
274: @retArray=&math_random_exponential($item_cnt,$av);
275: return @retArray;
276: }
277:
278: sub random_poisson {
279: my ($item_cnt,$seed,$mu) = @_;
280: my @retArray;
281: &random_set_seed_from_phrase($seed);
282: @retArray=&math_random_poisson($item_cnt,$mu);
283: return @retArray;
284: }
285:
286: sub random_chi {
287: my ($item_cnt,$seed,$df) = @_;
288: my @retArray;
289: &random_set_seed_from_phrase($seed);
290: @retArray=&math_random_chi_square($item_cnt,$df);
291: return @retArray;
292: }
293:
294: sub random_noncentral_chi {
295: my ($item_cnt,$seed,$df,$nonc) = @_;
296: my @retArray;
297: &random_set_seed_from_phrase($seed);
298: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
299: return @retArray;
300: }
301:
302: sub random_f {
303: my ($item_cnt,$seed,$dfn,$dfd) = @_;
304: my @retArray;
305: &random_set_seed_from_phrase($seed);
306: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
307: return @retArray;
308: }
309:
310: sub random_noncentral_f {
311: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
312: my @retArray;
313: &random_set_seed_from_phrase($seed);
314: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
315: return @retArray;
316: }
317:
318: sub random_multivariate_normal {
319: my ($item_cnt,$seed,$mean,$covar) = @_;
320: &random_set_seed_from_phrase($seed);
321: @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
322: return @retArray;
323: }
324:
325: sub random_multinomial {
326: my ($item_cnt,$seed,@p) = @_;
327: my @retArray;
328: &random_set_seed_from_phrase($seed);
329: @retArray=&math_random_multinomial($item_cnt,@p);
330: return @retArray;
331: }
332:
333: sub random_permutation {
334: my ($seed,@inArray) = @_;
335: my @retArray;
336: &random_set_seed_from_phrase($seed);
337: @retArray=&math_random_permutation(@inArray);
338: return @retArray;
339: }
340:
341: sub random_uniform {
342: my ($item_cnt,$seed,$low,$high) = @_;
343: my @retArray;
344: &random_set_seed_from_phrase($seed);
345: @retArray=&math_random_uniform($item_cnt,$low,$high);
346: return @retArray;
347: }
348:
349: sub random_uniform_integer {
350: my ($item_cnt,$seed,$low,$high) = @_;
351: my @retArray;
352: &random_set_seed_from_phrase($seed);
353: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
354: return @retArray;
355: }
356:
357: sub random_binomial {
358: my ($item_cnt,$seed,$nt,$p) = @_;
359: my @retArray;
360: &random_set_seed_from_phrase($seed);
361: @retArray=&math_random_binomial($item_cnt,$nt,$p);
362: return @retArray;
363: }
364:
365: sub random_negative_binomial {
366: my ($item_cnt,$seed,$ne,$p) = @_;
367: my @retArray;
368: &random_set_seed_from_phrase($seed);
369: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
370: return @retArray;
371: }
372:
373: sub abs { abs(shift) }
374: sub sin { sin(shift) }
375: sub cos { cos(shift) }
376: sub exp { exp(shift) }
377: sub int { int(shift) }
378: sub log { log(shift) }
379: sub atan2 { atan2($_[0],$_[1]) }
380: sub sqrt { sqrt(shift) }
381:
382: sub tan { sin($_[0]) / cos($_[0]) }
383: #sub atan { atan2($_[0], 1); }
384: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
385: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
386:
387: sub log10 { log($_[0])/log(10); }
388:
389: sub factorial {
390: my $input = int(shift);
391: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
392: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
393: return 1 if $input == 0;
394: my $result = 1;
395: for (my $i=2; $i<=$input; $i++) { $result *= $i }
396: return $result;
397: }
398:
399: sub sgn {
400: return -1 if $_[0] < 0;
401: return 0 if $_[0] == 0;
402: return 1 if $_[0] > 0;
403: }
404:
405: sub min {
406: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
407: return shift @sorted;
408: }
409:
410: sub max {
411: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
412: return pop @sorted;
413: }
414:
415: sub roundto {
416: my ($input,$n) = @_;
417: return sprintf('%.'.$n.'f',$input);
418: }
419:
420: sub to_string {
421: my ($input,$n) = @_;
422: return sprintf($input) if $n eq "";
423: $n = '.'.$n if $n !~ /^\./;
424: return sprintf('%'.$n,$input) if $n ne "";
425: }
426:
427: sub sub_string {
428: my ($str,$start,$len) = @_;
429: return substr($str,$start-1,$len);
430: }
431:
432: sub pow {return $_[0] ** $_[1]; }
433: sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (int($_[0])+ 1) : int($_[0])); }
434: sub floor {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? int($_[0]) : (int($_[0])-1)); }
435: #sub floor {return int($_[0]); }
436:
437: sub format {
438: my ($value,$fmt)=@_;
439: return sprintf('%.'.$fmt,$value);
440: }
441:
442: sub map {
443: my ($phrase,$dest,$source)=@_;
444: my @seed = &random_seed_from_phrase($phrase);
445: &random_set_seed(@seed);
446: my $destct = scalar(@$dest);
447: if (!$source) {
448: my @output;
449: my @idx = &math_random_permuted_index($destct);
450: my $ctr = 0;
451: while ($ctr < $destct) {
452: $output[$ctr] = $$dest[$idx[$ctr]];
453: $ctr++;
454: }
455: return @output;
456: } else {
457: my $num = scalar(@$source);
458: my @idx = &math_random_permuted_index($num);
459: my $ctr = 0;
460: my $tot = $num;
461: $tot = $destct if $destct < $num;
462: if (ref($$dest[0])) {
463: while ($ctr < $tot) {
464: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
465: $ctr++;
466: }
467: } else {
468: while ($ctr < $tot) {
469: $$dest[$ctr] = $$source[$idx[$ctr]];
470: $ctr++;
471: }
472: }
473: }
474: }
475:
476: sub rmap {
477: my ($phrase,$dest,$source)=@_;
478: my @seed = &random_seed_from_phrase($phrase);
479: &random_set_seed(@seed);
480: my $destct = scalar(@$dest);
481: if (!$source) {
482: my @idx = &math_random_permuted_index($destct);
483: my $ctr = 0;
484: my @r_idx;
485: while ($ctr < $destct) {
486: $r_idx[$idx[$ctr]] = $ctr;
487: $ctr++;
488: }
489: my @output;
490: $ctr = 0;
491: while ($ctr < $destct) {
492: $output[$ctr] = $$dest[$r_idx[$ctr]];
493: $ctr++;
494: }
495: return @output;
496: } else {
497: my $num = scalar(@$source);
498: my @idx = &math_random_permuted_index($num);
499: my $ctr = 0;
500: my $tot = $num;
501: $tot = $destct if $destct < $num;
502: my @r_idx;
503: while ($ctr < $tot) {
504: $r_idx[$idx[$ctr]] = $ctr;
505: $ctr++;
506: }
507: $ctr = 0;
508: if (ref($$dest[0])) {
509: while ($ctr < $tot) {
510: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
511: $ctr++;
512: }
513: } else {
514: while ($ctr < $tot) {
515: $$dest[$ctr] = $$source[$r_idx[$ctr]];
516: $ctr++;
517: }
518: }
519: }
520: }
521:
522: sub capa_id { return }
523:
524: sub problem { return }
525:
526: sub name{
527: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
528: $fullname = "" if $fullname eq ", ";
529: $fullname =~ s/\%2d/-/g;
530: return $fullname;
531: }
532:
533: sub student_number {
534: my $id = &EXT('environment.id');
535: $id = '' if $id eq "";
536: return $id;
537: }
538:
539: sub class {
540: my $course = &EXT('course.description');
541: $course = '' if $course eq "";
542: return $course;
543: }
544:
545: sub sec {
546: my $sec = &EXT('request.course.sec');
547: $sec = '' if $sec eq "";
548: return $sec;
549: }
550:
551: sub open_date {
552: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
553: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
554: my @hm = split(/:/,$dc[3]);
555: my $ampm = " am";
556: if ($hm[0] > 12) {
557: $hm[0]-=12;
558: $ampm = " pm";
559: }
560: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
561: }
562:
563: sub due_date {
564: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
565: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
566: my @hm = split(/:/,$dc[3]);
567: my $ampm = " am";
568: if ($hm[0] > 12) {
569: $hm[0]-=12;
570: $ampm = " pm";
571: }
572: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
573: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
574: }
575:
576: sub answer_date {
577: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
578: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
579: my @hm = split(/:/,$dc[3]);
580: my $ampm = " am";
581: if ($hm[0] > 12) {
582: $hm[0]-=12;
583: $ampm = " pm";
584: }
585: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
586: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
587: }
588:
589: sub array_moments {
590: my @input=@_;
591: my (@output,$N);
592: $N=scalar (@input);
593: $output[0]=$N;
594: if ($N <= 1) {
595: $output[1]=$input[0];
596: $output[1]="Input array not defined" if ($N == 0);
597: $output[2]="variance undefined for N<=1";
598: $output[3]="skewness undefined for N<=1";
599: $output[4]="kurtosis undefined for N<=1";
600: return @output;
601: }
602: my $sum=0;
603: foreach my $line (@input) {
604: $sum+=$line;
605: }
606: $output[1] = $sum/$N;
607: my ($x,$sdev,$var,$skew,$kurt) = 0;
608: foreach my $line (@input) {
609: $x=$line-$output[1];
610: $var+=$x**2;
611: $skew+=$x**3;
612: $kurt+=$x**4;
613: }
614: $output[2]=$var/($N-1);
615: $sdev=sqrt($output[2]);
616: if ($sdev == 0) {
617: $output[3]="inf-variance=0";
618: $output[4]="inf-variance=0";
619: return @output;
620: }
621: $output[3]=$skew/($sdev**3*$N);
622: $output[4]=$kurt/($sdev**4*$N)-3;
623: return @output;
624: }
625:
626: sub choose {
627: my $num = $_[0];
628: return $_[$num];
629: }
630:
631:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>