From 1bdc3af98f098f45e97e9c1a3225c5dcb64e3119 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Tue, 22 Jan 2008 10:08:07 +0000 Subject: CInt in GSL args --- lib/Numeric/GSL/Differentiation.hs | 10 +++++++--- lib/Numeric/GSL/Fourier.hs | 3 ++- lib/Numeric/GSL/Integration.hs | 11 +++++++---- lib/Numeric/GSL/Minimization.hs | 31 ++++++++++++++++--------------- lib/Numeric/GSL/Vector.hs | 31 +++++++++++++++++-------------- lib/Numeric/GSL/gsl-aux.h | 2 +- 6 files changed, 50 insertions(+), 38 deletions(-) (limited to 'lib/Numeric') 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 ( import Foreign import Foreign.C.Types(CInt) -import Data.Packed.Internal(mkfun,check,(//)) +import Data.Packed.Internal(check,(//)) derivGen :: - Int -- ^ type: 0 central, 1 forward, 2 backward + CInt -- ^ type: 0 central, 1 forward, 2 backward -> Double -- ^ initial step size -> (Double -> Double) -- ^ function -> Double -- ^ point where the derivative is taken @@ -46,7 +46,7 @@ derivGen c h f x = unsafePerformIO $ do return result foreign import ccall "gsl-aux.h deriv" - c_deriv :: Int -> FunPtr (Double -> Ptr () -> Double) -> Double -> Double + c_deriv :: CInt -> FunPtr (Double -> Ptr () -> Double) -> Double -> Double -> Ptr Double -> Ptr Double -> IO CInt @@ -78,3 +78,7 @@ derivBackward ::Double -- ^ initial step size -> Double -- ^ point where the derivative is taken -> (Double, Double) -- ^ result and absolute error derivBackward = derivGen 2 + +{- | conversion of Haskell functions into function pointers that can be used in the C side +-} +foreign 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 ( import Data.Packed.Internal import Complex import Foreign +import Foreign.C.Types(CInt) genfft code v = unsafePerformIO $ do r <- createVector (dim v) app2 (c_fft code) vec v vec r "fft" return r -foreign import ccall "gsl-aux.h fft" c_fft :: Int -> TCVCV +foreign import ccall "gsl-aux.h fft" c_fft :: CInt -> TCVCV {- | 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 ( import Foreign import Foreign.C.Types(CInt) -import Data.Packed.Internal(mkfun,check,(//)) +import Data.Packed.Internal(check,(//)) +{- | conversion of Haskell functions into function pointers that can be used in the C side +-} +foreign import ccall "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double)) + -------------------------------------------------------------------- {- | Numerical integration using /gsl_integration_qags/ (adaptive integration with singularities). For example: @@ -45,7 +49,7 @@ integrateQAGS prec n f a b = unsafePerformIO $ do r <- malloc e <- malloc fp <- mkfun (\x _ -> f x) - c_integrate_qags fp a b prec n r e // check "integrate_qags" + c_integrate_qags fp a b prec (fromIntegral n) r e // check "integrate_qags" vr <- peek r ve <- peek e let result = (vr,ve) @@ -55,7 +59,7 @@ integrateQAGS prec n f a b = unsafePerformIO $ do return result foreign import ccall "gsl-aux.h integrate_qags" - c_integrate_qags :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double -> Double -> Int + c_integrate_qags :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double -> Double -> CInt -> Ptr Double -> Ptr Double -> IO CInt ----------------------------------------------------------------- @@ -88,4 +92,3 @@ integrateQNG prec f a b = unsafePerformIO $ do foreign import ccall "gsl-aux.h integrate_qng" c_integrate_qng :: FunPtr (Double-> Ptr() -> Double) -> Double -> Double -> Double -> Ptr Double -> Ptr Double -> IO CInt - 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 ( import Data.Packed.Internal import Data.Packed.Matrix import Foreign +import Foreign.C.Types(CInt) ------------------------------------------------------------------------- @@ -85,7 +86,7 @@ minimizeNMSimplex f xi sz tol maxit = unsafePerformIO $ do fp <- mkVecfun (iv (f.toList)) rawpath <- ww2 withVector xiv withVector szv $ \xiv' szv' -> createMIO maxit (n+3) - (c_minimizeNMSimplex fp tol maxit // xiv' // szv') + (c_minimizeNMSimplex fp tol (fromIntegral maxit) // xiv' // szv') "minimizeNMSimplex" let it = round (rawpath @@> (maxit-1,0)) path = takeRows it rawpath @@ -95,7 +96,7 @@ minimizeNMSimplex f xi sz tol maxit = unsafePerformIO $ do foreign import ccall "gsl-aux.h minimize" - c_minimizeNMSimplex:: FunPtr (Int -> Ptr Double -> Double) -> Double -> Int + c_minimizeNMSimplex:: FunPtr (CInt -> Ptr Double -> Double) -> Double -> CInt -> TVVM ---------------------------------------------------------------------------------- @@ -151,7 +152,7 @@ minimizeConjugateGradient istep minimpar tol maxit f df xi = unsafePerformIO $ d dfp <- mkVecVecfun (aux_vTov df') rawpath <- withVector xiv $ \xiv' -> createMIO maxit (n+2) - (c_minimizeConjugateGradient fp dfp istep minimpar tol maxit // xiv') + (c_minimizeConjugateGradient fp dfp istep minimpar tol (fromIntegral maxit) // xiv') "minimizeDerivV" let it = round (rawpath @@> (maxit-1,0)) path = takeRows it rawpath @@ -162,36 +163,36 @@ minimizeConjugateGradient istep minimpar tol maxit f df xi = unsafePerformIO $ d foreign import ccall "gsl-aux.h minimizeWithDeriv" - c_minimizeConjugateGradient :: FunPtr (Int -> Ptr Double -> Double) - -> FunPtr (Int -> Ptr Double -> Ptr Double -> IO ()) - -> Double -> Double -> Double -> Int + c_minimizeConjugateGradient :: FunPtr (CInt -> Ptr Double -> Double) + -> FunPtr (CInt -> Ptr Double -> Ptr Double -> IO ()) + -> Double -> Double -> Double -> CInt -> TVM --------------------------------------------------------------------- -iv :: (Vector Double -> Double) -> (Int -> Ptr Double -> Double) -iv f n p = f (createV n copy "iv") where +iv :: (Vector Double -> Double) -> (CInt -> Ptr Double -> Double) +iv f n p = f (createV (fromIntegral n) copy "iv") where copy n' q = do copyArray q p n' return 0 -- | conversion of Haskell functions into function pointers that can be used in the C side foreign import ccall "wrapper" - mkVecfun :: (Int -> Ptr Double -> Double) - -> IO( FunPtr (Int -> Ptr Double -> Double)) + mkVecfun :: (CInt -> Ptr Double -> Double) + -> IO( FunPtr (CInt -> Ptr Double -> Double)) -- | another required conversion foreign import ccall "wrapper" - mkVecVecfun :: (Int -> Ptr Double -> Ptr Double -> IO ()) - -> IO (FunPtr (Int -> Ptr Double -> Ptr Double->IO())) + mkVecVecfun :: (CInt -> Ptr Double -> Ptr Double -> IO ()) + -> IO (FunPtr (CInt -> Ptr Double -> Ptr Double->IO())) -aux_vTov :: (Vector Double -> Vector Double) -> (Int -> Ptr Double -> Ptr Double -> IO()) +aux_vTov :: (Vector Double -> Vector Double) -> (CInt -> Ptr Double -> Ptr Double -> IO()) aux_vTov f n p r = g where V {fptr = pr} = f x - x = createV n copy "aux_vTov" + x = createV (fromIntegral n) copy "aux_vTov" copy n' q = do copyArray q p n' return 0 - g = withForeignPtr pr $ \p' -> copyArray r p' n + g = withForeignPtr pr $ \p' -> copyArray r p' (fromIntegral n) -------------------------------------------------------------------- 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 import Complex import Foreign +import Foreign.C.Types(CInt) + +fromei = fromIntegral . fromei data FunCodeV = Sin | Cos @@ -73,33 +76,33 @@ data FunCodeS = Norm2 toScalarAux fun code v = unsafePerformIO $ do r <- createVector 1 - app2 (fun (fromEnum code)) vec v vec r "toScalarAux" + app2 (fun (fromei code)) vec v vec r "toScalarAux" return (r `at` 0) vectorMapAux fun code v = unsafePerformIO $ do r <- createVector (dim v) - app2 (fun (fromEnum code)) vec v vec r "vectorMapAux" + app2 (fun (fromei code)) vec v vec r "vectorMapAux" return r vectorMapValAux fun code val v = unsafePerformIO $ do r <- createVector (dim v) pval <- newArray [val] - app2 (fun (fromEnum code) pval) vec v vec r "vectorMapValAux" + app2 (fun (fromei code) pval) vec v vec r "vectorMapValAux" free pval return r vectorZipAux fun code u v = unsafePerformIO $ do r <- createVector (dim u) - app3 (fun (fromEnum code)) vec u vec v vec r "vectorZipAux" + app3 (fun (fromei code)) vec u vec v vec r "vectorZipAux" return r --------------------------------------------------------------------- -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. toScalarR :: FunCodeS -> Vector Double -> Double -toScalarR oper = toScalarAux c_toScalarR (fromEnum oper) +toScalarR oper = toScalarAux c_toScalarR (fromei oper) -foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: Int -> TVV +foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: CInt -> TVV ------------------------------------------------------------------ @@ -107,27 +110,27 @@ foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: Int -> TVV vectorMapR :: FunCodeV -> Vector Double -> Vector Double vectorMapR = vectorMapAux c_vectorMapR -foreign import ccall safe "gsl-aux.h mapR" c_vectorMapR :: Int -> TVV +foreign import ccall safe "gsl-aux.h mapR" c_vectorMapR :: CInt -> TVV -- | map of complex vectors with given function vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double) -vectorMapC oper = vectorMapAux c_vectorMapC (fromEnum oper) +vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper) -foreign import ccall safe "gsl-aux.h mapC" c_vectorMapC :: Int -> TCVCV +foreign import ccall safe "gsl-aux.h mapC" c_vectorMapC :: CInt -> TCVCV ------------------------------------------------------------------- -- | map of real vectors with given function vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double -vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromEnum oper) +vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromei oper) -foreign import ccall safe "gsl-aux.h mapValR" c_vectorMapValR :: Int -> Ptr Double -> TVV +foreign import ccall safe "gsl-aux.h mapValR" c_vectorMapValR :: CInt -> Ptr Double -> TVV -- | map of complex vectors with given function vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double) vectorMapValC = vectorMapValAux c_vectorMapValC -foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: Int -> Ptr (Complex Double) -> TCVCV +foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV ------------------------------------------------------------------- @@ -135,10 +138,10 @@ foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: Int -> Ptr (Com vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double vectorZipR = vectorZipAux c_vectorZipR -foreign import ccall safe "gsl-aux.h zipR" c_vectorZipR :: Int -> TVVV +foreign import ccall safe "gsl-aux.h zipR" c_vectorZipR :: CInt -> TVVV -- | elementwise operation on complex vectors vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double) vectorZipC = vectorZipAux c_vectorZipC -foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: Int -> TCVCVCV +foreign 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)); int integrate_qng(double f(double, void*), double a, double b, double prec, double *result, double*error); -int integrate_qags(double f(double,void*), double a, double b, double prec, int w, +int integrate_qags(double f(double,void*), double a, double b, double prec, int w, double *result, double* error); int polySolve(KRVEC(a), CVEC(z)); -- cgit v1.2.3