1: # The LearningOnline Network with CAPA
2: # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
3: #
4: # $Id: default_homework.lcpm,v 1.63 2003/06/07 03:52:39 albertel Exp $
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: #
28: #
29: # Guy Albertelli
30: #
31: # 05/25/2001 H. K. Ng
32: # 05/31/2001 H. K. Ng
33: # 12/21/2001 Matthew
34: #
35: #init some globals
36: $hidden::RANDOMINIT=0;
37: $pi=atan2(1,1)*4;
38: $rad2deg=180.0/$pi;
39: $deg2rad=$pi/180.0;
40: $"=' ';
41:
42: sub caparesponse_check {
43: #not properly used yet: calc
44: #not to be used: $ans_fmt
45: my ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples) =
46: eval $_[1].
47: ';return ($answer,$type,$tol,$sig,$ans_fmt,$unit,$calc,$samples);';
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: my ($response,$expr)=@_;
53:
54:
55: #type's definitons come from capaParser.h
56: my $message='';
57: #remove leading and trailing whitespace
58: if ($response=~ /^\s|\s$/) {
59: $response=~ s:^\s+|\s+$::g;
60: $message .="Removed ws now :$response:\n";
61: } else {
62: $message .="no ws in :$response:\n";
63: }
64:
65: if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
66:
67: if ($type eq '' ) {
68: $message .= "Didn't find a type :$type:$expr: defaulting\n";
69: if ( $answer eq ($answer *1.0)) { $type = 2;
70: } else { $type = 3; }
71: } else {
72: if ($type eq 'cs') { $type = 4;
73: } elsif ($type eq 'ci') { $type = 3;
74: } elsif ($type eq 'mc') { $type = 5;
75: } elsif ($type eq 'fml') { $type = 8;
76: } elsif ($type eq 'subj') { $type = 7;
77: } elsif ($type eq 'float') { $type = 2;
78: } elsif ($type eq 'int') { $type = 1;
79: } else { return "ERROR: Unknown type of answer: $type" }
80: }
81:
82: my $points;
83: my $id_list;
84: #formula type setup the sample points
85: if ($type eq '8') {
86: ($id_list,$points)=split(/@/,$samples);
87: $message.="Found :$id_list:$points: points in $samples\n";
88: }
89: if ($tol eq '') {
90: $tol=0.0;
91: $tol_type=1; #TOL_ABSOLUTE
92: } else {
93: if ($tol =~ /%$/) {
94: chop $tol;
95: $tol_type=2; #TOL_PERCENTAGE
96: } else {
97: $tol_type=1; #TOL_ABSOLUTE
98: }
99: }
100:
101: if ($sig eq '') {
102: $sig_lbound = 0; #SIG_LB_DEFAULT
103: $sig_ubound =15; #SIG_UB_DEFAULT
104: } else {
105: ($sig_lbound,$sig_ubound) = split /,/,$sig;
106: if (!defined($sig_lbound)) {
107: $sig_lbound = 0; #SIG_LB_DEFAULT
108: $sig_ubound =15; #SIG_UB_DEFAULT
109: }
110: if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
111: }
112: my $result = &caparesponse_capa_check_answer($response,$answer,$type,
113: $tol_type,$tol,
114: $sig_lbound,$sig_ubound,
115: $ans_fmt,$unit,$calc,$id_list,
116: $points,$external::randomseed);
117:
118: if ($result == '1') { $result='EXACT_ANS'; }
119: elsif ($result == '2') { $result='APPROX_ANS'; }
120: elsif ($result == '3') { $result='SIG_FAIL'; }
121: elsif ($result == '4') { $result='UNIT_FAIL'; }
122: elsif ($result == '5') { $result='NO_UNIT'; }
123: elsif ($result == '6') { $result='UNIT_OK'; }
124: elsif ($result == '7') { $result='INCORRECT'; }
125: elsif ($result == '8') { $result='UNIT_NOTNEEDED'; }
126: elsif ($result == '9') { $result='ANS_CNT_NOT_MATCH'; }
127: elsif ($result =='10') { $result='SUB_RECORDED'; }
128: elsif ($result =='11') { $result='BAD_FORMULA'; }
129: elsif ($result =='12') { $result='WANTED_NUMERIC'; }
130: else {$result = "ERROR: Unknown Result:$result:$@:";}
131:
132: return "$result:\nError $error:\nAnswer $answer:\nResponse $response:\n type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|\n$message$expr";
133: }
134:
135: sub get_array_args {
136: my ($expr,$arg)=@_;
137: # do these first, because who knows what varname the instructor might have used
138: # but it probably isn't $CAPARESPONSE_CHECK_LIST_answer
139: my $CAPARESPONSE_CHECK_LIST_answer = eval $expr.';return $'.$arg; #' stupid emacs
140: my $GET_ARRAY_ARGS_result;
141: my @GET_ARRAY_ARGS_list;
142: if ($CAPARESPONSE_CHECK_LIST_answer =~ /^\s*[\$\@]/) {
143: (@GET_ARRAY_ARGS_list) = eval $CAPARESPONSE_CHECK_LIST_answer;
144: }
145: $GET_ARRAY_ARGS_result.="error:$@:\n";
146: # if the eval fails just use what is in the answer exactly
147: if (!(@GET_ARRAY_ARGS_list) || !defined($GET_ARRAY_ARGS_list[0])) {
148: $GET_ARRAY_ARGS_result.="list zero is undefined\n";
149: $GET_ARRAY_ARGS_list[0]=$CAPARESPONSE_CHECK_LIST_answer;
150: }
151: return $GET_ARRAY_ARGS_result,@GET_ARRAY_ARGS_list;
152: }
153:
154: sub caparesponse_check_list {
155: my ($response,$expr)=@_;
156: $expr =~ s/\\/\\\\/g;
157: $expr =~ s/\'/\\\'/g;
158: my ($result,@list) = &get_array_args($expr,'answer');
159: $expr =~ s/\\\'/\'/g;
160: $expr =~ s/\\\\/\\/g;
161: my $aresult='';
162: my $current_answer;
163: my $answers=join(':',@list);
164: $result.="Got response :$answers:\n";
165: my @responselist;
166: my $type =eval $expr.';return $answer;';
167: if ($type ne '' && $#list > 0) {
168: (@responselist)=split /,/,$response;
169: } else {
170: (@responselist)=($response);
171: }
172: my $unit='';
173: $result.="Initial final response :$responselist['-1']:\n";
174: if ($type eq '') {
175: #for numerical problems split off the unit
176: if ( $responselist['-1']=~ /(.*[^\s])\s+([^\s]+)/ ) {
177: $responselist['-1']=$1;
178: $unit=$2;
179: }
180: }
181: $result.="Final final response :$responselist['-1']:\n";
182: $result.=":$#list: answers\n";
183: $unit=~s/\s//;
184: my $i=0;
185: my $awards='';
186: for ($i=0; $i<@list;$i++) {
187: $result.="trying answer :$list[$i]:\n";
188: my $thisanswer=$list[$i];
189: $thisanswer=~ s/\\/\\\\/g;
190: $thisanswer =~ s/\'/\\\'/g;
191: if ($unit eq '') {
192: $aresult=&caparesponse_check($responselist[$i],
193: $expr.';my $answer=\''.$thisanswer.'\';');
194: } else {
195: $aresult=&caparesponse_check($responselist[$i]." $unit",
196: $expr.';my $answer=\''.$thisanswer.'\';');
197: }
198: my ($temp)=split /:/, $aresult;
199: $awards.="$temp,";
200: $result.=$aresult;
201: }
202: chop $awards;
203: return "$awards:\n$result";
204: }
205:
206: sub tex {
207: if ( $external::target eq "tex" ) {
208: return $_[0];
209: } else {
210: return $_[1];
211: }
212: }
213:
214: sub var_in_tex {
215: if ( $external::target eq "tex" ) {
216: return $_[0];
217: } else {
218: return "";
219: }
220: }
221:
222: sub web {
223: if ( $external::target eq "tex" ) {
224: return $_[1];
225: } else {
226: if ( $external::target eq "web" || $external::target eq "answer") {
227: return $_[2];
228: } else {
229: return $_[0];
230: }
231: }
232: }
233:
234: sub html {
235: if ( $external::target eq "web" ) {
236: return shift;
237: }
238: }
239:
240: sub hinton {
241: return 0;
242: }
243:
244: sub random {
245: my ($start,$end,$step)=@_;
246: if ( ! $hidden::RANDOMINIT ) {
247: if ($external::randomseed == 0) { $external::randomseed=1; }
248: if ($external::randomseed =~/,/) {
249: my ($num1,$num2)=split(/,/,$seed);
250: &random_set_seed(abs($num1),abs($num2));
251: } else {
252: &random_set_seed(1,int(abs($external::randomseed)));
253: }
254: &math_random_uniform();
255: $hidden::RANDOMINIT=1;
256: }
257: if (!defined($step)) { $step=1; }
258: my $num=1+int(($end-$start)/$step);
259: my $result=$start + int(&math_random_uniform() * $num)*$step;
260: return $result;
261: }
262:
263: sub random_normal {
264: my ($item_cnt,$seed,$av,$std_dev) = @_;
265: my @oldseed=&random_get_seed();
266: my @retArray;
267: &random_set_seed_from_phrase($seed);
268: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
269: &random_set_seed(@oldseed);
270: return @retArray;
271: }
272:
273: sub random_beta {
274: my ($item_cnt,$seed,$aa,$bb) = @_;
275: my @oldseed=&random_get_seed();
276: my @retArray;
277: &random_set_seed_from_phrase($seed);
278: @retArray=&math_random_beta($item_cnt,$aa,$bb);
279: &random_set_seed(@oldseed);
280: return @retArray;
281: }
282:
283: sub random_gamma {
284: my ($item_cnt,$seed,$a,$r) = @_;
285: my @oldseed=&random_get_seed();
286: my @retArray;
287: &random_set_seed_from_phrase($seed);
288: @retArray=&math_random_gamma($item_cnt,$a,$r);
289: &random_set_seed(@oldseed);
290: return @retArray;
291: }
292:
293: sub random_exponential {
294: my ($item_cnt,$seed,$av) = @_;
295: my @oldseed=&random_get_seed();
296: my @retArray;
297: &random_set_seed_from_phrase($seed);
298: @retArray=&math_random_exponential($item_cnt,$av);
299: &random_set_seed(@oldseed);
300: return @retArray;
301: }
302:
303: sub random_poisson {
304: my ($item_cnt,$seed,$mu) = @_;
305: my @oldseed=&random_get_seed();
306: my @retArray;
307: &random_set_seed_from_phrase($seed);
308: @retArray=&math_random_poisson($item_cnt,$mu);
309: &random_set_seed(@oldseed);
310: return @retArray;
311: }
312:
313: sub random_chi {
314: my ($item_cnt,$seed,$df) = @_;
315: my @oldseed=&random_get_seed();
316: my @retArray;
317: &random_set_seed_from_phrase($seed);
318: @retArray=&math_random_chi_square($item_cnt,$df);
319: &random_set_seed(@oldseed);
320: return @retArray;
321: }
322:
323: sub random_noncentral_chi {
324: my ($item_cnt,$seed,$df,$nonc) = @_;
325: my @oldseed=&random_get_seed();
326: my @retArray;
327: &random_set_seed_from_phrase($seed);
328: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
329: &random_set_seed(@oldseed);
330: return @retArray;
331: }
332:
333: sub random_f {
334: my ($item_cnt,$seed,$dfn,$dfd) = @_;
335: my @oldseed=&random_get_seed();
336: my @retArray;
337: &random_set_seed_from_phrase($seed);
338: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
339: &random_set_seed(@oldseed);
340: return @retArray;
341: }
342:
343: sub random_noncentral_f {
344: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
345: my @oldseed=&random_get_seed();
346: my @retArray;
347: &random_set_seed_from_phrase($seed);
348: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
349: &random_set_seed(@oldseed);
350: return @retArray;
351: }
352:
353: sub random_multivariate_normal {
354: my ($item_cnt,$seed,$mean,$covar) = @_;
355: my @oldseed=&random_get_seed();
356: &random_set_seed_from_phrase($seed);
357: @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
358: &random_set_seed(@oldseed);
359: return @retArray;
360: }
361:
362: sub random_multinomial {
363: my ($item_cnt,$seed,@p) = @_;
364: my @oldseed=&random_get_seed();
365: my @retArray;
366: &random_set_seed_from_phrase($seed);
367: @retArray=&math_random_multinomial($item_cnt,@p);
368: &random_set_seed(@oldseed);
369: return @retArray;
370: }
371:
372: sub random_permutation {
373: my ($seed,@inArray) = @_;
374: my @oldseed=&random_get_seed();
375: my @retArray;
376: &random_set_seed_from_phrase($seed);
377: @retArray=&math_random_permutation(@inArray);
378: &random_set_seed(@oldseed);
379: return @retArray;
380: }
381:
382: sub random_uniform {
383: my ($item_cnt,$seed,$low,$high) = @_;
384: my @oldseed=&random_get_seed();
385: my @retArray;
386: &random_set_seed_from_phrase($seed);
387: @retArray=&math_random_uniform($item_cnt,$low,$high);
388: &random_set_seed(@oldseed);
389: return @retArray;
390: }
391:
392: sub random_uniform_integer {
393: my ($item_cnt,$seed,$low,$high) = @_;
394: my @oldseed=&random_get_seed();
395: my @retArray;
396: &random_set_seed_from_phrase($seed);
397: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
398: &random_set_seed(@oldseed);
399: return @retArray;
400: }
401:
402: sub random_binomial {
403: my ($item_cnt,$seed,$nt,$p) = @_;
404: my @oldseed=&random_get_seed();
405: my @retArray;
406: &random_set_seed_from_phrase($seed);
407: @retArray=&math_random_binomial($item_cnt,$nt,$p);
408: &random_set_seed(@oldseed);
409: return @retArray;
410: }
411:
412: sub random_negative_binomial {
413: my ($item_cnt,$seed,$ne,$p) = @_;
414: my @oldseed=&random_get_seed();
415: my @retArray;
416: &random_set_seed_from_phrase($seed);
417: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
418: &random_set_seed(@oldseed);
419: return @retArray;
420: }
421:
422: sub abs { abs(shift) }
423: sub sin { sin(shift) }
424: sub cos { cos(shift) }
425: sub exp { exp(shift) }
426: sub int { int(shift) }
427: sub log { log(shift) }
428: sub atan2 { atan2($_[0],$_[1]) }
429: sub sqrt { sqrt(shift) }
430:
431: sub tan { CORE::sin($_[0]) / CORE::cos($_[0]) }
432: #sub atan { atan2($_[0], 1); }
433: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
434: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
435:
436: sub log10 { CORE::log($_[0])/CORE::log(10); }
437:
438: sub factorial {
439: my $input = CORE::int(shift);
440: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
441: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
442: return 1 if $input == 0;
443: my $result = 1;
444: for (my $i=2; $i<=$input; $i++) { $result *= $i }
445: return $result;
446: }
447:
448: sub sgn {
449: return -1 if $_[0] < 0;
450: return 0 if $_[0] == 0;
451: return 1 if $_[0] > 0;
452: }
453:
454: sub min {
455: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
456: return shift @sorted;
457: }
458:
459: sub max {
460: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
461: return pop @sorted;
462: }
463:
464: sub roundto {
465: my ($input,$n) = @_;
466: return sprintf('%.'.$n.'f',$input);
467: }
468:
469: sub to_string {
470: my ($input,$n) = @_;
471: return sprintf($input) if $n eq "";
472: $n = '.'.$n if $n !~ /^\./;
473: return sprintf('%'.$n,$input) if $n ne "";
474: }
475:
476: sub sub_string {
477: my ($str,$start,$len) = @_;
478: return substr($str,$start-1,$len);
479: }
480:
481: sub pow {return $_[0] ** $_[1]; }
482: sub ceil {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (CORE::int($_[0])+ 1) : CORE::int($_[0])); }
483: sub floor {return (($_[0]-CORE::int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? CORE::int($_[0]) : (CORE::int($_[0])-1)); }
484: #sub floor {return int($_[0]); }
485:
486: sub format {
487: my ($value,$fmt)=@_;
488: my $dollarmode;
489: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
490: my $result=sprintf('%.'.$fmt,$value);
491: $result=~s/(E[+-]*)0/$1/;
492: if ($dollarmode) {$result=&dollarmode($result);}
493: return $result;
494: }
495:
496: sub prettyprint {
497: my ($value,$fmt)=@_;
498: my $result;
499: my $dollarmode;
500: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
501: if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
502: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
503: my $frac=$1;
504: if ($dollarmode) { $frac=&dollarformat($frac); }
505: my $exponent=$2;
506: $exponent=~s/^\+0*//;
507: $exponent=~s/^-0*/-/;
508: if ($exponent) {
509: if ($external::target eq 'web') {
510: $result=$frac.'×10<sup>'.$exponent.'</sup>';
511: } elsif ($external::target eq 'tex') {
512: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
513: } else {
514: $result=$value;
515: }
516: } else {
517: $result=$frac;
518: }
519: } else {
520: $result=$value;
521: if ($dollarmode) { $result=&dollarformat($result); }
522: }
523: return $result;
524: }
525:
526: sub dollarformat {
527: my ($number) = @_;
528: if ($number =~ /\./) {
529: while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
530: $number = $1.','.$2.$3;
531: }
532: } else {
533: while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
534: $number = $1.','.$2.$3;
535: }
536: }
537: if ($external::target eq 'tex') {
538: $number='\$'.$number; #' stupid emacs
539: } else {
540: $number='$'.$number; #' stupid emacs
541: }
542: return $number;
543: }
544:
545: sub map {
546: my ($phrase,$dest,$source)=@_;
547: my @oldseed=&random_get_seed();
548: my @seed = &random_seed_from_phrase($phrase);
549: &random_set_seed(@seed);
550: my $destct = scalar(@$dest);
551: if (!$source) {
552: my @output;
553: my @idx = &math_random_permuted_index($destct);
554: my $ctr = 0;
555: while ($ctr < $destct) {
556: $output[$ctr] = $$dest[$idx[$ctr]];
557: $ctr++;
558: }
559: &random_set_seed(@oldseed);
560: return @output;
561: } else {
562: my $num = scalar(@$source);
563: my @idx = &math_random_permuted_index($num);
564: my $ctr = 0;
565: my $tot = $num;
566: $tot = $destct if $destct < $num;
567: if (ref($$dest[0])) {
568: while ($ctr < $tot) {
569: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
570: $ctr++;
571: }
572: } else {
573: while ($ctr < $tot) {
574: $$dest[$ctr] = $$source[$idx[$ctr]];
575: $ctr++;
576: }
577: }
578: }
579: &random_set_seed(@oldseed);
580: return '';
581: }
582:
583: sub rmap {
584: my ($phrase,$dest,$source)=@_;
585: my @oldseed=&random_get_seed();
586: my @seed = &random_seed_from_phrase($phrase);
587: &random_set_seed(@seed);
588: my $destct = scalar(@$dest);
589: if (!$source) {
590: my @idx = &math_random_permuted_index($destct);
591: my $ctr = 0;
592: my @r_idx;
593: while ($ctr < $destct) {
594: $r_idx[$idx[$ctr]] = $ctr;
595: $ctr++;
596: }
597: my @output;
598: $ctr = 0;
599: while ($ctr < $destct) {
600: $output[$ctr] = $$dest[$r_idx[$ctr]];
601: $ctr++;
602: }
603: &random_set_seed(@oldseed);
604: return @output;
605: } else {
606: my $num = scalar(@$source);
607: my @idx = &math_random_permuted_index($num);
608: my $ctr = 0;
609: my $tot = $num;
610: $tot = $destct if $destct < $num;
611: my @r_idx;
612: while ($ctr < $tot) {
613: $r_idx[$idx[$ctr]] = $ctr;
614: $ctr++;
615: }
616: $ctr = 0;
617: if (ref($$dest[0])) {
618: while ($ctr < $tot) {
619: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
620: $ctr++;
621: }
622: } else {
623: while ($ctr < $tot) {
624: $$dest[$ctr] = $$source[$r_idx[$ctr]];
625: $ctr++;
626: }
627: }
628: }
629: &random_set_seed(@oldseed);
630: return '';
631: }
632:
633: sub capa_id { return }
634:
635: sub problem { return }
636:
637: sub name{
638: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
639: $fullname = "" if $fullname eq ", ";
640: $fullname =~ s/\%2d/-/g;
641: return $fullname;
642: }
643:
644: sub student_number {
645: my $id = &EXT('environment.id');
646: $id = '' if $id eq "";
647: return $id;
648: }
649:
650: sub class {
651: my $course = &EXT('course.description');
652: $course = '' if $course eq "";
653: return $course;
654: }
655:
656: sub sec {
657: my $sec = &EXT('request.course.sec');
658: $sec = '' if $sec eq "";
659: return $sec;
660: }
661:
662: sub open_date {
663: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
664: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
665: my @hm = split(/:/,$dc[3]);
666: my $ampm = " am";
667: if ($hm[0] > 12) {
668: $hm[0]-=12;
669: $ampm = " pm";
670: }
671: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
672: }
673:
674: sub due_date {
675: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
676: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
677: my @hm = split(/:/,$dc[3]);
678: my $ampm = " am";
679: if ($hm[0] > 12) {
680: $hm[0]-=12;
681: $ampm = " pm";
682: }
683: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
684: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
685: }
686:
687: sub answer_date {
688: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
689: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
690: my @hm = split(/:/,$dc[3]);
691: my $ampm = " am";
692: if ($hm[0] > 12) {
693: $hm[0]-=12;
694: $ampm = " pm";
695: }
696: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
697: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
698: }
699:
700: sub array_moments {
701: my @input=@_;
702: my (@output,$N);
703: $N=scalar (@input);
704: $output[0]=$N;
705: if ($N <= 1) {
706: $output[1]=$input[0];
707: $output[1]="Input array not defined" if ($N == 0);
708: $output[2]="variance undefined for N<=1";
709: $output[3]="skewness undefined for N<=1";
710: $output[4]="kurtosis undefined for N<=1";
711: return @output;
712: }
713: my $sum=0;
714: foreach my $line (@input) {
715: $sum+=$line;
716: }
717: $output[1] = $sum/$N;
718: my ($x,$sdev,$var,$skew,$kurt) = 0;
719: foreach my $line (@input) {
720: $x=$line-$output[1];
721: $var+=$x**2;
722: $skew+=$x**3;
723: $kurt+=$x**4;
724: }
725: $output[2]=$var/($N-1);
726: $sdev=CORE::sqrt($output[2]);
727: if ($sdev == 0) {
728: $output[3]="inf-variance=0";
729: $output[4]="inf-variance=0";
730: return @output;
731: }
732: $output[3]=$skew/($sdev**3*$N);
733: $output[4]=$kurt/($sdev**4*$N)-3;
734: return @output;
735: }
736:
737: sub choose {
738: my $num = $_[0];
739: return $_[$num];
740: }
741:
742: # expiremental idea
743: sub proper_path {
744: my ($path)=@_;
745: if ( $external::target eq "tex" ) {
746: return '/home/httpd/html'.$path;
747: } else {
748: return $path;
749: }
750: }
751:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>