Format

The lines below ensure a polynomial is written in the opposite order from the usual Format.

Unprotect[Plus] ;    Format[HoldPattern[Plus[p__]]] := Module[{s1, s2},   s1 = Hold[p] ... }]/;  OrderedQ[s1] && (s1 =!= s2)] ;    Clear[x] ;    Expand[(2 + x)^4]

x^4 + 8 x^3 + 24 x^2 + 32 x + 16

Before moving on the normal convention is restored.

Format[HoldPattern[Plus[p__]]] =.    ; Protect[Plus] ;

Another application of Format is given below. A user once wrote to the MathGroup asking how one could ensure rational numbers greater than one are displayed as improper fractions.  P.J. Hinton of Wolfram Research gave the solution below to change the way rational numbers are formatted.

Unprotect[Rational] ; Format[Rational[num_Integer, den_Integer]] := DisplayForm[ᡝ ... [Mod[num, den]], ToString[den]] } ] ] Protect[Rational] ;    14/3 - 1

32/3

Before continuing we the next cell should be evaluated to remove the above formatting rule.

Unprotect[Rational] ; FormatValues[Rational] = {} ; Protect[Rational] ;


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page