File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.65: download - view: text, annotated - select for diffs
Thu Sep 18 18:54:35 2003 UTC (20 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixes reopen of BUG#1874, &prettyprint(.9999,'2E'); said 1.00x10-

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

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