3

I want Mathemtica to stop manipulating my polynomials! I mean, I want the output of

Print[3 x + 5 + x^2]

to be just $3x+5+x^2$, not $5+3x+x^2$ as Mathematica returns.

How can I do this?

Michael E2
  • 235,386
  • 17
  • 334
  • 747
Behzad
  • 575
  • 2
  • 11
  • 1
    Maybe wrapping it in Defer[] will do what you want? (I guess it will depend on how you want to use the expression.) – Daniel Lichtblau Jul 03 '14 at 22:05
  • @DanielLichtblau As I said below eldo's answer, if we have parameters, there will be another problem: commands a=-3; Print[Defer[ax-5+x^2]] yield $ax-5+x^2$, not $-3x-5+x^2$. – Behzad Jul 03 '14 at 22:14
  • 2
    @Behzad You can't both have and not have evaluation ... What is your use case for this exactly? Understand that will help come up with a solution. Generally, consider everything you enter into Mathematica as program code, not a mathematical expression. The way Mathematica works implies that this transformation will take place and it's not a good idea to try to change that. What I suggest instead is to have two steps: 1) one processing step where you manipulate symbolic expressions, letting Mathematica work the way it does 2) and a formatting step where you print them to specification. – Szabolcs Jul 03 '14 at 22:17
  • 3
    Specifically for your example, With[{a=3}, Print@HoldForm[a x - 5 + x^2]] does work, but I don't know if it's general enough for your use case. – Szabolcs Jul 03 '14 at 22:19
  • 1
  • 1
  • After looking at about a dozen related questions I picked one that I think is closest to what you want. I have marked this question as a duplicate of that question. Please review it, and its answers, and tell me if your problem is solved therein. – Mr.Wizard Jul 04 '14 at 00:31
  • @Mr.Wizard Please practice your gentle ministry to close down questions more hesitantly. The question you picked (after having reviewed many other archeological sites) is a question of which you were the prime respondent. – eldo Jul 04 '14 at 02:24
  • @eldo Yes, it is. Unfortunately I don't have a good memory, and actually writing in answer is one of the few things I seems to (sometimes) remember, so I am biased toward Questions that I have answered. If you have another Question in mind that would be a better fit please let me know! – Mr.Wizard Jul 04 '14 at 02:39
  • @Mr.Wizard, I don't have another question in mind, but I feel that THIS question has an elegant and new answer with Kuba's comment. Therefore, it shouldn't be disqualified in one way or the other. Also, the Print- statement complicates things here. – eldo Jul 04 '14 at 03:00
  • @eldo, Kuba's comment does not handle the OP's comment re a x - 5 + x^2. – Michael E2 Jul 04 '14 at 04:02
  • @I added another Question to my related list that is perhaps closer still. (Last in line.) I'll leave this as is until Behzad clarifies his full intent, unless someone has an Answer he wishes to post in which case I shall reopen. – Mr.Wizard Jul 04 '14 at 04:27

2 Answers2

7

Will

a = -3; Print[Defer[\[FormalA] x + 5 + x^2] /. \[FormalA] -> a]

-3 x + 5 + x^2

work for you?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
4

What about

Print[HoldForm[3 x + 5 + x^2]]

?

eldo
  • 67,911
  • 5
  • 60
  • 168
  • Well, if we have parameters, there will be another problem: commands a=-3; Print[HoldForm[ax-5+x^2]] yield $ax-5+x^2$, not $-3x-5+x^2$. – Behzad Jul 03 '14 at 22:13
  • 1
    @Behzad There will always be a problem unless you post full context :) this answer fits the question. You can use With to inject in held expressions but I suppose it may still be a problem with sign marks. With[{a = -3}, HoldForm[a x - 5 + x^2]] – Kuba Jul 03 '14 at 22:19
  • @Kuba Interesting question though, because fun = 3 x + 5 + x^2; and then Print[HoldForm@fun] doesn't work. – eldo Jul 03 '14 at 22:26
  • @eldo With Set it is already lost so even if you inject it it will be formated :) but with SetDelayed you can do: ClearAll[fun]; fun := 3 x + 5 + x^2; and then HoldForm[fun] /. OwnValues@fun – Kuba Jul 03 '14 at 22:30
  • @kuba Brilliant ! If OP wants this, he could even append a // Print after /. OwnValues@fun. – eldo Jul 03 '14 at 22:44