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