[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 45 Revision 293
1#!/usr/local/bin/perl 1#!/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=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
2BEGIN{ 25BEGIN{
3 be_strict(); 26 be_strict();
4 sub i(); 27
5} 28}
29sub _PGcomplexmacros_init {
30}
31# export functions from Complex1.
6 32
7foreach my $f (@Complex1::EXPORT) { 33foreach my $f (@Complex1::EXPORT) {
8 PG_restricted_eval("\*$f = \*Complex1::$f"); 34 #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 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 sub main::$f {
43 &Complex1::$f;
44 }
45 };
46 PG_restricted_eval($string);
9} 47}
10 48
11# You need to add 49# You need to add
12# sub i(); # to your problem or else to dangerousMacros.pl 50# sub i(); # to your problem or else to dangerousMacros.pl
13# in order to use expressions such as 1 +3*i; 51# in order to use expressions such as 1 +3*i;
14# Without this prototype you would have to write 1+3*i(); 52# Without this prototype you would have to write 1+3*i();
15# The prototype has to be defined at compile time, but dangerousMacros.pl is complied first. 53# The prototype has to be defined at compile time, but dangerousMacros.pl is complied first.
16#Complex1::display_format('cartesian'); 54#Complex1::display_format('cartesian');
17 55
56
57=head4 polar
58 Usage polar($complex_number,r_format=>"%0.3f",theta_format=>"%0.3f")
59
60 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
62 format for the modulus is determined by the C<r_format> option.
63
64
65
18sub polar{ 66sub polar{
19 my $z = shift; 67 my $z = shift;
20 my %options = @_; 68 my %options = @_;
69 set_default_options(\%options, r_format => ':%0.3f',
70 theta_format => ':%0.3f',
71 );
21 my $r = rho($z); 72 my $r = rho($z);
22 my $theta = $z->theta; 73 my $theta = $z->theta;
23 my $r_format = ':%0.3f'; 74 $r_format=":" . $options{r_format};
24 my $theta_format = ':%0.3f'; 75 $theta_format = ":" . $options{theta_format});
25 $r_format=":" . $options{r_format} if defined($options{r_format});
26 $theta_format = ":" . $options{theta_format} if defined($options{theta_format});
27 "{$r$r_format} e^{i {$theta$theta_format}}"; 76 "{$r$r_format} e^{i {$theta$theta_format}}";
28 77
29} 78}
79
30sub cplx_cmp { 80sub cplx_cmp {
31 my $correctAns = shift; 81 my $correctAns = shift;
32 my %options = @_; 82 my %options = @_;
33 $correctAns = cplx($correctAns,0) unless ref($correctAns) =~/Complex/; 83 $correctAns = cplx($correctAns,0) unless ref($correctAns) =~/Complex/;
84 assign_option_aliases( \%options,
85 'reltol' => 'relTol',
86 );
34 set_default_options(\%options, 87 set_default_options(\%options,
35 reltol => .01 88 'tolType' => (defined($options{tol}) ) ? 'absolute' : 'relative',
89 # default mode should be relative, to obtain this tol must not be defined
90 'tol' => $main::numAbsTolDefault,
91 'relTol' => $main::numRelPercentTolDefault,
92 'zeroLevel' => $main::numZeroLevelDefault,
93 'zeroLevelTol' => $main::numZeroLevelTolDefault,
94 'format' => $main::numFormatDefault,
95 'debug' => 0,
96
36 ); 97 );
37 my $tol = .01*$options{reltol}; 98
99
38 my $ans_eval = sub { 100 my $ans_eval = sub {
39 my $in = shift; 101 my $in = shift;
40 my $rh_ans = new AnswerHash; 102 my $rh_ans = new AnswerHash;
103 $rh_ans->{correct_ans} = $correctAns;
104 unless (defined($in) and $in =~/\S/ ) { #bail on empty answer
105 $rh_ans->{student_ans} = "error: empty";
106 return $rh_ans;
107 }
41 my($PG_errors,$PG_errors_long); 108 my($PG_errors,$PG_errors_long);
42 $rh_ans->{correct_ans} = $correctAns; 109
43 $rh_ans->input($in); 110 $rh_ans->input($in);
111
112 $rh_ans->{student_ans}=math_constants($rh_ans->{student_ans});
113 $rh_ans->{student_ans} =~ s/e\^/exp /g; #try to handle exponents
44 $rh_ans=check_syntax($rh_ans); 114 $rh_ans=check_syntax($rh_ans);
45 $rh_ans->{student_ans} =~ s/e\^/exp /g; #try to handle exponents
46 $rh_ans->{student_ans} =~ s/-\s*i/-1*i/g; #try to keep -i being recognized as a file reference 115 $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
117
118
119
120 warn $rh_ans->pretty_print() if defined($options{debug}) and $options{debug}==1;
47 ($in,$PG_errors,$PG_errors_long) = PG_restricted_eval($rh_ans->{student_ans}); 121 ($in,$PG_errors,$PG_errors_long) = PG_restricted_eval($rh_ans->{student_ans});
122 $in = $in +0*i; # force the input to be complex
48 if ($PG_errors_long) { 123 if ($PG_errors_long) {
49 $rh_ans->{error}=1; 124 $rh_ans->{error}=1;
50 $rh_ans->{ans_message} = $PG_errors; 125 $rh_ans->{ans_message} = $PG_errors;
51 } else { 126 } else {
52 $in->display_format('cartesian') if ref($in) =~/Complex/; 127 $in->display_format('cartesian') if ref($in) =~/Complex/;
53 $rh_ans->{student_ans}="$in"; 128 $rh_ans->{student_ans}="$in";
129 my $permitted_error;
130 if (defined($options{tolType}) && $options{tolType} eq 'absolute') {
131 $permitted_error = $options{tol};
132 } elsif ( abs($correctAns) <= $options{zeroLevel}) {
133 $permitted_error = $options{zeroLevelTol}; ## want $tol to be non zero
134 } else {
135 $permitted_error = abs(.01*$options{relTol}*$correctAns); # relative tolerance is given in per cent
136 }
54 $rh_ans->{score} = (abs($in - $correctAns)<$tol*abs($correctAns) )? 1:0; 137 $rh_ans->{score} = (abs($in - $correctAns)<$permitted_error )? 1:0;
55 138
56 } 139 }
140
57 $rh_ans; 141 $rh_ans;
58 }; 142 };
59 $ans_eval; 143 $ans_eval;
60} 144}
61 145

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9