1
$\begingroup$

Suppose a computation gives the polynomial as its output. I'd like to know how to format the output similarly to the input line of the screenshot below.

Mathematica's output likes to combine fractions, and when the expressions are much more involved and symbolic, this makes it more difficult to read off.

Thanks in advance!

How do I get Mathematica to format the output like the input is formatted

$\endgroup$
1
  • $\begingroup$ Apart? $\endgroup$
    – Natas
    Aug 29, 2020 at 10:44

2 Answers 2

2
$\begingroup$

Mma has its internal rules concerning the order of the terms in expressions. It is possible, but very difficult to override this order. Generally, it is not recommended.

Nevertheless, I often use such transformations on the very last stage of my calculations in order to bring the expression to the form most comfortable to look at. This may be important to reduce errors and simplify work.

Here is your expression:

expr = 5/32*x + 7/11*x^5

enter image description here

You might do something like this

expr /. a_*x^n_. :> HoldForm[a]*HoldForm[x^n]/. HoldForm[x^1] :> HoldForm[x]

enter image description here

Try.

Do not forget that after this action it is impossible to make any further operations unless you remove the HoldForm by applying ReleaseHold[%]to the result.

Have fun!

$\endgroup$
4
  • $\begingroup$ Awesome, thanks!! This was very helpful getting me on the right track. The actual application was multivariate but a function based on your initial idea got the job done. I'll add an answer with the function in case it's helpful, or there are suggestions. I'm new-ish to using stack exchange, so let me know if that is/isn't in line with usual procedure. $\endgroup$
    – Jaffe42
    Aug 30, 2020 at 16:34
  • $\begingroup$ That's perfectly OK. $\endgroup$ Aug 30, 2020 at 17:56
  • $\begingroup$ This gives an undesirable result when 7 is replaced by -7. Then + remains and it is followed by a negative sign and then the fraction. Is there a fix for this problem? $\endgroup$
    – wdacda
    May 14, 2023 at 3:14
  • $\begingroup$ @wdacda In this case one should slightly change the code. For example,like this: rule1 = a_*x^n_. :>If[a >= 0, HoldForm[a]*HoldForm[x^n], -HoldForm[Evaluate[Abs[a]]]* HoldForm[x^n]]; rule2 = HoldForm[x_^1] :> HoldForm[x] . Then 5/32*x - 7/11*x^5/.rule1/.rule2 yields the desired answer. Have fun! $\endgroup$ May 14, 2023 at 16:57
0
$\begingroup$

As per @Alexei's suggestion to use HoldForm, here's a function that formats a polynomial the way I was looking for. In this case it's for hard-coded 3 variables, but could probably be straightforwardly extended to use the length of var to do so automatically:

formatPolynomial[poly_, vars_ : {p, q, pdq}] := Module[{ dim, coeffs, output },
    dim = Max[Cases[CoefficientRules[poly, vars], v_?VectorQ :> Total[v], 2]] + 1;
    coeffs = CoefficientList[poly, vars, {dim, dim, dim}];
    output = Sum[coeffs[[i, j, k]] Subscript[tmp, i - 1, j - 1, k - 1], {i, 1, 
 dim, 1}, {j, 1, dim, 1}, {k, 1, dim, 1}];
    output = output /. {a_ Subscript[tmp, n_, m_, l_] :> HoldForm[a] Subscript[tmp, n, m,l]} /. {Subscript[tmp, n_, m_, l_] :> vars[[1]]^n vars[[2]]^m vars[[3]]^l} /. {a_ HoldForm[b_] :> HoldForm[({b} // MatrixForm)] a};
    output
]

This gives output so the coefficients of individual monomials (which I'm looking for) are easily identifiable: enter image description here

$\endgroup$
3
  • $\begingroup$ Perhaps formatPolynomial[poly_, v_] := Expand @ Collect[poly, v, HoldForm] is simpler? $\endgroup$
    – Carl Woll
    Aug 30, 2020 at 16:50
  • $\begingroup$ Whoa, that's much simpler!!! I didn't realize Collect could take a third argument like that. Would you like to post a new answer with this so I can accept it? $\endgroup$
    – Jaffe42
    Aug 31, 2020 at 1:20
  • $\begingroup$ This is great, with a but: How do I get ordered powers. For example Expand@Collect[3/8 - (15 x^2)/4 + (35 x^4)/8, x, HoldForm] gives x^2 (-15/4) +3/8 + x^4 (35/8) $\endgroup$
    – wdacda
    May 14, 2023 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.