Message Boards Message Boards

0
|
5298 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

[?] Get polynomials in TraditionalForm to be ordered in descending order?

Posted 7 years ago

I would like polynomials in TraditionalForm to be ordered in descending order of degrees. All the polynomials that I am dealing with are in one variable. For instance, I have -5x+3. TraditionalForm outputs 3-5x. I found an article HERE that talks about a solution, but I can't get that solution to work. It says to do this:

poly=-5x+3;   
MonomialList[poly, x];
poly // TraditionalForm

But the output I'm getting is still 3-5x. What am I doing wrong?

Thanks in advance,

Mark Greenberg

POSTED BY: Mark Greenberg
4 Replies
Posted 7 years ago

Ah, yes. That does solve both the ordering problem and the typesetting issues. Thank you very much! I could not have written that code myself, at least not yet.

I also have found that wrapping the polynomial in HoldForm[] and then applying TraditionalForm[] does what I want... if I do that before storing the polynomial in a variable. Once it is in the variable, the order has been changed. It seems like there would be a way to turn off the reordering algorithm in TraditionalForm[], similar to the way we can turn off the SingleLetterItalics[] in TraditionalForm[].

Between your function and HoldForm[], I should be able to make things work the way I want.

Thanks again,

Mark

POSTED BY: Mark Greenberg

Here is a more complicated version:

nicePolynomial[poly_, x_] := 
  DisplayForm[
   RowBox[Flatten[
     List @@ (ToBoxes[
           Inactive[Plus] @@ 
            MonomialList[poly, x]] //. {{a___, TagBox[__], 
             RowBox[{"-", b___}], c___} :> {a, RowBox[{"-", b}], 
             c}, {a___, TagBox[__], 
             RowBox[{RowBox[{"-", b___}], c___}], d___} :> {a, 
             RowBox[{RowBox[{"-", b}], c}], d}} /. {TagBox[__] :> "+",
           " " :> Nothing} //. RowBox -> List)]]];
POSTED BY: Gianluca Gorni
Posted 7 years ago

Thank you, Gianluca, for the solution. Your code reminds me that there is so much more that I have yet to learn about the Wolfram Language.

This solution doesn't quite satisfy my needs. While it does arrange the terms in the order I need, it loses some of the nice typesetting. Specifically, minus signs are shifted to the right, leaving a gap to their left and too small of a space to their right, as if they were negative signs attached to the next coefficient. Also, the plus sign is greyed out even if I apply a style of Black, I'm guessing because of the Inactive[] function.

enter image description here

Thanks again for the help.

Mark Greenberg

POSTED BY: Mark Greenberg

Don't worry, formatting of polynomials has been a problem for decades. For your particular polynomial you get a nice-looking output with Inactive:

Inactive[Plus] @@ MonomialList[-5 x + 3, x]

but negative coefficients are a problem in other cases:

Inactive[Plus] @@ MonomialList[-5 x - 3, x]

Here is an attempt to get a nice formatting:

nicePolynomial[poly_, x_] := 
  DisplayForm[
   ToBoxes[Inactive[Plus] @@ 
      MonomialList[poly, x]] //. {{a___, TagBox[__], 
       RowBox[{"-", b___}], c___} :> {a, RowBox[{"-", b}], c},
     {a___, TagBox[__], RowBox[{RowBox[{"-", b___}], c___}], 
       d___} :> {a, RowBox[{RowBox[{"-", b}], c}], d}}];

It seems to work well in the few cases I tested it on, such as

nicePolynomial[-5 x - 3 - x^3, x]

Do not use the output for further calculations, though! It is only for display. An expert in box structures could do a better job.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract