StepFunctions
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.
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
Alternately, we can add the step function to the standard Numeric Context. This has the advantage that we can name it
The code here defines the step function as 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. |