diff options
Diffstat (limited to 'lib/GSL/Differentiation.hs')
-rw-r--r-- | lib/GSL/Differentiation.hs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/GSL/Differentiation.hs b/lib/GSL/Differentiation.hs new file mode 100644 index 0000000..6fd7565 --- /dev/null +++ b/lib/GSL/Differentiation.hs | |||
@@ -0,0 +1,79 @@ | |||
1 | {-# OPTIONS #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : GSL.Differentiation | ||
5 | Copyright : (c) Alberto Ruiz 2006 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : uses ffi | ||
11 | |||
12 | Numerical differentiation. | ||
13 | |||
14 | <http://www.gnu.org/software/gsl/manual/html_node/Numerical-Differentiation.html#Numerical-Differentiation> | ||
15 | |||
16 | From 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.\" | ||
17 | -} | ||
18 | ----------------------------------------------------------------------------- | ||
19 | module GSL.Differentiation ( | ||
20 | derivCentral, | ||
21 | derivForward, | ||
22 | derivBackward | ||
23 | ) where | ||
24 | |||
25 | import Foreign | ||
26 | import Data.Packed.Internal.Common(mkfun,check,(//)) | ||
27 | |||
28 | derivGen :: | ||
29 | Int -- ^ type: 0 central, 1 forward, 2 backward | ||
30 | -> Double -- ^ initial step size | ||
31 | -> (Double -> Double) -- ^ function | ||
32 | -> Double -- ^ point where the derivative is taken | ||
33 | -> (Double, Double) -- ^ result and error | ||
34 | derivGen c h f x = unsafePerformIO $ do | ||
35 | r <- malloc | ||
36 | e <- malloc | ||
37 | fp <- mkfun (\x _ -> f x) | ||
38 | c_deriv c fp x h r e // check "deriv" [] | ||
39 | vr <- peek r | ||
40 | ve <- peek e | ||
41 | let result = (vr,ve) | ||
42 | free r | ||
43 | free e | ||
44 | freeHaskellFunPtr fp | ||
45 | return result | ||
46 | |||
47 | foreign import ccall "gsl-aux.h deriv" | ||
48 | c_deriv :: Int -> FunPtr (Double -> Ptr () -> Double) -> Double -> Double | ||
49 | -> Ptr Double -> Ptr Double -> IO Int | ||
50 | |||
51 | |||
52 | {- | Adaptive central difference algorithm, /gsl_deriv_central/. For example: | ||
53 | |||
54 | > > let deriv = derivCentral 0.01 | ||
55 | > > deriv sin (pi/4) | ||
56 | >(0.7071067812000676,1.0600063101654055e-10) | ||
57 | > > cos (pi/4) | ||
58 | >0.7071067811865476 | ||
59 | |||
60 | -} | ||
61 | derivCentral :: Double -- ^ initial step size | ||
62 | -> (Double -> Double) -- ^ function | ||
63 | -> Double -- ^ point where the derivative is taken | ||
64 | -> (Double, Double) -- ^ result and absolute error | ||
65 | derivCentral = derivGen 0 | ||
66 | |||
67 | -- | 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. | ||
68 | derivForward :: Double -- ^ initial step size | ||
69 | -> (Double -> Double) -- ^ function | ||
70 | -> Double -- ^ point where the derivative is taken | ||
71 | -> (Double, Double) -- ^ result and absolute error | ||
72 | derivForward = derivGen 1 | ||
73 | |||
74 | -- | Adaptive backward difference algorithm, /gsl_deriv_backward/. | ||
75 | derivBackward ::Double -- ^ initial step size | ||
76 | -> (Double -> Double) -- ^ function | ||
77 | -> Double -- ^ point where the derivative is taken | ||
78 | -> (Double, Double) -- ^ result and absolute error | ||
79 | derivBackward = derivGen 2 | ||