Difference between revisions of "Introduction to Contexts"
Line 94: | Line 94: | ||
====Other useful Context changes==== |
====Other useful Context changes==== |
||
− | <ul> |
||
+ | ;Preserving constant values in Formulas |
||
− | <li> <strong>Preserving constant values in Formulas</strong><br /> 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. <dl> <dt> Suppose that we have the !WeBWorK code </dt> <dd> <code>$f = Formula("sin(x)");<br /> $f0 = $f->eval(x=>pi/6);</code> </dd> <dt> Then we are substituting the Real value pi/6 into the sine function, and <code>$f0</code> 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 <code>reduceConstants</code> flag in the Context and <em>substituting</em> a Formula for <code>x</code> instead of <em>evaluating</em> the expression: </dt> <dd> <code>Context()->flags->set(reduceConstants=>0);<br /> $f = Formula("sin(x)")<br /> $f0 = $f->substitute(x=>Formula("pi/6"));</code> </dd> <dt> will result in the Formula for pi/6 being substituted into the function <code>$f</code> to obtain a new Formula, and when it is shown in the problem the expression will be displayed as <code>sin(pi/6)</code>, not in the reduced form (0.5). </dt> </dl> </li> |
||
+ | : This technique helps to make the correct answer shown to students educationally useful. |
||
− | </ul> |
||
+ | : The answer <code>0.5</code> might be correct but it is not as enlightening as <code>sin(pi/6)</code>. |
||
+ | By default WeBWorK will reduce constant expressions that are ''substituted'' into Formulas. This is not always what we want to do. It's a somewhat subtle point, which may be easiest to consider in the context of an example. |
||
+ | * Consider the WeBWorK code |
||
+ | $f = Formula("sin(x)"); |
||
+ | $f0 = $f->eval(x=>pi/6); |
||
+ | displayed as: (0.5) |
||
+ | By evaulating the sine function at the the Real value <code>pi/6</code> we obtain a real number. |
||
+ | <code>$f0</code> is accordingly a Real value (0.5) which will be displayed as a decimal. |
||
+ | |||
+ | We can make WeBWorK display the unreduced expression <code>sin(pi/6)</code> by setting the |
||
+ | <code>reduceConstants</code> flag in the Context and |
||
+ | '''substituting''' a Formula for <code>x</code> instead of '''evaluating''' the expression: |
||
+ | |||
+ | Context()->flags->set(reduceConstants=>0); |
||
+ | $f = Formula("sin(x)") |
||
+ | $f0 = $f->substitute(x=>Formula("pi/6")); |
||
+ | displayed as: sin(pi/6) |
||
+ | |||
+ | Here the Formula for <code>pi/6</code> is substituted into the function <code>$f</code>. <code>$f0</code> is a new Formula which when displayed appears as <code>sin(pi/6)</code> not in the reduced form (0.5). |
||
+ | |||
+ | =====Summary: evaluate vs. substitute===== |
||
+ | |||
+ | *The result of <code>$formula->evaluate(45)</code> has the type obtained by evaluating the formula at 45. It is no longer |
||
+ | a formula, but has the type of the range of the original formula. |
||
+ | *The result of <code>$formula->substitute(45)</code> is still a formula, with a string representation in which each <code>x</code> has been replaced by 45. |
||
[[Category:MathObjects]] |
[[Category:MathObjects]] |
Revision as of 08:23, 16 June 2008
Contents
Contexts
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. 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.
List of Basic Contexts
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 complex numbers, points, etc.) -- this is the default contextComplex
(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: [[0,1],[1,0]] )Interval
(subsets of the real line: parens and brackets form intervals, finite subsets, (5,6] ,(-infinity,6), {5,6} etc. )
Another method of selecting a more nuanced context is to include one of the "context" macro packages using the syntax.
loadMacros("contextLimitedPolynomials.pl");
This example would specify that only "simplified" polynomials in a_n x^n + ... +a_0
would be allowed as answers. Inspecting the file "contextLimitedPolynomials.pl" is one way to learn how to modify a context to meet your
requirements.
Common Context methods
Producing "TeX" strings
This is the most common use of a Context method. Suppose that you have a formula such as
$f = Formula("sin(3x^2)/cos(x)");
Normally "$f" emits the string "sin(3x^2)/cos(x)"
which defines the function. However if you set
the current context to "texStrings": Context()->texStrings;
then "$f" produces the TeX string used to typeset the function:
"\frac{ \sin(3x^2) }{\cos(x) } "
In practice this means that virtually every occurence of a BEGIN_TEXT/END_TEXT block should be written as
Context()->texStrings; BEGIN_TEXT Differentiate the function \( $f\) ... ...... END_TEXT Context()->normalStrings;
This means that the "$f" is replaced by TeX code placed between the "LaTeX math" symbols \( and \) and is then transformed into a typeset representation of the formula "$f".
It is a good idea to return to "normalStrings" outside BEGIN_TEXT blocks since the normal string representation of $f may be required inside perl macros in the rest of the problem.
Context changes for Formulas
- Adding variables
- In the Numeric Context, the default variable is
x
. Use thevariables->add(y=>'Real')
method to specify additional legitimate variables and their type in the current context.
For example: Context()->variables->add(y=>'Real');
Context()->variables->add(y=>'Real',z=>'Real');
Context()->variables->add(z=>'Complex');
- Setting variables
- To set the variable(s) in the current Context (replacing the default variable(s)), use the
variables->are()
method, as shown in the following: Context()->variables->are(y=>'Real');
Context()->variables->are(y=>'Real',z=>'Real');
Context()->variables->are(z=>'Complex');
- 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
,Context()->variables->set(x=>{limits=>[-1,1]});
- We can similarly set limits for several variables at once, if they have been defined in the Context:
Context()->variables->set(x=>{limits=>[-1,1]},y=>{limits=>[-1,1]});
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
, andDNE
. 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 thestrings->add()
method:
- To add the string "Continuous" to the Context,
Context()->strings->add(Continuous=>{});
- 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),
Context()->strings->add(True=>{},T=>{alias=>'True'});
- And to add "True", "False" and aliases for both,
Context()->strings->add(True=>{},False=>{},T=>{alias=>'True'},F=>{alias=>'False'});
- 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 while adding the variable to the Context:
Context()->strings->add(True=>{caseSensitive=>1});
Other useful Context changes
- Preserving constant values in Formulas
- This technique helps to make the correct answer shown to students educationally useful.
- The answer
0.5
might be correct but it is not as enlightening assin(pi/6)
.
By default WeBWorK will reduce constant expressions that are substituted into Formulas. This is not always what we want to do. It's a somewhat subtle point, which may be easiest to consider in the context of an example.
- Consider the WeBWorK code
$f = Formula("sin(x)"); $f0 = $f->eval(x=>pi/6); displayed as: (0.5)
By evaulating the sine function at the the Real value pi/6
we obtain a real number.
$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:
Context()->flags->set(reduceConstants=>0); $f = Formula("sin(x)") $f0 = $f->substitute(x=>Formula("pi/6")); displayed as: sin(pi/6)
Here the Formula for pi/6
is substituted into the function $f
. $f0
is a new Formula which when displayed appears as sin(pi/6)
not in the reduced form (0.5).
Summary: evaluate vs. substitute
- The result of
$formula->evaluate(45)
has the type obtained by evaluating the formula at 45. It is no longer
a formula, but has the type of the range of the original formula.
- The result of
$formula->substitute(45)
is still a formula, with a string representation in which eachx
has been replaced by 45.