Skip to content

Instantly share code, notes, and snippets.

@madmann91
Created March 15, 2019 16:19
Show Gist options
  • Save madmann91/3c4c978620e9abb4be39fc2a531b56c8 to your computer and use it in GitHub Desktop.
Save madmann91/3c4c978620e9abb4be39fc2a531b56c8 to your computer and use it in GitHub Desktop.
Chebyshev approximation of arbitrary functions
import Math.Polynomial
import Math.Polynomial.Chebyshev
coeff f n j = (2 / nf) * (sum [f(xk) * yk | (xk, yk) <- zip x y])
where
x = map ((\x -> cos (pi * (x + 0.5) / nf)) . fromIntegral) [0..n-1]
y = map ((\x -> cos (jf * pi * (x + 0.5) / nf)) . fromIntegral) [0..n-1]
nf = fromIntegral n
jf = fromIntegral j
approx f n = foldl1 addPoly $ (constPoly (-0.5 * c0)) : [scalePoly ck (t k) | (ck, k) <- zip c [0..n-1]]
where
c = map ((coeff f n) . fromIntegral) [0..n-1]
c0 = head c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment