diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-09-08 09:46:33 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-09-08 09:46:33 +0000 |
commit | 34380f2b5d7b048a4d68197f16a8db0e53742030 (patch) | |
tree | 444aff88cda5c247d49bac0d294d8cfb9ef7bf23 /lib/Data/Packed/Internal/Vector.hs | |
parent | 0c38c1b0e122a56ea98c494e60ba90afe2688664 (diff) |
type classes
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 42 |
1 files changed, 14 insertions, 28 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index ab93577..f2646a4 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | 1 | {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} |
2 | ----------------------------------------------------------------------------- | 2 | ----------------------------------------------------------------------------- |
3 | -- | | 3 | -- | |
4 | -- Module : Data.Packed.Internal.Vector | 4 | -- Module : Data.Packed.Internal.Vector |
@@ -19,6 +19,8 @@ import Data.Packed.Internal.Common | |||
19 | import Foreign | 19 | import Foreign |
20 | import Complex | 20 | import Complex |
21 | import Control.Monad(when) | 21 | import Control.Monad(when) |
22 | import Data.List(transpose) | ||
23 | import Debug.Trace(trace) | ||
22 | 24 | ||
23 | type Vc t s = Int -> Ptr t -> s | 25 | type Vc t s = Int -> Ptr t -> s |
24 | -- not yet admitted by my haddock version | 26 | -- not yet admitted by my haddock version |
@@ -28,7 +30,7 @@ type Vc t s = Int -> Ptr t -> s | |||
28 | vec :: Vector t -> (Vc t s) -> s | 30 | vec :: Vector t -> (Vc t s) -> s |
29 | vec v f = f (dim v) (ptr v) | 31 | vec v f = f (dim v) (ptr v) |
30 | 32 | ||
31 | baseOf v = (v `at` 0) | 33 | --baseOf v = (v `at` 0) |
32 | 34 | ||
33 | createVector :: Storable a => Int -> IO (Vector a) | 35 | createVector :: Storable a => Int -> IO (Vector a) |
34 | createVector n = do | 36 | createVector n = do |
@@ -78,9 +80,16 @@ subVector' k l (v@V {dim=n, ptr=p, fptr=fp}) | |||
78 | | otherwise = v {dim=l, ptr=advancePtr p k} | 80 | | otherwise = v {dim=l, ptr=advancePtr p k} |
79 | 81 | ||
80 | 82 | ||
83 | -- | Reads a vector position. | ||
84 | (@>) :: Storable t => Vector t -> Int -> t | ||
85 | infixl 9 @> | ||
86 | (@>) = at | ||
87 | |||
88 | |||
89 | |||
81 | 90 | ||
82 | -- | creates a new Vector by joining a list of Vectors | 91 | -- | creates a new Vector by joining a list of Vectors |
83 | join :: Field t => [Vector t] -> Vector t | 92 | join :: Storable t => [Vector t] -> Vector t |
84 | join [] = error "joining zero vectors" | 93 | join [] = error "joining zero vectors" |
85 | join as = unsafePerformIO $ do | 94 | join as = unsafePerformIO $ do |
86 | let tot = sum (map dim as) | 95 | let tot = sum (map dim as) |
@@ -103,34 +112,11 @@ asComplex :: Vector Double -> Vector (Complex Double) | |||
103 | asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) } | 112 | asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) } |
104 | 113 | ||
105 | 114 | ||
106 | constantG x n = fromList (replicate n x) | 115 | ---------------------------------------------------------------- |
107 | |||
108 | constantR :: Double -> Int -> Vector Double | ||
109 | constantR = constantAux cconstantR | ||
110 | |||
111 | constantC :: Complex Double -> Int -> Vector (Complex Double) | ||
112 | constantC = constantAux cconstantC | ||
113 | |||
114 | constantAux fun x n = unsafePerformIO $ do | ||
115 | v <- createVector n | ||
116 | px <- newArray [x] | ||
117 | fun px // vec v // check "constantAux" [] | ||
118 | free px | ||
119 | return v | ||
120 | |||
121 | foreign import ccall safe "aux.h constantR" | ||
122 | cconstantR :: Ptr Double -> TV -- Double :> IO Int | ||
123 | |||
124 | foreign import ccall safe "aux.h constantC" | ||
125 | cconstantC :: Ptr (Complex Double) -> TCV -- Complex Double :> IO Int | ||
126 | |||
127 | constant :: Field a => a -> Int -> Vector a | ||
128 | constant x n | isReal id x = scast $ constantR (scast x) n | ||
129 | | isComp id x = scast $ constantC (scast x) n | ||
130 | | otherwise = constantG x n | ||
131 | 116 | ||
132 | liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b | 117 | liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b |
133 | liftVector f = fromList . map f . toList | 118 | liftVector f = fromList . map f . toList |
134 | 119 | ||
135 | liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c | 120 | liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c |
136 | liftVector2 f u v = fromList $ zipWith f (toList u) (toList v) | 121 | liftVector2 f u v = fromList $ zipWith f (toList u) (toList v) |
122 | |||