WeBWorK Problems

nicestring and fractions with contextFraction.pl

nicestring and fractions with contextFraction.pl

by Peter Staab -
Number of replies: 9
First of all, I've been using the contextFraction.pl to rewrite many existing problems of mine (and kudos to that package). I'm trying to make the output look as nice as possible (generally to avoid confusion). For example, if

$a = Fraction(5,2);
$b = Fraction(-6,3);
$f = Compute("$a*x+$b");

Then in output such as \( f(x)=$f \), there are extra set of parentheses around the $b term (as the fraction is reduced).

If I use nicestring to perform output, such as

$g = nicestring([$a,$b]);

The output such as \( f(x)= $g\), has the fraction written in a linear form as 5/2x-2, which students will see as the x in the denominator.

I can live with the first output (with the extra parentheses), however, I prefer to have output in standard form as I would write it down.


Peter
In reply to Peter Staab

Re: nicestring and fractions with contextFraction.pl

by Davide Cervone -
MathObjects know how to produce both their TeX strings and also algebra-style strings (like what students need to enter). By default, MathObjects stringify themselves using the algebra style strings, so when you do something like $f = Formula("$g / $h"), you get a correct formula (given MathObects $g and $h). Note that this usually involves the insertion of an extra set of parentheses, since for example, if $g = Formula("1+x"), then $f = Formula("$g^2") would produce $f = Formula("(1+x)^2") as expected rather than $f = Formula("1+x^2").

This is usually the correct thing to do when converting a MathObject to a string, but there is an important case where that is the wrong thing to do. It is when the MathObject is used in math mode within a BEGIN_TEXT/END_TEXT block, as in your case above. In that case, you would want the MathObject to produce its TeX version instead. That could be done using the rather verbose \( f(x) = \{$f->TeX\} \) construct, but there is an easier way.

If you use the command

    Context()->texStrings;
before the BEGIN_TEXT and
    Context()->normalStrings;
after the END_TEXT that will cause the \( f(x) = $f \) to work as you probably intend.

Hope that helps.

Davide

In reply to Davide Cervone

Re: nicestring and fractions with contextFraction.pl

by Peter Staab -
I have the commands:
 Context()->texStrings;


 Context()->normalStrings;

in the appropriate places. After some additional playing I understand this a bit better, but can't get precisely what I'd like. For example if I use "contextFraction.pl" and have:

$c=Fraction(1,3);
$d=Fraction(-2,3);
$g=Compute("$c*x+$d");

Then \( y=$g\)produces nice output as intended, however if the Fraction reduces to an integer (and negative) such as:

$c=Fraction(1,3);
$d=Fraction(-6,3);
$g=Compute("$c*x+$d");

(which sometimes occurs with random numerators), then the output \( y=$g \) looks like y=(1/3)x+(-2) [except that the 1/3 is like \frac{1}{3}. ] And I would like to see it as y=(1/3)x-2.

If instead, I replace $d with -2, such as:

$c=Fraction(1,3);
$d=-2;
$g=Compute("$c*x+$d");

Then the result is the same as the 2nd case, so it appears that Fraction(-6,3) reduces to the integer -2, however, the TeX output with the integer (-2) is not formatted nicely.

To improve the formatting for linear functions (and polynomial), I generally use the nicestring function, however this strips TeX formatting out of fractions it appears. I've tried various combinations with nicestring, but to no avail.

Peter


In reply to Peter Staab

Re: nicestring and fractions with contextFraction.pl

by Davide Cervone -
The behavior concerning (-2) in the output is actually correct. MathObjects don't automatically change the structure of a formula, so x+-2 will remain x+(-2) unless you say otherwise (this is because some problems want the original structure). You want to use the MathObject reduce() method to remove redundent sign changes and so on. So in your case, use
    $g = Compute("$c*x+$d")->reduce;
in order to turn the x+-2 into x-2.

At least that is what is supposed to happen. It turns out that there were problems with reduce() in the Fraction contexts, but I have fixed them. You will need to get the lasted version of contextFraction.pl in order to have reduce() work as expected.

Let me know if that doesn't work for you.

Davide

In reply to Davide Cervone

Re: nicestring and fractions with contextFraction.pl

by Peter Staab -
Davide,

I just upgraded Webwork via CVS (from the server at rochester), although I don't think that gave me a new version. The date-stamp seems to be 2009-06-25. Did you just change something in the past few days? Do you add a date-change or a version inside the file? I couldn't find anything.

Also, when I run the version I have there seems to be a problem when I change the line:
$g=Compute("$c*x+$d");

to

$g=Compute("$c*x+$d")->reduce;

in that the webserver just stalls out. (Once I think I got an error about an infinite loop, but I can't seem to replicate it.)


Peter
In reply to Peter Staab

Re: nicestring and fractions with contextFraction.pl

by Davide Cervone -
You have the old version. (There was an infinite loop in the reduce function, which is one of the things that I fixed. The Fraction context overrides negation, and that confused the reduction rules.)

You need to get the latest HEAD version, but you probably just got the rel-2-4-7 version. Try

    cvs update -A contextFraction.pl
and see if that doesn't do it for you.

Davide

In reply to Davide Cervone

Re: nicestring and fractions with contextFraction.pl

by Peter Staab -
Davide,

I have a new version now. At least, I think so, the time-stamp is 8/04 (today, when I updated). The previous version I had was in June.

It still seems to in a infinite loop. Again the lines of code that are doing it are:

$c = Fraction(1,3)
$d = -2;
$g = Compute("$c*x+$d")->reduce;

If I don't apply "reduce", then there are no problems.

Peter
In reply to Peter Staab

Re: nicestring and fractions with contextFraction.pl

by Davide Cervone -
Still sounds like you have the old version. Do
    cvs status contextFractions.pl
and see what version you have. It should be 1.7, but I suspect you have an earlier one. If you do, try
    cvs update -r HEAD contextFraction.pl
to get the latest version.

Davide

In reply to Davide Cervone

Re: nicestring and fractions with contextFraction.pl

by Peter Staab -
Davide,

It looks like I have the current version (1.7). I actually updated to the current version of everything in pg directory (I don't know if this could be an issue--If you think so I could revert back to the 2.4.7 release).

Are there other packages that contextFraction.pl depends on? I'm guessing mathObjects.pl, which is version 1.10.

Peter




In reply to Peter Staab

Re: nicestring and fractions with contextFraction.pl

by Davide Cervone -
Well, I updated to 2.4.7 to see if that was it, but it still works for me. Did you restart the server after updating contextFraction.pl?

It still sounds to me like the old version is in place.

If you are sure it's not, then can you send the pg file and the seed that you are using and I'll test with that.

Davide