summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal/Vector.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-09-08 09:46:33 +0000
committerAlberto Ruiz <aruiz@um.es>2007-09-08 09:46:33 +0000
commit34380f2b5d7b048a4d68197f16a8db0e53742030 (patch)
tree444aff88cda5c247d49bac0d294d8cfb9ef7bf23 /lib/Data/Packed/Internal/Vector.hs
parent0c38c1b0e122a56ea98c494e60ba90afe2688664 (diff)
type classes
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs42
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
19import Foreign 19import Foreign
20import Complex 20import Complex
21import Control.Monad(when) 21import Control.Monad(when)
22import Data.List(transpose)
23import Debug.Trace(trace)
22 24
23type Vc t s = Int -> Ptr t -> s 25type 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
28vec :: Vector t -> (Vc t s) -> s 30vec :: Vector t -> (Vc t s) -> s
29vec v f = f (dim v) (ptr v) 31vec v f = f (dim v) (ptr v)
30 32
31baseOf v = (v `at` 0) 33--baseOf v = (v `at` 0)
32 34
33createVector :: Storable a => Int -> IO (Vector a) 35createVector :: Storable a => Int -> IO (Vector a)
34createVector n = do 36createVector 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
85infixl 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
83join :: Field t => [Vector t] -> Vector t 92join :: Storable t => [Vector t] -> Vector t
84join [] = error "joining zero vectors" 93join [] = error "joining zero vectors"
85join as = unsafePerformIO $ do 94join 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)
103asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) } 112asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v), ptr = castPtr (ptr v) }
104 113
105 114
106constantG x n = fromList (replicate n x) 115----------------------------------------------------------------
107
108constantR :: Double -> Int -> Vector Double
109constantR = constantAux cconstantR
110
111constantC :: Complex Double -> Int -> Vector (Complex Double)
112constantC = constantAux cconstantC
113
114constantAux 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
121foreign import ccall safe "aux.h constantR"
122 cconstantR :: Ptr Double -> TV -- Double :> IO Int
123
124foreign import ccall safe "aux.h constantC"
125 cconstantC :: Ptr (Complex Double) -> TCV -- Complex Double :> IO Int
126
127constant :: Field a => a -> Int -> Vector a
128constant 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
132liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b 117liftVector :: (Storable a, Storable b) => (a-> b) -> Vector a -> Vector b
133liftVector f = fromList . map f . toList 118liftVector f = fromList . map f . toList
134 119
135liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c 120liftVector2 :: (Storable a, Storable b, Storable c) => (a-> b -> c) -> Vector a -> Vector b -> Vector c
136liftVector2 f u v = fromList $ zipWith f (toList u) (toList v) 121liftVector2 f u v = fromList $ zipWith f (toList u) (toList v)
122