3

If I have the polynomial x^2+y^2 in maxima, then maxima will display it like y^2+x^2. Is there a way to specify that the variable x will be displayed before the variable y instead?

Kasper
  • 12,594
  • 12
  • 41
  • 63
  • `ordergreat` and `mainvar` do the job. See this question https://stackoverflow.com/q/36456544/349708, which I believe is the same. – Eelvex Oct 16 '17 at 14:48
  • @Eelvex Thanks, trying that, but now if I try `x^2+x*y+y^2`, I get `x^2+y*x+y^2` – Kasper Oct 16 '17 at 15:45

2 Answers2

5

Maxima has a built-in idea about ordering variables which is pretty strong and imposed on all expressions in an effort to regularize them for simplification. In some ways this ordering is not the same as the conventional ordering that is typically used for calculations by hand, but one can get accustomed to it. My advice to just accept it as it is -- it's pretty hard to get Maxima to change its mind about it.

That said, the global variable powerdisp governs the display of "+" expressions. powerdisp is false by default. When powerdisp is true, the order of "+" expressions is reversed.

(%i1) x^2 + y^2;
                                     2    2
(%o1)                               y  + x
(%i2) x^2 + y^2 + 2*x*y;
                                 2            2
(%o2)                           y  + 2 x y + x
(%i3) powerdisp; 
(%o3)                                false
(%i4) powerdisp:true;
(%o4)                                true
(%i5) %o2;        
                                 2            2
(%o5)                           x  + 2 x y + y
(%i6) %o1;
                                     2    2
(%o6)                               x  + y

Note that powerdisp is a blunt instrument -- it reorders all "+" expressions. You can decide if that's acceptable.

I don't recommend using mainvar or ordergreat. That fixes the problem only for specific variables.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • Would it be possible to have a better solution if you are only interested in having the variables to be ordered differently (in alphabetical order) in the tex output? I don't care so much about the order when working with maxima, but when I display it to my students, they find it confusing. I use maxima to autogenerate worked out latex solutions for exercises. – Kasper Oct 17 '17 at 08:02
  • Yes, it's possible to change only the TeX output. The clearest way to do that requires a little bit of Lisp programming. Does `powerdisp:true` have the desired effect? Or there is still some other ordering desired? – Robert Dodier Oct 17 '17 at 17:52
  • Powerdisp seems to be quite good, the only problems I have so far is with exponents. If I set `powerdisp: true` then, `x^3+x^2+2*x*y+y^2` becomes `x^2+x^3+2*x*y+y^2` – Kasper Oct 19 '17 at 10:23
  • 1
    OK, I see what you mean. Let me think about it a bit. – Robert Dodier Oct 19 '17 at 16:53
2

I've figured out a way to reverse the ordering of symbols in "+" expressions, so that a goes before z. But for any given symbol, exponents are ordered the same as they are now, i.e., in decreasing order. I've posted code for this as a Github project: https://github.com/robert-dodier/maxima-mplus-symbols-reversed

There is one file of Lisp code. You can clone the Git repo or just download the one file from the project page mentioned above. Please give it a try and let me know what you think. This code changes the 2-d and 1-d console displays, and also the TeX output. I'll show only the 2-d output below; the other outputs have the same ordering of terms.

Examples:

(%i1) load("form-mplus-symbols-reversed.lisp") $
(%i2) reverse_symbols_order:true $
(%i3) x^2+y^2;
                                     2    2
(%o3)                               x  + y
(%i4) x^3+x^2+2*x*y+y^2;
                              3    2    2
(%o4)                        x  + x  + y  + 2 x y

That's a little disappointing -- it's customary to put mixed terms in between x terms and y terms. But it doesn't seem possible to achieve that without a more complicated sorting heuristic, as the placement of 2*x*y relative to y^2 seems to depend on the presence or absence of x^2 -- we can't order the terms by comparing them two at a time (as in the present implementation).

For comparison here is the default ordering for the preceding expression:

(%i5) reset (reverse_symbols_order) $
(%i2) x^3+x^2+2*x*y+y^2;
                              2            3    2
(%o2)                        y  + 2 x y + x  + x
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • Thanks! That seems indeed already a great improvement on powerdisp. I will try it out in our code, and see what the students think. It would be indeed ideal if 2*x*y could be placed in between. – Kasper Oct 31 '17 at 09:03