Annotation of loncom/homework/default_homework.lcpm, revision 1.122
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.122 ! www 4: # $Id: default_homework.lcpm,v 1.121 2006/12/11 00:42:48 albertel 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.76 albertel 160: #for string answers make surec all places spaces occur, there is
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.117 albertel 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;
220: if ($type eq '9') {
1.108 www 221: $result = &maxima_check(&maxima_cas_formula_fix($response),&maxima_cas_formula_fix($answer),\$reterror);
1.107 albertel 222: } else {
1.109 albertel 223: if ($type eq '8') { # fml type
224: $response = &capa_formula_fix($response);
225: $answer = &capa_formula_fix($answer);
226: }
227: $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.73 albertel 228: $tol_type,$tol,
229: $sig_lbound,$sig_ubound,
230: $ans_fmt,$unit,$calc,$id_list,
231: $points,$external::randomseed,
232: \$reterror);
1.107 albertel 233: }
1.73 albertel 234: if ($result == '1') { $result='EXACT_ANS'; }
235: elsif ($result == '2') { $result='APPROX_ANS'; }
236: elsif ($result == '3') { $result='SIG_FAIL'; }
237: elsif ($result == '4') { $result='UNIT_FAIL'; }
238: elsif ($result == '5') { $result='NO_UNIT'; }
239: elsif ($result == '6') { $result='UNIT_OK'; }
240: elsif ($result == '7') { $result='INCORRECT'; }
241: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
242: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
243: elsif ($result =='10') { $result='SUB_RECORDED'; }
244: elsif ($result =='11') { $result='BAD_FORMULA'; }
1.94 albertel 245: elsif ($result =='12' && !$response) { $result='MISSING_ANSWER'; }
246: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.77 albertel 247: elsif ($result =='13') { $result='UNIT_INVALID_INSTRUCTOR'; }
248: elsif ($result =='141') { $result='UNIT_INVALID_STUDENT'; }
249: elsif ($result =='142') { $result='UNIT_INVALID_STUDENT'; }
250: elsif ($result =='143') { $result='UNIT_INVALID_STUDENT'; }
251: elsif ($result =='15') { $result='UNIT_IRRECONCIBLE'; }
1.73 albertel 252: else {$result = "ERROR: Unknown Result:$result:$@:";}
253:
1.119 albertel 254: &LONCAPA_INTERNAL_DEBUG("RetError $reterror: Answer $answer: Response $response: type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|");
255: &LONCAPA_INTERNAL_DEBUG(" $answer $response $result ");
1.116 albertel 256: return ($result,$reterror)
1.37 albertel 257: }
258:
1.108 www 259: sub maxima_cas_formula_fix {
260: my ($expression)=@_;
261: return &implicit_multiplication($expression);
262: }
263:
264: sub capa_formula_fix {
265: my ($expression)=@_;
266: return &implicit_multiplication($expression);
267: }
268:
269: sub implicit_multiplication {
270: my ($expression)=@_;
1.111 www 271: # Escape scientific notation, so 3e8 does not become 3*e*8
272: # 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8
1.110 www 273: $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi;
1.111 www 274: # 3x10^8 -> 3&8; 3*10^-8 -> 3&-8
1.110 www 275: $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi;
1.111 www 276: # Fill in multiplication signs
1.114 albertel 277: # a b -> a*b;3 b -> 3*b;3 4 -> 3*4
278: $expression=~s/(\w)\s+(\w)/$1\*$2/gs;
1.115 www 279: # )( -> )*(; ) ( -> )*(
280: $expression=~s/\)\s*\(/\)\*\(/gs;
281: # 3a -> 3*a; 3( -> 3*(; 3 ( -> 3*(; 3A -> 3*A
282: $expression=~s/(\d)\s*([a-zA-Z\(])/$1\*$2/gs;
283: # a ( -> a*(
284: $expression=~s/(\w)\s+\(/$1\*\(/gs;
285: # )a -> )*a; )3 -> )*3; ) 3 -> )*3
286: $expression=~s/\)\s*(\w)/\)\*$1/gs;
1.111 www 287: # 3&8 -> 3e8; 3&-4 -> 3e-4
1.110 www 288: $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs;
1.108 www 289: return $expression;
290: }
1.73 albertel 291:
1.37 albertel 292: sub caparesponse_check_list {
1.119 albertel 293: my $responses=$LONCAPA::CAPAresponse_args{'response'};
1.121 albertel 294: # &LONCAPA_INTERNAL_DEBUG(" answer is ".
295: # &LONCAPA_INTERNAL_Dumper($LONCAPA::CAPAresponse_answer).":\n");
296: # &LONCAPA_INTERNAL_DEBUG(" respons is ".
297: # &LONCAPA_INTERNAL_Dumper($responses).":\n");
1.105 albertel 298: &LONCAPA_INTERNAL_DEBUG("args ".join(':',%LONCAPA::CAPAresponse_args));
1.74 albertel 299: my $type = $LONCAPA::CAPAresponse_args{'type'};
1.116 albertel 300: &LONCAPA_INTERNAL_DEBUG("Got type :$type:\n");
1.119 albertel 301:
302: my $num_input_lines =
303: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}});
1.120 albertel 304:
305: if ($type ne '' ) {
1.119 albertel 306: if (scalar(@$responses) < $num_input_lines) {
1.105 albertel 307: return 'MISSING_ANSWER';
308: }
1.119 albertel 309: if (scalar(@$responses) > $num_input_lines) {
310: return 'EXTRA_ANSWER';
311: }
312:
313: }
314:
315: foreach my $which (0..($num_input_lines-1)) {
316: my $answer_size =
317: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}[$which]});
318: if ($type ne ''
319: && $answer_size > 1) {
320: $responses->[$which]=[split(/,/,$responses->[$which])];
321: } else {
322: $responses->[$which]=[$responses->[$which]];
323: }
324: }
1.121 albertel 325: # &LONCAPA_INTERNAL_DEBUG(" parsed response is ".
326: # &LONCAPA_INTERNAL_Dumper($responses).":\n");
1.119 albertel 327: foreach my $which (0..($num_input_lines-1)) {
328: my $answer_size =
329: scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}[$which]});
330: my $response_size =
331: scalar(@{$responses->[$which]});
332: if ($answer_size > $response_size) {
333: return 'MISSING_ANSWER';
334: }
335: if ($answer_size < $response_size) {
1.105 albertel 336: return 'EXTRA_ANSWER';
337: }
1.73 albertel 338: }
1.119 albertel 339:
340: &LONCAPA_INTERNAL_DEBUG("Initial final response :$responses->[0][-1]:");
1.105 albertel 341: my $unit;
1.73 albertel 342: if ($type eq '' || $type eq 'float') {
343: #for numerical problems split off the unit
1.119 albertel 344: if ( $responses->[0][-1]=~ /(.*[^\s])\s+([^\s]+)/ ) {
345: $responses->[0][-1]=$1;
1.73 albertel 346: $unit=$2;
347: }
348: }
1.119 albertel 349: &LONCAPA_INTERNAL_DEBUG("Final final response :$responses->[0][-1]:$unit:");
1.73 albertel 350: $unit=~s/\s//;
1.119 albertel 351: if ($unit ne '') {
352: foreach my $response (@$responses) {
353: foreach my $element (@$response) {
354: $element .= " $unit";
355: }
356: }
357: }
358:
1.117 albertel 359: foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
360: if (!defined($thisanswer)) {
361: return ('ERROR','answer was undefined');
362: }
363: }
364:
365:
1.121 albertel 366: # &LONCAPA_INTERNAL_DEBUG(&LONCAPA_INTERNAL_Dumper($responses));
1.117 albertel 367: my %memoized;
368: if ($LONCAPA::CAPAresponse_answer->{'type'} eq 'ordered') {
1.119 albertel 369: for (my $i=0; $i<scalar(@$responses);$i++) {
1.117 albertel 370: my $answer = $LONCAPA::CAPAresponse_answer->{'answers'}[$i];
1.119 albertel 371: my $response = $responses->[$i];
1.117 albertel 372: my $key = "$answer\0$response";
1.119 albertel 373: my (@awards,@msgs);
374: for (my $j=0; $j<scalar(@$response); $j++) {
375: my ($award,$msg) = &caparesponse_check($answer->[$j],
376: $response->[$j]);
377: push(@awards,$award);
378: push(@msgs, $msg);
379: }
380: my ($award,$msg) =
381: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
382: $memoized{$key} = [$award,$msg];
1.117 albertel 383: }
384: } else {
1.119 albertel 385: #FIXME broken with unorder responses where one is a <value>
386: # and the other is a <vector> (need to delay parse til
387: # inside the loop?)
388: foreach my $response (@$responses) {
389: my $response_size = scalar(@{$response});
1.117 albertel 390: foreach my $answer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
391: my $key = "$answer\0$response";
1.119 albertel 392: my $answer_size = scalar(@{$answer});
393: my ($award,$msg);
394: if ($answer_size > $response_size) {
395: $award = 'MISSING_ANSWER';
396: } elsif ($answer_size < $response_size) {
397: $award = 'EXTRA_ANSWER';
398: } else {
399: my (@awards,@msgs);
400: for (my $j=0; $j<scalar(@$response); $j++) {
401: my ($award,$msg) = &caparesponse_check($answer->[$j],
402: $response->[$j]);
403: push(@awards,$award);
404: push(@msgs, $msg);
405: }
406: ($award,$msg) =
407: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
408: }
409: $memoized{$key} = [$award,$msg];
1.117 albertel 410: }
411: }
412: }
413:
1.116 albertel 414: my ($final_award,$final_msg);
1.119 albertel 415: &init_permutation(scalar(@$responses),
1.116 albertel 416: $LONCAPA::CAPAresponse_answer->{'type'});
1.117 albertel 417:
1.118 albertel 418: # possible FIXMEs
419: # - significant time is spent calling non-safe space routine
420: # from safe space
421: # - early outs could be possible with classifying awards is to stratas
422: # and stopping as so as hitting the top strata
423: # - some early outs also might be possible with check ing the
424: # memoized hash of results (is correct even possible? etc.)
425:
1.117 albertel 426: my (@final_awards,@final_msg);
1.116 albertel 427: while( &get_permutations_left() ) {
1.117 albertel 428: my $order = &get_next_permutation();
1.116 albertel 429: my (@awards, @msgs, $i);
430: foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
1.119 albertel 431: my $key = "$thisanswer\0".$responses->[$order->[$i]];
1.117 albertel 432: push(@awards,$memoized{$key}[0]);
433: push(@msgs,$memoized{$key}[1]);
1.116 albertel 434: $i++;
1.119 albertel 435:
1.116 albertel 436: }
1.119 albertel 437: &LONCAPA_INTERNAL_DEBUG(" all awards ".join(':',@awards));
438:
1.116 albertel 439: my ($possible_award,$possible_msg) =
440: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
1.119 albertel 441: &LONCAPA_INTERNAL_DEBUG(" pos awards ".$possible_award);
1.117 albertel 442: push(@final_awards,$possible_award);
443: push(@final_msg,$possible_msg);
1.73 albertel 444: }
1.117 albertel 445:
1.119 albertel 446: &LONCAPA_INTERNAL_DEBUG(" all final_awards ".join(':',@final_awards));
1.117 albertel 447: my ($final_award,$final_msg) =
448: &LONCAPA_INTERNAL_FINALIZEAWARDS(\@final_awards,\@final_msg,undef,1);
1.116 albertel 449: return ($final_award,$final_msg);
1.7 albertel 450: }
451:
1.4 albertel 452: sub tex {
1.73 albertel 453: if ( $external::target eq "tex" ) {
454: return $_[0];
455: } else {
456: return $_[1];
457: }
1.4 albertel 458: }
459:
1.24 ng 460: sub var_in_tex {
1.73 albertel 461: if ( $external::target eq "tex" ) {
462: return $_[0];
463: } else {
464: return "";
465: }
1.24 ng 466: }
467:
1.4 albertel 468: sub web {
1.73 albertel 469: if ( $external::target eq "tex" ) {
470: return $_[1];
1.26 ng 471: } else {
1.73 albertel 472: if ( $external::target eq "web" || $external::target eq "answer") {
473: return $_[2];
474: } else {
475: return $_[0];
476: }
1.4 albertel 477: }
478: }
479:
1.24 ng 480: sub html {
1.73 albertel 481: if ( $external::target eq "web" ) {
482: return shift;
483: }
1.24 ng 484: }
485:
1.1 harris41 486: sub hinton {
1.73 albertel 487: return 0;
1.1 harris41 488: }
489:
490: sub random {
1.61 albertel 491: my ($start,$end,$step)=@_;
492: if ( ! $hidden::RANDOMINIT ) {
493: if ($external::randomseed == 0) { $external::randomseed=1; }
494: if ($external::randomseed =~/,/) {
1.84 albertel 495: my ($num1,$num2)=split(/,/,$external::randomseed);
496: &random_set_seed(1,abs($num1));
497: } elsif ($external::randomseed =~/:/) {
498: my ($num1,$num2)=split(/:/,$external::randomseed);
1.61 albertel 499: &random_set_seed(abs($num1),abs($num2));
500: } else {
501: &random_set_seed(1,int(abs($external::randomseed)));
502: }
503: &math_random_uniform();
504: $hidden::RANDOMINIT=1;
505: }
506: if (!defined($step)) { $step=1; }
507: my $num=1+int(($end-$start)/$step);
508: my $result=$start + int(&math_random_uniform() * $num)*$step;
509: return $result;
1.1 harris41 510: }
511:
1.26 ng 512: sub random_normal {
1.73 albertel 513: my ($item_cnt,$seed,$av,$std_dev) = @_;
514: my @oldseed=&random_get_seed();
515: my @retArray;
516: &random_set_seed_from_phrase($seed);
517: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
518: &random_set_seed(@oldseed);
519: return @retArray;
1.26 ng 520: }
521:
522: sub random_beta {
1.73 albertel 523: my ($item_cnt,$seed,$aa,$bb) = @_;
524: my @oldseed=&random_get_seed();
525: my @retArray;
526: &random_set_seed_from_phrase($seed);
527: @retArray=&math_random_beta($item_cnt,$aa,$bb);
528: &random_set_seed(@oldseed);
529: return @retArray;
1.26 ng 530: }
531:
532: sub random_gamma {
1.73 albertel 533: my ($item_cnt,$seed,$a,$r) = @_;
534: my @oldseed=&random_get_seed();
535: my @retArray;
536: &random_set_seed_from_phrase($seed);
537: @retArray=&math_random_gamma($item_cnt,$a,$r);
538: &random_set_seed(@oldseed);
539: return @retArray;
1.26 ng 540: }
541:
542: sub random_exponential {
1.73 albertel 543: my ($item_cnt,$seed,$av) = @_;
544: my @oldseed=&random_get_seed();
545: my @retArray;
546: &random_set_seed_from_phrase($seed);
547: @retArray=&math_random_exponential($item_cnt,$av);
548: &random_set_seed(@oldseed);
549: return @retArray;
1.26 ng 550: }
551:
552: sub random_poisson {
1.73 albertel 553: my ($item_cnt,$seed,$mu) = @_;
554: my @oldseed=&random_get_seed();
555: my @retArray;
556: &random_set_seed_from_phrase($seed);
557: @retArray=&math_random_poisson($item_cnt,$mu);
558: &random_set_seed(@oldseed);
559: return @retArray;
1.26 ng 560: }
561:
562: sub random_chi {
1.73 albertel 563: my ($item_cnt,$seed,$df) = @_;
564: my @oldseed=&random_get_seed();
565: my @retArray;
566: &random_set_seed_from_phrase($seed);
567: @retArray=&math_random_chi_square($item_cnt,$df);
568: &random_set_seed(@oldseed);
569: return @retArray;
1.26 ng 570: }
571:
572: sub random_noncentral_chi {
1.73 albertel 573: my ($item_cnt,$seed,$df,$nonc) = @_;
574: my @oldseed=&random_get_seed();
575: my @retArray;
576: &random_set_seed_from_phrase($seed);
577: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
578: &random_set_seed(@oldseed);
579: return @retArray;
1.26 ng 580: }
581:
582: sub random_f {
1.73 albertel 583: my ($item_cnt,$seed,$dfn,$dfd) = @_;
584: my @oldseed=&random_get_seed();
585: my @retArray;
586: &random_set_seed_from_phrase($seed);
587: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
588: &random_set_seed(@oldseed);
589: return @retArray;
1.26 ng 590: }
591:
592: sub random_noncentral_f {
1.73 albertel 593: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
594: my @oldseed=&random_get_seed();
595: my @retArray;
596: &random_set_seed_from_phrase($seed);
597: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
598: &random_set_seed(@oldseed);
599: return @retArray;
1.26 ng 600: }
601:
602: sub random_multivariate_normal {
1.73 albertel 603: my ($item_cnt,$seed,$mean,$covar) = @_;
604: my @oldseed=&random_get_seed();
605: &random_set_seed_from_phrase($seed);
1.87 albertel 606: my @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.73 albertel 607: &random_set_seed(@oldseed);
608: return @retArray;
1.26 ng 609: }
610:
611: sub random_multinomial {
1.73 albertel 612: my ($item_cnt,$seed,@p) = @_;
613: my @oldseed=&random_get_seed();
614: my @retArray;
615: &random_set_seed_from_phrase($seed);
1.87 albertel 616: my @retArray=&math_random_multinomial($item_cnt,@p);
1.73 albertel 617: &random_set_seed(@oldseed);
618: return @retArray;
1.26 ng 619: }
620:
621: sub random_permutation {
1.73 albertel 622: my ($seed,@inArray) = @_;
623: my @oldseed=&random_get_seed();
624: my @retArray;
625: &random_set_seed_from_phrase($seed);
626: @retArray=&math_random_permutation(@inArray);
627: &random_set_seed(@oldseed);
628: return @retArray;
1.26 ng 629: }
630:
631: sub random_uniform {
1.73 albertel 632: my ($item_cnt,$seed,$low,$high) = @_;
633: my @oldseed=&random_get_seed();
634: my @retArray;
635: &random_set_seed_from_phrase($seed);
636: @retArray=&math_random_uniform($item_cnt,$low,$high);
637: &random_set_seed(@oldseed);
638: return @retArray;
1.26 ng 639: }
640:
641: sub random_uniform_integer {
1.73 albertel 642: my ($item_cnt,$seed,$low,$high) = @_;
643: my @oldseed=&random_get_seed();
644: my @retArray;
645: &random_set_seed_from_phrase($seed);
646: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
647: &random_set_seed(@oldseed);
648: return @retArray;
1.26 ng 649: }
650:
651: sub random_binomial {
1.73 albertel 652: my ($item_cnt,$seed,$nt,$p) = @_;
653: my @oldseed=&random_get_seed();
654: my @retArray;
655: &random_set_seed_from_phrase($seed);
656: @retArray=&math_random_binomial($item_cnt,$nt,$p);
657: &random_set_seed(@oldseed);
658: return @retArray;
1.26 ng 659: }
660:
661: sub random_negative_binomial {
1.73 albertel 662: my ($item_cnt,$seed,$ne,$p) = @_;
663: my @oldseed=&random_get_seed();
664: my @retArray;
665: &random_set_seed_from_phrase($seed);
666: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
667: &random_set_seed(@oldseed);
668: return @retArray;
1.26 ng 669: }
670:
1.103 albertel 671: sub abs { CORE::abs(shift) }
672: sub sin { CORE::sin(shift) }
673: sub cos { CORE::cos(shift) }
674: sub exp { CORE::exp(shift) }
675: sub int { CORE::int(shift) }
676: sub log { CORE::log(shift) }
677: sub atan2 { CORE::atan2($_[0],$_[1]) }
678: sub sqrt { CORE::sqrt(shift) }
1.23 ng 679:
1.59 albertel 680: sub tan { CORE::sin($_[0]) / CORE::cos($_[0]) }
1.21 harris41 681: #sub atan { atan2($_[0], 1); }
682: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
683: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
1.22 ng 684:
1.59 albertel 685: sub log10 { CORE::log($_[0])/CORE::log(10); }
1.22 ng 686:
1.20 harris41 687: sub factorial {
1.59 albertel 688: my $input = CORE::int(shift);
1.20 harris41 689: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
690: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
691: return 1 if $input == 0;
692: my $result = 1;
693: for (my $i=2; $i<=$input; $i++) { $result *= $i }
694: return $result;
695: }
696:
697: sub sgn {
698: return -1 if $_[0] < 0;
699: return 0 if $_[0] == 0;
700: return 1 if $_[0] > 0;
701: }
702:
703: sub min {
704: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
705: return shift @sorted;
706: }
707:
708: sub max {
709: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
710: return pop @sorted;
711: }
1.1 harris41 712:
1.20 harris41 713: sub roundto {
714: my ($input,$n) = @_;
715: return sprintf('%.'.$n.'f',$input);
716: }
717:
718: sub to_string {
719: my ($input,$n) = @_;
1.26 ng 720: return sprintf($input) if $n eq "";
721: $n = '.'.$n if $n !~ /^\./;
1.20 harris41 722: return sprintf('%'.$n,$input) if $n ne "";
723: }
724:
725: sub sub_string {
726: my ($str,$start,$len) = @_;
727: return substr($str,$start-1,$len);
728: }
1.1 harris41 729:
730: sub pow {return $_[0] ** $_[1]; }
1.59 albertel 731: sub ceil {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
732: sub floor {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
1.27 ng 733: #sub floor {return int($_[0]); }
1.1 harris41 734:
1.2 albertel 735: sub format {
1.73 albertel 736: my ($value,$fmt)=@_;
1.81 albertel 737: my ($dollarmode,$commamode,$alwaysperiod,$options);
738: if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; }
739: #if ($options =~ /\$/) { $dollamode=1; }
740: #if ($options =~ /,/) { $commamode=1; }
1.82 albertel 741: if ($options =~ /\./) { $alwaysperiod=1; }
1.99 ng 742: my $result;
1.97 albertel 743: if ($fmt=~/s$/i) {
744: $result=&format_significant_figures($value,$fmt);
745: } else {
746: $fmt=~s/e/E/g;
1.99 ng 747: $result=sprintf('%.'.$fmt,$value);
1.97 albertel 748: if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
749: $result=~s/(E[+-]*)0/$1/;
750: }
1.81 albertel 751: #if ($dollarmode) {$result=&dollarformat($result);}
752: #if ($commamode) {$result=&commaformat($result);}
1.73 albertel 753: return $result;
1.46 albertel 754: }
755:
1.75 albertel 756: sub chemparse {
757: my ($reaction) = @_;
1.96 albertel 758: my @tokens = split(/(\s\+|\->|<=>|<\-|\.)/,$reaction);
1.75 albertel 759: my $formula = '';
760: foreach my $token (@tokens) {
761: if ($token eq '->' ) {
762: $formula .= '<m>\ensuremath{\rightarrow}</m> ';
763: next;
764: }
1.96 albertel 765: if ($token eq '<-' ) {
766: $formula .= '<m>\ensuremath{\leftarrow}</m> ';
767: next;
768: }
1.75 albertel 769: if ($token eq '<=>') {
770: if ($external::target eq 'web' &&
771: &EXT('request.browser.unicode')) {
1.76 albertel 772: $formula .= '⇌ ';
1.75 albertel 773: } else {
774: $formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
1.95 albertel 775: '<=> ');
1.75 albertel 776: }
777: next;
778: }
1.96 albertel 779: if ($token eq '.') {
780: $formula =~ s/(\ \;| )$//;
781: $formula .= '·';
782: next;
783: }
784: $token =~ /^\s*([\d|\/]*(?:&frac\d\d)?)(.*)/;
1.90 albertel 785: $formula .= $1 if ($1 ne '1'); # stoichiometric coefficient
1.75 albertel 786:
787: my $molecule = $2;
788: # subscripts
1.78 albertel 789: $molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
1.75 albertel 790: # superscripts
791: $molecule =~ s|\^(\d*[+\-]*)|<sup>$1</sup>|g;
792: # strip whitespace
793: $molecule =~ s/\s*//g;
794: # forced space
795: $molecule =~ s/_/ /g;
1.96 albertel 796: $molecule =~ s/-/−/g;
1.75 albertel 797: $formula .= $molecule.' ';
798: }
799: # get rid of trailing space
1.87 albertel 800: $formula =~ s/(\ \;| )$//;
1.75 albertel 801: return &xmlparse($formula);
802: }
803:
1.46 albertel 804: sub prettyprint {
1.73 albertel 805: my ($value,$fmt,$target)=@_;
806: my $result;
807: if (!$target) { $target = $external::target; }
1.75 albertel 808: if ($fmt =~ /chem/i) { return(&chemparse($value)); }
1.81 albertel 809: my ($dollarmode,$commamode,$alwaysperiod,$options);
810: if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; }
1.86 albertel 811: if ($options =~ /\$/) { $dollarmode=1; }
1.81 albertel 812: if ($options =~ /,/) { $commamode=1; }
813: if ($options =~ /\./) { $alwaysperiod=1; }
1.97 albertel 814: if ($fmt=~/s$/i) {
815: $value=&format_significant_figures($value,$fmt);
816: } elsif ($fmt) {
817: $value=sprintf('%.'.$fmt,$value);
818: }
1.81 albertel 819: if ($alwaysperiod && $fmt eq '0f') {
820: if ($target eq 'tex') {
821: $value .='\\ensuremath{.}';
822: } else {
823: $value .='.';
824: }
825: }
1.73 albertel 826: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
827: my $frac=$1;
828: if ($dollarmode) { $frac=&dollarformat($frac); }
1.80 albertel 829: if ($commamode) { $frac=&commaformat($frac); }
1.73 albertel 830: my $exponent=$2;
831: $exponent=~s/^\+0*//;
832: $exponent=~s/^-0*/-/;
833: $exponent=~s/^-0*/-/;
834: if ($exponent eq '-') { undef($exponent); }
835: if ($exponent) {
836: if ($target eq 'web') {
837: $result=$frac.'×10<sup>'.$exponent.'</sup>';
838: } elsif ($target eq 'tex') {
839: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
840: } else {
841: $result=$value;
842: }
843: } else {
844: $result=$frac;
845: }
846: } else {
1.48 albertel 847: $result=$value;
1.86 albertel 848: if ($dollarmode) { $result=&dollarformat($result,$target); }
849: elsif ($commamode) { $result=&commaformat($result,$target); }
1.46 albertel 850: }
1.73 albertel 851: return $result;
1.48 albertel 852: }
853:
1.80 albertel 854: sub commaformat {
1.73 albertel 855: my ($number,$target) = @_;
856: if ($number =~ /\./) {
1.102 albertel 857: while ($number =~ /([^0-9]*)([0-9]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
858: $number = $1.$2.','.$3.$4;
1.73 albertel 859: }
860: } else {
1.102 albertel 861: while ($number =~ /^([^0-9]*)([0-9]+)([^,][^,][^,])([,0-9]*)$/) {
862: $number = $1.$2.','.$3.$4;
1.73 albertel 863: }
864: }
1.80 albertel 865: return $number;
866: }
867:
868: sub dollarformat {
869: my ($number,$target) = @_;
870: if (!$target) { $target = $external::target; }
871: $number=&commaformat($number,$target);
1.73 albertel 872: if ($target eq 'tex') {
873: $number='\$'.$number; #' stupid emacs
874: } else {
875: $number='$'.$number; #' stupid emacs
876: }
877: return $number;
1.2 albertel 878: }
1.5 albertel 879:
1.97 albertel 880: # format of form ns or nS where n is an integer
881: sub format_significant_figures {
882: my ($number,$format) = @_;
883: return '0' if ($number == 0);
884: # extract number of significant figures needed
885: my ($sig) = ($format =~ /(\d+)s/i);
886: # arbitrary choice - suggestions ?? or throw error message?
887: $sig = 3 if ($sig eq '');
888: # save the minus sign
889: my $sign = ($number < 0) ? '-' : '';
890: $number = abs($number);
891: # needed to correct for a number greater than 1 (or
892: my $power = ($number < 1) ? 0 : 1;
893: # could round up. Take the integer part of log10.
894: my $x10 = int(log($number)/log(10));
895: # find number with values left of decimal pt = # of sign figs.
896: my $xsig = $number*10**($sig-$x10-$power);
897: # get just digits left of decimal pt - also rounds off correctly
898: my $xint = sprintf('%.0f',$xsig);
899: # save any trailing zero's
900: my ($zeros) = ($xint =~ /(0+)$/);
901: # return number to original magnitude
902: my $numSig = $xint*10**($x10-$sig+$power);
903: # insert trailing zero's if have decimal point
904: $numSig =~ s/^(\d+)\.(\d+)(\e?(.*)?)$/$1\.$2$zeros$3/;
1.98 albertel 905: # put a decimal pt for number ending with 0 and length = # of sig fig
906: $numSig.='.' if (length($numSig) == $sig && $numSig =~ /0$/);
907: if (length($numSig) < $sig) {
908: $numSig.='.'.substr($zeros,0,($sig-length($numSig)));
909: }
1.97 albertel 910: # return number with sign
911: return $sign.$numSig;
912:
913: }
914:
1.5 albertel 915: sub map {
1.27 ng 916: my ($phrase,$dest,$source)=@_;
1.51 albertel 917: my @oldseed=&random_get_seed();
1.27 ng 918: my @seed = &random_seed_from_phrase($phrase);
919: &random_set_seed(@seed);
920: my $destct = scalar(@$dest);
1.28 ng 921: if (!$source) {
922: my @output;
923: my @idx = &math_random_permuted_index($destct);
924: my $ctr = 0;
925: while ($ctr < $destct) {
926: $output[$ctr] = $$dest[$idx[$ctr]];
1.27 ng 927: $ctr++;
1.28 ng 928: }
1.51 albertel 929: &random_set_seed(@oldseed);
1.28 ng 930: return @output;
1.27 ng 931: } else {
1.28 ng 932: my $num = scalar(@$source);
933: my @idx = &math_random_permuted_index($num);
934: my $ctr = 0;
935: my $tot = $num;
936: $tot = $destct if $destct < $num;
937: if (ref($$dest[0])) {
938: while ($ctr < $tot) {
939: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
940: $ctr++;
941: }
942: } else {
943: while ($ctr < $tot) {
944: $$dest[$ctr] = $$source[$idx[$ctr]];
945: $ctr++;
946: }
947: }
1.27 ng 948: }
1.56 albertel 949: &random_set_seed(@oldseed);
1.51 albertel 950: return '';
1.27 ng 951: }
952:
953: sub rmap {
954: my ($phrase,$dest,$source)=@_;
1.51 albertel 955: my @oldseed=&random_get_seed();
1.27 ng 956: my @seed = &random_seed_from_phrase($phrase);
957: &random_set_seed(@seed);
958: my $destct = scalar(@$dest);
1.28 ng 959: if (!$source) {
960: my @idx = &math_random_permuted_index($destct);
961: my $ctr = 0;
962: my @r_idx;
963: while ($ctr < $destct) {
964: $r_idx[$idx[$ctr]] = $ctr;
965: $ctr++;
966: }
967: my @output;
968: $ctr = 0;
969: while ($ctr < $destct) {
970: $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27 ng 971: $ctr++;
1.28 ng 972: }
1.51 albertel 973: &random_set_seed(@oldseed);
1.28 ng 974: return @output;
1.27 ng 975: } else {
1.28 ng 976: my $num = scalar(@$source);
977: my @idx = &math_random_permuted_index($num);
978: my $ctr = 0;
979: my $tot = $num;
980: $tot = $destct if $destct < $num;
981: my @r_idx;
1.27 ng 982: while ($ctr < $tot) {
1.28 ng 983: $r_idx[$idx[$ctr]] = $ctr;
1.27 ng 984: $ctr++;
1.28 ng 985: }
986: $ctr = 0;
987: if (ref($$dest[0])) {
988: while ($ctr < $tot) {
989: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
990: $ctr++;
991: }
992: } else {
993: while ($ctr < $tot) {
994: $$dest[$ctr] = $$source[$r_idx[$ctr]];
995: $ctr++;
996: }
997: }
1.6 albertel 998: }
1.51 albertel 999: &random_set_seed(@oldseed);
1000: return '';
1.5 albertel 1001: }
1.22 ng 1002:
1.23 ng 1003: sub capa_id { return }
1004:
1005: sub problem { return }
1006:
1.22 ng 1007: sub name{
1.73 albertel 1008: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
1009: $fullname = "" if $fullname eq ", ";
1010: $fullname =~ s/\%2d/-/g;
1011: return $fullname;
1.22 ng 1012: }
1013:
1014: sub student_number {
1.73 albertel 1015: my $id = &EXT('environment.id');
1016: $id = '' if $id eq "";
1017: return $id;
1.22 ng 1018: }
1019:
1020: sub class {
1.73 albertel 1021: my $course = &EXT('course.description');
1022: $course = '' if $course eq "";
1023: return $course;
1.22 ng 1024: }
1025:
1.112 www 1026: sub firstname {
1027: my $firstname = &EXT('environment.firstname');
1028: $firstname = '' if $firstname eq "";
1029: return $firstname;
1030: }
1031:
1032: sub lastname {
1033: my $lastname = &EXT('environment.lastname');
1034: $lastname = '' if $lastname eq "";
1035: return $lastname;
1036: }
1037:
1.22 ng 1038: sub sec {
1.73 albertel 1039: my $sec = &EXT('request.course.sec');
1040: $sec = '' if $sec eq "";
1041: return $sec;
1.22 ng 1042: }
1043:
1.23 ng 1044: sub open_date {
1.73 albertel 1045: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
1046: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
1047: my @hm = split(/:/,$dc[3]);
1048: my $ampm = " am";
1049: if ($hm[0] > 12) {
1050: $hm[0]-=12;
1051: $ampm = " pm";
1052: }
1053: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 1054: }
1055:
1056: sub due_date {
1.73 albertel 1057: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
1058: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
1059: my @hm = split(/:/,$dc[3]);
1060: my $ampm = " am";
1061: if ($hm[0] > 12) {
1062: $hm[0]-=12;
1063: $ampm = " pm";
1064: }
1065: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23 ng 1066: }
1067:
1068: sub answer_date {
1.73 albertel 1069: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
1070: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
1071: my @hm = split(/:/,$dc[3]);
1072: my $ampm = " am";
1073: if ($hm[0] > 12) {
1074: $hm[0]-=12;
1075: $ampm = " pm";
1076: }
1077: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.24 ng 1078: }
1079:
1080: sub array_moments {
1.73 albertel 1081: my @input=@_;
1082: my (@output,$N);
1083: $N=scalar (@input);
1084: $output[0]=$N;
1085: if ($N <= 1) {
1086: $output[1]=$input[0];
1087: $output[1]="Input array not defined" if ($N == 0);
1088: $output[2]="variance undefined for N<=1";
1089: $output[3]="skewness undefined for N<=1";
1090: $output[4]="kurtosis undefined for N<=1";
1091: return @output;
1092: }
1093: my $sum=0;
1094: foreach my $line (@input) {
1095: $sum+=$line;
1096: }
1097: $output[1] = $sum/$N;
1098: my ($x,$sdev,$var,$skew,$kurt) = 0;
1099: foreach my $line (@input) {
1100: $x=$line-$output[1];
1101: $var+=$x**2;
1102: $skew+=$x**3;
1103: $kurt+=$x**4;
1104: }
1105: $output[2]=$var/($N-1);
1106: $sdev=CORE::sqrt($output[2]);
1107: if ($sdev == 0) {
1108: $output[3]="inf-variance=0";
1109: $output[4]="inf-variance=0";
1110: return @output;
1111: }
1112: $output[3]=$skew/($sdev**3*$N);
1113: $output[4]=$kurt/($sdev**4*$N)-3;
1.24 ng 1114: return @output;
1115: }
1.5 albertel 1116:
1117: sub choose {
1.73 albertel 1118: my $num = $_[0];
1119: return $_[$num];
1.5 albertel 1120: }
1.23 ng 1121:
1.101 albertel 1122: #&sum1(1,$x,sub { &sum1($_[0],2*$_[0], sub { fact($_[0])**2 })});
1123: #sub sum1 {
1124: # my ($start,$end,$sub)=@_;
1125: # my $sum=0;
1126: # for (my $i=$start;$i<=$end;$i++) {
1127: # $sum+=&$sub($i);
1128: # }
1129: # return $sum
1130: #}
1131:
1132: #&sum2('a',1,$x,'&sum2(\'b\',$a,2*$a, \'&factorial($b)**2\')');
1133: #sub sum2 {
1134: # my ($varname,$start,$end,$line)=@_;
1135: # my $sum=0;
1136: # for (my $i=$start;$i<=$end;$i++) {
1137: # my $func=sub {
1138: # eval("\$".$varname."=$i");
1139: # eval($line);
1140: # };
1141: # $sum+=&$func($i);
1142: # }
1143: # return $sum
1144: #}
1145:
1.49 albertel 1146: # expiremental idea
1147: sub proper_path {
1.73 albertel 1148: my ($path)=@_;
1149: if ( $external::target eq "tex" ) {
1150: return '/home/httpd/html'.$path;
1151: } else {
1152: return $path;
1153: }
1.49 albertel 1154: }
1.23 ng 1155:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>