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