summaryrefslogtreecommitdiff
path: root/lib/Numeric
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric')
-rw-r--r--lib/Numeric/GSL/Differentiation.hs10
-rw-r--r--lib/Numeric/GSL/Fourier.hs3
-rw-r--r--lib/Numeric/GSL/Integration.hs11
-rw-r--r--lib/Numeric/GSL/Minimization.hs31
-rw-r--r--lib/Numeric/GSL/Vector.hs31
-rw-r--r--lib/Numeric/GSL/gsl-aux.h2
6 files changed, 50 insertions, 38 deletions
diff --git a/lib/Numeric/GSL/Differentiation.hs b/lib/Numeric/GSL/Differentiation.hs
index 841f2a0..ebbada0 100644
--- a/lib/Numeric/GSL/Differentiation.hs
+++ b/lib/Numeric/GSL/Differentiation.hs
@@ -24,10 +24,10 @@ module Numeric.GSL.Differentiation (
24 24
25import Foreign 25import Foreign
26import Foreign.C.Types(CInt) 26import Foreign.C.Types(CInt)
27import Data.Packed.Internal(mkfun,check,(//)) 27import Data.Packed.Internal(check,(//))
28 28
29derivGen :: 29derivGen ::
30 Int -- ^ type: 0 central, 1 forward, 2 backward 30 CInt -- ^ type: 0 central, 1 forward, 2 backward
31 -> Double -- ^ initial step size 31 -> Double -- ^ initial step size
32 -> (Double -> Double) -- ^ function 32 -> (Double -> Double) -- ^ function
33 -> Double -- ^ point where the derivative is taken 33 -> Double -- ^ point where the derivative is taken
@@ -46,7 +46,7 @@ derivGen c h f x = unsafePerformIO $ do
46 return result 46 return result
47 47
48foreign import ccall "gsl-aux.h deriv" 48foreign import ccall "gsl-aux.h deriv"
49 c_deriv :: Int -> FunPtr (Double -> Ptr () -> Double) -> Double -> Double 49 c_deriv :: CInt -> FunPtr (Double -> Ptr () -> Double) -> Double -> Double
50 -> Ptr Double -> Ptr Double -> IO CInt 50 -> Ptr Double -> Ptr Double -> IO CInt
51 51
52 52
@@ -78,3 +78,7 @@ derivBackward ::Double -- ^ initial step size
78 -> Double -- ^ point where the derivative is taken 78 -> Double -- ^ point where the derivative is taken
79 -> (Double, Double) -- ^ result and absolute error 79 -> (Double, Double) -- ^ result and absolute error
80derivBackward = derivGen 2 80derivBackward = derivGen 2
81
82{- | conversion of Haskell functions into function pointers that can be used in the C side
83-}
84foreign import ccall "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double))
diff --git a/lib/Numeric/GSL/Fourier.hs b/lib/Numeric/GSL/Fourier.hs
index 149c1e1..98bfb9c 100644
--- a/lib/Numeric/GSL/Fourier.hs
+++ b/lib/Numeric/GSL/Fourier.hs
@@ -23,13 +23,14 @@ module Numeric.GSL.Fourier (
23import Data.Packed.Internal 23import Data.Packed.Internal
24import Complex 24import Complex
25import Foreign 25import Foreign
26import Foreign.C.Types(CInt)
26 27
27genfft code v = unsafePerformIO $ do 28genfft code v = unsafePerformIO $ do
28 r <- createVector (dim v) 29 r <- createVector (dim v)
29 app2 (c_fft code) vec v vec r "fft" 30 app2 (c_fft code) vec v vec r "fft"
30 return r 31 return r
31 32
32foreign import ccall "gsl-aux.h fft" c_fft :: Int -> TCVCV 33foreign import ccall "gsl-aux.h fft" c_fft :: CInt -> TCVCV
33 34
34 35
35{- | 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.
diff --git a/lib/Numeric/GSL/Integration.hs b/lib/Numeric/GSL/Integration.hs
index b5f5930..7103ea4 100644
--- a/lib/Numeric/GSL/Integration.hs
+++ b/lib/Numeric/GSL/Integration.hs
@@ -22,9 +22,13 @@ module Numeric.GSL.Integration (
22 22
23import Foreign 23import Foreign
24import Foreign.C.Types(CInt) 24import Foreign.C.Types(CInt)
25import Data.Packed.Internal(mkfun,check,(//)) 25import Data.Packed.Internal(check,(//))
26 26
27 27
28{- | conversion of Haskell functions into function pointers that can be used in the C side
29-}
30foreign import ccall "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double))
31
28-------------------------------------------------------------------- 32--------------------------------------------------------------------
29{- | Numerical integration using /gsl_integration_qags/ (adaptive integration with singularities). For example: 33{- | Numerical integration using /gsl_integration_qags/ (adaptive integration with singularities). For example:
30 34
@@ -45,7 +49,7 @@ integrateQAGS prec n f a b = unsafePerformIO $ do
45 r <- malloc 49 r <- malloc
46 e <- malloc 50 e <- malloc
47 fp <- mkfun (\x _ -> f x) 51 fp <- mkfun (\x _ -> f x)
48 c_integrate_qags fp a b prec n r e // check "integrate_qags" 52 c_integrate_qags fp a b prec (fromIntegral n) r e // check "integrate_qags"
49 vr <- peek r 53 vr <- peek r
50 ve <- peek e 54 ve <- peek e
51 let result = (vr,ve) 55 let result = (vr,ve)
@@ -55,7 +59,7 @@ integrateQAGS prec n f a b = unsafePerformIO $ do
55 return result 59 return result
56 60
57foreign import ccall "gsl-aux.h integrate_qags" 61foreign import ccall "gsl-aux.h integrate_qags"
58 c_integrate_qags :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double -> Double -> Int 62 c_integrate_qags :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double -> Double -> CInt
59 -> Ptr Double -> Ptr Double -> IO CInt 63 -> Ptr Double -> Ptr Double -> IO CInt
60 64
61----------------------------------------------------------------- 65-----------------------------------------------------------------
@@ -88,4 +92,3 @@ integrateQNG prec f a b = unsafePerformIO $ do
88foreign import ccall "gsl-aux.h integrate_qng" 92foreign import ccall "gsl-aux.h integrate_qng"
89 c_integrate_qng :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double -> Double 93 c_integrate_qng :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double -> Double
90 -> Ptr Double -> Ptr Double -> IO CInt 94 -> Ptr Double -> Ptr Double -> IO CInt
91
diff --git a/lib/Numeric/GSL/Minimization.hs b/lib/Numeric/GSL/Minimization.hs
index 98c0ca9..65705cc 100644
--- a/lib/Numeric/GSL/Minimization.hs
+++ b/lib/Numeric/GSL/Minimization.hs
@@ -24,6 +24,7 @@ module Numeric.GSL.Minimization (
24import Data.Packed.Internal 24import Data.Packed.Internal
25import Data.Packed.Matrix 25import Data.Packed.Matrix
26import Foreign 26import Foreign
27import Foreign.C.Types(CInt)
27 28
28------------------------------------------------------------------------- 29-------------------------------------------------------------------------
29 30
@@ -85,7 +86,7 @@ minimizeNMSimplex f xi sz tol maxit = unsafePerformIO $ do
85 fp <- mkVecfun (iv (f.toList)) 86 fp <- mkVecfun (iv (f.toList))
86 rawpath <- ww2 withVector xiv withVector szv $ \xiv' szv' -> 87 rawpath <- ww2 withVector xiv withVector szv $ \xiv' szv' ->
87 createMIO maxit (n+3) 88 createMIO maxit (n+3)
88 (c_minimizeNMSimplex fp tol maxit // xiv' // szv') 89 (c_minimizeNMSimplex fp tol (fromIntegral maxit) // xiv' // szv')
89 "minimizeNMSimplex" 90 "minimizeNMSimplex"
90 let it = round (rawpath @@> (maxit-1,0)) 91 let it = round (rawpath @@> (maxit-1,0))
91 path = takeRows it rawpath 92 path = takeRows it rawpath
@@ -95,7 +96,7 @@ minimizeNMSimplex f xi sz tol maxit = unsafePerformIO $ do
95 96
96 97
97foreign import ccall "gsl-aux.h minimize" 98foreign import ccall "gsl-aux.h minimize"
98 c_minimizeNMSimplex:: FunPtr (Int -> Ptr Double -> Double) -> Double -> Int 99 c_minimizeNMSimplex:: FunPtr (CInt -> Ptr Double -> Double) -> Double -> CInt
99 -> TVVM 100 -> TVVM
100 101
101---------------------------------------------------------------------------------- 102----------------------------------------------------------------------------------
@@ -151,7 +152,7 @@ minimizeConjugateGradient istep minimpar tol maxit f df xi = unsafePerformIO $ d
151 dfp <- mkVecVecfun (aux_vTov df') 152 dfp <- mkVecVecfun (aux_vTov df')
152 rawpath <- withVector xiv $ \xiv' -> 153 rawpath <- withVector xiv $ \xiv' ->
153 createMIO maxit (n+2) 154 createMIO maxit (n+2)
154 (c_minimizeConjugateGradient fp dfp istep minimpar tol maxit // xiv') 155 (c_minimizeConjugateGradient fp dfp istep minimpar tol (fromIntegral maxit) // xiv')
155 "minimizeDerivV" 156 "minimizeDerivV"
156 let it = round (rawpath @@> (maxit-1,0)) 157 let it = round (rawpath @@> (maxit-1,0))
157 path = takeRows it rawpath 158 path = takeRows it rawpath
@@ -162,36 +163,36 @@ minimizeConjugateGradient istep minimpar tol maxit f df xi = unsafePerformIO $ d
162 163
163 164
164foreign import ccall "gsl-aux.h minimizeWithDeriv" 165foreign import ccall "gsl-aux.h minimizeWithDeriv"
165 c_minimizeConjugateGradient :: FunPtr (Int -> Ptr Double -> Double) 166 c_minimizeConjugateGradient :: FunPtr (CInt -> Ptr Double -> Double)
166 -> FunPtr (Int -> Ptr Double -> Ptr Double -> IO ()) 167 -> FunPtr (CInt -> Ptr Double -> Ptr Double -> IO ())
167 -> Double -> Double -> Double -> Int 168 -> Double -> Double -> Double -> CInt
168 -> TVM 169 -> TVM
169 170
170--------------------------------------------------------------------- 171---------------------------------------------------------------------
171iv :: (Vector Double -> Double) -> (Int -> Ptr Double -> Double) 172iv :: (Vector Double -> Double) -> (CInt -> Ptr Double -> Double)
172iv f n p = f (createV n copy "iv") where 173iv f n p = f (createV (fromIntegral n) copy "iv") where
173 copy n' q = do 174 copy n' q = do
174 copyArray q p n' 175 copyArray q p n'
175 return 0 176 return 0
176 177
177-- | conversion of Haskell functions into function pointers that can be used in the C side 178-- | conversion of Haskell functions into function pointers that can be used in the C side
178foreign import ccall "wrapper" 179foreign import ccall "wrapper"
179 mkVecfun :: (Int -> Ptr Double -> Double) 180 mkVecfun :: (CInt -> Ptr Double -> Double)
180 -> IO( FunPtr (Int -> Ptr Double -> Double)) 181 -> IO( FunPtr (CInt -> Ptr Double -> Double))
181 182
182-- | another required conversion 183-- | another required conversion
183foreign import ccall "wrapper" 184foreign import ccall "wrapper"
184 mkVecVecfun :: (Int -> Ptr Double -> Ptr Double -> IO ()) 185 mkVecVecfun :: (CInt -> Ptr Double -> Ptr Double -> IO ())
185 -> IO (FunPtr (Int -> Ptr Double -> Ptr Double->IO())) 186 -> IO (FunPtr (CInt -> Ptr Double -> Ptr Double->IO()))
186 187
187aux_vTov :: (Vector Double -> Vector Double) -> (Int -> Ptr Double -> Ptr Double -> IO()) 188aux_vTov :: (Vector Double -> Vector Double) -> (CInt -> Ptr Double -> Ptr Double -> IO())
188aux_vTov f n p r = g where 189aux_vTov f n p r = g where
189 V {fptr = pr} = f x 190 V {fptr = pr} = f x
190 x = createV n copy "aux_vTov" 191 x = createV (fromIntegral n) copy "aux_vTov"
191 copy n' q = do 192 copy n' q = do
192 copyArray q p n' 193 copyArray q p n'
193 return 0 194 return 0
194 g = withForeignPtr pr $ \p' -> copyArray r p' n 195 g = withForeignPtr pr $ \p' -> copyArray r p' (fromIntegral n)
195 196
196-------------------------------------------------------------------- 197--------------------------------------------------------------------
197 198
diff --git a/lib/Numeric/GSL/Vector.hs b/lib/Numeric/GSL/Vector.hs
index 86d8f10..0db2f84 100644
--- a/lib/Numeric/GSL/Vector.hs
+++ b/lib/Numeric/GSL/Vector.hs
@@ -25,6 +25,9 @@ import Data.Packed.Internal.Vector
25 25
26import Complex 26import Complex
27import Foreign 27import Foreign
28import Foreign.C.Types(CInt)
29
30fromei = fromIntegral . fromei
28 31
29data FunCodeV = Sin 32data FunCodeV = Sin
30 | Cos 33 | Cos
@@ -73,33 +76,33 @@ data FunCodeS = Norm2
73 76
74toScalarAux fun code v = unsafePerformIO $ do 77toScalarAux fun code v = unsafePerformIO $ do
75 r <- createVector 1 78 r <- createVector 1
76 app2 (fun (fromEnum code)) vec v vec r "toScalarAux" 79 app2 (fun (fromei code)) vec v vec r "toScalarAux"
77 return (r `at` 0) 80 return (r `at` 0)
78 81
79vectorMapAux fun code v = unsafePerformIO $ do 82vectorMapAux fun code v = unsafePerformIO $ do
80 r <- createVector (dim v) 83 r <- createVector (dim v)
81 app2 (fun (fromEnum code)) vec v vec r "vectorMapAux" 84 app2 (fun (fromei code)) vec v vec r "vectorMapAux"
82 return r 85 return r
83 86
84vectorMapValAux fun code val v = unsafePerformIO $ do 87vectorMapValAux fun code val v = unsafePerformIO $ do
85 r <- createVector (dim v) 88 r <- createVector (dim v)
86 pval <- newArray [val] 89 pval <- newArray [val]
87 app2 (fun (fromEnum code) pval) vec v vec r "vectorMapValAux" 90 app2 (fun (fromei code) pval) vec v vec r "vectorMapValAux"
88 free pval 91 free pval
89 return r 92 return r
90 93
91vectorZipAux fun code u v = unsafePerformIO $ do 94vectorZipAux fun code u v = unsafePerformIO $ do
92 r <- createVector (dim u) 95 r <- createVector (dim u)
93 app3 (fun (fromEnum code)) vec u vec v vec r "vectorZipAux" 96 app3 (fun (fromei code)) vec u vec v vec r "vectorZipAux"
94 return r 97 return r
95 98
96--------------------------------------------------------------------- 99---------------------------------------------------------------------
97 100
98-- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. 101-- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc.
99toScalarR :: FunCodeS -> Vector Double -> Double 102toScalarR :: FunCodeS -> Vector Double -> Double
100toScalarR oper = toScalarAux c_toScalarR (fromEnum oper) 103toScalarR oper = toScalarAux c_toScalarR (fromei oper)
101 104
102foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: Int -> TVV 105foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: CInt -> TVV
103 106
104------------------------------------------------------------------ 107------------------------------------------------------------------
105 108
@@ -107,27 +110,27 @@ foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: Int -> TVV
107vectorMapR :: FunCodeV -> Vector Double -> Vector Double 110vectorMapR :: FunCodeV -> Vector Double -> Vector Double
108vectorMapR = vectorMapAux c_vectorMapR 111vectorMapR = vectorMapAux c_vectorMapR
109 112
110foreign import ccall safe "gsl-aux.h mapR" c_vectorMapR :: Int -> TVV 113foreign import ccall safe "gsl-aux.h mapR" c_vectorMapR :: CInt -> TVV
111 114
112-- | map of complex vectors with given function 115-- | map of complex vectors with given function
113vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double) 116vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double)
114vectorMapC oper = vectorMapAux c_vectorMapC (fromEnum oper) 117vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper)
115 118
116foreign import ccall safe "gsl-aux.h mapC" c_vectorMapC :: Int -> TCVCV 119foreign import ccall safe "gsl-aux.h mapC" c_vectorMapC :: CInt -> TCVCV
117 120
118------------------------------------------------------------------- 121-------------------------------------------------------------------
119 122
120-- | map of real vectors with given function 123-- | map of real vectors with given function
121vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double 124vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double
122vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromEnum oper) 125vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromei oper)
123 126
124foreign import ccall safe "gsl-aux.h mapValR" c_vectorMapValR :: Int -> Ptr Double -> TVV 127foreign import ccall safe "gsl-aux.h mapValR" c_vectorMapValR :: CInt -> Ptr Double -> TVV
125 128
126-- | map of complex vectors with given function 129-- | map of complex vectors with given function
127vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double) 130vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double)
128vectorMapValC = vectorMapValAux c_vectorMapValC 131vectorMapValC = vectorMapValAux c_vectorMapValC
129 132
130foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: Int -> Ptr (Complex Double) -> TCVCV 133foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV
131 134
132------------------------------------------------------------------- 135-------------------------------------------------------------------
133 136
@@ -135,10 +138,10 @@ foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: Int -> Ptr (Com
135vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double 138vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double
136vectorZipR = vectorZipAux c_vectorZipR 139vectorZipR = vectorZipAux c_vectorZipR
137 140
138foreign import ccall safe "gsl-aux.h zipR" c_vectorZipR :: Int -> TVVV 141foreign import ccall safe "gsl-aux.h zipR" c_vectorZipR :: CInt -> TVVV
139 142
140-- | elementwise operation on complex vectors 143-- | elementwise operation on complex vectors
141vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double) 144vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double)
142vectorZipC = vectorZipAux c_vectorZipC 145vectorZipC = vectorZipAux c_vectorZipC
143 146
144foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: Int -> TCVCVCV 147foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: CInt -> TCVCVCV
diff --git a/lib/Numeric/GSL/gsl-aux.h b/lib/Numeric/GSL/gsl-aux.h
index cf22024..eee15e7 100644
--- a/lib/Numeric/GSL/gsl-aux.h
+++ b/lib/Numeric/GSL/gsl-aux.h
@@ -50,7 +50,7 @@ int fft(int code, KCVEC(a), CVEC(b));
50int integrate_qng(double f(double, void*), double a, double b, double prec, 50int integrate_qng(double f(double, void*), double a, double b, double prec,
51 double *result, double*error); 51 double *result, double*error);
52 52
53int integrate_qags(double f(double,void*), double a, double b, double prec, int w, 53int integrate_qags(double f(double,void*), double a, double b, double prec, int w,
54 double *result, double* error); 54 double *result, double* error);
55 55
56int polySolve(KRVEC(a), CVEC(z)); 56int polySolve(KRVEC(a), CVEC(z));