WeBWorK Problems

displaying a reduced version of a polynomial

Re: displaying a reduced version of a polynomial

by Davide Cervone -
Number of replies: 0
If you have a formula that you know to be linear in two variables, like your example here, then there is a sneaky way to find the coefficients to write it in standard form. Here is an example.
    Context("Numeric");
    Context()->variables->are(m=>"Real",s=>"Real");

    $a = random(2,9,1);
    do { $b = random(2,9,1); } until ($a != $b);

    $f = Formula("$a s + $b (s-m)");
    $A = $f->eval(s=>1,m=>0);
    $B = $f->eval(s=>0,m=>1);
    $F = Formula("$A s + $B m")->reduce;

    TEXT($F == $f  ? "F and f agree" : "F and f are different");
The key is that (for a linear function) the coefficients of s and m are the values of the function when (s,m)=(1,0), and (s,m)=(0,1), respectively. Then you can create the formula with the proper coefficients directly. The TEXT call should print "F and f agree", but you can use a test like this to check that the original function actually was linear (if it is coming from student input, for example).

Davide