Debian Codegraph @

src/Diagrams/TwoD/Segment/Bernstein.hs

raw symbols SCIP
1 {-# LANGUAGE DeriveFunctor #-}
2 {-# LANGUAGE TypeFamilies #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module : Diagrams.TwoD.Segment.Bernstein
6 -- Copyright : (c) 2014-2015 diagrams-lib team (see LICENSE)
7 -- License : BSD-style (see LICENSE)
8 -- Maintainer : [email protected]
9 --
10 -- Bernstein polynomials, used internally by code to find
11 -- intersections of paths. This module is probably not of any
12 -- relevance to most users of diagrams.
13 -----------------------------------------------------------------------------
14 module Diagrams.TwoD.Segment.Bernstein
15 ( BernsteinPoly (..)
16 , listToBernstein
17 , evaluateBernstein
18
19 , degreeElevate
20 , bernsteinDeriv
21 , evaluateBernsteinDerivs
22 ) where
23
24 import Data.List (tails)
25 import Diagrams.Core.V
26 import Diagrams.Parametric
27 import Linear.V1
28
29 -- | Compute the binomial coefficients of degree n.
30 binomials :: Num n => Int -> [n]
31 binomials n = map fromIntegral $ scanl (\x m -> x * (n - m+1) `quot` m) 1 [1..n]
32
33 data BernsteinPoly n = BernsteinPoly
34 { bernsteinDegree :: Int
35 , bernsteinCoeffs :: [n]
36 } deriving (Show, Functor)
37
38 type instance V (BernsteinPoly n) = V1
39 type instance N (BernsteinPoly n) = n
40 type instance Codomain (BernsteinPoly n) = V1
41
42 -- | Create a bernstein polynomial from a list of coƫfficients.
43 listToBernstein :: Fractional n => [n] -> BernsteinPoly n
44 listToBernstein [] = 0
45 listToBernstein l = BernsteinPoly (length l - 1) l
46
47 -- | Degree elevate a bernstein polynomial a number of times.
48 degreeElevate :: Fractional n => BernsteinPoly n -> Int -> BernsteinPoly n
49 degreeElevate b 0 = b
50 degreeElevate (BernsteinPoly lp p) times =
51 degreeElevate (BernsteinPoly (lp+1) (head p:inner p 1)) (times-1)
52 where
53 n = fromIntegral lp
54
55 inner [] _ = [0]
56 inner [a] _ = [a]
57 inner (a:b:rest) i = (i*a/(n+1) + b*(1 - i/(n+1))) : inner (b:rest) (i+1)
58
59 -- | Evaluate the bernstein polynomial.
60 evaluateBernstein :: Fractional n => BernsteinPoly n -> n -> n
61 evaluateBernstein (BernsteinPoly _ []) _ = 0
62 evaluateBernstein (BernsteinPoly _ [b]) _ = b
63 evaluateBernstein (BernsteinPoly lp (b':bs)) t = go t n (b'*u) 2 bs
64 where
65 u = 1-t
66 n = fromIntegral lp
67
68 go tn bc tmp _ [b] = tmp + tn*bc*b
69 go tn bc tmp i (b:rest) =
70 go (tn*t) -- tn
71 (bc*(n - i+1)/i) -- bc
72 ((tmp + tn*bc*b)*u) -- tmp
73 (i+1) -- i
74 rest
75 go _ _ _ _ [] = error "evaluateBernstein: impossible"
76
77 -- | Evaluate the bernstein polynomial and its derivatives.
78 evaluateBernsteinDerivs :: Fractional n => BernsteinPoly n -> n -> [n]
79 evaluateBernsteinDerivs b t
80 | bernsteinDegree b == 0 = [evaluateBernstein b t]
81 | otherwise = evaluateBernstein b t : evaluateBernsteinDerivs (bernsteinDeriv b) t
82
83 -- | Find the derivative of a bernstein polynomial.
84 bernsteinDeriv :: Fractional n => BernsteinPoly n -> BernsteinPoly n
85 bernsteinDeriv (BernsteinPoly 0 _) = 0
86 bernsteinDeriv (BernsteinPoly lp p) =
87 -- BernsteinPoly (lp-1) $ map (* fromIntegral lp) $ zipWith (-) (tail p) p
88 BernsteinPoly (lp-1) $ zipWith (\a b -> (a - b) * fromIntegral lp) (tail p) p
89
90 instance Fractional n => Parametric (BernsteinPoly n) where
91 atParam b = V1 . evaluateBernstein b
92 instance Num n => DomainBounds (BernsteinPoly n)
93 instance Fractional n => EndValues (BernsteinPoly n)
94 instance Fractional n => Sectionable (BernsteinPoly n) where
95 splitAtParam = bernsteinSplit
96 reverseDomain (BernsteinPoly i xs) = BernsteinPoly i (reverse xs)
97
98 -- | Split a bernstein polynomial.
99 bernsteinSplit :: Num n => BernsteinPoly n -> n -> (BernsteinPoly n, BernsteinPoly n)
100 bernsteinSplit (BernsteinPoly lp p) t =
101 (BernsteinPoly lp $ map head controls,
102 BernsteinPoly lp $ reverse $ map last controls)
103 where
104 interp a b = (1-t)*a + t*b
105
106 terp [_] = []
107 terp l = let ctrs = zipWith interp l (tail l)
108 in ctrs : terp ctrs
109 controls = p : terp p
110
111 instance Fractional n => Num (BernsteinPoly n) where
112 ba@(BernsteinPoly la a) + bb@(BernsteinPoly lb b)
113 | la < lb = BernsteinPoly lb $ zipWith (+) (bernsteinCoeffs $ degreeElevate ba $ lb - la) b
114 | la > lb = BernsteinPoly la $ zipWith (+) a (bernsteinCoeffs $ degreeElevate bb $ la - lb)
115 | otherwise = BernsteinPoly la $ zipWith (+) a b
116
117 ba@(BernsteinPoly la a) - bb@(BernsteinPoly lb b)
118 | la < lb = BernsteinPoly lb $ zipWith (-) (bernsteinCoeffs $ degreeElevate ba (lb - la)) b
119 | la > lb = BernsteinPoly la $ zipWith (-) a (bernsteinCoeffs $ degreeElevate bb (la - lb))
120 | otherwise = BernsteinPoly la $ zipWith (-) a b
121
122 (BernsteinPoly la a) * (BernsteinPoly lb b) =
123 BernsteinPoly (la+lb) $
124 zipWith (flip (/)) (binomials (la + lb)) $
125 init $ map sum $
126 map (zipWith (*) a') (down b') ++
127 map (zipWith (*) (reverse b')) (tail $ tails a')
128 -- zipWith (zipWith (*)) (tail $ tails a') (repeat $ reverse b')
129 where down l = tail $ scanl (flip (:)) [] l -- [[1], [2, 1], [3, 2, 1], ...
130 a' = zipWith (*) a (binomials la)
131 b' = zipWith (*) b (binomials lb)
132
133 fromInteger a = BernsteinPoly 0 [fromInteger a]
134
135 signum (BernsteinPoly _ []) = 0
136 signum (BernsteinPoly _ (a:_)) = BernsteinPoly 0 [signum a]
137
138 abs = fmap abs