[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 102 Revision 321
7# All Rights Reserved 7# All Rights Reserved
8#################################################################### 8####################################################################
9#$Id$ 9#$Id$
10 10
11 11
12
13
14
15
16
17
18
19=head1 NAME
20
21 Macros for complex numbers for the PG language
22
23=head1 SYNPOSIS
24
25
26
27=head1 DESCRIPTION
28
29=cut
30
31
12BEGIN{ 32BEGIN{
13 be_strict(); 33 be_strict();
14 34
15} 35}
36sub _PGcomplexmacros_init {
37
16# export functions from Complex1. 38# export functions from Complex1.
17 39
18foreach my $f (@Complex1::EXPORT) { 40 foreach my $f (@Complex1::EXPORT) {
19 #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever -- 41 #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever --
20 # the original subroutines are destroyed 42 # the original subroutines are destroyed
21 next if $f eq 'sqrt'; #exporting the square root caused conflicts with the standard version 43 next if $f eq 'sqrt'; #exporting the square root caused conflicts with the standard version
22 # You can still use Complex1::sqrt to take square root of complex numbers 44 # You can still use Complex1::sqrt to take square root of complex numbers
23 local($main::string) = qq{ 45 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
47
48 my $string = qq{
24 sub main::$f { 49 sub main::$f {
25 &Complex1::$f; 50 &Complex1::$f;
26 } 51 }
27 }; 52 };
28 PG_restricted_eval($main::string); 53 PG_restricted_eval($string);
54 }
29} 55}
30 56
31# You need to add 57# You need to add
32# sub i(); # to your problem or else to dangerousMacros.pl 58# sub i(); # to your problem or else to dangerousMacros.pl
33# in order to use expressions such as 1 +3*i; 59# in order to use expressions such as 1 +3*i;
34# Without this prototype you would have to write 1+3*i(); 60# Without this prototype you would have to write 1+3*i();
35# The prototype has to be defined at compile time, but dangerousMacros.pl is complied first. 61# The prototype has to be defined at compile time, but dangerousMacros.pl is complied first.
36#Complex1::display_format('cartesian'); 62#Complex1::display_format('cartesian');
37 63
64
65=head4 polar
66
67 Usage polar($complex_number,r_format=>"%0.3f",theta_format=>"%0.3f")
68
69 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
71 format for the modulus is determined by the C<r_format> option.
72
73=cut
74
75
38sub polar{ 76sub polar{
39 my $z = shift; 77 my $z = shift;
40 my %options = @_; 78 my %options = @_;
79 set_default_options(\%options, r_format => ':%0.3f',
80 theta_format => ':%0.3f',
81 );
41 my $r = rho($z); 82 my $r = rho($z);
42 my $theta = $z->theta; 83 my $theta = $z->theta;
43 my $r_format = ':%0.3f'; 84 my $r_format=":" . $options{r_format};
44 my $theta_format = ':%0.3f'; 85 my $theta_format = ":" . $options{theta_format});
45 $r_format=":" . $options{r_format} if defined($options{r_format});
46 $theta_format = ":" . $options{theta_format} if defined($options{theta_format});
47 "{$r$r_format} e^{i {$theta$theta_format}}"; 86 "{$r$r_format} e^{i {$theta$theta_format}}";
48 87
49} 88}
89
50sub cplx_cmp { 90sub cplx_cmp {
51 my $correctAns = shift; 91 my $correctAns = shift;
52 my %options = @_; 92 my %options = @_;
53 $correctAns = cplx($correctAns,0) unless ref($correctAns) =~/Complex/; 93 $correctAns = cplx($correctAns,0) unless ref($correctAns) =~/Complex/;
54 assign_option_aliases( \%options, 94 assign_option_aliases( \%options,
55 'reltol' => 'relTol', 95 'reltol' => 'relTol',
56 ); 96 );
57 set_default_options(\%options, 97 set_default_options(\%options,
58 'tolType' => (defined($options{tol}) ) ? 'absolute' : 'relative', 98 'tolType' => (defined($options{tol}) ) ? 'absolute' : 'relative',
59 # default mode should be relative, to obtain this tol must not be defined 99 # default mode should be relative, to obtain this tol must not be defined
60 'tol' => $main::numAbsTolDefault, 100 'tol' => $main::numAbsTolDefault,
61 'relTol' => $main::numRelPercentTolDefault, 101 'relTol' => $main::numRelPercentTolDefault,
62 'zeroLevel' => $main::numZeroLevelDefault, 102 'zeroLevel' => $main::numZeroLevelDefault,
63 'zeroLevelTol' => $main::numZeroLevelTolDefault, 103 'zeroLevelTol' => $main::numZeroLevelTolDefault,
64 'format' => $main::numFormatDefault, 104 'format' => $main::numFormatDefault,
65 'debug' => 0, 105 'debug' => 0,
66 106
67 ); 107 );
68 108
69 109
70 my $ans_eval = sub { 110 my $ans_eval = sub {
71 my $in = shift; 111 my $in = shift;
72 my $rh_ans = new AnswerHash; 112 my $rh_ans = new AnswerHash;
73 $rh_ans->{correct_ans} = $correctAns; 113 $rh_ans->{correct_ans} = $correctAns;
74 unless (defined($in) and $in =~/\S/ ) { #bail on empty answer 114 unless (defined($in) and $in =~/\S/ ) { #bail on empty answer
75 $rh_ans->{student_ans} = "error: empty"; 115 $rh_ans->{student_ans} = "error: empty";
76 return $rh_ans; 116 return $rh_ans;
77 } 117 }

Legend:
Removed from v.102  
changed lines
  Added in v.321

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9