DisableFunctions

From WeBWorK_wiki
Revision as of 14:05, 12 February 2008 by Glarose (talk | contribs)
Jump to navigation Jump to search

Disabling Functions 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.

Problem Techniques Index

PG problem file Explanation
  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 Context that we want. In this case, we have selected the LimitedPolynomial context, which will require all answers to be (expanded) polynomials (i.e., sums of multiples of powers of x). This context allows also requiring that there be only one term with each power of x, as noted in the comment to the right.

There are a number of limited contexts like LimitedPolynomial that are available by default:

  • LimitedPolynomial-Strict, which disallows mathematical operations even in the coefficients of the polynomial;
  • LimitedComplex, which allows complex numbers to be entered, but no complex operations;
  • LimitedNumeric, which allows numbers to be entered, but no operations are permitted between them;
  • LimitedNumeric-List, which is similar but for lists;
  • LimitedPoint, which allows points to be entered, but no operations between them;
  • LimitedPowers, which restricts what constants may be raised to powers. There are several available restrictions, only one of which may be selected:
    • LimitedPowers::NoBaseE(), which doesn't allow e to be raised to a power,
    • LimitedPowers::OnlyIntegers(), which only allows positive and negative integers to be raised to powers,
    • LimitedPowers::OnlyPositiveIntegers(), which only allows positive integers,
    • LimitedPowers::OnlyNonNegativeIntegers(), which only allows positive integers and zero;
  • LimitedVector, which allows vectors to be entered but disallows operations between vectors; there are two variations to this,
  • LimitedVector-coordinate, which requires that the vectors be entered in coordinate format (<a,b,c>), and
  • LimitedVector-ijk, which requires ijk-format.
  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.

Problem Techniques Index

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");

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 Numeric context), and then change it.

We can disable specific operations in the Context: in general, predefined operations are * / + - ! >< U ^ ** . ,, for multiplication, division, addition, subtraction, the factorial operation, the cross-product ><, set union, exponentiation (both ^ and ** give exponentiation), the dot product, and list creation (,).

After disabling the operation, they can be re-enabled with operators->redefine(), e.g., Context()->operators->redefine("^"). We can also remove operators with operators->remove(), but this is not recommended, as it makes it completely unknown in the Context so that students won't get helpful error messages if they try to use them.

To disable specific functions in the Context, we similarly undefine them from the predefined functions. The predefined functions are sin, cos, tan, sec, csc, cot, asin, acos, atan, asec, acsc, acot, sinh, cosh, tanh, sech, csch, coth, asinh, acosh, atanh, asech, csch, acoth, ln, log, log10, exp, sqrt, abs, int, sgn, atan2, norm, unit, arg, mod, Re, Im, conj.

In addition, classes of functions can be disabled with functions->disable():

  • Context()->functions->disable("Trig"); (disables all trig functions in both SimpleTrig and InverseTrig functions)
  • Context()->functions->disable("SimpleTrig"); (disables sin, cos, tan, sec, csc, cot)
  • Context()->functions->disable("InverseTrig"); (disables asin, acos, atan, asec, acsc, acot, atan2)
  • Context()->functions->disable("Hyperbolic"); (disables all hyperbolic functions in both SimpleHyperbolic and InverseHyperbolic functions)
  • Context()->functions->disable("SimpleHyperbolic"); (disables sinh, cosh, tanh, sech, csch, coth)
  • Context()->functions->disable("InverseHyperbolic"); (disables asinh, acosh, atanh, asech, acsch, acoth)
  • Context()->functions->disable("Numeric"); (disables ln, log, log10, exp, sqrt, abs, int, sgn)
  • Context()->functions->disable("Vector"); (disables norm, unit)
  • Context()->functions->disable("Complex"); (disables arg, mod, Re, Im, conj)
  • 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.

Problem Techniques Index