File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.122: download - view: text, annotated - select for diffs
Fri Dec 15 21:15:42 2006 UTC (17 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Doesn't make much sense to convert a3 to a*3

    1: # The LearningOnline Network with CAPA 
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: #
    4: # $Id: default_homework.lcpm,v 1.122 2006/12/15 21:15:42 www Exp $
    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: #
   28: #
   29: 
   30: #init some globals
   31: $hidden::RANDOMINIT=0;
   32: $pi=atan2(1,1)*4;
   33: $rad2deg=180.0/$pi;
   34: $deg2rad=$pi/180.0;
   35: $"=' ';
   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--;
   61: 	    return \@order;
   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--;
   95: 	return(\@order);
   96:     }
   97:     
   98:     sub get_permutations_left {
   99: 	return $num_left;
  100:     }
  101: }
  102: 
  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: 
  129: 
  130: sub caparesponse_check {
  131:     my ($answer,$response)=@_;
  132:     #not properly used yet: calc
  133:     #not to be used: $ans_fmt
  134:     my $type=$LONCAPA::CAPAresponse_args{'type'};
  135:     my $tol=$LONCAPA::CAPAresponse_args{'tol'};
  136:     my $sig=$LONCAPA::CAPAresponse_args{'sig'};
  137:     my $ans_fmt=$LONCAPA::CAPAresponse_args{'format'};
  138:     my $unit=$LONCAPA::CAPAresponse_args{'unit'};
  139:     my $calc=$LONCAPA::CAPAresponse_args{'calc'};
  140:     my $samples=$LONCAPA::CAPAresponse_args{'samples'};
  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
  148: 
  149:     #remove leading and trailing whitespace
  150:     if (!defined($response)) {
  151: 	$response='';
  152:     }
  153:     if ($response=~ /^\s|\s$/) {
  154: 	$response=~ s:^\s+|\s+$::g;
  155: 	&LONCAPA_INTERNAL_DEBUG("Removed ws now :$response:");
  156:     }
  157: 
  158:     #&LONCAPA_INTERNAL_DEBUG(" type is $type ");
  159:     if ($type eq 'cs' || $type eq 'ci') {
  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;
  164:     } elsif ($type eq 'mc') {
  165: 	$answer=~s/[\s,]//g;
  166: 	$response=~s/[\s,]//g;
  167:     }
  168:     if ($type eq 'float' && $unit=~/\$/) {
  169: 	if ($response!~/^\$/)  { return ('NO_UNIT', undef); }
  170: 	$response=~s/\$//g;
  171:     }
  172:     if ($type eq 'float' && $unit=~/\,/ && (&check_commas($response)<0)) {
  173: 	return "COMMA_FAIL:";
  174:     }
  175:     $ans_fmt=~s/\W//g;
  176:     $unit=~s/[\$,]//g;
  177:     if ($type eq 'float') { $response=~s/,//g; }
  178: 
  179:     if (length($response) > 500) { return ('TOO_LONG',undef); }
  180: 
  181:     if ($type eq '' ) {
  182: 	&LONCAPA_INTERNAL_DEBUG("Didn't find a type :$type: defaulting");
  183: 	if ( $answer eq ($answer *1.0)) { $type = 2;
  184: 				      } else { $type = 3; }
  185:     } else {
  186: 	if    ($type eq 'cs')    { $type = 4; }
  187: 	elsif ($type eq 'ci')    { $type = 3 }
  188: 	elsif ($type eq 'mc')    { $type = 5; }
  189: 	elsif ($type eq 'fml')   { $type = 8; }
  190:         elsif ($type eq 'math')  { $type = 9; }
  191: 	elsif ($type eq 'subj')  { $type = 7; }
  192: 	elsif ($type eq 'float') { $type = 2; }
  193: 	elsif ($type eq 'int')   { $type = 1; }
  194: 	else { return ('ERROR', "Unknown type of answer: $type") }
  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);
  202: 	&LONCAPA_INTERNAL_DEBUG("Found :$id_list:$points: points in $samples");
  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: 
  216:     ($sig_ubound,$sig_lbound)=&LONCAPA_INTERNAL_get_sigrange($sig);
  217: 
  218:     my $reterror="";
  219:     my $result;
  220:     if ($type eq '9') {
  221:       $result = &maxima_check(&maxima_cas_formula_fix($response),&maxima_cas_formula_fix($answer),\$reterror);
  222:     } else {
  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,
  228: 						 $tol_type,$tol,
  229: 						 $sig_lbound,$sig_ubound,
  230: 						 $ans_fmt,$unit,$calc,$id_list,
  231: 						 $points,$external::randomseed,
  232: 						 \$reterror);
  233:     }
  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'; }
  245:     elsif ($result =='12' && !$response) { $result='MISSING_ANSWER'; }
  246:     elsif ($result =='12') { $result='WANTED_NUMERIC'; }
  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'; }
  252:     else  {$result = "ERROR: Unknown Result:$result:$@:";}
  253: 
  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 ");
  256:     return ($result,$reterror)
  257: }
  258: 
  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)=@_;
  271: # Escape scientific notation, so 3e8 does not become 3*e*8
  272: # 3e8 -> 3&8; 3e-8 -> 3&-8; 3E+8 -> e&+8
  273:     $expression=~s/(\d+)e([\+\-]*\d+)/$1\&\($2\)/gsi;
  274: # 3x10^8 -> 3&8; 3*10^-8 -> 3&-8
  275:     $expression=~s/(\d+)(?:x|\*)10(?:\^|\*\*)([\+\-]*\d+)/$1\&\($2\)/gsi;
  276: # Fill in multiplication signs
  277: # a b -> a*b;3 b -> 3*b;3 4 -> 3*4
  278:     $expression=~s/(\w)\s+(\w)/$1\*$2/gs;
  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;
  287: # 3&8 -> 3e8; 3&-4 -> 3e-4
  288:     $expression=~s/(\d+)\&\(([\+\-]*\d+)\)/$1e$2/gs;
  289:     return $expression;
  290: }
  291: 
  292: sub caparesponse_check_list {
  293:     my $responses=$LONCAPA::CAPAresponse_args{'response'};
  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");
  298:     &LONCAPA_INTERNAL_DEBUG("args ".join(':',%LONCAPA::CAPAresponse_args));
  299:     my $type = $LONCAPA::CAPAresponse_args{'type'};
  300:     &LONCAPA_INTERNAL_DEBUG("Got type :$type:\n");
  301:     
  302:     my $num_input_lines =
  303: 	scalar(@{$LONCAPA::CAPAresponse_answer->{'answers'}});
  304:     
  305:     if ($type ne '' ) {
  306: 	if (scalar(@$responses) < $num_input_lines) {
  307: 	    return 'MISSING_ANSWER';
  308: 	}
  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:     }
  325: #    &LONCAPA_INTERNAL_DEBUG(" parsed response is ".
  326: #			    &LONCAPA_INTERNAL_Dumper($responses).":\n");
  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) {
  336: 	    return 'EXTRA_ANSWER';
  337: 	}
  338:     }
  339: 
  340:     &LONCAPA_INTERNAL_DEBUG("Initial final response :$responses->[0][-1]:");
  341:     my $unit;
  342:     if ($type eq '' || $type eq 'float') {
  343: 	#for numerical problems split off the unit
  344: 	if ( $responses->[0][-1]=~ /(.*[^\s])\s+([^\s]+)/ ) {
  345: 	    $responses->[0][-1]=$1;
  346: 	    $unit=$2;
  347: 	}
  348:     }
  349:     &LONCAPA_INTERNAL_DEBUG("Final final response :$responses->[0][-1]:$unit:");
  350:     $unit=~s/\s//;
  351:     if ($unit ne '') {
  352: 	foreach my $response (@$responses) {
  353: 	    foreach my $element (@$response) {	    
  354: 		$element .= " $unit";
  355: 	    }
  356: 	}
  357:     }
  358:     
  359:     foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
  360: 	if (!defined($thisanswer)) {
  361: 	    return ('ERROR','answer was undefined');
  362: 	}
  363:     }
  364: 
  365: 
  366: #    &LONCAPA_INTERNAL_DEBUG(&LONCAPA_INTERNAL_Dumper($responses));
  367:     my %memoized;
  368:     if ($LONCAPA::CAPAresponse_answer->{'type'}  eq 'ordered') {
  369: 	for (my $i=0; $i<scalar(@$responses);$i++) {
  370: 	    my $answer   = $LONCAPA::CAPAresponse_answer->{'answers'}[$i];
  371: 	    my $response = $responses->[$i];
  372: 	    my $key = "$answer\0$response";
  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];
  383: 	}
  384:     } else {
  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});
  390: 	    foreach my $answer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
  391: 		my $key = "$answer\0$response";
  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];
  410: 	    }
  411: 	}
  412:     }
  413: 
  414:     my ($final_award,$final_msg);
  415:     &init_permutation(scalar(@$responses),
  416: 		      $LONCAPA::CAPAresponse_answer->{'type'});
  417: 
  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: 
  426:     my (@final_awards,@final_msg);
  427:     while( &get_permutations_left() ) {
  428: 	my $order = &get_next_permutation();
  429: 	my (@awards, @msgs, $i);
  430: 	foreach my $thisanswer (@{ $LONCAPA::CAPAresponse_answer->{'answers'} }) {
  431: 	    my $key = "$thisanswer\0".$responses->[$order->[$i]];
  432: 	    push(@awards,$memoized{$key}[0]);
  433: 	    push(@msgs,$memoized{$key}[1]);
  434: 	    $i++;
  435: 
  436: 	}
  437: 	&LONCAPA_INTERNAL_DEBUG(" all awards ".join(':',@awards));
  438: 
  439: 	my ($possible_award,$possible_msg) = 
  440: 	    &LONCAPA_INTERNAL_FINALIZEAWARDS(\@awards,\@msgs);
  441: 	&LONCAPA_INTERNAL_DEBUG(" pos awards ".$possible_award);
  442: 	push(@final_awards,$possible_award);
  443: 	push(@final_msg,$possible_msg);
  444:     }
  445: 
  446:     &LONCAPA_INTERNAL_DEBUG(" all final_awards ".join(':',@final_awards));
  447:     my ($final_award,$final_msg) = 
  448: 	&LONCAPA_INTERNAL_FINALIZEAWARDS(\@final_awards,\@final_msg,undef,1);
  449:     return ($final_award,$final_msg);
  450: }
  451: 
  452: sub tex {
  453:     if ( $external::target eq "tex" ) {
  454: 	return $_[0];
  455:     } else {
  456: 	return $_[1];
  457:     }
  458: }
  459: 
  460: sub var_in_tex {
  461:     if ( $external::target eq "tex" ) {
  462: 	return $_[0];
  463:     } else {
  464: 	return "";
  465:     }
  466: }
  467: 
  468: sub web {
  469:     if ( $external::target eq "tex" ) {
  470: 	return $_[1];
  471:     } else {
  472: 	if ( $external::target eq "web" || $external::target eq "answer") {
  473: 	    return $_[2];
  474: 	} else {
  475: 	    return $_[0];
  476: 	}
  477:     }
  478: }
  479: 
  480: sub html {
  481:     if ( $external::target eq "web" ) {
  482: 	return shift;
  483:     }
  484: }
  485: 
  486: sub hinton {
  487:     return 0;
  488: }
  489: 
  490: sub random {
  491:     my ($start,$end,$step)=@_;
  492:     if ( ! $hidden::RANDOMINIT ) {
  493: 	if ($external::randomseed == 0) { $external::randomseed=1; }
  494: 	if ($external::randomseed =~/,/) {
  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);
  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;
  510: }
  511: 
  512: sub random_normal {
  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;
  520: }
  521: 
  522: sub random_beta {
  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;
  530: }
  531: 
  532: sub random_gamma {
  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;
  540: }
  541: 
  542: sub random_exponential {
  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;
  550: }
  551: 
  552: sub random_poisson {
  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;
  560: }
  561: 
  562: sub random_chi {
  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;
  570: }
  571: 
  572: sub random_noncentral_chi {
  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;
  580: }
  581: 
  582: sub random_f {
  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;
  590: }
  591: 
  592: sub random_noncentral_f {
  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;
  600: }
  601: 
  602: sub random_multivariate_normal {
  603:     my ($item_cnt,$seed,$mean,$covar) = @_;
  604:     my @oldseed=&random_get_seed();
  605:     &random_set_seed_from_phrase($seed);
  606:     my @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
  607:     &random_set_seed(@oldseed);
  608:     return @retArray;
  609: }
  610: 
  611: sub random_multinomial {
  612:     my ($item_cnt,$seed,@p) = @_;
  613:     my @oldseed=&random_get_seed();
  614:     my @retArray;
  615:     &random_set_seed_from_phrase($seed);
  616:     my @retArray=&math_random_multinomial($item_cnt,@p);
  617:     &random_set_seed(@oldseed);
  618:     return @retArray;
  619: }
  620: 
  621: sub random_permutation {
  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;
  629: }
  630: 
  631: sub random_uniform {
  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;
  639: }
  640: 
  641: sub random_uniform_integer {
  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;
  649: }
  650: 
  651: sub random_binomial {
  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;
  659: }
  660: 
  661: sub random_negative_binomial {
  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;
  669: }
  670: 
  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) }
  679: 
  680: sub tan  { CORE::sin($_[0]) / CORE::cos($_[0]) }
  681: #sub atan { atan2($_[0], 1); }
  682: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  683: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  684: 
  685: sub log10 { CORE::log($_[0])/CORE::log(10); }
  686: 
  687: sub factorial {
  688:     my $input = CORE::int(shift);
  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: }
  712: 
  713: sub roundto {
  714:     my ($input,$n) = @_;
  715:     return sprintf('%.'.$n.'f',$input);
  716: }
  717: 
  718: sub to_string {
  719:     my ($input,$n) = @_;
  720:     return sprintf($input) if $n eq "";
  721:     $n = '.'.$n if $n !~ /^\./;
  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: }
  729: 
  730: sub pow   {return $_[0] ** $_[1]; }
  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)); }
  733: #sub floor {return int($_[0]); }
  734: 
  735: sub format {
  736:     my ($value,$fmt)=@_;
  737:     my ($dollarmode,$commamode,$alwaysperiod,$options);
  738:     if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; } 
  739:     #if ($options =~ /\$/) { $dollamode=1; }
  740:     #if ($options =~ /,/)  { $commamode=1; }
  741:     if ($options =~ /\./) { $alwaysperiod=1; }
  742:     my $result;
  743:     if ($fmt=~/s$/i) {
  744: 	$result=&format_significant_figures($value,$fmt);
  745:     } else {
  746: 	$fmt=~s/e/E/g;
  747: 	$result=sprintf('%.'.$fmt,$value);
  748: 	if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
  749: 	$result=~s/(E[+-]*)0/$1/;
  750:     }
  751:     #if ($dollarmode) {$result=&dollarformat($result);}
  752:     #if ($commamode) {$result=&commaformat($result);}
  753:     return $result;
  754: }
  755: 
  756: sub chemparse {
  757:     my ($reaction) = @_;
  758:     my @tokens = split(/(\s\+|\->|<=>|<\-|\.)/,$reaction);
  759:     my $formula = '';
  760:     foreach my $token (@tokens) {
  761: 	if ($token eq '->' ) {
  762: 	    $formula .= '<m>\ensuremath{\rightarrow}</m> ';
  763: 	    next;
  764: 	}
  765: 	if ($token eq '<-' ) {
  766: 	    $formula .= '<m>\ensuremath{\leftarrow}</m> ';
  767: 	    next;
  768: 	}  
  769: 	if ($token eq '<=>') {
  770: 	    if ($external::target eq 'web' &&
  771: 		&EXT('request.browser.unicode')) {
  772: 		$formula .= '&#8652; ';
  773: 	    } else {
  774: 		$formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
  775: 				 '&lt;=&gt; ');
  776: 	    }
  777: 	    next;
  778: 	}
  779: 	if ($token eq '.') {
  780: 	  $formula =~ s/(\&nbsp\;| )$//;
  781: 	  $formula .= '&middot;';
  782: 	  next;
  783: 	}
  784: 	$token =~ /^\s*([\d|\/]*(?:&frac\d\d)?)(.*)/;
  785:         $formula .= $1 if ($1 ne '1');  # stoichiometric coefficient
  786: 	
  787: 	my $molecule = $2;
  788: 	# subscripts
  789: 	$molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
  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;
  796: 	$molecule =~ s/-/&minus;/g;
  797: 	$formula .= $molecule.'&nbsp;';
  798:     }
  799:     # get rid of trailing space
  800:     $formula =~ s/(\&nbsp\;| )$//;
  801:     return &xmlparse($formula);
  802: }
  803: 
  804: sub prettyprint {
  805:     my ($value,$fmt,$target)=@_;
  806:     my $result;
  807:     if (!$target) { $target = $external::target; }
  808:     if ($fmt =~ /chem/i) { return(&chemparse($value)); }
  809:     my ($dollarmode,$commamode,$alwaysperiod,$options);
  810:     if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; } 
  811:     if ($options =~ /\$/) { $dollarmode=1; }
  812:     if ($options =~ /,/)  { $commamode=1; }
  813:     if ($options =~ /\./) { $alwaysperiod=1; }
  814:     if ($fmt=~/s$/i) {
  815: 	$value=&format_significant_figures($value,$fmt);
  816:     } elsif ($fmt) {
  817: 	$value=sprintf('%.'.$fmt,$value);
  818:     }
  819:     if ($alwaysperiod && $fmt eq '0f') {
  820: 	if ($target eq 'tex') {
  821: 	    $value .='\\ensuremath{.}';
  822: 	} else {
  823: 	    $value .='.';
  824: 	}
  825:     }
  826:     if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
  827: 	my $frac=$1;
  828: 	if ($dollarmode) { $frac=&dollarformat($frac); }
  829: 	if ($commamode) { $frac=&commaformat($frac); }
  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.'&#215;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 {
  847: 	$result=$value;
  848: 	if    ($dollarmode) { $result=&dollarformat($result,$target); }
  849: 	elsif ($commamode)  { $result=&commaformat($result,$target); }
  850:     }
  851:     return $result;
  852: }
  853: 
  854: sub commaformat {
  855:     my ($number,$target) = @_;
  856:     if ($number =~ /\./) {
  857: 	while ($number =~ /([^0-9]*)([0-9]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
  858: 	    $number = $1.$2.','.$3.$4;
  859: 	}
  860:     } else {
  861: 	while ($number =~ /^([^0-9]*)([0-9]+)([^,][^,][^,])([,0-9]*)$/) {
  862: 	    $number = $1.$2.','.$3.$4;
  863: 	}
  864:     }
  865:     return $number;
  866: }
  867: 
  868: sub dollarformat {
  869:     my ($number,$target) = @_;
  870:     if (!$target) { $target = $external::target; }
  871:     $number=&commaformat($number,$target);
  872:     if ($target eq 'tex') {
  873: 	$number='\$'.$number; #' stupid emacs
  874:     } else {
  875: 	$number='$'.$number; #' stupid emacs
  876:     }
  877:     return $number; 
  878: }
  879: 
  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/;
  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:     }
  910:     # return number with sign
  911:     return $sign.$numSig;
  912: 
  913: }
  914: 
  915: sub map {
  916:     my ($phrase,$dest,$source)=@_;
  917:     my @oldseed=&random_get_seed();
  918:     my @seed = &random_seed_from_phrase($phrase);
  919:     &random_set_seed(@seed);
  920:     my $destct = scalar(@$dest);
  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]];
  927: 	    $ctr++;
  928: 	}
  929:         &random_set_seed(@oldseed);
  930: 	return @output;
  931:     } else {
  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: 	}
  948:     }
  949:     &random_set_seed(@oldseed);
  950:     return '';
  951: }
  952: 
  953: sub rmap {
  954:     my ($phrase,$dest,$source)=@_;
  955:     my @oldseed=&random_get_seed();
  956:     my @seed = &random_seed_from_phrase($phrase);
  957:     &random_set_seed(@seed);
  958:     my $destct = scalar(@$dest);
  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]];
  971: 	    $ctr++;
  972: 	}
  973:         &random_set_seed(@oldseed);
  974: 	return @output;
  975:     } else {
  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;
  982: 	while ($ctr < $tot) {
  983: 	    $r_idx[$idx[$ctr]] = $ctr;
  984: 	    $ctr++;
  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: 	}
  998:     }
  999:     &random_set_seed(@oldseed);
 1000:     return '';
 1001: }
 1002: 
 1003: sub capa_id { return }
 1004: 
 1005: sub problem { return }
 1006: 
 1007: sub name{
 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;
 1012: }
 1013: 
 1014: sub student_number { 
 1015:     my $id = &EXT('environment.id');
 1016:     $id = '' if $id eq "";
 1017:     return $id;
 1018: }
 1019: 
 1020: sub class {
 1021:     my $course = &EXT('course.description');
 1022:     $course = '' if $course eq "";
 1023:     return $course;
 1024: }
 1025: 
 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: 
 1038: sub sec { 
 1039:     my $sec = &EXT('request.course.sec');
 1040:     $sec = '' if $sec eq "";
 1041:     return $sec;
 1042: }
 1043: 
 1044: sub open_date { 
 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;
 1054: }
 1055: 
 1056: sub due_date { 
 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;
 1066: }
 1067: 
 1068: sub answer_date { 
 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;
 1078: }
 1079: 
 1080: sub array_moments {
 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;
 1114:     return @output;
 1115: }
 1116: 
 1117: sub choose {
 1118:     my $num = $_[0];
 1119:     return $_[$num];
 1120: }
 1121: 
 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: 
 1146: # expiremental idea
 1147: sub proper_path {
 1148:     my ($path)=@_;
 1149:     if ( $external::target eq "tex" ) {
 1150: 	return '/home/httpd/html'.$path;
 1151:     } else {
 1152: 	return $path;
 1153:     }
 1154: }
 1155: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>