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.51 2002/09/26 20:45:00 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:<br />";
61: } else {
62: $message .="no ws in :$response:<br />";
63: }
64:
65: if ($type eq '' ) {
66: $message .= "Didn't find a type :$type:$expr: defaulting<br />";
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<br />";
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 (!$sig_lbound) {
105: $sig_lbound = 0; #SIG_LB_DEFAULT
106: $sig_ubound =15; #SIG_UB_DEFAULT
107: }
108: if (!$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:<br />Error $error:<br />Answer $answer:<br />Response $response:<br /> type-$type|$tol|$tol_type|$sig:$sig_lbound:$sig_ubound|$unit|<br />$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:$@:<br />";
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<br />";
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:<br />";
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']:<br />";
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']:<br />";
177: $result.=":$#list: answers<br />";
178: $unit=~s/\s//;
179: my $i=0;
180: my $awards='';
181: for ($i=0; $i<@list;$i++) {
182: $result.="trying answer :$list[$i]:<br />";
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:<br />$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: &random_set_seed_from_phrase($external::randomseed);
244: $hidden::RANDOMINIT=1;
245: }
246: if (!defined($step)) { $step=1; }
247: my $num=1+int(($end-$start)/$step);
248: my $result=$start + int(&math_random_uniform() * $num)*$step;
249: return $result;
250: }
251:
252: sub random_normal {
253: my ($item_cnt,$seed,$av,$std_dev) = @_;
254: my @oldseed=&random_get_seed();
255: my @retArray;
256: &random_set_seed_from_phrase($seed);
257: @retArray=&math_random_normal($item_cnt,$av,$std_dev);
258: &random_set_seed(@oldseed);
259: return @retArray;
260: }
261:
262: sub random_beta {
263: my ($item_cnt,$seed,$aa,$bb) = @_;
264: my @oldseed=&random_get_seed();
265: my @retArray;
266: &random_set_seed_from_phrase($seed);
267: @retArray=&math_random_beta($item_cnt,$aa,$bb);
268: &random_set_seed(@oldseed);
269: return @retArray;
270: }
271:
272: sub random_gamma {
273: my ($item_cnt,$seed,$a,$r) = @_;
274: my @oldseed=&random_get_seed();
275: my @retArray;
276: &random_set_seed_from_phrase($seed);
277: @retArray=&math_random_gamma($item_cnt,$a,$r);
278: &random_set_seed(@oldseed);
279: return @retArray;
280: }
281:
282: sub random_exponential {
283: my ($item_cnt,$seed,$av) = @_;
284: my @oldseed=&random_get_seed();
285: my @retArray;
286: &random_set_seed_from_phrase($seed);
287: @retArray=&math_random_exponential($item_cnt,$av);
288: &random_set_seed(@oldseed);
289: return @retArray;
290: }
291:
292: sub random_poisson {
293: my ($item_cnt,$seed,$mu) = @_;
294: my @oldseed=&random_get_seed();
295: my @retArray;
296: &random_set_seed_from_phrase($seed);
297: @retArray=&math_random_poisson($item_cnt,$mu);
298: &random_set_seed(@oldseed);
299: return @retArray;
300: }
301:
302: sub random_chi {
303: my ($item_cnt,$seed,$df) = @_;
304: my @oldseed=&random_get_seed();
305: my @retArray;
306: &random_set_seed_from_phrase($seed);
307: @retArray=&math_random_chi_square($item_cnt,$df);
308: &random_set_seed(@oldseed);
309: return @retArray;
310: }
311:
312: sub random_noncentral_chi {
313: my ($item_cnt,$seed,$df,$nonc) = @_;
314: my @oldseed=&random_get_seed();
315: my @retArray;
316: &random_set_seed_from_phrase($seed);
317: @retArray=&math_random_noncentral_chi_square($item_cnt,$df,$nonc);
318: &random_set_seed(@oldseed);
319: return @retArray;
320: }
321:
322: sub random_f {
323: my ($item_cnt,$seed,$dfn,$dfd) = @_;
324: my @oldseed=&random_get_seed();
325: my @retArray;
326: &random_set_seed_from_phrase($seed);
327: @retArray=&math_random_f($item_cnt,$dfn,$dfd);
328: &random_set_seed(@oldseed);
329: return @retArray;
330: }
331:
332: sub random_noncentral_f {
333: my ($item_cnt,$seed,$dfn,$dfd,$nonc) = @_;
334: my @oldseed=&random_get_seed();
335: my @retArray;
336: &random_set_seed_from_phrase($seed);
337: @retArray=&math_random_noncentral_f($item_cnt,$dfn,$dfd,$nonc);
338: &random_set_seed(@oldseed);
339: return @retArray;
340: }
341:
342: sub random_multivariate_normal {
343: my ($item_cnt,$seed,$mean,$covar) = @_;
344: my @oldseed=&random_get_seed();
345: &random_set_seed_from_phrase($seed);
346: @retArray=&math_random_multivariate_normal($item_cnt,@$mean,@$covar);
347: &random_set_seed(@oldseed);
348: return @retArray;
349: }
350:
351: sub random_multinomial {
352: my ($item_cnt,$seed,@p) = @_;
353: my @oldseed=&random_get_seed();
354: my @retArray;
355: &random_set_seed_from_phrase($seed);
356: @retArray=&math_random_multinomial($item_cnt,@p);
357: &random_set_seed(@oldseed);
358: return @retArray;
359: }
360:
361: sub random_permutation {
362: my ($seed,@inArray) = @_;
363: my @oldseed=&random_get_seed();
364: my @retArray;
365: &random_set_seed_from_phrase($seed);
366: @retArray=&math_random_permutation(@inArray);
367: &random_set_seed(@oldseed);
368: return @retArray;
369: }
370:
371: sub random_uniform {
372: my ($item_cnt,$seed,$low,$high) = @_;
373: my @oldseed=&random_get_seed();
374: my @retArray;
375: &random_set_seed_from_phrase($seed);
376: @retArray=&math_random_uniform($item_cnt,$low,$high);
377: &random_set_seed(@oldseed);
378: return @retArray;
379: }
380:
381: sub random_uniform_integer {
382: my ($item_cnt,$seed,$low,$high) = @_;
383: my @oldseed=&random_get_seed();
384: my @retArray;
385: &random_set_seed_from_phrase($seed);
386: @retArray=&math_random_uniform_integer($item_cnt,$low,$high);
387: &random_set_seed(@oldseed);
388: return @retArray;
389: }
390:
391: sub random_binomial {
392: my ($item_cnt,$seed,$nt,$p) = @_;
393: my @oldseed=&random_get_seed();
394: my @retArray;
395: &random_set_seed_from_phrase($seed);
396: @retArray=&math_random_binomial($item_cnt,$nt,$p);
397: &random_set_seed(@oldseed);
398: return @retArray;
399: }
400:
401: sub random_negative_binomial {
402: my ($item_cnt,$seed,$ne,$p) = @_;
403: my @oldseed=&random_get_seed();
404: my @retArray;
405: &random_set_seed_from_phrase($seed);
406: @retArray=&math_random_negative_binomial($item_cnt,$ne,$p);
407: &random_set_seed(@oldseed);
408: return @retArray;
409: }
410:
411: sub abs { abs(shift) }
412: sub sin { sin(shift) }
413: sub cos { cos(shift) }
414: sub exp { exp(shift) }
415: sub int { int(shift) }
416: sub log { log(shift) }
417: sub atan2 { atan2($_[0],$_[1]) }
418: sub sqrt { sqrt(shift) }
419:
420: sub tan { sin($_[0]) / cos($_[0]) }
421: #sub atan { atan2($_[0], 1); }
422: #sub acos { atan2(sqrt(1 - $_[0] * $_[0]), $_[0] ); }
423: #sub asin { atan2($_[0], sqrt(1- $_[0] * $_[0]) ); }
424:
425: sub log10 { log($_[0])/log(10); }
426:
427: sub factorial {
428: my $input = int(shift);
429: return "Error - unable to take factorial of an negative number ($input)" if $input < 0;
430: return "Error - factorial result is greater than system limit ($input)" if $input > 170;
431: return 1 if $input == 0;
432: my $result = 1;
433: for (my $i=2; $i<=$input; $i++) { $result *= $i }
434: return $result;
435: }
436:
437: sub sgn {
438: return -1 if $_[0] < 0;
439: return 0 if $_[0] == 0;
440: return 1 if $_[0] > 0;
441: }
442:
443: sub min {
444: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
445: return shift @sorted;
446: }
447:
448: sub max {
449: my @sorted = sort { $a <=> $b || $a cmp $b } @_;
450: return pop @sorted;
451: }
452:
453: sub roundto {
454: my ($input,$n) = @_;
455: return sprintf('%.'.$n.'f',$input);
456: }
457:
458: sub to_string {
459: my ($input,$n) = @_;
460: return sprintf($input) if $n eq "";
461: $n = '.'.$n if $n !~ /^\./;
462: return sprintf('%'.$n,$input) if $n ne "";
463: }
464:
465: sub sub_string {
466: my ($str,$start,$len) = @_;
467: return substr($str,$start-1,$len);
468: }
469:
470: sub pow {return $_[0] ** $_[1]; }
471: sub ceil {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? (int($_[0])+ 1) : int($_[0])); }
472: sub floor {return (($_[0]-int($_[0]))== 0.0) ? $_[0] : (($_[0] > 0) ? int($_[0]) : (int($_[0])-1)); }
473: #sub floor {return int($_[0]); }
474:
475: sub format {
476: my ($value,$fmt)=@_;
477: my $dollarmode;
478: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
479: my $result=sprintf('%.'.$fmt,$value);
480: $result=~s/(E[+-]*)0/$1/;
481: if ($dollarmode) {$result=&dollarmode($result);}
482: return $result;
483: }
484:
485: sub prettyprint {
486: my ($value,$fmt)=@_;
487: my $result;
488: my $dollarmode;
489: if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
490: if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
491: if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
492: my $frac=$1;
493: if ($dollarmode) { $frac=&dollarformat($frac); }
494: my $exponent=$2;
495: $exponent=~s/^\+0*//;
496: $exponent=~s/^-0*/-/;
497: if ($exponent) {
498: if ($external::target eq 'web') {
499: $result=$frac.'×10<sup>'.$exponent.'</sup>';
500: } elsif ($external::target eq 'tex') {
501: $result='\ensuremath{'.$frac.'\times 10^{'.$exponent.'}}';
502: } else {
503: $result=$value;
504: }
505: } else {
506: $result=$frac;
507: }
508: } else {
509: $result=$value;
510: if ($dollarmode) { $result=&dollarformat($result); }
511: }
512: return $result;
513: }
514:
515: sub dollarformat {
516: my ($number) = @_;
517: if ($number =~ /\./) {
518: while ($number =~ /([^\.,]+)([^\.,][^\.,][^\.,])([,0-9]*\.[0-9]*$)/) {
519: $number = $1.','.$2.$3;
520: }
521: } else {
522: while ($number =~ /([^,]+)([^,][^,][^,])([,0-9]*)$/) {
523: $number = $1.','.$2.$3;
524: }
525: }
526: if ($external::target eq 'tex') {
527: $number='\$'.$number; #' stupid emacs
528: } else {
529: $number='$'.$number; #' stupid emacs
530: }
531: return $number;
532: }
533:
534: sub map {
535: my ($phrase,$dest,$source)=@_;
536: my @oldseed=&random_get_seed();
537: my @seed = &random_seed_from_phrase($phrase);
538: &random_set_seed(@seed);
539: my $destct = scalar(@$dest);
540: if (!$source) {
541: my @output;
542: my @idx = &math_random_permuted_index($destct);
543: my $ctr = 0;
544: while ($ctr < $destct) {
545: $output[$ctr] = $$dest[$idx[$ctr]];
546: $ctr++;
547: }
548: &random_set_seed(@oldseed);
549: return @output;
550: } else {
551: my $num = scalar(@$source);
552: my @idx = &math_random_permuted_index($num);
553: my $ctr = 0;
554: my $tot = $num;
555: $tot = $destct if $destct < $num;
556: if (ref($$dest[0])) {
557: while ($ctr < $tot) {
558: ${$$dest[$ctr]} = $$source[$idx[$ctr]];
559: $ctr++;
560: }
561: } else {
562: while ($ctr < $tot) {
563: $$dest[$ctr] = $$source[$idx[$ctr]];
564: $ctr++;
565: }
566: }
567: }
568: &random_set_seed(@seed);
569: return '';
570: }
571:
572: sub rmap {
573: my ($phrase,$dest,$source)=@_;
574: my @oldseed=&random_get_seed();
575: my @seed = &random_seed_from_phrase($phrase);
576: &random_set_seed(@seed);
577: my $destct = scalar(@$dest);
578: if (!$source) {
579: my @idx = &math_random_permuted_index($destct);
580: my $ctr = 0;
581: my @r_idx;
582: while ($ctr < $destct) {
583: $r_idx[$idx[$ctr]] = $ctr;
584: $ctr++;
585: }
586: my @output;
587: $ctr = 0;
588: while ($ctr < $destct) {
589: $output[$ctr] = $$dest[$r_idx[$ctr]];
590: $ctr++;
591: }
592: &random_set_seed(@oldseed);
593: return @output;
594: } else {
595: my $num = scalar(@$source);
596: my @idx = &math_random_permuted_index($num);
597: my $ctr = 0;
598: my $tot = $num;
599: $tot = $destct if $destct < $num;
600: my @r_idx;
601: while ($ctr < $tot) {
602: $r_idx[$idx[$ctr]] = $ctr;
603: $ctr++;
604: }
605: $ctr = 0;
606: if (ref($$dest[0])) {
607: while ($ctr < $tot) {
608: ${$$dest[$ctr]} = $$source[$r_idx[$ctr]];
609: $ctr++;
610: }
611: } else {
612: while ($ctr < $tot) {
613: $$dest[$ctr] = $$source[$r_idx[$ctr]];
614: $ctr++;
615: }
616: }
617: }
618: &random_set_seed(@oldseed);
619: return '';
620: }
621:
622: sub capa_id { return }
623:
624: sub problem { return }
625:
626: sub name{
627: my $fullname = &EXT('environment.lastname').', '.&EXT('environment.firstname').' '.&EXT('environment.middlename');
628: $fullname = "" if $fullname eq ", ";
629: $fullname =~ s/\%2d/-/g;
630: return $fullname;
631: }
632:
633: sub student_number {
634: my $id = &EXT('environment.id');
635: $id = '' if $id eq "";
636: return $id;
637: }
638:
639: sub class {
640: my $course = &EXT('course.description');
641: $course = '' if $course eq "";
642: return $course;
643: }
644:
645: sub sec {
646: my $sec = &EXT('request.course.sec');
647: $sec = '' if $sec eq "";
648: return $sec;
649: }
650:
651: sub open_date {
652: my @dc = split(/\s+/,localtime(&EXT('resource.0.opendate')));
653: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
654: my @hm = split(/:/,$dc[3]);
655: my $ampm = " am";
656: if ($hm[0] > 12) {
657: $hm[0]-=12;
658: $ampm = " pm";
659: }
660: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
661: }
662:
663: sub due_date {
664: my @dc = split(/\s+/,localtime(&EXT('resource.0.duedate')));
665: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
666: my @hm = split(/:/,$dc[3]);
667: my $ampm = " am";
668: if ($hm[0] > 12) {
669: $hm[0]-=12;
670: $ampm = " pm";
671: }
672: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
673: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
674: }
675:
676: sub answer_date {
677: my @dc = split(/\s+/,localtime(&EXT('resource.0.answerdate')));
678: return '' if ($dc[0] eq "Wed" and $dc[2] == 31 and $dc[4] == 1969);
679: my @hm = split(/:/,$dc[3]);
680: my $ampm = " am";
681: if ($hm[0] > 12) {
682: $hm[0]-=12;
683: $ampm = " pm";
684: }
685: return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$hm[0].':'.$hm[1].$ampm;
686: # return $dc[0].', '.$dc[1].' '.$dc[2].', '.$dc[4].' at '.$dc[3];
687: }
688:
689: sub array_moments {
690: my @input=@_;
691: my (@output,$N);
692: $N=scalar (@input);
693: $output[0]=$N;
694: if ($N <= 1) {
695: $output[1]=$input[0];
696: $output[1]="Input array not defined" if ($N == 0);
697: $output[2]="variance undefined for N<=1";
698: $output[3]="skewness undefined for N<=1";
699: $output[4]="kurtosis undefined for N<=1";
700: return @output;
701: }
702: my $sum=0;
703: foreach my $line (@input) {
704: $sum+=$line;
705: }
706: $output[1] = $sum/$N;
707: my ($x,$sdev,$var,$skew,$kurt) = 0;
708: foreach my $line (@input) {
709: $x=$line-$output[1];
710: $var+=$x**2;
711: $skew+=$x**3;
712: $kurt+=$x**4;
713: }
714: $output[2]=$var/($N-1);
715: $sdev=sqrt($output[2]);
716: if ($sdev == 0) {
717: $output[3]="inf-variance=0";
718: $output[4]="inf-variance=0";
719: return @output;
720: }
721: $output[3]=$skew/($sdev**3*$N);
722: $output[4]=$kurt/($sdev**4*$N)-3;
723: return @output;
724: }
725:
726: sub choose {
727: my $num = $_[0];
728: return $_[$num];
729: }
730:
731: # expiremental idea
732: sub proper_path {
733: my ($path)=@_;
734: if ( $external::target eq "tex" ) {
735: return '/home/httpd/html'.$path;
736: } else {
737: return $path;
738: }
739: }
740:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>