Difference between revisions of "Answer Checkers and the Context"

From WeBWorK_wiki
Jump to navigation Jump to search
(Make link back to answer checkers)
(→‎Disabling Operators: -- replaced "operations" with "operators" which is the correct name of the method)
 
(5 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
Context()->functions->disable('sin','cos','tan');
 
Context()->functions->disable('sin','cos','tan');
   
would remove those three functions from use. One would need to remove <code>cot</code>, <code>sec</code>, <code>csc</code>, <code>arcsin</code>, <code>asin</code>, etc., to do this properly, however. It is also possible to remove whole categories of functions once, e.g.
+
would remove those three functions from use. One would need to remove <code>cot</code>, <code>sec</code>, <code>csc</code>, <code>arcsin</code>, <code>asin</code>, etc., to do this properly, however. It is also possible to remove whole categories of functions at once, e.g.
   
 
Context()->functions->disable('Trig');
 
Context()->functions->disable('Trig');
Line 14: Line 14:
 
Context()->functions->enable('sqrt');
 
Context()->functions->enable('sqrt');
   
would allow only the <code>sqrt</code> function to be used in student answers. The available categories are the following:
+
would allow only the <code>sqrt</code> function to be used in student answers. The list of categories is available in the [[Context Function Categories]] list.
 
{| class="wikitable"
 
! Category !! Functions Included
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>SimpleTrig</code>
 
| style="padding: 5px" | <code>sin</code>, <code>cos</code>, <code>tan</code>, <code>sec</code>, <code>csc</code>, <code>cot</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>InverseTrig</code>
 
| style="padding: 5px" | <code>asin</code>, <code>acos</code>, <code>atan</code>, <code>asec</code>, <code>acsc</code>, <code>acot</code>, <code>arcsin</code>, <code>arccos</code>, <code>arctan</code>, <code>arcsec</code>, <code>arccsc</code>, <code>arccot</code>, <code>atan2</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>SimpleHyperbolic</code>
 
| style="padding: 5px" | <code>sinh</code>, <code>cosh</code>, <code>tanh</code>, <code>sech</code>, <code>csch</code>, <code>coth</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>InverseHyperbolic</code>
 
| style="padding: 5px" | <code>asinh</code>, <code>acosh</code>, <code>atanh</code>, <code>asech</code>, <code>acsch</code>, <code>acoth</code>, <code>arcsinh</code>, <code>arccosh</code>, <code>arctanh</code>, <code>arcsech</code>, <code>arccsch</code>, <code>arccoth</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>Numeric</code>
 
| style="padding: 5px" | <code>log</code>, <code>log10</code>, <code>exp</code>, <code>sqrt</code>, <code>abs</code>, <code>int</code>, <code>sgn</code>, <code>ln</code>, <code>logten</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>Vector</code>
 
| style="padding: 5px" | <code>norm</code>, <code>unit</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>Complex</code>
 
| style="padding: 5px" | <code>arg</code>, <code>mod</code>, <code>Re</code>, <code>Im</code>, <code>conj</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>Hyperbolic</code>
 
| style="padding: 5px" | all of <code>SimpleHyperbolic</code> and <code>InverseHyperbolic</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>Trig</code>
 
| style="padding: 5px" | all of <code>SimpleTrig</code>, <code>InverseTrig</code>, and <code>Hyperbolic</code>
 
|- style="vertical-align:top"
 
| style="padding: 5px" | <code>All</code>
 
| style="padding: 5px" | all of <code>Trig</code>, <code>Numeric</code>, <code>Vector</code>, and <code>Complex</code>
 
|}
 
   
 
Note that some functions can be obtained via operators (e.g., <code>abs(x)</code> can be obtained via <code>|x|</code> and <code>sqrt(x)</code> can be obtained by
 
Note that some functions can be obtained via operators (e.g., <code>abs(x)</code> can be obtained via <code>|x|</code> and <code>sqrt(x)</code> can be obtained by
Line 22: Line 22:
 
== Disabling Operators ==
 
== Disabling Operators ==
   
Which arithmetic operations are available is controlled through <code>Context()->operations</code>. For example,
+
Which arithmetic operations are available is controlled through <code>Context()->operators</code>. For example,
   
Context()->operations->undefine('^','**');
+
Context()->operators->undefine('^','**');
   
 
would disable the ability for students to enter powers. Note that multiplication and division have several forms (in order to make a non-standard precedence that allows things like <code>sin(2x)</code> to be entered as <code>sin 2x</code>). So if you want to disable them you need to include all of them. E.g.,
 
would disable the ability for students to enter powers. Note that multiplication and division have several forms (in order to make a non-standard precedence that allows things like <code>sin(2x)</code> to be entered as <code>sin 2x</code>). So if you want to disable them you need to include all of them. E.g.,
   
Context()->operations->undefine('*',' *','* ');
+
Context()->operators->undefine('*',' *','* ');
Context()->operations->undefine('/',' /','/ ','//');
+
Context()->operators->undefine('/',' /','/ ','//');
   
would be required in order to make multiplication and division unavailable.
+
would be required in order to make multiplication and division unavailable. See the [[Context Operator Table]] for more details.
   
 
Finally, absolute values are treated as a specialized form of parenthesis, so to remove them, use
 
Finally, absolute values are treated as a specialized form of parenthesis, so to remove them, use
Line 38: Line 38:
   
 
The [https://github.com/openwebwork/pg/tree/macros pg/macros/] directory contains a number of predefined contexts that limit the operations that can be performed in a student answer. For example, the <code>contextLimitedNumeric.pl</code> file defines contexts in which students can enter numbers, but no operations, so they would have to reduce their answer to a single number by hand. There are limited contexts for complex numbers, points, and vectors, and there are also specialized contexts for entering polynomials, or where powers are restricted in various ways.
 
The [https://github.com/openwebwork/pg/tree/macros pg/macros/] directory contains a number of predefined contexts that limit the operations that can be performed in a student answer. For example, the <code>contextLimitedNumeric.pl</code> file defines contexts in which students can enter numbers, but no operations, so they would have to reduce their answer to a single number by hand. There are limited contexts for complex numbers, points, and vectors, and there are also specialized contexts for entering polynomials, or where powers are restricted in various ways.
 
   
 
== Tolerances and Limits ==
 
== Tolerances and Limits ==
   
The tolerances used in comparing numbers are part of the Context aswell. You can set these via:
+
The tolerances used in comparing numbers are part of the Context as well. You can set these via:
   
 
Context()->flags->set(
 
Context()->flags->set(
Line 87: Line 86:
   
 
ANS($f->cmp(limits=>[10,12]));
 
ANS($f->cmp(limits=>[10,12]));
  +
  +
== See Also ==
  +
  +
* [[Context Function Categories]]
  +
* [[Context Operator Table]]
  +
* [[Answer Checkers (MathObjects)| MathObject Answer Checkers]]
   
 
<br>
 
<br>
   
  +
[[Category:Contexts]]
 
[[Category:MathObjects]]
 
[[Category:MathObjects]]

Latest revision as of 15:55, 23 February 2019

Disabling Functions

Some things, like whether trigonometric functions are allowed in the answer, are controlled through the Context() rather than through the MathObjects answer checker itself. For example,

   Context()->functions->disable('sin','cos','tan');

would remove those three functions from use. One would need to remove cot, sec, csc, arcsin, asin, etc., to do this properly, however. It is also possible to remove whole categories of functions at once, e.g.

   Context()->functions->disable('Trig');

would disable all trig functions, while

   Context()->functions->disable('All');
   Context()->functions->enable('sqrt');

would allow only the sqrt function to be used in student answers. The list of categories is available in the Context Function Categories list.

Note that some functions can be obtained via operators (e.g., abs(x) can be obtained via |x| and sqrt(x) can be obtained by x^(1/2) or x^.5, so you might need to remove more than just the named functions to limit these operations).


Disabling Operators

Which arithmetic operations are available is controlled through Context()->operators. For example,

   Context()->operators->undefine('^','**');

would disable the ability for students to enter powers. Note that multiplication and division have several forms (in order to make a non-standard precedence that allows things like sin(2x) to be entered as sin 2x). So if you want to disable them you need to include all of them. E.g.,

   Context()->operators->undefine('*',' *','* ');
   Context()->operators->undefine('/',' /','/ ','//');

would be required in order to make multiplication and division unavailable. See the Context Operator Table for more details.

Finally, absolute values are treated as a specialized form of parenthesis, so to remove them, use

   Context()->parens->remove('|');

The pg/macros/ directory contains a number of predefined contexts that limit the operations that can be performed in a student answer. For example, the contextLimitedNumeric.pl file defines contexts in which students can enter numbers, but no operations, so they would have to reduce their answer to a single number by hand. There are limited contexts for complex numbers, points, and vectors, and there are also specialized contexts for entering polynomials, or where powers are restricted in various ways.

Tolerances and Limits

The tolerances used in comparing numbers are part of the Context as well. You can set these via:

   Context()->flags->set(
     tolerance    => .0001,       # the relative or absolute tolerance
     tolType      => 'relative',  # or 'absolute'
     zeroLevel    => 1E-14,       # when to use zeroLevelTol
     zeroLevelTol => 1E-12,       # smaller than this matches zero
                                  #  when one of the two is less
                                  #  than zeroLevel
     limits       => [-2,2],      # limits for variables in formulas
     num_points   => 5,           # the number of test points
   );

Note that for testing formulas, you can override these values by setting these fields of the formula itself:

   $f = Formula("sqrt(x-10)");
   $f->{limits} = [10,12];
   
   $f = Formula("log(xy)");
   $f->{limits} = [[.1,2],[.1,2]]; # x and y limits

You can also specify the test points explicitly:

   $f = Formula("sqrt(x-10)");
   $f->{test_at} = [[11],[11.5],[12]];  # use these plus random ones
   
   $f = Formula("log(xy)");
   $f->{test_points} = [[.1,.1],[.1,.5],[.1,.75],
                        [.5,.1],[.5,.5],[.5,.75]];  # test only at these

You can specify the value at the same time you create the object, as in

   $f = Formula("sqrt(x-1)")->with(limits=>[10,12]);

It is also possible to set the limits of variables in the context itself,

   Context()->variables->set(x => {limits => [10,12]});

or when a variable is created in the Context,

   Context()->variables->add(t => ['Real',limits=>[1,2]]);

or even when the answer checker is specified,

   ANS($f->cmp(limits=>[10,12]));

See Also