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.
How does one suppress terms with coefficient 0 in text display?
by Michele Titcombe - Number of replies: 4
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);
will do the same thing as $a = random(-1,1,2) * random(2,5,1); We plan to go over more details such as these randomization functions in future workshop sessions.
Best regards,
Paul Pearson
In reply to Paul Pearson
Re: How does one suppress terms with coefficient 0 in text display?
by Michele Titcombe -
Excellent answers - both will be very helpful to me.
I have another (perhaps related to reduce?) question:
Is there a way to display in text the value of a variable as a fraction rather than as a decimal?
For example: say I have a variable $ans = Compute("$a+1/$b"); where $a and $b are nonzero randomized and $b is not 1. In the text of a problem, I'd like to display $ans as an improper fraction, not a decimal approximation.
I have another (perhaps related to reduce?) question:
Is there a way to display in text the value of a variable as a fraction rather than as a decimal?
For example: say I have a variable $ans = Compute("$a+1/$b"); where $a and $b are nonzero randomized and $b is not 1. In the text of a problem, I'd like to display $ans as an improper fraction, not a decimal approximation.
In reply to Michele Titcombe
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;
should do what you ask.