diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-09-11 17:34:24 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-09-11 17:34:24 +0000 |
commit | cd937c2be2900b8f13506d9ae7c731ad43d74e05 (patch) | |
tree | 8a4161c6510aac58b3ab85041145fb19ea6c5615 /lib | |
parent | 834b4837799611fd7fbaa9609ea587e041cb0ca1 (diff) |
allow setting off GSL default error handler
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Data/Packed/Internal/Common.hs | 35 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 27 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/aux.h | 2 | ||||
-rw-r--r-- | lib/GSL.hs | 7 | ||||
-rw-r--r-- | lib/GSL/Differentiation.hs | 2 | ||||
-rw-r--r-- | lib/GSL/Integration.hs | 2 | ||||
-rw-r--r-- | lib/GSL/Special.hs | 52 | ||||
-rw-r--r-- | lib/GSL/Special/Internal.hs | 10 | ||||
-rw-r--r-- | lib/GSL/gsl-aux.c | 5 | ||||
-rw-r--r-- | lib/GSL/gsl-aux.h | 9 |
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) | |||
23 | import Data.Typeable | 23 | import Data.Typeable |
24 | import Data.Maybe(fromJust) | 24 | import Data.Maybe(fromJust) |
25 | 25 | ||
26 | debug x = trace (show x) x | ||
27 | |||
28 | data Vector t = V { dim :: Int | ||
29 | , fptr :: ForeignPtr t | ||
30 | , ptr :: Ptr t | ||
31 | } -- deriving Typeable | ||
32 | |||
33 | ---------------------------------------------------------------------- | 26 | ---------------------------------------------------------------------- |
34 | instance (Storable a, RealFloat a) => Storable (Complex a) where -- | 27 | instance (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 | ||
36 | debug x = trace (show x) x | ||
37 | |||
43 | on :: (a -> a -> b) -> (t -> a) -> t -> t -> b | 38 | on :: (a -> a -> b) -> (t -> a) -> t -> t -> b |
44 | on f g = \x y -> f (g x) (g y) | 39 | on 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 | ||
58 | xor :: Bool -> Bool -> Bool | ||
59 | xor a b = a && not b || b && not a | ||
60 | |||
61 | (//) :: x -> (x -> y) -> y | 53 | (//) :: x -> (x -> y) -> y |
62 | infixl 0 // | 54 | infixl 0 // |
63 | (//) = flip ($) | 55 | (//) = flip ($) |
64 | 56 | ||
57 | -- our codes should start from 1024 | ||
58 | |||
65 | errorCode :: Int -> String | 59 | errorCode :: Int -> String |
66 | errorCode 1000 = "bad size" | 60 | errorCode 1000 = "bad size" |
67 | errorCode 1001 = "bad function code" | 61 | errorCode 1001 = "bad function code" |
@@ -71,25 +65,6 @@ errorCode 1004 = "singular" | |||
71 | errorCode 1005 = "didn't converge" | 65 | errorCode 1005 = "didn't converge" |
72 | errorCode n = "code "++show n | 66 | errorCode n = "code "++show n |
73 | 67 | ||
74 | check :: String -> [Vector a] -> IO Int -> IO () | ||
75 | check 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 | -} |
95 | foreign import ccall "wrapper" mkfun:: (Double -> Ptr() -> Double) -> IO( FunPtr (Double -> Ptr() -> Double)) | 70 | foreign 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 | |||
21 | import Control.Monad(when) | 21 | import Control.Monad(when) |
22 | import Data.List(transpose) | 22 | import Data.List(transpose) |
23 | import Debug.Trace(trace) | 23 | import Debug.Trace(trace) |
24 | import Foreign.C.String(peekCString) | ||
25 | import Foreign.C.Types | ||
26 | |||
27 | |||
28 | data Vector t = V { dim :: Int | ||
29 | , fptr :: ForeignPtr t | ||
30 | , ptr :: Ptr t | ||
31 | } | ||
32 | |||
33 | check :: String -> [Vector a] -> IO Int -> IO () | ||
34 | check 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 | |||
45 | foreign import ccall "aux.h gsl_strerror" gsl_strerror :: Int -> IO (Ptr CChar) | ||
24 | 46 | ||
25 | type Vc t s = Int -> Ptr t -> s | 47 | type 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 | |||
30 | vec :: Vector t -> (Vc t s) -> s | 52 | vec :: Vector t -> (Vc t s) -> s |
31 | vec v f = f (dim v) (ptr v) | 53 | vec v f = f (dim v) (ptr v) |
32 | 54 | ||
33 | --baseOf v = (v `at` 0) | ||
34 | |||
35 | createVector :: Storable a => Int -> IO (Vector a) | 55 | createVector :: Storable a => Int -> IO (Vector a) |
36 | createVector n = do | 56 | createVector 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 |
92 | join :: Storable t => [Vector t] -> Vector t | 110 | join :: Storable t => [Vector t] -> Vector t |
93 | join [] = error "joining zero vectors" | 111 | join [] = error "joining zero vectors" |
@@ -111,7 +129,6 @@ asReal v = V { dim = 2*dim v, fptr = castForeignPtr (fptr v), ptr = castPtr (pt | |||
111 | asComplex :: Vector Double -> Vector (Complex Double) | 129 | asComplex :: Vector Double -> Vector (Complex Double) |
112 | asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) } | 130 | asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) } |
113 | 131 | ||
114 | |||
115 | ---------------------------------------------------------------- | 132 | ---------------------------------------------------------------- |
116 | 133 | ||
117 | liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b | 134 | liftVector :: (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 | ||
25 | int diagR(KRVEC(d),RMAT(r)); | 25 | int diagR(KRVEC(d),RMAT(r)); |
26 | int diagC(KCVEC(d),CMAT(r)); | 26 | int diagC(KCVEC(d),CMAT(r)); |
27 | |||
28 | const char * gsl_strerror (const int gsl_errno); | ||
@@ -21,20 +21,20 @@ module LinearAlgebra.Algorithms, | |||
21 | module LAPACK, | 21 | module LAPACK, |
22 | module GSL.Integration, | 22 | module GSL.Integration, |
23 | module GSL.Differentiation, | 23 | module GSL.Differentiation, |
24 | --module GSL.Special, | ||
25 | module GSL.Fourier, | 24 | module GSL.Fourier, |
26 | module GSL.Polynomials, | 25 | module GSL.Polynomials, |
27 | module GSL.Minimization, | 26 | module GSL.Minimization, |
28 | module GSL.Matrix, | 27 | module GSL.Matrix, |
29 | module GSL.Compat, | 28 | module GSL.Compat, |
30 | module Data.Packed.Plot, | 29 | module Data.Packed.Plot, |
31 | module Complex | 30 | module Complex, |
31 | |||
32 | setErrorHandlerOff | ||
32 | 33 | ||
33 | ) where | 34 | ) where |
34 | 35 | ||
35 | import Data.Packed.Vector hiding (constant) | 36 | import Data.Packed.Vector hiding (constant) |
36 | import Data.Packed.Matrix hiding ((><), multiply) | 37 | import Data.Packed.Matrix hiding ((><), multiply) |
37 | --import Data.Packed.Tensor | ||
38 | import LinearAlgebra.Algorithms hiding (pnorm) | 38 | import LinearAlgebra.Algorithms hiding (pnorm) |
39 | import LAPACK | 39 | import LAPACK |
40 | import GSL.Integration | 40 | import GSL.Integration |
@@ -47,3 +47,4 @@ import GSL.Matrix | |||
47 | import GSL.Compat | 47 | import GSL.Compat |
48 | import Data.Packed.Plot | 48 | import Data.Packed.Plot |
49 | import Complex | 49 | import Complex |
50 | import 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 | ||
25 | import Foreign | 25 | import Foreign |
26 | import Data.Packed.Internal.Common(mkfun,check,(//)) | 26 | import Data.Packed.Internal(mkfun,check,(//)) |
27 | 27 | ||
28 | derivGen :: | 28 | derivGen :: |
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 | ||
23 | import Foreign | 23 | import Foreign |
24 | import Data.Packed.Internal.Common(mkfun,check,(//)) | 24 | import 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 | ) |
45 | where | 46 | where |
46 | 47 | ||
@@ -74,51 +75,6 @@ import GSL.Special.Trig | |||
74 | import GSL.Special.Zeta | 75 | import 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. | 80 | foreign import ccall "gsl-aux.h no_abort_on_error" setErrorHandlerOff :: IO () |
80 | |||
81 | @> erf 1.5 | ||
82 | 0.9661051464753108@ | ||
83 | |||
84 | -} | ||
85 | foreign 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 | -} | ||
93 | foreign 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 | -} | ||
105 | bessel_J0_e :: Double -> (Double,Double) | ||
106 | bessel_J0_e x = createSFR "bessel_J0_e" (gsl_sf_bessel_J0_e x) | ||
107 | foreign 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 | |||
113 | For example: | ||
114 | |||
115 | @\> exp_e10_e 30.0 | ||
116 | (1.0686474581524432,1.4711818964275088e-14,13)@ | ||
117 | |||
118 | @\> exp 30.0 | ||
119 | 1.0686474581524463e13@ | ||
120 | |||
121 | -} | ||
122 | exp_e10_e :: Double -> (Double,Int,Double) | ||
123 | exp_e10_e x = createSFR_E10 "exp_e10_e" (gsl_sf_exp_e10_e x) | ||
124 | foreign 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 ( | |||
25 | where | 25 | where |
26 | 26 | ||
27 | import Foreign | 27 | import Foreign |
28 | import Data.Packed.Internal.Common(check,(//)) | 28 | import Data.Packed.Internal(check,(//)) |
29 | 29 | ||
30 | 30 | ||
31 | data Precision = PrecDouble | PrecSingle | PrecApprox | 31 | data 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 |
42 | createSFR :: Storable a => t -> (Ptr a -> IO Int) -> (a, a) | 42 | createSFR :: Storable a => String -> (Ptr a -> IO Int) -> (a, a) |
43 | createSFR s f = unsafePerformIO $ do | 43 | createSFR 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 |
55 | createSFR_E10 :: (Storable t2, Storable t3, Storable t1) => t -> (Ptr a -> IO Int) -> (t1, t2, t3) | 55 | createSFR_E10 :: (Storable t2, Storable t3, Storable t1) => String -> (Ptr a -> IO Int) -> (t1, t2, t3) |
56 | createSFR_E10 s f = unsafePerformIO $ do | 56 | createSFR_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 | ||
67 | void no_abort_on_error() { | ||
68 | gsl_set_error_handler_off(); | ||
69 | } | ||
70 | |||
71 | |||
67 | int toScalarR(int code, KRVEC(x), RVEC(r)) { | 72 | int 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 | ||
13 | void no_abort_on_error(); | ||
14 | |||
13 | int toScalarR(int code, KRVEC(x), RVEC(r)); | 15 | int 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 | ||
60 | int deriv(int code, double f(double, void*), double x, double h, double * result, double * abserr); | 62 | int deriv(int code, double f(double, void*), double x, double h, double * result, double * abserr); |
61 | |||
62 | double gsl_sf_erf(double); | ||
63 | double gsl_sf_erf_Z(double); | ||
64 | double gsl_sf_gamma(double); | ||
65 | |||
66 | int gsl_sf_bessel_J0_e(double, double*); // hmmm... | ||
67 | int gsl_sf_exp_e10_e(double, double*); // HMMMMM... | ||