summaryrefslogtreecommitdiff
path: root/packages/hmatrix/src/Numeric/GSL/Differentiation.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/src/Numeric/GSL/Differentiation.hs')
-rw-r--r--packages/hmatrix/src/Numeric/GSL/Differentiation.hs85
1 files changed, 0 insertions, 85 deletions
diff --git a/packages/hmatrix/src/Numeric/GSL/Differentiation.hs b/packages/hmatrix/src/Numeric/GSL/Differentiation.hs
deleted file mode 100644
index 0fb58ef..0000000
--- a/packages/hmatrix/src/Numeric/GSL/Differentiation.hs
+++ /dev/null
@@ -1,85 +0,0 @@
1{- |
2Module : Numeric.GSL.Differentiation
3Copyright : (c) Alberto Ruiz 2006
4License : GPL
5
6Maintainer : Alberto Ruiz
7Stability : provisional
8
9Numerical differentiation.
10
11<http://www.gnu.org/software/gsl/manual/html_node/Numerical-Differentiation.html#Numerical-Differentiation>
12
13From the GSL manual: \"The functions described in this chapter compute numerical derivatives by finite differencing. An adaptive algorithm is used to find the best choice of finite difference and to estimate the error in the derivative.\"
14-}
15
16
17module Numeric.GSL.Differentiation (
18 derivCentral,
19 derivForward,
20 derivBackward
21) where
22
23import Foreign.C.Types
24import Foreign.Marshal.Alloc(malloc, free)
25import Foreign.Ptr(Ptr, FunPtr, freeHaskellFunPtr)
26import Foreign.Storable(peek)
27import Numeric.GSL.Internal
28import System.IO.Unsafe(unsafePerformIO)
29
30derivGen ::
31 CInt -- ^ type: 0 central, 1 forward, 2 backward
32 -> Double -- ^ initial step size
33 -> (Double -> Double) -- ^ function
34 -> Double -- ^ point where the derivative is taken
35 -> (Double, Double) -- ^ result and error
36derivGen c h f x = unsafePerformIO $ do
37 r <- malloc
38 e <- malloc
39 fp <- mkfun (\y _ -> f y)
40 c_deriv c fp x h r e // check "deriv"
41 vr <- peek r
42 ve <- peek e
43 let result = (vr,ve)
44 free r
45 free e
46 freeHaskellFunPtr fp
47 return result
48
49foreign import ccall safe "gsl-aux.h deriv"
50 c_deriv :: CInt -> FunPtr (Double -> Ptr () -> Double) -> Double -> Double
51 -> Ptr Double -> Ptr Double -> IO CInt
52
53
54{- | Adaptive central difference algorithm, /gsl_deriv_central/. For example:
55
56>>> let deriv = derivCentral 0.01
57>>> deriv sin (pi/4)
58(0.7071067812000676,1.0600063101654055e-10)
59>>> cos (pi/4)
600.7071067811865476
61
62-}
63derivCentral :: Double -- ^ initial step size
64 -> (Double -> Double) -- ^ function
65 -> Double -- ^ point where the derivative is taken
66 -> (Double, Double) -- ^ result and absolute error
67derivCentral = derivGen 0
68
69-- | Adaptive forward difference algorithm, /gsl_deriv_forward/. The function is evaluated only at points greater than x, and never at x itself. The derivative is returned in result and an estimate of its absolute error is returned in abserr. This function should be used if f(x) has a discontinuity at x, or is undefined for values less than x. A backward derivative can be obtained using a negative step.
70derivForward :: Double -- ^ initial step size
71 -> (Double -> Double) -- ^ function
72 -> Double -- ^ point where the derivative is taken
73 -> (Double, Double) -- ^ result and absolute error
74derivForward = derivGen 1
75
76-- | Adaptive backward difference algorithm, /gsl_deriv_backward/.
77derivBackward ::Double -- ^ initial step size
78 -> (Double -> Double) -- ^ function
79 -> Double -- ^ point where the derivative is taken
80 -> (Double, Double) -- ^ result and absolute error
81derivBackward = derivGen 2
82
83{- | conversion of Haskell functions into function pointers that can be used in the C side
84-}
85foreign import ccall safe "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double))