WeBWorK Problems

displaying a reduced version of a polynomial

displaying a reduced version of a polynomial

by Darwyn Cook -
Number of replies: 2
I know the idea of reducing polynomials has been discusses a lot, but searching through the forums has not led to a solution of this particular kind of issue.

I am writing a Laplace transforms problem where I have created a table of functions along with the numerators and denominators of their Laplace transforms. I randomly choose two functions from the table and two coefficients. I then compute the numerator and denominator separately for the linear combination of the Laplace transforms.

I want to display the Laplace transform and ask for the inverse, the issue is that I get numerators of the form
6s - 5(s-m).
I would like to reduce this to s + 5m in some automated way for display purposes. Ideally the numerator would be displayed in the format required by the LimitedPolynomial context.
In reply to Darwyn Cook

Re: displaying a reduced version of a polynomial

by Paul Pearson -
Hi Darwyn,

Davide Cervone has provided a reduce method

Context("Numeric");
$a = non_zero_random(-5,5,1);
$f = Formula("x - $a")->reduce();

that will do very simple algebraic simplification such as changing -- to + and -+ to -. It was not designed to do more complicated algebra, such as applying the distributive law.

The contexts Context("LimitedPolynomial"); and Context("LimitedPolynomial-Strict"); define a way to parse MathObjects and determine whether something is a fully simplified polynomial. However, these contexts do not provide any methods to simplify a polynomial.

It seems like what you want is a method that will simplify complicated algebraic expressions, which, to my knowledge, is not currently available in WebWork.

Certainly, the algebraic simplification can be done manually. Further, it seems to me that if you want to display the fully simplified answer, you probably also expect your students to enter a fully simplified answer. In the code below, I show how to require students to fully simplify their answer. Since you are also dealing with algebraic fractions, you may want to have a separate answer blank for the numerator and denominator and use techniques to force students to reduce their fraction as much as possible (e.g., canceling common factors on top and bottom). These algebraic fraction techniques are illustrated at

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

Best Regards,

Paul Pearson

########################################
# Initialization

DOCUMENT();

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

TEXT(beginproblem());


#######################################
# Set-up

Context("LimitedPolynomial-Strict");
Context()->variables->are(m=>"Real",s=>"Real");

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

# Suppose the general answer has the unsimplified form
# $a s - $b (s-m).

# In the LimitedPolynomial context, both of these
# would give errors because they are not fully
# simplified.
#
# $answer = Formula("$a * s - $b * (s-m)")->reduce();
#
# $answer = Formula("($a - $b) * s + $b * m")->reduce();

# The format of the answer must be a LimitedPolynomial
# MathObject, so we need to simplify it to the form
# $A s + $B m

$A = $a - $b;
$B = $b;

$answer = Formula("$A * s + $B * m")->reduce();


################################################
# Main text

Context()->texStrings;
BEGIN_TEXT
Simplify the expression \( $a s - $b (s-m) \)
by combining like terms.
$BR
$BR
\{ ans_rule(20) \}
END_TEXT
Context()->texStrings;


################################################
# Answer evaluation

$showPartialCorrectAnswers = 1;

ANS($answer->cmp());

COMMENT('MathObject version');

ENDDOCUMENT();
In reply to Darwyn Cook

Re: displaying a reduced version of a polynomial

by Davide Cervone -
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