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

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

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