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

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

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