{-# LANGUAGE FlexibleContexts #-}
module Numeric.Polynomial.Chebyshev (
chebyshev
, chebyshevBroucke
) where
import qualified Data.Vector.Generic as G
data C = C {-# UNPACK #-} !Double {-# UNPACK #-} !Double
chebyshev :: (G.Vector v Double) =>
Double
-> v Double
-> Double
chebyshev :: forall (v :: * -> *).
Vector v Double =>
Double -> v Double -> Double
chebyshev Double
x v Double
a = C -> Double
fini (C -> Double) -> (v Double -> C) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> C -> C) -> C -> v Double -> C
forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
G.foldr' Double -> C -> C
step (Double -> Double -> C
C Double
0 Double
0) (v Double -> C) -> (v Double -> v Double) -> v Double -> C
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Double -> v Double
forall (v :: * -> *) a. Vector v a => v a -> v a
G.tail (v Double -> Double) -> v Double -> Double
forall a b. (a -> b) -> a -> b
$ v Double
a
where step :: Double -> C -> C
step Double
k (C Double
b0 Double
b1) = Double -> Double -> C
C (Double
k Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
b0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
b1) Double
b0
fini :: C -> Double
fini (C Double
b0 Double
b1) = v Double -> Double
forall (v :: * -> *) a. Vector v a => v a -> a
G.head v Double
a Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
b0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
b1
x2 :: Double
x2 = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
2
{-# INLINE chebyshev #-}
data B = B {-# UNPACK #-} !Double {-# UNPACK #-} !Double {-# UNPACK #-} !Double
chebyshevBroucke :: (G.Vector v Double) =>
Double
-> v Double
-> Double
chebyshevBroucke :: forall (v :: * -> *).
Vector v Double =>
Double -> v Double -> Double
chebyshevBroucke Double
x = B -> Double
fini (B -> Double) -> (v Double -> B) -> v Double -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> B -> B) -> B -> v Double -> B
forall (v :: * -> *) a b.
Vector v a =>
(a -> b -> b) -> b -> v a -> b
G.foldr' Double -> B -> B
step (Double -> Double -> Double -> B
B Double
0 Double
0 Double
0)
where step :: Double -> B -> B
step Double
k (B Double
b0 Double
b1 Double
_) = Double -> Double -> Double -> B
B (Double
k Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
x2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
b0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
b1) Double
b0 Double
b1
fini :: B -> Double
fini (B Double
b0 Double
_ Double
b2) = (Double
b0 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
b2) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
0.5
x2 :: Double
x2 = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
2
{-# INLINE chebyshevBroucke #-}