# Substitution in expressions

As the title suggests, I want in an expression to to some substitutions. For example in the expression $$\mathcal{L}^+_0+\mathcal{L}^-_0+\mathcal{L}^+_0+\mathcal{L}^-_0+\mathcal{L}_1$$ I want to substitute $$\mathcal{L}^+_0+\mathcal{L}^-_0\rightarrow A$$ and $$\mathcal{L}^+_0+\mathcal{L}^-_0+\mathcal{L}_1\rightarrow B$$. Obviously I cannot solve for $$\mathcal{L}_0^i$$ and then substitute, because then it will substitute every $$\mathcal{L}_0^i$$.

I think that it can be solved with some short of pattern matching but my skills stop there.

Any ideas?

• This is far too generic. Show us an example of your expression in actual code. – MarcoB Mar 15 '19 at 19:13
• – MarcoB Mar 15 '19 at 19:25
• People here generally like users to post code as Mathematica code instead of just images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this this meta Q&A helpful – Michael E2 Mar 15 '19 at 21:46
• Somewhere on site I believe there's a simple answer that says if you want to apply substitution based on the identity a + b == c, instead of applying a + b -> c, use a -> b - c. But I can't find it. – Michael E2 Mar 15 '19 at 21:54

I'll use the notation $$\mathcal{L}_0^+=$$L0p, $$\mathcal{L}_0^-=$$L0m, $$\mathcal{L}_1=$$L1.

You could solve for the L expressions with

Solve[{L0p + L0m == A, L0p + L0m + L1 == B}, {L0p, L1}]


{{L0p -> A - L0m, L1 -> -A + B}}

and then substitute with

Expand[L0p + L0m + L0p + L0m + L1 /. {L0p -> A - L0m, L1 -> B - A}]


A + B

Alternatively,

Solve[{L0p + L0m == A, L0p + L0m + L1 == B}, {L0m, L1}]


{{L0m -> A - L0p, L1 -> -A + B}}

Expand[L0p + L0m + L0p + L0m + L1 /. {L0m -> A - L0p, L1 -> B - A}]


A + B

You seem to suggest that this method does not work for you. Can you give an example where this method gives the wrong answer?

• Hey thank you. Yeah, it's wrong to do it like this because the example that I gave is just representative. I expect to have even more complicated expressions than the one I gave, and therefore I can't base on this method. – hal Mar 16 '19 at 12:58
• Can you please give an example to work with? – Roman Mar 16 '19 at 13:35
• Hi and thanks for your response. Could you look above my discussion with kglr. There I explain what I want to get. If not I can provide you with more info. Thank you. – hal Mar 16 '19 at 14:00

You can use a combination of Defer (to prevent a + b + a + b + c from evaluating to 2 a + 2 b + c) and ReplaceRepeated :

ClearAll[a, b, c]
Defer[a + b + a + b + c] //. {a + b + c -> A, a + b -> B}


A + B

Alternatively, you can use Unevaluated in place of Defer:

Unevaluated[a + b + a + b + c] //. {a + b + c -> A, a + b -> B}


A + B

Or a sequence of ReplaceAlls in place of ReplaceRepeated:

Unevaluated[a + b + a + b + c] /. a + b + c -> A /. a + b -> B


A + B

You can also use a combination of HoldForm, ReplaceRepeated and ReleaseHold:

ClearAll[a, b, c]
HoldForm[a + b + a + b + c] //. {a + b + c -> A, a + b -> B} // ReleaseHold


A + B

• Hey, thank you for your answer. The first one seems to work but I have one question: what is //.? Whats the difference with ./? Additionally what happens if for example I have the expression Defer[Pi a + Pi b + a + b + c] //. {a + b + c -> B, a + b -> A}? In this case due to Pi the substitution doesn't take place any more.. – hal Mar 16 '19 at 13:05
• @hal, for //. (ReplaceRepeated) see the docs, the section Properties and Relations. If a, b, c appear with coefficients, you can try a more general pattern Defer[Pi a + Pi b + a + b + c] //. { a + b + c -> B, x_ a + x_ b :> x A} (or Defer[Pi a + Pi b + a + b + c] //. { a + b + c -> B, _. a + _. b :> A} if you the drop Pi) – kglr Mar 16 '19 at 13:17
• I have one more question though. Because the expression that I am going to apply these rules might have coefficient 1, when I apply the above pattern matching I want also to include this case but somehow it doesn't work. For example Defer[Pi a + Pi b + a + b + c] //. {c1_ a + c1_ b + c1_ c -> c1 B, c2_ a + c2_ b -> c2 A} doesn't work for the first substitution. Is there anything that I can do about that? – hal Mar 16 '19 at 13:40
• @kglr, the Default pattern _. a + _. b :> A matches too widely, should be something like i_. a + i_. b -> i A or even something like i_. a + j_. b :> With[{k = First@MinimalBy[{i, j}, Abs]}, k A + (i - k) a + (j - k) b]. – Roman Mar 16 '19 at 14:15
• @hal, try the pattern c1_. (which matches 1 for multiplication and 0 for addition): Defer[Pi a + Pi b + a + b + c] //. {c1_. a + c1_. b + c1_. c -> c1 B, c2_. a + c2_. b -> c2 A} // First – kglr Mar 16 '19 at 17:57

This Should Work i tested it despite i'm sorry i'm kind of beginner

l0 = Superscript[Subscript[L, 0], "+"];
l1 = Superscript[Subscript[L, 0], "-"];
l2 = Superscript[Subscript[L, 0], "+"];
l3 = Superscript[Subscript[L, 0], "-"];
l4 = Subscript[L, 1];

s1 == l0 + l1 /. s1 -> a;
s2 == l2 + l3 + l4 /. s2 -> b;

s3 = s1 + s2

• sometimes using /. Would be fast way to subtitle or replace something – Alrubaie Mar 15 '19 at 20:16
• Have you tried evaluating your code more than once? Because you define s1 as a function of itself, you get different results each time. – MarcoB Mar 15 '19 at 20:47
• you right while this surprised me ! its still work fine unless our friend want to go further with the code or show his full question so we help further! – Alrubaie Mar 15 '19 at 20:57
• i should have done == not = on what you said – Alrubaie Mar 15 '19 at 21:39
• try it now work just fine – Alrubaie Mar 15 '19 at 21:40