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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 293 Revision 326
19 19
20=head1 DESCRIPTION 20=head1 DESCRIPTION
21 21
22=cut 22=cut
23 23
24
25BEGIN{ 24BEGIN{
26 be_strict(); 25 be_strict();
27 26
28} 27}
28
29sub _PGcomplexmacros_init { 29 sub _PGcomplexmacros_init {
30} 30
31
31# export functions from Complex1. 32# export functions from Complex1.
32 33
33foreach my $f (@Complex1::EXPORT) { 34 foreach my $f (@Complex1::EXPORT) {
34 #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever -- 35 #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever --
35 # the original subroutines are destroyed 36 # the original subroutines are destroyed
36 next if $f eq 'sqrt'; #exporting the square root caused conflicts with the standard version 37 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 # You can still use Complex1::sqrt to take square root of complex numbers
38 next if $f eq 'log'; #exporting loq caused conflicts with the standard version 39 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 # You can still use Complex1::log to take square root of complex numbers
41
42 my $string = qq{
43 &Complex1::$f;
44 }
45
46 PG_restricted_eval($string);
47 };
40 48
41 my $string = qq{
42 sub main::$f {
43 &Complex1::$f;
44 }
45 };
46 PG_restricted_eval($string);
47} 49}
50
51
48 52
49# You need to add 53# You need to add
50# sub i(); # to your problem or else to dangerousMacros.pl 54# sub i(); # to your problem or else to dangerousMacros.pl
51# in order to use expressions such as 1 +3*i; 55# in order to use expressions such as 1 +3*i;
52# Without this prototype you would have to write 1+3*i(); 56# 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. 57# The prototype has to be defined at compile time, but dangerousMacros.pl is complied first.
54#Complex1::display_format('cartesian'); 58#Complex1::display_format('cartesian');
55 59
56 60
57=head4 polar 61=head4 polar
62
63=pod
64
58 Usage polar($complex_number,r_format=>"%0.3f",theta_format=>"%0.3f") 65 Usage polar($complex_number,r_format=>"%0.3f",theta_format=>"%0.3f")
59 66
60 Output is text displaying the complex number in "e to the i theta" form. The 67 Output is text displaying the complex number in "e to the i theta" form. The
61 formats for the argument theta is determined by the option C<theta_format> and the 68 formats for the argument theta is determined by the option C<theta_format> and the
62 format for the modulus is determined by the C<r_format> option. 69 format for the modulus is determined by the C<r_format> option.
63 70
64 71=cut
72
73
65 74
66sub polar{ 75sub polar{
67 my $z = shift; 76 my $z = shift;
68 my %options = @_; 77 my %options = @_;
69 set_default_options(\%options, r_format => ':%0.3f', 78 set_default_options(\%options, r_format => ':%0.3f',
70 theta_format => ':%0.3f', 79 theta_format => ':%0.3f',
71 ); 80 );
72 my $r = rho($z); 81 my $r = rho($z);
73 my $theta = $z->theta; 82 my $theta = $z->theta;
74 $r_format=":" . $options{r_format}; 83 my $r_format=":" . $options{r_format};
75 $theta_format = ":" . $options{theta_format}); 84 my $theta_format = ":" . $options{theta_format};
76 "{$r$r_format} e^{i {$theta$theta_format}}"; 85 "{$r$r_format} e^{i {$theta$theta_format}}";
77
78} 86}
79 87
80sub cplx_cmp { 88sub cplx_cmp {
81 my $correctAns = shift; 89 my $correctAns = shift;
82 my %options = @_; 90 my %options = @_;
113 $rh_ans->{student_ans} =~ s/e\^/exp /g; #try to handle exponents 121 $rh_ans->{student_ans} =~ s/e\^/exp /g; #try to handle exponents
114 $rh_ans=check_syntax($rh_ans); 122 $rh_ans=check_syntax($rh_ans);
115 $rh_ans->{student_ans} =~ s/\bi\b/(i)/g; #try to keep -i being recognized as a file reference 123 $rh_ans->{student_ans} =~ s/\bi\b/(i)/g; #try to keep -i being recognized as a file reference
116 # and recognized as a function whose output is an imaginary number 124 # and recognized as a function whose output is an imaginary number
117 125
118 126
119
120 warn $rh_ans->pretty_print() if defined($options{debug}) and $options{debug}==1; 127 warn $rh_ans->pretty_print() if defined($options{debug}) and $options{debug}==1;
121 ($in,$PG_errors,$PG_errors_long) = PG_restricted_eval($rh_ans->{student_ans}); 128 ($in,$PG_errors,$PG_errors_long) = PG_restricted_eval($rh_ans->{student_ans});
122 $in = $in +0*i; # force the input to be complex 129 $in = $in +0*i; # force the input to be complex
123 if ($PG_errors_long) { 130 if ($PG_errors_long) {
124 $rh_ans->{error}=1; 131 $rh_ans->{error}=1;

Legend:
Removed from v.293  
changed lines
  Added in v.326

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9