Suppose I have an expression that employs addition and multiplication, but no subtraction or division: $$2 a + 5 b x^3 + c \;.$$ I would like to change this to $$(2 + a) (5 +b + x +x +x) c \;,$$ where within each term, the products have become sums, and at the top level, the sums have become products. Is there a clever way to access the representation of the expression and perform the operator swapping?

z = FullForm[2 a + x^5];
Print[z];
z = z /. {Times -> Plus, Plus -> Times, Power -> Times};
Print[z];
Print[Evaluate[z]];


prints:

Plus[Times[2,a],Power[x,5]]
Times[5,Plus[2,a],x]
Times[5,Plus[2,a],x]


whereas I was hoping for $$5 (2 + a) x$$

• Take a look at FullForm[2 a + 5 b x^3 + c] then lookup ReplaceAll documentation. Keep in mind: FullForm[a - b] so either you will go fully consistent or you have to introduce exceptions. – Kuba Aug 8 '17 at 13:18
• So what Kuba said would mean 2 a + 5 b x^3 + c /. {Times -> Plus, Plus -> Times, Power -> Times}. But do take great care to only have positive integers, and no - or / showing up ... the representation of those is less uniform. – Szabolcs Aug 8 '17 at 13:33
• @JosephO'Rourke I think you may have a fundamental misunderstanding about the structure of expressions, but due to the limitation of comment lengths, it is hard for me to see what it is exactly and try to correct it. Suppose you have an expression expr=a+b. When you type FullForm[expr], this does not change expr in any way. It simply reveals its internal structure. FullForm (and other *Form functions) are only useful for printing the same expressions in different ways. If you want to replace Times with Plus, you still need to operate on expr itself, and not ... – Szabolcs Aug 8 '17 at 14:08
• What this means in practice: use FullForm, InputForm, MatrixForm, or other similar functions only to print the same thing in a different way. In the vast majority of cases the output from these need not be reused for computation—they just change the way expressions are printed (for readability). – Szabolcs Aug 8 '17 at 14:13
• @JosephO'Rourke you will get into a lot of confusion by using random procedures you have not understood. /. FullForm -> Evaluate is as good as First while the latter is not confusing. Also, don't strip FullForm, just don't use it. – Kuba Aug 8 '17 at 14:14

Using With you can get a more interactive feel:

With[{Times = Plus, Plus = Times, Power = Times},
2 a + 5 b x^3 + c]

(* (2 + a) c (5 + b + 3 x) *)

• I think this is safer than replacement. For the sufficiently paranoid, you can wrap the expression in Hold[], make the injections with With[], and ReleaseHold[] afterwards. – J. M.'s ennui Aug 8 '17 at 14:23
• @J.M. Good point. Thanks! – Anton Antonov Aug 8 '17 at 16:25

I'm not convinced you're asking for a well defined operation. Consider the ambiguities of $0$'s that can be considered always added to any expression, and $1$'s that are always multiplied to any expression.

• Do you want $5+0 \mapsto 5$ or $0$?
• Do you want $5\times1\times1 \mapsto 5$ or $7$? (Note e.g. $5 x^2 \mapsto 5+2 x$ in your example)
• Should $25 a \mapsto (25 + a)$ or $(5+5+a)$?

A natural attempt that reproduces your above example is something like:

    expr=2 a + 5 b x^3 + c;
expr/. {Times :> Plus, Plus :> Times, Power :> Times}


But you should know there are tons of glitches with this type of game, many related to the type of ambiguities mentioned above, how to handle negative numbers, etc. If you really want to play it, and you chase down all your use cases, you may still want to consider some sort of canonicalization procedure (e.g. ExpandAll) before your operation.