For the reasons mentioned above, I wrote the following "shell" for these functions:
(* Weak test for singularity *)
findSingular[coe_, k_, assump___] :=
Piecewise[{{{}, # === k}}, #] &[
k /. Union@Solve[{assump, Denominator@Together@coe == 0, k ∈ Integers}, k]]
generalTerm[expr_, {t_, a_, b_}, n_: C][coefunc_, termfunc_, assump___] :=
With[{generalCoe = coefunc[expr, {t, a, b}, \[FormalK]]},
With[{singularity = findSingular[generalCoe, \[FormalK], assump]},
Piecewise[{{generalCoe, And @@ (\[FormalK] != # & /@ singularity)},
Sequence @@ ({coefunc[expr, {t, a, b}, #], \[FormalK] == #} & /@
singularity)}] termfunc[{t, a, b}, \[FormalK]]]]
summation[lowerbound_, upperbound_] :=
HoldForm@Sum[#, {\[FormalK], lowerbound, upperbound}] &
trans[old_, new_, var_] := First@RescalingTransform[{new}, {old}][{var}]
easyFourierSinCoefficient[expr_, {t_, a_, b_}, k_] :=
FourierSinCoefficient[expr /. t -> trans[{a, b}, {0, Pi}, t], t, k] /.
t -> trans[{0, Pi}, {a, b}, t]
Block[{t, a, b, k},
easySinTerm[{t_, a_, b_}, k_] = Sin[k t] /. t -> trans[{0, Pi}, {a, b}, t] // Simplify];
easyFourierSinSeries[expr_, {t_, a_, b_}, n_: C] :=
summation[1, n]@
generalTerm[expr, {t, a, b}][easyFourierSinCoefficient, easySinTerm, \[FormalK] > 0]
easyFourierCoefficient[expr_, {t_, a_, b_}, k_] :=
FourierCoefficient[expr /. t -> trans[{a, b}, {-Pi, Pi}, t], t, k] /.
t -> trans[{-Pi, Pi}, {a, b}, t]
Block[{t, a, b, k},
easyTerm[{t_, a_, b_}, k_] =
Exp[I k t] /. t -> trans[{-Pi, Pi}, {a, b}, t] // Simplify];
easyFourierSeries[expr_, {t_, a_, b_}, n_: C] :=
summation[-n, n]@
PiecewiseExpand@
Piecewise[{{generalTerm[expr, {t, a, b}][easyFourierCoefficient,
easyTerm], \[FormalK] != 0}},
easyFourierCoefficient[expr, {t, a, b}, 0] easyTerm[{t, a, b}, 0]]
easyFourierCosCoefficient[expr_, {t_, a_, b_}, k_] :=
FourierCosCoefficient[expr /. t -> trans[{a, b}, {0, Pi}, t], t, k] /.
t -> trans[{0, Pi}, {a, b}, t]
Block[{t, a, b, k},
easyCosTerm[{t_, a_, b_}, k_] = Cos[k t] /. t -> trans[{0, Pi}, {a, b}, t] // Simplify];
easyFourierCosSeries[expr_, {t_, a_, b_}, n_: C] :=
easyFourierCosCoefficient[expr, {t, a, b}, 0]/2 +
summation[1, n]@
generalTerm[expr, {t, a, b}][easyFourierCosCoefficient, easyCosTerm, \[FormalK] > 0]
Block[{t, a, b, k},
easyTrigTerm[{t_, a_, b_},
k_] = {Cos[k t], Sin[k t]} /. t -> trans[{-Pi, Pi}, {a, b}, t] // Simplify];
easyFourierTrigSeries[expr_, {t_, a_, b_}, n_: C] :=
easyFourierCoefficient[expr, {t, a, b}, 0] +
summation[1, n]@
Collect[Simplify[
generalTerm[expr, {t, a, b}][easyFourierCoefficient,
ExpToTrig@easyTerm@## &, \[FormalK] !=
0] /. {{\[FormalK] -> \[FormalK]}, {\[FormalK] -> -\[FormalK]}} //
Total, \[FormalK] > 0 && \[FormalK] ∈ Integers], (Cos | Sin)[_]]
I'm still in v9 so choose HoldForm
rather than Inactivate
to hold Sum
.
The usage of these functions are simple and straightforward. For example, suppose I want to expand
$$
f(x)=\left\{
\begin{array}{ll}
x & -\frac{1}{2}\leq x<0 \\
1 & 0\leq x<\frac{1}{2} \\
-1 & \frac{1}{2}\leq x<\frac{3}{2} \\
\end{array}\right.$$
in $-\frac{1}{2}<x<\frac{3}{2}$ with Fourier sine series, then I just need to write
expr = Piecewise[{{x, -1/2 <= x < 0}, {1, 0 <= x < 1/2}, {-1, 1/2 <= x < 3/2}}]
series = easyFourierSinSeries[expr, {x, -1/2, 3/2}]
If you want to plot or do numeric calculation with the result, just replace C
with a specific integer and ReleaseHold
:
Plot[{expr, series /. C -> 24 // ReleaseHold} // Evaluate, {x, -1/2, 3/2},
PlotRange -> All]