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