summaryrefslogtreecommitdiff
path: root/packages/gsl/src/Numeric/GSL/Integration.hs
blob: 9c1d43ab067f03e78facd8ff3fd65d5be147b8cc (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{- |
Module      :  Numeric.GSL.Integration
Copyright   :  (c) Alberto Ruiz 2006
License     :  GPL
Maintainer  :  Alberto Ruiz
Stability   :  provisional

Numerical integration routines.

<http://www.gnu.org/software/gsl/manual/html_node/Numerical-Integration.html#Numerical-Integration>
-}


module Numeric.GSL.Integration (
    integrateQNG,
    integrateQAGS,
    integrateQAGI,
    integrateQAGIU,
    integrateQAGIL,
    integrateCQUAD
) where

import Foreign.C.Types
import Foreign.Marshal.Alloc(malloc, free)
import Foreign.Ptr(Ptr, FunPtr, freeHaskellFunPtr)
import Foreign.Storable(peek)
import Numeric.GSL.Internal
import System.IO.Unsafe(unsafePerformIO)

eps = 1e-12

{- | conversion of Haskell functions into function pointers that can be used in the C side
-}
foreign import ccall safe "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double))

--------------------------------------------------------------------
{- | Numerical integration using /gsl_integration_qags/ (adaptive integration with singularities). For example:

>>> let quad = integrateQAGS 1E-9 1000
>>> let f a x = x**(-0.5) * log (a*x)
>>> quad (f 1) 0 1
(-3.999999999999974,4.871658632055187e-13)

-}

integrateQAGS :: Double               -- ^ precision (e.g. 1E-9)
                 -> Int               -- ^ size of auxiliary workspace (e.g. 1000)
                 -> (Double -> Double) -- ^ function to be integrated on the interval (a,b)
                 -> Double           -- ^ a
                 -> Double           -- ^ b
                 -> (Double, Double) -- ^ result of the integration and error
integrateQAGS prec n f a b = unsafePerformIO $ do
    r <- malloc
    e <- malloc
    fp <- mkfun (\x _ -> f x)
    c_integrate_qags fp a b eps prec (fromIntegral n) r e // check "integrate_qags"
    vr <- peek r
    ve <- peek e
    let result = (vr,ve)
    free r
    free e
    freeHaskellFunPtr fp
    return result

foreign import ccall safe "integrate_qags" c_integrate_qags
    :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double
    -> Double -> Double -> CInt -> Ptr Double -> Ptr Double -> IO CInt

-----------------------------------------------------------------
{- | Numerical integration using /gsl_integration_qng/ (useful for fast integration of smooth functions). For example:

>>> let quad = integrateQNG 1E-6
>>> quad (\x -> 4/(1+x*x)) 0 1
(3.141592653589793,3.487868498008632e-14)

-}

integrateQNG :: Double               -- ^ precision (e.g. 1E-9)
                 -> (Double -> Double) -- ^ function to be integrated on the interval (a,b)
                 -> Double           -- ^ a
                 -> Double           -- ^ b
                 -> (Double, Double) -- ^ result of the integration and error
integrateQNG prec f a b = unsafePerformIO $ do
    r <- malloc
    e <- malloc
    fp <- mkfun (\x _ -> f x)
    c_integrate_qng fp a b eps prec r e  // check "integrate_qng"
    vr <- peek r
    ve <- peek e
    let result = (vr,ve)
    free r
    free e
    freeHaskellFunPtr fp
    return result

foreign import ccall safe "integrate_qng" c_integrate_qng
    :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double
    -> Double -> Double -> Ptr Double -> Ptr Double -> IO CInt

--------------------------------------------------------------------
{- | Numerical integration using /gsl_integration_qagi/ (integration over the infinite integral -Inf..Inf using QAGS).
For example:

>>> let quad = integrateQAGI 1E-9 1000
>>> let f a x = exp(-a * x^2)
>>> quad (f 0.5)
(2.5066282746310002,6.229215880648858e-11)

-}

integrateQAGI :: Double               -- ^ precision (e.g. 1E-9)
                 -> Int               -- ^ size of auxiliary workspace (e.g. 1000)
                 -> (Double -> Double) -- ^ function to be integrated on the interval (-Inf,Inf)
                 -> (Double, Double) -- ^ result of the integration and error
integrateQAGI prec n f = unsafePerformIO $ do
    r <- malloc
    e <- malloc
    fp <- mkfun (\x _ -> f x)
    c_integrate_qagi fp eps prec (fromIntegral n) r e // check "integrate_qagi"
    vr <- peek r
    ve <- peek e
    let result = (vr,ve)
    free r
    free e
    freeHaskellFunPtr fp
    return result

foreign import ccall safe "integrate_qagi" c_integrate_qagi
    :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double
    -> CInt -> Ptr Double -> Ptr Double -> IO CInt

--------------------------------------------------------------------
{- | Numerical integration using /gsl_integration_qagiu/ (integration over the semi-infinite integral a..Inf).
For example:

>>> let quad = integrateQAGIU 1E-9 1000
>>> let f a x = exp(-a * x^2)
>>> quad (f 0.5) 0
(1.2533141373155001,3.114607940324429e-11)

-}

integrateQAGIU :: Double               -- ^ precision (e.g. 1E-9)
                 -> Int               -- ^ size of auxiliary workspace (e.g. 1000)
                 -> (Double -> Double) -- ^ function to be integrated on the interval (a,Inf)
                 -> Double             -- ^ a
                 -> (Double, Double) -- ^ result of the integration and error
integrateQAGIU prec n f a = unsafePerformIO $ do
    r <- malloc
    e <- malloc
    fp <- mkfun (\x _ -> f x)
    c_integrate_qagiu fp a eps prec (fromIntegral n) r e // check "integrate_qagiu"
    vr <- peek r
    ve <- peek e
    let result = (vr,ve)
    free r
    free e
    freeHaskellFunPtr fp
    return result

foreign import ccall safe "integrate_qagiu" c_integrate_qagiu
    :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double
    -> Double -> CInt -> Ptr Double -> Ptr Double -> IO CInt

--------------------------------------------------------------------
{- | Numerical integration using /gsl_integration_qagil/ (integration over the semi-infinite integral -Inf..b).
For example:

>>> let quad = integrateQAGIL 1E-9 1000
>>> let f a x = exp(-a * x^2)
>>> quad (f 0.5) 0
(1.2533141373155001,3.114607940324429e-11)

-}

integrateQAGIL :: Double               -- ^ precision (e.g. 1E-9)
                 -> Int               -- ^ size of auxiliary workspace (e.g. 1000)
                 -> (Double -> Double) -- ^ function to be integrated on the interval (a,Inf)
                 -> Double             -- ^ b
                 -> (Double, Double) -- ^ result of the integration and error
integrateQAGIL prec n f b = unsafePerformIO $ do
    r <- malloc
    e <- malloc
    fp <- mkfun (\x _ -> f x)
    c_integrate_qagil fp b eps prec (fromIntegral n) r e // check "integrate_qagil"
    vr <- peek r
    ve <- peek e
    let result = (vr,ve)
    free r
    free e
    freeHaskellFunPtr fp
    return result

foreign import ccall safe "gsl-aux.h integrate_qagil" c_integrate_qagil
    :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double
    -> Double -> CInt -> Ptr Double -> Ptr Double -> IO CInt


--------------------------------------------------------------------
{- | Numerical integration using /gsl_integration_cquad/ (quadrature
for general integrands).  From the GSL manual:

@CQUAD is a new doubly-adaptive general-purpose quadrature routine
which can handle most types of singularities, non-numerical function
values such as Inf or NaN, as well as some divergent integrals. It
generally requires more function evaluations than the integration
routines in QUADPACK, yet fails less often for difficult integrands.@

For example:

>>> let quad = integrateCQUAD 1E-12 1000
>>> let f a x = exp(-a * x^2)
>>> quad (f 0.5) 2 5
(5.7025405463957006e-2,9.678874441303705e-16,95)

Unlike other quadrature methods, integrateCQUAD also returns the
number of function evaluations required.

-}

integrateCQUAD :: Double               -- ^ precision (e.g. 1E-9)
                 -> Int               -- ^ size of auxiliary workspace (e.g. 1000)
                 -> (Double -> Double) -- ^ function to be integrated on the interval (a, b)
                 -> Double             -- ^ a
                 -> Double             -- ^ b
                 -> (Double, Double, Int) -- ^ result of the integration, error and number of function evaluations performed
integrateCQUAD prec n f a b = unsafePerformIO $ do
    r <- malloc
    e <- malloc
    neval <- malloc
    fp <- mkfun (\x _ -> f x)
    c_integrate_cquad fp a b eps prec (fromIntegral n) r e neval // check "integrate_cquad"
    vr <- peek r
    ve <- peek e
    vneval <- peek neval
    let result = (vr,ve,vneval)
    free r
    free e
    free neval
    freeHaskellFunPtr fp
    return result

foreign import ccall safe "integrate_cquad" c_integrate_cquad
    :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double
    -> Double -> Double -> CInt -> Ptr Double -> Ptr Double -> Ptr Int -> IO CInt