[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 324 Revision 325
5#################################################################### 5####################################################################
6# Copyright @ 1995-2001 The WeBWorK Team 6# Copyright @ 1995-2001 The WeBWorK Team
7# All Rights Reserved 7# All Rights Reserved
8#################################################################### 8####################################################################
9#$Id$ 9#$Id$
10
11
12
13
14
15
16
17 10
18 11
19=head1 NAME 12=head1 NAME
20 13
21 Macros for complex numbers for the PG language 14 Macros for complex numbers for the PG language
26 19
27=head1 DESCRIPTION 20=head1 DESCRIPTION
28 21
29=cut 22=cut
30 23
31
32BEGIN{ 24BEGIN{
33 be_strict(); 25 be_strict();
34 26
35} 27}
28
36sub _PGcomplexmacros_init { 29 sub _PGcomplexmacros_init {
30
37 31
38# export functions from Complex1. 32# export functions from Complex1.
39 33
40 foreach my $f (@Complex1::EXPORT) { 34 foreach my $f (@Complex1::EXPORT) {
41 #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever -- 35 #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever --
42 # the original subroutines are destroyed 36 # the original subroutines are destroyed
43 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
44 # 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
45 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
46 # 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
47 41
48 my $string = qq{ 42 my $string = qq{
49 sub main::$f {
50 &Complex1::$f; 43 &Complex1::$f;
51 } 44 }
52 }; 45
53 PG_restricted_eval($string); 46 PG_restricted_eval($string);
54 } 47 };
48
55} 49}
50
51
56 52
57# You need to add 53# You need to add
58# sub i(); # to your problem or else to dangerousMacros.pl 54# sub i(); # to your problem or else to dangerousMacros.pl
59# in order to use expressions such as 1 +3*i; 55# in order to use expressions such as 1 +3*i;
60# Without this prototype you would have to write 1+3*i(); 56# Without this prototype you would have to write 1+3*i();
62#Complex1::display_format('cartesian'); 58#Complex1::display_format('cartesian');
63 59
64 60
65=head4 polar 61=head4 polar
66 62
63=pod
64
67 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")
68 66
69 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
70 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
71 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.
72 70
73=cut 71=cut
72
74 73
75 74
76sub polar{ 75sub polar{
77 my $z = shift; 76 my $z = shift;
78 my %options = @_; 77 my %options = @_;
80 theta_format => ':%0.3f', 79 theta_format => ':%0.3f',
81 ); 80 );
82 my $r = rho($z); 81 my $r = rho($z);
83 my $theta = $z->theta; 82 my $theta = $z->theta;
84 my $r_format=":" . $options{r_format}; 83 my $r_format=":" . $options{r_format};
85 my $theta_format = ":" . $options{theta_format}); 84 my $theta_format = ":" . $options{theta_format};
86 "{$r$r_format} e^{i {$theta$theta_format}}"; 85 "{$r$r_format} e^{i {$theta$theta_format}}";
87
88} 86}
89 87
90sub cplx_cmp { 88sub cplx_cmp {
91 my $correctAns = shift; 89 my $correctAns = shift;
92 my %options = @_; 90 my %options = @_;
123 $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
124 $rh_ans=check_syntax($rh_ans); 122 $rh_ans=check_syntax($rh_ans);
125 $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
126 # and recognized as a function whose output is an imaginary number 124 # and recognized as a function whose output is an imaginary number
127 125
128 126
129
130 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;
131 ($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});
132 $in = $in +0*i; # force the input to be complex 129 $in = $in +0*i; # force the input to be complex
133 if ($PG_errors_long) { 130 if ($PG_errors_long) {
134 $rh_ans->{error}=1; 131 $rh_ans->{error}=1;

Legend:
Removed from v.324  
changed lines
  Added in v.325

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9