File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.86: download - view: text, annotated - select for diffs
Wed Jun 9 21:08:21 2004 UTC (19 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
 BUG#3069

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

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