# List of all Variables that match an specific pattern

I'd like to make a list of all constants that match an specific pattern, wich includes all terms that multiply an specific variable. For example, I'd like to take all terms that multiply t in:

(2 + 2 x) Cos[2 w t + 4 e^2 t/3 + ArcTan[2 x/s] + 2 t + 4 r/h^n]


matching this pattern: a*Cos[f + w t], and I would have:

{a,f,w}={2 + 2 x,
ArcTan[2 x/s] + 4 r/h^n,
2 w+ 4 e^2 /3+2}


I tried this:

exp = (2 + 2 x) Cos[2 w t + 4 e^2 t/3 + ArcTan[2 x/s] + 2 t] //.a_ Cos[f_ + w_ t] :> {a, f, w}

output:{2 + 2 x, 4 h^-n r + (4 e^2 t)/3 + 2 t w + ArcTan[(2 x)/s], 2}


But it didn't work, since it takes only one term... This looks to work better, but still not what I want:

exp = (2 + 2 x) Cos[2 w t + 4 e^2 t/3 + ArcTan[2 x/s] + 2 t + 4 r/h^n] //.a_ Cos[f_ + w : t*(___) ..] :> {a, f, w}

output:{2 + 2 x, 4 h^-n r + ArcTan[(2 x)/s], 2 t, (4 e^2 t)/3, 2 t w}

• Of course I could do something like: ...{a, f, Simplify[Plus[w]/t]}..., but it would be a horrible solution Mar 30, 2016 at 14:58

expr = (2 + 2 x) Cos[2 w t + 4 e^2 t/3 + ArcTan[2 x/s] + 2 t + 4 r/h^n];

Replace[expr,
a_ Cos[f_ + w : Repeated[_ t]] :> {a, f, Coefficient[+w, t]}
]

(* {2 + 2 x,
4 h^-n r + ArcTan[(2 x)/s],
2 + (4 e^2)/3 + 2 w} *)

exp = (2 + 2 x) Cos[ 2 w t + 4 e^2 t/3 + ArcTan[2 x/s] + 2 t + 4 r/h^n];
exp /. Cos[a_] :> Cos[Collect[a, t]] /. a_ Cos[f_ + w_ t] :> {a, f, w}
(*
{2 + 2 x,
4 h^-n r + ArcTan[(2 x)/s],
2 + (4 e^2)/3 + 2 w}
*)

• Thank you very much, but the reason I can't use Simplify if because my expression is very complicated, so Simplify would take too long... The expression i used here is just an example Mar 30, 2016 at 16:34
• @Fábio Well, it is not possible to verify if a replacement rule works with an unknown expression ... Mar 30, 2016 at 16:40
• Well, maybe I can give you some more information: although the expression is very complicated, the variable t is only present multiplied by simple constants... So, my expression would bem something like: (complicated_expression1)*Cos[(Complicated_expression2) -(2*c*Pi/l)*t + wb*t] Mar 30, 2016 at 16:44
• @Fábio You may try exp /. Cos[a_] :> Cos[Collect[a, t]] /. a_ Cos[f_ + w_ t] :> {a, f, w} Mar 30, 2016 at 16:59