[system] / trunk / pg / macros / parserFormulaUpToConstant.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/parserFormulaUpToConstant.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 5394 Revision 5461
13 # they want, and it doesn't have to be the one the professor used. 13 # they want, and it doesn't have to be the one the professor used.
14 # 14 #
15 # To use FormulaWithConstat objects, load this macro file at the 15 # To use FormulaWithConstat objects, load this macro file at the
16 # top of your problem: 16 # top of your problem:
17 # 17 #
18 # loadMacros("parserFormulaUpToConstant"); 18 # loadMacros("parserFormulaUpToConstant.pl");
19 # 19 #
20 # then create a formula with constant as follows: 20 # then create a formula with constant as follows:
21 # 21 #
22 # $f = FormulaUpToConstant("sin(x)+C"); 22 # $f = FormulaUpToConstant("sin(x)+C");
23 # 23 #
69 # to the student when the "+ C" is left out. You can turn off 69 # to the student when the "+ C" is left out. You can turn off
70 # these messages using the showHints option to the cmp() method: 70 # these messages using the showHints option to the cmp() method:
71 # 71 #
72 # ANS($f->cmp(showHints => 0)); 72 # ANS($f->cmp(showHints => 0));
73 # 73 #
74 # One of the hints is about whether the student's answer is linear
75 # in the arbitrary constant. This test requires differentiating
76 # the student answer. Since there are times when that could be
77 # problematic, you can disable that test via the showLinearityHints
78 # flag. (Note: setting showHints to 0 also disables these hints.)
79 #
80 # ANS($f->cmp(showLinearityHints => 0));
81 #
74 ###################################################################### 82 ######################################################################
75 83
76=cut 84=cut
77 85
78package FormulaUpToConstant; 86package FormulaUpToConstant;
112 # 120 #
113 my $n = $f->D($f->{constant}); 121 my $n = $f->D($f->{constant});
114 Value->Error("Your formula isn't linear in the arbitrary constant '%s'",$f->{constant}) 122 Value->Error("Your formula isn't linear in the arbitrary constant '%s'",$f->{constant})
115 unless $n->isConstant; 123 unless $n->isConstant;
116 # 124 #
117 # Make a version with an adaptive parameter for use in the 125 # Make a version with adaptive parameters for use in the
118 # comparison later on. We could like n0*C, but already have $n 126 # comparison later on. We could like n0*C, but already have $n
119 # copies of C, so remove them. That way, n0 will be 0 when there 127 # copies of C, so remove them. That way, n0 will be 0 when there
120 # are no C's in the student answer during the adaptive comparison. 128 # are no C's in the student answer during the adaptive comparison.
121 # (Again, should really check that n0 is not in use already) 129 # (Again, should really check that n0 is not in use already)
122 # 130 #
123 my $n0 = $context->variables->get("n0"); 131 my $n00 = $context->variables->get("n00");
124 $context->variables->add(n0=>'Parameter') unless $n0 and $n0->{parameter}; 132 $context->variables->add(n00=>'Parameter') unless $n00 and $n00->{parameter};
133 my $n01 = $context->variables->get("n01");
134 $context->variables->add(n01=>'Parameter') unless $n01 and $n01->{parameter};
125 $f->{adapt} = $f + "(n0-$n)$f->{constant}"; 135 $f->{adapt} = $f + "(n00-$n)$f->{constant} + n01";
126 return bless $f, $class; 136 return bless $f, $class;
127} 137}
128 138
129################################################## 139##################################################
130# 140#
152 return -1 unless $l->{adapt} == $r; 162 return -1 unless $l->{adapt} == $r;
153 # 163 #
154 # Check that n0 is non-zero (i.e., there is a multiple of C in the student answer) 164 # Check that n0 is non-zero (i.e., there is a multiple of C in the student answer)
155 # (remember: return value of 0 is equal, and non-zero is unequal) 165 # (remember: return value of 0 is equal, and non-zero is unequal)
156 # 166 #
157 return abs($context->variables->get("n0")->{value}) < $context->flag("zeroLevelTol"); 167 return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol");
158} 168}
169
170##################################################
171#
172# Here we override part of the answer comparison
173# routines in order to be able to generate
174# helpful error messages for students when
175# they leave off the + C.
176#
159 177
160# 178#
161# Show hints by default 179# Show hints by default
162# 180#
163sub cmp_defaults {((shift)->SUPER::cmp_defaults,showHints => 1)}; 181sub cmp_defaults {((shift)->SUPER::cmp_defaults,showHints => 1, showLinearityHints => 1)};
164 182
165# 183#
166# Add useful messages, if the author requested them 184# Add useful messages, if the author requested them
167# 185#
168sub cmp_postprocess { 186sub cmp_postprocess {
169 my $self = shift; my $ans = shift; 187 my $self = shift; my $ans = shift;
170 $self->SUPER::cmp_postprocess($ans); 188 $self->SUPER::cmp_postprocess($ans);
171 return unless $ans->{score} == 0 && !$ans->{isPreview}; 189 return unless $ans->{score} == 0 && !$ans->{isPreview};
172 return if $ans->{ans_message} || !$self->getFlag("showHints"); 190 return if $ans->{ans_message} || !$self->getFlag("showHints");
191 my $student = $ans->{student_value};
173 my $result = $ans->{correct_value} <=> $ans->{student_value}; # compare encodes the reason in the result 192 my $result = $ans->{correct_value} <=> $student; # compare encodes the reason in the result
174 $self->cmp_Error($ans,"Note: there is always more than one posibility") if $result == 2 || $result == 3; 193 $self->cmp_Error($ans,"Note: there is always more than one posibility") if $result == 2 || $result == 3;
194 if ($result == 3) {
195 $self->context->flags->set(no_parameters=>0);
196 $result = 1 if $self->removeConstant+"n01+n00x" == $student+"x"; # must use both parameters
197 $self->context->flags->set(no_parameters=>1);
198 }
175 $self->cmp_Error($ans,"Your answer is not the most general solution") 199 $self->cmp_Error($ans,"Your answer is not the most general solution") if $result == 1;
176 if $result == 1 || ($result == 3 && $self->removeConstant == $ans->{student_value}); 200 $self->cmp_Error($ans,"Your formula should be linear in the constant '$student->{constant}'")
201 if $result == -1 && $self->getFlag("showLinearityHints") && !$student->D($student->{constant})->isConstant;
177} 202}
178 203
204##################################################
179# 205#
180# Get the name of the constant 206# Get the name of the constant
181# 207#
182sub constant {(shift)->{constant}} 208sub constant {(shift)->{constant}}
183 209
237 # Do the usual Variable stuff. 263 # Do the usual Variable stuff.
238 # 264 #
239 $self->SUPER::new($equation,$name,$ref); 265 $self->SUPER::new($equation,$name,$ref);
240} 266}
241 267
242
2431; 2681;

Legend:
Removed from v.5394  
changed lines
  Added in v.5461

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9