1

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?

MarcoB
  • 67,153
  • 18
  • 91
  • 189
hal
  • 783
  • 3
  • 14

3 Answers3

1

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?

Roman
  • 47,322
  • 2
  • 55
  • 121
  • 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
  • 1
    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
1

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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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
  • 1
    @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
  • 1
    @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
  • @Roman, right; good point. For the case Pi a + Pi b + a + b + c, it may also make sense to have more specific Pi a + Pi b -> A or ``Pi a + Pi b -> Pi A` . – kglr Mar 16 '19 at 18:00
0

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
nufaie
  • 1,081
  • 6
  • 14