File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.57: download - view: text, annotated - select for diffs
Wed Feb 12 22:45:01 2003 UTC (21 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- explict strings \ and ' now work in string response BUG#1036

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

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