3
$\begingroup$

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)?"

$\endgroup$
  • 2
    $\begingroup$ 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. $\endgroup$ – march Sep 27 '16 at 16:01
  • 1
    $\begingroup$ 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. $\endgroup$ – george2079 Sep 27 '16 at 18:53
  • $\begingroup$ @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. $\endgroup$ – theorist Dec 30 '16 at 23:41
  • 1
    $\begingroup$ @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. $\endgroup$ – theorist Dec 30 '16 at 23:48
3
$\begingroup$

The default order of terms is determined based on lexicographic sorting rules that can in principle be exploited to get the output in just the right form:

Solve[(\[FormalX] - a) x == (\[FormalY] - a) y, x] /. 
    {\[FormalX] -> 1, \[FormalY] -> 2}

output

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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