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