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

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.93    ! albertel    4: # $Id: default_homework.lcpm,v 1.92 2005/02/17 21:42:37 albertel Exp $
1.42      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.20      harris41   28: #
1.76      albertel   29: 
1.25      albertel   30: #init some globals
1.38      albertel   31: $hidden::RANDOMINIT=0;
1.22      ng         32: $pi=atan2(1,1)*4;
                     33: $rad2deg=180.0/$pi;
                     34: $deg2rad=$pi/180.0;
1.44      matthew    35: $"=' ';
1.3       albertel   36: 
1.91      albertel   37: sub check_commas {
                     38:     my ($response)=@_;
                     39:     #print("$response ");
                     40:     my @numbers=split(',',$response);
                     41:     #print(" numbers ".join('-',@numbers)." ");
                     42:     if (scalar(@numbers) > 1) {
                     43:         #print(" numbers[0] ".$numbers[0]." "); 
                     44: 	if (length($numbers[0]) > 3 || length($numbers[0]) == 0) { return -1; }
                     45: 	shift(@numbers);
                     46: 	#print(" numbers ".scalar(@numbers)." ");
                     47: 	while (scalar(@numbers) > 1) {
                     48: 	    #print(" numbers ".join('-',@numbers)." ");
                     49: 	    if (length($numbers[0]) != 3) { return -2; }
                     50: 	    shift(@numbers);
                     51: 	}
                     52: 	my ($number)=split('\.',$numbers[0]);
                     53: 	#print(" number ".$number." ");
                     54: 	#print(" numbers[0] ".$numbers[0]." ");
                     55: 	if (length($number) != 3) { return -3; }
                     56:     } else {
                     57: 	my ($number)=split('\.',$numbers[0]);
                     58: 	if (length($number) > 3) { return -4; }
                     59:     }
                     60:     return 1;
                     61: }
                     62: 
1.7       albertel   63: sub caparesponse_check {
1.74      albertel   64:     my ($answer,$response)=@_;
1.73      albertel   65:     #not properly used yet: calc
                     66:     #not to be used: $ans_fmt
1.74      albertel   67:     my $type=$LONCAPA::CAPAresponse_args{'type'};
                     68:     my $tol=$LONCAPA::CAPAresponse_args{'tol'};
                     69:     my $sig=$LONCAPA::CAPAresponse_args{'sig'};
1.88      albertel   70:     my $ans_fmt=$LONCAPA::CAPAresponse_args{'format'};
1.74      albertel   71:     my $unit=$LONCAPA::CAPAresponse_args{'unit'};
                     72:     my $calc=$LONCAPA::CAPAresponse_args{'calc'};
                     73:     my $samples=$LONCAPA::CAPAresponse_args{'samples'};
1.73      albertel   74:     
                     75:     my $tol_type=''; # gets it's value from whether tol has a % or not done
                     76:     my $sig_lbound=''; #done
                     77:     my $sig_ubound=''; #done
                     78: 
                     79: 
                     80:     #type's definitons come from capaParser.h
                     81:     my $message='';
                     82:     #remove leading and trailing whitespace
                     83:     if (!defined($response)) {
                     84: 	$response='';
                     85:     }
                     86:     if ($response=~ /^\s|\s$/) {
                     87: 	$response=~ s:^\s+|\s+$::g;
                     88: 	$message .="Removed ws now :$response:\n";
                     89:     } else {
                     90: 	$message .="no ws in :$response:\n";
                     91:     }
1.76      albertel   92:     if ($type eq 'cs' || $type eq 'ci' || $type eq 'mc') {
                     93: 	#for string answers make surec all places spaces occur, there is 
                     94:         #really only 1 space, in both the answer and the response
                     95: 	$answer=~s/ +/ /g;
                     96: 	$response=~s/ +/ /g;
                     97:     }
1.91      albertel   98:     if ($type eq 'float' && $unit=~/\$/) {
1.88      albertel   99: 	if ($response!~/^\$/)  { return "NO_UNIT: Missing \$ "; }
                    100: 	$response=~s/\$//g;
                    101:     }
1.91      albertel  102:     if ($type eq 'float' && $unit=~/\,/ && (&check_commas($response)<0)) {
                    103: 	return "COMMA_FAIL:";
                    104:     }
1.88      albertel  105:     $ans_fmt=~s/\W//g;
1.91      albertel  106:     $unit=~s/[\$,]//g;
                    107:     if ($type eq 'float') { $response=~s/,//g; }
1.88      albertel  108: 
1.73      albertel  109:     if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
                    110: 
                    111:     if ($type eq '' ) {
1.74      albertel  112: 	$message .= "Didn't find a type :$type: defaulting\n";
1.73      albertel  113: 	if ( $answer eq ($answer *1.0)) { $type = 2;
                    114: 				      } else { $type = 3; }
                    115:     } else {
                    116: 	if ($type eq 'cs')    { $type = 4; }
                    117: 	elsif ($type eq 'ci')    { $type = 3 }
                    118: 	elsif ($type eq 'mc')    { $type = 5; }
                    119: 	elsif ($type eq 'fml')   { $type = 8; }
                    120: 	elsif ($type eq 'subj')  { $type = 7; }
                    121: 	elsif ($type eq 'float') { $type = 2; }
                    122: 	elsif ($type eq 'int')   { $type = 1; }
                    123: 	else { return "ERROR: Unknown type of answer: $type" }
                    124:     }
                    125: 
                    126:     my $points;
                    127:     my $id_list;
                    128:     #formula type setup the sample points
                    129:     if ($type eq '8') {
                    130: 	($id_list,$points)=split(/@/,$samples);
                    131: 	$message.="Found :$id_list:$points: points in $samples\n";
                    132:     }
                    133:     if ($tol eq '') {
                    134: 	$tol=0.0;
                    135: 	$tol_type=1; #TOL_ABSOLUTE
                    136:     } else {
                    137: 	if ($tol =~ /%$/) {
                    138: 	    chop $tol;
                    139: 	    $tol_type=2; #TOL_PERCENTAGE
                    140: 	} else {
                    141: 	    $tol_type=1; #TOL_ABSOLUTE
                    142: 	}
                    143:     }
                    144: 
1.85      albertel  145:     ($sig_ubound,$sig_lbound)=&LONCAPA_INTERNAL_get_sigrange($sig);
                    146: 
1.73      albertel  147:     my $reterror="";
                    148:     my $result = &caparesponse_capa_check_answer($response,$answer,$type,
                    149: 						 $tol_type,$tol,
                    150: 						 $sig_lbound,$sig_ubound,
                    151: 						 $ans_fmt,$unit,$calc,$id_list,
                    152: 						 $points,$external::randomseed,
                    153: 						 \$reterror);
                    154: 
                    155:     if    ($result == '1') { $result='EXACT_ANS'; } 
                    156:     elsif ($result == '2') { $result='APPROX_ANS'; }
                    157:     elsif ($result == '3') { $result='SIG_FAIL'; }
                    158:     elsif ($result == '4') { $result='UNIT_FAIL'; }
                    159:     elsif ($result == '5') { $result='NO_UNIT'; }
                    160:     elsif ($result == '6') { $result='UNIT_OK'; }
                    161:     elsif ($result == '7') { $result='INCORRECT'; }
                    162:     elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
                    163:     elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
                    164:     elsif ($result =='10') { $result='SUB_RECORDED'; }
                    165:     elsif ($result =='11') { $result='BAD_FORMULA'; }
1.77      albertel  166:     elsif ($result =='13') { $result='UNIT_INVALID_INSTRUCTOR'; }
                    167:     elsif ($result =='141') { $result='UNIT_INVALID_STUDENT'; }
                    168:     elsif ($result =='142') { $result='UNIT_INVALID_STUDENT'; }
                    169:     elsif ($result =='143') { $result='UNIT_INVALID_STUDENT'; }
                    170:     elsif ($result =='15') { $result='UNIT_IRRECONCIBLE'; }
1.73      albertel  171:     else  {$result = "ERROR: Unknown Result:$result:$@:";}
                    172: 
1.82      albertel  173:     return ("$result:\nRetError $reterror:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message",$reterror);
1.37      albertel  174: }
                    175: 
1.73      albertel  176: 
1.37      albertel  177: sub caparesponse_check_list {
1.74      albertel  178:     my $response=$LONCAPA::CAPAresponse_args{'response'};
1.73      albertel  179:     my ($result,@list);
1.74      albertel  180:     @list=@LONCAPA::CAPAresponse_answer;
1.73      albertel  181:     my $aresult='';
                    182:     my $current_answer;
                    183:     my $answers=join(':',@list);
                    184:     $result.="Got response :$answers:\n";
1.74      albertel  185:     &LONCAPA_INTERNAL_DEBUG("<blink>Yo!</blink> got ".join(':',%LONCAPA::CAPAresponse_args));
1.73      albertel  186:     my @responselist;
1.74      albertel  187:     my $type = $LONCAPA::CAPAresponse_args{'type'};
1.73      albertel  188:     $result.="Got type :$type:\n";
                    189:     if ($type ne '' && $#list > 0) {
                    190: 	(@responselist)=split /,/,$response;
                    191:     } else {
                    192: 	(@responselist)=($response);
                    193:     }
                    194:     my $unit='';
                    195:     $result.="Initial final response :$responselist['-1']:\n";
                    196:     if ($type eq '' || $type eq 'float') {
                    197: 	#for numerical problems split off the unit
                    198: 	if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
                    199: 	    $responselist['-1']=$1;
                    200: 	    $unit=$2;
                    201: 	}
                    202:     }
1.82      albertel  203:     $result.="Final final response :$responselist['-1']:$unit:\n";
1.73      albertel  204:     $result.=":$#list: answers\n";
                    205:     $unit=~s/\s//;
                    206:     my $i=0;
                    207:     my $awards='';
                    208:     my @msgs;
                    209:     for ($i=0; $i<@list;$i++) {
                    210: 	my $msg;
                    211: 	$result.="trying answer :$list[$i]:\n";
                    212: 	my $thisanswer=$list[$i];
                    213: 	$result.="trying answer :$thisanswer:\n";
                    214: 	if ($unit eq '') {
1.74      albertel  215: 	    ($aresult,$msg)=&caparesponse_check($thisanswer,$responselist[$i]);
1.73      albertel  216: 	} else {
                    217: 	    ($aresult,$msg)=&caparesponse_check($thisanswer,
1.74      albertel  218: 						$responselist[$i]." $unit");
1.73      albertel  219: 	}
                    220: 	my ($temp)=split /:/, $aresult;
                    221: 	$awards.="$temp,";
                    222: 	$result.=$aresult;
                    223: 	push(@msgs,$msg);
                    224:     }
                    225:     chop $awards;
                    226:     return ("$awards:\n$result",@msgs);
1.7       albertel  227: }
                    228: 
1.4       albertel  229: sub tex {
1.73      albertel  230:     if ( $external::target eq "tex" ) {
                    231: 	return $_[0];
                    232:     } else {
                    233: 	return $_[1];
                    234:     }
1.4       albertel  235: }
                    236: 
1.24      ng        237: sub var_in_tex {
1.73      albertel  238:     if ( $external::target eq "tex" ) {
                    239: 	return $_[0];
                    240:     } else {
                    241: 	return "";
                    242:     }
1.24      ng        243: }
                    244: 
1.4       albertel  245: sub web {
1.73      albertel  246:     if ( $external::target eq "tex" ) {
                    247: 	return $_[1];
1.26      ng        248:     } else {
1.73      albertel  249: 	if ( $external::target eq "web" || $external::target eq "answer") {
                    250: 	    return $_[2];
                    251: 	} else {
                    252: 	    return $_[0];
                    253: 	}
1.4       albertel  254:     }
                    255: }
                    256: 
1.24      ng        257: sub html {
1.73      albertel  258:     if ( $external::target eq "web" ) {
                    259: 	return shift;
                    260:     }
1.24      ng        261: }
                    262: 
1.1       harris41  263: sub hinton {
1.73      albertel  264:     return 0;
1.1       harris41  265: }
                    266: 
                    267: sub random {
1.61      albertel  268:     my ($start,$end,$step)=@_;
                    269:     if ( ! $hidden::RANDOMINIT ) {
                    270: 	if ($external::randomseed == 0) { $external::randomseed=1; }
                    271: 	if ($external::randomseed =~/,/) {
1.84      albertel  272: 	    my ($num1,$num2)=split(/,/,$external::randomseed);
                    273: 	    &random_set_seed(1,abs($num1));
                    274: 	} elsif ($external::randomseed =~/:/) {
                    275: 	    my ($num1,$num2)=split(/:/,$external::randomseed);
1.61      albertel  276: 	    &random_set_seed(abs($num1),abs($num2));
                    277: 	} else {
                    278: 	    &random_set_seed(1,int(abs($external::randomseed)));
                    279: 	}
                    280: 	&math_random_uniform();
                    281: 	$hidden::RANDOMINIT=1;
                    282:     }
                    283:     if (!defined($step)) { $step=1; }
                    284:     my $num=1+int(($end-$start)/$step);
                    285:     my $result=$start + int(&math_random_uniform() * $num)*$step;
                    286:     return $result;
1.1       harris41  287: }
                    288: 
1.26      ng        289: sub random_normal {
1.73      albertel  290:     my ($item_cnt,$seed,$av,$std_dev) = @_;
                    291:     my @oldseed=&random_get_seed();
                    292:     my @retArray;
                    293:     &random_set_seed_from_phrase($seed);
                    294:     @retArray=&math_random_normal($item_cnt,$av,$std_dev);
                    295:     &random_set_seed(@oldseed);
                    296:     return @retArray;
1.26      ng        297: }
                    298: 
                    299: sub random_beta {
1.73      albertel  300:     my ($item_cnt,$seed,$aa,$bb) = @_;
                    301:     my @oldseed=&random_get_seed();
                    302:     my @retArray;
                    303:     &random_set_seed_from_phrase($seed);
                    304:     @retArray=&math_random_beta($item_cnt,$aa,$bb);
                    305:     &random_set_seed(@oldseed);
                    306:     return @retArray;
1.26      ng        307: }
                    308: 
                    309: sub random_gamma {
1.73      albertel  310:     my ($item_cnt,$seed,$a,$r) = @_;
                    311:     my @oldseed=&random_get_seed();
                    312:     my @retArray;
                    313:     &random_set_seed_from_phrase($seed);
                    314:     @retArray=&math_random_gamma($item_cnt,$a,$r);
                    315:     &random_set_seed(@oldseed);
                    316:     return @retArray;
1.26      ng        317: }
                    318: 
                    319: sub random_exponential {
1.73      albertel  320:     my ($item_cnt,$seed,$av) = @_;
                    321:     my @oldseed=&random_get_seed();
                    322:     my @retArray;
                    323:     &random_set_seed_from_phrase($seed);
                    324:     @retArray=&math_random_exponential($item_cnt,$av);
                    325:     &random_set_seed(@oldseed);
                    326:     return @retArray;
1.26      ng        327: }
                    328: 
                    329: sub random_poisson {
1.73      albertel  330:     my ($item_cnt,$seed,$mu) = @_;
                    331:     my @oldseed=&random_get_seed();
                    332:     my @retArray;
                    333:     &random_set_seed_from_phrase($seed);
                    334:     @retArray=&math_random_poisson($item_cnt,$mu);
                    335:     &random_set_seed(@oldseed);
                    336:     return @retArray;
1.26      ng        337: }
                    338: 
                    339: sub random_chi {
1.73      albertel  340:     my ($item_cnt,$seed,$df) = @_;
                    341:     my @oldseed=&random_get_seed();
                    342:     my @retArray;
                    343:     &random_set_seed_from_phrase($seed);
                    344:     @retArray=&math_random_chi_square($item_cnt,$df);
                    345:     &random_set_seed(@oldseed);
                    346:     return @retArray;
1.26      ng        347: }
                    348: 
                    349: sub random_noncentral_chi {
1.73      albertel  350:     my ($item_cnt,$seed,$df,$nonc) = @_;
                    351:     my @oldseed=&random_get_seed();
                    352:     my @retArray;
                    353:     &random_set_seed_from_phrase($seed);
                    354:     @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
                    355:     &random_set_seed(@oldseed);
                    356:     return @retArray;
1.26      ng        357: }
                    358: 
                    359: sub random_f {
1.73      albertel  360:     my ($item_cnt,$seed,$dfn,$dfd) = @_;
                    361:     my @oldseed=&random_get_seed();
                    362:     my @retArray;
                    363:     &random_set_seed_from_phrase($seed);
                    364:     @retArray=&math_random_f($item_cnt,$dfn,$dfd);
                    365:     &random_set_seed(@oldseed);
                    366:     return @retArray;
1.26      ng        367: }
                    368: 
                    369: sub random_noncentral_f {
1.73      albertel  370:     my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
                    371:     my @oldseed=&random_get_seed();
                    372:     my @retArray;
                    373:     &random_set_seed_from_phrase($seed);
                    374:     @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
                    375:     &random_set_seed(@oldseed);
                    376:     return @retArray;
1.26      ng        377: }
                    378: 
                    379: sub random_multivariate_normal {
1.73      albertel  380:     my ($item_cnt,$seed,$mean,$covar) = @_;
                    381:     my @oldseed=&random_get_seed();
                    382:     &random_set_seed_from_phrase($seed);
1.87      albertel  383:     my @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.73      albertel  384:     &random_set_seed(@oldseed);
                    385:     return @retArray;
1.26      ng        386: }
                    387: 
                    388: sub random_multinomial {
1.73      albertel  389:     my ($item_cnt,$seed,@p) = @_;
                    390:     my @oldseed=&random_get_seed();
                    391:     my @retArray;
                    392:     &random_set_seed_from_phrase($seed);
1.87      albertel  393:     my @retArray=&math_random_multinomial($item_cnt,@p);
1.73      albertel  394:     &random_set_seed(@oldseed);
                    395:     return @retArray;
1.26      ng        396: }
                    397: 
                    398: sub random_permutation {
1.73      albertel  399:     my ($seed,@inArray) = @_;
                    400:     my @oldseed=&random_get_seed();
                    401:     my @retArray;
                    402:     &random_set_seed_from_phrase($seed);
                    403:     @retArray=&math_random_permutation(@inArray);
                    404:     &random_set_seed(@oldseed);
                    405:     return @retArray;
1.26      ng        406: }
                    407: 
                    408: sub random_uniform {
1.73      albertel  409:     my ($item_cnt,$seed,$low,$high) = @_;
                    410:     my @oldseed=&random_get_seed();
                    411:     my @retArray;
                    412:     &random_set_seed_from_phrase($seed);
                    413:     @retArray=&math_random_uniform($item_cnt,$low,$high);
                    414:     &random_set_seed(@oldseed);
                    415:     return @retArray;
1.26      ng        416: }
                    417: 
                    418: sub random_uniform_integer {
1.73      albertel  419:     my ($item_cnt,$seed,$low,$high) = @_;
                    420:     my @oldseed=&random_get_seed();
                    421:     my @retArray;
                    422:     &random_set_seed_from_phrase($seed);
                    423:     @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
                    424:     &random_set_seed(@oldseed);
                    425:     return @retArray;
1.26      ng        426: }
                    427: 
                    428: sub random_binomial {
1.73      albertel  429:     my ($item_cnt,$seed,$nt,$p) = @_;
                    430:     my @oldseed=&random_get_seed();
                    431:     my @retArray;
                    432:     &random_set_seed_from_phrase($seed);
                    433:     @retArray=&math_random_binomial($item_cnt,$nt,$p);
                    434:     &random_set_seed(@oldseed);
                    435:     return @retArray;
1.26      ng        436: }
                    437: 
                    438: sub random_negative_binomial {
1.73      albertel  439:     my ($item_cnt,$seed,$ne,$p) = @_;
                    440:     my @oldseed=&random_get_seed();
                    441:     my @retArray;
                    442:     &random_set_seed_from_phrase($seed);
                    443:     @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
                    444:     &random_set_seed(@oldseed);
                    445:     return @retArray;
1.26      ng        446: }
                    447: 
1.23      ng        448: sub abs { abs(shift) }
                    449: sub sin { sin(shift) }
                    450: sub cos { cos(shift) }
                    451: sub exp { exp(shift) }
                    452: sub int { int(shift) }
                    453: sub log { log(shift) }
                    454: sub atan2 { atan2($_[0],$_[1]) }
                    455: sub sqrt { sqrt(shift) }
                    456: 
1.59      albertel  457: sub tan  { CORE::sin($_[0]) / CORE::cos($_[0]) }
1.21      harris41  458: #sub atan { atan2($_[0], 1); }
                    459: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
                    460: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
1.22      ng        461: 
1.59      albertel  462: sub log10 { CORE::log($_[0])/CORE::log(10); }
1.22      ng        463: 
1.20      harris41  464: sub factorial {
1.59      albertel  465:     my $input = CORE::int(shift);
1.20      harris41  466:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
                    467:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
                    468:     return 1 if $input == 0;
                    469:     my $result = 1; 
                    470:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
                    471:     return $result;
                    472: }
                    473: 
                    474: sub sgn {
                    475:     return -1 if $_[0] < 0;
                    476:     return 0 if $_[0] == 0;
                    477:     return 1 if $_[0] > 0;
                    478: }
                    479: 
                    480: sub min {
                    481:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
                    482:     return shift @sorted;
                    483: }
                    484: 
                    485: sub max {
                    486:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
                    487:     return pop @sorted;
                    488: }
1.1       harris41  489: 
1.20      harris41  490: sub roundto {
                    491:     my ($input,$n) = @_;
                    492:     return sprintf('%.'.$n.'f',$input);
                    493: }
                    494: 
                    495: sub to_string {
                    496:     my ($input,$n) = @_;
1.26      ng        497:     return sprintf($input) if $n eq "";
                    498:     $n = '.'.$n if $n !~ /^\./;
1.20      harris41  499:     return sprintf('%'.$n,$input) if $n ne "";
                    500: }
                    501: 
                    502: sub sub_string {
                    503:     my ($str,$start,$len) = @_;
                    504:     return substr($str,$start-1,$len);
                    505: }
1.1       harris41  506: 
                    507: sub pow   {return $_[0] ** $_[1]; }
1.59      albertel  508: sub ceil  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
                    509: sub floor  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
1.27      ng        510: #sub floor {return int($_[0]); }
1.1       harris41  511: 
1.2       albertel  512: sub format {
1.73      albertel  513:     my ($value,$fmt)=@_;
1.81      albertel  514:     my ($dollarmode,$commamode,$alwaysperiod,$options);
                    515:     if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; } 
                    516:     #if ($options =~ /\$/) { $dollamode=1; }
                    517:     #if ($options =~ /,/)  { $commamode=1; }
1.82      albertel  518:     if ($options =~ /\./) { $alwaysperiod=1; }
1.73      albertel  519:     $fmt=~s/e/E/g;
                    520:     my $result=sprintf('%.'.$fmt,$value);
1.81      albertel  521:     if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
1.73      albertel  522:     $result=~s/(E[+-]*)0/$1/;
1.81      albertel  523:     #if ($dollarmode) {$result=&dollarformat($result);}
                    524:     #if ($commamode) {$result=&commaformat($result);}
1.73      albertel  525:     return $result;
1.46      albertel  526: }
                    527: 
1.75      albertel  528: sub chemparse {
                    529:     my ($reaction) = @_;
                    530:     my @tokens = split(/(\s\+|\->|<=>)/,$reaction);
                    531:     my $formula = '';
                    532:     foreach my $token (@tokens) {
                    533: 	if ($token eq '->' ) {
                    534: 	    $formula .= '<m>\ensuremath{\rightarrow}</m> ';
                    535: 	    next;
                    536: 	}
                    537: 	if ($token eq '<=>') {
                    538: 	    if ($external::target eq 'web' &&
                    539: 		&EXT('request.browser.unicode')) {
1.76      albertel  540: 		$formula .= '&#8652; ';
1.75      albertel  541: 	    } else {
                    542: 		$formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
                    543: 				 '&lt;=$gt; ');
                    544: 	    }
                    545: 	    next;
                    546: 	}
1.90      albertel  547: 	$token =~ /^\s*(\d*(?:&frac\d\d)?)(.*)/;
                    548:         $formula .= $1 if ($1 ne '1');  # stoichiometric coefficient
1.75      albertel  549: 	
                    550: 	my $molecule = $2;
                    551: 	# subscripts
1.78      albertel  552: 	$molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
1.75      albertel  553: 	# superscripts
                    554: 	$molecule =~ s|\^(\d*[+\-]*)|<sup>$1</sup>|g;
                    555: 	# strip whitespace
                    556: 	$molecule =~ s/\s*//g;
                    557: 	# forced space
                    558: 	$molecule =~ s/_/ /g;
                    559: 	$formula .= $molecule.'&nbsp;';
                    560:     }
                    561:     # get rid of trailing space
1.87      albertel  562:     $formula =~ s/(\&nbsp\;| )$//;
1.75      albertel  563:     return &xmlparse($formula);
                    564: }
                    565: 
1.46      albertel  566: sub prettyprint {
1.73      albertel  567:     my ($value,$fmt,$target)=@_;
                    568:     my $result;
                    569:     if (!$target) { $target = $external::target; }
1.75      albertel  570:     if ($fmt =~ /chem/i) { return(&chemparse($value)); }
1.81      albertel  571:     my ($dollarmode,$commamode,$alwaysperiod,$options);
                    572:     if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; } 
1.86      albertel  573:     if ($options =~ /\$/) { $dollarmode=1; }
1.81      albertel  574:     if ($options =~ /,/)  { $commamode=1; }
                    575:     if ($options =~ /\./) { $alwaysperiod=1; }
1.73      albertel  576:     if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
1.81      albertel  577:     if ($alwaysperiod && $fmt eq '0f') {
                    578: 	if ($target eq 'tex') {
                    579: 	    $value .='\\ensuremath{.}';
                    580: 	} else {
                    581: 	    $value .='.';
                    582: 	}
                    583:     }
1.73      albertel  584:     if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
                    585: 	my $frac=$1;
                    586: 	if ($dollarmode) { $frac=&dollarformat($frac); }
1.80      albertel  587: 	if ($commamode) { $frac=&commaformat($frac); }
1.73      albertel  588: 	my $exponent=$2;
                    589: 	$exponent=~s/^\+0*//;
                    590: 	$exponent=~s/^-0*/-/;
                    591: 	$exponent=~s/^-0*/-/;
                    592: 	if ($exponent eq '-') { undef($exponent); }
                    593: 	if ($exponent) {
                    594: 	    if ($target eq 'web') {
                    595: 		$result=$frac.'&#215;10<sup>'.$exponent.'</sup>';
                    596: 	    } elsif ($target eq 'tex') {
                    597: 		$result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
                    598: 	    } else {
                    599: 		$result=$value;
                    600: 	    }
                    601: 	} else {
                    602: 	    $result=$frac;
                    603: 	}
                    604:     } else {
1.48      albertel  605: 	$result=$value;
1.86      albertel  606: 	if    ($dollarmode) { $result=&dollarformat($result,$target); }
                    607: 	elsif ($commamode)  { $result=&commaformat($result,$target); }
1.46      albertel  608:     }
1.73      albertel  609:     return $result;
1.48      albertel  610: }
                    611: 
1.80      albertel  612: sub commaformat {
1.73      albertel  613:     my ($number,$target) = @_;
                    614:     if ($number =~ /\./) {
                    615: 	while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
                    616: 	    $number = $1.','.$2.$3;
                    617: 	}
                    618:     } else {
                    619: 	while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
                    620: 	    $number = $1.','.$2.$3;
                    621: 	}
                    622:     }
1.80      albertel  623:     return $number;
                    624: }
                    625: 
                    626: sub dollarformat {
                    627:     my ($number,$target) = @_;
                    628:     if (!$target) { $target = $external::target; }
                    629:     $number=&commaformat($number,$target);
1.73      albertel  630:     if ($target eq 'tex') {
                    631: 	$number='\$'.$number; #' stupid emacs
                    632:     } else {
                    633: 	$number='$'.$number; #' stupid emacs
                    634:     }
                    635:     return $number; 
1.2       albertel  636: }
1.5       albertel  637: 
                    638: sub map {
1.27      ng        639:     my ($phrase,$dest,$source)=@_;
1.51      albertel  640:     my @oldseed=&random_get_seed();
1.27      ng        641:     my @seed = &random_seed_from_phrase($phrase);
                    642:     &random_set_seed(@seed);
                    643:     my $destct = scalar(@$dest);
1.28      ng        644:     if (!$source) {
                    645: 	my @output;
                    646: 	my @idx = &math_random_permuted_index($destct);
                    647: 	my $ctr = 0;
                    648: 	while ($ctr < $destct) {
                    649: 	    $output[$ctr] = $$dest[$idx[$ctr]];
1.27      ng        650: 	    $ctr++;
1.28      ng        651: 	}
1.51      albertel  652:         &random_set_seed(@oldseed);
1.28      ng        653: 	return @output;
1.27      ng        654:     } else {
1.28      ng        655: 	my $num = scalar(@$source);
                    656: 	my @idx = &math_random_permuted_index($num);
                    657: 	my $ctr = 0;
                    658: 	my $tot = $num;
                    659: 	$tot = $destct if $destct < $num;
                    660: 	if (ref($$dest[0])) {
                    661: 	    while ($ctr < $tot) {
                    662: 		${$$dest[$ctr]} = $$source[$idx[$ctr]];
                    663: 	        $ctr++;
                    664:             }
                    665:         } else {
                    666: 	    while ($ctr < $tot) {
                    667: 		$$dest[$ctr] = $$source[$idx[$ctr]];
                    668: 		$ctr++;
                    669: 	    }
                    670: 	}
1.27      ng        671:     }
1.56      albertel  672:     &random_set_seed(@oldseed);
1.51      albertel  673:     return '';
1.27      ng        674: }
                    675: 
                    676: sub rmap {
                    677:     my ($phrase,$dest,$source)=@_;
1.51      albertel  678:     my @oldseed=&random_get_seed();
1.27      ng        679:     my @seed = &random_seed_from_phrase($phrase);
                    680:     &random_set_seed(@seed);
                    681:     my $destct = scalar(@$dest);
1.28      ng        682:     if (!$source) {
                    683: 	my @idx = &math_random_permuted_index($destct);
                    684: 	my $ctr = 0;
                    685: 	my @r_idx;
                    686: 	while ($ctr < $destct) {
                    687: 	    $r_idx[$idx[$ctr]] = $ctr;
                    688: 	    $ctr++;
                    689: 	}
                    690: 	my @output;
                    691: 	$ctr = 0;
                    692: 	while ($ctr < $destct) {
                    693: 	    $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27      ng        694: 	    $ctr++;
1.28      ng        695: 	}
1.51      albertel  696:         &random_set_seed(@oldseed);
1.28      ng        697: 	return @output;
1.27      ng        698:     } else {
1.28      ng        699: 	my $num = scalar(@$source);
                    700: 	my @idx = &math_random_permuted_index($num);
                    701: 	my $ctr = 0;
                    702: 	my $tot = $num;
                    703: 	$tot = $destct if $destct < $num;
                    704: 	my @r_idx;
1.27      ng        705: 	while ($ctr < $tot) {
1.28      ng        706: 	    $r_idx[$idx[$ctr]] = $ctr;
1.27      ng        707: 	    $ctr++;
1.28      ng        708: 	}
                    709: 	$ctr = 0;
                    710: 	if (ref($$dest[0])) {
                    711: 	    while ($ctr < $tot) {
                    712: 		${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
                    713: 	        $ctr++;
                    714:             }
                    715:         } else {
                    716: 	    while ($ctr < $tot) {
                    717: 		$$dest[$ctr] = $$source[$r_idx[$ctr]];
                    718: 		$ctr++;
                    719: 	    }
                    720: 	}
1.6       albertel  721:     }
1.51      albertel  722:     &random_set_seed(@oldseed);
                    723:     return '';
1.5       albertel  724: }
1.22      ng        725: 
1.23      ng        726: sub capa_id { return }
                    727: 
                    728: sub problem { return }
                    729: 
1.22      ng        730: sub name{
1.73      albertel  731:     my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
                    732:     $fullname = "" if $fullname eq ",  ";
                    733:     $fullname =~ s/\%2d/-/g;
                    734:     return $fullname;
1.22      ng        735: }
                    736: 
                    737: sub student_number { 
1.73      albertel  738:     my $id = &EXT('environment.id');
                    739:     $id = '' if $id eq "";
                    740:     return $id;
1.22      ng        741: }
                    742: 
                    743: sub class {
1.73      albertel  744:     my $course = &EXT('course.description');
                    745:     $course = '' if $course eq "";
                    746:     return $course;
1.22      ng        747: }
                    748: 
                    749: sub sec { 
1.73      albertel  750:     my $sec = &EXT('request.course.sec');
                    751:     $sec = '' if $sec eq "";
                    752:     return $sec;
1.22      ng        753: }
                    754: 
1.23      ng        755: sub open_date { 
1.73      albertel  756:     my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
                    757:     return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    758:     my @hm = split(/:/,$dc[3]);
                    759:     my $ampm = " am";
                    760:     if ($hm[0] > 12) {
                    761: 	$hm[0]-=12;
                    762: 	$ampm = " pm";
                    763:     }
                    764:     return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23      ng        765: }
                    766: 
                    767: sub due_date { 
1.73      albertel  768:     my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
                    769:     return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    770:     my @hm = split(/:/,$dc[3]);
                    771:     my $ampm = " am";
                    772:     if ($hm[0] > 12) {
                    773: 	$hm[0]-=12;
                    774: 	$ampm = " pm";
                    775:     }
                    776:     return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23      ng        777: }
                    778: 
                    779: sub answer_date { 
1.73      albertel  780:     my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
                    781:     return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    782:     my @hm = split(/:/,$dc[3]);
                    783:     my $ampm = " am";
                    784:     if ($hm[0] > 12) {
                    785: 	$hm[0]-=12;
                    786: 	$ampm = " pm";
                    787:     }
                    788:     return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.24      ng        789: }
                    790: 
                    791: sub array_moments {
1.73      albertel  792:     my @input=@_;
                    793:     my (@output,$N);
                    794:     $N=scalar (@input);
                    795:     $output[0]=$N;
                    796:     if ($N <= 1) {
                    797: 	$output[1]=$input[0];
                    798: 	$output[1]="Input array not defined" if ($N == 0);
                    799: 	$output[2]="variance undefined for N<=1";
                    800: 	$output[3]="skewness undefined for N<=1";
                    801: 	$output[4]="kurtosis undefined for N<=1";
                    802: 	return @output;
                    803:     }
                    804:     my $sum=0;
                    805:     foreach my $line (@input) {
                    806: 	$sum+=$line;
                    807:     }
                    808:     $output[1] = $sum/$N;
                    809:     my ($x,$sdev,$var,$skew,$kurt) = 0;
                    810:     foreach my $line (@input) {
                    811: 	$x=$line-$output[1];
                    812: 	$var+=$x**2;
                    813: 	$skew+=$x**3;
                    814: 	$kurt+=$x**4;
                    815:     }
                    816:     $output[2]=$var/($N-1);
                    817:     $sdev=CORE::sqrt($output[2]);
                    818:     if ($sdev == 0) {
                    819: 	$output[3]="inf-variance=0";
                    820: 	$output[4]="inf-variance=0";
                    821: 	return @output;
                    822:     }
                    823:     $output[3]=$skew/($sdev**3*$N);
                    824:     $output[4]=$kurt/($sdev**4*$N)-3;
1.24      ng        825:     return @output;
                    826: }
1.5       albertel  827: 
                    828: sub choose {
1.73      albertel  829:     my $num = $_[0];
                    830:     return $_[$num];
1.5       albertel  831: }
1.23      ng        832: 
1.49      albertel  833: # expiremental idea
                    834: sub proper_path {
1.73      albertel  835:     my ($path)=@_;
                    836:     if ( $external::target eq "tex" ) {
                    837: 	return '/home/httpd/html'.$path;
                    838:     } else {
                    839: 	return $path;
                    840:     }
1.49      albertel  841: }
1.23      ng        842: 

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