[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 219 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9