summaryrefslogtreecommitdiff
path: root/lib/Numeric/GSL
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/GSL')
-rw-r--r--lib/Numeric/GSL/Differentiation.hs10
-rw-r--r--lib/Numeric/GSL/Fitting.hs10
-rw-r--r--lib/Numeric/GSL/Fourier.hs4
-rw-r--r--lib/Numeric/GSL/Integration.hs74
-rw-r--r--lib/Numeric/GSL/Minimization.hs5
-rw-r--r--lib/Numeric/GSL/ODE.hs8
-rw-r--r--lib/Numeric/GSL/Polynomials.hs24
-rw-r--r--lib/Numeric/GSL/Root.hs34
8 files changed, 81 insertions, 88 deletions
diff --git a/lib/Numeric/GSL/Differentiation.hs b/lib/Numeric/GSL/Differentiation.hs
index d2a332c..93c5007 100644
--- a/lib/Numeric/GSL/Differentiation.hs
+++ b/lib/Numeric/GSL/Differentiation.hs
@@ -55,11 +55,11 @@ foreign import ccall safe "gsl-aux.h deriv"
55 55
56{- | Adaptive central difference algorithm, /gsl_deriv_central/. For example: 56{- | Adaptive central difference algorithm, /gsl_deriv_central/. For example:
57 57
58> > let deriv = derivCentral 0.01 58>>> let deriv = derivCentral 0.01
59> > deriv sin (pi/4) 59>>> deriv sin (pi/4)
60>(0.7071067812000676,1.0600063101654055e-10) 60(0.7071067812000676,1.0600063101654055e-10)
61> > cos (pi/4) 61>>> cos (pi/4)
62>0.7071067811865476 620.7071067811865476
63 63
64-} 64-}
65derivCentral :: Double -- ^ initial step size 65derivCentral :: Double -- ^ initial step size
diff --git a/lib/Numeric/GSL/Fitting.hs b/lib/Numeric/GSL/Fitting.hs
index 6343b76..c4f3a91 100644
--- a/lib/Numeric/GSL/Fitting.hs
+++ b/lib/Numeric/GSL/Fitting.hs
@@ -13,7 +13,8 @@ Nonlinear Least-Squares Fitting
13 13
14The example program in the GSL manual (see examples/fitting.hs): 14The example program in the GSL manual (see examples/fitting.hs):
15 15
16@dat = [ 16@
17dat = [
17 ([0.0],([6.0133918608118675],0.1)), 18 ([0.0],([6.0133918608118675],0.1)),
18 ([1.0],([5.5153769909966535],0.1)), 19 ([1.0],([5.5153769909966535],0.1)),
19 ([2.0],([5.261094606015287],0.1)), 20 ([2.0],([5.261094606015287],0.1)),
@@ -25,8 +26,9 @@ expModel [a,lambda,b] [t] = [a * exp (-lambda * t) + b]
25expModelDer [a,lambda,b] [t] = [[exp (-lambda * t), -t * a * exp(-lambda*t) , 1]] 26expModelDer [a,lambda,b] [t] = [[exp (-lambda * t), -t * a * exp(-lambda*t) , 1]]
26 27
27(sol,path) = fitModelScaled 1E-4 1E-4 20 (expModel, expModelDer) dat [1,0,0] 28(sol,path) = fitModelScaled 1E-4 1E-4 20 (expModel, expModelDer) dat [1,0,0]
29@
28 30
29\> path 31>>> path
30(6><5) 32(6><5)
31 [ 1.0, 76.45780563978782, 1.6465931240727802, 1.8147715267618197e-2, 0.6465931240727797 33 [ 1.0, 76.45780563978782, 1.6465931240727802, 1.8147715267618197e-2, 0.6465931240727797
32 , 2.0, 37.683816318260355, 2.858760367632973, 8.092094813253975e-2, 1.4479636296208662 34 , 2.0, 37.683816318260355, 2.858760367632973, 8.092094813253975e-2, 1.4479636296208662
@@ -34,10 +36,10 @@ expModelDer [a,lambda,b] [t] = [[exp (-lambda * t), -t * a * exp(-lambda*t) , 1]
34 , 4.0, 5.630494933603935, 5.021755718065913, 0.10287787128056883, 1.0338835440862608 36 , 4.0, 5.630494933603935, 5.021755718065913, 0.10287787128056883, 1.0338835440862608
35 , 5.0, 5.443976278682909, 5.045204331329302, 0.10405523433131504, 1.019416067207375 37 , 5.0, 5.443976278682909, 5.045204331329302, 0.10405523433131504, 1.019416067207375
36 , 6.0, 5.4439736648994685, 5.045357818922331, 0.10404905846029407, 1.0192487112786812 ] 38 , 6.0, 5.4439736648994685, 5.045357818922331, 0.10404905846029407, 1.0192487112786812 ]
37\> sol 39>>> sol
38[(5.045357818922331,6.027976702418132e-2), 40[(5.045357818922331,6.027976702418132e-2),
39(0.10404905846029407,3.157045047172834e-3), 41(0.10404905846029407,3.157045047172834e-3),
40(1.0192487112786812,3.782067731353722e-2)]@ 42(1.0192487112786812,3.782067731353722e-2)]
41 43
42-} 44-}
43----------------------------------------------------------------------------- 45-----------------------------------------------------------------------------
diff --git a/lib/Numeric/GSL/Fourier.hs b/lib/Numeric/GSL/Fourier.hs
index 4ef19b3..86aedd6 100644
--- a/lib/Numeric/GSL/Fourier.hs
+++ b/lib/Numeric/GSL/Fourier.hs
@@ -35,8 +35,8 @@ foreign import ccall unsafe "gsl-aux.h fft" c_fft :: CInt -> TCVCV
35 35
36{- | Fast 1D Fourier transform of a 'Vector' @(@'Complex' 'Double'@)@ using /gsl_fft_complex_forward/. It uses the same scaling conventions as GNU Octave. 36{- | Fast 1D Fourier transform of a 'Vector' @(@'Complex' 'Double'@)@ using /gsl_fft_complex_forward/. It uses the same scaling conventions as GNU Octave.
37 37
38@> fft ('fromList' [1,2,3,4]) 38>>> fft (fromList [1,2,3,4])
39vector (4) [10.0 :+ 0.0,(-2.0) :+ 2.0,(-2.0) :+ 0.0,(-2.0) :+ (-2.0)]@ 39fromList [10.0 :+ 0.0,(-2.0) :+ 2.0,(-2.0) :+ 0.0,(-2.0) :+ (-2.0)]
40 40
41-} 41-}
42fft :: Vector (Complex Double) -> Vector (Complex Double) 42fft :: Vector (Complex Double) -> Vector (Complex Double)
diff --git a/lib/Numeric/GSL/Integration.hs b/lib/Numeric/GSL/Integration.hs
index 7c1cdb9..5f0a415 100644
--- a/lib/Numeric/GSL/Integration.hs
+++ b/lib/Numeric/GSL/Integration.hs
@@ -35,16 +35,16 @@ eps = 1e-12
35 35
36{- | conversion of Haskell functions into function pointers that can be used in the C side 36{- | conversion of Haskell functions into function pointers that can be used in the C side
37-} 37-}
38foreign import ccall safe "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double)) 38foreign import ccall safe "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double))
39 39
40-------------------------------------------------------------------- 40--------------------------------------------------------------------
41{- | Numerical integration using /gsl_integration_qags/ (adaptive integration with singularities). For example: 41{- | Numerical integration using /gsl_integration_qags/ (adaptive integration with singularities). For example:
42 42
43@\> let quad = integrateQAGS 1E-9 1000 43>>> let quad = integrateQAGS 1E-9 1000
44\> let f a x = x**(-0.5) * log (a*x) 44>>> let f a x = x**(-0.5) * log (a*x)
45\> quad (f 1) 0 1 45>>> quad (f 1) 0 1
46(-3.999999999999974,4.871658632055187e-13)@ 46(-3.999999999999974,4.871658632055187e-13)
47 47
48-} 48-}
49 49
50integrateQAGS :: Double -- ^ precision (e.g. 1E-9) 50integrateQAGS :: Double -- ^ precision (e.g. 1E-9)
@@ -56,7 +56,7 @@ integrateQAGS :: Double -- ^ precision (e.g. 1E-9)
56integrateQAGS prec n f a b = unsafePerformIO $ do 56integrateQAGS prec n f a b = unsafePerformIO $ do
57 r <- malloc 57 r <- malloc
58 e <- malloc 58 e <- malloc
59 fp <- mkfun (\x _ -> f x) 59 fp <- mkfun (\x _ -> f x)
60 c_integrate_qags fp a b eps prec (fromIntegral n) r e // check "integrate_qags" 60 c_integrate_qags fp a b eps prec (fromIntegral n) r e // check "integrate_qags"
61 vr <- peek r 61 vr <- peek r
62 ve <- peek e 62 ve <- peek e
@@ -73,10 +73,10 @@ foreign import ccall safe "integrate_qags" c_integrate_qags
73----------------------------------------------------------------- 73-----------------------------------------------------------------
74{- | Numerical integration using /gsl_integration_qng/ (useful for fast integration of smooth functions). For example: 74{- | Numerical integration using /gsl_integration_qng/ (useful for fast integration of smooth functions). For example:
75 75
76@\> let quad = integrateQNG 1E-6 76>>> let quad = integrateQNG 1E-6
77\> quad (\\x -> 4\/(1+x*x)) 0 1 77>>> quad (\x -> 4/(1+x*x)) 0 1
78(3.141592653589793,3.487868498008632e-14)@ 78(3.141592653589793,3.487868498008632e-14)
79 79
80-} 80-}
81 81
82integrateQNG :: Double -- ^ precision (e.g. 1E-9) 82integrateQNG :: Double -- ^ precision (e.g. 1E-9)
@@ -87,7 +87,7 @@ integrateQNG :: Double -- ^ precision (e.g. 1E-9)
87integrateQNG prec f a b = unsafePerformIO $ do 87integrateQNG prec f a b = unsafePerformIO $ do
88 r <- malloc 88 r <- malloc
89 e <- malloc 89 e <- malloc
90 fp <- mkfun (\x _ -> f x) 90 fp <- mkfun (\x _ -> f x)
91 c_integrate_qng fp a b eps prec r e // check "integrate_qng" 91 c_integrate_qng fp a b eps prec r e // check "integrate_qng"
92 vr <- peek r 92 vr <- peek r
93 ve <- peek e 93 ve <- peek e
@@ -102,14 +102,14 @@ foreign import ccall safe "integrate_qng" c_integrate_qng
102 -> Double -> Double -> Ptr Double -> Ptr Double -> IO CInt 102 -> Double -> Double -> Ptr Double -> Ptr Double -> IO CInt
103 103
104-------------------------------------------------------------------- 104--------------------------------------------------------------------
105{- | Numerical integration using /gsl_integration_qagi/ (integration over the infinite integral -Inf..Inf using QAGS). 105{- | Numerical integration using /gsl_integration_qagi/ (integration over the infinite integral -Inf..Inf using QAGS).
106For example: 106For example:
107 107
108@\> let quad = integrateQAGI 1E-9 1000 108>>> let quad = integrateQAGI 1E-9 1000
109\> let f a x = exp(-a * x^2) 109>>> let f a x = exp(-a * x^2)
110\> quad (f 0.5) 110>>> quad (f 0.5)
111(2.5066282746310002,6.229215880648858e-11)@ 111(2.5066282746310002,6.229215880648858e-11)
112 112
113-} 113-}
114 114
115integrateQAGI :: Double -- ^ precision (e.g. 1E-9) 115integrateQAGI :: Double -- ^ precision (e.g. 1E-9)
@@ -119,7 +119,7 @@ integrateQAGI :: Double -- ^ precision (e.g. 1E-9)
119integrateQAGI prec n f = unsafePerformIO $ do 119integrateQAGI prec n f = unsafePerformIO $ do
120 r <- malloc 120 r <- malloc
121 e <- malloc 121 e <- malloc
122 fp <- mkfun (\x _ -> f x) 122 fp <- mkfun (\x _ -> f x)
123 c_integrate_qagi fp eps prec (fromIntegral n) r e // check "integrate_qagi" 123 c_integrate_qagi fp eps prec (fromIntegral n) r e // check "integrate_qagi"
124 vr <- peek r 124 vr <- peek r
125 ve <- peek e 125 ve <- peek e
@@ -134,14 +134,14 @@ foreign import ccall safe "integrate_qagi" c_integrate_qagi
134 -> CInt -> Ptr Double -> Ptr Double -> IO CInt 134 -> CInt -> Ptr Double -> Ptr Double -> IO CInt
135 135
136-------------------------------------------------------------------- 136--------------------------------------------------------------------
137{- | Numerical integration using /gsl_integration_qagiu/ (integration over the semi-infinite integral a..Inf). 137{- | Numerical integration using /gsl_integration_qagiu/ (integration over the semi-infinite integral a..Inf).
138For example: 138For example:
139 139
140@\> let quad = integrateQAGIU 1E-9 1000 140>>> let quad = integrateQAGIU 1E-9 1000
141\> let f a x = exp(-a * x^2) 141>>> let f a x = exp(-a * x^2)
142\> quad (f 0.5) 0 142>>> quad (f 0.5) 0
143(1.2533141373155001,3.114607940324429e-11)@ 143(1.2533141373155001,3.114607940324429e-11)
144 144
145-} 145-}
146 146
147integrateQAGIU :: Double -- ^ precision (e.g. 1E-9) 147integrateQAGIU :: Double -- ^ precision (e.g. 1E-9)
@@ -152,7 +152,7 @@ integrateQAGIU :: Double -- ^ precision (e.g. 1E-9)
152integrateQAGIU prec n f a = unsafePerformIO $ do 152integrateQAGIU prec n f a = unsafePerformIO $ do
153 r <- malloc 153 r <- malloc
154 e <- malloc 154 e <- malloc
155 fp <- mkfun (\x _ -> f x) 155 fp <- mkfun (\x _ -> f x)
156 c_integrate_qagiu fp a eps prec (fromIntegral n) r e // check "integrate_qagiu" 156 c_integrate_qagiu fp a eps prec (fromIntegral n) r e // check "integrate_qagiu"
157 vr <- peek r 157 vr <- peek r
158 ve <- peek e 158 ve <- peek e
@@ -167,14 +167,14 @@ foreign import ccall safe "integrate_qagiu" c_integrate_qagiu
167 -> Double -> CInt -> Ptr Double -> Ptr Double -> IO CInt 167 -> Double -> CInt -> Ptr Double -> Ptr Double -> IO CInt
168 168
169-------------------------------------------------------------------- 169--------------------------------------------------------------------
170{- | Numerical integration using /gsl_integration_qagil/ (integration over the semi-infinite integral -Inf..b). 170{- | Numerical integration using /gsl_integration_qagil/ (integration over the semi-infinite integral -Inf..b).
171For example: 171For example:
172 172
173@\> let quad = integrateQAGIL 1E-9 1000 173>>> let quad = integrateQAGIL 1E-9 1000
174\> let f a x = exp(-a * x^2) 174>>> let f a x = exp(-a * x^2)
175\> quad (f 0.5) 0 175>>> quad (f 0.5) 0
176(1.2533141373155001,3.114607940324429e-11)@ 176(1.2533141373155001,3.114607940324429e-11)
177 177
178-} 178-}
179 179
180integrateQAGIL :: Double -- ^ precision (e.g. 1E-9) 180integrateQAGIL :: Double -- ^ precision (e.g. 1E-9)
@@ -185,7 +185,7 @@ integrateQAGIL :: Double -- ^ precision (e.g. 1E-9)
185integrateQAGIL prec n f b = unsafePerformIO $ do 185integrateQAGIL prec n f b = unsafePerformIO $ do
186 r <- malloc 186 r <- malloc
187 e <- malloc 187 e <- malloc
188 fp <- mkfun (\x _ -> f x) 188 fp <- mkfun (\x _ -> f x)
189 c_integrate_qagil fp b eps prec (fromIntegral n) r e // check "integrate_qagil" 189 c_integrate_qagil fp b eps prec (fromIntegral n) r e // check "integrate_qagil"
190 vr <- peek r 190 vr <- peek r
191 ve <- peek e 191 ve <- peek e
@@ -212,10 +212,10 @@ routines in QUADPACK, yet fails less often for difficult integrands.@
212 212
213For example: 213For example:
214 214
215@\> let quad = integrateCQUAD 1E-12 1000 215>>> let quad = integrateCQUAD 1E-12 1000
216\> let f a x = exp(-a * x^2) 216>>> let f a x = exp(-a * x^2)
217\> quad (f 0.5) 2 5 217>>> quad (f 0.5) 2 5
218(5.7025405463957006e-2,9.678874441303705e-16,95)@ 218(5.7025405463957006e-2,9.678874441303705e-16,95)
219 219
220Unlike other quadrature methods, integrateCQUAD also returns the 220Unlike other quadrature methods, integrateCQUAD also returns the
221number of function evaluations required. 221number of function evaluations required.
diff --git a/lib/Numeric/GSL/Minimization.hs b/lib/Numeric/GSL/Minimization.hs
index 7ccca81..1879dab 100644
--- a/lib/Numeric/GSL/Minimization.hs
+++ b/lib/Numeric/GSL/Minimization.hs
@@ -16,15 +16,15 @@ Minimization of a multidimensional function using some of the algorithms describ
16The example in the GSL manual: 16The example in the GSL manual:
17 17
18@ 18@
19
20f [x,y] = 10*(x-1)^2 + 20*(y-2)^2 + 30 19f [x,y] = 10*(x-1)^2 + 20*(y-2)^2 + 30
21 20
22main = do 21main = do
23 let (s,p) = minimize NMSimplex2 1E-2 30 [1,1] f [5,7] 22 let (s,p) = minimize NMSimplex2 1E-2 30 [1,1] f [5,7]
24 print s 23 print s
25 print p 24 print p
25@
26 26
27\> main 27>>> main
28[0.9920430849306288,1.9969168063253182] 28[0.9920430849306288,1.9969168063253182]
29 0.000 512.500 1.130 6.500 5.000 29 0.000 512.500 1.130 6.500 5.000
30 1.000 290.625 1.409 5.250 4.000 30 1.000 290.625 1.409 5.250 4.000
@@ -33,7 +33,6 @@ main = do
33 ... 33 ...
3422.000 30.001 0.013 0.992 1.997 3422.000 30.001 0.013 0.992 1.997
3523.000 30.001 0.008 0.992 1.997 3523.000 30.001 0.008 0.992 1.997
36@
37 36
38The path to the solution can be graphically shown by means of: 37The path to the solution can be graphically shown by means of:
39 38
diff --git a/lib/Numeric/GSL/ODE.hs b/lib/Numeric/GSL/ODE.hs
index ab037bd..9a29085 100644
--- a/lib/Numeric/GSL/ODE.hs
+++ b/lib/Numeric/GSL/ODE.hs
@@ -13,9 +13,10 @@ Solution of ordinary differential equation (ODE) initial value problems.
13 13
14A simple example: 14A simple example:
15 15
16@import Numeric.GSL 16@
17import Numeric.GSL.ODE
17import Numeric.LinearAlgebra 18import Numeric.LinearAlgebra
18import Graphics.Plot 19import Numeric.LinearAlgebra.Util(mplot)
19 20
20xdot t [x,v] = [v, -0.95*x - 0.1*v] 21xdot t [x,v] = [v, -0.95*x - 0.1*v]
21 22
@@ -23,7 +24,8 @@ ts = linspace 100 (0,20 :: Double)
23 24
24sol = odeSolve xdot [10,0] ts 25sol = odeSolve xdot [10,0] ts
25 26
26main = mplot (ts : toColumns sol)@ 27main = mplot (ts : toColumns sol)
28@
27 29
28-} 30-}
29----------------------------------------------------------------------------- 31-----------------------------------------------------------------------------
diff --git a/lib/Numeric/GSL/Polynomials.hs b/lib/Numeric/GSL/Polynomials.hs
index 694c003..290c615 100644
--- a/lib/Numeric/GSL/Polynomials.hs
+++ b/lib/Numeric/GSL/Polynomials.hs
@@ -27,22 +27,22 @@ import System.IO.Unsafe (unsafePerformIO)
27import Foreign.C.Types (CInt(..)) 27import Foreign.C.Types (CInt(..))
28#endif 28#endif
29 29
30{- | Solution of general polynomial equations, using /gsl_poly_complex_solve/. For example, 30{- | Solution of general polynomial equations, using /gsl_poly_complex_solve/.
31 the three solutions of x^3 + 8 = 0 31
32For example, the three solutions of x^3 + 8 = 0
33
34>>> polySolve [8,0,0,1]
35[(-2.0) :+ 0.0,1.0 :+ 1.7320508075688776,1.0 :+ (-1.7320508075688776)]
32 36
33@\> polySolve [8,0,0,1]
34[(-1.9999999999999998) :+ 0.0,
35 1.0 :+ 1.732050807568877,
36 1.0 :+ (-1.732050807568877)]@
37 37
38The example in the GSL manual: To find the roots of x^5 -1 = 0: 38The example in the GSL manual: To find the roots of x^5 -1 = 0:
39 39
40@\> polySolve [-1, 0, 0, 0, 0, 1] 40>>> polySolve [-1, 0, 0, 0, 0, 1]
41[(-0.8090169943749475) :+ 0.5877852522924731, 41[(-0.8090169943749472) :+ 0.5877852522924731,
42(-0.8090169943749475) :+ (-0.5877852522924731), 42(-0.8090169943749472) :+ (-0.5877852522924731),
430.30901699437494734 :+ 0.9510565162951536, 430.30901699437494756 :+ 0.9510565162951535,
440.30901699437494734 :+ (-0.9510565162951536), 440.30901699437494756 :+ (-0.9510565162951535),
451.0 :+ 0.0]@ 451.0000000000000002 :+ 0.0]
46 46
47-} 47-}
48polySolve :: [Double] -> [Complex Double] 48polySolve :: [Double] -> [Complex Double]
diff --git a/lib/Numeric/GSL/Root.hs b/lib/Numeric/GSL/Root.hs
index 6da15e5..9d561c4 100644
--- a/lib/Numeric/GSL/Root.hs
+++ b/lib/Numeric/GSL/Root.hs
@@ -13,33 +13,23 @@ Multidimensional root finding.
13 13
14The example in the GSL manual: 14The example in the GSL manual:
15 15
16@import Numeric.GSL 16>>> let rosenbrock a b [x,y] = [ a*(1-x), b*(y-x^2) ]
17import Numeric.LinearAlgebra(format) 17>>> let (sol,path) = root Hybrids 1E-7 30 (rosenbrock 1 10) [-10,-5]
18import Text.Printf(printf) 18>>> sol
19
20rosenbrock a b [x,y] = [ a*(1-x), b*(y-x^2) ]
21
22disp = putStrLn . format \" \" (printf \"%.3f\")
23
24main = do
25 let (sol,path) = root Hybrids 1E-7 30 (rosenbrock 1 10) [-10,-5]
26 print sol
27 disp path
28
29\> main
30[1.0,1.0] 19[1.0,1.0]
31 0.000 -10.000 -5.000 11.000 -1050.000 20>>> disp 3 path
32 1.000 -3.976 24.827 4.976 90.203 2111x5
22 1.000 -10.000 -5.000 11.000 -1050.000
33 2.000 -3.976 24.827 4.976 90.203 23 2.000 -3.976 24.827 4.976 90.203
34 3.000 -3.976 24.827 4.976 90.203 24 3.000 -3.976 24.827 4.976 90.203
35 4.000 -1.274 -5.680 2.274 -73.018 25 4.000 -3.976 24.827 4.976 90.203
36 5.000 -1.274 -5.680 2.274 -73.018 26 5.000 -1.274 -5.680 2.274 -73.018
37 6.000 0.249 0.298 0.751 2.359 27 6.000 -1.274 -5.680 2.274 -73.018
38 7.000 0.249 0.298 0.751 2.359 28 7.000 0.249 0.298 0.751 2.359
39 8.000 1.000 0.878 -0.000 -1.218 29 8.000 0.249 0.298 0.751 2.359
40 9.000 1.000 0.989 -0.000 -0.108 30 9.000 1.000 0.878 -0.000 -1.218
4110.000 1.000 1.000 0.000 0.000 3110.000 1.000 0.989 -0.000 -0.108
42@ 3211.000 1.000 1.000 0.000 0.000
43 33
44-} 34-}
45----------------------------------------------------------------------------- 35-----------------------------------------------------------------------------