File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.96: download - view: text, annotated - select for diffs
Tue Mar 29 17:57:51 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#4035, buy Guy Ash.
   1. Rational coefficients are now supported (e.g. Na + 1/2Cl2 -> NaCl )
   2. Hydrates are now supported (e.g. CuSO4.5H2O )

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

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