File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.79: download - view: text, annotated - select for diffs
Mon May 17 21:15:56 2004 UTC (19 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#3013, add a period to numbers that are 0f formatted

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

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