File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.50: download - view: text, annotated - select for diffs
Fri Sep 20 21:11:18 2002 UTC (21 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixes reopend BUG#485

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

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