File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.70: download - view: text, annotated - select for diffs
Fri Mar 12 16:55:35 2004 UTC (20 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#2820, namescape the internal variables we use

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

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