Annotation of loncom/homework/default_homework.lcpm, revision 1.76

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

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