Difference between revisions of "DisableFunctions"
Line 58: | Line 58: | ||
<li> <code>LimitedVector-ijk</code>, which requires ijk-format. </li> |
<li> <code>LimitedVector-ijk</code>, which requires ijk-format. </li> |
||
</ul> |
</ul> |
||
− | |||
+ | <p> |
||
− | It is possible to restrict what constants may be raised to powers in the <b>current</b> context. There are several available restrictions, only one of which may be selected: |
+ | It is possible to restrict what constants may be raised to powers in the <b>current</b> context (i.e., there is no LimitedPowers context). There are several available restrictions, only one of which may be selected: |
+ | </p> |
||
<ul> |
<ul> |
||
<li> <code>LimitedPowers::NoBaseE()</code>, which doesn't allow ''e'' to be raised to a power, </li> |
<li> <code>LimitedPowers::NoBaseE()</code>, which doesn't allow ''e'' to be raised to a power, </li> |
Revision as of 21:09, 12 March 2010
Disabling Functions and Operators in Student Answers: PG Code Snippet
This code snippet shows the essential PG code to Disabling Functions in Student Answers. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.
In general, this can be done in a number of ways: there are a number of available limited contexts that limit the types of answers that students may enter, or, alternately, it is possible to specifically indicate within the context what functions or operations are not allowed. In the following, we first consider the available contexts, and, below that, show how to specifically disallow some functions.
PG problem file | Explanation |
---|---|
loadMacros("contextLimitedPolynomial.pl"); |
In the initialization section of the file, we need to load the Context that we're using. This will be |
Context("LimitedPolynomial"); # to require that only one monomial of each # degree be included in the polynomial, we # would also set that flag in the Context: # Context()->flags->set(singlePowers=>1); $ans = Compute("x^2 + 3x + 2"); |
There are no additions required in the tagging and description or initialization sections of the problem file. In the problem set-up section, we specify the
There are a number of limited contexts like
It is possible to restrict what constants may be raised to powers in the current context (i.e., there is no LimitedPowers context). There are several available restrictions, only one of which may be selected:
|
BEGIN_TEXT Expand the polynomial: $BR \((x+1)(x+2) = \) \{ ans_rule(25) \} END_TEXT |
No changes are required in the text section of the problem file. |
ANS( $ans->cmp() ); |
And in the answer and solution section of the file, we check the answer as usual. |
Alternately, we may find that we need to disable specific functions or operations in student answers. This can be done manually within an existing Context, as shown in the following snippet.
PG problem file | Explanation |
---|---|
Context("Numeric"); ## to disable specific operations in ## student answers, use the undefine ## method for the operations: Context()->operators->undefine("^","**"); ## we can similarly disable specific ## functions with Context()->functions->undefine("sin","cos","tan","sqrt"); $ans = Compute("1/2"); # To disallow absolute value, disable abs(), # sqrt and exponentiation (for sqrt(x^2) and (x^4)^(1/4)), and # the parentheses | |, and give consistent # error messages Context()->functions->disable("abs","sqrt"); Context()->operators->undefine("^","**"); Context()->parens->remove("|"); Context()->{error}{convert} = sub { my $message = shift; $message =~ s/Unexpected character '~~|'/Absolute value is not allowed/; return $message; }; |
No changes are necessary in the documentation and tagging section of the problem, or in the initialization section. In the problem set-up section, we declare a context (here, we've chosen the
We can disable specific operations in the Context: in general, predefined operations are
After disabling the operation, they can be re-enabled with
To disable specific functions in the Context, we similarly
In addition, classes of functions can be disabled with
Alternatively, we could use the following syntax. Parser::Context::Functions::Disable('All'); |
BEGIN_TEXT Find the numerical value: \( sin^2(\pi/4) = \) \{ ans_rule(15) \} END_TEXT |
We don't need to make any changes to the text portion of the problem file. |
ANS( $ans->cmp() ); |
Nor in the answer and solution section. |
- POD documentation: contextLimitedPowers.pl.html
- PG macro: contextLimitedPowers.pl