Answer Checkers and the Context

From WeBWorK_wiki
Jump to navigation Jump to search

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