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

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

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