I have an expression:

{((-2*m*n + B*R0^2)*Sin[t])/(4*Pi*R0), -1/4*((-2*m*n + B*R0^2)*Cos[t])/(Pi*R0), 0}

And I would like it to recognize this pattern

%/.{-Sin[t],Cos[t],0}-> ET

such that

{((-2*m*n + B*R0^2)*Sin[t])/(4*Pi*R0), -1/4*((-2*m*n + B*R0^2)*Cos[t])/(Pi*R0), 0} /.{-Sin[t],Cos[t],0}-> ET

should result in

-ET (-2*m*n + B*R0^2)/(4*Pi*R0)

or something the like. It however does not work. Who can help?


2 Answers 2


I think your result has a typo. Instead of -ET (2*m*n + B*R0^2)/(4*Pi*R0) it should read -ET (-2*m*n + B*R0^2)/(4*Pi*R0), otherwise there is no solution.

A further problem is the minus sign in front of the Cos term. MMA always uses the full form of an expression for matching. Look at:

((-2 m n + B R0^2) Cos[t])/(4 \[Pi] R0) // FullForm
(* Times[Rational[1,4],Power[Pi,-1],Power[R0,-1],Plus[Times[-2,m,n],Times[B,Power[R0,2]]],Cos[t]] *)


-(((-2 m n + B R0^2) Cos[t])/(4 \[Pi] R0)) // FullForm
(* Times[Rational[-1,4],Power[Pi,-1],Power[R0,-1],Plus[Times[-2,m,n],Times[B,Power[R0,2]]],Cos[t]] *)

You see, the minus is integrated in the Rational[-1,4]. Therefore, for matching, the minus can not be matched separately. Here is how you can do it:

{((-2*m*n + B*R0^2)*Sin[t])/(4*Pi*R0), -1/4*((-2*m*n + B*R0^2)*Cos[t])/(Pi*R0),  0} /. { x__ Sin[t], y__ Cos[t], 0} :> ET y

enter image description here

  • $\begingroup$ I have corrected the wrong minus sign, it was typo. That the integration of the sign into the fraction hampers the pattern detection is a bit odd, but I suppose there have to be some limits in pattern detection otherwise any pattern would be everywhere I suppose. Thank you! $\endgroup$ Dec 10, 2022 at 17:33

Consider vector projection using the dot product. Let v be the input vector and p be the pattern:

v = {((-2*m*n + B*R0^2)*Sin[t])/(4*Pi*R0), -1/4*((-2*m*n + B*R0^2)*Cos[t])/(Pi*R0), 0};
p = {-Sin[t], Cos[t], 0};

Then the parts of the vector which are in line with the given pattern are:

Simplify[v.p/Norm[p], Element[t, Reals]]

(2 m n - B R0^2)/(4 π R0)

You may multiply this by ET, if you wish. For other expression the Simplify may not be necessary to achieve a nice result, and I have assumed t is a real number here for simplicity. If that is not necessarily the case, this method may not be appropriate.

Note also that the non-conforming parts of the vector are given by:

Simplify[v-v.p/Norm[p], Element[t, Reals]]

{0, 0, 0}

In general two vectors will not be perfectly aligned with each other, so this term is normally non-zero. This is the part of your vector which does not co-align with your given pattern.

  • $\begingroup$ OK, I see, I have also tried with Solve[b p = v, b] which also works. $\endgroup$ Dec 10, 2022 at 13:12

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.