File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.62: download - view: text, annotated - select for diffs
Fri May 23 07:04:44 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: version_0_99_1, version_0_99_0, conference_2003, HEAD
- Fxes Bug #941, students submitting answers over 500 characters in length mad capa engine unhappy
- documented INVALID_FILETYPE
- finished off support for ERROR return (and documented)

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

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