## PREP 2014 Question Authoring - Archived

### How does one suppress terms with coefficient 0 in text display?

by Michele Titcombe -
Number of replies: 4
Or how does one suppress the 1 as a multiplicative constant ?

For example, if you have a function such as $f=Compute("$a*x**2 + $b*x + c"); where$a, $b$ and $c are randomized coefficients that could take on values of 0 or 1. Instead of a function displaying as 0x^2+1x+2 (in appropriate pretty print), I'd like it to display x+2. In reply to Michele Titcombe ### Re: How does one suppress terms with coefficient 0 in text display? by Davide Cervone - The Formula object class has a reduce() method that will remove terms with coefficients of 0 and remove coefficients of 1 (among other things). So try $f = Compute("$a x^2 +$b x + $c")->reduce;  and see if that works better. In reply to Davide Cervone ### Re: How does one suppress terms with coefficient 0 in text display? by Paul Pearson - Hi Michele, Also, if you want to ensure that the leading coefficient is nonzero, you can use another randomization method that we haven't yet taught all of you. For example,$a = non_zero_random(-5,5,1);

would select a non-zero random number from -5 to 5 with increment 1, i.e., a random number from the list -5, -4, -3, -2, -1, 1, 2, 3, 4, 5.  If you want to ensure that the leading coefficient is not 1 or -1, you could instead use something like

$a = random(-1,1,2) * random(2,5,1); to produce a random integer from the list -5, -4, -3, -2, 2, 3, 4, 5. The random(-1,1,2); function will return either -1 or +1 (since the increment is 2) and the random(2,5,1); function will return 2, 3, 4, or 5. You could also use the list_random function in which all possible list items are specified in the arguments to the function. So, for example,$a = list_random(-5,-4,-3,-2,2,3,4,5);

### Re: How does one suppress terms with coefficient 0 in text display?

by Davide Cervone -
There are two things at play, here. The first is that Compute() does just what it says, it computes the value of the equation and returns that. The result is a real number, not a formula, and real numbers don't have a tree structure like a formula does.

On the other hand, Compute() does first parse the string as a formula, and then if it is constant it returns that constant value rather than the formula itself. But Compute() does retain the original formula and makes it available via the original_formula property of the returned MathObject. So the original formula could be obtained from

    $ans->{original_formula}  in your case. The problem, however, is that, by default, constant values within a formula are reduced automatically, so the results of 1/$b and the addition of $a will be performed during the parsing of the formula, so the original formula will also just be the final number. You can control that automatic evaluation using some flags set in the Context. So  Context()->flags->set( reduceConstants => 0, reduceConstantFunctions => 0, );  will prevent reducing of constant operations and constant function calls, leaving the original formula as the numeric expression you have in mind. That means  Context()->flags->set( reduceConstants => 0, reduceConstantFunctions => 0, );$a = non_zero_random(-5,5,1);
$b = random(2,10,1);$ans = Compute("$a+1/$b");

Context()->texStrings;
BEGIN_TEXT
The answer is $$ans->{original_formula}$$.
END_TEXT
Context()->normalStrings;