Difference between revisions of "AddingFunctions"
Line 30: | Line 30: | ||
<pre> |
<pre> |
||
loadMacros( |
loadMacros( |
||
+ | "PGstandard.pl", |
||
"MathObjects.pl", |
"MathObjects.pl", |
||
"parserFunction.pl", |
"parserFunction.pl", |
||
Line 36: | Line 37: | ||
Context("Numeric")->variables->add(y=>"Real"); |
Context("Numeric")->variables->add(y=>"Real"); |
||
parserFunction("f(x,y)" => "sqrt(x*y)"); |
parserFunction("f(x,y)" => "sqrt(x*y)"); |
||
+ | |||
+ | $answer = Formula("f(x-4,3)"); |
||
</pre> |
</pre> |
||
</td> |
</td> |
Revision as of 18:19, 23 April 2010
Adding Functions to the Context: PG Code Snippet
This code snippet shows the essential PG code to add a named function to the Context in a problem. 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.
- Example 1: The newer and easier way to add a function to the context using parserFunction.pl.
- Example 2: The rudimentary way to add a function to the context.
Example 1: The newer and easier way to add a function to the context using parserFunction.pl.
PG problem file | Explanation |
---|---|
loadMacros( "PGstandard.pl", "MathObjects.pl", "parserFunction.pl", ); Context("Numeric")->variables->add(y=>"Real"); parserFunction("f(x,y)" => "sqrt(x*y)"); $answer = Formula("f(x-4,3)"); |
Initializaiton and Setup:
We need to load the |
Example 2: The rudimentary way to add a function to the context.
(The information here is taken from this discussion thread and a sample problem in the doc/parser/extensions directory of the WeBWorK tree.)
PG problem file | Explanation |
---|---|
package NewFunc; # this next line makes the function a # function from reals to reals our @ISA = qw(Parser::Function::numeric); sub log2 { shift; my $x = shift; return CORE::log($x)/CORE::log(2); } package main; # Make it work on formulas as well as numbers sub log2 {Parser::Function->call('log2',@_)} # Add the new functions to the Context Context()->functions->add( log2 => {class => 'NewFunc', TeX => '\log_2'}, ); |
To define our new function, we first create a class in which it can live, where the behavior of the function is articulated, and then we add the function to the Context for the problem. Here, we borrow from the extensions sample provided in the webwork2/doc directory of the distribution and define a log base 2 function. If we didn't have any fancy TeX formatting for the function we could omit that hash key in the functions->add() call from the Context.
We can define a multivariable function by changing the inheritance list: if we had
sub f { shift; my ( $x, $y ) = @_; ... } |
BEGIN_TEXT Solve \( 2^{3x} = 5 \): \( x = \) \{ ans_rule(25) \} $BR ${BITALIC}(Your answer may involve the function \(log_2(x)\), which you should enter as ${BTT}log2(x)$ETT.)$EITALIC END_TEXT |
The problem text is as we would expect, except that we are allowed to use the function that we defined in the problem text. |
ANS( Compute("(1/3)*log2(5)")->cmp() ); |
And then in the answer and solution section of the problem the function may also be used. |
- POD documentation: parserFunction.pl.html
- PG macro: parserFunction.pl