ClearAll[finiteFourierSinTransform, finiteFourierCosTransform, finiteFourierTransform,
transformToIntegrate]
(#[(h : List | Plus | Equal)[a__], x_, n_] := Function[f, #[f, x, n]] /@ h[a];
#[a_ b_, {x_, xmin_, xmax_}, n_] /; FreeQ[b, x] :=
b #[a, {x, xmin, xmax}, n]) & /@ {finiteFourierSinTransform,
finiteFourierCosTransform, finiteFourierTransform};
argumentPattern = (#[
Derivative[i___, j_, k___][head_][var1___, x_, var2___], {x_, xmin_, xmax_}, n_] /;
Length@{i} === Length@{var1} && j > 0) &;
With[{f = Derivative[i, j - 1, k][head]},
Evaluate@argumentPattern@
finiteFourierSinTransform := -((n Pi)/(xmax - xmin)) finiteFourierCosTransform[
f[var1, x, var2], {x, xmin, xmax}, n];
Evaluate@argumentPattern@
finiteFourierCosTransform := ((n Pi)/(xmax - xmin)) finiteFourierSinTransform[
f[var1, x, var2], {x, xmin, xmax}, n] + (-1)^n f[var1, xmax, var2] -
f[var1, xmin, var2];
Evaluate@argumentPattern@
finiteFourierTransform := ((2 I n Pi)/(xmax - xmin)) finiteFourierTransform[
f[var1, x, var2], {x, xmin, xmax},
n] + (-1)^-n (f[var1, xmax, var2] - f[var1, xmin, var2]);
(#[f_ /; AtomQ@f || Quiet@Context@Evaluate@Head[f] === "System`", {x_, xmin_, xmax_},
n_] :=
With[{assump = {n ∈ Integers, xmax > xmin, #3},
integral =
Function[index,
Simplify@Integrate[f #2[(index Pi (x - xmin))/(xmax - xmin)], {x, xmin, xmax}]]},
Module[{general =
Assuming[assump, integral@n]},
With[{singularity =
If[IntegerQ@n, {},
Union@Join[If[#2 === Cos, {0}, {}],
Piecewise[{{{}, # === n}}, #] &@(n /.
Solve[Flatten@{assump, Denominator@Together@general == 0}, n])]]},
Piecewise[{integral@#, n == #} & /@ singularity, general]]
]
]) & @@@ {{finiteFourierSinTransform, Sin, n > 0}, {finiteFourierCosTransform, Cos,
n >= 0}};
finiteFourierTransform[
f_ /; AtomQ@f || Quiet@Context@Evaluate@Head[f] === "System`", {x_, xmin_, xmax_},
n_] :=
With[{assump = {n ∈ Integers, xmax > xmin},
integral = Function[index,
Simplify@Integrate[
f E^(-((2 I index π (x - xmin - (xmax - xmin)/2))/(xmax - xmin))), {x, xmin,
xmax}]]},
Module[{general =
Assuming[assump, integral@n]},
With[{singularity =
If[IntegerQ@n, {},
Piecewise[{{{}, # === n}}, #] &@(n /.
Solve[Flatten@{assump, Denominator@Together@general == 0}, n])]},
Piecewise[{integral@#, n == #} & /@ singularity, general]]
]
]
]
inverseFiniteFourierSinTransform[f_, n_, {x_, xmin_, xmax_}] :=
2/(xmax - xmin) HoldForm@Sum[#, {n, C}] &[f Sin[(n Pi (x - xmin))/(xmax - xmin)]]
inverseFiniteFourierCosTransform[f_, n_, {x_, xmin_, xmax_}] :=
1/(xmax - xmin) (f /. n -> 0) + 2/(xmax - xmin) HoldForm@Sum[#, {n, C}] &@
Simplify[f Cos[(n Pi (x - xmin))/(xmax - xmin)], n > 0]
inverseFiniteFourierTransform[f_, n_, {x_, xmin_, xmax_}], Re] :=
1/(xmax - xmin) ((f E^((2 I n π (x - xmin - (xmax - xmin)/2))/(xmax - xmin)) /.
n -> 0) + 2 HoldForm@Sum[#, {n, 1, C}] &@
Simplify[f E^((2 I n π (x - xmin - (xmax - xmin)/2))/(xmax - xmin)) // Re,
n ∈ Integers])
inverseFiniteFourierTransform[f_, n_, {x_, xmin_, xmax_}] :=
1/(xmax - xmin) (HoldForm@Sum[#, {n, -C, C}] &@
Simplify[f E^((2 I n π (x - xmin - (xmax - xmin)/2))/(xmax - xmin)),
n ∈ Integers])
transformToIntegrate[expr_] :=
expr /. (HoldPattern@#[f_, {x_, xmin_, xmax_}, n_] :>
RuleCondition@(HoldForm@Integrate[#, {\[FormalX], xmin, xmax}] &)[
f #2[(n Pi (x - xmin))/(xmax - xmin)] /.
x -> \[FormalX]] & @@@ {{finiteFourierSinTransform,
Sin}, {finiteFourierCosTransform, Cos}}) /.
HoldPattern@finiteFourierTransform[f_, {x_, xmin_, xmax_}, n_] :>
RuleCondition@(HoldForm@Integrate[#, {\[FormalX], xmin, xmax}] &)@
Simplify[f E^(-((2 I n π (x - xmin - (xmax - xmin)/2))/(xmax - xmin))) /.
x -> \[FormalX], n ∈ Integers];