summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Data/Packed/Internal/Common.hs35
-rw-r--r--lib/Data/Packed/Internal/Vector.hs27
-rw-r--r--lib/Data/Packed/Internal/aux.h2
-rw-r--r--lib/GSL.hs7
-rw-r--r--lib/GSL/Differentiation.hs2
-rw-r--r--lib/GSL/Integration.hs2
-rw-r--r--lib/GSL/Special.hs52
-rw-r--r--lib/GSL/Special/Internal.hs10
-rw-r--r--lib/GSL/gsl-aux.c5
-rw-r--r--lib/GSL/gsl-aux.h9
10 files changed, 51 insertions, 100 deletions
diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs
index 1212968..5548285 100644
--- a/lib/Data/Packed/Internal/Common.hs
+++ b/lib/Data/Packed/Internal/Common.hs
@@ -1,4 +1,4 @@
1{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} 1{-# OPTIONS_GHC -fglasgow-exts #-}
2----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
3-- | 3-- |
4-- Module : Data.Packed.Internal.Common 4-- Module : Data.Packed.Internal.Common
@@ -23,13 +23,6 @@ import Data.List(transpose,intersperse)
23import Data.Typeable 23import Data.Typeable
24import Data.Maybe(fromJust) 24import Data.Maybe(fromJust)
25 25
26debug x = trace (show x) x
27
28data Vector t = V { dim :: Int
29 , fptr :: ForeignPtr t
30 , ptr :: Ptr t
31 } -- deriving Typeable
32
33---------------------------------------------------------------------- 26----------------------------------------------------------------------
34instance (Storable a, RealFloat a) => Storable (Complex a) where -- 27instance (Storable a, RealFloat a) => Storable (Complex a) where --
35 alignment x = alignment (realPart x) -- 28 alignment x = alignment (realPart x) --
@@ -40,6 +33,8 @@ instance (Storable a, RealFloat a) => Storable (Complex a) where --
40 poke p (a :+ b) = pokeArray (castPtr p) [a,b] -- 33 poke p (a :+ b) = pokeArray (castPtr p) [a,b] --
41---------------------------------------------------------------------- 34----------------------------------------------------------------------
42 35
36debug x = trace (show x) x
37
43on :: (a -> a -> b) -> (t -> a) -> t -> t -> b 38on :: (a -> a -> b) -> (t -> a) -> t -> t -> b
44on f g = \x y -> f (g x) (g y) 39on f g = \x y -> f (g x) (g y)
45 40
@@ -55,13 +50,12 @@ common f = commonval . map f where
55 commonval [a] = Just a 50 commonval [a] = Just a
56 commonval (a:b:xs) = if a==b then commonval (b:xs) else Nothing 51 commonval (a:b:xs) = if a==b then commonval (b:xs) else Nothing
57 52
58xor :: Bool -> Bool -> Bool
59xor a b = a && not b || b && not a
60
61(//) :: x -> (x -> y) -> y 53(//) :: x -> (x -> y) -> y
62infixl 0 // 54infixl 0 //
63(//) = flip ($) 55(//) = flip ($)
64 56
57-- our codes should start from 1024
58
65errorCode :: Int -> String 59errorCode :: Int -> String
66errorCode 1000 = "bad size" 60errorCode 1000 = "bad size"
67errorCode 1001 = "bad function code" 61errorCode 1001 = "bad function code"
@@ -71,25 +65,6 @@ errorCode 1004 = "singular"
71errorCode 1005 = "didn't converge" 65errorCode 1005 = "didn't converge"
72errorCode n = "code "++show n 66errorCode n = "code "++show n
73 67
74check :: String -> [Vector a] -> IO Int -> IO ()
75check msg ls f = do
76 err <- f
77 when (err/=0) (error (msg++": "++errorCode err))
78 mapM_ (touchForeignPtr . fptr) ls
79 return ()
80
81--class (Storable a, Typeable a) => Field a
82--instance (Storable a, Typeable a) => Field a
83
84--isReal :: (Data.Typeable.Typeable a) => (t -> a) -> t -> Bool
85--isReal w x = typeOf (undefined :: Double) == typeOf (w x)
86
87--isComp :: (Data.Typeable.Typeable a) => (t -> a) -> t -> Bool
88--isComp w x = typeOf (undefined :: Complex Double) == typeOf (w x)
89
90--scast :: forall a . forall b . (Typeable a, Typeable b) => a -> b
91--scast = fromJust . cast
92
93{- | conversion of Haskell functions into function pointers that can be used in the C side 68{- | conversion of Haskell functions into function pointers that can be used in the C side
94-} 69-}
95foreign import ccall "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double)) 70foreign import ccall "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double))
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index f2646a4..0d9dc70 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -21,6 +21,28 @@ import Complex
21import Control.Monad(when) 21import Control.Monad(when)
22import Data.List(transpose) 22import Data.List(transpose)
23import Debug.Trace(trace) 23import Debug.Trace(trace)
24import Foreign.C.String(peekCString)
25import Foreign.C.Types
26
27
28data Vector t = V { dim :: Int
29 , fptr :: ForeignPtr t
30 , ptr :: Ptr t
31 }
32
33check :: String -> [Vector a] -> IO Int -> IO ()
34check msg ls f = do
35 err <- f
36 when (err/=0) $ if err > 999 -- FIXME, it should be 1024
37 then (error (msg++": "++errorCode err))
38 else do
39 ps <- gsl_strerror err
40 s <- peekCString ps
41 error (msg++": "++s)
42 mapM_ (touchForeignPtr . fptr) ls
43 return ()
44
45foreign import ccall "aux.h gsl_strerror" gsl_strerror :: Int -> IO (Ptr CChar)
24 46
25type Vc t s = Int -> Ptr t -> s 47type Vc t s = Int -> Ptr t -> s
26-- not yet admitted by my haddock version 48-- not yet admitted by my haddock version
@@ -30,8 +52,6 @@ type Vc t s = Int -> Ptr t -> s
30vec :: Vector t -> (Vc t s) -> s 52vec :: Vector t -> (Vc t s) -> s
31vec v f = f (dim v) (ptr v) 53vec v f = f (dim v) (ptr v)
32 54
33--baseOf v = (v `at` 0)
34
35createVector :: Storable a => Int -> IO (Vector a) 55createVector :: Storable a => Int -> IO (Vector a)
36createVector n = do 56createVector n = do
37 when (n <= 0) $ error ("trying to createVector of dim "++show n) 57 when (n <= 0) $ error ("trying to createVector of dim "++show n)
@@ -86,8 +106,6 @@ infixl 9 @>
86(@>) = at 106(@>) = at
87 107
88 108
89
90
91-- | creates a new Vector by joining a list of Vectors 109-- | creates a new Vector by joining a list of Vectors
92join :: Storable t => [Vector t] -> Vector t 110join :: Storable t => [Vector t] -> Vector t
93join [] = error "joining zero vectors" 111join [] = error "joining zero vectors"
@@ -111,7 +129,6 @@ asReal v = V { dim = 2*dim v, fptr = castForeignPtr (fptr v), ptr = castPtr (pt
111asComplex :: Vector Double -> Vector (Complex Double) 129asComplex :: Vector Double -> Vector (Complex Double)
112asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) } 130asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) }
113 131
114
115---------------------------------------------------------------- 132----------------------------------------------------------------
116 133
117liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b 134liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b
diff --git a/lib/Data/Packed/Internal/aux.h b/lib/Data/Packed/Internal/aux.h
index d055d35..83111e5 100644
--- a/lib/Data/Packed/Internal/aux.h
+++ b/lib/Data/Packed/Internal/aux.h
@@ -24,3 +24,5 @@ int submatrixR(int r1, int r2, int c1, int c2, KRMAT(x),RMAT(r));
24 24
25int diagR(KRVEC(d),RMAT(r)); 25int diagR(KRVEC(d),RMAT(r));
26int diagC(KCVEC(d),CMAT(r)); 26int diagC(KCVEC(d),CMAT(r));
27
28const char * gsl_strerror (const int gsl_errno);
diff --git a/lib/GSL.hs b/lib/GSL.hs
index a2137d3..a42ede8 100644
--- a/lib/GSL.hs
+++ b/lib/GSL.hs
@@ -21,20 +21,20 @@ module LinearAlgebra.Algorithms,
21module LAPACK, 21module LAPACK,
22module GSL.Integration, 22module GSL.Integration,
23module GSL.Differentiation, 23module GSL.Differentiation,
24--module GSL.Special,
25module GSL.Fourier, 24module GSL.Fourier,
26module GSL.Polynomials, 25module GSL.Polynomials,
27module GSL.Minimization, 26module GSL.Minimization,
28module GSL.Matrix, 27module GSL.Matrix,
29module GSL.Compat, 28module GSL.Compat,
30module Data.Packed.Plot, 29module Data.Packed.Plot,
31module Complex 30module Complex,
31
32setErrorHandlerOff
32 33
33) where 34) where
34 35
35import Data.Packed.Vector hiding (constant) 36import Data.Packed.Vector hiding (constant)
36import Data.Packed.Matrix hiding ((><), multiply) 37import Data.Packed.Matrix hiding ((><), multiply)
37--import Data.Packed.Tensor
38import LinearAlgebra.Algorithms hiding (pnorm) 38import LinearAlgebra.Algorithms hiding (pnorm)
39import LAPACK 39import LAPACK
40import GSL.Integration 40import GSL.Integration
@@ -47,3 +47,4 @@ import GSL.Matrix
47import GSL.Compat 47import GSL.Compat
48import Data.Packed.Plot 48import Data.Packed.Plot
49import Complex 49import Complex
50import GSL.Special(setErrorHandlerOff)
diff --git a/lib/GSL/Differentiation.hs b/lib/GSL/Differentiation.hs
index 6fd7565..e8e22d2 100644
--- a/lib/GSL/Differentiation.hs
+++ b/lib/GSL/Differentiation.hs
@@ -23,7 +23,7 @@ module GSL.Differentiation (
23) where 23) where
24 24
25import Foreign 25import Foreign
26import Data.Packed.Internal.Common(mkfun,check,(//)) 26import Data.Packed.Internal(mkfun,check,(//))
27 27
28derivGen :: 28derivGen ::
29 Int -- ^ type: 0 central, 1 forward, 2 backward 29 Int -- ^ type: 0 central, 1 forward, 2 backward
diff --git a/lib/GSL/Integration.hs b/lib/GSL/Integration.hs
index dd76be8..4152c9d 100644
--- a/lib/GSL/Integration.hs
+++ b/lib/GSL/Integration.hs
@@ -21,7 +21,7 @@ module GSL.Integration (
21) where 21) where
22 22
23import Foreign 23import Foreign
24import Data.Packed.Internal.Common(mkfun,check,(//)) 24import Data.Packed.Internal(mkfun,check,(//))
25 25
26 26
27-------------------------------------------------------------------- 27--------------------------------------------------------------------
diff --git a/lib/GSL/Special.hs b/lib/GSL/Special.hs
index 9eaf733..bdee1b2 100644
--- a/lib/GSL/Special.hs
+++ b/lib/GSL/Special.hs
@@ -41,6 +41,7 @@ module GSL.Special (
41, module GSL.Special.Synchrotron 41, module GSL.Special.Synchrotron
42, module GSL.Special.Trig 42, module GSL.Special.Trig
43, module GSL.Special.Zeta 43, module GSL.Special.Zeta
44, setErrorHandlerOff
44) 45)
45where 46where
46 47
@@ -74,51 +75,6 @@ import GSL.Special.Trig
74import GSL.Special.Zeta 75import GSL.Special.Zeta
75 76
76 77
77-------------------- simple functions -------------------------- 78-- | This action removes the GSL default error handler which aborts the program, so that
78 79-- GSL errors can be handled by Haskell (using Control.Exception) and ghci doesn't abort.
79{- | The error function (/gsl_sf_erf/), defined as 2\/ \\sqrt \\pi * \int\_0\^t \\exp -t\^2 dt. 80foreign import ccall "gsl-aux.h no_abort_on_error" setErrorHandlerOff :: IO ()
80
81@> erf 1.5
820.9661051464753108@
83
84-}
85foreign import ccall "gsl-aux.h gsl_sf_erf" erf :: Double -> Double
86
87{- | The Gaussian probability density function (/gsl_sf_erf_Z/), defined as (1\/\\sqrt\{2\\pi\}) \\exp(-x\^2\/2).
88
89>> erf_Z 1.5
90>0.12951759566589172
91
92-}
93foreign import ccall "gsl-aux.h gsl_sf_erf_Z" erf_Z :: Double -> Double
94
95-------------------- functions returning sf_result -------------
96
97{- | The regular cylindrical Bessel function of zeroth order, J_0(x). This is
98 the example in the GSL manual, returning the value of the function and
99 the error term:
100
101@\> bessel_J0_e 5.0
102(-0.1775967713143383,1.9302109579684196e-16)@
103
104-}
105bessel_J0_e :: Double -> (Double,Double)
106bessel_J0_e x = createSFR "bessel_J0_e" (gsl_sf_bessel_J0_e x)
107foreign import ccall "gsl-aux.h gsl_sf_bessel_J0_e" gsl_sf_bessel_J0_e :: Double -> Ptr Double -> IO Int
108
109-------------------- functions returning sf_result_e10 -------------
110
111{- | (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\".
112
113For example:
114
115@\> exp_e10_e 30.0
116(1.0686474581524432,1.4711818964275088e-14,13)@
117
118@\> exp 30.0
1191.0686474581524463e13@
120
121-}
122exp_e10_e :: Double -> (Double,Int,Double)
123exp_e10_e x = createSFR_E10 "exp_e10_e" (gsl_sf_exp_e10_e x)
124foreign import ccall "gsl-aux.h gsl_sf_exp_e10_e" gsl_sf_exp_e10_e :: Double -> Ptr Double -> IO Int
diff --git a/lib/GSL/Special/Internal.hs b/lib/GSL/Special/Internal.hs
index c7455d9..ad1aaa2 100644
--- a/lib/GSL/Special/Internal.hs
+++ b/lib/GSL/Special/Internal.hs
@@ -25,7 +25,7 @@ module GSL.Special.Internal (
25where 25where
26 26
27import Foreign 27import Foreign
28import Data.Packed.Internal.Common(check,(//)) 28import Data.Packed.Internal(check,(//))
29 29
30 30
31data Precision = PrecDouble | PrecSingle | PrecApprox 31data Precision = PrecDouble | PrecSingle | PrecApprox
@@ -39,10 +39,10 @@ type Gsl_mode_t = Int
39 39
40---------------------------------------------------------------- 40----------------------------------------------------------------
41-- | access to a sf_result 41-- | access to a sf_result
42createSFR :: Storable a => t -> (Ptr a -> IO Int) -> (a, a) 42createSFR :: Storable a => String -> (Ptr a -> IO Int) -> (a, a)
43createSFR s f = unsafePerformIO $ do 43createSFR s f = unsafePerformIO $ do
44 p <- mallocArray 2 44 p <- mallocArray 2
45 f p // check "createSFR" [] 45 f p // check s []
46 [val,err] <- peekArray 2 p 46 [val,err] <- peekArray 2 p
47 free p 47 free p
48 return (val,err) 48 return (val,err)
@@ -52,12 +52,12 @@ createSFR s f = unsafePerformIO $ do
52-- the sf_result_e10 contains two doubles and the exponent 52-- the sf_result_e10 contains two doubles and the exponent
53 53
54-- | acces to sf_result_e10 54-- | acces to sf_result_e10
55createSFR_E10 :: (Storable t2, Storable t3, Storable t1) => t -> (Ptr a -> IO Int) -> (t1, t2, t3) 55createSFR_E10 :: (Storable t2, Storable t3, Storable t1) => String -> (Ptr a -> IO Int) -> (t1, t2, t3)
56createSFR_E10 s f = unsafePerformIO $ do 56createSFR_E10 s f = unsafePerformIO $ do
57 let sd = sizeOf (0::Double) 57 let sd = sizeOf (0::Double)
58 let si = sizeOf (0::Int) 58 let si = sizeOf (0::Int)
59 p <- mallocBytes (2*sd + si) 59 p <- mallocBytes (2*sd + si)
60 f p // check "createSFR_E10" [] 60 f p // check s []
61 val <- peekByteOff p 0 61 val <- peekByteOff p 0
62 err <- peekByteOff p sd 62 err <- peekByteOff p sd
63 expo <- peekByteOff p (2*sd) 63 expo <- peekByteOff p (2*sd)
diff --git a/lib/GSL/gsl-aux.c b/lib/GSL/gsl-aux.c
index fc95e6f..eec3e5f 100644
--- a/lib/GSL/gsl-aux.c
+++ b/lib/GSL/gsl-aux.c
@@ -64,6 +64,11 @@
64#define BAD_FILE 1003 64#define BAD_FILE 1003
65 65
66 66
67void no_abort_on_error() {
68 gsl_set_error_handler_off();
69}
70
71
67int toScalarR(int code, KRVEC(x), RVEC(r)) { 72int toScalarR(int code, KRVEC(x), RVEC(r)) {
68 REQUIRES(rn==1,BAD_SIZE); 73 REQUIRES(rn==1,BAD_SIZE);
69 DEBUGMSG("toScalarR"); 74 DEBUGMSG("toScalarR");
diff --git a/lib/GSL/gsl-aux.h b/lib/GSL/gsl-aux.h
index 2be07c1..8f8618a 100644
--- a/lib/GSL/gsl-aux.h
+++ b/lib/GSL/gsl-aux.h
@@ -10,6 +10,8 @@
10#define KCVEC(A) int A##n, const gsl_complex*A##p 10#define KCVEC(A) int A##n, const gsl_complex*A##p
11#define KCMAT(A) int A##r, int A##c, const gsl_complex* A##p 11#define KCMAT(A) int A##r, int A##c, const gsl_complex* A##p
12 12
13void no_abort_on_error();
14
13int toScalarR(int code, KRVEC(x), RVEC(r)); 15int toScalarR(int code, KRVEC(x), RVEC(r));
14/* norm2, absdif, maximum, posmax, etc. */ 16/* norm2, absdif, maximum, posmax, etc. */
15 17
@@ -58,10 +60,3 @@ int minimizeWithDeriv(double f(int, double*), void df(int, double*, double*),
58 KRVEC(xi), RMAT(sol)); 60 KRVEC(xi), RMAT(sol));
59 61
60int deriv(int code, double f(double, void*), double x, double h, double * result, double * abserr); 62int deriv(int code, double f(double, void*), double x, double h, double * result, double * abserr);
61
62double gsl_sf_erf(double);
63double gsl_sf_erf_Z(double);
64double gsl_sf_gamma(double);
65
66int gsl_sf_bessel_J0_e(double, double*); // hmmm...
67int gsl_sf_exp_e10_e(double, double*); // HMMMMM...