1
$\begingroup$

I have question related to pattern matching of a complex algebraic equations. If I am about to extract certain terms from an algebraic expressions that contains mixture of terms $\nabla_\gamma 𝑓_{\alpha\beta}$ and $𝑓_{\alpha\beta}$ as in:

$𝑒π‘₯π‘π‘Ÿ=β‹―\times\nabla_\gamma 𝑓_{\alpha\beta}+β‹―\times 𝑓_{\alpha\beta},$

and in code as

𝑒π‘₯π‘π‘Ÿ = -(I*Subscript[A, 2, 1]*Subscript[f, 1, 2]*(Cross[v, B] +   El)) + 
   I*Subscript[A, 1, 2]*Subscript[f, 2, 1]*(Cross[v, B] + El) - 
   (1/2)*Grad[Subscript[f, 1, 2], k]*
  Cross[-2*I*\[Epsilon]*Subscript[A, 2, 1], B] - 
   (1/2)*Grad[Subscript[f, 2, 1], k]*
  Cross[2*I*\[Epsilon]*Subscript[A, 1, 2], B] - 
   (1/2)*I*Subscript[A, 1, 2]*Subscript[f, 1, 1]*
  Cross[-2*I*\[Epsilon]*Subscript[A, 2, 1], B] + 
   (1/2)*I*Subscript[A, 2, 1]*Subscript[f, 1, 1]*
  Cross[2*I*\[Epsilon]*Subscript[A, 1, 2], B] + 
   (1/2)*I*Subscript[A, 1, 1]*Subscript[f, 1, 2]*
  Cross[-2*I*\[Epsilon]*Subscript[A, 2, 1], B] - 
   (1/2)*I*Subscript[A, 2, 2]*Subscript[f, 1, 2]*
  Cross[-2*I*\[Epsilon]*Subscript[A, 2, 1], B] - 
   (1/2)*I*Subscript[A, 1, 1]*Subscript[f, 2, 1]*
  Cross[2*I*\[Epsilon]*Subscript[A, 1, 2], B] + 
   (1/2)*I*Subscript[A, 2, 2]*Subscript[f, 2, 1]*
  Cross[2*I*\[Epsilon]*Subscript[A, 1, 2], B] + 
   (1/2)*I*Subscript[A, 1, 2]*Subscript[f, 2, 2]*
  Cross[-2*I*\[Epsilon]*Subscript[A, 2, 1], B] - 
   (1/2)*I*Subscript[A, 2, 1]*Subscript[f, 2, 2]*
  Cross[2*I*\[Epsilon]*Subscript[A, 1, 2], B] + 
   I*\[Epsilon]*Subscript[A, 1, 2]*Grad[Subscript[f, 2, 1], r] - 
   I*\[Epsilon]*Subscript[A, 2, 1]*Grad[Subscript[f, 1, 2], r] + 
   Grad[Subscript[f, 1, 1], k]*(-(Cross[v, B] + El)) + 
 v*Grad[Subscript[f, 1, 1], r] + 
   Grad[Subscript[f, 1, 1], t]

how can I use multiple patterns to do the job. I tired the following two patterns to extract diagonal terms for instance

pat1 = h_*Grad[Subscript[f, x_, y_] /; x != y, _];
pat2 = h_*(Subscript[f, x_, y_] /; x != y);

and they work fine if I use DeleteCases twice on the original expression

expr1=Plus @@ Flatten@DeleteCases[expr, pat1, All];
expr2=Plus @@ Flatten@DeleteCases[expr1, pat2, All];
Print[expr2]

But is there a more efficient way to do this?

$\endgroup$
1
  • 1
    $\begingroup$ Please include code for expr. $\endgroup$
    – Syed
    Commented Sep 25, 2023 at 8:02

2 Answers 2

1
$\begingroup$

You can avoid the doubled DeleteCases by specifying the two patterns as Alternatives. Also, Flatten and All are not needed in the given case.

Plus @@ DeleteCases[expr, pat1 | pat2]

result == expr2

True

$\endgroup$
0
0
$\begingroup$

Generally, use of derivatives with patterns don't work, because the patterns are evaluated and produce 0 if the expression is not visibly dependent on the variable.

Use HoldPattern as a wrapper e.g

 expr /.{ HoldPattern[ Grad[a_,{t__}] :> Subscript[\[Nabla],t ] . a

 Cases[expr,HoldPattern[ D[Subscript[_,_,_],t__]]

Another way is the use of Wrapper Inactive[Grad] on the operators in the formula, that is semantically identical but does nothing on evaluation.

More than one pattern can be supplied by Alternatives (_ | _ |...)

  Select[expr, (MatchQ[#,  pat1 | pt2]&)] 
$\endgroup$
4
  • $\begingroup$ Your Select doesn't give the expected result - even after replacing pt2with pat2 $\endgroup$
    – eldo
    Commented Sep 25, 2023 at 8:52
  • $\begingroup$ As far as I see the original expression evaluates all derivatives to zero. $\endgroup$
    – Roland F
    Commented Sep 25, 2023 at 9:30
  • $\begingroup$ @Ronald F In the original expression the function is $f (r,k,t)$ so the derivatives survive. None of this solutions provide my desired result which must have terms that are all diagonal in $f$. $\endgroup$
    – Harken
    Commented Sep 25, 2023 at 10:44
  • $\begingroup$ I have a suspicion that utilizing the Alternatives (_|_|...) approach in this scenario will yield the correct answer. I am seeking a method to combine both the pattern (pat1 and pat2) with boolean logic using and. Is there a way to integrate the pattern with the and operator? $\endgroup$
    – Harken
    Commented Sep 25, 2023 at 10:56

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.