Annotation of loncom/homework/default_homework.lcpm, revision 1.137

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

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