From db223fb5f9cd4adef54736812f796b48ecc289e6 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Wed, 31 Oct 2007 13:36:37 +0000 Subject: Field->Element, GenMat->Field --- lib/Data/Packed.hs | 2 +- lib/Data/Packed/Internal/Matrix.hs | 36 +++++++++++++++---------------- lib/Data/Packed/Matrix.hs | 44 +++++++++++++++++++------------------- 3 files changed, 41 insertions(+), 41 deletions(-) (limited to 'lib/Data') diff --git a/lib/Data/Packed.hs b/lib/Data/Packed.hs index 668d2f7..53aced9 100644 --- a/lib/Data/Packed.hs +++ b/lib/Data/Packed.hs @@ -27,7 +27,7 @@ import Data.Complex import Data.Packed.Internal -- | conversion utilities -class (Field e) => Container c e where +class (Element e) => Container c e where toComplex :: RealFloat e => (c e, c e) -> c (Complex e) fromComplex :: RealFloat e => c (Complex e) -> (c e, c e) comp :: RealFloat e => c e -> c (Complex e) diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index f63ee52..fbab33c 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs @@ -84,17 +84,17 @@ type Mt t s = Int -> Int -> Ptr t -> s -- type t ::> s = Mt t s -- | the inverse of 'Data.Packed.Matrix.fromLists' -toLists :: (Field t) => Matrix t -> [[t]] +toLists :: (Element t) => Matrix t -> [[t]] toLists m = partit (cols m) . toList . cdat $ m -- | creates a Matrix from a list of vectors -fromRows :: Field t => [Vector t] -> Matrix t +fromRows :: Element t => [Vector t] -> Matrix t fromRows vs = case common dim vs of Nothing -> error "fromRows applied to [] or to vectors with different sizes" Just c -> reshape c (join vs) -- | extracts the rows of a matrix as a list of vectors -toRows :: Field t => Matrix t -> [Vector t] +toRows :: Element t => Matrix t -> [Vector t] toRows m = toRows' 0 where v = cdat m r = rows m @@ -103,11 +103,11 @@ toRows m = toRows' 0 where | otherwise = subVector k c v : toRows' (k+c) -- | Creates a matrix from a list of vectors, as columns -fromColumns :: Field t => [Vector t] -> Matrix t +fromColumns :: Element t => [Vector t] -> Matrix t fromColumns m = trans . fromRows $ m -- | Creates a list of vectors from the columns of a matrix -toColumns :: Field t => Matrix t -> [Vector t] +toColumns :: Element t => Matrix t -> [Vector t] toColumns m = toRows . trans $ m @@ -152,18 +152,18 @@ where r is the desired number of rows.) , 9.0, 10.0, 11.0, 12.0 ]@ -} -reshape :: Field t => Int -> Vector t -> Matrix t +reshape :: Element t => Int -> Vector t -> Matrix t reshape c v = matrixFromVector RowMajor c v singleton x = reshape 1 (fromList [x]) -- | application of a vector function on the flattened matrix elements -liftMatrix :: (Field a, Field b) => (Vector a -> Vector b) -> Matrix a -> Matrix b +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) -- | application of a vector function on the flattened matrices elements -liftMatrix2 :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t +liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t liftMatrix2 f m1 m2 | not (compat m1 m2) = error "nonconformant matrices in liftMatrix2" | otherwise = case m1 of @@ -176,8 +176,8 @@ compat m1 m2 = rows m1 == rows m2 && cols m1 == cols m2 ---------------------------------------------------------------- --- | Optimized matrix computations are provided for elements in the Field class. -class (Storable a, Floating a) => Field a where +-- | Optimized matrix computations are provided for elements in the Element class. +class (Storable a, Floating a) => Element a where constantD :: a -> Int -> Vector a transdata :: Int -> Vector a -> Int -> Vector a multiplyD :: Matrix a -> Matrix a -> Matrix a @@ -186,14 +186,14 @@ class (Storable a, Floating a) => Field a where -> Matrix a -> Matrix a diagD :: Vector a -> Matrix a -instance Field Double where +instance Element Double where constantD = constantR transdata = transdataR multiplyD = multiplyR subMatrixD = subMatrixR diagD = diagR -instance Field (Complex Double) where +instance Element (Complex Double) where constantD = constantC transdata = transdataC multiplyD = multiplyC @@ -202,7 +202,7 @@ instance Field (Complex Double) where ------------------------------------------------------------------ -(>|<) :: (Field a) => Int -> Int -> [a] -> Matrix a +(>|<) :: (Element a) => Int -> Int -> [a] -> Matrix a r >|< c = f where f l | dim v == r*c = matrixFromVector ColumnMajor c v | otherwise = error $ "inconsistent list size = " @@ -260,13 +260,13 @@ foreign import ccall safe "auxi.h multiplyC" -> Int -> Int -> Ptr (Complex Double) -> IO Int -multiply' :: (Field a) => MatrixOrder -> Matrix a -> Matrix a -> Matrix a +multiply' :: (Element a) => MatrixOrder -> Matrix a -> Matrix a -> Matrix a multiply' RowMajor a b = multiplyD a b multiply' ColumnMajor a b = trans $ multiplyD (trans b) (trans a) -- | matrix product -multiply :: (Field a) => Matrix a -> Matrix a -> Matrix a +multiply :: (Element a) => Matrix a -> Matrix a -> Matrix a multiply = multiplyD ---------------------------------------------------------------------- @@ -287,7 +287,7 @@ subMatrixC (r0,c0) (rt,ct) x = reshape (2*cols x) . asReal . cdat $ x -- | Extracts a submatrix from a matrix. -subMatrix :: Field a +subMatrix :: Element a => (Int,Int) -- ^ (r0,c0) starting position -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix -> Matrix a -- ^ input matrix @@ -313,7 +313,7 @@ diagC = diagAux c_diagC "diagC" foreign import ccall "auxi.h diagC" c_diagC :: TCVCM -- | creates a square matrix with the given diagonal -diag :: Field a => Vector a -> Matrix a +diag :: Element a => Vector a -> Matrix a diag = diagD ------------------------------------------------------------------------ @@ -340,7 +340,7 @@ foreign import ccall safe "auxi.h constantC" @> constant 2 7 7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ -} -constant :: Field a => a -> Int -> Vector a +constant :: Element a => a -> Int -> Vector a constant = constantD -------------------------------------------------------------------------- diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index a705975..e96500f 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -14,7 +14,7 @@ ----------------------------------------------------------------------------- module Data.Packed.Matrix ( - Field, + Element, Matrix,rows,cols, (><), trans, @@ -41,13 +41,13 @@ import Data.List(transpose,intersperse) import Data.Array -- | creates a matrix from a vertical list of matrices -joinVert :: Field t => [Matrix t] -> Matrix t +joinVert :: Element t => [Matrix t] -> Matrix t joinVert ms = case common cols ms of Nothing -> error "joinVert on matrices with different number of columns" Just c -> reshape c $ join (map cdat ms) -- | creates a matrix from a horizontal list of matrices -joinHoriz :: Field t => [Matrix t] -> Matrix t +joinHoriz :: Element t => [Matrix t] -> Matrix t joinHoriz ms = trans. joinVert . map trans $ ms {- | Creates a matrix from blocks given as a list of lists of matrices: @@ -63,15 +63,15 @@ joinHoriz ms = trans. joinVert . map trans $ ms , -1.0, -1.0, -1.0, -1.0, 0.0, 7.0, 0.0 , -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 2.0 ]@ -} -fromBlocks :: Field t => [[Matrix t]] -> Matrix t +fromBlocks :: Element t => [[Matrix t]] -> Matrix t fromBlocks = joinVert . map joinHoriz -- | Reverse rows -flipud :: Field t => Matrix t -> Matrix t +flipud :: Element t => Matrix t -> Matrix t flipud m = fromRows . reverse . toRows $ m -- | Reverse columns -fliprl :: Field t => Matrix t -> Matrix t +fliprl :: Element t => Matrix t -> Matrix t fliprl m = fromColumns . reverse . toColumns $ m ------------------------------------------------------------ @@ -84,7 +84,7 @@ fliprl m = fromColumns . reverse . toColumns $ m , 0.0, 5.0, 0.0, 0.0 , 0.0, 0.0, 5.0, 0.0 ]@ -} -diagRect :: (Field t, Num t) => Vector t -> Int -> Int -> Matrix t +diagRect :: (Element t, Num t) => Vector t -> Int -> Int -> Matrix t diagRect s r c | dim s < min r c = error "diagRect" | r == c = diag s @@ -93,11 +93,11 @@ diagRect s r c where zeros (r,c) = reshape c $ constantD 0 (r*c) -- | extracts the diagonal from a rectangular matrix -takeDiag :: (Field t) => Matrix t -> Vector t +takeDiag :: (Element t) => Matrix t -> Vector t takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] -- | creates the identity matrix of given dimension -ident :: Field a => Int -> Matrix a +ident :: Element a => Int -> Matrix a ident n = diag (constant 1 n) ------------------------------------------------------------ @@ -112,7 +112,7 @@ ident n = diag (constant 1 n) This is the format produced by the instances of Show (Matrix a), which can also be used for input. -} -(><) :: (Field a) => Int -> Int -> [a] -> Matrix a +(><) :: (Element a) => Int -> Int -> [a] -> Matrix a r >< c = f where f l | dim v == r*c = matrixFromVector RowMajor c v | otherwise = error $ "inconsistent list size = " @@ -122,16 +122,16 @@ r >< c = f where ---------------------------------------------------------------- -- | Creates a matrix with the first n rows of another matrix -takeRows :: Field t => Int -> Matrix t -> Matrix t +takeRows :: Element t => Int -> Matrix t -> Matrix t takeRows n mat = subMatrix (0,0) (n, cols mat) mat -- | Creates a copy of a matrix without the first n rows -dropRows :: Field t => Int -> Matrix t -> Matrix t +dropRows :: Element t => Int -> Matrix t -> Matrix t dropRows n mat = subMatrix (n,0) (rows mat - n, cols mat) mat -- |Creates a matrix with the first n columns of another matrix -takeColumns :: Field t => Int -> Matrix t -> Matrix t +takeColumns :: Element t => Int -> Matrix t -> Matrix t takeColumns n mat = subMatrix (0,0) (rows mat, n) mat -- | Creates a copy of a matrix without the first n columns -dropColumns :: Field t => Int -> Matrix t -> Matrix t +dropColumns :: Element t => Int -> Matrix t -> Matrix t dropColumns n mat = subMatrix (0,n) (rows mat, cols mat - n) mat ---------------------------------------------------------------- @@ -141,7 +141,7 @@ dropColumns n mat = subMatrix (0,n) (rows mat, cols mat - n) mat @\> flatten ('ident' 3) 9 |> [1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]@ -} -flatten :: Field t => Matrix t -> Vector t +flatten :: Element t => Matrix t -> Vector t flatten = cdat {- | Creates a 'Matrix' from a list of lists (considered as rows). @@ -152,20 +152,20 @@ flatten = cdat , 3.0, 4.0 , 5.0, 6.0 ]@ -} -fromLists :: Field t => [[t]] -> Matrix t +fromLists :: Element t => [[t]] -> Matrix t fromLists = fromRows . map fromList -- | creates a 1-row matrix from a vector -asRow :: Field a => Vector a -> Matrix a +asRow :: Element a => Vector a -> Matrix a asRow v = reshape (dim v) v -- | creates a 1-column matrix from a vector -asColumn :: Field a => Vector a -> Matrix a +asColumn :: Element a => Vector a -> Matrix a asColumn v = reshape 1 v ----------------------------------------------------- -fromArray2D :: (Field e) => Array (Int, Int) e -> Matrix e +fromArray2D :: (Element e) => Array (Int, Int) e -> Matrix e fromArray2D m = (r> String -> (t -> String) -> Matrix t -> String +format :: (Element t) => String -> (t -> String) -> Matrix t -> String format sep f m = dsp' sep . map (map f) . toLists $ m disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m @@ -217,7 +217,7 @@ readMatrix :: String -> Matrix Double readMatrix = fromLists . map (map read). map words . filter (not.null) . lines -- | rearranges the rows of a matrix according to the order given in a list of integers. -extractRows :: Field t => [Int] -> Matrix t -> Matrix t +extractRows :: Element t => [Int] -> Matrix t -> Matrix t extractRows l m = fromRows $ extract (toRows $ m) l where extract l is = [l!!i |i<-is] @@ -231,5 +231,5 @@ extractRows l m = fromRows $ extract (toRows $ m) l , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ]@ -} -repmat :: (Field t) => Matrix t -> Int -> Int -> Matrix t +repmat :: (Element t) => Matrix t -> Int -> Int -> Matrix t repmat m r c = fromBlocks $ partit c $ replicate (r*c) m \ No newline at end of file -- cgit v1.2.3