File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.34: download - view: text, annotated - select for diffs
Wed Aug 22 21:04:58 2001 UTC (22 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- was masking some variable names

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

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