[Mathematica] Formatting output of polynomials

In summary, I'm having problems formatting polynomials in a standard way in Mathematica. I generate them randomly and would like them output using Print in a particular format. Say I have:w^4 (-5 z^2 - 5 z^3)I can for example use TraditionalForm to get:w^4 \left(-5 z^3-5 z^2\right)+w^3 (-3 z-9)+w^2 (5 z+4)+w \left(8-2 z^2\right)-7 z-2but that's really not the way I want it. I'd like to output it in the form:(-
  • #1
jackmell
1,807
54
Hi guys,

I seem to still be having problems formatting polynomials in a standard way in Mathematica. I generate them randomly and would like them output using Print in a particular format. Say I have:

theFunction=-2 + w^3 (-9 - 3 z) - 7 z + w^2 (4 + 5 z) + w (8 - 2 z^2) +
w^4 (-5 z^2 - 5 z^3)

I can for example use TraditionalForm to get:

[tex]w^4 \left(-5 z^3-5 z^2\right)+w^3 (-3 z-9)+w^2 (5 z+4)+w \left(8-2 z^2\right)-7 z-2[/tex]

but that's really not the way I want it. I'd like to output it in the form:

[tex](-2-7z)+(8-2z^2)w+(4+5z)w^2+(-9-3z)w^3+(-5z^2-5z^3)w^4[/tex]

with the parenthesis. I've tried CoefficientList to get the coefficients directly and then put the polynomial back together the way I want it but Mathematica changes it and doesn't keep the parenthesis around the w^0 term and will also place the w^n term first in some of the terms. Here's the code I'm using to generate the polynomials:

Code:
degree = 5;
bitsize = 5;
thenumber = 
  IntegerDigits[RandomInteger[{1, 2^(bitsize (degree + 1))}], 2, 
   bitsize (degree + 1)];
orderTable = Table[0, {degree + 1}];
mylist = Table[
   aseq = Take[thenumber, {bitsize (n - 1) + 1, bitsize n}];
   atemp = Table[If[aseq[[j]] != 0,
      RandomInteger[{-9, 9}] Power[z, j - 1], "A"], {j, 1, bitsize}];
   Subscript[a, n - 1] = Plus @@ DeleteCases[atemp, _String]
   , {n, 1, degree + 1}];

theFunction = Plus @@ Table[Subscript[a, n] w^n, {n, 0, degree}]
TraditionalForm[theFunction]

You guys have any ideas how to change it so that I can Print the output in the form I described above?
Thanks,
Jack
 
Last edited:
Physics news on Phys.org
  • #2
Here's a quick hack

Code:
polyForm[poly_, var_] := 
 Module[{coeffs = CoefficientRules[poly, var] // Sort},
  Interpretation[Row[Table[Row[{"(", coeff[[2]], ")", w^coeff[[1, 1]] /. 1 -> ""}], 
                                     {coeff, coeffs}], "+"], poly]]

Then

Code:
theFunction = -2 + w^3 (-9 - 3 z) - 7 z + w^2 (4 + 5 z) + 
                              w (8 - 2 z^2) + w^4 (-5 z^2 - 5 z^3);

polyForm[theFunction, w]
(-2 - 7 z) + (8 - 2 z^2) w + (4 + 5 z) w^2 + (-9 - 3 z) w^3 + (-5 z^2 - 5 z^3) w^4

It's only really for displaying the polynomial, since the `Interpretation` thing only works for cutting and pasting (that is `t = polyForm[poly, var]` does not work). If you worked at a lower level with `InterpretationBox` then you could get it working more smoothly.
 
Last edited:
  • #3
Ok. That works fine. I only want it for displaying. The reason is so that I can easily relate the algebra to the geometry whereas when it's not in that easy-to-recognize form sometimes I mis-interpret the geometry based on the algebra only to realize after some work that I didn't notice a term.

Thanks!
 

1. How do I format the output of a polynomial in Mathematica?

To format the output of a polynomial in Mathematica, you can use the TraditionalForm function. This will display the polynomial in a traditional mathematical notation, with exponents and coefficients properly aligned.

2. Can I customize the formatting of a polynomial in Mathematica?

Yes, you can customize the formatting of a polynomial in Mathematica by using the Format function. This allows you to define your own rules for how the polynomial should be displayed.

3. How do I display only certain terms of a polynomial in Mathematica?

You can use the Series function in Mathematica to display only certain terms of a polynomial. For example, Series[poly, {x, 0, n}] will display the first n terms of the polynomial poly around x = 0.

4. Is there a way to rearrange the terms of a polynomial in Mathematica?

Yes, you can use the Collect function to rearrange the terms of a polynomial in Mathematica. This function collects terms with the same powers of a given variable and displays them together.

5. How do I convert a polynomial to a different form in Mathematica?

You can use the Expand function in Mathematica to convert a polynomial to a different form. This function expands out all the terms in the polynomial and simplifies it according to algebraic rules.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
Back
Top