4

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$$

3Take 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 – 2017-08-08T13:18:21.560

@Kuba: Thanks, just what I was seeking. – Joseph O'Rourke – 2017-08-08T13:25:54.910

2So 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 – 2017-08-08T13:33:13.370

How does one force evaluation of a FullForm expression? When z is the FullForm Times[2,x,Plus[2,x]], Evaluate[z] leaves it unaltered. – Joseph O'Rourke – 2017-08-08T13:39:55.903

There isn't really such a thing as "a FullForm expression". When you evaluate FullForm[expr], it will show you the internal structure of expr (but this output should not / cannot be used for further computations). Can you clarify what you meant with your last comment, through a concrete example? – Szabolcs – 2017-08-08T13:43:05.977

@Szabolcs: "but this output should not / cannot be used for further computations"---This is precisely what I would like to do. There must be a way... – Joseph O'Rourke – 2017-08-08T13:57:44.613

Will you be subtracting anything, or will there be negative quantities? How about divisions? – J. M.'s ennui – 2017-08-08T14:02:41.827

1@J.M.: no subtraction or division. – Joseph O'Rourke – 2017-08-08T14:05:36.640

2@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 – 2017-08-08T14:08:37.400

... FullForm[expr]. When you input something: Plus[a,b] and a+b are two different ways to write the very same thing. There will be no difference between the data structures that Mathematica constructs from these two textual descriptions. When Mathematica outputs something, it can also write it in multiple equivalent ways. FullForm[expr] will print differently than simply expr: The FullForm wrapped is hidden (but still present!—and this is what makes it "unusable for computation"), and the internal structure is printed explicitly. – Szabolcs – 2017-08-08T14:11:51.220

@Szabolcs: Thank you, yes, I do/did have a misunderstanding. But now I see that z/.FullForm-> Evaluate does lead to what I want. – Joseph O'Rourke – 2017-08-08T14:12:47.013

2What 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 – 2017-08-08T14:13:18.040

2@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 – 2017-08-08T14:14:32.570

@JosephO'Rourke Yes, z /. FullForm -> Evaluate technically works, but in practice it doesn't do more than First[z]. Just use FullForm to look at things, but don't assign its output to a variable and do not try to re-use it. – Szabolcs – 2017-08-08T14:14:33.997

@Kuba: Thanks for everyone's help! – Joseph O'Rourke – 2017-08-08T14:18:18.990

6

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) *)


3I 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 – 2017-08-08T14:23:51.380

@J.M. Good point. Thanks! – Anton Antonov – 2017-08-08T16:25:31.827

3

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.