File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.92: download - view: text, annotated - select for diffs
Thu Feb 17 21:42:37 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- adding the <chem> tag

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

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