Parent Directory | Revision Log | 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 | |||

19 | parserFormulaUpToConstant.pl - implements formulas "plus a constant". | ||

20 | |||

21 | =head1 DESCRIPTION | ||

22 | |||

23 | This file implements the FormulaUpToConstant object, which is | ||

24 | a formula that is only unique up to a constant (i.e., this is | ||

25 | an anti-derivative). Students must include the "+C" as part of | ||

26 | their answers, but they can use any (single-letter) constant that | ||

27 | they want, and it doesn't have to be the one the professor used. | ||

28 | |||

29 | To use FormulaWithConstat objects, load this macro file at the | ||

30 | top of your problem: | ||

31 | |||

32 | loadMacros("parserFormulaUpToConstant.pl"); | ||

33 | |||

34 | then create a formula with constant as follows: | ||

35 | |||

36 | $f = FormulaUpToConstant("sin(x)+C"); | ||

37 | |||

38 | Note that the C should NOT already be a variable in the Context; | ||

39 | the FormulaUpToConstant object will handle adding it in for | ||

40 | you. If you don't include a constant in your formula (i.e., if | ||

41 | all the variables that you used are already in your Context, | ||

42 | then the FormulaUpToConstant object will add "+C" for you. | ||

43 | |||

44 | The FormulaUpToConstant should work like any normal Formula, | ||

45 | and in particular, you use $f->cmp to get its answer checker. | ||

46 | |||

47 | ANS($f->cmp); | ||

48 | |||

49 | Note that the FormulaUpToConstant object creates its only private | ||

50 | copy of the current Context (so that it can add variables without | ||

51 | affecting the rest of the problem). You should not notice this | ||

52 | in general, but if you need to access that context, use $f->{context}. | ||

53 | E.g. | ||

54 | |||

55 | Context($f->{context}); | ||

56 | |||

57 | would make the current context the one being used by the | ||

58 | FormulaUpToConstant, while | ||

59 | |||

60 | $f->{context}->variables->names | ||

61 | |||

62 | would return a list of the variables in the private context. | ||

63 | |||

64 | To get the name of the constant in use in the formula, | ||

65 | use | ||

66 | |||

67 | $f->constant. | ||

68 | |||

69 | If you combine a FormulaUpToConstant with other formulas, | ||

70 | the result will be a new FormulaUpToConstant object, with | ||

71 | a new Context, and potentially a new + C added to it. This | ||

72 | is likely not what you want. Instead, you should convert | ||

73 | back to a Formula first, then combine with other objects, | ||

74 | then convert back to a FormulaUpToConstant, if necessary. | ||

75 | To 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 | |||

82 | The answer evaluator by default will give "helpful" messages | ||

83 | to the student when the "+ C" is left out. You can turn off | ||

84 | these messages using the showHints option to the cmp() method: | ||

85 | |||

86 | ANS($f->cmp(showHints => 0)); | ||

87 | |||

88 | One of the hints is about whether the student's answer is linear | ||

89 | in the arbitrary constant. This test requires differentiating | ||

90 | the student answer. Since there are times when that could be | ||

91 | problematic, you can disable that test via the showLinearityHints | ||

92 | flag. (Note: setting showHints to 0 also disables these hints.) | ||

93 | |||

94 |