For periodic not-necessarily smooth $f$ and a range of $m$, say $0\ldots 31$, I want to compute $\int_{-\pi}^\pi f(t) \cos (mt) dt$ (and maybe the same integral with $\sin$ instead of $\cos$) to machine precision (or close-ish to it).

The obvious thing to do is to evaluate $f$ at an equispaced grid and take a DFT, but this is only giving me linear convergence, and getting enough precision will take a very fine grid.

The other obvious thing (which I haven't yet tried) would be to pre-compute $w_i \cos(m t_i)$ for $w_i$ $t_i$ the Legendre weights and points (scaled to $(-\pi,\pi)$), then at evaluation time compute $f(t_i)$ and find all the integrals with a matvec. (All else being equal, I'd prefer not to have to have a bunch of pre-computed data to keep track of, but it's better than re-computing it every time if that's the only other option)

My only other idea (also not yet tried) was to figure out a closed-form solution for $\int_{-\pi}^\pi T_n(t/\pi)\cos(mt)$ for $T_n$ the Chebyshev basis, then at evaluation time do a fast Chebyshev transform to get $f \approx \sum_i c_i T_i $ and use that to compute the integrals.

None of these seem particularly satisfying.

So my question is: is there some neat technique I'm missing that will let me compute banks of these coefficients with spectral (or at least quadratic) accuracy and with good complexity?

whydo you want these integrals evaluated? What will you do with their values? $\endgroup$ – Somos Mar 28 at 18:58