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

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

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