## PREP 2013 Question Authoring - Archived

### Simplifying Coefficients in Derivatives

by Stacy Hoehn -
Number of replies: 4
Is there a way to simplify the coefficients generated using the built-in derivative abilities for formulas?  When I display the derivative of $$x^3 + 2x^2+5x$$ in the problem below, it shows up as $$3x^2+2*2x+5$$, not $$3x^2+4x+5$$.

########################################################################

DOCUMENT();

"PGstandard.pl",     # Standard macros for PG language
"MathObjects.pl",
"parserFormulaUpToConstant.pl",
#"source.pl",        # allows code to be displayed on certain sites.
#"PGcourse.pl",      # Customization file for the course
);

# Print problem number and point value (weight) for the problem
TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1; ############################################################## # # Setup # # Context("Numeric");$a = random(1,10,1);
$b = random(1,10,1);$c = random(1,10,1);

$f = Formula("$a*x^3 + $b*x^2 +$c*x")->reduce;
$df =$f->D('x');

$antideriv = FormulaUpToConstant("$f");

##############################################################
#
#  Text
#
#

Context()->texStrings;
BEGIN_TEXT

a.  An antiderivative of $$df$$ is \{ans_rule()\}.
$PAR b. The general antiderivative of $$df$$ is \{ans_rule()\}.$PAR

$BITALIC Note: Use "C" for any arbitrary constant of integration in your answers.$EITALIC

END_TEXT
Context()->normalStrings;

##############################################################
#
#
#

ANS( $f->cmp(upToConstant=>1) ); ANS($antideriv->cmp() );

ENDDOCUMENT();

### Re: Simplifying Coefficients in Derivatives

by Paul Pearson -
Hi Stacy,

I thought that adding another reduce might do the trick:

$df =$f->D('x')->reduce;

but sadly it did not. Perhaps someone else has a helpful suggestion.

Nota bene: Students do not have to enter the letter "C" as an arbitrary constant. Other letters like "k", "K", or anything that's not "x" will also be accepted by webwork as a constant of integration.

Good luck!

Paul Pearson

### Re: Simplifying Coefficients in Derivatives

by John Travis -
This may be a Davide-required solution.  For the record, adding the macro "contextLimitedPolynomial.pl" and then using

Context("LimitedPolynomial");

does not help either.

JT

### Re: Simplifying Coefficients in Derivatives

by Davide Cervone -
The reason you are getting 2*(2x) from 2x^2 is that MathObjects is performing the differentiation via the constant multiple rule followed by the power rule, so you get (2x^2)' = 2(x^2)' = 2*(2x*x') = 2*(2x).

Unfortunately, this is not a form that MathObject knows how to reduce further. If it were (2*2)x, it would make it 4x, but 2*(2x) is structurally different, and it doesn't have a reduction rule for that.

MathObjects is not a computer algebra system, and its reduction rules are mostly designed around getting rid of coefficients of 1, or adding 0, or double negatives, or adding a negative, and so on. There are lots of situations that it doesn't know how to handle. It would be possible to extend the reduction rules, but that is the current state of things.

On the other hand, this particular issue can be addressed in a sneaky way. Paul is right that calling reduce() again won't help (the derivative method already does that). But here's a trick that does help.

Because the two multiplications in 2*(2x) have the same precedence, MathObjects doesn't show the parentheses, and so writes this as 2*2x. If it were to parse that expression, it would do it as (2*2)x, and so would reduce the constant. So the trick is to ask MathObjects to reparse the resulting expression.

For example,

    $f = Compute("x^3 + 2x^2+5x")->D;$f = Compute("$f");  Here, "$f" in quotes will force \$f to turn itself into a string, and then that string is passed to Compute() to be parsed again from scratch. The result should be 3x^2+4x+5, because the original nesting is lost in the conversion to the string.

This doesn't work for all expressions, but for polynomials, it should do what you need.

Davide