diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-06-13 16:35:02 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-06-13 16:35:02 +0000 |
commit | 713d4056abb2e786b4084e7e220d359b06dcaf1f (patch) | |
tree | 4535b87da8756ca89f07e46181891df2b90406dc /lib/Data/Packed/Internal/Vector.hs | |
parent | 8c5be977858723aac8b8f47f57ce98d82fe249b4 (diff) |
refactoring
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 62 |
1 files changed, 5 insertions, 57 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 4836bdb..125df1e 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.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.Vector | 4 | -- Module : Data.Packed.Internal.Vector |
@@ -9,70 +9,16 @@ | |||
9 | -- Stability : provisional | 9 | -- Stability : provisional |
10 | -- Portability : portable (uses FFI) | 10 | -- Portability : portable (uses FFI) |
11 | -- | 11 | -- |
12 | -- Fundamental types | 12 | -- Vector implementation |
13 | -- | 13 | -- |
14 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
15 | 15 | ||
16 | module Data.Packed.Internal.Vector where | 16 | module Data.Packed.Internal.Vector where |
17 | 17 | ||
18 | import Data.Packed.Internal.Common | ||
18 | import Foreign | 19 | import Foreign |
19 | import Complex | 20 | import Complex |
20 | import Control.Monad(when) | 21 | import Control.Monad(when) |
21 | import Debug.Trace | ||
22 | import Data.List(transpose,intersperse) | ||
23 | import Data.Typeable | ||
24 | import Data.Maybe(fromJust) | ||
25 | |||
26 | debug x = trace (show x) x | ||
27 | |||
28 | ---------------------------------------------------------------------- | ||
29 | instance (Storable a, RealFloat a) => Storable (Complex a) where -- | ||
30 | alignment x = alignment (realPart x) -- | ||
31 | sizeOf x = 2 * sizeOf (realPart x) -- | ||
32 | peek p = do -- | ||
33 | [re,im] <- peekArray 2 (castPtr p) -- | ||
34 | return (re :+ im) -- | ||
35 | poke p (a :+ b) = pokeArray (castPtr p) [a,b] -- | ||
36 | ---------------------------------------------------------------------- | ||
37 | |||
38 | on f g = \x y -> f (g x) (g y) | ||
39 | |||
40 | (//) :: x -> (x -> y) -> y | ||
41 | infixl 0 // | ||
42 | (//) = flip ($) | ||
43 | |||
44 | errorCode 1000 = "bad size" | ||
45 | errorCode 1001 = "bad function code" | ||
46 | errorCode 1002 = "memory problem" | ||
47 | errorCode 1003 = "bad file" | ||
48 | errorCode 1004 = "singular" | ||
49 | errorCode 1005 = "didn't converge" | ||
50 | errorCode n = "code "++show n | ||
51 | |||
52 | check msg ls f = do | ||
53 | err <- f | ||
54 | when (err/=0) (error (msg++": "++errorCode err)) | ||
55 | mapM_ (touchForeignPtr . fptr) ls | ||
56 | return () | ||
57 | |||
58 | class (Storable a, Typeable a) => Field a where | ||
59 | instance (Storable a, Typeable a) => Field a where | ||
60 | |||
61 | isReal w x = typeOf (undefined :: Double) == typeOf (w x) | ||
62 | isComp w x = typeOf (undefined :: Complex Double) == typeOf (w x) | ||
63 | baseOf v = (v `at` 0) | ||
64 | |||
65 | scast :: forall a . forall b . (Typeable a, Typeable b) => a -> b | ||
66 | scast = fromJust . cast | ||
67 | |||
68 | |||
69 | |||
70 | ---------------------------------------------------------------------- | ||
71 | |||
72 | data Vector t = V { dim :: Int | ||
73 | , fptr :: ForeignPtr t | ||
74 | , ptr :: Ptr t | ||
75 | } deriving Typeable | ||
76 | 22 | ||
77 | type Vc t s = Int -> Ptr t -> s | 23 | type Vc t s = Int -> Ptr t -> s |
78 | infixr 5 :> | 24 | infixr 5 :> |
@@ -81,6 +27,8 @@ type t :> s = Vc t s | |||
81 | vec :: Vector t -> (Vc t s) -> s | 27 | vec :: Vector t -> (Vc t s) -> s |
82 | vec v f = f (dim v) (ptr v) | 28 | vec v f = f (dim v) (ptr v) |
83 | 29 | ||
30 | baseOf v = (v `at` 0) | ||
31 | |||
84 | createVector :: Storable a => Int -> IO (Vector a) | 32 | createVector :: Storable a => Int -> IO (Vector a) |
85 | createVector n = do | 33 | createVector n = do |
86 | when (n <= 0) $ error ("trying to createVector of dim "++show n) | 34 | when (n <= 0) $ error ("trying to createVector of dim "++show n) |