2
$\begingroup$

Maybe the way I expressed is not very accurate, but a very simple example will help to understand the question:

For

Subscript[a,i]+Subscript[a,j]**Subscript[a,k]

I would like to replace a pattern that a_i shows exactly one time with some other symbol.

An intuitive way fails to work using Except:

Subscript[a, i] + Subscript[a, j] ** Subscript[a, k] /. 
 Except[Subscript[a, x_] ** Subscript[a, y_], Subscript[a, z_]] -> zz

The output is zz + zz ** zz. However, I would like it to be zz+Subscript[a, j] ** Subscript[a, k].

The reason I need this is that I want to drop the linear term but keep the quadratic term of a_i by replacing only the linear term of a_i to zero.

$\endgroup$
3
  • 1
    $\begingroup$ Look at Subscript[a, i] + Subscript[a, j] ** Subscript[a, k] // FullForm. That should explain why the pattern does not match. $\endgroup$ Apr 12, 2020 at 2:28
  • $\begingroup$ @Rohit Namjoshi, Thanks! I understand why it fails now. But is there a workaround that can realize my idea? $\endgroup$
    – Jake Pan
    Apr 12, 2020 at 3:12
  • 1
    $\begingroup$ Subscript[a, i] + Subscript[a, j] ** Subscript[a, k] /. Plus[a___, b_Subscript, c___] :> a + zz + c $\endgroup$
    – LouisB
    Apr 12, 2020 at 4:23

1 Answer 1

4
$\begingroup$

You can also do

Subscript[a, i] + Subscript[a, j] ** Subscript[a, k] /.
  {a_NonCommutativeMultiply :> a, Subscript[a, z_] -> zz}
  zz + Subscript[a, j] ** Subscript[a, k]

because ReplaceAll >> Details:

  • "ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part or on any of its subparts.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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