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

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

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