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

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

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