packages feed

numeric-tools 0.2.0.0 → 0.2.0.1

raw patch · 6 files changed

+118/−53 lines, 6 files

Files

Numeric/Classes/Indexing.hs view
@@ -31,6 +31,7 @@   (!)         :: a -> Int -> IndexVal a   x ! i | i < 0 || i > size x = error "Numeric.Classes.Indexing.!: index is out of range"         | otherwise           = unsafeIndex x i+  {-# INLINE (!) #-}  -- | Check that index is valid validIndex :: Indexable a => a -> Int -> Bool @@ -42,15 +43,24 @@   size        = V.length   unsafeIndex = V.unsafeIndex   (!)         = (V.!)+  {-# INLINE size        #-}+  {-# INLINE (!)         #-}+  {-# INLINE unsafeIndex #-}  instance U.Unbox a => Indexable (U.Vector a) where   type IndexVal (U.Vector a) = a   size        = U.length   unsafeIndex = U.unsafeIndex   (!)         = (U.!)+  {-# INLINE size        #-}+  {-# INLINE (!)         #-}+  {-# INLINE unsafeIndex #-}  instance S.Storable a => Indexable (S.Vector a) where   type IndexVal (S.Vector a) = a   size        = S.length   unsafeIndex = S.unsafeIndex   (!)         = (S.!)+  {-# INLINE size        #-}+  {-# INLINE (!)         #-}+  {-# INLINE unsafeIndex #-}
Numeric/Tools/Differentiation.hs view
@@ -28,8 +28,8 @@ import Control.Monad.ST   (runST) import Data.Data          (Data,Typeable) import qualified Data.Vector.Unboxed.Mutable as M-import Foreign-import Foreign.C+import Foreign.C          (CDouble(..))+import System.IO.Unsafe   (unsafePerformIO)  import Numeric.IEEE (infinity, nan) 
Numeric/Tools/Interpolation.hs view
@@ -1,6 +1,8 @@-{-# LANGUAGE FlexibleContexts   #-}-{-# LANGUAGE DeriveDataTypeable #-}-{-# LANGUAGE TypeFamilies       #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleContexts      #-}+{-# LANGUAGE FlexibleInstances     #-}+{-# LANGUAGE DeriveDataTypeable    #-}+{-# LANGUAGE TypeFamilies          #-} -- | -- Module    : Numeric.Tools.Interpolation -- Copyright : (c) 2011 Aleksey Khudyakov@@ -21,14 +23,18 @@     -- * Type class     Interpolation(..)   , tabulate+  , tabulateFun     -- * Linear interpolation   , LinearInterp   , linearInterp     -- * Cubic splines   , CubicSpline   , cubicSpline-    --+    -- * Reexport of mesh type   , module Numeric.Tools.Mesh+    -- * Default methods+  , defaultInterpSize+  , defaultInterpIndex   ) where  import Control.Monad.ST   (runST)@@ -46,58 +52,65 @@  ---------------------------------------------------------------- --- | Interpolation for arbitraty meshes-class Interpolation a where+-- | Type class for Interpolation algorithms. Since some algorithms+--   require some particular mesh type it's present as type class+--   parameter. Every algorithms should be instance of 'Indexable' as+--   well. Indexing should return pair @(x,y)@ for u'th mesh node.+class ( IndexVal (interp mesh) ~ (Double,Double), Indexable (interp mesh)+      , IndexVal mesh ~ Double, Mesh mesh+      ) => Interpolation interp mesh where   -- | Interpolate function at some point. Function should not   --   fail outside of mesh however it may and most likely will give   --   nonsensical results-  at          :: (IndexVal m ~ Double, Mesh m) => a m -> Double -> Double-  -- | Tabulate function-  tabulateFun :: (IndexVal m ~ Double, Mesh m) => m -> (Double -> Double) -> a m+  at          :: interp mesh -> Double -> Double   -- | Use table of already evaluated function and mesh. Sizes of mesh   --   and table must coincide but it's not checked. Do not use this   --   function use 'tabulate' instead.-  unsafeTabulate :: (IndexVal m ~ Double, Mesh m, G.Vector v Double) => m -> v Double -> a m+  unsafeTabulate :: (G.Vector v Double) => mesh -> v Double -> interp mesh   -- | Get mesh.-  interpolationMesh  :: a m -> m+  interpolationMesh  :: interp mesh -> mesh   -- | Get table of function values -  interpolationTable :: a m -> U.Vector Double+  interpolationTable :: interp mesh -> U.Vector Double     +-- | Tabulate function.+tabulateFun :: (Interpolation i m) => m -> (Double -> Double) -> i m+tabulateFun mesh f = unsafeTabulate mesh $ U.generate (size mesh) (f . unsafeIndex mesh)+{-# INLINE tabulateFun #-}  -- | Use table of already evaluated function and mesh. Sizes of mesh --   and table must coincide. -tabulate :: (Interpolation a, IndexVal m ~ Double, Mesh m, G.Vector v Double) => m -> v Double -> a m+tabulate :: (Interpolation i m, G.Vector v Double) => m -> v Double -> i m+{-# INLINE tabulate #-} tabulate mesh tbl   | size mesh /= G.length tbl = error "Numeric.Tools.Interpolation.tabulate: size of vector and mesh do not match"   | otherwise                 = unsafeTabulate mesh tbl-{-# INLINE tabulate #-} ++ ---------------------------------------------------------------- -- Linear interpolation ----------------------------------------------------------------  -- | Data for linear interpolation-data LinearInterp a = LinearInterp { linearInterpMesh  :: a-                                   , linearInterpTable :: U.Vector Double-                                   }-                      deriving (Show,Eq,Data,Typeable)+data LinearInterp mesh = LinearInterp+  { linearInterpMesh  :: mesh+  , linearInterpTable :: U.Vector Double+  }+  deriving (Show,Eq,Data,Typeable)  -- | Function used to fix types-linearInterp :: LinearInterp a -> LinearInterp a+linearInterp :: LinearInterp mesh -> LinearInterp mesh linearInterp = id -instance Mesh a => Indexable (LinearInterp a) where-  type IndexVal (LinearInterp a) = (IndexVal a, Double)-  size        (LinearInterp _    vec)   = size vec-  unsafeIndex (LinearInterp mesh vec) i = ( unsafeIndex mesh i-                                          , unsafeIndex vec  i-                                          )+instance (Mesh mesh, IndexVal mesh ~ Double) => Indexable (LinearInterp mesh) where+  type IndexVal (LinearInterp mesh) = (IndexVal mesh, Double)+  size        = defaultInterpSize+  unsafeIndex = defaultInterpIndex   {-# INLINE size        #-}   {-# INLINE unsafeIndex #-} -instance Interpolation LinearInterp where+instance (Mesh mesh, IndexVal mesh ~ Double) => Interpolation LinearInterp mesh where   at                      = linearInterpolation-  tabulateFun    mesh f   = LinearInterp mesh (U.generate (size mesh) (f . unsafeIndex mesh))   unsafeTabulate mesh tbl = LinearInterp mesh (G.convert tbl)   interpolationMesh       = linearInterpMesh   interpolationTable      = linearInterpTable@@ -118,7 +131,7 @@ -- | Natural cubic splines data CubicSpline a = CubicSpline { cubicSplineMesh   :: a                                  , cubicSplineTable  :: U.Vector Double-                                 , cubicSplineY2     :: U.Vector Double+                                 , _cubicSplineY2    :: U.Vector Double                                  }                    deriving (Eq,Show,Data,Typeable) @@ -126,7 +139,14 @@ cubicSpline :: CubicSpline a -> CubicSpline a  cubicSpline = id -instance Interpolation CubicSpline where+instance (Mesh mesh, IndexVal mesh ~ Double) => Indexable (CubicSpline mesh) where+  type IndexVal (CubicSpline mesh) = (IndexVal mesh, Double)+  size        = defaultInterpSize+  unsafeIndex = defaultInterpIndex+  {-# INLINE size        #-}+  {-# INLINE unsafeIndex #-}++instance (Mesh mesh, IndexVal mesh ~ Double) => Interpolation CubicSpline mesh where   at (CubicSpline mesh ys y2) x = y     where     i  = safeFindIndex mesh x@@ -144,7 +164,6 @@     y  = a * ya + b * yb         + ((a*a*a - a) * da + (b*b*b - b) * db) * (h * h) / 6   -------  tabulateFun    mesh f   = makeCubicSpline mesh (U.generate (size mesh) (f . unsafeIndex mesh))   unsafeTabulate mesh tbl = makeCubicSpline mesh (G.convert tbl)   interpolationMesh       = cubicSplineMesh   interpolationTable      = cubicSplineTable@@ -195,3 +214,15 @@     where       n = size mesh - 2 {-# INLINE safeFindIndex #-}++-- | Default implementation of 'size' for interpolation algorithms.+defaultInterpSize :: Interpolation i m => i m -> Int+defaultInterpSize = U.length . interpolationTable+{-# INLINE defaultInterpSize #-}++-- | Default implementation of 'unsafeIndex' for interpolation algorithms.+defaultInterpIndex :: Interpolation i m => i m -> Int -> (Double, Double)+defaultInterpIndex tbl i = ( unsafeIndex (interpolationMesh  tbl) i+                           , unsafeIndex (interpolationTable tbl) i+                           )+{-# INLINE defaultInterpIndex #-}
Numeric/Tools/Mesh.hs view
@@ -16,8 +16,10 @@     Mesh(..)     -- ** Uniform mesh   , UniformMesh-  , uniformMesh   , uniformMeshStep+    -- *** Constructors+  , uniformMesh+  , uniformMeshN   ) where  import Data.Data          (Data,Typeable)@@ -32,6 +34,9 @@ -- | 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@@ -68,6 +73,16 @@   | 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++-- | Create uniform mesh+uniformMeshN :: Double  -- ^ Lower bound+             -> Double  -- ^ Mesh step+             -> Int     -- ^ Number of points+             -> UniformMesh+uniformMeshN a dx n+  | n  <  2   = error "Numeric.Tool.Interpolation.Mesh.uniformMeshStep: too few points"+  | dx <= 0   = error "Numeric.Tool.Interpolation.Mesh.uniformMeshStep: nonpositive step"+  | otherwise = UniformMesh a dx n   instance Indexable UniformMesh where
+ README.markdown view
@@ -0,0 +1,4 @@+# Numeric tools [![Build Status](https://travis-ci.org/Shimuuar/numeric-tools.png?branch=master)](https://travis-ci.org/Shimuuar/numeric-tools)++This package provides set of functions for numerical integration,+differentiation and function interpolation.
numeric-tools.cabal view
@@ -1,5 +1,5 @@ Name:           numeric-tools-Version:        0.2.0.0+Version:        0.2.0.1 Cabal-Version:  >= 1.8 License:        BSD3 License-File:   LICENSE@@ -28,35 +28,40 @@   .   * Improve convergence test when integral converges to     zero. Convergence is still poor--+extra-source-files:+  README.markdown  source-repository head   type:     hg   location: https://bitbucket.org/Shimuuar/numeric-tools+source-repository head+  type:     git+  location: https://github.com/Shimuuar/numeric-tools +Library+  Build-Depends:+    base       >=3 && <5,+    ieee754    >= 0.7.3,+    primitive,+    vector     >= 0.7.0.1+  Exposed-modules:+    Control.Monad.Numeric+    Numeric.ApproxEq+    Numeric.Classes.Indexing+    Numeric.Tools.Equation+    Numeric.Tools.Differentiation+    Numeric.Tools.Integration+    Numeric.Tools.Interpolation+    Numeric.Tools.Mesh+  c-sources:       cbits/ieee.c+  ghc-options:	   -Wall -O2+ test-suite tests   type:           exitcode-stdio-1.0   hs-source-dirs: test   main-is:        test.hs-  ghc-options:    -Wall -threaded -rtsopts+  ghc-options:    -O2 -Wall -threaded -rtsopts   build-depends:     base,     numeric-tools,     HUnit--Library-  Build-Depends:   base >=3 && <5,-                   ieee754 >= 0.7.3,-                   primitive,-                   vector >= 0.7.0.1-  Exposed-modules: Control.Monad.Numeric-                   Numeric.ApproxEq-                   Numeric.Classes.Indexing-                   Numeric.Tools.Equation-                   Numeric.Tools.Differentiation-                   Numeric.Tools.Integration-                   Numeric.Tools.Interpolation-                   Numeric.Tools.Mesh-  c-sources:       cbits/ieee.c-  ghc-options:	   -Wall -O2