File:  [LON-CAPA] / loncom / homework / default_homework.lcpm
Revision 1.24: download - view: text, annotated - select for diffs
Thu May 31 20:47:50 2001 UTC (22 years, 11 months ago) by ng
Branches: MAIN
CVS tags: HEAD
add array_moments function

    1: # file name (temp): default_homework 
    2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
    3: # 
    4: #
    5: #init some globals
    6: #
    7: # Guy Albertelli
    8: #
    9: # 05/25/2001 H. K. Ng
   10: # 05/31/2001 H. K. Ng
   11: #
   12: $RANDOMINIT=0;
   13: $pi=atan2(1,1)*4;
   14: $rad2deg=180.0/$pi;
   15: $deg2rad=$pi/180.0;
   16: 
   17: sub caparesponse_check {
   18:   my $answer='';  #done
   19:   my $type='';    #done
   20:   my $tol_type=''; # gets it's value from whether tol has a % or not done
   21:   my $tol='';     #done
   22:   my $sig='';     #done lowerbnd,upperbnd
   23:   my $sig_lbound=''; #done
   24:   my $sig_ubound=''; #done
   25:   my $ans_fmt='';
   26:   my $units='';     #done
   27:   my $calc='';
   28:   my ($response,$expr)=@_;
   29:   
   30:   ($answer,$type,$tol,$sig,$ans_fmt,
   31:    $units,$calc) = eval $expr.';return $answer,$type,$tol,$sig,$ans_fmt,$units,$calc';
   32:   #type's definitons come from capaParser.h
   33:   if ($type eq '' ) {
   34:     if ( $answer eq ($answer *1.0)) { $type = 2;
   35:     } else { $type = 3; }
   36:   } else {
   37:          if ($type eq 'cs')    { $type = 4;
   38:     } elsif ($type eq 'ci')    { $type = 3;
   39:     } elsif ($type eq 'mc')    { $type = 5;
   40:     } elsif ($type eq 'fml')   { $type = 8;
   41:     } elsif ($type eq 'subj')  { $type = 7;
   42:     } else { return "ERROR: Unknown type of answer: $type" }
   43:   }
   44: 
   45:   if ($tol eq '') {
   46:     $tol=0.0;
   47:     $tol_type=1; #TOL_ABSOLUTE
   48:   } else {
   49:     if ($tol =~ /%$/) {
   50:       chop $tol;
   51:       $tol_type=2; #TOL_PERCENTAGE
   52:     } else {
   53:       $tol_type=1; #TOL_ABSOLUTE
   54:     }
   55:   }
   56: 
   57:   if ($sig eq '') {
   58:     $sig_lbound = 0; #SIG_LB_DEFAULT
   59:     $sig_ubound =15; #SIG_UB_DEFAULT
   60:   } else {
   61:     ($sig_lbound,$sig_ubound) = split /,/,$sig;
   62:   }
   63:   my $result = &caparesponse_capa_check_answer($response,$answer,$type,
   64: 					       $tol_type,$tol,
   65: 					       $sig_lbound,$sig_ubound,
   66: 					       $ans_fmt,$units,$calc);
   67: 
   68:   if    ($result == '1') { $result='EXACT_ANS'; } 
   69:   elsif ($result == '2') { $result='APPROX_ANS'; }
   70:   elsif ($result == '3') { $result='SIG_FAIL'; }
   71:   elsif ($result == '4') { $result='UNIT_FAIL'; }
   72:   elsif ($result == '5') { $result='NO_UNIT'; }
   73:   elsif ($result == '6') { $result='UNIT_OK'; }
   74:   elsif ($result == '7') { $result='INCORRECT'; }
   75:   elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
   76:   elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
   77:   elsif ($result =='10') { $result='SUB_RECORDED'; }
   78:   elsif ($result =='11') { $result='BAD_FORMULA'; }
   79:   elsif ($result =='12') { $result='WANTED_NUMERIC'; }
   80:   else  {$result = "ERROR: Unknown Result:$result:$@:";}
   81: 
   82:   return "$result:<br />Error $error:<br />Answer $answer:<br />Response $response:<br />$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$units<br />";
   83: }
   84: 
   85: sub caparesponse_check_list {
   86:   my ($response,$expr)=@_;
   87:   my $result='';
   88:   my $aresult='';
   89:   my $current_answer;
   90:   my $answer = eval $expr.';return $answer';
   91:   my (@list) = eval $answer;
   92:   my (@responselist)=split /,/,$response;
   93:   my $unit='';
   94:   $result.="Final response :$responselist['-1']:<br />";
   95:   if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
   96:     $responselist['-1']=$1;
   97:     $unit=$2;
   98:   }
   99:   $result.="Final response :$responselist['-1']:<br />";
  100:   $unit=~s/\s//;
  101:   my $i=0;
  102:   my $awards='';
  103:   for ($i=0; $i<@list;$i++) {
  104:     if ($unit eq '') {
  105:       $aresult=&caparesponse_check($responselist[$i],
  106: 			     $expr.';my $answer='.$list[$i].';');
  107:     } else {
  108:       $aresult=&caparesponse_check($responselist[$i]." $unit",
  109: 				   $expr.';my $answer='.$list[$i].';');
  110:     }
  111:     my ($temp)=split /:/, $aresult;
  112:     $awards.="$temp,";
  113:     $result.=$aresult;
  114:   }
  115:   chop $awards;
  116:   return "$awards:<br />$result";
  117: }
  118: 
  119: sub tex {
  120:   if ( $external::target eq "tex" ) {
  121:     return @_[0];
  122:   } else {
  123:     return @_[1];
  124:   }
  125: }
  126: 
  127: sub var_in_tex {
  128:   if ( $external::target eq "tex" ) {
  129:     return @_[0];
  130:   } else {
  131:     return "";
  132:   }
  133: }
  134: 
  135: sub web {
  136:   if ( $external::target eq "tex" ) {
  137:     return @_[1];
  138:   } else {
  139:     if ( $external::target eq "web") {
  140:       return @_[0];
  141:     } else {
  142:       return @_[2];
  143:     }
  144:   }
  145: }
  146: 
  147: sub html {
  148:   if ( $external::target eq "web" ) {
  149:     return @_[0];
  150:   }
  151: }
  152: 
  153: sub problem {
  154:   return '1';
  155: }
  156: 
  157: sub hinton {
  158:   return 0;
  159: }
  160: 
  161: sub random {
  162:   my ($start,$end,$step)=@_;
  163:   if ( ! $RANDOMINIT ) { srand($external::randomseed); $RANDOMINIT=1; }
  164:   my $num=1+int(($end-$start)/$step);
  165:   my $result=$start + int(rand() * $num)*$step;
  166:   return $result;
  167: }
  168: 
  169: sub abs { abs(shift) }
  170: sub sin { sin(shift) }
  171: sub cos { cos(shift) }
  172: sub exp { exp(shift) }
  173: sub int { int(shift) }
  174: sub log { log(shift) }
  175: sub atan2 { atan2($_[0],$_[1]) }
  176: sub sqrt { sqrt(shift) }
  177: 
  178: sub tan  { sin($_[0]) / cos($_[0]) }
  179: #sub atan { atan2($_[0], 1); }
  180: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
  181: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) );  }
  182: 
  183: sub log10 { log($_[0])/log(10); }
  184: 
  185: sub factorial {
  186:     my $input = int(shift);
  187:     return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
  188:     return "Error - factorial result is greater than system limit ($input)" if $input > 170;
  189:     return 1 if $input == 0;
  190:     my $result = 1; 
  191:     for (my $i=2; $i<=$input; $i++) { $result *= $i }
  192:     return $result;
  193: }
  194: 
  195: sub sgn {
  196:     return -1 if $_[0] < 0;
  197:     return 0 if $_[0] == 0;
  198:     return 1 if $_[0] > 0;
  199: }
  200: 
  201: sub min {
  202:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  203:     return shift @sorted;
  204: }
  205: 
  206: sub max {
  207:     my @sorted = sort { $a <=> $b || $a cmp $b } @_;
  208:     return pop @sorted;
  209: }
  210: 
  211: sub roundto {
  212:     my ($input,$n) = @_;
  213:     return sprintf('%.'.$n.'f',$input);
  214: }
  215: 
  216: sub to_string {
  217:     my ($input,$n) = @_;
  218:     return sprintf('%'.$n,$input) if $n ne "";
  219:     return sprintf($input) if $n eq "";
  220: }
  221: 
  222: sub sub_string {
  223:     my ($str,$start,$len) = @_;
  224:     return substr($str,$start-1,$len);
  225: }
  226: 
  227: sub pow   {return $_[0] ** $_[1]; }
  228: sub ceil  {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (int($_[0])+ 1); }
  229: sub floor {return int($_[0]);  }
  230: 
  231: sub format {
  232:   my ($value,$fmt)=@_;
  233:   return sprintf('%.'.$fmt,$value);
  234: }
  235: 
  236: sub map {
  237:   my $num = $#_/2;
  238:   my $i;
  239:   my @used;
  240:   my @permutation;
  241:   for ($i=0; $i < $num;) {
  242:     $rand = &random(1,$num,1);
  243:     if ( $used[$rand] == '0' ) {
  244:       $i++;
  245:       $used[$rand]='1';
  246:       $permutation[$i]=$rand;
  247:     }
  248:   }
  249:   for ( $i=1 ; $i<$num+1 ; $i++ ) {
  250:     ${$_[$permutation[$i]]}=$_[$i+$num]
  251:   }
  252: }
  253: 
  254: sub capa_id { return }
  255: 
  256: sub problem { return }
  257: 
  258: sub name{
  259:   my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
  260:   $fullname = "" if $fullname eq ",  ";
  261:   $fullname =~ s/\%2d/-/;
  262:   return $fullname;
  263: }
  264: 
  265: sub student_number { 
  266:   my $id = &EXT('environment.id');
  267:   $id = '' if $id eq "";
  268:   return $id;
  269: }
  270: 
  271: sub class {
  272:   my $course = &EXT('course.description');
  273:   $course = '' if $course eq "";
  274:   return $course;
  275: }
  276: 
  277: sub sec { 
  278:   my $sec = &EXT('request.course.sec');
  279:   $sec = '' if $sec eq "";
  280:   return $sec;
  281: }
  282: 
  283: sub open_date { 
  284:   my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
  285:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  286:   my @hm = split(/:/,$dc[3]);
  287:   my $ampm = " am";
  288:   if ($hm[0] > 12) {
  289:     $hm[0]-=12;
  290:     $ampm = " pm";
  291:   }
  292:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  293: }
  294: 
  295: sub due_date { 
  296:   my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
  297:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  298:   my @hm = split(/:/,$dc[3]);
  299:   my $ampm = " am";
  300:   if ($hm[0] > 12) {
  301:     $hm[0]-=12;
  302:     $ampm = " pm";
  303:   }
  304:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  305: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  306: }
  307: 
  308: sub answer_date { 
  309:   my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
  310:   return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
  311:   my @hm = split(/:/,$dc[3]);
  312:   my $ampm = " am";
  313:   if ($hm[0] > 12) {
  314:     $hm[0]-=12;
  315:     $ampm = " pm";
  316:   }
  317:   return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
  318: #  return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
  319: }
  320: 
  321: sub array_moments {
  322:   my @input=@_;
  323:   my (@output,$N);
  324:   $N=scalar (@input);
  325:   $output[0]=$N;
  326:   if ($N <= 1) {
  327:     $output[1]=$input[0];
  328:     $output[1] = "Input array not defined" if ($N == 0);
  329:     $output[2]="variance undefined for N<=1";
  330:     $output[3]="skewness undefined for N<=1";
  331:     $output[4]="kurtosis undefined for N<=1";
  332:     return @output;
  333:   }
  334:   my $sum=0;
  335:   foreach my $line (@input) {
  336:     $sum+=$line;
  337:   }
  338:   $output[1] = $sum/$N;
  339:   my ($x,$sdev,$var,$skew,$kurt) = 0;
  340:   foreach my $line (@input) {
  341:     $x=$line-$output[1];
  342:     $var+=$x**2;
  343:     $skew+=$x**3;
  344:     $kurt+=$x**4;
  345:   }
  346:   $output[2]=$var/($N-1);
  347:   $sdev=sqrt($output[2]);
  348:   if ($sdev == 0) {
  349:      $output[3]="inf-variance=0";
  350:      $output[4]="inf-variance=0";
  351:      return @output;
  352:   }
  353:   $output[3]=$skew/($sdev**3*$N);
  354:   $output[4]=$kurt/($sdev**4*$N)-3;
  355:   return @output;
  356: }
  357: 
  358: sub choose {
  359:   my $num = $_[0];
  360:   return $_[$num];
  361: }
  362: 
  363: #$external::randomseed=time;
  364: #$a=2;
  365: #$b=2;
  366: #$c=2;
  367: #&map(1,\$a,\$b,\$c,3,4,5);
  368: #print $a."\n".$b."\n";
  369: 
  370: 

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