WeBWorK Problems

How to get WebWork to simplify its own answers

How to get WebWork to simplify its own answers

by James Mc Laughlin -
Number of replies: 6

I have made a copy of 

Rochester/setAlgebra35SystemMatrices/solve_RREF.pg

which I am trying to modify. 

This is a problem involving a set of linear equations in six variables, x1,x2,... x6, some of which are free.

The original code is quite complicated and I do not understand how to edit it. 

To the point: What students will see as the value for x1, what Webwork generates from 

ANS(fun_cmp(($ans[1]),var=>["x1","x2","x3","x4","x5","x6"]));
is
 −7−(−9)x2−(−1)(5−(−6)(1−(−5)x5−(−2)x6)−1x5−0x6)−(−9)(1−(−5)x5−(−2)x6)−(−2)x5−(−6)x6
which simplifies to 
13 + 9 x2 + 76 x5 + 36 x6
How can I get work to change  $ans[1] from the former messy form to the latter simplified form?

Thanks in advance,

Jimmy Mc Laughlin

PS I don't know if it is necessary to show more of the problem code, or if there is a standard WebWork command like Simplify[ ] in Mathematica that will simplify any algebraic expression.
In reply to James Mc Laughlin

Re: How to get WebWork to simplify its own answers

by Alex Jordan -

That problem file was written in 2002, and uses PG coding techniques from that era. The code is complicated and in the end the presentation of the correct answer suffers from the issue you are trying to get around. There is a quick way to patch the exercise to get what you want, but the best would be if this exercise (and many OPL exercises) could be recoded. It's a concern if new users have the impression that this is what PG coding is like.

For a quick fix, go to the end of the problem where the six ANS() calls happen. Replace those six lines with this:

Context("Numeric")->variables->are(
    x1 => 'Real',
    x2 => 'Real',
    x3 => 'Real',
    x4 => 'Real',
    x5 => 'Real',
    x6 => 'Real'
);
for (1..6) {
    ANS(Compute($ans[$_])->reduce->cmp)
};


For that to work, you also must add "MathObjects.pl" to the loadMacros() call near the top of the file.

One thing I would also change: you could replace the things like "x1" with "x_1". You would do this in the lines I posted above, as well as the line that defines @var and the line that prints the instructions to the student. And then it all comes out with subscript. The students would need to know to type the underscores, and you may need to make them aware of that.


In reply to Alex Jordan

Re: How to get WebWork to simplify its own answers

by James Mc Laughlin -
Thanks for the code.

That did work to some extent as it simplified 
−7−(−9)x2−(−1)(5−(−6)(1−(−5)x5−(−2)x6)−1x5−0x6)−(−9)(1−(−5)x5−(−2)x6)−(−2)x5−(−6)x6
to
9x27+5+6(1+5x5+2x6)x5+9(1+5x5+2x6)+2x5+6x6

Anyone have any ideas how to take it further to get to 
13 + 9 x2 + 76 x5 + 36 x6 ?

I will work on the underscores later (the answers now do have the format x_i rather than xi.

In reply to James Mc Laughlin

Re: How to get WebWork to simplify its own answers

by Alex Jordan -

WeBWorK/PG is not a CAS and can't directly do that kind of simplification. The best thing for this exercise would be to rewrite it using MathObjects from the beginning. However you could rebuild each expression right at the end, since you know they are linear expressions in the given variables. Something like (where I am using the underscored variables):

Context("Numeric")->variables->are(
    x_1 => 'Real',
    x_2 => 'Real',
    x_3 => 'Real',
    x_4 => 'Real',
    x_5 => 'Real',
    x_6 => 'Real'
);
for $i (1..6) {
    # Make the current answer in the loop a MathObject Formula
    $ans[$i] = Formula($ans[$i]);
    # Get the constant term for the current answer
    $c[0] = $ans[$i]->eval(
        x_1 => 0,
        x_2 => 0,
        x_3 => 0,
        x_4 => 0,
        x_5 => 0,
        x_6 => 0
    );
    # Get all the coefficients for the current answer
    for $j (1..6) {
$c[$j] = $ans[$i]->D("x_$j");
    }
    # Glue it all together
    $ans[$i] = Compute("$c[0] + $c[1]x_1 + $c[2]x_2 + $c[3]x_3 + $c[4]x_4 + $c[5]x_5 + $c[6]x_6")->reduce;
    ANS($ans[$i]->cmp);
};


If you use this to replace the original six ANS() calls, that works with my limited testing.
In reply to Alex Jordan

Re: How to get WebWork to simplify its own answers

by James Mc Laughlin -

That is brilliant!!

Thanks for working that out/. 

I went with the simpler xi instead of x_i at this point just to get it working and will go back and change it later.

In reply to James Mc Laughlin

Re: How to get WebWork to simplify its own answers

by Jaimos Skriletz -
When having students enter in variables, I find that using underscores isn't always the best so I both modify the TeX output and use alias, that way students can just type 'x1' vs having to making it an underscore for simplicity, but the TeX output will show the underscore. I have reduced this to two variables, but you can modify as needed.

Context('Numeric')->variables->are(
x1 => [ 'Real', TeX => 'x_1 ' ],
x2 => [ 'Real', TeX => 'x_2 ' ],
x_1 => [ 'Real', alias => 'x1' ],
x_2 => [ 'Real', alias => 'x2' ],
);

Of course if you want x_1 and x_2 to be the main variables, you can use x1 => ['Real', alias => 'x_1'] if you would prefer. I just do the other way as I find students have an easier time entering in x1 and not use underscores. This way the output looks like I want it to, students can enter in the answers without underscores, but if they do use underscores it still works.
In reply to Jaimos Skriletz

Re: How to get WebWork to simplify its own answers

by Davide Cervone -

Note that you don't need to set the TeX for x1, as the MathObject parser will output x1 as x_{1} in TeX automatically. Any trailing digits are typeset as a subscript in a variable name.