I have a long set of replacement rules that I utilize to solve some longish integrals over trigonometric functions. Just look at this snippet (from this answer):
validQ[coeff___, arg_, v_] :=
FreeQ[{coeff}, v] && Exponent[arg, v] == 1;
testInt[v_, a_, b_] := {
coeff___*Cos[arg_] /; validQ[coeff, arg, v] :> coeff/Coefficient[arg, v]*Simplify[(First@Differences[Sin[arg] /. {{v -> a}, {v -> b}}]), Trig -> False],
(*failure rule*)
integrand_ :> Inactive[Integrate][integrand, {v, a, b}]
};
See, that e.g. 2*Cos[t] /. testInt[t, 0, \[Pi]/2]
returns 2
as expected. However, I am struggling with terms that are like Cos[t]
:
Cos[t] /. testInt[t, 0, \[Pi]/2]
(* Inactive[Integrate][Cos[t], {t, 0, \[Pi]/2}] *)
Same for 1*Cos[t]
. However, applying the rules to 1.*Cos[t]
yields 2.
. In principle, I could just define an additional rule for Cos
with the negative side-effect that I would have to be very cautious since this would also replace the Cos
in terms where it may be wrong. How can I change the patterns so that ideally coeff___*Cos
also accepts 1
as a coefficient?