diff options
Diffstat (limited to 'lib/GSL/Special.hs')
-rw-r--r-- | lib/GSL/Special.hs | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/GSL/Special.hs b/lib/GSL/Special.hs new file mode 100644 index 0000000..643c499 --- /dev/null +++ b/lib/GSL/Special.hs | |||
@@ -0,0 +1,101 @@ | |||
1 | {-# OPTIONS #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : GSL.Special | ||
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 | Special functions. | ||
13 | |||
14 | <http://www.gnu.org/software/gsl/manual/html_node/Special-Functions.html#Special-Functions> | ||
15 | -} | ||
16 | ----------------------------------------------------------------------------- | ||
17 | |||
18 | module GSL.Special ( | ||
19 | erf, | ||
20 | erf_Z, | ||
21 | bessel_J0_e, | ||
22 | exp_e10_e | ||
23 | ) | ||
24 | where | ||
25 | |||
26 | import Foreign | ||
27 | import Data.Packed.Internal.Common(check,(//)) | ||
28 | |||
29 | ---------------------------------------------------------------- | ||
30 | -------------------- simple functions -------------------------- | ||
31 | |||
32 | {- | The error function (/gsl_sf_erf/), defined as 2\/ \\sqrt \\pi * \int\_0\^t \\exp -t\^2 dt. | ||
33 | |||
34 | @> erf 1.5 | ||
35 | 0.9661051464753108@ | ||
36 | |||
37 | -} | ||
38 | foreign import ccall "gsl-aux.h gsl_sf_erf" erf :: Double -> Double | ||
39 | |||
40 | {- | The Gaussian probability density function (/gsl_sf_erf_Z/), defined as (1\/\\sqrt\{2\\pi\}) \\exp(-x\^2\/2). | ||
41 | |||
42 | >> erf_Z 1.5 | ||
43 | >0.12951759566589172 | ||
44 | |||
45 | -} | ||
46 | foreign import ccall "gsl-aux.h gsl_sf_erf_Z" erf_Z :: Double -> Double | ||
47 | |||
48 | ---------------------------------------------------------------- | ||
49 | -- the sf_result struct is equivalent to an array of two doubles | ||
50 | |||
51 | createSFR s f = unsafePerformIO $ do | ||
52 | p <- mallocArray 2 | ||
53 | f p // check "createSFR" [] | ||
54 | [val,err] <- peekArray 2 p | ||
55 | free p | ||
56 | return (val,err) | ||
57 | |||
58 | -------------------- functions returning sf_result ------------- | ||
59 | |||
60 | {- | The regular cylindrical Bessel function of zeroth order, J_0(x). This is | ||
61 | the example in the GSL manual, returning the value of the function and | ||
62 | the error term: | ||
63 | |||
64 | @\> bessel_J0_e 5.0 | ||
65 | (-0.1775967713143383,1.9302109579684196e-16)@ | ||
66 | |||
67 | -} | ||
68 | bessel_J0_e :: Double -> (Double,Double) | ||
69 | bessel_J0_e x = createSFR "bessel_J0_e" (gsl_sf_bessel_J0_e x) | ||
70 | foreign import ccall "gsl-aux.h gsl_sf_bessel_J0_e" gsl_sf_bessel_J0_e :: Double -> Ptr Double -> IO Int | ||
71 | |||
72 | --------------------------------------------------------------------- | ||
73 | -- the sf_result_e10 contains two doubles and the exponent | ||
74 | |||
75 | createSFR_E10 s f = unsafePerformIO $ do | ||
76 | let sd = sizeOf (0::Double) | ||
77 | let si = sizeOf (0::Int) | ||
78 | p <- mallocBytes (2*sd + si) | ||
79 | f p // check "createSFR_E10" [] | ||
80 | val <- peekByteOff p 0 | ||
81 | err <- peekByteOff p sd | ||
82 | expo <- peekByteOff p (2*sd) | ||
83 | free p | ||
84 | return (val,expo,err) | ||
85 | |||
86 | -------------------- functions returning sf_result_e10 ------------- | ||
87 | |||
88 | {- | (From the GSL manual) \"This function computes the exponential \exp(x) using the @gsl_sf_result_e10@ type to return a result with extended range. This function may be useful if the value of \exp(x) would overflow the numeric range of double\". | ||
89 | |||
90 | For example: | ||
91 | |||
92 | @\> exp_e10_e 30.0 | ||
93 | (1.0686474581524432,1.4711818964275088e-14,13)@ | ||
94 | |||
95 | @\> exp 30.0 | ||
96 | 1.0686474581524463e13@ | ||
97 | |||
98 | -} | ||
99 | exp_e10_e :: Double -> (Double,Int,Double) | ||
100 | exp_e10_e x = createSFR_E10 "exp_e10_e" (gsl_sf_exp_e10_e x) | ||
101 | foreign import ccall "gsl-aux.h gsl_sf_exp_e10_e" gsl_sf_exp_e10_e :: Double -> Ptr Double -> IO Int | ||