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

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

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