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

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9