WeBWorK Problems

Step Functions Should Not Be Legacious

Step Functions Should Not Be Legacious

by Fred Sullivan -
Number of replies: 2
I am bothered by the relegation of the step() function to the LegacyNumeric context, as this implies that it has been replaced by something better and that it might go away.

PiecewiseFunction might be nice for many applications. However, I mostly use webwork for differential equations, and when we do Laplace transforms, all problems of real interest - that is, all that can't be easily done by other methods, have answers that are expressed in terms of the unit step function. For example, e^t(u(t)-u(t-4)) + 2t e^t u(t-4).

It would actually be nice to be able to write the answers with u(t-a), but having step(t-a) is not too bad. It would, however, be very unfriendly to have to write them using the PiecewiseFunction syntax.

Can step be put into Numeric Context? And how would I fix it so that my students can write their answers as u(t-a) instead of step(t-a)?
In reply to Fred Sullivan

Re: Step Functions Should Not Be Legacious

by Davide Cervone -
The step() function is in the LegacyNumeric context because that context is the one that implements the traditional num_cmp() answer checker (which is now just a shell that calls MathObjects behind the scenes). The Legacy contexts are for the traditional answer checkers.

Because MathObjects contexts allow you to add (and remove) functions, operators, constants, and so on, the various contexts do not include all possible functions. The MathObjects Numeric context is a general one, but it only include functions whose mathematical names are standardized. The "step" function is not one of those, and so is not in the Numeric context by default.

If you want to add it in, use

Context("Numeric")->functions->add(
  step => {
    class => 'Parser::Legacy::Numeric',
    perl => 'Parser::Legacy::Numeric::do_step'
  },
);
You could also just use
    Context("LegacyNumeric");
or make your own context with a different name if you wanted, and could put that into a macro file if you preferred.

To make the function be named u instead of step, use

Context("Numeric")->functions->add(
  u => {
    class => 'Parser::Legacy::Numeric',
    perl => 'Parser::Legacy::Numeric::do_step'
  },
);

Hope that helps.

Davide

In reply to Davide Cervone

Re: Step Functions Should Not Be Legacious

by Paul Pearson -
Hi,

I know this reply is coming quite a bit after the fact, but it ought to be useful to those who browse the forum in the future. Davide Cervone just pointed out to me that in order to set test_points or test_at for a function that has been added to the context, you must use the syntax $f->with(test_at => ...) as illustrated in the example below. (That is, the syntax for test_points and test_at in http://webwork.maa.org/wiki/FormulaTestPoints doesn't quite work for functions that are added to the context.)

loadMacros("PGstandard.pl","MathObjects.pl","parserFunction.pl",);

Context("LegacyNumeric");
parserFunction("u(t)" => "step(t)");
$a = random(2,4,1);
$f = Formula("5 u(t-$a)");

$answer = $f->with(
 limits=>[$a-5,$a+5],
 test_at => [[$a-1],[$a],[$a+0.0000001],[$a+1]],
 num_points=>10,
);

Notice that to get reliable answer checking, we have set the limits to be centered about the discontinuity, that we use 10 points total for checking the answer, and that in the "test_at" section we have specified four of those points for answer checking, two of which are very close to the discontinuity.

Davide also pointed out that the step() and fact() function (from WeBWorK's equation parser prior to MathObjects) are defined in the LegacyNumeric context. If you don't want fact(), you can use

Context("LegacyNumeric")->functions->disable("fact");


If you are writing questions that have the Heaviside function u(t) in their answer, but students never actually see the values of the function u(t), it is possible to define u(t) as something other than the Heaviside function so that it is possible to use the default settings (limits, test_at, num_points) and have a reliable answer checker. I have explained this in more detail in the right column on the page:

http://webwork.maa.org/wiki/HeavisideStep1

Best Regards,

Paul Pearson