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

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

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