[system] / trunk / webwork / system / courseScripts / PGcomplexmacros.pl Repository:
ViewVC logotype

Annotation of /trunk/webwork/system/courseScripts/PGcomplexmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91 - (view) (download) (as text)

1 : gage 91 #!/usr/local/bin/perl
2 : gage 45 BEGIN{
3 :     be_strict();
4 : gage 91
5 : gage 45 }
6 : gage 91 # export functions from Complex1.
7 : gage 45
8 :     foreach my $f (@Complex1::EXPORT) {
9 : gage 91 #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever --
10 :     # the original subroutines are destroyed
11 :     next if $f eq 'sqrt'; #exporting the square root caused conflicts with the standard version
12 :     # You can still use Complex1::sqrt to take square root of complex numbers
13 :     local($main::string) = qq{
14 :     sub main::$f {
15 :     &Complex1::$f;
16 :     }
17 :     };
18 :     PG_restricted_eval($main::string);
19 : gage 45 }
20 :    
21 :     # You need to add
22 :     # sub i(); # to your problem or else to dangerousMacros.pl
23 :     # in order to use expressions such as 1 +3*i;
24 :     # Without this prototype you would have to write 1+3*i();
25 :     # The prototype has to be defined at compile time, but dangerousMacros.pl is complied first.
26 :     #Complex1::display_format('cartesian');
27 :    
28 :     sub polar{
29 :     my $z = shift;
30 :     my %options = @_;
31 :     my $r = rho($z);
32 :     my $theta = $z->theta;
33 :     my $r_format = ':%0.3f';
34 :     my $theta_format = ':%0.3f';
35 :     $r_format=":" . $options{r_format} if defined($options{r_format});
36 :     $theta_format = ":" . $options{theta_format} if defined($options{theta_format});
37 :     "{$r$r_format} e^{i {$theta$theta_format}}";
38 :    
39 :     }
40 :     sub cplx_cmp {
41 :     my $correctAns = shift;
42 :     my %options = @_;
43 :     $correctAns = cplx($correctAns,0) unless ref($correctAns) =~/Complex/;
44 : gage 91 assign_option_aliases( \%options,
45 :     'reltol' => 'relTol',
46 :     );
47 : gage 45 set_default_options(\%options,
48 : gage 91 'tolType' => (defined($options{tol}) ) ? 'absolute' : 'relative',
49 :     # default mode should be relative, to obtain this tol must not be defined
50 :     'tol' => $main::numAbsTolDefault,
51 :     'relTol' => $main::numRelPercentTolDefault,
52 :     'zeroLevel' => $main::numZeroLevelDefault,
53 :     'zeroLevelTol' => $main::numZeroLevelTolDefault,
54 :     'format' => $main::numFormatDefault,
55 :     'debug' => 0,
56 :    
57 : gage 45 );
58 : gage 91
59 :    
60 : gage 45 my $ans_eval = sub {
61 :     my $in = shift;
62 :     my $rh_ans = new AnswerHash;
63 : gage 91 $rh_ans->{correct_ans} = $correctAns;
64 :     unless (defined($in) and $in =~/\S/ ) { #bail on empty answer
65 :     $rh_ans->{student_ans} = "error: empty";
66 :     return $rh_ans;
67 :     }
68 : gage 45 my($PG_errors,$PG_errors_long);
69 : gage 91
70 : gage 45 $rh_ans->input($in);
71 : gage 91
72 :     $rh_ans->{student_ans}=math_constants($rh_ans->{student_ans});
73 :     $rh_ans->{student_ans} =~ s/e\^/exp /g; #try to handle exponents
74 : gage 45 $rh_ans=check_syntax($rh_ans);
75 : gage 91 $rh_ans->{student_ans} =~ s/\bi\b/(i)/g; #try to keep -i being recognized as a file reference
76 :     # and recognized as a function whose output is an imaginary number
77 :    
78 :    
79 :    
80 :     warn $rh_ans->pretty_print() if defined($options{debug}) and $options{debug}==1;
81 : gage 45 ($in,$PG_errors,$PG_errors_long) = PG_restricted_eval($rh_ans->{student_ans});
82 : gage 91 $in = $in +0*i; # force the input to be complex
83 : gage 45 if ($PG_errors_long) {
84 :     $rh_ans->{error}=1;
85 :     $rh_ans->{ans_message} = $PG_errors;
86 :     } else {
87 :     $in->display_format('cartesian') if ref($in) =~/Complex/;
88 :     $rh_ans->{student_ans}="$in";
89 : gage 91 my $permitted_error;
90 :     if (defined($options{tolType}) && $options{tolType} eq 'absolute') {
91 :     $permitted_error = $options{tol};
92 :     } elsif ( abs($correctAns) <= $options{zeroLevel}) {
93 :     $permitted_error = $options{zeroLevelTol}; ## want $tol to be non zero
94 :     } else {
95 :     $permitted_error = abs(.01*$options{relTol}*$correctAns); # relative tolerance is given in per cent
96 :     }
97 :     $rh_ans->{score} = (abs($in - $correctAns)<$permitted_error )? 1:0;
98 : gage 45
99 :     }
100 : gage 91
101 : gage 45 $rh_ans;
102 :     };
103 :     $ans_eval;
104 :     }
105 :    
106 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9