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

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.68    ! ng          4: # $Id: default_homework.lcpm,v 1.67 2003/09/25 22:57:01 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.64      albertel   45:   my ($type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
                     46:     eval $_[2].
                     47:       ';return ($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.64      albertel   52:   my ($answer,$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.64      albertel  140:   my $GET_ARRAY_ARGS_result="expr is $expr\narg is $arg\nfirst answer guess is $CAPARESPONSE_CHECK_LIST_answer\n";
1.40      albertel  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.64      albertel  156: #  $expr =~ s/\\/\\\\/g;
                    157: #  $expr =~ s/\'/\\\'/g;
                    158: #  my ($result,@list) = &get_array_args($expr,'answer');
                    159: #  $expr =~ s/\\\'/\'/g;
                    160: #  $expr =~ s/\\\\/\\/g;
                    161:   my ($result,@list);
                    162:   @list=@CAPARESPONSE_CHECK_LIST_answer;
1.15      albertel  163:   my $aresult='';
1.14      albertel  164:   my $current_answer;
1.39      albertel  165:   my $answers=join(':',@list);
1.52      albertel  166:   $result.="Got response :$answers:\n";
1.31      albertel  167:   my @responselist;
1.64      albertel  168:   my $type =eval $expr.';return $type;';
1.31      albertel  169:   if ($type ne '' && $#list > 0) {
                    170:     (@responselist)=split /,/,$response;
                    171:   } else {
                    172:     (@responselist)=($response);
                    173:   }
1.15      albertel  174:   my $unit='';
1.52      albertel  175:   $result.="Initial final response :$responselist['-1']:\n";
1.64      albertel  176:   if ($type eq '' || $type eq 'float') {
1.31      albertel  177:     #for numerical problems split off the unit
                    178:     if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
                    179:       $responselist['-1']=$1;
                    180:       $unit=$2;
                    181:     }
1.15      albertel  182:   }
1.52      albertel  183:   $result.="Final final response :$responselist['-1']:\n";
                    184:   $result.=":$#list: answers\n";
1.14      albertel  185:   $unit=~s/\s//;
                    186:   my $i=0;
                    187:   my $awards='';
                    188:   for ($i=0; $i<@list;$i++) {
1.52      albertel  189:     $result.="trying answer :$list[$i]:\n";
1.57      albertel  190:     my $thisanswer=$list[$i];
1.64      albertel  191: #    $thisanswer=~ s/\\/\\\\/g;
                    192: #    $thisanswer =~ s/\'/\\\'/g;
                    193:     $result.="trying answer :$thisanswer:\n";
1.19      albertel  194:     if ($unit eq '') {
1.64      albertel  195:       $aresult=&caparesponse_check($thisanswer,$responselist[$i],
                    196: 			     $expr);
1.19      albertel  197:     } else {
1.64      albertel  198:       $aresult=&caparesponse_check($thisanswer,$responselist[$i]." $unit",
                    199: 				   $expr);
1.19      albertel  200:     }
1.15      albertel  201:     my ($temp)=split /:/, $aresult;
1.14      albertel  202:     $awards.="$temp,";
1.15      albertel  203:     $result.=$aresult;
1.14      albertel  204:   }
                    205:   chop $awards;
1.52      albertel  206:   return "$awards:\n$result";
1.7       albertel  207: }
                    208: 
1.4       albertel  209: sub tex {
                    210:   if ( $external::target eq "tex" ) {
1.58      albertel  211:     return $_[0];
1.4       albertel  212:   } else {
1.58      albertel  213:     return $_[1];
1.4       albertel  214:   }
                    215: }
                    216: 
1.24      ng        217: sub var_in_tex {
                    218:   if ( $external::target eq "tex" ) {
1.58      albertel  219:     return $_[0];
1.24      ng        220:   } else {
                    221:     return "";
                    222:   }
                    223: }
                    224: 
1.4       albertel  225: sub web {
                    226:   if ( $external::target eq "tex" ) {
1.58      albertel  227:     return $_[1];
1.4       albertel  228:   } else {
1.43      albertel  229:     if ( $external::target eq "web" || $external::target eq "answer") {
1.58      albertel  230:       return $_[2];
1.26      ng        231:     } else {
1.58      albertel  232:       return $_[0];
1.4       albertel  233:     }
                    234:   }
                    235: }
                    236: 
1.24      ng        237: sub html {
                    238:   if ( $external::target eq "web" ) {
1.26      ng        239:     return shift;
1.24      ng        240:   }
                    241: }
                    242: 
1.1       harris41  243: sub hinton {
                    244:   return 0;
                    245: }
                    246: 
                    247: sub random {
1.61      albertel  248:     my ($start,$end,$step)=@_;
                    249:     if ( ! $hidden::RANDOMINIT ) {
                    250: 	if ($external::randomseed == 0) { $external::randomseed=1; }
                    251: 	if ($external::randomseed =~/,/) {
                    252: 	    my ($num1,$num2)=split(/,/,$seed);
                    253: 	    &random_set_seed(abs($num1),abs($num2));
                    254: 	} else {
                    255: 	    &random_set_seed(1,int(abs($external::randomseed)));
                    256: 	}
                    257: 	&math_random_uniform();
                    258: 	$hidden::RANDOMINIT=1;
                    259:     }
                    260:     if (!defined($step)) { $step=1; }
                    261:     my $num=1+int(($end-$start)/$step);
                    262:     my $result=$start + int(&math_random_uniform() * $num)*$step;
                    263:     return $result;
1.1       harris41  264: }
                    265: 
1.26      ng        266: sub random_normal {
                    267:   my ($item_cnt,$seed,$av,$std_dev) = @_;
1.51      albertel  268:   my @oldseed=&random_get_seed();
1.26      ng        269:   my @retArray;
                    270:   &random_set_seed_from_phrase($seed);
                    271:   @retArray=&math_random_normal($item_cnt,$av,$std_dev);
1.51      albertel  272:   &random_set_seed(@oldseed);
1.26      ng        273:   return @retArray;
                    274: }
                    275: 
                    276: sub random_beta {
                    277:   my ($item_cnt,$seed,$aa,$bb) = @_;
1.51      albertel  278:   my @oldseed=&random_get_seed();
1.26      ng        279:   my @retArray;
                    280:   &random_set_seed_from_phrase($seed);
                    281:   @retArray=&math_random_beta($item_cnt,$aa,$bb);
1.51      albertel  282:   &random_set_seed(@oldseed);
1.26      ng        283:   return @retArray;
                    284: }
                    285: 
                    286: sub random_gamma {
                    287:   my ($item_cnt,$seed,$a,$r) = @_;
1.51      albertel  288:   my @oldseed=&random_get_seed();
1.26      ng        289:   my @retArray;
                    290:   &random_set_seed_from_phrase($seed);
                    291:   @retArray=&math_random_gamma($item_cnt,$a,$r);
1.51      albertel  292:   &random_set_seed(@oldseed);
1.26      ng        293:   return @retArray;
                    294: }
                    295: 
                    296: sub random_exponential {
                    297:   my ($item_cnt,$seed,$av) = @_;
1.51      albertel  298:   my @oldseed=&random_get_seed();
1.26      ng        299:   my @retArray;
                    300:   &random_set_seed_from_phrase($seed);
                    301:   @retArray=&math_random_exponential($item_cnt,$av);
1.51      albertel  302:   &random_set_seed(@oldseed);
1.26      ng        303:   return @retArray;
                    304: }
                    305: 
                    306: sub random_poisson {
                    307:   my ($item_cnt,$seed,$mu) = @_;
1.51      albertel  308:   my @oldseed=&random_get_seed();
1.26      ng        309:   my @retArray;
                    310:   &random_set_seed_from_phrase($seed);
                    311:   @retArray=&math_random_poisson($item_cnt,$mu);
1.51      albertel  312:   &random_set_seed(@oldseed);
1.26      ng        313:   return @retArray;
                    314: }
                    315: 
                    316: sub random_chi {
                    317:   my ($item_cnt,$seed,$df) = @_;
1.51      albertel  318:   my @oldseed=&random_get_seed();
1.26      ng        319:   my @retArray;
                    320:   &random_set_seed_from_phrase($seed);
                    321:   @retArray=&math_random_chi_square($item_cnt,$df);
1.51      albertel  322:   &random_set_seed(@oldseed);
1.26      ng        323:   return @retArray;
                    324: }
                    325: 
                    326: sub random_noncentral_chi {
                    327:   my ($item_cnt,$seed,$df,$nonc) = @_;
1.51      albertel  328:   my @oldseed=&random_get_seed();
1.26      ng        329:   my @retArray;
                    330:   &random_set_seed_from_phrase($seed);
                    331:   @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
1.51      albertel  332:   &random_set_seed(@oldseed);
1.26      ng        333:   return @retArray;
                    334: }
                    335: 
                    336: sub random_f {
                    337:   my ($item_cnt,$seed,$dfn,$dfd) = @_;
1.51      albertel  338:   my @oldseed=&random_get_seed();
1.26      ng        339:   my @retArray;
                    340:   &random_set_seed_from_phrase($seed);
                    341:   @retArray=&math_random_f($item_cnt,$dfn,$dfd);
1.51      albertel  342:   &random_set_seed(@oldseed);
1.26      ng        343:   return @retArray;
                    344: }
                    345: 
                    346: sub random_noncentral_f {
                    347:   my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
1.51      albertel  348:   my @oldseed=&random_get_seed();
1.26      ng        349:   my @retArray;
                    350:   &random_set_seed_from_phrase($seed);
                    351:   @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
1.51      albertel  352:   &random_set_seed(@oldseed);
1.26      ng        353:   return @retArray;
                    354: }
                    355: 
                    356: sub random_multivariate_normal {
1.33      ng        357:   my ($item_cnt,$seed,$mean,$covar) = @_;
1.51      albertel  358:   my @oldseed=&random_get_seed();
1.26      ng        359:   &random_set_seed_from_phrase($seed);
1.33      ng        360:   @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
1.51      albertel  361:   &random_set_seed(@oldseed);
1.26      ng        362:   return @retArray;
                    363: }
                    364: 
                    365: sub random_multinomial {
                    366:   my ($item_cnt,$seed,@p) = @_;
1.51      albertel  367:   my @oldseed=&random_get_seed();
1.26      ng        368:   my @retArray;
                    369:   &random_set_seed_from_phrase($seed);
                    370:   @retArray=&math_random_multinomial($item_cnt,@p);
1.51      albertel  371:   &random_set_seed(@oldseed);
1.26      ng        372:   return @retArray;
                    373: }
                    374: 
                    375: sub random_permutation {
                    376:   my ($seed,@inArray) = @_;
1.51      albertel  377:   my @oldseed=&random_get_seed();
1.26      ng        378:   my @retArray;
                    379:   &random_set_seed_from_phrase($seed);
                    380:   @retArray=&math_random_permutation(@inArray);
1.51      albertel  381:   &random_set_seed(@oldseed);
1.26      ng        382:   return @retArray;
                    383: }
                    384: 
                    385: sub random_uniform {
                    386:   my ($item_cnt,$seed,$low,$high) = @_;
1.51      albertel  387:   my @oldseed=&random_get_seed();
1.26      ng        388:   my @retArray;
                    389:   &random_set_seed_from_phrase($seed);
                    390:   @retArray=&math_random_uniform($item_cnt,$low,$high);
1.51      albertel  391:   &random_set_seed(@oldseed);
1.26      ng        392:   return @retArray;
                    393: }
                    394: 
                    395: sub random_uniform_integer {
                    396:   my ($item_cnt,$seed,$low,$high) = @_;
1.51      albertel  397:   my @oldseed=&random_get_seed();
1.26      ng        398:   my @retArray;
                    399:   &random_set_seed_from_phrase($seed);
                    400:   @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
1.51      albertel  401:   &random_set_seed(@oldseed);
1.26      ng        402:   return @retArray;
                    403: }
                    404: 
                    405: sub random_binomial {
                    406:   my ($item_cnt,$seed,$nt,$p) = @_;
1.51      albertel  407:   my @oldseed=&random_get_seed();
1.26      ng        408:   my @retArray;
                    409:   &random_set_seed_from_phrase($seed);
                    410:   @retArray=&math_random_binomial($item_cnt,$nt,$p);
1.51      albertel  411:   &random_set_seed(@oldseed);
1.26      ng        412:   return @retArray;
                    413: }
                    414: 
                    415: sub random_negative_binomial {
                    416:   my ($item_cnt,$seed,$ne,$p) = @_;
1.51      albertel  417:   my @oldseed=&random_get_seed();
1.26      ng        418:   my @retArray;
                    419:   &random_set_seed_from_phrase($seed);
                    420:   @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
1.51      albertel  421:   &random_set_seed(@oldseed);
1.26      ng        422:   return @retArray;
                    423: }
                    424: 
1.23      ng        425: sub abs { abs(shift) }
                    426: sub sin { sin(shift) }
                    427: sub cos { cos(shift) }
                    428: sub exp { exp(shift) }
                    429: sub int { int(shift) }
                    430: sub log { log(shift) }
                    431: sub atan2 { atan2($_[0],$_[1]) }
                    432: sub sqrt { sqrt(shift) }
                    433: 
1.59      albertel  434: sub tan  { CORE::sin($_[0]) / CORE::cos($_[0]) }
1.21      harris41  435: #sub atan { atan2($_[0], 1); }
                    436: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
                    437: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
1.22      ng        438: 
1.59      albertel  439: sub log10 { CORE::log($_[0])/CORE::log(10); }
1.22      ng        440: 
1.20      harris41  441: sub factorial {
1.59      albertel  442:     my $input = CORE::int(shift);
1.20      harris41  443:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
                    444:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
                    445:     return 1 if $input == 0;
                    446:     my $result = 1; 
                    447:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
                    448:     return $result;
                    449: }
                    450: 
                    451: sub sgn {
                    452:     return -1 if $_[0] < 0;
                    453:     return 0 if $_[0] == 0;
                    454:     return 1 if $_[0] > 0;
                    455: }
                    456: 
                    457: sub min {
                    458:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
                    459:     return shift @sorted;
                    460: }
                    461: 
                    462: sub max {
                    463:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
                    464:     return pop @sorted;
                    465: }
1.1       harris41  466: 
1.20      harris41  467: sub roundto {
                    468:     my ($input,$n) = @_;
                    469:     return sprintf('%.'.$n.'f',$input);
                    470: }
                    471: 
                    472: sub to_string {
                    473:     my ($input,$n) = @_;
1.26      ng        474:     return sprintf($input) if $n eq "";
                    475:     $n = '.'.$n if $n !~ /^\./;
1.20      harris41  476:     return sprintf('%'.$n,$input) if $n ne "";
                    477: }
                    478: 
                    479: sub sub_string {
                    480:     my ($str,$start,$len) = @_;
                    481:     return substr($str,$start-1,$len);
                    482: }
1.1       harris41  483: 
                    484: sub pow   {return $_[0] ** $_[1]; }
1.59      albertel  485: sub ceil  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
                    486: sub floor  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
1.27      ng        487: #sub floor {return int($_[0]); }
1.1       harris41  488: 
1.2       albertel  489: sub format {
                    490:   my ($value,$fmt)=@_;
1.48      albertel  491:   my $dollarmode;
                    492:   if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; } 
1.67      albertel  493:   $fmt=~s/e/E/g;
1.48      albertel  494:   my $result=sprintf('%.'.$fmt,$value);
1.50      albertel  495:   $result=~s/(E[+-]*)0/$1/;
1.48      albertel  496:   if ($dollarmode) {$result=&dollarmode($result);}
                    497:   return $result;
1.46      albertel  498: }
                    499: 
                    500: sub prettyprint {
                    501:   my ($value,$fmt)=@_;
                    502:   my $result;
1.48      albertel  503:   my $dollarmode;
                    504:   if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; } 
1.46      albertel  505:   if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
1.68    ! ng        506:   if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
1.46      albertel  507:     my $frac=$1;
1.48      albertel  508:     if ($dollarmode) { $frac=&dollarformat($frac); }
1.46      albertel  509:     my $exponent=$2;
                    510:     $exponent=~s/^\+0*//;
1.50      albertel  511:     $exponent=~s/^-0*/-/;
1.65      albertel  512:     $exponent=~s/^-0*/-/;
                    513:     if ($exponent eq '-') { undef($exponent); }
1.48      albertel  514:     if ($exponent) {
                    515:       if ($external::target eq 'web') {
                    516: 	$result=$frac.'&#215;10<sup>'.$exponent.'</sup>';
                    517:       } elsif ($external::target eq 'tex') {
                    518: 	$result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
                    519:       } else {
                    520: 	$result=$value;
                    521:       }
1.46      albertel  522:     } else {
1.48      albertel  523:       $result=$frac;
1.46      albertel  524:     }
                    525:   } else {
                    526:     $result=$value;
1.48      albertel  527:     if ($dollarmode) { $result=&dollarformat($result); }
1.46      albertel  528:   }
                    529:   return $result;
1.48      albertel  530: }
                    531: 
                    532: sub dollarformat {
                    533:   my ($number) = @_;
                    534:   if ($number =~ /\./) {
                    535:     while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
                    536:       $number = $1.','.$2.$3;
                    537:     }
                    538:   } else {
                    539:     while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
                    540:       $number = $1.','.$2.$3;
                    541:     }
                    542:   }
                    543:   if ($external::target eq 'tex') {
                    544:     $number='\$'.$number; #' stupid emacs
                    545:   } else {
                    546:     $number='$'.$number; #' stupid emacs
                    547:   }
                    548:   return $number; 
1.2       albertel  549: }
1.5       albertel  550: 
                    551: sub map {
1.27      ng        552:     my ($phrase,$dest,$source)=@_;
1.51      albertel  553:     my @oldseed=&random_get_seed();
1.27      ng        554:     my @seed = &random_seed_from_phrase($phrase);
                    555:     &random_set_seed(@seed);
                    556:     my $destct = scalar(@$dest);
1.28      ng        557:     if (!$source) {
                    558: 	my @output;
                    559: 	my @idx = &math_random_permuted_index($destct);
                    560: 	my $ctr = 0;
                    561: 	while ($ctr < $destct) {
                    562: 	    $output[$ctr] = $$dest[$idx[$ctr]];
1.27      ng        563: 	    $ctr++;
1.28      ng        564: 	}
1.51      albertel  565:         &random_set_seed(@oldseed);
1.28      ng        566: 	return @output;
1.27      ng        567:     } else {
1.28      ng        568: 	my $num = scalar(@$source);
                    569: 	my @idx = &math_random_permuted_index($num);
                    570: 	my $ctr = 0;
                    571: 	my $tot = $num;
                    572: 	$tot = $destct if $destct < $num;
                    573: 	if (ref($$dest[0])) {
                    574: 	    while ($ctr < $tot) {
                    575: 		${$$dest[$ctr]} = $$source[$idx[$ctr]];
                    576: 	        $ctr++;
                    577:             }
                    578:         } else {
                    579: 	    while ($ctr < $tot) {
                    580: 		$$dest[$ctr] = $$source[$idx[$ctr]];
                    581: 		$ctr++;
                    582: 	    }
                    583: 	}
1.27      ng        584:     }
1.56      albertel  585:     &random_set_seed(@oldseed);
1.51      albertel  586:     return '';
1.27      ng        587: }
                    588: 
                    589: sub rmap {
                    590:     my ($phrase,$dest,$source)=@_;
1.51      albertel  591:     my @oldseed=&random_get_seed();
1.27      ng        592:     my @seed = &random_seed_from_phrase($phrase);
                    593:     &random_set_seed(@seed);
                    594:     my $destct = scalar(@$dest);
1.28      ng        595:     if (!$source) {
                    596: 	my @idx = &math_random_permuted_index($destct);
                    597: 	my $ctr = 0;
                    598: 	my @r_idx;
                    599: 	while ($ctr < $destct) {
                    600: 	    $r_idx[$idx[$ctr]] = $ctr;
                    601: 	    $ctr++;
                    602: 	}
                    603: 	my @output;
                    604: 	$ctr = 0;
                    605: 	while ($ctr < $destct) {
                    606: 	    $output[$ctr] = $$dest[$r_idx[$ctr]];
1.27      ng        607: 	    $ctr++;
1.28      ng        608: 	}
1.51      albertel  609:         &random_set_seed(@oldseed);
1.28      ng        610: 	return @output;
1.27      ng        611:     } else {
1.28      ng        612: 	my $num = scalar(@$source);
                    613: 	my @idx = &math_random_permuted_index($num);
                    614: 	my $ctr = 0;
                    615: 	my $tot = $num;
                    616: 	$tot = $destct if $destct < $num;
                    617: 	my @r_idx;
1.27      ng        618: 	while ($ctr < $tot) {
1.28      ng        619: 	    $r_idx[$idx[$ctr]] = $ctr;
1.27      ng        620: 	    $ctr++;
1.28      ng        621: 	}
                    622: 	$ctr = 0;
                    623: 	if (ref($$dest[0])) {
                    624: 	    while ($ctr < $tot) {
                    625: 		${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
                    626: 	        $ctr++;
                    627:             }
                    628:         } else {
                    629: 	    while ($ctr < $tot) {
                    630: 		$$dest[$ctr] = $$source[$r_idx[$ctr]];
                    631: 		$ctr++;
                    632: 	    }
                    633: 	}
1.6       albertel  634:     }
1.51      albertel  635:     &random_set_seed(@oldseed);
                    636:     return '';
1.5       albertel  637: }
1.22      ng        638: 
1.23      ng        639: sub capa_id { return }
                    640: 
                    641: sub problem { return }
                    642: 
1.22      ng        643: sub name{
                    644:   my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
                    645:   $fullname = "" if $fullname eq ",  ";
1.26      ng        646:   $fullname =~ s/\%2d/-/g;
1.22      ng        647:   return $fullname;
                    648: }
                    649: 
                    650: sub student_number { 
                    651:   my $id = &EXT('environment.id');
                    652:   $id = '' if $id eq "";
                    653:   return $id;
                    654: }
                    655: 
                    656: sub class {
                    657:   my $course = &EXT('course.description');
                    658:   $course = '' if $course eq "";
                    659:   return $course;
                    660: }
                    661: 
                    662: sub sec { 
                    663:   my $sec = &EXT('request.course.sec');
1.23      ng        664:   $sec = '' if $sec eq "";
1.22      ng        665:   return $sec;
                    666: }
                    667: 
1.23      ng        668: sub open_date { 
                    669:   my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
1.24      ng        670:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    671:   my @hm = split(/:/,$dc[3]);
                    672:   my $ampm = " am";
                    673:   if ($hm[0] > 12) {
                    674:     $hm[0]-=12;
                    675:     $ampm = " pm";
                    676:   }
                    677:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
1.23      ng        678: }
                    679: 
                    680: sub due_date { 
                    681:   my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
1.24      ng        682:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    683:   my @hm = split(/:/,$dc[3]);
                    684:   my $ampm = " am";
                    685:   if ($hm[0] > 12) {
                    686:     $hm[0]-=12;
                    687:     $ampm = " pm";
                    688:   }
                    689:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
                    690: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
1.23      ng        691: }
                    692: 
                    693: sub answer_date { 
                    694:   my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
1.24      ng        695:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
                    696:   my @hm = split(/:/,$dc[3]);
                    697:   my $ampm = " am";
                    698:   if ($hm[0] > 12) {
                    699:     $hm[0]-=12;
                    700:     $ampm = " pm";
                    701:   }
                    702:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
                    703: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
                    704: }
                    705: 
                    706: sub array_moments {
                    707:   my @input=@_;
                    708:   my (@output,$N);
                    709:   $N=scalar (@input);
                    710:   $output[0]=$N;
                    711:   if ($N <= 1) {
                    712:     $output[1]=$input[0];
1.28      ng        713:     $output[1]="Input array not defined" if ($N == 0);
1.24      ng        714:     $output[2]="variance undefined for N<=1";
                    715:     $output[3]="skewness undefined for N<=1";
                    716:     $output[4]="kurtosis undefined for N<=1";
                    717:     return @output;
                    718:   }
                    719:   my $sum=0;
                    720:   foreach my $line (@input) {
                    721:     $sum+=$line;
                    722:   }
                    723:   $output[1] = $sum/$N;
                    724:   my ($x,$sdev,$var,$skew,$kurt) = 0;
                    725:   foreach my $line (@input) {
                    726:     $x=$line-$output[1];
                    727:     $var+=$x**2;
                    728:     $skew+=$x**3;
                    729:     $kurt+=$x**4;
                    730:   }
                    731:   $output[2]=$var/($N-1);
1.59      albertel  732:   $sdev=CORE::sqrt($output[2]);
1.24      ng        733:   if ($sdev == 0) {
                    734:      $output[3]="inf-variance=0";
                    735:      $output[4]="inf-variance=0";
                    736:      return @output;
                    737:   }
                    738:   $output[3]=$skew/($sdev**3*$N);
                    739:   $output[4]=$kurt/($sdev**4*$N)-3;
                    740:   return @output;
                    741: }
1.5       albertel  742: 
                    743: sub choose {
                    744:   my $num = $_[0];
                    745:   return $_[$num];
                    746: }
1.23      ng        747: 
1.49      albertel  748: # expiremental idea
                    749: sub proper_path {
                    750:   my ($path)=@_;
                    751:   if ( $external::target eq "tex" ) {
                    752:     return '/home/httpd/html'.$path;
                    753:   } else {
                    754:     return $path;
                    755:   }
                    756: }
1.23      ng        757: 

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