## PREP 2014 Question Authoring - Archived

### Simplifying output of automatic differentiation ### Re: Simplifying output of automatic differentiation

by Davide Cervone -
Number of replies: 0
I get has an extra pair of parentheses, namely: -(sqrt(2x+3))+2 instead of -sqrt(2x+3)+2.

Your issue turns out to be from a different cause. The MathObjects library adds extra parentheses in its output to try to make it painfully clear to the student what the structure of the expression is. For example, sin(x)^2 is shown as [sin(x)]^2 so it is clear that the whole expression is squared, not just the x.

Your situation is coming from that process. You can do

    Context()->operators->set(fn => {parenPrecedence => 6.5})

to fix this. (The parenPrecedence tells when extra parens are added around functions based on the precedence of the operator that surrounds them. 6.5 puts it between the precedence of unary minus and exponentiation, so that you still get [sin(x)]^2 but not -[sin(x)].)

I give you a modified version of your problem below. There are several small changes. One thing to keep in mind is that the names of the reduction formulas must be given exactly, so your " x-(-y)" (which has an extra leading space) does nothing. In any case, I don't think you want that one since, you do want x-(-3) to be converted to x+3, I assume. (Your $c can be negative in "$b * x - $c"). Similarly, your "-a"=>0 specification does nothing since there is no rule with the name "-a". Did you mean "-n"? In any case, I would not recommend that you remove that one in this case. The parenPrecedence is what is needed, here. So here is the modified version: loadMacros( "PGstandard.pl", # Standard macros for PG language "MathObjects.pl", ); TEXT(beginproblem()); Context("Numeric");$a = -1;      #non_zero_random(-3, 3, 1);            # vertical stretch and reflection
$b = non_zero_random(-2, 2, 1); # horizontal stretch and reflection$c = random(-4, 4, 1);                              # horizontal shift
$d = random(-4, 4, 1); # vertical shift Context()->operators->set(fn => {parenPrecedence => 6.5}); Context()->reduction->noreduce("(-x)-y","(-x)+y");$parent = Formula("sqrt(x)");
$fx =$parent->substitute(x => "$b * x -$c")->reduce;
$fxy = Formula("$a * $fx +$d")->reduce;

Context()->texStrings;
BEGIN_TEXT
Given the function $$f(x) = fxy$$ and its parent function $$y = parent$$, the mid-formula is  $$y = fx$$
END_TEXT