[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 5440 Revision 5914
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.17 2008/09/16 03:01:17 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