6
$\begingroup$

I am making some random algebra equations, and I want to have the ordering be random too, such that if my random equation generator makes x+1, the output stays x+1, without reverting to 1+x.

I have tried ClearAttributes[Plus, Orderless], but it simply did not work - entering x+1 returns 1+x.

The following sort-of works:

Unprotect[Plus]; Format[Plus[a_, b_]] := ToString@a <> " + " <> ToString@b 

But it uses Unprotect, converts everything to strings, and also doesn't work when b is a fraction (fractions p/q are represented as p\n--\nq).

Is there a nicer way to do this?

Also, I plan to convert everything to TraditionalForm at the end, so using that to control ordering won't work either.

$\endgroup$
6
  • $\begingroup$ I found another solution, which is again not that great (as in it is super slow), but at least it gives me the results I want: Rasterize[TraditionalForm[a]]+Rasterize[TraditionalForm[b]] $\endgroup$ Commented Nov 4, 2012 at 19:18
  • $\begingroup$ What exactly are you trying to do? I see the problem but I don't understand the objective well enough to offer alternatives. Your suggested fix still makes x+1 turn into 1+x, right? $\endgroup$ Commented Nov 4, 2012 at 19:26
  • 1
    $\begingroup$ @Rojo actually, don't worry about it. I figured it out. plus = Row[{#1, " + ", #2}] &; was what I was looking for: x~plus~1 // TraditionalForm gives exactly what I want. $\endgroup$ Commented Nov 4, 2012 at 19:29
  • $\begingroup$ Yeah, that would work for 2 arguments, for more you can use Riffle. Row@Riffle[{##}, " + "] &. I would prefer using that as Format to plus more than as an ownvalue. Something like Format[plus[args__]] := Interpretation[HoldForm[Plus[args]], plus[args]] $\endgroup$ Commented Nov 4, 2012 at 19:49
  • 2
    $\begingroup$ Also, perhaps you would like wrapping your code in Module (or Block, depending on what you are doing) with Plus=plus, so you can use the + symbol at will. Module[{Plus = plus}, x + 1] $\endgroup$ Commented Nov 4, 2012 at 19:50

1 Answer 1

3
$\begingroup$

With the suggested edits from Rojo in the comments above, the following is what answers my question:

plus[args__] := Row[Riffle[{args}, " + "]] 

Then, Block[{Plus = plus}, x + 1 + i + 4 + z] // TraditionalForm returns:

enter image description here

$\endgroup$
5
  • $\begingroup$ Looks good, +1. The idea of using Interpretation however was so that the output, if used literally as input, would be interpreted as ´plus[x,1,y...]` if you wanted to further operate on that, even though it "looks like" plus. If you care about that then the second argument shouldn't be ´Row[Riffle...` but plus[args]. If you don't care about that then you can do without ´Interpretation´ and use your Row@Riffle or that HoldForm@Plus $\endgroup$ Commented Nov 6, 2012 at 5:53
  • $\begingroup$ @Rojo Yes - the whole point was just presentation anyway. But do you have any ideas as to why ClearAttributes didn't work? $\endgroup$ Commented Nov 6, 2012 at 6:54
  • $\begingroup$ sorry for the late response. I was planning on digging into it before answering, but got sidetracked and lazy. So far I hadn't thought of a good reason why, and my lack of humility makes my ignorance default to "bug" $\endgroup$ Commented Nov 19, 2012 at 19:05
  • 1
    $\begingroup$ Hmmm... what about if he has terms with negative coefficients? $\endgroup$ Commented Aug 16, 2013 at 23:56
  • $\begingroup$ @JohnD - It's interesting you pointed that out. That issue was actually resolved in another question. A combination of that answer and maybe some tinkering should do the job. $\endgroup$ Commented Aug 19, 2013 at 5:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.