[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 5461 Revision 5917
1################################################################################
2# WeBWorK Online Homework Delivery System
3# Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4# $CVSHeader: pg/macros/parserFormulaUpToConstant.pl,v 1.18 2008/09/16 03:23:54 dpvc Exp $
5#
6# This program is free software; you can redistribute it and/or modify it under
7# the terms of either: (a) the GNU General Public License as published by the
8# Free Software Foundation; either version 2, or (at your option) any later
9# version, or (b) the "Artistic License" which comes with this package.
10#
11# This program is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13# FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14# Artistic License for more details.
15################################################################################
16
17=head1 NAME
18
19parserFormulaUpToConstant.pl - implements formulas "plus a constant".
20
21=head1 DESCRIPTION
22
23This file implements the FormulaUpToConstant object, which is
24a formula that is only unique up to a constant (i.e., this is
25an anti-derivative). Students must include the "+C" as part of
26their answers, but they can use any (single-letter) constant that
27they want, and it doesn't have to be the one the professor used.
28
29To use FormulaWithConstat objects, load this macro file at the
30top of your problem:
31
32 loadMacros("parserFormulaUpToConstant.pl");
33
34then create a formula with constant as follows:
35
36 $f = FormulaUpToConstant("sin(x)+C");
37
38Note that the C should NOT already be a variable in the Context;
39the FormulaUpToConstant object will handle adding it in for
40you. If you don't include a constant in your formula (i.e., if
41all the variables that you used are already in your Context,
42then the FormulaUpToConstant object will add "+C" for you.
43
44The FormulaUpToConstant should work like any normal Formula,
45and in particular, you use $f->cmp to get its answer checker.
46
47 ANS($f->cmp);
48
49Note that the FormulaUpToConstant object creates its only private
50copy of the current Context (so that it can add variables without
51affecting the rest of the problem). You should not notice this
52in general, but if you need to access that context, use $f->{context}.
53E.g.
54
55 Context($f->{context});
56
57would make the current context the one being used by the
58FormulaUpToConstant, while
59
60 $f->{context}->variables->names
61
62would return a list of the variables in the private context.
63
64To get the name of the constant in use in the formula,
65use
66
67 $f->constant.
68
69If you combine a FormulaUpToConstant with other formulas,
70the result will be a new FormulaUpToConstant object, with
71a new Context, and potentially a new + C added to it. This
72is likely not what you want. Instead, you should convert
73back to a Formula first, then combine with other objects,
74then convert back to a FormulaUpToConstant, if necessary.
75To do this, use the removeConstant() method:
76
77 $f = FormulaUpToConstant("sin(x)+C");
78 $g = Formula("cos(x)");
79 $h = $f->removeConstant + $g; # $h will be "sin(x)+cos(x)"
80 $h = FormulaUpToConstant($h); # $h will be "sin(x)+cos(x)+C"
81
82The answer evaluator by default will give "helpful" messages
83to the student when the "+ C" is left out. You can turn off
84these messages using the showHints option to the cmp() method:
85
86 ANS($f->cmp(showHints => 0));
87
88One of the hints is about whether the student's answer is linear
89in the arbitrary constant. This test requires differentiating
90the student answer. Since there are times when that could be
91problematic, you can disable that test via the showLinearityHints
92flag. (Note: setting showHints to 0 also disables these hints.)
93
94 ANS($f->cmp(showLinearityHints => 0));
95
96=cut
97
1loadMacros("MathObjects.pl"); 98loadMacros("MathObjects.pl");
2 99
3sub _parserFormulaUpToConstant_init {FormulaUpToConstant::Init()} 100sub _parserFormulaUpToConstant_init {FormulaUpToConstant::Init()}
4
5=head1 FormulaUpToConstant();
6
7 ######################################################################
8 #
9 # This file implements the FormulaUpToConstant object, which is
10 # a formula that is only unique up to a constant (i.e., this is
11 # an anti-derivative). Students must include the "+C" as part of
12 # their answers, but they can use any (single-letter) constant that
13 # they want, and it doesn't have to be the one the professor used.
14 #
15 # To use FormulaWithConstat objects, load this macro file at the
16 # top of your problem:
17 #
18 # loadMacros("parserFormulaUpToConstant.pl");
19 #
20 # then create a formula with constant as follows:
21 #
22 # $f = FormulaUpToConstant("sin(x)+C");
23 #
24 # Note that the C should NOT already be a variable in the Context;
25 # the FormulaUpToConstant object will handle adding it in for
26 # you. If you don't include a constant in your formula (i.e., if
27 # all the variables that you used are already in your Context,
28 # then the FormulaUpToConstant object will add "+C" for you.
29 #
30 # The FormulaUpToConstant should work like any normal Formula,
31 # and in particular, you use $f->cmp to get its answer checker.
32 #
33 # ANS($f->cmp);
34 #
35 # Note that the FormulaUpToConstant object creates its only private
36 # copy of the current Context (so that it can add variables without
37 # affecting the rest of the problem). You should not notice this
38 # in general, but if you need to access that context, use $f->{context}.
39 # E.g.
40 #
41 # Context($f->{context});
42 #
43 # would make the current context the one being used by the
44 # FormulaUpToConstant, while
45 #
46 # $f->{context}->variables->names
47 #
48 # would return a list of the variables in the private context.
49 #
50 # To get the name of the constant in use in the formula,
51 # use
52 #
53 # $f->constant.
54 #
55 # If you combine a FormulaUpToConstant with other formulas,
56 # the result will be a new FormulaUpToConstant object, with
57 # a new Context, and potentially a new + C added to it. This
58 # is likely not what you want. Instead, you should convert
59 # back to a Formula first, then combine with other objects,
60 # then convert back to a FormulaUpToConstant, if necessary.
61 # To do this, use the removeConstant() method:
62 #
63 # $f = FormulaUpToConstant("sin(x)+C");
64 # $g = Formula("cos(x)");
65 # $h = $f->removeConstant + $g; # $h will be "sin(x)+cos(x)"
66 # $h = FormulaUpToConstant($h); # $h will be "sin(x)+cos(x)+C"
67 #
68 # The answer evaluator by default will give "helpful" messages
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:
71 #
72 # ANS($f->cmp(showHints => 0));
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 #
82 ######################################################################
83
84=cut
85 101
86package FormulaUpToConstant; 102package FormulaUpToConstant;
87@ISA = ('Value::Formula'); 103@ISA = ('Value::Formula');
88 104
89sub Init { 105sub Init {
131 my $n00 = $context->variables->get("n00"); 147 my $n00 = $context->variables->get("n00");
132 $context->variables->add(n00=>'Parameter') unless $n00 and $n00->{parameter}; 148 $context->variables->add(n00=>'Parameter') unless $n00 and $n00->{parameter};
133 my $n01 = $context->variables->get("n01"); 149 my $n01 = $context->variables->get("n01");
134 $context->variables->add(n01=>'Parameter') unless $n01 and $n01->{parameter}; 150 $context->variables->add(n01=>'Parameter') unless $n01 and $n01->{parameter};
135 $f->{adapt} = $f + "(n00-$n)$f->{constant} + n01"; 151 $f->{adapt} = $f + "(n00-$n)$f->{constant} + n01";
152
136 return bless $f, $class; 153 return bless $f, $class;
137} 154}
138 155
139################################################## 156##################################################
140# 157#
157 # 174 #
158 $r = $r->substitute($r->{constant}=>$l->{constant}) unless $r->{constant} eq $l->{constant}; 175 $r = $r->substitute($r->{constant}=>$l->{constant}) unless $r->{constant} eq $l->{constant};
159 # 176 #
160 # Compare with adaptive parameters to see if $l + n0 C = $r for some n0. 177 # Compare with adaptive parameters to see if $l + n0 C = $r for some n0.
161 # 178 #
162 return -1 unless $l->{adapt} == $r; 179 my $adapt = $l->adapt;
180 my $equal = $adapt->cmp_compare($r,{});
181 $self->{adapt} = $self->{adapt}->inherit($adapt); # save the adapted value's flags
182 return -1 unless $equal;
163 # 183 #
164 # Check that n0 is non-zero (i.e., there is a multiple of C in the student answer) 184 # Check that n0 is non-zero (i.e., there is a multiple of C in the student answer)
165 # (remember: return value of 0 is equal, and non-zero is unequal) 185 # (remember: return value of 0 is equal, and non-zero is unequal)
166 # 186 #
167 return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol"); 187 return abs($context->variables->get("n00")->{value}) < $context->flag("zeroLevelTol");
188}
189
190#
191# Return the {adapt} formula with test points adjusted
192#
193sub adapt {
194 my $self = shift;
195 my $adapt = $self->{adapt}->inherit($self); delete $adapt->{adapt};
196 return $self->adjustInherit($self->{adapt});
197}
198
199#
200# Inherit from the main FormulaUpToConstant, but
201# adjust the test points to include the constants
202#
203sub adjustInherit {
204 my $self = shift;
205 my $f = shift->inherit($self);
206 delete $f->{adapt}; delete $f->{constant};
207 foreach my $id ('test_points','test_at') {
208 if (defined $f->{$id}) {
209 $f->{$id} = [$f->{$id}->value] if Value::isValue($f->{$id});
210 $f->{$id} = [$f->{$id}] unless ref($f->{$id}) eq 'ARRAY';
211 $f->{$id} = [map {
212 (Value::isValue($_) ? [$_->value] :
213 (ref($_) eq 'ARRAY'? $_ : [$_]))
214 } @{$f->{$id}}];
215 $f->{$id} = $self->addConstants($f->{$id});
216 }
217 }
218 return $f;
219}
220
221#
222# Insert dummy values for the constants for the test points
223# (These are supposed to be +C, so the value shouldn't matter?)
224#
225sub addConstants {
226 my $self = shift; my $points = shift;
227 my @names = $self->context->variables->variables;
228 my $variables = $self->context->{variables};
229 my $Points = [];
230 foreach my $p (@{$points}) {
231 if (scalar(@{$p}) == scalar(@names)) {
232 push (@{$Points},$p);
233 } else {
234 my @P = (.1) x scalar(@names); my $j = 0;
235 foreach my $i (0..scalar(@names)-1) {
236 if (!$variables->{$names[$i]}{arbitraryConstant}) {
237 $P[$i] = $p->[$j] if defined $p->[$j]; $j++;
238 }
239 }
240 push (@{$Points}, \@P);
241 }
242 }
243 return $Points;
168} 244}
169 245
170################################################## 246##################################################
171# 247#
172# Here we override part of the answer comparison 248# Here we override part of the answer comparison
179# Show hints by default 255# Show hints by default
180# 256#
181sub cmp_defaults {((shift)->SUPER::cmp_defaults,showHints => 1, showLinearityHints => 1)}; 257sub cmp_defaults {((shift)->SUPER::cmp_defaults,showHints => 1, showLinearityHints => 1)};
182 258
183# 259#
260# Provide diagnostics based on the adapted function used to check
261# the student's answer
262#
263sub cmp_diagnostics {
264 my $self = shift;
265 $self->inherit($self->{adapt})->SUPER::cmp_diagnostics(@_);
266}
267
268#
269# Make it possible to graph single-variable formulas by setting
270# the arbitrary constants to 0 first.
271#
272sub cmp_graph {
273 my $self = shift; my $diagnostics = shift;
274 my $F1 = shift; my $F2; ($F1,$F2) = @{$F1} if (ref($F1) eq 'ARRAY');
275 my %subs; my $context = $self->context;
276 foreach my $v ($context->variables->variables)
277 {$subs{$v} = 0 if ($context->variables->get($v)->{arbitraryConstant})}
278 $F1 = $F1->inherit($F1->{adapt})->substitute(%subs)->reduce;
279 $F2 = $F2->inherit($F2->{adapt})->substitute(%subs)->reduce;
280 $self->SUPER::cmp_graph($diagnostics,[$F1,$F2]);
281}
282
283#
184# Add useful messages, if the author requested them 284# Add useful messages, if the author requested them
185# 285#
186sub cmp_postprocess { 286sub cmp_postprocess {
187 my $self = shift; my $ans = shift; 287 my $self = shift; my $ans = shift;
188 $self->SUPER::cmp_postprocess($ans); 288 $self->SUPER::cmp_postprocess($ans,@_);
189 return unless $ans->{score} == 0 && !$ans->{isPreview}; 289 return unless $ans->{score} == 0 && !$ans->{isPreview};
190 return if $ans->{ans_message} || !$self->getFlag("showHints"); 290 return if $ans->{ans_message} || !$self->getFlag("showHints");
191 my $student = $ans->{student_value}; 291 my $student = $ans->{student_value};
192 my $result = $ans->{correct_value} <=> $student; # compare encodes the reason in the result 292 $main::{_cmp_} = sub {return $ans->{correct_value} <=> $student}; # compare encodes the reason in the result
293 my $result = main::PG_restricted_eval('&{$main::{_cmp_}}');
294 delete $main::{_cmp_};
193 $self->cmp_Error($ans,"Note: there is always more than one posibility") if $result == 2 || $result == 3; 295 $self->cmp_Error($ans,"Note: there is always more than one posibility") if $result == 2 || $result == 3;
194 if ($result == 3) { 296 if ($result == 3) {
297 my $context = $self->context;
195 $self->context->flags->set(no_parameters=>0); 298 $context->flags->set(no_parameters=>0);
299 $context->variables->add(x00=>'Real');
196 $result = 1 if $self->removeConstant+"n01+n00x" == $student+"x"; # must use both parameters 300 my $correct = $self->removeConstant+"n01+n00x00"; # must use both parameters
301 $result = 1 if $correct->cmp_compare($student+"x00",{});
302 $context->variables->remove('x00');
197 $self->context->flags->set(no_parameters=>1); 303 $context->flags->set(no_parameters=>1);
198 } 304 }
199 $self->cmp_Error($ans,"Your answer is not the most general solution") if $result == 1; 305 $self->cmp_Error($ans,"Your answer is not the most general solution") if $result == 1;
200 $self->cmp_Error($ans,"Your formula should be linear in the constant '$student->{constant}'") 306 $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; 307 if $result == -1 && $self->getFlag("showLinearityHints") && !$student->D($student->{constant})->isConstant;
202} 308}
210# 316#
211# Remove the constant and return a Formula object 317# Remove the constant and return a Formula object
212# 318#
213sub removeConstant { 319sub removeConstant {
214 my $self = shift; 320 my $self = shift;
215 main::Formula($self->substitute($self->{constant}=>0))->reduce; 321 return $self->adjustInherit(main::Formula($self->substitute($self->{constant}=>0))->reduce);
216} 322}
217 323
218# 324#
219# Override the differentiation so that we always return 325# Override the differentiation so that we always return
220# a Formula, not a FormulaUpToConstant (we don't want to 326# a Formula, not a FormulaUpToConstant (we don't want to

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9