Message Boards Message Boards

GROUPS:

The replacement problem

Posted 1 year ago
2517 Views
|
3 Replies
|
2 Total Likes
|

For this problem

The answer should be -Subscript[[Delta], 2], but it remains the original form. Can someone answer my question? Thank you very much!

3 Replies
Posted 1 year ago

If the structure of the pattern is not the same then the pattern will usually not match and the replacement will not be done, even if the expressions are mathematically equivalent.

-1+2*Subscript[λ,1]^2*Subscript[λ,2]^4-Subscript[λ,1]^4*Subscript[λ,2]^8/.{
 1-2*Subscript[λ,1]^2*Subscript[λ,2]^4+Subscript[λ,1]^4*Subscript[λ,2]^8->Subscript[δ,2]}

returns

-1+2*Subscript[λ,1]^2*Subscript[λ,2]^4-Subscript[λ,1]^4*Subscript[λ,2]^8

If the structure is the same then the pattern will usually match and the replacement will be done.

-1+2*Subscript[λ,1]^2*Subscript[λ,2]^4-Subscript[λ,1]^4*Subscript[λ,2]^8/.{
-1+2*Subscript[λ,1]^2*Subscript[λ,2]^4-Subscript[λ,1]^4*Subscript[λ,2]^8->-Subscript[δ,2]}

returns

-Subscript[δ,2]

Sometimes Simplify can be used to replace more complicated mathematically equivalent expressions with simpler expressions, even if the structure is not exactly the same

Simplify[
-1+2*Subscript[λ,1]^2*Subscript[λ,2]^4-Subscript[λ1]^4*Subscript[λ,2]^8,
 1-2*Subscript[λ,1]^2*Subscript[λ,2]^4+Subscript[λ,1]^4*Subscript[λ,2]^8==Subscript[δ,2]]

returns

-Subscript[δ,2]

You are misunderstanding how Rules work. It would go more like this:

expr = -1 + 2 Subscript[\[Lambda], 1]^2 Subscript[\[Lambda], 2]^4 - 
    Subscript[\[Lambda], 1]^4 Subscript[\[Lambda], 2]^8 /. {1 - 
      2 Subscript[\[Lambda], 1]^2 Subscript[\[Lambda], 2]^4 + 
      Subscript[\[Lambda], 1]^4 Subscript[\[Lambda], 2]^8 -> 
     Subscript[\[Delta], 2]};
rule = expr -> Subscript[\[Delta], 2];
expr /. rule

giving

Subscript[\[Delta], 2]

Besides the fact that rigid replacement can be problematic, I would like to remark one more pitfall (or misunderstanding) one might face when using Subscript (because you started your code nicely with Clear["Global*'"]- in general a good idea!). But have a look at this:

Subscript[\[Lambda], 1] = 42;
Clear["Global`*"]; ClearAll["Global`*"];
Subscript[\[Lambda], 1]
(*  Out:   42   *)

Any assignment made to Subscript stays uncleared here because its context is System'!

A good advice might be not to use Subscript at all - except for displaying reasons at the very end of a calculation.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract