1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ This is far too generic. Show us an example of your expression in actual code. $\endgroup$ – MarcoB Mar 15 at 19:13
  • $\begingroup$ Perhaps look into: Can I simplify an expression into form which uses my own definitions?, and Replace expressions with symbols? $\endgroup$ – MarcoB Mar 15 at 19:25
  • $\begingroup$ 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 $\endgroup$ – Michael E2 Mar 15 at 21:46
  • 1
    $\begingroup$ 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. $\endgroup$ – Michael E2 Mar 15 at 21:54
1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – hal Mar 16 at 12:58
  • 1
    $\begingroup$ Can you please give an example to work with? $\endgroup$ – Roman Mar 16 at 13:35
  • $\begingroup$ 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. $\endgroup$ – hal Mar 16 at 14:00
1
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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.. $\endgroup$ – hal Mar 16 at 13:05
  • 1
    $\begingroup$ @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) $\endgroup$ – kglr Mar 16 at 13:17
  • $\begingroup$ 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? $\endgroup$ – hal Mar 16 at 13:40
  • 1
    $\begingroup$ @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]. $\endgroup$ – Roman Mar 16 at 14:15
  • $\begingroup$ @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 $\endgroup$ – kglr Mar 16 at 17:57
0
$\begingroup$

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
$\endgroup$
  • $\begingroup$ sometimes using /. Would be fast way to subtitle or replace something $\endgroup$ – Alrubaie Mar 15 at 20:16
  • $\begingroup$ Have you tried evaluating your code more than once? Because you define s1 as a function of itself, you get different results each time. $\endgroup$ – MarcoB Mar 15 at 20:47
  • $\begingroup$ 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! $\endgroup$ – Alrubaie Mar 15 at 20:57
  • $\begingroup$ i should have done == not = on what you said $\endgroup$ – Alrubaie Mar 15 at 21:39
  • $\begingroup$ try it now work just fine $\endgroup$ – Alrubaie Mar 15 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.