File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.89: download - view: text, annotated - select for diffs
Tue Oct 5 20:30:17 2004 UTC (19 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- remove debug

    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.89 2004/10/05 20:30:17 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: 
   30: #init some globals
   31: $hidden::RANDOMINIT=0;
   32: $pi=atan2(1,1)*4;
   33: $rad2deg=180.0/$pi;
   34: $deg2rad=$pi/180.0;
   35: $"=' ';
   36: 
   37: sub caparesponse_check {
   38:     my ($answer,$response)=@_;
   39:     #not properly used yet: calc
   40:     #not to be used: $ans_fmt
   41:     my $type=$LONCAPA::CAPAresponse_args{'type'};
   42:     my $tol=$LONCAPA::CAPAresponse_args{'tol'};
   43:     my $sig=$LONCAPA::CAPAresponse_args{'sig'};
   44:     my $ans_fmt=$LONCAPA::CAPAresponse_args{'format'};
   45:     my $unit=$LONCAPA::CAPAresponse_args{'unit'};
   46:     my $calc=$LONCAPA::CAPAresponse_args{'calc'};
   47:     my $samples=$LONCAPA::CAPAresponse_args{'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: 
   53: 
   54:     #type's definitons come from capaParser.h
   55:     my $message='';
   56:     #remove leading and trailing whitespace
   57:     if (!defined($response)) {
   58: 	$response='';
   59:     }
   60:     if ($response=~ /^\s|\s$/) {
   61: 	$response=~ s:^\s+|\s+$::g;
   62: 	$message .="Removed ws now :$response:\n";
   63:     } else {
   64: 	$message .="no ws in :$response:\n";
   65:     }
   66:     if ($type eq 'cs' || $type eq 'ci' || $type eq 'mc') {
   67: 	#for string answers make surec all places spaces occur, there is 
   68:         #really only 1 space, in both the answer and the response
   69: 	$answer=~s/ +/ /g;
   70: 	$response=~s/ +/ /g;
   71:     }
   72:     if ($type eq 'float') { $response=~s/,//g; }
   73:     if ($type eq 'float' && $ans_fmt=~/\$/) {
   74: 	if ($response!~/^\$/)  { return "NO_UNIT: Missing \$ "; }
   75: 	$response=~s/\$//g;
   76:     }
   77:     $ans_fmt=~s/\W//g;
   78: 
   79:     if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
   80: 
   81:     if ($type eq '' ) {
   82: 	$message .= "Didn't find a type :$type: defaulting\n";
   83: 	if ( $answer eq ($answer *1.0)) { $type = 2;
   84: 				      } else { $type = 3; }
   85:     } else {
   86: 	if ($type eq 'cs')    { $type = 4; }
   87: 	elsif ($type eq 'ci')    { $type = 3 }
   88: 	elsif ($type eq 'mc')    { $type = 5; }
   89: 	elsif ($type eq 'fml')   { $type = 8; }
   90: 	elsif ($type eq 'subj')  { $type = 7; }
   91: 	elsif ($type eq 'float') { $type = 2; }
   92: 	elsif ($type eq 'int')   { $type = 1; }
   93: 	else { return "ERROR: Unknown type of answer: $type" }
   94:     }
   95: 
   96:     my $points;
   97:     my $id_list;
   98:     #formula type setup the sample points
   99:     if ($type eq '8') {
  100: 	($id_list,$points)=split(/@/,$samples);
  101: 	$message.="Found :$id_list:$points: points in $samples\n";
  102:     }
  103:     if ($tol eq '') {
  104: 	$tol=0.0;
  105: 	$tol_type=1; #TOL_ABSOLUTE
  106:     } else {
  107: 	if ($tol =~ /%$/) {
  108: 	    chop $tol;
  109: 	    $tol_type=2; #TOL_PERCENTAGE
  110: 	} else {
  111: 	    $tol_type=1; #TOL_ABSOLUTE
  112: 	}
  113:     }
  114: 
  115:     ($sig_ubound,$sig_lbound)=&LONCAPA_INTERNAL_get_sigrange($sig);
  116: 
  117:     my $reterror="";
  118:     my $result = &caparesponse_capa_check_answer($response,$answer,$type,
  119: 						 $tol_type,$tol,
  120: 						 $sig_lbound,$sig_ubound,
  121: 						 $ans_fmt,$unit,$calc,$id_list,
  122: 						 $points,$external::randomseed,
  123: 						 \$reterror);
  124: 
  125:     if    ($result == '1') { $result='EXACT_ANS'; } 
  126:     elsif ($result == '2') { $result='APPROX_ANS'; }
  127:     elsif ($result == '3') { $result='SIG_FAIL'; }
  128:     elsif ($result == '4') { $result='UNIT_FAIL'; }
  129:     elsif ($result == '5') { $result='NO_UNIT'; }
  130:     elsif ($result == '6') { $result='UNIT_OK'; }
  131:     elsif ($result == '7') { $result='INCORRECT'; }
  132:     elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
  133:     elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
  134:     elsif ($result =='10') { $result='SUB_RECORDED'; }
  135:     elsif ($result =='11') { $result='BAD_FORMULA'; }
  136:     elsif ($result =='13') { $result='UNIT_INVALID_INSTRUCTOR'; }
  137:     elsif ($result =='141') { $result='UNIT_INVALID_STUDENT'; }
  138:     elsif ($result =='142') { $result='UNIT_INVALID_STUDENT'; }
  139:     elsif ($result =='143') { $result='UNIT_INVALID_STUDENT'; }
  140:     elsif ($result =='15') { $result='UNIT_IRRECONCIBLE'; }
  141:     else  {$result = "ERROR: Unknown Result:$result:$@:";}
  142: 
  143:     return ("$result:\nRetError $reterror:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message",$reterror);
  144: }
  145: 
  146: 
  147: sub caparesponse_check_list {
  148:     my $response=$LONCAPA::CAPAresponse_args{'response'};
  149:     my ($result,@list);
  150:     @list=@LONCAPA::CAPAresponse_answer;
  151:     my $aresult='';
  152:     my $current_answer;
  153:     my $answers=join(':',@list);
  154:     $result.="Got response :$answers:\n";
  155:     &LONCAPA_INTERNAL_DEBUG("<blink>Yo!</blink> got ".join(':',%LONCAPA::CAPAresponse_args));
  156:     my @responselist;
  157:     my $type = $LONCAPA::CAPAresponse_args{'type'};
  158:     $result.="Got type :$type:\n";
  159:     if ($type ne '' && $#list > 0) {
  160: 	(@responselist)=split /,/,$response;
  161:     } else {
  162: 	(@responselist)=($response);
  163:     }
  164:     my $unit='';
  165:     $result.="Initial final response :$responselist['-1']:\n";
  166:     if ($type eq '' || $type eq 'float') {
  167: 	#for numerical problems split off the unit
  168: 	if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
  169: 	    $responselist['-1']=$1;
  170: 	    $unit=$2;
  171: 	}
  172:     }
  173:     $result.="Final final response :$responselist['-1']:$unit:\n";
  174:     $result.=":$#list: answers\n";
  175:     $unit=~s/\s//;
  176:     my $i=0;
  177:     my $awards='';
  178:     my @msgs;
  179:     for ($i=0; $i<@list;$i++) {
  180: 	my $msg;
  181: 	$result.="trying answer :$list[$i]:\n";
  182: 	my $thisanswer=$list[$i];
  183: 	$result.="trying answer :$thisanswer:\n";
  184: 	if ($unit eq '') {
  185: 	    ($aresult,$msg)=&caparesponse_check($thisanswer,$responselist[$i]);
  186: 	} else {
  187: 	    ($aresult,$msg)=&caparesponse_check($thisanswer,
  188: 						$responselist[$i]." $unit");
  189: 	}
  190: 	my ($temp)=split /:/, $aresult;
  191: 	$awards.="$temp,";
  192: 	$result.=$aresult;
  193: 	push(@msgs,$msg);
  194:     }
  195:     chop $awards;
  196:     return ("$awards:\n$result",@msgs);
  197: }
  198: 
  199: sub tex {
  200:     if ( $external::target eq "tex" ) {
  201: 	return $_[0];
  202:     } else {
  203: 	return $_[1];
  204:     }
  205: }
  206: 
  207: sub var_in_tex {
  208:     if ( $external::target eq "tex" ) {
  209: 	return $_[0];
  210:     } else {
  211: 	return "";
  212:     }
  213: }
  214: 
  215: sub web {
  216:     if ( $external::target eq "tex" ) {
  217: 	return $_[1];
  218:     } else {
  219: 	if ( $external::target eq "web" || $external::target eq "answer") {
  220: 	    return $_[2];
  221: 	} else {
  222: 	    return $_[0];
  223: 	}
  224:     }
  225: }
  226: 
  227: sub html {
  228:     if ( $external::target eq "web" ) {
  229: 	return shift;
  230:     }
  231: }
  232: 
  233: sub hinton {
  234:     return 0;
  235: }
  236: 
  237: sub random {
  238:     my ($start,$end,$step)=@_;
  239:     if ( ! $hidden::RANDOMINIT ) {
  240: 	if ($external::randomseed == 0) { $external::randomseed=1; }
  241: 	if ($external::randomseed =~/,/) {
  242: 	    my ($num1,$num2)=split(/,/,$external::randomseed);
  243: 	    &random_set_seed(1,abs($num1));
  244: 	} elsif ($external::randomseed =~/:/) {
  245: 	    my ($num1,$num2)=split(/:/,$external::randomseed);
  246: 	    &random_set_seed(abs($num1),abs($num2));
  247: 	} else {
  248: 	    &random_set_seed(1,int(abs($external::randomseed)));
  249: 	}
  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:     my @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:     my @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  { CORE::sin($_[0]) / CORE::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 { CORE::log($_[0])/CORE::log(10); }
  433: 
  434: sub factorial {
  435:     my $input = CORE::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]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
  479: sub floor  {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
  480: #sub floor {return int($_[0]); }
  481: 
  482: sub format {
  483:     my ($value,$fmt)=@_;
  484:     my ($dollarmode,$commamode,$alwaysperiod,$options);
  485:     if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; } 
  486:     #if ($options =~ /\$/) { $dollamode=1; }
  487:     #if ($options =~ /,/)  { $commamode=1; }
  488:     if ($options =~ /\./) { $alwaysperiod=1; }
  489:     $fmt=~s/e/E/g;
  490:     my $result=sprintf('%.'.$fmt,$value);
  491:     if ($alwaysperiod && $fmt eq '0f') { $result .='.'; }
  492:     $result=~s/(E[+-]*)0/$1/;
  493:     #if ($dollarmode) {$result=&dollarformat($result);}
  494:     #if ($commamode) {$result=&commaformat($result);}
  495:     return $result;
  496: }
  497: 
  498: sub chemparse {
  499:     my ($reaction) = @_;
  500:     my @tokens = split(/(\s\+|\->|<=>)/,$reaction);
  501:     my $formula = '';
  502:     foreach my $token (@tokens) {
  503: 	if ($token eq '->' ) {
  504: 	    $formula .= '<m>\ensuremath{\rightarrow}</m> ';
  505: 	    next;
  506: 	}
  507: 	if ($token eq '<=>') {
  508: 	    if ($external::target eq 'web' &&
  509: 		&EXT('request.browser.unicode')) {
  510: 		$formula .= '&#8652; ';
  511: 	    } else {
  512: 		$formula .= &web('<=> ','<m>\ensuremath{\rightleftharpoons}</m> ',
  513: 				 '&lt;=$gt; ');
  514: 	    }
  515: 	    next;
  516: 	}
  517: 	$token =~ /^\s*(\d*)(.*)/;
  518: 	$formula .= $1 if ($1 > 1);  # stoichiometric coefficient
  519: 	
  520: 	my $molecule = $2;
  521: 	# subscripts
  522: 	$molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
  523: 	# superscripts
  524: 	$molecule =~ s|\^(\d*[+\-]*)|<sup>$1</sup>|g;
  525: 	# strip whitespace
  526: 	$molecule =~ s/\s*//g;
  527: 	# forced space
  528: 	$molecule =~ s/_/ /g;
  529: 	$formula .= $molecule.'&nbsp;';
  530:     }
  531:     # get rid of trailing space
  532:     $formula =~ s/(\&nbsp\;| )$//;
  533:     
  534:     return &xmlparse($formula);
  535: }
  536: 
  537: sub prettyprint {
  538:     my ($value,$fmt,$target)=@_;
  539:     my $result;
  540:     if (!$target) { $target = $external::target; }
  541:     if ($fmt =~ /chem/i) { return(&chemparse($value)); }
  542:     my ($dollarmode,$commamode,$alwaysperiod,$options);
  543:     if ($fmt =~ /^([^\d]*)(.*)/) { $options=$1; $fmt=$2; } 
  544:     if ($options =~ /\$/) { $dollarmode=1; }
  545:     if ($options =~ /,/)  { $commamode=1; }
  546:     if ($options =~ /\./) { $alwaysperiod=1; }
  547:     if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
  548:     if ($alwaysperiod && $fmt eq '0f') {
  549: 	if ($target eq 'tex') {
  550: 	    $value .='\\ensuremath{.}';
  551: 	} else {
  552: 	    $value .='.';
  553: 	}
  554:     }
  555:     if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {
  556: 	my $frac=$1;
  557: 	if ($dollarmode) { $frac=&dollarformat($frac); }
  558: 	if ($commamode) { $frac=&commaformat($frac); }
  559: 	my $exponent=$2;
  560: 	$exponent=~s/^\+0*//;
  561: 	$exponent=~s/^-0*/-/;
  562: 	$exponent=~s/^-0*/-/;
  563: 	if ($exponent eq '-') { undef($exponent); }
  564: 	if ($exponent) {
  565: 	    if ($target eq 'web') {
  566: 		$result=$frac.'&#215;10<sup>'.$exponent.'</sup>';
  567: 	    } elsif ($target eq 'tex') {
  568: 		$result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
  569: 	    } else {
  570: 		$result=$value;
  571: 	    }
  572: 	} else {
  573: 	    $result=$frac;
  574: 	}
  575:     } else {
  576: 	$result=$value;
  577: 	if    ($dollarmode) { $result=&dollarformat($result,$target); }
  578: 	elsif ($commamode)  { $result=&commaformat($result,$target); }
  579:     }
  580:     return $result;
  581: }
  582: 
  583: sub commaformat {
  584:     my ($number,$target) = @_;
  585:     if ($number =~ /\./) {
  586: 	while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*)$/) {
  587: 	    $number = $1.','.$2.$3;
  588: 	}
  589:     } else {
  590: 	while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
  591: 	    $number = $1.','.$2.$3;
  592: 	}
  593:     }
  594:     return $number;
  595: }
  596: 
  597: sub dollarformat {
  598:     my ($number,$target) = @_;
  599:     if (!$target) { $target = $external::target; }
  600:     $number=&commaformat($number,$target);
  601:     if ($target eq 'tex') {
  602: 	$number='\$'.$number; #' stupid emacs
  603:     } else {
  604: 	$number='$'.$number; #' stupid emacs
  605:     }
  606:     return $number; 
  607: }
  608: 
  609: sub map {
  610:     my ($phrase,$dest,$source)=@_;
  611:     my @oldseed=&random_get_seed();
  612:     my @seed = &random_seed_from_phrase($phrase);
  613:     &random_set_seed(@seed);
  614:     my $destct = scalar(@$dest);
  615:     if (!$source) {
  616: 	my @output;
  617: 	my @idx = &math_random_permuted_index($destct);
  618: 	my $ctr = 0;
  619: 	while ($ctr < $destct) {
  620: 	    $output[$ctr] = $$dest[$idx[$ctr]];
  621: 	    $ctr++;
  622: 	}
  623:         &random_set_seed(@oldseed);
  624: 	return @output;
  625:     } else {
  626: 	my $num = scalar(@$source);
  627: 	my @idx = &math_random_permuted_index($num);
  628: 	my $ctr = 0;
  629: 	my $tot = $num;
  630: 	$tot = $destct if $destct < $num;
  631: 	if (ref($$dest[0])) {
  632: 	    while ($ctr < $tot) {
  633: 		${$$dest[$ctr]} = $$source[$idx[$ctr]];
  634: 	        $ctr++;
  635:             }
  636:         } else {
  637: 	    while ($ctr < $tot) {
  638: 		$$dest[$ctr] = $$source[$idx[$ctr]];
  639: 		$ctr++;
  640: 	    }
  641: 	}
  642:     }
  643:     &random_set_seed(@oldseed);
  644:     return '';
  645: }
  646: 
  647: sub rmap {
  648:     my ($phrase,$dest,$source)=@_;
  649:     my @oldseed=&random_get_seed();
  650:     my @seed = &random_seed_from_phrase($phrase);
  651:     &random_set_seed(@seed);
  652:     my $destct = scalar(@$dest);
  653:     if (!$source) {
  654: 	my @idx = &math_random_permuted_index($destct);
  655: 	my $ctr = 0;
  656: 	my @r_idx;
  657: 	while ($ctr < $destct) {
  658: 	    $r_idx[$idx[$ctr]] = $ctr;
  659: 	    $ctr++;
  660: 	}
  661: 	my @output;
  662: 	$ctr = 0;
  663: 	while ($ctr < $destct) {
  664: 	    $output[$ctr] = $$dest[$r_idx[$ctr]];
  665: 	    $ctr++;
  666: 	}
  667:         &random_set_seed(@oldseed);
  668: 	return @output;
  669:     } else {
  670: 	my $num = scalar(@$source);
  671: 	my @idx = &math_random_permuted_index($num);
  672: 	my $ctr = 0;
  673: 	my $tot = $num;
  674: 	$tot = $destct if $destct < $num;
  675: 	my @r_idx;
  676: 	while ($ctr < $tot) {
  677: 	    $r_idx[$idx[$ctr]] = $ctr;
  678: 	    $ctr++;
  679: 	}
  680: 	$ctr = 0;
  681: 	if (ref($$dest[0])) {
  682: 	    while ($ctr < $tot) {
  683: 		${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
  684: 	        $ctr++;
  685:             }
  686:         } else {
  687: 	    while ($ctr < $tot) {
  688: 		$$dest[$ctr] = $$source[$r_idx[$ctr]];
  689: 		$ctr++;
  690: 	    }
  691: 	}
  692:     }
  693:     &random_set_seed(@oldseed);
  694:     return '';
  695: }
  696: 
  697: sub capa_id { return }
  698: 
  699: sub problem { return }
  700: 
  701: sub name{
  702:     my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
  703:     $fullname = "" if $fullname eq ",  ";
  704:     $fullname =~ s/\%2d/-/g;
  705:     return $fullname;
  706: }
  707: 
  708: sub student_number { 
  709:     my $id = &EXT('environment.id');
  710:     $id = '' if $id eq "";
  711:     return $id;
  712: }
  713: 
  714: sub class {
  715:     my $course = &EXT('course.description');
  716:     $course = '' if $course eq "";
  717:     return $course;
  718: }
  719: 
  720: sub sec { 
  721:     my $sec = &EXT('request.course.sec');
  722:     $sec = '' if $sec eq "";
  723:     return $sec;
  724: }
  725: 
  726: sub open_date { 
  727:     my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
  728:     return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  729:     my @hm = split(/:/,$dc[3]);
  730:     my $ampm = " am";
  731:     if ($hm[0] > 12) {
  732: 	$hm[0]-=12;
  733: 	$ampm = " pm";
  734:     }
  735:     return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  736: }
  737: 
  738: sub due_date { 
  739:     my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
  740:     return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  741:     my @hm = split(/:/,$dc[3]);
  742:     my $ampm = " am";
  743:     if ($hm[0] > 12) {
  744: 	$hm[0]-=12;
  745: 	$ampm = " pm";
  746:     }
  747:     return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  748: }
  749: 
  750: sub answer_date { 
  751:     my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
  752:     return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  753:     my @hm = split(/:/,$dc[3]);
  754:     my $ampm = " am";
  755:     if ($hm[0] > 12) {
  756: 	$hm[0]-=12;
  757: 	$ampm = " pm";
  758:     }
  759:     return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  760: }
  761: 
  762: sub array_moments {
  763:     my @input=@_;
  764:     my (@output,$N);
  765:     $N=scalar (@input);
  766:     $output[0]=$N;
  767:     if ($N <= 1) {
  768: 	$output[1]=$input[0];
  769: 	$output[1]="Input array not defined" if ($N == 0);
  770: 	$output[2]="variance undefined for N<=1";
  771: 	$output[3]="skewness undefined for N<=1";
  772: 	$output[4]="kurtosis undefined for N<=1";
  773: 	return @output;
  774:     }
  775:     my $sum=0;
  776:     foreach my $line (@input) {
  777: 	$sum+=$line;
  778:     }
  779:     $output[1] = $sum/$N;
  780:     my ($x,$sdev,$var,$skew,$kurt) = 0;
  781:     foreach my $line (@input) {
  782: 	$x=$line-$output[1];
  783: 	$var+=$x**2;
  784: 	$skew+=$x**3;
  785: 	$kurt+=$x**4;
  786:     }
  787:     $output[2]=$var/($N-1);
  788:     $sdev=CORE::sqrt($output[2]);
  789:     if ($sdev == 0) {
  790: 	$output[3]="inf-variance=0";
  791: 	$output[4]="inf-variance=0";
  792: 	return @output;
  793:     }
  794:     $output[3]=$skew/($sdev**3*$N);
  795:     $output[4]=$kurt/($sdev**4*$N)-3;
  796:     return @output;
  797: }
  798: 
  799: sub choose {
  800:     my $num = $_[0];
  801:     return $_[$num];
  802: }
  803: 
  804: # expiremental idea
  805: sub proper_path {
  806:     my ($path)=@_;
  807:     if ( $external::target eq "tex" ) {
  808: 	return '/home/httpd/html'.$path;
  809:     } else {
  810: 	return $path;
  811:     }
  812: }
  813: 

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