File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.33: download - view: text, annotated - select for diffs
Fri Jul 27 18:15:04 2001 UTC (22 years, 9 months ago) by ng
Branches: MAIN
CVS tags: HEAD
fix call to random_multivariate function

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

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