File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.27: download - view: text, annotated - select for diffs
Wed Jun 13 20:00:07 2001 UTC (22 years, 10 months ago) by ng
Branches: MAIN
CVS tags: HEAD
added the map and rmap functions.
correct ceil and floor functions to match CAPA.

    1: # file name (temp): default_homework
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: #
    4: # Guy Albertelli
    5: #
    6: # 05/25/2001 H. K. Ng
    7: # 05/31/2001 H. K. Ng
    8: #
    9: #init some globals
   10: $RANDOMINIT=0;
   11: $pi=atan2(1,1)*4;
   12: $rad2deg=180.0/$pi;
   13: $deg2rad=$pi/180.0;
   14: 
   15: sub caparesponse_check {
   16:   my $answer='';  #done
   17:   my $type='';    #done
   18:   my $tol_type=''; # gets it's value from whether tol has a % or not done
   19:   my $tol='';     #done
   20:   my $sig='';     #done lowerbnd,upperbnd
   21:   my $sig_lbound=''; #done
   22:   my $sig_ubound=''; #done
   23:   my $ans_fmt='';
   24:   my $units='';     #done
   25:   my $calc='';
   26:   my ($response,$expr)=@_;
   27:   
   28:   ($answer,$type,$tol,$sig,$ans_fmt,
   29:    $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
   30:   #type's definitons come from capaParser.h
   31:   if ($type eq '' ) {
   32:     if ( $answer eq ($answer *1.0)) { $type = 2;
   33:     } else { $type = 3; }
   34:   } else {
   35:          if ($type eq 'cs')    { $type = 4;
   36:     } elsif ($type eq 'ci')    { $type = 3;
   37:     } elsif ($type eq 'mc')    { $type = 5;
   38:     } elsif ($type eq 'fml')   { $type = 8;
   39:     } elsif ($type eq 'subj')  { $type = 7;
   40:     } else { return "ERROR: Unknown type of answer: $type" }
   41:   }
   42: 
   43:   if ($tol eq '') {
   44:     $tol=0.0;
   45:     $tol_type=1; #TOL_ABSOLUTE
   46:   } else {
   47:     if ($tol =~ /%$/) {
   48:       chop $tol;
   49:       $tol_type=2; #TOL_PERCENTAGE
   50:     } else {
   51:       $tol_type=1; #TOL_ABSOLUTE
   52:     }
   53:   }
   54: 
   55:   if ($sig eq '') {
   56:     $sig_lbound = 0; #SIG_LB_DEFAULT
   57:     $sig_ubound =15; #SIG_UB_DEFAULT
   58:   } else {
   59:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
   60:   }
   61:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
   62: 					       $tol_type,$tol,
   63: 					       $sig_lbound,$sig_ubound,
   64: 					       $ans_fmt,$units,$calc);
   65: 
   66:   if    ($result == '1') { $result='EXACT_ANS'; } 
   67:   elsif ($result == '2') { $result='APPROX_ANS'; }
   68:   elsif ($result == '3') { $result='SIG_FAIL'; }
   69:   elsif ($result == '4') { $result='UNIT_FAIL'; }
   70:   elsif ($result == '5') { $result='NO_UNIT'; }
   71:   elsif ($result == '6') { $result='UNIT_OK'; }
   72:   elsif ($result == '7') { $result='INCORRECT'; }
   73:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
   74:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
   75:   elsif ($result =='10') { $result='SUB_RECORDED'; }
   76:   elsif ($result =='11') { $result='BAD_FORMULA'; }
   77:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
   78:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
   79: 
   80:   return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br />$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units<br />";
   81: }
   82: 
   83: sub caparesponse_check_list {
   84:   my ($response,$expr)=@_;
   85:   my $result='';
   86:   my $aresult='';
   87:   my $current_answer;
   88:   my $answer = eval $expr.';return $answer';
   89:   my (@list) = eval $answer;
   90:   my (@responselist)=split /,/,$response;
   91:   my $unit='';
   92:   $result.="Final response :$responselist['-1']:<br />";
   93:   if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
   94:     $responselist['-1']=$1;
   95:     $unit=$2;
   96:   }
   97:   $result.="Final response :$responselist['-1']:<br />";
   98:   $unit=~s/\s//;
   99:   my $i=0;
  100:   my $awards='';
  101:   for ($i=0; $i<@list;$i++) {
  102:     if ($unit eq '') {
  103:       $aresult=&caparesponse_check($responselist[$i],
  104: 			     $expr.';my $answer='.$list[$i].';');
  105:     } else {
  106:       $aresult=&caparesponse_check($responselist[$i]." $unit",
  107: 				   $expr.';my $answer='.$list[$i].';');
  108:     }
  109:     my ($temp)=split /:/, $aresult;
  110:     $awards.="$temp,";
  111:     $result.=$aresult;
  112:   }
  113:   chop $awards;
  114:   return "$awards:<br />$result";
  115: }
  116: 
  117: sub tex {
  118:   if ( $external::target eq "tex" ) {
  119:     return @_[0];
  120:   } else {
  121:     return @_[1];
  122:   }
  123: }
  124: 
  125: sub var_in_tex {
  126:   if ( $external::target eq "tex" ) {
  127:     return @_[0];
  128:   } else {
  129:     return "";
  130:   }
  131: }
  132: 
  133: sub web {
  134:   if ( $external::target eq "tex" ) {
  135:     return @_[1];
  136:   } else {
  137:     if ( $external::target eq "web") {
  138:       return @_[2];
  139:     } else {
  140:       return @_[0];
  141:     }
  142:   }
  143: }
  144: 
  145: sub html {
  146:   if ( $external::target eq "web" ) {
  147:     return shift;
  148:   }
  149: }
  150: 
  151: sub problem {
  152:   return '1';
  153: }
  154: 
  155: sub hinton {
  156:   return 0;
  157: }
  158: 
  159: sub random {
  160:   my ($start,$end,$step)=@_;
  161:   if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
  162:   my $num=1+int(($end-$start)/$step);
  163:   my $result=$start + int(rand() * $num)*$step;
  164:   return $result;
  165: }
  166: 
  167: sub random_normal {
  168:   my ($item_cnt,$seed,$av,$std_dev) = @_;
  169:   my @retArray;
  170:   &random_set_seed_from_phrase($seed);
  171:   @retArray=&math_random_normal($item_cnt,$av,$std_dev);
  172:   return @retArray;
  173: }
  174: 
  175: sub random_beta {
  176:   my ($item_cnt,$seed,$aa,$bb) = @_;
  177:   my @retArray;
  178:   &random_set_seed_from_phrase($seed);
  179:   @retArray=&math_random_beta($item_cnt,$aa,$bb);
  180:   return @retArray;
  181: }
  182: 
  183: sub random_gamma {
  184:   my ($item_cnt,$seed,$a,$r) = @_;
  185:   my @retArray;
  186:   &random_set_seed_from_phrase($seed);
  187:   @retArray=&math_random_gamma($item_cnt,$a,$r);
  188:   return @retArray;
  189: }
  190: 
  191: sub random_exponential {
  192:   my ($item_cnt,$seed,$av) = @_;
  193:   my @retArray;
  194:   &random_set_seed_from_phrase($seed);
  195:   @retArray=&math_random_exponential($item_cnt,$av);
  196:   return @retArray;
  197: }
  198: 
  199: sub random_poisson {
  200:   my ($item_cnt,$seed,$mu) = @_;
  201:   my @retArray;
  202:   &random_set_seed_from_phrase($seed);
  203:   @retArray=&math_random_poisson($item_cnt,$mu);
  204:   return @retArray;
  205: }
  206: 
  207: sub random_chi {
  208:   my ($item_cnt,$seed,$df) = @_;
  209:   my @retArray;
  210:   &random_set_seed_from_phrase($seed);
  211:   @retArray=&math_random_chi_square($item_cnt,$df);
  212:   return @retArray;
  213: }
  214: 
  215: sub random_noncentral_chi {
  216:   my ($item_cnt,$seed,$df,$nonc) = @_;
  217:   my @retArray;
  218:   &random_set_seed_from_phrase($seed);
  219:   @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
  220:   return @retArray;
  221: }
  222: 
  223: sub random_f {
  224:   my ($item_cnt,$seed,$dfn,$dfd) = @_;
  225:   my @retArray;
  226:   &random_set_seed_from_phrase($seed);
  227:   @retArray=&math_random_f($item_cnt,$dfn,$dfd);
  228:   return @retArray;
  229: }
  230: 
  231: sub random_noncentral_f {
  232:   my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
  233:   my @retArray;
  234:   &random_set_seed_from_phrase($seed);
  235:   @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
  236:   return @retArray;
  237: }
  238: 
  239: sub random_multivariate_normal {
  240:   my ($item_cnt,$seed,@mean) = @_;
  241:   return "Number of deviates must be greater than 0" if $item_cnt <= 0;
  242:   my (@covar,@retArray);
  243:   my $ind = 0;
  244:   while ($ind<$item_cnt) {
  245:      push @covar, pop (@mean);
  246:      $ind++;
  247:   }
  248:   &random_set_seed_from_phrase($seed);
  249:   @retArray=&math_random_multivariate_normal($item_cnt,@mean,@covar);
  250:   return @retArray;
  251: }
  252: 
  253: sub random_multinomial {
  254:   my ($item_cnt,$seed,@p) = @_;
  255:   my @retArray;
  256:   &random_set_seed_from_phrase($seed);
  257:   @retArray=&math_random_multinomial($item_cnt,@p);
  258:   return @retArray;
  259: }
  260: 
  261: sub random_permutation {
  262:   my ($seed,@inArray) = @_;
  263:   my @retArray;
  264:   &random_set_seed_from_phrase($seed);
  265:   @retArray=&math_random_permutation(@inArray);
  266:   return @retArray;
  267: }
  268: 
  269: sub random_uniform {
  270:   my ($item_cnt,$seed,$low,$high) = @_;
  271:   my @retArray;
  272:   &random_set_seed_from_phrase($seed);
  273:   @retArray=&math_random_uniform($item_cnt,$low,$high);
  274:   return @retArray;
  275: }
  276: 
  277: sub random_uniform_integer {
  278:   my ($item_cnt,$seed,$low,$high) = @_;
  279:   my @retArray;
  280:   &random_set_seed_from_phrase($seed);
  281:   @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
  282:   return @retArray;
  283: }
  284: 
  285: sub random_binomial {
  286:   my ($item_cnt,$seed,$nt,$p) = @_;
  287:   my @retArray;
  288:   &random_set_seed_from_phrase($seed);
  289:   @retArray=&math_random_binomial($item_cnt,$nt,$p);
  290:   return @retArray;
  291: }
  292: 
  293: sub random_negative_binomial {
  294:   my ($item_cnt,$seed,$ne,$p) = @_;
  295:   my @retArray;
  296:   &random_set_seed_from_phrase($seed);
  297:   @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
  298:   return @retArray;
  299: }
  300: 
  301: sub abs { abs(shift) }
  302: sub sin { sin(shift) }
  303: sub cos { cos(shift) }
  304: sub exp { exp(shift) }
  305: sub int { int(shift) }
  306: sub log { log(shift) }
  307: sub atan2 { atan2($_[0],$_[1]) }
  308: sub sqrt { sqrt(shift) }
  309: 
  310: sub tan  { sin($_[0]) / cos($_[0]) }
  311: #sub atan { atan2($_[0], 1); }
  312: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  313: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  314: 
  315: sub log10 { log($_[0])/log(10); }
  316: 
  317: sub factorial {
  318:     my $input = int(shift);
  319:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
  320:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
  321:     return 1 if $input == 0;
  322:     my $result = 1; 
  323:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
  324:     return $result;
  325: }
  326: 
  327: sub sgn {
  328:     return -1 if $_[0] < 0;
  329:     return 0 if $_[0] == 0;
  330:     return 1 if $_[0] > 0;
  331: }
  332: 
  333: sub min {
  334:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  335:     return shift @sorted;
  336: }
  337: 
  338: sub max {
  339:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  340:     return pop @sorted;
  341: }
  342: 
  343: sub roundto {
  344:     my ($input,$n) = @_;
  345:     return sprintf('%.'.$n.'f',$input);
  346: }
  347: 
  348: sub to_string {
  349:     my ($input,$n) = @_;
  350:     return sprintf($input) if $n eq "";
  351:     $n = '.'.$n if $n !~ /^\./;
  352:     return sprintf('%'.$n,$input) if $n ne "";
  353: }
  354: 
  355: sub sub_string {
  356:     my ($str,$start,$len) = @_;
  357:     return substr($str,$start-1,$len);
  358: }
  359: 
  360: sub pow   {return $_[0] ** $_[1]; }
  361: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (int($_[0])+ 1) : int($_[0])); }
  362: sub floor  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? int($_[0]) : (int($_[0])-1)); }
  363: #sub floor {return int($_[0]); }
  364: 
  365: sub format {
  366:   my ($value,$fmt)=@_;
  367:   return sprintf('%.'.$fmt,$value);
  368: }
  369: 
  370: sub map {
  371:     my ($phrase,$dest,$source)=@_;
  372:     my @seed = &random_seed_from_phrase($phrase);
  373:     &random_set_seed(@seed);
  374:     my $num = scalar(@$source);
  375:     my $destct = scalar(@$dest);
  376: #    print "Warning - Number of source elements and destination elements does not match.\n" if $destct != $num;
  377:     my @idx = &math_random_permuted_index($num);
  378:     my $ctr = 0;
  379:     my $tot = $num;
  380:     $tot = $destct if $destct < $num;
  381:     if (ref($$dest[0])) {
  382: 	while ($ctr < $tot) {
  383: 	    ${$$dest[$ctr]} = $$source[$idx[$ctr]];
  384: 	    $ctr++;
  385:         }
  386:     } else {
  387: 	while ($ctr < $tot) {
  388: 	    $$dest[$ctr] = $$source[$idx[$ctr]];
  389: 	    $ctr++;
  390:         }
  391:     }
  392: }
  393: 
  394: sub rmap {
  395:     my ($phrase,$dest,$source)=@_;
  396:     my @seed = &random_seed_from_phrase($phrase);
  397:     &random_set_seed(@seed);
  398:     my $num = scalar(@$source);
  399:     my $destct = scalar(@$dest);
  400: #    print "Warning - Number of source elements and destination elements does not match.\n" if $destct != $num;
  401:     my @idx = &math_random_permuted_index($num);
  402:     my $ctr = 0;
  403:     my $tot = $num;
  404:     $tot = $destct if $destct < $num;
  405:     my @r_idx;
  406:     while ($ctr < $tot) {
  407: 	$r_idx[$idx[$ctr]] = $ctr;
  408: 	$ctr++;
  409:     }
  410:     $ctr = 0;
  411:     if (ref($$dest[0])) {
  412: 	while ($ctr < $tot) {
  413: 	    ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
  414: 	    $ctr++;
  415:         }
  416:     } else {
  417: 	while ($ctr < $tot) {
  418: 	    $$dest[$ctr] = $$source[$r_idx[$ctr]];
  419: 	    $ctr++;
  420:         }
  421:     }
  422: }
  423: 
  424: sub capa_id { return }
  425: 
  426: sub problem { return }
  427: 
  428: sub name{
  429:   my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
  430:   $fullname = "" if $fullname eq ",  ";
  431:   $fullname =~ s/\%2d/-/g;
  432:   return $fullname;
  433: }
  434: 
  435: sub student_number { 
  436:   my $id = &EXT('environment.id');
  437:   $id = '' if $id eq "";
  438:   return $id;
  439: }
  440: 
  441: sub class {
  442:   my $course = &EXT('course.description');
  443:   $course = '' if $course eq "";
  444:   return $course;
  445: }
  446: 
  447: sub sec { 
  448:   my $sec = &EXT('request.course.sec');
  449:   $sec = '' if $sec eq "";
  450:   return $sec;
  451: }
  452: 
  453: sub open_date { 
  454:   my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
  455:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  456:   my @hm = split(/:/,$dc[3]);
  457:   my $ampm = " am";
  458:   if ($hm[0] > 12) {
  459:     $hm[0]-=12;
  460:     $ampm = " pm";
  461:   }
  462:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  463: }
  464: 
  465: sub due_date { 
  466:   my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
  467:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  468:   my @hm = split(/:/,$dc[3]);
  469:   my $ampm = " am";
  470:   if ($hm[0] > 12) {
  471:     $hm[0]-=12;
  472:     $ampm = " pm";
  473:   }
  474:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  475: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  476: }
  477: 
  478: sub answer_date { 
  479:   my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
  480:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  481:   my @hm = split(/:/,$dc[3]);
  482:   my $ampm = " am";
  483:   if ($hm[0] > 12) {
  484:     $hm[0]-=12;
  485:     $ampm = " pm";
  486:   }
  487:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  488: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  489: }
  490: 
  491: sub array_moments {
  492:   my @input=@_;
  493:   my (@output,$N);
  494:   $N=scalar (@input);
  495:   $output[0]=$N;
  496:   if ($N <= 1) {
  497:     $output[1]=$input[0];
  498:     $output[1] = "Input array not defined" if ($N == 0);
  499:     $output[2]="variance undefined for N<=1";
  500:     $output[3]="skewness undefined for N<=1";
  501:     $output[4]="kurtosis undefined for N<=1";
  502:     return @output;
  503:   }
  504:   my $sum=0;
  505:   foreach my $line (@input) {
  506:     $sum+=$line;
  507:   }
  508:   $output[1] = $sum/$N;
  509:   my ($x,$sdev,$var,$skew,$kurt) = 0;
  510:   foreach my $line (@input) {
  511:     $x=$line-$output[1];
  512:     $var+=$x**2;
  513:     $skew+=$x**3;
  514:     $kurt+=$x**4;
  515:   }
  516:   $output[2]=$var/($N-1);
  517:   $sdev=sqrt($output[2]);
  518:   if ($sdev == 0) {
  519:      $output[3]="inf-variance=0";
  520:      $output[4]="inf-variance=0";
  521:      return @output;
  522:   }
  523:   $output[3]=$skew/($sdev**3*$N);
  524:   $output[4]=$kurt/($sdev**4*$N)-3;
  525:   return @output;
  526: }
  527: 
  528: sub choose {
  529:   my $num = $_[0];
  530:   return $_[$num];
  531: }
  532: 
  533: 

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