From 6f4137cabbc16fa616e823db3d1b2cf90c03e5c9 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Mon, 1 Feb 2010 08:41:08 +0000 Subject: internal idim irows icols --- lib/Data/Packed/Internal/Matrix.hs | 40 +++++++++++++++++++--------------- lib/Data/Packed/Internal/Vector.hs | 18 +++++++++------ lib/Numeric/LinearAlgebra/Instances.hs | 2 +- 3 files changed, 35 insertions(+), 25 deletions(-) (limited to 'lib') diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 33c9324..8bd6fb2 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs @@ -16,7 +16,7 @@ -- #hide module Data.Packed.Internal.Matrix( - Matrix(..), + Matrix(..), rows, cols, MatrixOrder(..), orderOf, createMatrix, withMatrix, mat, cmat, fmat, @@ -77,17 +77,23 @@ import Foreign.C.String data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) -- | Matrix representation suitable for GSL and LAPACK computations. -data Matrix t = MC { rows :: {-# UNPACK #-} !Int - , cols :: {-# UNPACK #-} !Int +data Matrix t = MC { irows :: {-# UNPACK #-} !Int + , icols :: {-# UNPACK #-} !Int , cdat :: {-# UNPACK #-} !(Vector t) } - | MF { rows :: {-# UNPACK #-} !Int - , cols :: {-# UNPACK #-} !Int + | MF { irows :: {-# UNPACK #-} !Int + , icols :: {-# UNPACK #-} !Int , fdat :: {-# UNPACK #-} !(Vector t) } -- MC: preferred by C, fdat may require a transposition -- MF: preferred by LAPACK, cdat may require a transposition +rows :: Matrix t -> Int +rows = irows + +cols :: Matrix t -> Int +cols = icols + xdat MC {cdat = d } = d xdat MF {fdat = d } = d @@ -97,16 +103,16 @@ orderOf MC{} = RowMajor -- | Matrix transpose. trans :: Matrix t -> Matrix t -trans MC {rows = r, cols = c, cdat = d } = MF {rows = c, cols = r, fdat = d } -trans MF {rows = r, cols = c, fdat = d } = MC {rows = c, cols = r, cdat = d } +trans MC {irows = r, icols = c, cdat = d } = MF {irows = c, icols = r, fdat = d } +trans MF {irows = r, icols = c, fdat = d } = MC {irows = c, icols = r, cdat = d } cmat :: (Element t) => Matrix t -> Matrix t cmat m@MC{} = m -cmat MF {rows = r, cols = c, fdat = d } = MC {rows = r, cols = c, cdat = transdata r d c} +cmat MF {irows = r, icols = c, fdat = d } = MC {irows = r, icols = c, cdat = transdata r d c} fmat :: (Element t) => Matrix t -> Matrix t fmat m@MF{} = m -fmat MC {rows = r, cols = c, cdat = d } = MF {rows = r, cols = c, fdat = transdata c d r} +fmat MC {irows = r, icols = c, cdat = d } = MF {irows = r, icols = c, fdat = transdata c d r} -- C-Haskell matrix adapter mat :: Adapt (CInt -> CInt -> Ptr t -> r) (Matrix t) r @@ -170,13 +176,13 @@ infixl 9 @@> -- | i<0 || i>=r || j<0 || j>=c = error "matrix indexing out of range" -- | otherwise = cdat m `at` (i*c+j) -MC {rows = r, cols = c, cdat = v} @@> (i,j) +MC {irows = r, icols = c, cdat = v} @@> (i,j) | safe = if i<0 || i>=r || j<0 || j>=c then error "matrix indexing out of range" else v `at` (i*c+j) | otherwise = v `at` (i*c+j) -MF {rows = r, cols = c, fdat = v} @@> (i,j) +MF {irows = r, icols = c, fdat = v} @@> (i,j) | safe = if i<0 || i>=r || j<0 || j>=c then error "matrix indexing out of range" else v `at` (j*r+i) @@ -184,18 +190,18 @@ MF {rows = r, cols = c, fdat = v} @@> (i,j) {-# INLINE (@@>) #-} -- Unsafe matrix access without range checking -atM' MC {cols = c, cdat = v} i j = v `at'` (i*c+j) -atM' MF {rows = r, fdat = v} i j = v `at'` (j*r+i) +atM' MC {icols = c, cdat = v} i j = v `at'` (i*c+j) +atM' MF {irows = r, fdat = v} i j = v `at'` (j*r+i) {-# INLINE atM' #-} ------------------------------------------------------------------ -matrixFromVector RowMajor c v = MC { rows = r, cols = c, cdat = v } +matrixFromVector RowMajor c v = MC { irows = r, icols = c, cdat = v } where (d,m) = dim v `divMod` c r | m==0 = d | otherwise = error "matrixFromVector" -matrixFromVector ColumnMajor c v = MF { rows = r, cols = c, fdat = v } +matrixFromVector ColumnMajor c v = MF { irows = r, icols = c, fdat = v } where (d,m) = dim v `divMod` c r | m==0 = d | otherwise = error "matrixFromVector" @@ -223,8 +229,8 @@ singleton x = reshape 1 (fromList [x]) -- | application of a vector function on the flattened matrix elements liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b -liftMatrix f MC { cols = c, cdat = d } = matrixFromVector RowMajor c (f d) -liftMatrix f MF { cols = c, fdat = d } = matrixFromVector ColumnMajor c (f d) +liftMatrix f MC { icols = c, cdat = d } = matrixFromVector RowMajor c (f d) +liftMatrix f MF { icols = c, fdat = d } = matrixFromVector ColumnMajor c (f d) -- | application of a vector function on the flattened matrices elements liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index bc623de..66acd87 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs @@ -15,7 +15,7 @@ -- #hide module Data.Packed.Internal.Vector ( - Vector(..), + Vector(..), dim, fromList, toList, (|>), join, (@>), safe, at, at', subVector, mapVector, zipVector, @@ -47,10 +47,14 @@ import GHC.IOBase -- | A one-dimensional array of objects stored in a contiguous memory block. data Vector t = - V { dim :: {-# UNPACK #-} !Int -- ^ number of elements + V { idim :: {-# UNPACK #-} !Int -- ^ number of elements , fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block } +-- | Number of elements +dim :: Vector t -> Int +dim = idim + -- C-Haskell vector adapter vec :: Adapt (CInt -> Ptr t -> r) (Vector t) r vec = withVector @@ -157,7 +161,7 @@ subVector :: Storable t => Int -- ^ index of the starting element -> Int -- ^ number of elements to extract -> Vector t -- ^ source -> Vector t -- ^ result -subVector k l (v@V {dim=n}) +subVector k l (v@V {idim=n}) | k<0 || k >= n || k+l > n || l < 0 = error "subVector out of range" | otherwise = unsafePerformIO $ do r <- createVector l @@ -192,23 +196,23 @@ join as = unsafePerformIO $ do joiner as tot ptr return r where joiner [] _ _ = return () - joiner (V {dim = n, fptr = b} : cs) _ p = do + joiner (V {idim = n, fptr = b} : cs) _ p = do withForeignPtr b $ \pb -> copyArray p pb n joiner cs 0 (advancePtr p n) -- | transforms a complex vector into a real vector with alternating real and imaginary parts asReal :: Vector (Complex Double) -> Vector Double -asReal v = V { dim = 2*dim v, fptr = castForeignPtr (fptr v) } +asReal v = V { idim = 2*dim v, fptr = castForeignPtr (fptr v) } -- | transforms a real vector into a complex vector with alternating real and imaginary parts asComplex :: Vector Double -> Vector (Complex Double) -asComplex v = V { dim = dim v `div` 2, fptr = castForeignPtr (fptr v) } +asComplex v = V { idim = dim v `div` 2, fptr = castForeignPtr (fptr v) } ---------------------------------------------------------------- cloneVector :: Storable t => Vector t -> IO (Vector t) -cloneVector (v@V {dim=n}) = do +cloneVector (v@V {idim=n}) = do r <- createVector n let f _ s _ d = copyArray d s n >> return 0 app2 f vec v vec r "cloneVector" diff --git a/lib/Numeric/LinearAlgebra/Instances.hs b/lib/Numeric/LinearAlgebra/Instances.hs index ffc4f17..1f8b5a0 100644 --- a/lib/Numeric/LinearAlgebra/Instances.hs +++ b/lib/Numeric/LinearAlgebra/Instances.hs @@ -203,7 +203,7 @@ instance (Linear Vector a, Floating (Vector a), Fractional (Matrix a)) => Floati --------------------------------------------------------------- instance (Storable a, Num (Vector a)) => Monoid (Vector a) where - mempty = 0 { dim = 0 } + mempty = 0 { idim = 0 } mappend a b = mconcat [a,b] mconcat = j . filter ((>0).dim) where j [] = mempty -- cgit v1.2.3