Debian Codegraph @

src/Diagrams/CubicSpline/Internal.hs

raw symbols SCIP
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Diagrams.CubicSpline
4 -- Copyright : (c) 2011 diagrams-lib team (see LICENSE)
5 -- License : BSD-style (see LICENSE)
6 -- Maintainer : [email protected]
7 --
8 -- A /cubic spline/ is a smooth, connected sequence of cubic curves
9 -- passing through a given sequence of points. This module implements
10 -- a straightforward spline generation algorithm based on solving
11 -- tridiagonal systems of linear equations.
12 --
13 -----------------------------------------------------------------------------
14 module Diagrams.CubicSpline.Internal
15 (
16 -- * Solving for spline coefficents
17 solveCubicSplineDerivatives
18 , solveCubicSplineDerivativesClosed
19 , solveCubicSplineCoefficients
20 ) where
21
22 import Diagrams.Solve.Tridiagonal
23
24 import Data.List
25
26 -- | Use the tri-diagonal solver with the appropriate parameters for an open cubic spline.
27 solveCubicSplineDerivatives :: Fractional a => [a] -> [a]
28 solveCubicSplineDerivatives (x:xs) = solveTriDiagonal as bs as ds
29 where
30 as = replicate (l - 1) 1
31 bs = 2 : replicate (l - 2) 4 ++ [2]
32 l = length ds
33 ds = zipWith f (xs ++ [last xs]) (x:x:xs)
34 f a b = 3*(a - b)
35
36 solveCubicSplineDerivatives _ = error "argument to solveCubicSplineDerivatives must be nonempty"
37
38 -- | Use the cyclic-tri-diagonal solver with the appropriate parameters for a closed cubic spline.
39 solveCubicSplineDerivativesClosed :: Fractional a => [a] -> [a]
40 solveCubicSplineDerivativesClosed xs = solveCyclicTriDiagonal as bs as ds 1 1
41 where
42 as = replicate (l - 1) 1
43 bs = replicate l 4
44 l = length xs
45 xs' = cycle xs
46 ds = take l $ zipWith f (drop 1 xs') (drop (l - 1) xs')
47 f a b = 3*(a - b)
48
49 -- | Use the cyclic-tri-diagonal solver with the appropriate parameters for a closed cubic spline.
50 solveCubicSplineCoefficients :: Fractional a => Bool -> [a] -> [[a]]
51 solveCubicSplineCoefficients closed xs =
52 [ [x,d,3*(x1-x)-2*d-d1,2*(x-x1)+d+d1]
53 | (x,x1,d,d1) <- zip4 xs' (tail xs') ds' (tail ds')
54 ]
55 where
56 ds | closed = solveCubicSplineDerivativesClosed xs
57 | otherwise = solveCubicSplineDerivatives xs
58 close as | closed = as ++ [head as]
59 | otherwise = as
60 xs' = close xs
61 ds' = close ds