Changing a__
and b__
to a_
and b_
, respectively,
j2[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> {{a}, {b}, {x}};
j2[h]
gives
$\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]];
j3[h]
gives
$ \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
ja1[h]
gives the expected result (in TeXForm
)
$\frac{\tau w}{\tau ^2 w^2+1}$
ja2[q_] := q /. b_ Cos[x__] + a__ Sin[x__] :> a
ja2[h]
gives
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
is
$ a^{b^{c^2}} $
and
Sequence[tau, w, 1/(1 + tau^2 w^2)]^2
is
which is what we get from
jb2[q_] := q /. b_ Cos[x__] + a__ Sin[x__] :> a^2
jb2[h]
while
jb1[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> a^2
jb1[h]
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.
a__
toa_
andb__
tob_
? $\endgroup$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, tryff[a__] := a^2
and evaluateff[a,b,c]
. If you haven't already seen it you will find the tutorial Patterns Overview extremely useful. $\endgroup$