# numeric-tools / Numeric / Tools / Mesh.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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84``` ```{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE TypeFamilies #-} -- | -- Module : Numeric.Tools.Mesh -- Copyright : (c) 2011 Aleksey Khudyakov -- License : BSD3 -- -- Maintainer : Aleksey Khudyakov -- Stability : experimental -- Portability : portable -- -- 1-dimensional meshes. Used by 'Numeric.Tools.Interpolation'. -- module Numeric.Tools.Mesh ( -- * Meshes Mesh(..) -- ** Uniform mesh , UniformMesh , uniformMesh , uniformMeshStep ) where import Data.Data (Data,Typeable) import Numeric.Classes.Indexing ---------------------------------------------------------------- -- Type class ---------------------------------------------------------------- -- | Class for 1-dimensional meshes. Mesh is ordered set of -- points. Each instance must guarantee that every next point is -- greater that previous and there is at least 2 points in mesh. -- -- Every mesh is instance of 'Indexable' and indexing should get n'th -- mesh node. class Indexable a => Mesh a where -- | Low bound of mesh meshLowerBound :: a -> Double -- | Upper bound of mesh meshUpperBound :: a -> Double -- | Find such index for value that -- -- > mesh ! i <= x && mesh ! i+1 > x -- -- Will return invalid index if value is out of range. meshFindIndex :: a -> Double -> Int ---------------------------------------------------------------- -- Uniform mesh ---------------------------------------------------------------- -- | Uniform mesh data UniformMesh = UniformMesh { uniformMeshFrom :: Double , uniformMeshStep :: Double -- ^ Distance between points , uniformMeshSize :: Int } deriving (Eq,Show,Data,Typeable) -- | Create uniform mesh uniformMesh :: (Double,Double) -- ^ Lower and upper bound -> Int -- ^ Number of points -> UniformMesh uniformMesh (a,b) n | b <= a = error "Numeric.Tool.Interpolation.Mesh.uniformMesh: bad range" | n < 2 = error "Numeric.Tool.Interpolation.Mesh.uniformMesh: too few points" | otherwise = UniformMesh a ((b - a) / fromIntegral (n - 1)) n instance Indexable UniformMesh where type IndexVal UniformMesh = Double size = uniformMeshSize unsafeIndex (UniformMesh a da _) i = a + fromIntegral i * da instance Mesh UniformMesh where meshLowerBound = uniformMeshFrom meshUpperBound (UniformMesh a da n) = a + da * fromIntegral (n - 1) meshFindIndex (UniformMesh a da _) x = truncate \$ (x - a) / da ```