Difference between revisions of "StepFunctions"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Step Functions: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essential PG code to use step functions i...)
 
(Correct usage for adding step function as u(x))
Line 22: Line 22:
 
## function to the Numeric Context
 
## function to the Numeric Context
 
Context("Numeric");
 
Context("Numeric");
Context()->functions->add(
+
Context()->functions->redefine("step",from=>"LegacyNumeric");
step => {
 
class => 'Parser::Legacy::Numeric',
 
perl => 'Parser::Legacy::Numeric::do_step'
 
},
 
);
 
   
 
## then we can define an answer
 
## then we can define an answer
Line 36: Line 36:
 
</p>
 
</p>
 
<p>
 
<p>
The code here defines the step function as <code>step(x)</code>. To name it <code>u(x)</code>, we would make the obvious change:
+
The code here defines the step function as <code>step(x)</code>. To name it <code>u(x)</code>, we would need to create our own custom function:
 
</p>
 
</p>
 
<pre>
 
<pre>
  +
package myFunction; # a new class for our function
  +
our @ISA = ("Parser::Function::numeric"); # means it is real-valued and takes one real input
  +
sub u { # the function itself
  +
shift; my $x = shift;
  +
return main::step($x);
  +
}
  +
package main; # end of our class
  +
 
Context()->functions->add(
 
Context()->functions->add(
u => {
 
  +
u => {class => 'myFunction'}, # add our new function to the context
class => 'Parser::Legacy::Numeric',
 
perl => 'Parser::Legacy::Numeric::do_step'
 
},
 
 
);
 
);
  +
  +
sub u {Parser::Function->call('u',@_); # make it usable in Perl as well.
 
</pre>
 
</pre>
 
</td>
 
</td>

Revision as of 08:42, 25 August 2014

Step Functions: PG Code Snippet

This code snippet shows the essential PG code to use step functions 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.

Problem Techniques Index

PG problem file Explanation
  ## this uses the LegacyNumeric Context
  # Context("LegacyNumeric");

  ## alternately, we can just add the step
  ##    function to the Numeric Context
  Context("Numeric");
  Context()->functions->redefine("step",from=>"LegacyNumeric");

  ## then we can define an answer
  $step1 = Compute("step(x-1)");

No changes are necessary in the tagging and description or initialization sections of the problem file. In the problem set-up section, we declare a Context that will include the step function. This can be done in two ways: we can simply declare the Context to be the LegacyNumeric Context, which includes the step function step(x).

Alternately, we can add the step function to the standard Numeric Context. This has the advantage that we can name it step(x) (which is its default moniker) or, if we like, use something like u(x) instead (which is used in some other contexts, especially differential equations).

The code here defines the step function as step(x). To name it u(x), we would need to create our own custom function:

  package myFunction;                          # a new class for our function
  our @ISA = ("Parser::Function::numeric");    # means it is real-valued and takes one real input
  sub u {                                                   # the function itself
    shift; my $x = shift;
    return main::step($x);
  }
  package main;                                # end of our class

  Context()->functions->add(
    u => {class => 'myFunction'},              # add our new function to the context
  );

  sub u {Parser::Function->call('u',@_);       # make it usable in Perl as well.
  BEGIN_TEXT
  What is a function \(f(x)\) that is zero for 
  \(x<1\) and one for \(x\ge1\)?
  $BR
  \(f(x) = \) \{ ans_rule(35) \}
  END_TEXT

The text section of the problem is what we'd expect.

  ANS( $step1->cmp() );

As is the answer and solution section.

Problem Techniques Index