Difference between revisions of "AddingFunctions"

From WeBWorK_wiki
Jump to navigation Jump to search
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/problem-techniques/AddingFunctions.html a newer version of this problem]</p>
  +
 
<h2>Adding Functions to the Context: PG Code Snippet</h2>
 
<h2>Adding Functions to the Context: PG Code Snippet</h2>
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>This code snippet shows the essential PG code to add a named function to the Context in a problem. (March 2010: There is a easier way to do this: [http://webwork.maa.org/doc/cvs/pg_CURRENT/macros/parserFunction.pl.html parserFunction.pl.html].) Note that these are <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working. (The information here is taken from [http://webwork.maa.org/moodle/mod/forum/discuss.php?d=6194 this discussion thread] and [http://cvs.webwork.rochester.edu/viewcvs.cgi/webwork2/doc/parser/extensions/1-function.pg?rev=1.6&content-type=text/vnd.viewcvs-markup a sample problem] in the doc/parser/extensions directory of the WeBWorK tree.)</em>
+
<em>This code snippet shows the essential PG code to add a named function to the Context in a problem. Note that these are <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working. </em>
 
</p>
 
</p>
  +
  +
  +
<ul>
  +
<li><b>Example 1:</b> The newer and easier way to add a named function to the context using [http://webwork.maa.org/pod/pg/macros/parserFunction.html parserFunction.pl].</li>
  +
<li><b>Example 2:</b> The rudimentary way to add a function to the context.</li>
  +
</ul>
   
   
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>Example 1: The newer and easier way to add a function to the context.</em>
+
<em><b>Example 1:</b> The newer and easier way to add a named function to the context using [http://webwork.maa.org/pod/pg/macros/parserFunction.html parserFunction.pl].</em>
 
</p>
 
</p>
   
Line 24: Line 34:
 
<pre>
 
<pre>
 
loadMacros(
 
loadMacros(
  +
"PGstandard.pl",
 
"MathObjects.pl",
 
"MathObjects.pl",
 
"parserFunction.pl",
 
"parserFunction.pl",
 
);
 
);
   
  +
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)")->with(
  +
test_at => [[5,1],[6,2],[7,3]]
  +
);
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
<b>Initializaiton and Setup:</b>
+
<b>Initialization and Setup:</b>
We need to load the <code>parserFunction.pl</code> macro, and then use one of its routines to define a new function that students may type in their answers.
+
We need to load the <code>parserFunction.pl</code> macro, and then use one of its routines to define a new function that students may type in their answers. For more information, see [http://webwork.maa.org/pod/pg/macros/parserFunction.html parserFunction.pl.html]
  +
</p>
  +
</td>
  +
</tr>
  +
<tr valign="top">
  +
<td style="background-color:#ffdddd;border:black 1px dashed;">
  +
<pre>
  +
BEGIN_TEXT
  +
Given a surface \(z=f(x,y)\), what is the equation
  +
for the \(z\)-coordinate of the surface along a
  +
line having \(y=3\), shifted four units to the
  +
right? \(z = \) \{ ans_rule(25) \}
  +
END_TEXT
  +
</pre>
  +
<td style="background-color:#ffcccc;padding:7px;">
  +
<p>
  +
The text section of the file is the same as usual,
  +
though hopefully with a less strained problem
  +
formulation.
  +
</p>
  +
</td>
  +
</tr>
  +
<tr valign="top">
  +
<td style="background-color:#eeddff;border:black 1px dashed;">
  +
<pre>
  +
ANS( $answer->cmp() );
  +
</pre>
  +
<td style="background-color:#eeccff;padding:7px;">
  +
<p>
  +
And we check the answer as we expect.
 
</p>
 
</p>
 
</td>
 
</td>
Line 42: Line 58:
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
</p>
 
</p>
 
 
 
 
 
   
   
Line 52: Line 63:
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>Example 2: The rudimentary way to add a function to the context.</em>
+
<em><b>Example 2:</b> The rudimentary way to add a function to the context.
  +
<br />
  +
<br />
  +
(The information here is taken from [http://webwork.maa.org/moodle/mod/forum/discuss.php?d=414 this discussion thread] and [http://webwork.maa.org/viewvc/system/trunk/webwork2/doc/parser/extensions/1-function.pg?view=markup a sample problem] in the doc/parser/extensions directory of the WeBWorK tree.)</em>
 
</p>
 
</p>
   
Line 67: Line 78:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
package NewFunc;
+
package NewFunc;
# this next line makes the function a
+
# this next line makes the function a
# function from reals to reals
+
# function from reals to reals
our @ISA = qw(Parser::Function::numeric);
+
our @ISA = qw(Parser::Function::numeric);
   
sub log2 {
+
sub log2 {
shift; my $x = shift;
+
shift; my $x = shift;
return CORE::log($x)/CORE::log(2);
+
return CORE::log($x)/CORE::log(2);
}
+
}
   
package main;
+
package main;
   
# Make it work on formulas as well as numbers
+
# Make it work on formulas as well as numbers
sub log2 {Parser::Function->call('log2',@_)}
+
sub log2 {Parser::Function->call('log2',@_)}
   
# Add the new functions to the Context
+
# Add the new functions to the Context
Context()->functions->add(
+
Context()->functions->add(
log2 => {class => 'NewFunc',
+
log2 => {class => 'NewFunc',
TeX => '\log_2'}, );
+
TeX => '\log_2'}, );
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 142: Line 153:
   
 
<ul>
 
<ul>
<li>POD documentation: [http://webwork.maa.org/doc/cvs/pg_CURRENT/macros/parserFunction.pl.html parserFunction.pl.html]</li>
+
<li>POD documentation: [http://webwork.maa.org/pod/pg/macros/parserFunction.html parserFunction.pl]</li>
 
<li>PG macro: [http://cvs.webwork.rochester.edu/viewcvs.cgi/pg/macros/parserFunction.pl parserFunction.pl]</li>
 
<li>PG macro: [http://cvs.webwork.rochester.edu/viewcvs.cgi/pg/macros/parserFunction.pl parserFunction.pl]</li>
 
</ul>
 
</ul>
  +
  +
[[Category:Problem_Techniques]]

Latest revision as of 15:42, 20 June 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

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 named 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 named function to the context using parserFunction.pl.

Problem Techniques Index

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)")->with(
  test_at => [[5,1],[6,2],[7,3]]
);

Initialization and Setup: We need to load the parserFunction.pl macro, and then use one of its routines to define a new function that students may type in their answers. For more information, see parserFunction.pl.html

BEGIN_TEXT
Given a surface \(z=f(x,y)\), what is the equation
for the \(z\)-coordinate of the surface along a
line having \(y=3\), shifted four units to the
right?  \(z = \) \{ ans_rule(25) \}
END_TEXT

The text section of the file is the same as usual, though hopefully with a less strained problem formulation.

ANS( $answer->cmp() );

And we check the answer as we expect.

Problem Techniques Index



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.)

Problem Techniques Index

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 our ISA = qw(Parser::Function::numeric2);, then our function would be a two-variable function, f(x,y), and the subroutine defining it would take two variable arguments:

  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.

Problem Techniques Index