-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lagrange.hs
28 lines (19 loc) · 842 Bytes
/
Lagrange.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Lagrange (interpolate) where
import Data.List
-- |Compute interpolated value for point `x` from data points `xs` mapping to values `ys`.
interpolate :: Fractional a => a -> [a] -> [a] -> a
interpolate x xs ys = lagrange x dataTriples
where
dataTriples = zip3 ys xs $ sublists xs
-- |Lagrange interpolation polynomial.
lagrange :: Fractional a => a -> [(a, a, [a])] -> a
lagrange x = sum . map yL
where
yL (yj, xj, xs) = yj * basePolynomial x xj xs
-- |Lagrange base polynomial `L(x)`
basePolynomial :: Fractional a => a -> a -> [a] -> a
basePolynomial x xj xis = product $ map (\xi -> (x - xi) / (xj - xi)) xis
-- |Generate all sublists of `xs` where the nth list of the result contains all but the nth element of `xs`.
sublists :: [a] -> [[a]]
sublists [_] = [[]]
sublists (x:xs) = xs : map (x:) (sublists xs)