I'm trying to solve for the output of a simple RC low-pass filter, with Cos[w t] input. The convolution of the input with the RC transfer function gives me the right output, but I'd like to be able to automate getting it in the right equivalent form. Specifically, a phase-shifted Cosine function.

The convolution right now is in the form:

h = Cos[t w]/(1 + tau^2 w^2) + (tau w Sin[t w])/(1 + tau^2 w^2)

My first crack at automating the transformation is:

j[q_] := q /. b__ Cos[x__] + a__ Sin[x__] -> Sqrt[a^2 + b^2] Sin[x + Pi/2 + ArcTan[b/a]];


But, this produces the following insane output:

Sqrt[tau^w^(1/(1 + tau^2 w^2)^2) + 1/(1 + tau^2 w^2)^2]
  Sin[t w + Pi/2 + ArcTan[tau^w^(1 + tau^2 w^2)/(1 + tau^2 w^2)]]

Any idea why the value of a isn't what I'm anticipating?

  • $\begingroup$ Do you get what you expect if you change a__ to a_ and b__ to b_? $\endgroup$
    – kglr
    Oct 19, 2014 at 6:57
  • $\begingroup$ WITCHCRAFT. Also, thank you. $\endgroup$ Oct 19, 2014 at 6:59
  • $\begingroup$ welcome to Mathematica.SE. I assume the change gave the desired result? $\endgroup$
    – kglr
    Oct 19, 2014 at 7:03
  • $\begingroup$ It works perfectly. Dare I ask why? $\endgroup$ Oct 19, 2014 at 7:08
  • $\begingroup$ ladlibertine, examine the simpler cases: (1a) a b c Sin[x] /. a__ Sin[_] :> a versus (2a) a b c Sin[x] /. a_ Sin[_] :> a, then (1b) a b c Sin[x] /. a__ Sin[_] :> a^2 versus (2b) a b c Sin[x] /. a_ Sin[_] :> a^2. Then, try ff[a__] := a^2 and evaluate ff[a,b,c]. If you haven't already seen it you will find the tutorial Patterns Overview extremely useful. $\endgroup$
    – kglr
    Oct 19, 2014 at 7:26

2 Answers 2


Changing a__ and b__ to a_ and b_, respectively,

j2[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> {{a}, {b}, {x}};


$\left( \frac{\tau w}{\tau ^2 w^2+1}, \frac{1}{\tau ^2 w^2+1}, t w \right) $

and, the same change in OP's function j

j3[q_] :=   q /. b_ Cos[x__] + a_ Sin[x__] :>  Sqrt[a^2 + b^2] Sin[x + Pi/2 + ArcTan[b/a]];


$ \sqrt{\frac{\tau ^2 w^2}{\left(\tau ^2 w^2+1\right)^2}+\frac{1}{\left(\tau ^2 w^2+1\right)^2}} \cos \left(t w+\tan ^{-1}\left(\frac{1}{\tau w}\right)\right) $

which, per OP's comment, is the desired result.

Some observations on 'why':

The combination of BlankSequence (__) and Power is the source of the "insane" output the OP got. Power and BlankSequence appear together on the right-hand-side of OP's ReplaceAll in two separate terms: in Sqrt[a^2 + b^2] and, at a deeper level, in ArcTan[b/a].

First, while

ja1[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> a

gives the expected result (in TeXForm)

$\frac{\tau w}{\tau ^2 w^2+1}$

ja2[q_] := q /. b_ Cos[x__] + a__ Sin[x__] :> a


Sequence[tau, w, 1/(1 + tau^2 w^2)]

The contribution of Power to the "insanity" is due to the fact that:

Power[x,y,z, ... ]] is taken to be Power[x,Power[y,z, ... ]]. see: Power > Details

That is,

Sequence[a, b, c]^2


$ a^{b^{c^2}} $


Sequence[tau, w, 1/(1 + tau^2 w^2)]^2


enter image description here

which is what we get from

jb2[q_] := q /. b_ Cos[x__] + a__ Sin[x__] :> a^2


jb1[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> a^2

gives, as desired,

$ \frac{\tau ^2 w^2}{\left(\tau ^2 w^2+1\right)^2} $

Note that many other functions (other than Power), say Log, would produce an error message.

  • $\begingroup$ This, and the exercises you outlined above, have clarified so much. Thank you, thank you, thank you for taking the time to help me out. $\endgroup$ Oct 19, 2014 at 16:35
  • $\begingroup$ @ladlibertine, my pleasure. Really glad it was useful for you. $\endgroup$
    – kglr
    Oct 19, 2014 at 16:39
  • $\begingroup$ @ladlibertine, btw just noticed that this is your first day on the site -- welcome to Mathematica.SE! Please consider visiting the tour page on the basic rules of the site, and editing help page for editing help. $\endgroup$
    – kglr
    Oct 19, 2014 at 16:46

Pattern matching takes place on (something close to) the FullForm of the expression rather than the display form that you see. You can visualize it using TreeForm:

h // TreeForm

enter image description here

I am not sure what you are attempting but I imagine your pattern was not written with this in mind. What parts did you expect to match a__, b__, and x__? What actually matched was:

h /. b__ Cos[x__] + a__ Sin[x__] :> {{a}, {b}, {x}}
{{tau, w, 1/(1 + tau^2 w^2)}, {1/(1 + tau^2 w^2)}, {t w}}
  • $\begingroup$ Thank you so much for the quick response (and the edit to my question)! I was hoping that a__ would match with something like Times[tau, w, 1/(1 + tau^2 w^2)] (and b__ with 1/(1 + tau^2 w^2), x__ with w t). Looking at the TreeForm output, I guess my problem is that a__ is a combination of terms from different levels? Is there a way I can better define a__ to avoid this issue? $\endgroup$ Oct 19, 2014 at 6:47
  • $\begingroup$ @ladlibertine it appears that kguler described the problem in detail so I shall assume your question is answered unless you say otherwise. $\endgroup$
    – Mr.Wizard
    Oct 19, 2014 at 17:55

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