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