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

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.63    ! albertel    4: # $Id: default_homework.lcpm,v 1.62 2003/05/23 07:04:44 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: #
                     29: # Guy Albertelli
                     30: #
                     31: # 05/25/2001 H. K. Ng
1.24      ng         32: # 05/31/2001 H. K. Ng
1.44      matthew    33: # 12/21/2001 Matthew
1.20      harris41   34: #
1.25      albertel   35: #init some globals
1.38      albertel   36: $hidden::RANDOMINIT=0;
1.22      ng         37: $pi=atan2(1,1)*4;
                     38: $rad2deg=180.0/$pi;
                     39: $deg2rad=$pi/180.0;
1.44      matthew    40: $"=' ';
1.3       albertel   41: 
1.7       albertel   42: sub caparesponse_check {
1.36      albertel   43:   #not properly used yet: calc
                     44:   #not to be used: $ans_fmt
1.39      albertel   45:   my ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
                     46:     eval $_[1].
                     47:       ';return ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples);';
1.36      albertel   48: 
1.10      albertel   49:   my $tol_type=''; # gets it's value from whether tol has a % or not done
1.19      albertel   50:   my $sig_lbound=''; #done
                     51:   my $sig_ubound=''; #done
1.7       albertel   52:   my ($response,$expr)=@_;
1.31      albertel   53: 
1.32      albertel   54: 
1.8       albertel   55:   #type's definitons come from capaParser.h
1.31      albertel   56:   my $message='';
1.32      albertel   57:   #remove leading and trailing whitespace
                     58:   if ($response=~ /^\s|\s$/) {
                     59:     $response=~ s:^\s+|\s+$::g;
1.52      albertel   60:     $message .="Removed ws now :$response:\n";
1.32      albertel   61:   } else {
1.52      albertel   62:     $message .="no ws in :$response:\n";
1.32      albertel   63:   }
1.62      albertel   64: 
                     65:   if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
1.32      albertel   66: 
1.8       albertel   67:   if ($type eq '' ) {
1.52      albertel   68:     $message .= "Didn't find a type :$type:$expr: defaulting\n";
1.8       albertel   69:     if ( $answer eq ($answer *1.0)) { $type = 2;
                     70:     } else { $type = 3; }
                     71:   } else {
                     72:          if ($type eq 'cs')    { $type = 4;
                     73:     } elsif ($type eq 'ci')    { $type = 3;
                     74:     } elsif ($type eq 'mc')    { $type = 5;
                     75:     } elsif ($type eq 'fml')   { $type = 8;
1.9       albertel   76:     } elsif ($type eq 'subj')  { $type = 7;
1.41      albertel   77:     } elsif ($type eq 'float') { $type = 2;
                     78:     } elsif ($type eq 'int')   { $type = 1;
1.9       albertel   79:     } else { return "ERROR: Unknown type of answer: $type" }
1.8       albertel   80:   }
                     81: 
1.39      albertel   82:   my $points;
                     83:   my $id_list;
                     84:   #formula type setup the sample points
                     85:   if ($type eq '8') {
                     86:     ($id_list,$points)=split(/@/,$samples);
1.61      albertel   87:     $message.="Found :$id_list:$points: points in $samples\n";
1.39      albertel   88:   }
1.10      albertel   89:   if ($tol eq '') {
                     90:     $tol=0.0;
                     91:     $tol_type=1; #TOL_ABSOLUTE
                     92:   } else {
                     93:     if ($tol =~ /%$/) {
1.12      albertel   94:       chop $tol;
1.10      albertel   95:       $tol_type=2; #TOL_PERCENTAGE
                     96:     } else {
                     97:       $tol_type=1; #TOL_ABSOLUTE
                     98:     }
                     99:   }
1.12      albertel  100: 
                    101:   if ($sig eq '') {
                    102:     $sig_lbound = 0; #SIG_LB_DEFAULT
                    103:     $sig_ubound =15; #SIG_UB_DEFAULT
                    104:   } else {
                    105:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
1.53      albertel  106:     if (!defined($sig_lbound)) {
1.45      albertel  107:       $sig_lbound = 0; #SIG_LB_DEFAULT
                    108:       $sig_ubound =15; #SIG_UB_DEFAULT
                    109:     }
1.53      albertel  110:     if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1.12      albertel  111:   }
1.7       albertel  112:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
1.10      albertel  113: 					       $tol_type,$tol,
1.7       albertel  114: 					       $sig_lbound,$sig_ubound,
1.39      albertel  115: 					       $ans_fmt,$unit,$calc,$id_list,
                    116: 					       $points,$external::randomseed);
1.9       albertel  117: 
                    118:   if    ($result == '1') { $result='EXACT_ANS'; } 
                    119:   elsif ($result == '2') { $result='APPROX_ANS'; }
                    120:   elsif ($result == '3') { $result='SIG_FAIL'; }
                    121:   elsif ($result == '4') { $result='UNIT_FAIL'; }
                    122:   elsif ($result == '5') { $result='NO_UNIT'; }
                    123:   elsif ($result == '6') { $result='UNIT_OK'; }
                    124:   elsif ($result == '7') { $result='INCORRECT'; }
                    125:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
                    126:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
                    127:   elsif ($result =='10') { $result='SUB_RECORDED'; }
                    128:   elsif ($result =='11') { $result='BAD_FORMULA'; }
                    129:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
1.13      albertel  130:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
1.9       albertel  131: 
1.52      albertel  132:   return "$result:\nError $error:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message$expr";
1.14      albertel  133: }
                    134: 
1.37      albertel  135: sub get_array_args {
                    136:   my ($expr,$arg)=@_;
1.30      albertel  137:   # do these first, because who knows what varname the instructor might have used
                    138:   # but it probably isn't $CAPARESPONSE_CHECK_LIST_answer
1.63    ! albertel  139:   my $CAPARESPONSE_CHECK_LIST_answer = eval $expr.';return $'.$arg; #' stupid emacs
1.40      albertel  140:   my $GET_ARRAY_ARGS_result;
                    141:   my @GET_ARRAY_ARGS_list;
1.39      albertel  142:   if ($CAPARESPONSE_CHECK_LIST_answer =~ /^\s*[\$\@]/) {
1.40      albertel  143:     (@GET_ARRAY_ARGS_list) = eval $CAPARESPONSE_CHECK_LIST_answer;
1.39      albertel  144:   }
1.52      albertel  145:   $GET_ARRAY_ARGS_result.="error:$@:\n";
1.31      albertel  146:   # if the eval fails just use what is in the answer exactly
1.60      albertel  147:   if (!(@GET_ARRAY_ARGS_list) || !defined($GET_ARRAY_ARGS_list[0])) {
1.52      albertel  148:     $GET_ARRAY_ARGS_result.="list zero is undefined\n";
1.40      albertel  149:     $GET_ARRAY_ARGS_list[0]=$CAPARESPONSE_CHECK_LIST_answer;
1.31      albertel  150:   }
1.40      albertel  151:   return $GET_ARRAY_ARGS_result,@GET_ARRAY_ARGS_list;
1.37      albertel  152: }
                    153: 
                    154: sub caparesponse_check_list {
                    155:   my ($response,$expr)=@_;
1.57      albertel  156:   $expr =~ s/\\/\\\\/g;
                    157:   $expr =~ s/\'/\\\'/g;
1.40      albertel  158:   my ($result,@list) = &get_array_args($expr,'answer');
1.63    ! albertel  159:   $expr =~ s/\\\'/\'/g;
        !           160:   $expr =~ s/\\\\/\\/g;
1.15      albertel  161:   my $aresult='';
1.14      albertel  162:   my $current_answer;
1.39      albertel  163:   my $answers=join(':',@list);
1.52      albertel  164:   $result.="Got response :$answers:\n";
1.31      albertel  165:   my @responselist;
1.32      albertel  166:   my $type =eval $expr.';return $answer;';
1.31      albertel  167:   if ($type ne '' && $#list > 0) {
                    168:     (@responselist)=split /,/,$response;
                    169:   } else {
                    170:     (@responselist)=($response);
                    171:   }
1.15      albertel  172:   my $unit='';
1.52      albertel  173:   $result.="Initial final response :$responselist['-1']:\n";
1.31      albertel  174:   if ($type eq '') {
                    175:     #for numerical problems split off the unit
                    176:     if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
                    177:       $responselist['-1']=$1;
                    178:       $unit=$2;
                    179:     }
1.15      albertel  180:   }
1.52      albertel  181:   $result.="Final final response :$responselist['-1']:\n";
                    182:   $result.=":$#list: answers\n";
1.14      albertel  183:   $unit=~s/\s//;
                    184:   my $i=0;
                    185:   my $awards='';
                    186:   for ($i=0; $i<@list;$i++) {
1.52      albertel  187:     $result.="trying answer :$list[$i]:\n";
1.57      albertel  188:     my $thisanswer=$list[$i];
                    189:     $thisanswer=~ s/\\/\\\\/g;
                    190:     $thisanswer =~ s/\'/\\\'/g;
1.19      albertel  191:     if ($unit eq '') {
                    192:       $aresult=&caparesponse_check($responselist[$i],
1.57      albertel  193: 			     $expr.';my $answer=\''.$thisanswer.'\';');
1.19      albertel  194:     } else {
                    195:       $aresult=&caparesponse_check($responselist[$i]." $unit",
1.57      albertel  196: 				   $expr.';my $answer=\''.$thisanswer.'\';');
1.19      albertel  197:     }
1.15      albertel  198:     my ($temp)=split /:/, $aresult;
1.14      albertel  199:     $awards.="$temp,";
1.15      albertel  200:     $result.=$aresult;
1.14      albertel  201:   }
                    202:   chop $awards;
1.52      albertel  203:   return "$awards:\n$result";
1.7       albertel  204: }
                    205: 
1.4       albertel  206: sub tex {
                    207:   if ( $external::target eq "tex" ) {
1.58      albertel  208:     return $_[0];
1.4       albertel  209:   } else {
1.58      albertel  210:     return $_[1];
1.4       albertel  211:   }
                    212: }
                    213: 
1.24      ng        214: sub var_in_tex {
                    215:   if ( $external::target eq "tex" ) {
1.58      albertel  216:     return $_[0];
1.24      ng        217:   } else {
                    218:     return "";
                    219:   }
                    220: }
                    221: 
1.4       albertel  222: sub web {
                    223:   if ( $external::target eq "tex" ) {
1.58      albertel  224:     return $_[1];
1.4       albertel  225:   } else {
1.43      albertel  226:     if ( $external::target eq "web" || $external::target eq "answer") {
1.58      albertel  227:       return $_[2];
1.26      ng        228:     } else {
1.58      albertel  229:       return $_[0];
1.4       albertel  230:     }
                    231:   }
                    232: }
                    233: 
1.24      ng        234: sub html {
                    235:   if ( $external::target eq "web" ) {
1.26      ng        236:     return shift;
1.24      ng        237:   }
                    238: }
                    239: 
1.1       harris41  240: sub hinton {
                    241:   return 0;
                    242: }
                    243: 
                    244: sub random {
1.61      albertel  245:     my ($start,$end,$step)=@_;
                    246:     if ( ! $hidden::RANDOMINIT ) {
                    247: 	if ($external::randomseed == 0) { $external::randomseed=1; }
                    248: 	if ($external::randomseed =~/,/) {
                    249: 	    my ($num1,$num2)=split(/,/,$seed);
                    250: 	    &random_set_seed(abs($num1),abs($num2));
                    251: 	} else {
                    252: 	    &random_set_seed(1,int(abs($external::randomseed)));
                    253: 	}
                    254: 	&math_random_uniform();
                    255: 	$hidden::RANDOMINIT=1;
                    256:     }
                    257:     if (!defined($step)) { $step=1; }
                    258:     my $num=1+int(($end-$start)/$step);
                    259:     my $result=$start + int(&math_random_uniform() * $num)*$step;
                    260:     return $result;
1.1       harris41  261: }
                    262: 
1.26      ng        263: sub random_normal {
                    264:   my ($item_cnt,$seed,$av,$std_dev) = @_;
1.51      albertel  265:   my @oldseed=&random_get_seed();
1.26      ng        266:   my @retArray;
                    267:   &random_set_seed_from_phrase($seed);
                    268:   @retArray=&math_random_normal($item_cnt,$av,$std_dev);
1.51      albertel  269:   &random_set_seed(@oldseed);
1.26      ng        270:   return @retArray;
                    271: }
                    272: 
                    273: sub random_beta {
                    274:   my ($item_cnt,$seed,$aa,$bb) = @_;
1.51      albertel  275:   my @oldseed=&random_get_seed();
1.26      ng        276:   my @retArray;
                    277:   &random_set_seed_from_phrase($seed);
                    278:   @retArray=&math_random_beta($item_cnt,$aa,$bb);
1.51      albertel  279:   &random_set_seed(@oldseed);
1.26      ng        280:   return @retArray;
                    281: }
                    282: 
                    283: sub random_gamma {
                    284:   my ($item_cnt,$seed,$a,$r) = @_;
1.51      albertel  285:   my @oldseed=&random_get_seed();
1.26      ng        286:   my @retArray;
                    287:   &random_set_seed_from_phrase($seed);
                    288:   @retArray=&math_random_gamma($item_cnt,$a,$r);
1.51      albertel  289:   &random_set_seed(@oldseed);
1.26      ng        290:   return @retArray;
                    291: }
                    292: 
                    293: sub random_exponential {
                    294:   my ($item_cnt,$seed,$av) = @_;
1.51      albertel  295:   my @oldseed=&random_get_seed();
1.26      ng        296:   my @retArray;
                    297:   &random_set_seed_from_phrase($seed);
                    298:   @retArray=&math_random_exponential($item_cnt,$av);
1.51      albertel  299:   &random_set_seed(@oldseed);
1.26      ng        300:   return @retArray;
                    301: }
                    302: 
                    303: sub random_poisson {
                    304:   my ($item_cnt,$seed,$mu) = @_;
1.51      albertel  305:   my @oldseed=&random_get_seed();
1.26      ng        306:   my @retArray;
                    307:   &random_set_seed_from_phrase($seed);
                    308:   @retArray=&math_random_poisson($item_cnt,$mu);
1.51      albertel  309:   &random_set_seed(@oldseed);
1.26      ng        310:   return @retArray;
                    311: }
                    312: 
                    313: sub random_chi {
                    314:   my ($item_cnt,$seed,$df) = @_;
1.51      albertel  315:   my @oldseed=&random_get_seed();
1.26      ng        316:   my @retArray;
                    317:   &random_set_seed_from_phrase($seed);
                    318:   @retArray=&math_random_chi_square($item_cnt,$df);
1.51      albertel  319:   &random_set_seed(@oldseed);
1.26      ng        320:   return @retArray;
                    321: }
                    322: 
                    323: sub random_noncentral_chi {
                    324:   my ($item_cnt,$seed,$df,$nonc) = @_;
1.51      albertel  325:   my @oldseed=&random_get_seed();
1.26      ng        326:   my @retArray;
                    327:   &random_set_seed_from_phrase($seed);
                    328:   @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
1.51      albertel  329:   &random_set_seed(@oldseed);
1.26      ng        330:   return @retArray;
                    331: }
                    332: 
                    333: sub random_f {
                    334:   my ($item_cnt,$seed,$dfn,$dfd) = @_;
1.51      albertel  335:   my @oldseed=&random_get_seed();
1.26      ng        336:   my @retArray;
                    337:   &random_set_seed_from_phrase($seed);
                    338:   @retArray=&math_random_f($item_cnt,$dfn,$dfd);
1.51      albertel  339:   &random_set_seed(@oldseed);
1.26      ng        340:   return @retArray;
                    341: }
                    342: 
                    343: sub random_noncentral_f {
                    344:   my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
1.51      albertel  345:   my @oldseed=&random_get_seed();
1.26      ng        346:   my @retArray;
                    347:   &random_set_seed_from_phrase($seed);
                    348:   @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
1.51      albertel  349:   &random_set_seed(@oldseed);
1.26      ng        350:   return @retArray;
                    351: }
                    352: 
                    353: sub random_multivariate_normal {
1.33      ng        354:   my ($item_cnt,$seed,$mean,$covar) = @_;
1.51      albertel  355:   my @oldseed=&random_get_seed();
1.26      ng        356:   &random_set_seed_from_phrase($seed);
1.33      ng        357:   @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.51      albertel  358:   &random_set_seed(@oldseed);
1.26      ng        359:   return @retArray;
                    360: }
                    361: 
                    362: sub random_multinomial {
                    363:   my ($item_cnt,$seed,@p) = @_;
1.51      albertel  364:   my @oldseed=&random_get_seed();
1.26      ng        365:   my @retArray;
                    366:   &random_set_seed_from_phrase($seed);
                    367:   @retArray=&math_random_multinomial($item_cnt,@p);
1.51      albertel  368:   &random_set_seed(@oldseed);
1.26      ng        369:   return @retArray;
                    370: }
                    371: 
                    372: sub random_permutation {
                    373:   my ($seed,@inArray) = @_;
1.51      albertel  374:   my @oldseed=&random_get_seed();
1.26      ng        375:   my @retArray;
                    376:   &random_set_seed_from_phrase($seed);
                    377:   @retArray=&math_random_permutation(@inArray);
1.51      albertel  378:   &random_set_seed(@oldseed);
1.26      ng        379:   return @retArray;
                    380: }
                    381: 
                    382: sub random_uniform {
                    383:   my ($item_cnt,$seed,$low,$high) = @_;
1.51      albertel  384:   my @oldseed=&random_get_seed();
1.26      ng        385:   my @retArray;
                    386:   &random_set_seed_from_phrase($seed);
                    387:   @retArray=&math_random_uniform($item_cnt,$low,$high);
1.51      albertel  388:   &random_set_seed(@oldseed);
1.26      ng        389:   return @retArray;
                    390: }
                    391: 
                    392: sub random_uniform_integer {
                    393:   my ($item_cnt,$seed,$low,$high) = @_;
1.51      albertel  394:   my @oldseed=&random_get_seed();
1.26      ng        395:   my @retArray;
                    396:   &random_set_seed_from_phrase($seed);
                    397:   @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
1.51      albertel  398:   &random_set_seed(@oldseed);
1.26      ng        399:   return @retArray;
                    400: }
                    401: 
                    402: sub random_binomial {
                    403:   my ($item_cnt,$seed,$nt,$p) = @_;
1.51      albertel  404:   my @oldseed=&random_get_seed();
1.26      ng        405:   my @retArray;
                    406:   &random_set_seed_from_phrase($seed);
                    407:   @retArray=&math_random_binomial($item_cnt,$nt,$p);
1.51      albertel  408:   &random_set_seed(@oldseed);
1.26      ng        409:   return @retArray;
                    410: }
                    411: 
                    412: sub random_negative_binomial {
                    413:   my ($item_cnt,$seed,$ne,$p) = @_;
1.51      albertel  414:   my @oldseed=&random_get_seed();
1.26      ng        415:   my @retArray;
                    416:   &random_set_seed_from_phrase($seed);
                    417:   @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
1.51      albertel  418:   &random_set_seed(@oldseed);
1.26      ng        419:   return @retArray;
                    420: }
                    421: 
1.23      ng        422: sub abs { abs(shift) }
                    423: sub sin { sin(shift) }
                    424: sub cos { cos(shift) }
                    425: sub exp { exp(shift) }
                    426: sub int { int(shift) }
                    427: sub log { log(shift) }
                    428: sub atan2 { atan2($_[0],$_[1]) }
                    429: sub sqrt { sqrt(shift) }
                    430: 
1.59      albertel  431: sub tan  { CORE::sin($_[0]) / CORE::cos($_[0]) }
1.21      harris41  432: #sub atan { atan2($_[0], 1); }
                    433: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
                    434: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
1.22      ng        435: 
1.59      albertel  436: sub log10 { CORE::log($_[0])/CORE::log(10); }
1.22      ng        437: 
1.20      harris41  438: sub factorial {
1.59      albertel  439:     my $input = CORE::int(shift);
1.20      harris41  440:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
                    441:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
                    442:     return 1 if $input == 0;
                    443:     my $result = 1; 
                    444:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
                    445:     return $result;
                    446: }
                    447: 
                    448: sub sgn {
                    449:     return -1 if $_[0] < 0;
                    450:     return 0 if $_[0] == 0;
                    451:     return 1 if $_[0] > 0;
                    452: }
                    453: 
                    454: sub min {
                    455:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
                    456:     return shift @sorted;
                    457: }
                    458: 
                    459: sub max {
                    460:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
                    461:     return pop @sorted;
                    462: }
1.1       harris41  463: 
1.20      harris41  464: sub roundto {
                    465:     my ($input,$n) = @_;
                    466:     return sprintf('%.'.$n.'f',$input);
                    467: }
                    468: 
                    469: sub to_string {
                    470:     my ($input,$n) = @_;
1.26      ng        471:     return sprintf($input) if $n eq "";
                    472:     $n = '.'.$n if $n !~ /^\./;
1.20      harris41  473:     return sprintf('%'.$n,$input) if $n ne "";
                    474: }
                    475: 
                    476: sub sub_string {
                    477:     my ($str,$start,$len) = @_;
                    478:     return substr($str,$start-1,$len);
                    479: }
1.1       harris41  480: 
                    481: sub pow   {return $_[0] ** $_[1]; }
1.59      albertel  482: sub ceil  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
                    483: sub floor  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
1.27      ng        484: #sub floor {return int($_[0]); }
1.1       harris41  485: 
1.2       albertel  486: sub format {
                    487:   my ($value,$fmt)=@_;
1.48      albertel  488:   my $dollarmode;
                    489:   if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; } 
                    490:   my $result=sprintf('%.'.$fmt,$value);
1.50      albertel  491:   $result=~s/(E[+-]*)0/$1/;
1.48      albertel  492:   if ($dollarmode) {$result=&dollarmode($result);}
                    493:   return $result;
1.46      albertel  494: }
                    495: 
                    496: sub prettyprint {
                    497:   my ($value,$fmt)=@_;
                    498:   my $result;
1.48      albertel  499:   my $dollarmode;
                    500:   if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; } 
1.46      albertel  501:   if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
                    502:   if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
                    503:     my $frac=$1;
1.48      albertel  504:     if ($dollarmode) { $frac=&dollarformat($frac); }
1.46      albertel  505:     my $exponent=$2;
                    506:     $exponent=~s/^\+0*//;
1.50      albertel  507:     $exponent=~s/^-0*/-/;
1.48      albertel  508:     if ($exponent) {
                    509:       if ($external::target eq 'web') {
                    510: 	$result=$frac.'&#215;10<sup>'.$exponent.'</sup>';
                    511:       } elsif ($external::target eq 'tex') {
                    512: 	$result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
                    513:       } else {
                    514: 	$result=$value;
                    515:       }
1.46      albertel  516:     } else {
1.48      albertel  517:       $result=$frac;
1.46      albertel  518:     }
                    519:   } else {
                    520:     $result=$value;
1.48      albertel  521:     if ($dollarmode) { $result=&dollarformat($result); }
1.46      albertel  522:   }
                    523:   return $result;
1.48      albertel  524: }
                    525: 
                    526: sub dollarformat {
                    527:   my ($number) = @_;
                    528:   if ($number =~ /\./) {
                    529:     while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
                    530:       $number = $1.','.$2.$3;
                    531:     }
                    532:   } else {
                    533:     while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
                    534:       $number = $1.','.$2.$3;
                    535:     }
                    536:   }
                    537:   if ($external::target eq 'tex') {
                    538:     $number='\$'.$number; #' stupid emacs
                    539:   } else {
                    540:     $number='$'.$number; #' stupid emacs
                    541:   }
                    542:   return $number; 
1.2       albertel  543: }
1.5       albertel  544: 
                    545: sub map {
1.27      ng        546:     my ($phrase,$dest,$source)=@_;
1.51      albertel  547:     my @oldseed=&random_get_seed();
1.27      ng        548:     my @seed = &random_seed_from_phrase($phrase);
                    549:     &random_set_seed(@seed);
                    550:     my $destct = scalar(@$dest);
1.28      ng        551:     if (!$source) {
                    552: 	my @output;
                    553: 	my @idx = &math_random_permuted_index($destct);
                    554: 	my $ctr = 0;
                    555: 	while ($ctr < $destct) {
                    556: 	    $output[$ctr] = $$dest[$idx[$ctr]];
1.27      ng        557: 	    $ctr++;
1.28      ng        558: 	}
1.51      albertel  559:         &random_set_seed(@oldseed);
1.28      ng        560: 	return @output;
1.27      ng        561:     } else {
1.28      ng        562: 	my $num = scalar(@$source);
                    563: 	my @idx = &math_random_permuted_index($num);
                    564: 	my $ctr = 0;
                    565: 	my $tot = $num;
                    566: 	$tot = $destct if $destct < $num;
                    567: 	if (ref($$dest[0])) {
                    568: 	    while ($ctr < $tot) {
                    569: 		${$$dest[$ctr]} = $$source[$idx[$ctr]];
                    570: 	        $ctr++;
                    571:             }
                    572:         } else {
                    573: 	    while ($ctr < $tot) {
                    574: 		$$dest[$ctr] = $$source[$idx[$ctr]];
                    575: 		$ctr++;
                    576: 	    }
                    577: 	}
1.27      ng        578:     }
1.56      albertel  579:     &random_set_seed(@oldseed);
1.51      albertel  580:     return '';
1.27      ng        581: }
                    582: 
                    583: sub rmap {
                    584:     my ($phrase,$dest,$source)=@_;
1.51      albertel  585:     my @oldseed=&random_get_seed();
1.27      ng        586:     my @seed = &random_seed_from_phrase($phrase);
                    587:     &random_set_seed(@seed);
                    588:     my $destct = scalar(@$dest);
1.28      ng        589:     if (!$source) {
                    590: 	my @idx = &math_random_permuted_index($destct);
                    591: 	my $ctr = 0;
                    592: 	my @r_idx;
                    593: 	while ($ctr < $destct) {
                    594: 	    $r_idx[$idx[$ctr]] = $ctr;
                    595: 	    $ctr++;
                    596: 	}
                    597: 	my @output;
                    598: 	$ctr = 0;
                    599: 	while ($ctr < $destct) {
                    600: 	    $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27      ng        601: 	    $ctr++;
1.28      ng        602: 	}
1.51      albertel  603:         &random_set_seed(@oldseed);
1.28      ng        604: 	return @output;
1.27      ng        605:     } else {
1.28      ng        606: 	my $num = scalar(@$source);
                    607: 	my @idx = &math_random_permuted_index($num);
                    608: 	my $ctr = 0;
                    609: 	my $tot = $num;
                    610: 	$tot = $destct if $destct < $num;
                    611: 	my @r_idx;
1.27      ng        612: 	while ($ctr < $tot) {
1.28      ng        613: 	    $r_idx[$idx[$ctr]] = $ctr;
1.27      ng        614: 	    $ctr++;
1.28      ng        615: 	}
                    616: 	$ctr = 0;
                    617: 	if (ref($$dest[0])) {
                    618: 	    while ($ctr < $tot) {
                    619: 		${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
                    620: 	        $ctr++;
                    621:             }
                    622:         } else {
                    623: 	    while ($ctr < $tot) {
                    624: 		$$dest[$ctr] = $$source[$r_idx[$ctr]];
                    625: 		$ctr++;
                    626: 	    }
                    627: 	}
1.6       albertel  628:     }
1.51      albertel  629:     &random_set_seed(@oldseed);
                    630:     return '';
1.5       albertel  631: }
1.22      ng        632: 
1.23      ng        633: sub capa_id { return }
                    634: 
                    635: sub problem { return }
                    636: 
1.22      ng        637: sub name{
                    638:   my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
                    639:   $fullname = "" if $fullname eq ",  ";
1.26      ng        640:   $fullname =~ s/\%2d/-/g;
1.22      ng        641:   return $fullname;
                    642: }
                    643: 
                    644: sub student_number { 
                    645:   my $id = &EXT('environment.id');
                    646:   $id = '' if $id eq "";
                    647:   return $id;
                    648: }
                    649: 
                    650: sub class {
                    651:   my $course = &EXT('course.description');
                    652:   $course = '' if $course eq "";
                    653:   return $course;
                    654: }
                    655: 
                    656: sub sec { 
                    657:   my $sec = &EXT('request.course.sec');
1.23      ng        658:   $sec = '' if $sec eq "";
1.22      ng        659:   return $sec;
                    660: }
                    661: 
1.23      ng        662: sub open_date { 
                    663:   my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
1.24      ng        664:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    665:   my @hm = split(/:/,$dc[3]);
                    666:   my $ampm = " am";
                    667:   if ($hm[0] > 12) {
                    668:     $hm[0]-=12;
                    669:     $ampm = " pm";
                    670:   }
                    671:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23      ng        672: }
                    673: 
                    674: sub due_date { 
                    675:   my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
1.24      ng        676:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    677:   my @hm = split(/:/,$dc[3]);
                    678:   my $ampm = " am";
                    679:   if ($hm[0] > 12) {
                    680:     $hm[0]-=12;
                    681:     $ampm = " pm";
                    682:   }
                    683:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
                    684: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
1.23      ng        685: }
                    686: 
                    687: sub answer_date { 
                    688:   my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
1.24      ng        689:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    690:   my @hm = split(/:/,$dc[3]);
                    691:   my $ampm = " am";
                    692:   if ($hm[0] > 12) {
                    693:     $hm[0]-=12;
                    694:     $ampm = " pm";
                    695:   }
                    696:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
                    697: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
                    698: }
                    699: 
                    700: sub array_moments {
                    701:   my @input=@_;
                    702:   my (@output,$N);
                    703:   $N=scalar (@input);
                    704:   $output[0]=$N;
                    705:   if ($N <= 1) {
                    706:     $output[1]=$input[0];
1.28      ng        707:     $output[1]="Input array not defined" if ($N == 0);
1.24      ng        708:     $output[2]="variance undefined for N<=1";
                    709:     $output[3]="skewness undefined for N<=1";
                    710:     $output[4]="kurtosis undefined for N<=1";
                    711:     return @output;
                    712:   }
                    713:   my $sum=0;
                    714:   foreach my $line (@input) {
                    715:     $sum+=$line;
                    716:   }
                    717:   $output[1] = $sum/$N;
                    718:   my ($x,$sdev,$var,$skew,$kurt) = 0;
                    719:   foreach my $line (@input) {
                    720:     $x=$line-$output[1];
                    721:     $var+=$x**2;
                    722:     $skew+=$x**3;
                    723:     $kurt+=$x**4;
                    724:   }
                    725:   $output[2]=$var/($N-1);
1.59      albertel  726:   $sdev=CORE::sqrt($output[2]);
1.24      ng        727:   if ($sdev == 0) {
                    728:      $output[3]="inf-variance=0";
                    729:      $output[4]="inf-variance=0";
                    730:      return @output;
                    731:   }
                    732:   $output[3]=$skew/($sdev**3*$N);
                    733:   $output[4]=$kurt/($sdev**4*$N)-3;
                    734:   return @output;
                    735: }
1.5       albertel  736: 
                    737: sub choose {
                    738:   my $num = $_[0];
                    739:   return $_[$num];
                    740: }
1.23      ng        741: 
1.49      albertel  742: # expiremental idea
                    743: sub proper_path {
                    744:   my ($path)=@_;
                    745:   if ( $external::target eq "tex" ) {
                    746:     return '/home/httpd/html'.$path;
                    747:   } else {
                    748:     return $path;
                    749:   }
                    750: }
1.23      ng        751: 

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