NR in Haskell


olandere
01-24-2007, 11:42 PM
Any interest in porting NR to Haskell? I've been working on this as a way to brush up on Haskell. I'm working on routines from chapter 5 and 9. Here's the Chebyshev fit from p. 192:

-- This function computes the n coefficients of the approximating Chebyshev
-- polynomial given a function func to be approximated, lower and upper limits
-- of the interval [a, b] over which to approximate func and the maximum degree
-- n of the Chebyshev polynomial. This routine was converted from chebft,
-- p. 192, Numerical Recipes in C, 2nd ed.

chebyshevFit :: RealFloat a => (a -> a) -> a -> a -> Int -> [a]
chebyshevFit func a b n = c where
bma = 0.5*(b-a)
bpa = 0.5*(b+a)
f = [func (cos(pi*(fromIntegral k+0.5)/fromIntegral n)*bma+bpa) |
k <- [0 .. (n-1)]]
fac = 2.0/fromIntegral n
c = [fac * sum
[f!!k * cos(pi*fromIntegral j*(fromIntegral k + 0.5)/fromIntegral n)
| k <- [0 .. (n-1)]] | j <- [0 .. (n-1)]]