parserFormulaUpToConstant.pl - implements formulas "plus a constant".
This file implements the FormulaUpToConstant
object, which is a formula that is only unique up to a constant (i.e., this is an anti-derivative). Students must include the "+C" as part of their answers, but they can use any (single-letter) constant that they want, and it doesn't have to be the one the professor used.
To use FormulaUpToConstant
objects, load this macro file at the top of your problem:
loadMacros("parserFormulaUpToConstant.pl");
then create a formula with constant as follows:
$f = FormulaUpToConstant("sin(x)+C");
Note that the C should NOT already be a variable in the Context; the FormulaUpToConstant
object will handle adding it in for you. If you don't include a constant in your formula (i.e., if all the variables that you used are already in your Context, then the FormulaUpToConstant object will add "+C" for you.
The FormulaUpToConstant
should work like any normal Formula, and in particular, you use $f->cmp to get its answer checker.
ANS($f->cmp);
Note that the FormulaUpToConstant
object creates its own private copy of the current Context (so that it can add variables without affecting the rest of the problem). You should not notice this in general, but if you need to access that context, use $f->{context}. E.g.
Context($f->{context});
would make the current context the one being used by the FormulaUpToConstant
, while
$f->{context}->variables->names
would return a list of the variables in the private context.
To get the name of the constant in use in the formula, use
$f->constant
If you combine a FormulaUpToConstant
with other formulas, the result will be a new FormulaUpToConstant
object, with a new Context, and potentially a new + C added to it. This is likely not what you want. Instead, you should convert back to a Formula first, then combine with other objects, then convert back to a FormulaUpToConstant
, if necessary. To do this, use the removeConstant() method:
$f = FormulaUpToConstant("sin(x)+C");
$g = Formula("cos(x)");
$h = $f->removeConstant + $g; # $h will be "sin(x)+cos(x)"
$h = FormulaUpToConstant($h); # $h will be "sin(x)+cos(x)+C"
The answer evaluator by default will give "helpful" messages to the student when the "+ C" is left out. You can turn off these messages using the showHints option to the cmp() method:
ANS($f->cmp(showHints => 0));
One of the hints is about whether the student's answer is linear in the arbitrary constant. This test requires differentiating the student answer. Since there are times when that could be problematic, you can disable that test via the showLinearityHints flag. (Note: setting showHints to 0 also disables these hints.)
ANS($f->cmp(showLinearityHints => 0));