[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 5554 Revision 5555
11they want, and it doesn't have to be the one the professor used. 11they want, and it doesn't have to be the one the professor used.
12 12
13To use FormulaWithConstat objects, load this macro file at the 13To use FormulaWithConstat objects, load this macro file at the
14top of your problem: 14top of your problem:
15 15
16 loadMacros("parserFormulaUpToConstant.pl"); 16 loadMacros("parserFormulaUpToConstant.pl");
17 17
18then create a formula with constant as follows: 18then create a formula with constant as follows:
19 19
20 $f = FormulaUpToConstant("sin(x)+C"); 20 $f = FormulaUpToConstant("sin(x)+C");
21 21
22Note that the C should NOT already be a variable in the Context; 22Note that the C should NOT already be a variable in the Context;
23the FormulaUpToConstant object will handle adding it in for 23the FormulaUpToConstant object will handle adding it in for
24you. If you don't include a constant in your formula (i.e., if 24you. If you don't include a constant in your formula (i.e., if
25all the variables that you used are already in your Context, 25all the variables that you used are already in your Context,
26then the FormulaUpToConstant object will add "+C" for you. 26then the FormulaUpToConstant object will add "+C" for you.
27 27
28The FormulaUpToConstant should work like any normal Formula, 28The FormulaUpToConstant should work like any normal Formula,
29and in particular, you use $f->cmp to get its answer checker. 29and in particular, you use $f->cmp to get its answer checker.
30 30
31 ANS($f->cmp); 31 ANS($f->cmp);
32 32
33Note that the FormulaUpToConstant object creates its only private 33Note that the FormulaUpToConstant object creates its only private
34copy of the current Context (so that it can add variables without 34copy of the current Context (so that it can add variables without
35affecting the rest of the problem). You should not notice this 35affecting the rest of the problem). You should not notice this
36in general, but if you need to access that context, use $f->{context}. 36in general, but if you need to access that context, use $f->{context}.
37E.g. 37E.g.
38 38
39 Context($f->{context}); 39 Context($f->{context});
40 40
41would make the current context the one being used by the 41would make the current context the one being used by the
42FormulaUpToConstant, while 42FormulaUpToConstant, while
43 43
44 $f->{context}->variables->names 44 $f->{context}->variables->names
45 45
46would return a list of the variables in the private context. 46would return a list of the variables in the private context.
47 47
48To get the name of the constant in use in the formula, 48To get the name of the constant in use in the formula,
49use 49use
50 50
51 $f->constant. 51 $f->constant.
52 52
53If you combine a FormulaUpToConstant with other formulas, 53If you combine a FormulaUpToConstant with other formulas,
54the result will be a new FormulaUpToConstant object, with 54the result will be a new FormulaUpToConstant object, with
55a new Context, and potentially a new + C added to it. This 55a new Context, and potentially a new + C added to it. This
56is likely not what you want. Instead, you should convert 56is likely not what you want. Instead, you should convert
57back to a Formula first, then combine with other objects, 57back to a Formula first, then combine with other objects,
58then convert back to a FormulaUpToConstant, if necessary. 58then convert back to a FormulaUpToConstant, if necessary.
59To do this, use the removeConstant() method: 59To do this, use the removeConstant() method:
60 60
61 $f = FormulaUpToConstant("sin(x)+C"); 61 $f = FormulaUpToConstant("sin(x)+C");
62 $g = Formula("cos(x)"); 62 $g = Formula("cos(x)");
63 $h = $f->removeConstant + $g; # $h will be "sin(x)+cos(x)" 63 $h = $f->removeConstant + $g; # $h will be "sin(x)+cos(x)"
64 $h = FormulaUpToConstant($h); # $h will be "sin(x)+cos(x)+C" 64 $h = FormulaUpToConstant($h); # $h will be "sin(x)+cos(x)+C"
65 65
66The answer evaluator by default will give "helpful" messages 66The answer evaluator by default will give "helpful" messages
67to the student when the "+ C" is left out. You can turn off 67to the student when the "+ C" is left out. You can turn off
68these messages using the showHints option to the cmp() method: 68these messages using the showHints option to the cmp() method:
69 69
70 ANS($f->cmp(showHints => 0)); 70 ANS($f->cmp(showHints => 0));
71 71
72One of the hints is about whether the student's answer is linear 72One of the hints is about whether the student's answer is linear
73in the arbitrary constant. This test requires differentiating 73in the arbitrary constant. This test requires differentiating
74the student answer. Since there are times when that could be 74the student answer. Since there are times when that could be
75problematic, you can disable that test via the showLinearityHints 75problematic, you can disable that test via the showLinearityHints
76flag. (Note: setting showHints to 0 also disables these hints.) 76flag. (Note: setting showHints to 0 also disables these hints.)
77 77
78 ANS($f->cmp(showLinearityHints => 0)); 78 ANS($f->cmp(showLinearityHints => 0));
79 79
80=cut 80=cut
81 81
82loadMacros("MathObjects.pl"); 82loadMacros("MathObjects.pl");
83 83

Legend:
Removed from v.5554  
changed lines
  Added in v.5555

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9