[system] / trunk / pg / macros / PGcomplexmacros2Exp.pl Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

View of /trunk/pg/macros/PGcomplexmacros2Exp.pl

Revision 4974 - (download) (as text) (annotate)
Wed Jun 6 17:58:03 2007 UTC (12 years, 8 months ago) by gage
File size: 3707 byte(s)
```This is an experimental version of PGcomplexmacros2Exp.pl
```

```    1
2 # This file     is PGcomplexmacros2.pl
3 # This includes the subroutines for the ANS macros, that
4 # is, macros allowing a more flexible answer checking
5 ####################################################################
6 # Copyright @ 2006-2007 The WeBWorK Team
7 # All Rights Reserved
8 ####################################################################
9
10
11 =head1 NAME
12
13   More macros for handling multivalued functions of a complex variable
14
15 =head1 SYNPOSIS
16
17
18
19 =head1 DESCRIPTION
20
21 =cut
22
23
24 BEGIN{
25   be_strict();
26
27 }
28
29
30
31 sub _PGcomplexmacros2_init {
32 }
33
34 ### for handling multivalued functions of a complex variable
35 ###### subroutines ###########
36 sub pg_mod {      #calculate complex number mod \$b i.e \$a = k\$b + (\$a mod \$b) for some integer k
37     my \$a =shift;
38     my \$b =shift;
39     return \$a if \$b ==0;  # modulo 0 returns \$a itself.
40     \$a-int(Complex(\$a/\$b)->Re)*\$b;
41                 #int takes the integer real part of a complex number
42 }
43
44 sub compareMod {
45   my \$mod_factor = shift;
46   sub {
47     my (\$correct, \$response, \$ans_checker) = @_;
48     my \$New_Comp;
49     # my \$residue = pg_mod (\$correct - \$response, \$mod_factor);
50     #(\$residue == Complex(0,0)) ? 1:0;
51
52
53
54     if(\$mod_factor->isComplex)  {
55       my \$Abs_mod_factor = sqrt(((\$mod_factor)->Re)**2+((\$mod_factor)->Im)**2);
56       \$New_Comp = \$Abs_mod_factor + (\$Abs_mod_factor)*i;
57     } else    {
58       \$New_Comp = \$mod_factor;
59     }
60
61      my \$residue = pg_mod (\$correct - \$response + (\$New_Comp / 2), \$mod_factor);
62     (\$residue == (\$New_Comp)/2) ? 1:0;
63
64   }
65 }
66
67 sub uniqueList {
68   my \$ra_list = shift;
69   my \$rf_equiv = shift;
70   my \$listSize = @\$ra_list;
71   my @equalPairs = ();
72   my (\$i,\$j);
73   for (\$i=0;  \$i<\$listSize; \$i++) {
74     for(\$j=\$i+1;  \$j<\$listSize; \$j++) {
75       push( @equalPairs, [\$i,\$j] ) if &\$rf_equiv(\$ra_list->[\$i],\$ra_list->[\$j]);
76     }
77   }
78   @equalPairs;
79
80 }
81
82 # compares (z1 to z2 mod 4pi/3)
83
84
85
86
87 sub compareListsMod {
88   my \$alpha = shift;             # responses in list are compared mod \$alpha to the correct answers
89   my \$equiv = shift || "2pi*i";  # responses in list are considered duplicates if they are equivalent mod \$equiv
90   \$alpha = Complex(\$alpha); \$equiv = Complex(\$equiv);
91   my \$cmp_mod_alpha = compareMod(\$alpha);  # this must be the smallest interval mod 2pi
92     my \$cmp_mod2pi = compareMod(\$equiv);
93   sub {
94     my (\$correct, \$student, \$ans) = @_;
95     my @equalPairs = uniqueList(\$student, \$cmp_mod2pi);
96     if (@equalPairs) {
97       my \$msg = "";
98       for my \$pair (@equalPairs) {
99         my (\$pos1, \$pos2) = @\$pair;
100         \$msg .= \$student->[\$pos1]." and ".\$student->[\$pos2]. " are equivalent.  ";
101       }
102       Value::Error("Some of your answers are equivalent. ".\$msg);
103       return 0
104     } else {
105       my \$result=0; my \$msg = '';
106       my @correct = @\$correct; my @student=@\$student;
107       return 0 unless scalar(@correct) == scalar(@student);
108       foreach my \$i (0..\$#correct) {
109         if ( &\$cmp_mod_alpha(\$correct[\$i],\$student[\$i]) ) {
110           \$result++
111         } else {
112           \$msg .= "Your \$i answer is incorrect. ";
113         }
114       }
115       Value::Error(\$msg) unless \$result == @correct;
116       return \$result;
117     }
118   }
119
120
121 }
122
123 sub compareFormulaMod {
124   sub {
125     my (\$correct, \$student, \$ans) = @_;
126     my \$result = 1;
127     #  evaluate the formulas at 0 and 1 to find coefficients of powers of k
128     my \$correct_a0 = \$correct->eval(k=>0);
129     my \$student_a0 = \$student->eval(k=>0);
130     my \$correct_a1 = \$correct->eval(k=>1) - \$correct_a0;
131     my \$student_a1 = \$student->eval(k=>1) - \$student_a0;
132
133
134     my \$cmp_mod = compareMod(\$correct_a1);
135     &\$cmp_mod(\$correct_a0, \$student_a0) && (
136
137     #&cmp(\$correct_a0,\$student_a0) && (
138           \$correct_a1 == \$student_a1 or \$correct_a1 == - \$student_a1
139           # - \$student_a1 to make sure it is equal to correct_a1 rather than a multiple of it.
140     );
141   }
142
143 }
144 ##### end subroutines ##################
145
146
```

 aubreyja at gmail dot com ViewVC Help Powered by ViewVC 1.0.9