File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.145: download - view: text, annotated - select for diffs
Tue Jun 23 03:01:15 2009 UTC (14 years, 10 months ago) by www
Branches: MAIN
CVS tags: version_2_8_99_0, bz2851, HEAD, GCI_3, GCI_2
Paul Rubin's interface to R.

Works now. However, it needs additional Perl libraries, which are currently
commented out. Also, an R-package needs to be installed.

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

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