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