PREP 2015 Question Authoring - Archived

Question about Reduce (fiddly details!)

Question about Reduce (fiddly details!)

by Alice McLeod -
Number of replies: 2
I've been working on the homework problem involving asking a student to factor a quadratic expression, and I can't seem to get the reduce macro to work quite the way I want it to.

My full code is as follows (with both versions of my attempt to reduce, highlighted).

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"contextPolynomialFactors.pl",
"contextLimitedPowers.pl",
"PGML.pl",
);

TEXT(beginproblem());

##############################
#
# Setup
#

$a = non_zero_random(-5,5,1);
$b = non_zero_random(-5,5,1);
$c = non_zero_random(-5,5,1);

Context("Numeric");

#
# Expanded form
#

# Option 1
#$poly = Compute("$a*$b*x**2+($a*$b*$c+$a)*x+$a*$b")->reduce;

# Option 2
$poly = Compute("$a*$b*x**2+($a*$b*$c+$a)*x+$a*$b");
$poly->reduce("(-x)+y" => 0);

#
# Factored form
#

Context("PolynomialFactors-Strict");
Context()->flags->set(singleFactors=>0);
LimitedPowers::OnlyIntegers(
minPower => 0, maxPower => 1,
message => "either 0 or 1",
);

$factored = Compute("$a*($b*x+1)*(x+$c)")->reduce;

BEGIN_PGML

Write the quadratic expression [` [$poly] `] in factored form [` k(ax+b)(cx+d) `].

[________________________]{$factored}

END_PGML

ENDDOCUMENT();

My pseudorandom variables were producing the polynomial -4x^2+10x-4.

Option 1 tidied up the signs nicely, but reversed the x^2 and x terms in order to avoid a leading negative, giving 10x-4x^2-4.

I wanted to see if I could force reduce to keep the terms in order by descending exponent. The documentation at
http://webwork.maa.org/wiki/Introduction_to_Contexts#Reduction_Rules
and
http://webwork.maa.org/wiki/Reduction_rules_for_MathObject_Formulas#.VY7gxEZTPE0

made me think that Option 2 would work. And in fact it did, in the sense that the terms weren't re-ordered; but the output was (-4)x^2+10x+(-4), so now reduce was failing to get rid of unnecessary parentheses.

So my question is, is it possible to set it up so that the output will actually be -4x^2+10x-4?


Attachment factor_problem1.png
In reply to Alice McLeod

Re: Question about Reduce (fiddly details!)

by Davide Cervone -
There reason you are not getting the negatives processed properly in option 2 is because the reduce() method returns a copy of the original formula with the reductions performed; it doesn't change the original. Since you have just used
    $poly->reduce("(-x)+y"=>0);
the reduced copy is produced, and then discarded. What you want is
    $poly = $poly->reduce("(-x)+y"=>0);
So you were so close!

You may also want to disable the (-x)-y reduction rule as well. This will prevent rewriting -4x-10 as -(4x+10).