0
$\begingroup$

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}
$\endgroup$
1
  • $\begingroup$ Of course I could do something like: ...{a, f, Simplify[Plus[w]/t]}..., but it would be a horrible solution $\endgroup$
    – Fábio
    Mar 30, 2016 at 14:58

2 Answers 2

1
$\begingroup$
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} *)
$\endgroup$
1
$\begingroup$
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}
*)
$\endgroup$
4
  • $\begingroup$ 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 $\endgroup$
    – Fábio
    Mar 30, 2016 at 16:34
  • $\begingroup$ @Fábio Well, it is not possible to verify if a replacement rule works with an unknown expression ... $\endgroup$ Mar 30, 2016 at 16:40
  • $\begingroup$ 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] $\endgroup$
    – Fábio
    Mar 30, 2016 at 16:44
  • 1
    $\begingroup$ @Fábio You may try exp /. Cos[a_] :> Cos[Collect[a, t]] /. a_ Cos[f_ + w_ t] :> {a, f, w} $\endgroup$ Mar 30, 2016 at 16:59

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.