# Ordering of positive and negative terms in output

Consider the following input and output:

In:

Solve[(1 - a) x == (2 - a) y, x]

Out: $$\left\{\left\{x\rightarrow\frac{(-2+a)y}{-1+a}\right\}\right\}$$

There are two things I don't like about this output (the first fairly generic, and the second more specific to my application):

1. the fact that the negative term is written first makes the expression less compact than the alternative:$$\frac{(a-2)y}{a-1}.$$ I find this alternative to be typographically neater.

2. If $a$ is a probability, it is guaranteed to be no greater than 1. I think it makes semantically more sense to write the output as $$\frac{(2-a)y}{1-a}.$$ Obviously, Mathematica doesn't know that $0\leq a\leq 1$, but I supplied the terms written as $2-a$ rather than $a-2$ and it would be nice if it were to preserve this ordering to some extent.

Thus, my questions are

1. Why does mathematica write $-2+a$ in the solution rather than $a-2$?

2. Is there a way to get mathematica to arrange output so that terms start with a leading "$-$" as rarely as possible?

3. If there a way to tell Mathematica "$a\in[\underline{a},\overline{a}]$; please use this information, where possible, to arrange output such that terms are written as (positive x positive) rather than (negative x negative)?"

• Mathematica has a canonical ordering behind the scenes. Since Plus is Orderless, Plus[a, -2] will always become Plus[-2, a], because numbers come before symbols in the canonical ordering. Furthermore, when Mathematica simplifies, it's figure of merit for simplifying includes LeafCount, which is a proxy for the complexity of the expression, and ((-2 + a) y)/(-1 + a) has a smaller LeafCount than ((2 - a) y)/(1 - a). Now, there are ways to display this more nicely, if all you care about is displaying. Maybe I'll search for an example Q&A that answers your question. – march Sep 27 '16 at 16:01
• TraditionalForm will do what you want, but take care to only use it for display purpose, eg do TraditionalForm[solution = Solve[(1 - a) x == (2 - a) y, x]] so that solution holds the unformatted expression. – george2079 Sep 27 '16 at 18:53
• @march : I'm not sure if this is due to Plus being Orderless, since if you evaluate ClearAttributes[Plus, Orderless], a - 2 still becomes -2 + a. Also, I'm not sure if it's due to LeafCount either, since a - 2 and -2 + a both have a LeafCount of 3. – theorist Dec 30 '16 at 23:41
• @george2079 : As I understand it, this is a concern if do what you illustrated (applying TraditionalForm to an input) (and is also a concern if you copy-and-paste TraditionalForm output into a new input), but is not an issue if you globally set output to TraditionalForm in Preferences -> Evaluation. For more information, see "tutorial/HowInputAndOutputWork" in MMA's help documentation. – theorist Dec 30 '16 at 23:48

Solve[(\[FormalX] - a) x == (\[FormalY] - a) y, x] /. Here, I used the fact that the formal symbols come before the alphabet symbols, i.e., Ordering[{\[FormalX],a}] gives {1, 2}. This causes the output of Solve to have the desired order when using the formal symbols instead of numerical values. The latter are then substituted only after Solve has produced a formal result.