Introduction to Contexts

From WeBWorK
(Difference between revisions)
Jump to: navigation, search
(Basic Contexts)
Line 12: Line 12:
 
   Context("name");
 
   Context("name");
  
at the top of a problem file, where name is one of:
+
at the top of a problem file, where <code>name</code> is one of:
  
* Numeric (variable x, no complexes, points, etc.) -- this is the default context
+
* <code>Numeric</code> (variable x, no complexes, points, etc.) -- this is the default context
* Complex (variable z, i = \sqrt{-1}, no points, etc.)
+
* <code>Complex</code> (variable z, i = \sqrt{-1}, no points, etc.)
* Point (another name for Vector context, but angle brackets are not allowed)
+
* <code>Point</code> (another name for Vector context, but angle brackets are not allowed)
* Vector (variables x, y, z, angle brackets form vectors, i, j, k are unit coordinate vectors, etc.)
+
* <code>Vector</code> (variables x, y, z, angle brackets form vectors, i, j, k are unit coordinate vectors, etc.)
* Vector2D (same as Vector, but with i and j in 2D)
+
* <code>Vector2D</code> (same as Vector, but with i and j in 2D)
* Matrix (square brackets form matrices)
+
* <code>Matrix</code> (square brackets form matrices)
* Interval (parens and brakets form intervals)
+
* <code>Interval</code> (parens and brakets form intervals)
  
 
Another method of selecting a more nuanced context is to include one of the "context" macro packages using
 
Another method of selecting a more nuanced context is to include one of the "context" macro packages using
Line 27: Line 27:
  
 
which would specify that only "simplified" polynomials in <code> a_n x^n + ...  +a_0 </code> would be allowed as answers.
 
which would specify that only "simplified" polynomials in <code> a_n x^n + ...  +a_0 </code> would be allowed as answers.
 
 
  
 
==Common Manipulations of Context==
 
==Common Manipulations of Context==

Revision as of 14:37, 13 June 2008

Contents

Basic Contexts

The Context determines how variables are interpreted, sets the default variable(s), determines default constants available in the problem, and sets appropriate default system values such as the tolerance for student errors, etc.

It also adjusts the error messages for student responses so that, for example, "4i +5j +6k" will result in a syntax error message in "Numeric" context but not in "Vector" context.

Usually we select a Context by including

 Context("name");

at the top of a problem file, where name is one of:

  • Numeric (variable x, no complexes, points, etc.) -- this is the default context
  • Complex (variable z, i = \sqrt{-1}, no points, etc.)
  • Point (another name for Vector context, but angle brackets are not allowed)
  • Vector (variables x, y, z, angle brackets form vectors, i, j, k are unit coordinate vectors, etc.)
  • Vector2D (same as Vector, but with i and j in 2D)
  • Matrix (square brackets form matrices)
  • Interval (parens and brakets form intervals)

Another method of selecting a more nuanced context is to include one of the "context" macro packages using

 loadMacros("contextLimitedPolynomials");

which would specify that only "simplified" polynomials in a_n x^n + ... +a_0 would be allowed as answers.

Common Manipulations of Context

The Context of a problem determines how variables are interpreted, sets the default variable(s), determines default constants available in the problem, and sets appropriate default system values such as the tolerance for student errors, etc.

The most commonly used Contexts are listed in this [context list summary|ContextList]. The following gives the most common changes that we need to make to the Context in a problem. A more advanced list of information about the Context is [also available|$@WIKIVIEWBYID*10@$&page=ModifyingContext], as is an advanced reference page that [lists all Context flags|$@WIKIVIEWBYID*146@$&page=ContextFlags].

Types Of Context Changes

  • Context changes for Formulas in problems.
  • Context changes for Strings in problems.
  • Other useful changes to Contexts.

Context changes for Formulas

  • Adding variables
    In the Numeric Context, the default variable is x. To add to the variable(s) available in the Context, use the variables->add() method to specify the variable(s) to add and its (their) type, as in the following: </dt>
    Context()->variables->add(y=>'Real'); </dd>
    Context()->variables->add(y=>'Real',z=>'Real'); </dd>
    Context()->variables->add(z=>'Complex'); </dd>
  • Setting variables
    To set the variable(s) in the Context (replacing the default variable(s)), use the variables->are() method, as shown in the following:</dt>
    Context()->variables->are(y=>'Real'); </dd>
    Context()->variables->are(y=>'Real',z=>'Real'); </dd>
    Context()->variables->are(z=>'Complex'); </dd>
  • Setting variable limits
    The limits for the variables in the Context determine the values that may be used to determine the correctness of a Formula. If a Formula is not well-defined on the default range ![-2,2], it may be useful to set a different range for the variable(s) in the Context:
    If the only variable in the Context is x,</dt>
    Context()->variables->set(x=>{limits=>![-1,1]}); </dd>
    We can similarly set limits for multiple variables at once, if they have been defined in the Context:</dt>
    Context()->variables->set(x=>{limits=>![-1,1],y=>{limits=>![-1,1]}); </dd>

Context changes for Strings

  • Adding Strings to the Context
    By default there are a limited number of Strings that are predefined in the Context. In the Numeric Context (the default) these include inf, infinity, and DNE. Because they exist in the Context, students can enter these as answers without generating error messages (though, of course, the answer may be incorrect). To add other Strings to the Context, we use the strings->add() method:
    To add the string "Continuous" to the Context, </dt>
    Context()->strings->add(Continuous=>{});
    </dd>
    To add the strings "True" and "T" to the Context, making "T" be an alias for "True" (so that a student could enter either "True" or "T" as an answer and have either marked correct),</dt>
    Context()->strings->add(True=>{},T=>{alias=>'True'});
    </dd>
    And to add "True", "False" and aliases for both,</dt>
    Context()->strings->add(True=>{},False=>{},T=>{alias=>'True'},F=>{alias=>'False'});
    </dd>
  • Changing case-sensitivity of Strings
    By default, !WeBWorK regards String objects as being case-insensitive. Thus if the correct answer to a problem is the String object "True", a student could respond "True" or "true" and have either answer marked correct. To change this behavior, we set the caseSensitive flag when adding the variable to the Context:
    Context()->strings->add(True=>{caseSensitive=>1}); </dd>

Other useful Context changes

  • Preserving constant values in Formulas
    By default, !WeBWorK will reduce constant expressions that are substituted into Formulas, which is not always what we want to do. This is a somewhat subtle point, which may be easiest to consider in the context of an example.
    Suppose that we have the !WeBWorK code </dt>
    $f = Formula("sin(x)");
    $f0 = $f->eval(x=>pi/6);
    </dd>
    Then we are substituting the Real value pi/6 into the sine function, and $f0 is accordingly a Real value (0.5) which will be displayed as a decimal. We can make !WeBWorK display the unreduced expression sin(pi/6) by setting the reduceConstants flag in the Context and substituting a Formula for x instead of evaluating the expression: </dt>
    Context()->flags->set(reduceConstants=>0);
    $f = Formula("sin(x)")
    $f0 = $f->substitute(x=>Formula("pi/6"));
    </dd>
    will result in the Formula for pi/6 being substituted into the function $f to obtain a new Formula, and when it is shown in the problem the expression will be displayed as sin(pi/6), not in the reduced form (0.5). </dt>
follow us