## WeBWorK Problems

### getting a numerical MathObject to display its formula in text ### getting a numerical MathObject to display its formula in text

by Dick Lane -
Number of replies: 1
Although Compute can cause a formula to be displayed as CORRECT (for a student to see after the assignment closes), it does not do that within formatted text.  Parser.pl mentions a way to have a typeset formula in text, but at the price of creating a separate object --- I thought one of the design objectives for MathObjects was to avoid that.

Context( 'Numeric' ) ;
Context() -> flags -> set(reduceConstants => 0,reduceConstantFunctions => 0) ;
$a = Compute( "sqrt(3)/2" ) ;$b = $a -> {original_formula} ; If ANS($a -> cmp) is used, then the typeset version of \frac{\sqrt{3}}{2} is displayed as CORRECT, but using $$a$$ in the text of a solution [after Context() -> texStrings;] just displays 0.866025.  (Shouldn't "reduceConstantFunctions => 0" have inhibited that decimal?)  On the other hand, $$b$$ will display the typeset fraction including the surd.

Is there another flag or option to the Context which I need to set in order to avoid having to keep a synchronized pair of lists (one set of objects for numerical checking, the other set for display of exact expressions) ? ### Re: getting a numerical MathObject to display its formula in text

by Dick Lane -
I want to recant about half-way for the first paragraph (and a bit more for the last).

One list/array of MathObjects can be used, an item selected, and then it alone is put thru the ->{original_formula} filter to get the typeset-able version.  The benefit to programming effort can vary with the structure of the problem.

In a problem about converting rectangular coordinates to spherical, I began with an array of [x,y,z,rho,theta,phi] 6-tuples which were chosen to be "nice", selected one 6-tuple, used its values for answer-checking and put just its items thru the ->{original_formula} filter for use in the problem statement and in a solution.  To offer a bit more variety to students, I used a scale factor for [x,y,z,rho] and discovered that "original formula" is not inherited.  This was easy to fix: just put the scale factor into the computations which produce the array of 6-tuples, e.g., rather than having [sqrt(3),0,1,2,0,pi/4] be extracted from the array and then scale [x,y,z,rho], have [a sqrt(3),0,a,2 a, 0, pi/4] be in the original array (Perl note: of array references).

I would still prefer to have TexString be the default behavior for a Computed object in a texString context, but ->{original_formula} did simplify my example greatly.

technical note about data structure for my example:
@DTA = ( [6-tuple of Computed items] , [another 6-tuple] , ... ) ;
$pick = random( 0 , scalar(@DTA) , 1 ) ; ($x, $y,$z, $rho,$theta, $phi) = @{$DTA[ \$pick ] } ;