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