Annotation of loncom/homework/default_homework.lcpm, revision 1.152.2.3
1.42 albertel 1: # The LearningOnline Network with CAPA
1.1 harris41 2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
1.42 albertel 3: #
1.152.2.3! raeburn 4: # $Id: default_homework.lcpm,v 1.152.2.2 2011/10/06 17:20:37 raeburn Exp $
1.42 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.20 harris41 28: #
1.76 albertel 29:
1.25 albertel 30: #init some globals
1.38 albertel 31: $hidden::RANDOMINIT=0;
1.22 ng 32: $pi=atan2(1,1)*4;
33: $rad2deg=180.0/$pi;
34: $deg2rad=$pi/180.0;
1.44 matthew 35: $"=' ';
1.116 albertel 36: use strict;
37: {
38: my $n = 0;
39: my $total = 0;
40: my $num_left = 0;
41: my @order;
42: my $type;
43:
44: sub init_permutation {
45: my ($size,$requested_type) = @_;
46: @order = (0..$size-1);
47: $n = $size;
48: $type = $requested_type;
49: if ($type eq 'ordered') {
50: $total = $num_left = 1;
51: } elsif ($type eq 'unordered') {
52: $total = $num_left = &factorial($size);
53: } else {
54: die("Unkown type: $type");
55: }
56: }
57:
58: sub get_next_permutation {
59: if ($num_left == $total) {
60: $num_left--;
1.117 albertel 61: return \@order;
1.116 albertel 62: }
63:
64: # Find largest index j with a[j] < a[j+1]
65:
66: my $j = scalar(@order) - 2;
67: while ($order[$j] > $order[$j+1]) {
68: $j--;
69: }
70:
71: # Find index k such that a[k] is smallest integer
72: # greater than a[j] to the right of a[j]
73:
74: my $k = scalar(@order) - 1;
75: while ($order[$j] > $order[$k]) {
76: $k--;
77: }
78:
79: # Interchange a[j] and a[k]
80:
81: @order[($k,$j)] = @order[($j,$k)];
82:
83: # Put tail end of permutation after jth position in increasing order
84:
85: my $r = scalar(@order) - 1;
86: my $s = $j + 1;
87:
88: while ($r > $s) {
89: @order[($s,$r)]=@order[($r,$s)];
90: $r--;
91: $s++;
92: }
93:
94: $num_left--;
1.117 albertel 95: return(\@order);
1.116 albertel 96: }
97:
98: sub get_permutations_left {
99: return $num_left;
100: }
101: }
1.3 albertel 102:
1.91 albertel 103: sub check_commas {
104: my ($response)=@_;
105: #print("$response ");
106: my @numbers=split(',',$response);
107: #print(" numbers ".join('-',@numbers)." ");
108: if (scalar(@numbers) > 1) {
109: #print(" numbers[0] ".$numbers[0]." ");
110: if (length($numbers[0]) > 3 || length($numbers[0]) == 0) { return -1; }
111: shift(@numbers);
112: #print(" numbers ".scalar(@numbers)." ");
113: while (scalar(@numbers) > 1) {
114: #print(" numbers ".join('-',@numbers)." ");
115: if (length($numbers[0]) != 3) { return -2; }
116: shift(@numbers);
117: }
118: my ($number)=split('\.',$numbers[0]);
119: #print(" number ".$number." ");
120: #print(" numbers[0] ".$numbers[0]." ");
121: if (length($number) != 3) { return -3; }
122: } else {
123: my ($number)=split('\.',$numbers[0]);
124: if (length($number) > 3) { return -4; }
125: }
126: return 1;
127: }
128:
1.117 albertel 129:
1.7 albertel 130: sub caparesponse_check {
1.74 albertel 131: my ($answer,$response)=@_;
1.73 albertel 132: #not properly used yet: calc
133: #not to be used: $ans_fmt
1.74 albertel 134: my $type=$LONCAPA::CAPAresponse_args{'type'};
135: my $tol=$LONCAPA::CAPAresponse_args{'tol'};
136: my $sig=$LONCAPA::CAPAresponse_args{'sig'};
1.88 albertel 137: my $ans_fmt=$LONCAPA::CAPAresponse_args{'format'};
1.74 albertel 138: my $unit=$LONCAPA::CAPAresponse_args{'unit'};
139: my $calc=$LONCAPA::CAPAresponse_args{'calc'};
140: my $samples=$LONCAPA::CAPAresponse_args{'samples'};
1.73 albertel 141:
142: my $tol_type=''; # gets it's value from whether tol has a % or not done
143: my $sig_lbound=''; #done
144: my $sig_ubound=''; #done
145:
146: #type's definitons come from capaParser.h
1.116 albertel 147:
1.73 albertel 148: #remove leading and trailing whitespace
149: if (!defined($response)) {
150: $response='';
151: }
152: if ($response=~ /^\s|\s$/) {
153: $response=~ s:^\s+|\s+$::g;
154: }
1.116 albertel 155:
1.100 albertel 156: if ($type eq 'cs' || $type eq 'ci') {
1.130 www 157: #for string answers make sure all places spaces occur, there is
1.76 albertel 158: #really only 1 space, in both the answer and the response
159: $answer=~s/ +/ /g;
160: $response=~s/ +/ /g;
1.100 albertel 161: } elsif ($type eq 'mc') {
162: $answer=~s/[\s,]//g;
163: $response=~s/[\s,]//g;
1.76 albertel 164: }
1.91 albertel 165: if ($type eq 'float' && $unit=~/\$/) {
1.126 www 166: if ($response!~/^\$|\$$/) { return ('NO_UNIT', undef); }
1.88 albertel 167: $response=~s/\$//g;
168: }
1.91 albertel 169: if ($type eq 'float' && $unit=~/\,/ && (&check_commas($response)<0)) {
170: return "COMMA_FAIL:";
171: }
1.88 albertel 172: $ans_fmt=~s/\W//g;
1.91 albertel 173: $unit=~s/[\$,]//g;
174: if ($type eq 'float') { $response=~s/,//g; }
1.88 albertel 175:
1.117 albertel 176: if (length($response) > 500) { return ('TOO_LONG',undef); }
1.73 albertel 177:
178: if ($type eq '' ) {
179: if ( $answer eq ($answer *1.0)) { $type = 2;
180: } else { $type = 3; }
181: } else {
1.107 albertel 182: if ($type eq 'cs') { $type = 4; }
1.73 albertel 183: elsif ($type eq 'ci') { $type = 3 }
184: elsif ($type eq 'mc') { $type = 5; }
185: elsif ($type eq 'fml') { $type = 8; }
1.107 albertel 186: elsif ($type eq 'math') { $type = 9; }
1.73 albertel 187: elsif ($type eq 'subj') { $type = 7; }
188: elsif ($type eq 'float') { $type = 2; }
189: elsif ($type eq 'int') { $type = 1; }
1.117 albertel 190: else { return ('ERROR', "Unknown type of answer: $type") }
1.73 albertel 191: }
192:
193: my $points;
194: my $id_list;
195: #formula type setup the sample points
196: if ($type eq '8') {
197: ($id_list,$points)=split(/@/,$samples);
198: }
199: if ($tol eq '') {
200: $tol=0.0;
201: $tol_type=1; #TOL_ABSOLUTE
202: } else {
203: if ($tol =~ /%$/) {
204: chop $tol;
205: $tol_type=2; #TOL_PERCENTAGE
206: } else {
207: $tol_type=1; #TOL_ABSOLUTE
208: }
209: }
210:
1.85 albertel 211: ($sig_ubound,$sig_lbound)=&LONCAPA_INTERNAL_get_sigrange($sig);
212:
1.73 albertel 213: my $reterror="";
1.107 albertel 214: my $result;
1.142 raeburn 215: if (($type eq '9') || ($type eq '8')) {
216: if ($response=~/\=/) {
217: return ('BAD_FORMULA','Please submit just an expression, not an equation.');
1.143 riegler 218: } elsif ($response =~ /\,/ and $response !~ /^\s*\{.*\}\s*$/) {
1.142 raeburn 219: return ('BAD_FORMULA');
220: }
221: }
1.107 albertel 222: if ($type eq '9') {
1.108 www 223: $result = &maxima_check(&maxima_cas_formula_fix($response),&maxima_cas_formula_fix($answer),\$reterror);
1.107 albertel 224: } else {
1.109 albertel 225: if ($type eq '8') { # fml type
226: $response = &capa_formula_fix($response);
227: $answer = &capa_formula_fix($answer);
228: }
229: $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.73 albertel 230: $tol_type,$tol,
231: $sig_lbound,$sig_ubound,
232: $ans_fmt,$unit,$calc,$id_list,
233: $points,$external::randomseed,
234: \$reterror);
1.107 albertel 235: }
1.73 albertel 236: if ($result == '1') { $result='EXACT_ANS'; }
237: elsif ($result == '2') { $result='APPROX_ANS'; }
238: elsif ($result == '3') { $result='SIG_FAIL'; }
239: elsif ($result == '4') { $result='UNIT_FAIL'; }
240: elsif ($result == '5') { $result='NO_UNIT'; }
241: elsif ($result == '6') { $result='UNIT_OK'; }
242: elsif ($result == '7') { $result='INCORRECT'; }
243: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
244: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
245: elsif ($result =='10') { $result='SUB_RECORDED'; }
246: elsif ($result =='11') { $result='BAD_FORMULA'; }
1.94 albertel 247: elsif ($result =='12' && !$response) { $result='MISSING_ANSWER'; }
248: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.77 albertel 249: elsif ($result =='13') { $result='UNIT_INVALID_INSTRUCTOR'; }
250: elsif ($result =='141') { $result='UNIT_INVALID_STUDENT'; }
251: elsif ($result =='142') { $result='UNIT_INVALID_STUDENT'; }
252: elsif ($result =='143') { $result='UNIT_INVALID_STUDENT'; }
253: elsif ($result =='15') { $result='UNIT_IRRECONCIBLE'; }
1.73 albertel 254: else {$result = "ERROR: Unknown Result:$result:$@:";}
255:
1.152.2.2 raeburn 256: # &LONCAPA_INTERNAL_DEBUG("RetError $reterror: Answer $answer: Response $response: type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|");
257: # &LONCAPA_INTERNAL_DEBUG(" $answer $response $result ");
1.139 raeburn 258: return ($result,$reterror);
1.37 albertel 259: }
260:
1.73 albertel 261:
1.37 albertel 262: sub caparesponse_check_list {
1.119 albertel 263: my $responses=$LONCAPA::CAPAresponse_args{'response'};
1.105 albertel 264: &LONCAPA_INTERNAL_DEBUG("args ".join(':',%LONCAPA::CAPAresponse_args));
1.74 albertel 265: my $type = $LONCAPA::CAPAresponse_args{'type'};
1.133 www 266: my $answerunit=$LONCAPA::CAPAresponse_args{'unit'};
267: &LONCAPA_INTERNAL_DEBUG("Got type :$type: answer unit :$answerunit:\n");
1.119 albertel 268:
1.152.2.2 raeburn 269: my $preprocess=$LONCAPA::CAPAresponse_args{'preprocess'};
270: $preprocess=~s/^\&//;
271:
1.119 albertel 272: my $num_input_lines =
273: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}});
1.120 albertel 274:
275: if ($type ne '' ) {
1.119 albertel 276: if (scalar(@$responses) < $num_input_lines) {
1.105 albertel 277: return 'MISSING_ANSWER';
278: }
1.119 albertel 279: if (scalar(@$responses) > $num_input_lines) {
280: return 'EXTRA_ANSWER';
281: }
282:
283: }
284:
285: foreach my $which (0..($num_input_lines-1)) {
286: my $answer_size =
287: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}[$which]});
288: if ($type ne ''
289: && $answer_size > 1) {
290: $responses->[$which]=[split(/,/,$responses->[$which])];
291: } else {
292: $responses->[$which]=[$responses->[$which]];
293: }
294: }
295: foreach my $which (0..($num_input_lines-1)) {
296: my $answer_size =
297: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}[$which]});
298: my $response_size =
299: scalar(@{$responses->[$which]});
300: if ($answer_size > $response_size) {
301: return 'MISSING_ANSWER';
302: }
303: if ($answer_size < $response_size) {
1.105 albertel 304: return 'EXTRA_ANSWER';
305: }
1.73 albertel 306: }
1.119 albertel 307:
1.105 albertel 308: my $unit;
1.152.2.2 raeburn 309: my ($allowalgebra)=($LONCAPA::CAPAresponse_args{'allowalgebra'}=~/^(yes|1|on)$/i);
1.140 raeburn 310: if ($type eq 'float' || $type eq '') {
1.73 albertel 311: #for numerical problems split off the unit
1.152.2.2 raeburn 312: my ($part1,$part2);
313: if ($allowalgebra) {
314: ($part1,$part2)=($responses->[0][-1]=~ /^(.*[^\s])\s+([^\s]+)$/);
315: } else {
316: ($part1,$part2)=($responses->[0][-1]=~ /^([\d\.\,\s\$]*(?:(?:[xX\*]10[\^\*]*|[eE]*)[\+\-]*\d*)*(?:^|\S)\d+)([\$\s\w\^\*\/\(\)\+\-]*[^\d\.\s\,][\$\s\w\^\*\/\(\)\+\-]*)$/);
317: }
318: if (defined($part1) && defined($part2)) {
319: $responses->[0][-1]=$part1;
320: $unit=&capa_formula_fix($part2);
321: my $customunits=$LONCAPA::CAPAresponse_args{'customunits'};
322: if ($customunits =~ /\S/) {
323: foreach my $replacement (split(/\s*\,\s*/,$customunits)) {
324: my ($which,$what)=split(/\s*\=\s*/,$replacement);
325: if ((defined($which)) && (defined($what))) {
326: $what=&capa_formula_fix($what);
327: $unit=~s/$which/\($what\)/g;
328: }
329: }
330: }
1.73 albertel 331: }
332: }
333: $unit=~s/\s//;
1.149 raeburn 334: my $error;
1.133 www 335: foreach my $response (@$responses) {
1.152.2.2 raeburn 336: foreach my $element (@$response) {
337: # See if we have preprocessor
338: if ($preprocess=~/\S/) {
339: if (defined(&$preprocess)) {
340: no strict 'refs';
341: $element=&$preprocess($element,$unit);
342: use strict 'refs';
343: }
344: }
1.138 raeburn 345: if (($type eq 'float') || (($type eq '') && ($unit ne ''))) {
346: $element =~ s/\s//g;
347: }
1.133 www 348: my $appendunit=$unit;
1.147 www 349: # Deal with percentages
350: # unit is unit entered by student, answerunit is unit by author
351: # Deprecated: divide answer by 100 if student entered percent,
352: # but author did not. Too much confusion
1.146 www 353: # if (($unit=~/\%/) && ($answerunit ne '%')) {
354: # $element=$element/100;
355: # $appendunit=~s/\%//;
356: # }
1.147 www 357: # Author entered percent, student did not
358: if (($unit!~/\%/) && ($answerunit=~/\%/)) {
359: $element=$element*100;
360: $appendunit='%'.$appendunit;
361: }
362: # Zero does not need a dimension
1.152.2.1 raeburn 363: if (($element =~ /^[0\.]+$/) && ($unit!~/\w/) && ($answerunit=~/\w/)) {
1.133 www 364: $appendunit=$answerunit;
365: }
1.152.2.2 raeburn 366: # Do the math for the student if allowed
367: if ($allowalgebra) {
368: $element=&cas('maxima',$element);
369: }
1.148 raeburn 370: if ($appendunit ne '') {
371: $element .= " $appendunit";
372: }
1.133 www 373: &LONCAPA_INTERNAL_DEBUG("Made response element :$element:");
374: }
1.119 albertel 375: }
376:
1.117 albertel 377: foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
378: if (!defined($thisanswer)) {
379: return ('ERROR','answer was undefined');
380: }
381: }
382:
1.150 raeburn 383: my $allow_control_char = 0;
384: my $control_chars_removed = 0;
1.151 raeburn 385: my $ansstring;
1.150 raeburn 386: if ($type eq 'cs' || $type eq 'ci') {
387: if (ref($LONCAPA::CAPAresponse_answer->{'answers'}) eq 'ARRAY') {
388: foreach my $strans (@{$LONCAPA::CAPAresponse_answer->{'answers'}}) {
1.151 raeburn 389: if (ref($strans) eq 'ARRAY') {
1.152 raeburn 390: $ansstring = join("\0",@{$strans});
1.151 raeburn 391: foreach my $item (@{$strans}) {
392: if ($item =~ /[\000-\037]/) {
393: $allow_control_char = 1;
394: }
395: }
396: }
397: }
398: }
399: }
1.117 albertel 400:
1.121 albertel 401: # &LONCAPA_INTERNAL_DEBUG(&LONCAPA_INTERNAL_Dumper($responses));
1.117 albertel 402: my %memoized;
403: if ($LONCAPA::CAPAresponse_answer->{'type'} eq 'ordered') {
1.119 albertel 404: for (my $i=0; $i<scalar(@$responses);$i++) {
1.117 albertel 405: my $answer = $LONCAPA::CAPAresponse_answer->{'answers'}[$i];
1.119 albertel 406: my $response = $responses->[$i];
1.117 albertel 407: my $key = "$answer\0$response";
1.119 albertel 408: my (@awards,@msgs);
1.150 raeburn 409: for (my $j=0; $j<scalar(@$response); $j++) {
410: if ($type eq 'cs' || $type eq 'ci') {
411: unless ($allow_control_char) {
412: if ($response->[$j] =~ /[\000-\037]/) {
413: $response->[$j] =~ s/[\000-\037]//g;
414: $control_chars_removed = 1;
415: }
416: }
417: }
1.152.2.2 raeburn 418: # See if we have preprocessor for string responses
419: if (($preprocess=~/\S/) && ($type eq 'cs' || $type eq 'ci')) {
420: if (defined(&$preprocess)) {
421: no strict 'refs';
422: $response->[$j]=&$preprocess($response->[$j]);
423: use strict 'refs';
424: }
425: }
426:
1.119 albertel 427: my ($award,$msg) = &caparesponse_check($answer->[$j],
428: $response->[$j]);
1.149 raeburn 429: if ($type eq 'cs' || $type eq 'ci') {
430: $error = &verify_stringresponse($type,$award,$response->[$j],
431: $answer->[$j]);
432: }
1.119 albertel 433: push(@awards,$award);
434: push(@msgs, $msg);
435: }
436: my ($award,$msg) =
437: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
438: $memoized{$key} = [$award,$msg];
1.117 albertel 439: }
440: } else {
1.119 albertel 441: #FIXME broken with unorder responses where one is a <value>
442: # and the other is a <vector> (need to delay parse til
443: # inside the loop?)
444: foreach my $response (@$responses) {
445: my $response_size = scalar(@{$response});
1.117 albertel 446: foreach my $answer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
447: my $key = "$answer\0$response";
1.119 albertel 448: my $answer_size = scalar(@{$answer});
449: my ($award,$msg);
450: if ($answer_size > $response_size) {
451: $award = 'MISSING_ANSWER';
452: } elsif ($answer_size < $response_size) {
453: $award = 'EXTRA_ANSWER';
454: } else {
455: my (@awards,@msgs);
456: for (my $j=0; $j<scalar(@$response); $j++) {
1.150 raeburn 457: if ($type eq 'cs' || $type eq 'ci') {
458: unless ($allow_control_char) {
459: if ($response->[$j] =~ /[\000-\037]/) {
460: $response->[$j] =~ s/[\000-\037]//g;
461: $control_chars_removed = 1;
462: }
463: }
464: }
1.152.2.2 raeburn 465: # See if we have preprocessor
466: if (($preprocess=~/\S/) && ($type eq 'cs' || $type eq 'ci')) {
467: if (defined(&$preprocess)) {
468: no strict 'refs';
469: $response->[$j]=&$preprocess($response->[$j]);
470: use strict 'refs';
471: }
472: }
473:
1.119 albertel 474: my ($award,$msg) = &caparesponse_check($answer->[$j],
475: $response->[$j]);
1.149 raeburn 476: if ($type eq 'cs' || $type eq 'ci') {
477: $error = &verify_stringresponse($type,$award,$response->[$j],
478: $answer->[$j]);
479: }
1.119 albertel 480: push(@awards,$award);
481: push(@msgs, $msg);
482: }
483: ($award,$msg) =
484: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
485: }
486: $memoized{$key} = [$award,$msg];
1.117 albertel 487: }
488: }
489: }
490:
1.116 albertel 491: my ($final_award,$final_msg);
1.119 albertel 492: &init_permutation(scalar(@$responses),
1.116 albertel 493: $LONCAPA::CAPAresponse_answer->{'type'});
1.117 albertel 494:
1.118 albertel 495: # possible FIXMEs
496: # - significant time is spent calling non-safe space routine
497: # from safe space
498: # - early outs could be possible with classifying awards is to stratas
499: # and stopping as so as hitting the top strata
500: # - some early outs also might be possible with check ing the
501: # memoized hash of results (is correct even possible? etc.)
502:
1.117 albertel 503: my (@final_awards,@final_msg);
1.116 albertel 504: while( &get_permutations_left() ) {
1.117 albertel 505: my $order = &get_next_permutation();
1.116 albertel 506: my (@awards, @msgs, $i);
507: foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
1.119 albertel 508: my $key = "$thisanswer\0".$responses->[$order->[$i]];
1.117 albertel 509: push(@awards,$memoized{$key}[0]);
510: push(@msgs,$memoized{$key}[1]);
1.116 albertel 511: $i++;
1.119 albertel 512:
1.116 albertel 513: }
1.119 albertel 514: &LONCAPA_INTERNAL_DEBUG(" all awards ".join(':',@awards));
515:
1.116 albertel 516: my ($possible_award,$possible_msg) =
517: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
1.119 albertel 518: &LONCAPA_INTERNAL_DEBUG(" pos awards ".$possible_award);
1.117 albertel 519: push(@final_awards,$possible_award);
520: push(@final_msg,$possible_msg);
1.73 albertel 521: }
1.117 albertel 522:
1.119 albertel 523: &LONCAPA_INTERNAL_DEBUG(" all final_awards ".join(':',@final_awards));
1.117 albertel 524: my ($final_award,$final_msg) =
525: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@final_awards,\@final_msg,undef,1);
1.151 raeburn 526: return ($final_award,$final_msg,$error,$control_chars_removed,$ansstring);
1.149 raeburn 527: }
528:
529: sub verify_stringresponse {
530: my ($type,$award,$resp,$ans) = @_;
531: return if ($award eq 'EXACT_ANS');
532: my $error;
533: if ($resp =~ /^\s|\s$/) {
534: $resp =~ s{^\s+|\s+$}{}g;
535: }
536: if ($ans =~ /^\s|\s$/) {
537: $ans =~ s{^\s+|\s+$}{}g;
538: }
539: if ($type eq 'ci') {
540: $resp = lc($resp);
541: $ans = lc($ans);
542: }
543: if ($resp eq $ans) {
544: if ($award eq 'INCORRECT') {
545: $error = 'MISGRADED';
546: }
547: }
548: return $error;
1.7 albertel 549: }
550:
1.124 www 551: sub cas {
1.137 www 552: my ($system,$input,$library)=@_;
1.124 www 553: my $output;
1.145 www 554: my $dump;
1.124 www 555: if ($system eq 'maxima') {
1.137 www 556: $output=&maxima_eval($input,$library);
1.144 www 557: } elsif ($system eq 'R') {
1.145 www 558: ($output,$dump)=&r_eval($input,$library,0);
1.137 www 559: } else {
560: $output='Error: unrecognized CAS';
1.124 www 561: }
562: return $output;
563: }
564:
1.145 www 565: sub cas_hashref {
566: my ($system,$input,$library)=@_;
567: if ($system eq 'maxima') {
568: return 'Error: unsupported CAS';
569: } elsif ($system eq 'R') {
570: return &r_eval($input,$library,1);
571: } else {
572: return 'Error: unrecognized CAS';
573: }
574: }
575:
576: #
577: # cas_hashref_entry takes a list of indices and gets the entry in a hash generated by Rreturn.
578: # Call: cas_hashref_entry(Rvalue, index1, index2, ...) where Rvalue is a hash returned by Rreturn.
579: # Rentry will return the first scalar value it encounters (ignoring excess indices).
580: # If an invalid key is given, it returns undef.
581: #
582: sub cas_hashref_entry {
583: return &Rentry(@_);
584: }
585:
586: #
587: # cas_hashref_array takes a list of indices and gets a column array from a hash generated by Rreturn.
588: # Call: cas_hashref_array(Rvalue, index1, index2, ...) where Rvalue is a hash returned by Rreturn.
589: # If an invalid key is given, it returns undef.
590: #
591: sub cas_hashref_array {
592: return &Rarray(@_);
593: }
594:
1.4 albertel 595: sub tex {
1.73 albertel 596: if ( $external::target eq "tex" ) {
597: return $_[0];
598: } else {
599: return $_[1];
600: }
1.4 albertel 601: }
602:
1.24 ng 603: sub var_in_tex {
1.73 albertel 604: if ( $external::target eq "tex" ) {
605: return $_[0];
606: } else {
607: return "";
608: }
1.24 ng 609: }
610:
1.4 albertel 611: sub web {
1.73 albertel 612: if ( $external::target eq "tex" ) {
613: return $_[1];
1.26 ng 614: } else {
1.73 albertel 615: if ( $external::target eq "web" || $external::target eq "answer") {
616: return $_[2];
617: } else {
618: return $_[0];
619: }
1.4 albertel 620: }
621: }
622:
1.24 ng 623: sub html {
1.73 albertel 624: if ( $external::target eq "web" ) {
625: return shift;
626: }
1.24 ng 627: }
628:
1.1 harris41 629: sub hinton {
1.73 albertel 630: return 0;
1.1 harris41 631: }
632:
633: sub random {
1.61 albertel 634: my ($start,$end,$step)=@_;
635: if ( ! $hidden::RANDOMINIT ) {
636: if ($external::randomseed == 0) { $external::randomseed=1; }
637: if ($external::randomseed =~/,/) {
1.84 albertel 638: my ($num1,$num2)=split(/,/,$external::randomseed);
639: &random_set_seed(1,abs($num1));
640: } elsif ($external::randomseed =~/:/) {
641: my ($num1,$num2)=split(/:/,$external::randomseed);
1.61 albertel 642: &random_set_seed(abs($num1),abs($num2));
643: } else {
644: &random_set_seed(1,int(abs($external::randomseed)));
645: }
646: &math_random_uniform();
647: $hidden::RANDOMINIT=1;
648: }
649: if (!defined($step)) { $step=1; }
650: my $num=1+int(($end-$start)/$step);
651: my $result=$start + int(&math_random_uniform() * $num)*$step;
652: return $result;
1.1 harris41 653: }
654:
1.26 ng 655: sub random_normal {
1.73 albertel 656: my ($item_cnt,$seed,$av,$std_dev) = @_;
657: my @oldseed=&random_get_seed();
658: my @retArray;
659: &random_set_seed_from_phrase($seed);
660: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
661: &random_set_seed(@oldseed);
662: return @retArray;
1.26 ng 663: }
664:
665: sub random_beta {
1.73 albertel 666: my ($item_cnt,$seed,$aa,$bb) = @_;
667: my @oldseed=&random_get_seed();
668: my @retArray;
669: &random_set_seed_from_phrase($seed);
670: @retArray=&math_random_beta($item_cnt,$aa,$bb);
671: &random_set_seed(@oldseed);
672: return @retArray;
1.26 ng 673: }
674:
675: sub random_gamma {
1.73 albertel 676: my ($item_cnt,$seed,$a,$r) = @_;
677: my @oldseed=&random_get_seed();
678: my @retArray;
679: &random_set_seed_from_phrase($seed);
680: @retArray=&math_random_gamma($item_cnt,$a,$r);
681: &random_set_seed(@oldseed);
682: return @retArray;
1.26 ng 683: }
684:
685: sub random_exponential {
1.73 albertel 686: my ($item_cnt,$seed,$av) = @_;
687: my @oldseed=&random_get_seed();
688: my @retArray;
689: &random_set_seed_from_phrase($seed);
690: @retArray=&math_random_exponential($item_cnt,$av);
691: &random_set_seed(@oldseed);
692: return @retArray;
1.26 ng 693: }
694:
695: sub random_poisson {
1.73 albertel 696: my ($item_cnt,$seed,$mu) = @_;
697: my @oldseed=&random_get_seed();
698: my @retArray;
699: &random_set_seed_from_phrase($seed);
700: @retArray=&math_random_poisson($item_cnt,$mu);
701: &random_set_seed(@oldseed);
702: return @retArray;
1.26 ng 703: }
704:
705: sub random_chi {
1.73 albertel 706: my ($item_cnt,$seed,$df) = @_;
707: my @oldseed=&random_get_seed();
708: my @retArray;
709: &random_set_seed_from_phrase($seed);
710: @retArray=&math_random_chi_square($item_cnt,$df);
711: &random_set_seed(@oldseed);
712: return @retArray;
1.26 ng 713: }
714:
715: sub random_noncentral_chi {
1.73 albertel 716: my ($item_cnt,$seed,$df,$nonc) = @_;
717: my @oldseed=&random_get_seed();
718: my @retArray;
719: &random_set_seed_from_phrase($seed);
720: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
721: &random_set_seed(@oldseed);
722: return @retArray;
1.26 ng 723: }
724:
725: sub random_f {
1.73 albertel 726: my ($item_cnt,$seed,$dfn,$dfd) = @_;
727: my @oldseed=&random_get_seed();
728: my @retArray;
729: &random_set_seed_from_phrase($seed);
730: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
731: &random_set_seed(@oldseed);
732: return @retArray;
1.26 ng 733: }
734:
735: sub random_noncentral_f {
1.73 albertel 736: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
737: my @oldseed=&random_get_seed();
738: my @retArray;
739: &random_set_seed_from_phrase($seed);
740: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
741: &random_set_seed(@oldseed);
742: return @retArray;
1.26 ng 743: }
744:
745: sub random_multivariate_normal {
1.73 albertel 746: my ($item_cnt,$seed,$mean,$covar) = @_;
747: my @oldseed=&random_get_seed();
748: &random_set_seed_from_phrase($seed);
1.87 albertel 749: my @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.73 albertel 750: &random_set_seed(@oldseed);
751: return @retArray;
1.26 ng 752: }
753:
754: sub random_multinomial {
1.73 albertel 755: my ($item_cnt,$seed,@p) = @_;
756: my @oldseed=&random_get_seed();
757: my @retArray;
758: &random_set_seed_from_phrase($seed);
1.87 albertel 759: my @retArray=&math_random_multinomial($item_cnt,@p);
1.73 albertel 760: &random_set_seed(@oldseed);
761: return @retArray;
1.26 ng 762: }
763:
764: sub random_permutation {
1.73 albertel 765: my ($seed,@inArray) = @_;
766: my @oldseed=&random_get_seed();
767: my @retArray;
768: &random_set_seed_from_phrase($seed);
769: @retArray=&math_random_permutation(@inArray);
770: &random_set_seed(@oldseed);
771: return @retArray;
1.26 ng 772: }
773:
774: sub random_uniform {
1.73 albertel 775: my ($item_cnt,$seed,$low,$high) = @_;
776: my @oldseed=&random_get_seed();
777: my @retArray;
778: &random_set_seed_from_phrase($seed);
779: @retArray=&math_random_uniform($item_cnt,$low,$high);
780: &random_set_seed(@oldseed);
781: return @retArray;
1.26 ng 782: }
783:
784: sub random_uniform_integer {
1.73 albertel 785: my ($item_cnt,$seed,$low,$high) = @_;
786: my @oldseed=&random_get_seed();
787: my @retArray;
788: &random_set_seed_from_phrase($seed);
789: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
790: &random_set_seed(@oldseed);
791: return @retArray;
1.26 ng 792: }
793:
794: sub random_binomial {
1.73 albertel 795: my ($item_cnt,$seed,$nt,$p) = @_;
796: my @oldseed=&random_get_seed();
797: my @retArray;
798: &random_set_seed_from_phrase($seed);
799: @retArray=&math_random_binomial($item_cnt,$nt,$p);
800: &random_set_seed(@oldseed);
801: return @retArray;
1.26 ng 802: }
803:
804: sub random_negative_binomial {
1.73 albertel 805: my ($item_cnt,$seed,$ne,$p) = @_;
806: my @oldseed=&random_get_seed();
807: my @retArray;
808: &random_set_seed_from_phrase($seed);
809: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
810: &random_set_seed(@oldseed);
811: return @retArray;
1.26 ng 812: }
813:
1.103 albertel 814: sub abs { CORE::abs(shift) }
815: sub sin { CORE::sin(shift) }
816: sub cos { CORE::cos(shift) }
817: sub exp { CORE::exp(shift) }
818: sub int { CORE::int(shift) }
819: sub log { CORE::log(shift) }
1.152.2.3! raeburn 820: sub ln { CORE::log(shift) }
1.103 albertel 821: sub atan2 { CORE::atan2($_[0],$_[1]) }
822: sub sqrt { CORE::sqrt(shift) }
1.23 ng 823:
1.59 albertel 824: sub tan { CORE::sin($_[0]) / CORE::cos($_[0]) }
1.21 harris41 825: #sub atan { atan2($_[0], 1); }
826: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
827: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.22 ng 828:
1.59 albertel 829: sub log10 { CORE::log($_[0])/CORE::log(10); }
1.22 ng 830:
1.20 harris41 831: sub factorial {
1.59 albertel 832: my $input = CORE::int(shift);
1.20 harris41 833: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
834: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
835: return 1 if $input == 0;
836: my $result = 1;
837: for (my $i=2; $i<=$input; $i++) { $result *= $i }
838: return $result;
839: }
840:
841: sub sgn {
842: return -1 if $_[0] < 0;
843: return 0 if $_[0] == 0;
844: return 1 if $_[0] > 0;
845: }
846:
847: sub min {
848: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
849: return shift @sorted;
850: }
851:
852: sub max {
853: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
854: return pop @sorted;
855: }
1.1 harris41 856:
1.20 harris41 857: sub roundto {
858: my ($input,$n) = @_;
859: return sprintf('%.'.$n.'f',$input);
860: }
861:
862: sub to_string {
863: my ($input,$n) = @_;
1.26 ng 864: return sprintf($input) if $n eq "";
865: $n = '.'.$n if $n !~ /^\./;
1.20 harris41 866: return sprintf('%'.$n,$input) if $n ne "";
867: }
868:
869: sub sub_string {
870: my ($str,$start,$len) = @_;
871: return substr($str,$start-1,$len);
872: }
1.1 harris41 873:
874: sub pow {return $_[0] ** $_[1]; }
1.59 albertel 875: sub ceil {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
876: sub floor {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
1.27 ng 877: #sub floor {return int($_[0]); }
1.1 harris41 878:
1.2 albertel 879: sub format {
1.73 albertel 880: my ($value,$fmt)=@_;
1.81 albertel 881: my ($dollarmode,$commamode,$alwaysperiod,$options);
882: if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; }
883: #if ($options =~ /\$/) { $dollamode=1; }
884: #if ($options =~ /,/) { $commamode=1; }
1.82 albertel 885: if ($options =~ /\./) { $alwaysperiod=1; }
1.99 ng 886: my $result;
1.97 albertel 887: if ($fmt=~/s$/i) {
888: $result=&format_significant_figures($value,$fmt);
889: } else {
890: $fmt=~s/e/E/g;
1.99 ng 891: $result=sprintf('%.'.$fmt,$value);
1.97 albertel 892: if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
893: $result=~s/(E[+-]*)0/$1/;
894: }
1.81 albertel 895: #if ($dollarmode) {$result=&dollarformat($result);}
896: #if ($commamode) {$result=&commaformat($result);}
1.73 albertel 897: return $result;
1.46 albertel 898: }
899:
1.75 albertel 900: sub chemparse {
901: my ($reaction) = @_;
1.96 albertel 902: my @tokens = split(/(\s\+|\->|<=>|<\-|\.)/,$reaction);
1.75 albertel 903: my $formula = '';
904: foreach my $token (@tokens) {
905: if ($token eq '->' ) {
906: $formula .= '<m>\ensuremath{\rightarrow}</m> ';
907: next;
908: }
1.96 albertel 909: if ($token eq '<-' ) {
910: $formula .= '<m>\ensuremath{\leftarrow}</m> ';
911: next;
912: }
1.75 albertel 913: if ($token eq '<=>') {
914: if ($external::target eq 'web' &&
915: &EXT('request.browser.unicode')) {
1.76 albertel 916: $formula .= '⇌ ';
1.75 albertel 917: } else {
918: $formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
1.95 albertel 919: '<=> ');
1.75 albertel 920: }
921: next;
922: }
1.96 albertel 923: if ($token eq '.') {
924: $formula =~ s/(\ \;| )$//;
925: $formula .= '·';
926: next;
927: }
928: $token =~ /^\s*([\d|\/]*(?:&frac\d\d)?)(.*)/;
1.90 albertel 929: $formula .= $1 if ($1 ne '1'); # stoichiometric coefficient
1.75 albertel 930:
931: my $molecule = $2;
932: # subscripts
1.78 albertel 933: $molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
1.75 albertel 934: # superscripts
935: $molecule =~ s|\^(\d*[+\-]*)|<sup>$1</sup>|g;
936: # strip whitespace
937: $molecule =~ s/\s*//g;
938: # forced space
939: $molecule =~ s/_/ /g;
1.96 albertel 940: $molecule =~ s/-/−/g;
1.75 albertel 941: $formula .= $molecule.' ';
942: }
943: # get rid of trailing space
1.87 albertel 944: $formula =~ s/(\ \;| )$//;
1.75 albertel 945: return &xmlparse($formula);
946: }
947:
1.46 albertel 948: sub prettyprint {
1.73 albertel 949: my ($value,$fmt,$target)=@_;
950: my $result;
951: if (!$target) { $target = $external::target; }
1.75 albertel 952: if ($fmt =~ /chem/i) { return(&chemparse($value)); }
1.81 albertel 953: my ($dollarmode,$commamode,$alwaysperiod,$options);
954: if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; }
1.86 albertel 955: if ($options =~ /\$/) { $dollarmode=1; }
1.81 albertel 956: if ($options =~ /,/) { $commamode=1; }
957: if ($options =~ /\./) { $alwaysperiod=1; }
1.97 albertel 958: if ($fmt=~/s$/i) {
959: $value=&format_significant_figures($value,$fmt);
960: } elsif ($fmt) {
961: $value=sprintf('%.'.$fmt,$value);
962: }
1.81 albertel 963: if ($alwaysperiod && $fmt eq '0f') {
964: if ($target eq 'tex') {
965: $value .='\\ensuremath{.}';
966: } else {
967: $value .='.';
968: }
969: }
1.73 albertel 970: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
971: my $frac=$1;
972: if ($dollarmode) { $frac=&dollarformat($frac); }
1.80 albertel 973: if ($commamode) { $frac=&commaformat($frac); }
1.73 albertel 974: my $exponent=$2;
975: $exponent=~s/^\+0*//;
976: $exponent=~s/^-0*/-/;
977: $exponent=~s/^-0*/-/;
978: if ($exponent eq '-') { undef($exponent); }
979: if ($exponent) {
980: if ($target eq 'web') {
981: $result=$frac.'×10<sup>'.$exponent.'</sup>';
982: } elsif ($target eq 'tex') {
983: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
984: } else {
985: $result=$value;
986: }
987: } else {
988: $result=$frac;
989: }
990: } else {
1.48 albertel 991: $result=$value;
1.86 albertel 992: if ($dollarmode) { $result=&dollarformat($result,$target); }
993: elsif ($commamode) { $result=&commaformat($result,$target); }
1.46 albertel 994: }
1.73 albertel 995: return $result;
1.48 albertel 996: }
997:
1.80 albertel 998: sub commaformat {
1.73 albertel 999: my ($number,$target) = @_;
1000: if ($number =~ /\./) {
1.102 albertel 1001: while ($number =~ /([^0-9]*)([0-9]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
1002: $number = $1.$2.','.$3.$4;
1.73 albertel 1003: }
1004: } else {
1.102 albertel 1005: while ($number =~ /^([^0-9]*)([0-9]+)([^,][^,][^,])([,0-9]*)$/) {
1006: $number = $1.$2.','.$3.$4;
1.73 albertel 1007: }
1008: }
1.80 albertel 1009: return $number;
1010: }
1011:
1012: sub dollarformat {
1013: my ($number,$target) = @_;
1014: if (!$target) { $target = $external::target; }
1015: $number=&commaformat($number,$target);
1.73 albertel 1016: if ($target eq 'tex') {
1017: $number='\$'.$number; #' stupid emacs
1018: } else {
1019: $number='$'.$number; #' stupid emacs
1020: }
1021: return $number;
1.2 albertel 1022: }
1.5 albertel 1023:
1.97 albertel 1024: # format of form ns or nS where n is an integer
1025: sub format_significant_figures {
1026: my ($number,$format) = @_;
1027: return '0' if ($number == 0);
1028: # extract number of significant figures needed
1029: my ($sig) = ($format =~ /(\d+)s/i);
1030: # arbitrary choice - suggestions ?? or throw error message?
1031: $sig = 3 if ($sig eq '');
1032: # save the minus sign
1033: my $sign = ($number < 0) ? '-' : '';
1034: $number = abs($number);
1035: # needed to correct for a number greater than 1 (or
1036: my $power = ($number < 1) ? 0 : 1;
1037: # could round up. Take the integer part of log10.
1038: my $x10 = int(log($number)/log(10));
1039: # find number with values left of decimal pt = # of sign figs.
1040: my $xsig = $number*10**($sig-$x10-$power);
1041: # get just digits left of decimal pt - also rounds off correctly
1042: my $xint = sprintf('%.0f',$xsig);
1043: # save any trailing zero's
1044: my ($zeros) = ($xint =~ /(0+)$/);
1045: # return number to original magnitude
1046: my $numSig = $xint*10**($x10-$sig+$power);
1047: # insert trailing zero's if have decimal point
1048: $numSig =~ s/^(\d+)\.(\d+)(\e?(.*)?)$/$1\.$2$zeros$3/;
1.98 albertel 1049: # put a decimal pt for number ending with 0 and length = # of sig fig
1050: $numSig.='.' if (length($numSig) == $sig && $numSig =~ /0$/);
1051: if (length($numSig) < $sig) {
1052: $numSig.='.'.substr($zeros,0,($sig-length($numSig)));
1053: }
1.97 albertel 1054: # return number with sign
1055: return $sign.$numSig;
1056:
1057: }
1058:
1.5 albertel 1059: sub map {
1.27 ng 1060: my ($phrase,$dest,$source)=@_;
1.51 albertel 1061: my @oldseed=&random_get_seed();
1.27 ng 1062: my @seed = &random_seed_from_phrase($phrase);
1063: &random_set_seed(@seed);
1064: my $destct = scalar(@$dest);
1.28 ng 1065: if (!$source) {
1066: my @output;
1067: my @idx = &math_random_permuted_index($destct);
1068: my $ctr = 0;
1069: while ($ctr < $destct) {
1070: $output[$ctr] = $$dest[$idx[$ctr]];
1.27 ng 1071: $ctr++;
1.28 ng 1072: }
1.51 albertel 1073: &random_set_seed(@oldseed);
1.28 ng 1074: return @output;
1.27 ng 1075: } else {
1.28 ng 1076: my $num = scalar(@$source);
1077: my @idx = &math_random_permuted_index($num);
1078: my $ctr = 0;
1079: my $tot = $num;
1080: $tot = $destct if $destct < $num;
1081: if (ref($$dest[0])) {
1082: while ($ctr < $tot) {
1083: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
1084: $ctr++;
1085: }
1086: } else {
1087: while ($ctr < $tot) {
1088: $$dest[$ctr] = $$source[$idx[$ctr]];
1089: $ctr++;
1090: }
1091: }
1.27 ng 1092: }
1.56 albertel 1093: &random_set_seed(@oldseed);
1.51 albertel 1094: return '';
1.27 ng 1095: }
1096:
1097: sub rmap {
1098: my ($phrase,$dest,$source)=@_;
1.51 albertel 1099: my @oldseed=&random_get_seed();
1.27 ng 1100: my @seed = &random_seed_from_phrase($phrase);
1101: &random_set_seed(@seed);
1102: my $destct = scalar(@$dest);
1.28 ng 1103: if (!$source) {
1104: my @idx = &math_random_permuted_index($destct);
1105: my $ctr = 0;
1106: my @r_idx;
1107: while ($ctr < $destct) {
1108: $r_idx[$idx[$ctr]] = $ctr;
1109: $ctr++;
1110: }
1111: my @output;
1112: $ctr = 0;
1113: while ($ctr < $destct) {
1114: $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27 ng 1115: $ctr++;
1.28 ng 1116: }
1.51 albertel 1117: &random_set_seed(@oldseed);
1.28 ng 1118: return @output;
1.27 ng 1119: } else {
1.28 ng 1120: my $num = scalar(@$source);
1121: my @idx = &math_random_permuted_index($num);
1122: my $ctr = 0;
1123: my $tot = $num;
1124: $tot = $destct if $destct < $num;
1125: my @r_idx;
1.27 ng 1126: while ($ctr < $tot) {
1.28 ng 1127: $r_idx[$idx[$ctr]] = $ctr;
1.27 ng 1128: $ctr++;
1.28 ng 1129: }
1130: $ctr = 0;
1131: if (ref($$dest[0])) {
1132: while ($ctr < $tot) {
1133: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
1134: $ctr++;
1135: }
1136: } else {
1137: while ($ctr < $tot) {
1138: $$dest[$ctr] = $$source[$r_idx[$ctr]];
1139: $ctr++;
1140: }
1141: }
1.6 albertel 1142: }
1.51 albertel 1143: &random_set_seed(@oldseed);
1144: return '';
1.5 albertel 1145: }
1.22 ng 1146:
1.23 ng 1147: sub capa_id { return }
1148:
1149: sub problem { return }
1150:
1.22 ng 1151: sub name{
1.73 albertel 1152: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
1153: $fullname = "" if $fullname eq ", ";
1154: $fullname =~ s/\%2d/-/g;
1155: return $fullname;
1.22 ng 1156: }
1157:
1158: sub student_number {
1.73 albertel 1159: my $id = &EXT('environment.id');
1160: $id = '' if $id eq "";
1161: return $id;
1.22 ng 1162: }
1163:
1164: sub class {
1.73 albertel 1165: my $course = &EXT('course.description');
1166: $course = '' if $course eq "";
1167: return $course;
1.22 ng 1168: }
1169:
1.152.2.2 raeburn 1170: sub classid {
1171: my $courseid = &EXT('request.course.id');
1172: $courseid = '' if $courseid eq "";
1173: return $courseid;
1174: }
1175:
1.112 www 1176: sub firstname {
1177: my $firstname = &EXT('environment.firstname');
1178: $firstname = '' if $firstname eq "";
1179: return $firstname;
1180: }
1.152.2.2 raeburn 1181:
1182: sub middlename {
1183: my $middlename = &EXT('environment.middlename');
1184: $middlename = '' if $middlename eq "";
1185: return $middlename;
1186: }
1.112 www 1187:
1188: sub lastname {
1189: my $lastname = &EXT('environment.lastname');
1190: $lastname = '' if $lastname eq "";
1191: return $lastname;
1192: }
1193:
1.22 ng 1194: sub sec {
1.73 albertel 1195: my $sec = &EXT('request.course.sec');
1196: $sec = '' if $sec eq "";
1197: return $sec;
1.22 ng 1198: }
1199:
1.136 www 1200: sub submission {
1201: my ($partid,$responseid,$subnumber)=@_;
1202: my $sub='';
1203: if ($subnumber) { $sub=$subnumber.':'; }
1204: return &EXT('user.resource.'.$sub.'resource.'.$partid.'.'.$responseid.'.submission');
1205: }
1206:
1207: sub currentpart {
1208: return $external::part;
1209: }
1210:
1.135 www 1211: sub eval_time {
1212: my ($timestamp)=@_;
1213: unless ($timestamp) { return ''; }
1214: return &locallocaltime($timestamp);
1215: }
1216:
1.23 ng 1217: sub open_date {
1.134 www 1218: my ($partid)=@_;
1219: unless ($partid) { $partid=0; }
1.135 www 1220: return &eval_time(&EXT('resource.'.$partid.'.opendate'));
1.23 ng 1221: }
1222:
1.134 www 1223: sub due_date {
1224: my ($partid)=@_;
1225: unless ($partid) { $partid=0; }
1.135 www 1226: return &eval_time(&EXT('resource.'.$partid.'.duedate'));
1.23 ng 1227: }
1228:
1229: sub answer_date {
1.134 www 1230: my ($partid)=@_;
1231: unless ($partid) { $partid=0; }
1.135 www 1232: return &eval_time(&EXT('resource.'.$partid.'.answerdate'));
1.24 ng 1233: }
1234:
1.136 www 1235: sub open_date_epoch {
1236: my ($partid)=@_;
1237: unless ($partid) { $partid=0; }
1238: return &EXT('resource.'.$partid.'.opendate');
1239: }
1240:
1241: sub due_date_epoch {
1242: my ($partid)=@_;
1243: unless ($partid) { $partid=0; }
1244: return &EXT('resource.'.$partid.'.duedate');
1245: }
1246:
1247: sub answer_date_epoch {
1248: my ($partid)=@_;
1249: unless ($partid) { $partid=0; }
1250: return &EXT('resource.'.$partid.'.answerdate');
1251: }
1252:
1.152.2.2 raeburn 1253: sub parameter_setting {
1254: my ($which,$partid)=@_;
1255: unless ($partid) { $partid=0; }
1256: return &EXT('resource.'.$partid.'.'.$which);
1257: }
1258:
1259: sub stored_data {
1260: my ($which,$partid)=@_;
1261: unless ($partid) { $partid=0; }
1262: return &EXT('user.resource.resource.'.$partid.'.'.$which);
1263: }
1264:
1265: sub wrong_bubbles {
1266: my ($correct,$lower,$upper,$step,@given)=@_;
1267: my @array=();
1268: my %hash=();
1269: foreach my $new (@given) {
1270: $hash{$new}=1;
1271: }
1272: my $num=int(¶meter_setting('numbubbles',¤tpart()));
1273: unless ($num) { $num=8; }
1274: if ($num>1) {
1275: for (my $i=0;$i<=500;$i++) {
1276: my $new=&random($lower,$upper,$step);
1277: if ($hash{$new}) { next; }
1278: if (abs($new-$correct)<$step) { next; }
1279: $hash{$new}=1;
1280: @array=keys(%hash);
1281: if ($#array+2>=$num) { last; }
1282: }
1283: }
1284: return @array;
1285: }
1286:
1.24 ng 1287: sub array_moments {
1.73 albertel 1288: my @input=@_;
1289: my (@output,$N);
1290: $N=scalar (@input);
1291: $output[0]=$N;
1292: if ($N <= 1) {
1293: $output[1]=$input[0];
1294: $output[1]="Input array not defined" if ($N == 0);
1295: $output[2]="variance undefined for N<=1";
1296: $output[3]="skewness undefined for N<=1";
1297: $output[4]="kurtosis undefined for N<=1";
1298: return @output;
1299: }
1300: my $sum=0;
1301: foreach my $line (@input) {
1302: $sum+=$line;
1303: }
1304: $output[1] = $sum/$N;
1305: my ($x,$sdev,$var,$skew,$kurt) = 0;
1306: foreach my $line (@input) {
1307: $x=$line-$output[1];
1308: $var+=$x**2;
1309: $skew+=$x**3;
1310: $kurt+=$x**4;
1311: }
1312: $output[2]=$var/($N-1);
1313: $sdev=CORE::sqrt($output[2]);
1314: if ($sdev == 0) {
1315: $output[3]="inf-variance=0";
1316: $output[4]="inf-variance=0";
1317: return @output;
1318: }
1319: $output[3]=$skew/($sdev**3*$N);
1320: $output[4]=$kurt/($sdev**4*$N)-3;
1.24 ng 1321: return @output;
1322: }
1.5 albertel 1323:
1324: sub choose {
1.73 albertel 1325: my $num = $_[0];
1326: return $_[$num];
1.5 albertel 1327: }
1.23 ng 1328:
1.101 albertel 1329: #&sum1(1,$x,sub { &sum1($_[0],2*$_[0], sub { fact($_[0])**2 })});
1330: #sub sum1 {
1331: # my ($start,$end,$sub)=@_;
1332: # my $sum=0;
1333: # for (my $i=$start;$i<=$end;$i++) {
1334: # $sum+=&$sub($i);
1335: # }
1336: # return $sum
1337: #}
1338:
1339: #&sum2('a',1,$x,'&sum2(\'b\',$a,2*$a, \'&factorial($b)**2\')');
1340: #sub sum2 {
1341: # my ($varname,$start,$end,$line)=@_;
1342: # my $sum=0;
1343: # for (my $i=$start;$i<=$end;$i++) {
1344: # my $func=sub {
1345: # eval("\$".$varname."=$i");
1346: # eval($line);
1347: # };
1348: # $sum+=&$func($i);
1349: # }
1350: # return $sum
1351: #}
1352:
1.49 albertel 1353: # expiremental idea
1354: sub proper_path {
1.73 albertel 1355: my ($path)=@_;
1356: if ( $external::target eq "tex" ) {
1357: return '/home/httpd/html'.$path;
1358: } else {
1359: return $path;
1360: }
1.49 albertel 1361: }
1.23 ng 1362:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>