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

Legend:
Removed from v.91  
changed lines
  Added in v.385

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9