diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-06-23 20:21:03 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-06-23 20:21:03 +0000 |
commit | 3d5d6f06598aac00906c93ac5358e68697c47fc7 (patch) | |
tree | 77a76afcd561b8beee33c39b4eafe72b4fa10b86 /lib/Data/Packed/Internal/Matrix.hs | |
parent | 978e6d038239af50d70bae2c303f4e45b1879b7a (diff) |
more refactoring
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 32dc603..9309d1d 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -28,7 +28,6 @@ import Data.Maybe(fromJust) | |||
28 | 28 | ||
29 | data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) | 29 | data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) |
30 | 30 | ||
31 | -- | 2D array | ||
32 | data Matrix t = M { rows :: Int | 31 | data Matrix t = M { rows :: Int |
33 | , cols :: Int | 32 | , cols :: Int |
34 | , dat :: Vector t | 33 | , dat :: Vector t |
@@ -44,6 +43,7 @@ fortran m = order m == ColumnMajor | |||
44 | cdat m = if fortran m `xor` isTrans m then tdat m else dat m | 43 | cdat m = if fortran m `xor` isTrans m then tdat m else dat m |
45 | fdat m = if fortran m `xor` isTrans m then dat m else tdat m | 44 | fdat m = if fortran m `xor` isTrans m then dat m else tdat m |
46 | 45 | ||
46 | trans :: Matrix t -> Matrix t | ||
47 | trans m = m { rows = cols m | 47 | trans m = m { rows = cols m |
48 | , cols = rows m | 48 | , cols = rows m |
49 | , isTrans = not (isTrans m) | 49 | , isTrans = not (isTrans m) |
@@ -56,6 +56,7 @@ type Mt t s = Int -> Int -> Ptr t -> s | |||
56 | 56 | ||
57 | mat d m f = f (rows m) (cols m) (ptr (d m)) | 57 | mat d m f = f (rows m) (cols m) (ptr (d m)) |
58 | 58 | ||
59 | toLists :: (Storable t) => Matrix t -> [[t]] | ||
59 | toLists m = partit (cols m) . toList . cdat $ m | 60 | toLists m = partit (cols m) . toList . cdat $ m |
60 | 61 | ||
61 | instance (Show a, Storable a) => (Show (Matrix a)) where | 62 | instance (Show a, Storable a) => (Show (Matrix a)) where |
@@ -92,6 +93,7 @@ createMatrix order r c = do | |||
92 | p <- createVector (r*c) | 93 | p <- createVector (r*c) |
93 | return (matrixFromVector order c p) | 94 | return (matrixFromVector order c p) |
94 | 95 | ||
96 | reshape :: (Field t) => Int -> Vector t -> Matrix t | ||
95 | reshape c v = matrixFromVector RowMajor c v | 97 | reshape c v = matrixFromVector RowMajor c v |
96 | 98 | ||
97 | singleton x = reshape 1 (fromList [x]) | 99 | singleton x = reshape 1 (fromList [x]) |
@@ -133,8 +135,10 @@ transdata c1 d c2 | isReal baseOf d = scast $ transdataR c1 (scast d) c2 | |||
133 | --{-# RULES "transdataC" transdata=transdataC #-} | 135 | --{-# RULES "transdataC" transdata=transdataC #-} |
134 | 136 | ||
135 | ----------------------------------------------------------------- | 137 | ----------------------------------------------------------------- |
136 | 138 | liftMatrix :: (Vector a -> Vector b) -> Matrix a -> Matrix b | |
137 | liftMatrix f m = m { dat = f (dat m), tdat = f (tdat m) } -- check sizes | 139 | liftMatrix f m = m { dat = f (dat m), tdat = f (tdat m) } -- check sizes |
140 | |||
141 | liftMatrix2 :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | ||
138 | liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes | 142 | liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes |
139 | 143 | ||
140 | ------------------------------------------------------------------ | 144 | ------------------------------------------------------------------ |
@@ -196,10 +200,12 @@ multiplyD order a b | |||
196 | 200 | ||
197 | outer' u v = dat (outer u v) | 201 | outer' u v = dat (outer u v) |
198 | 202 | ||
203 | outer :: (Num t, Field t) => Vector t -> Vector t -> Matrix t | ||
199 | outer u v = multiply RowMajor r c | 204 | outer u v = multiply RowMajor r c |
200 | where r = matrixFromVector RowMajor 1 u | 205 | where r = matrixFromVector RowMajor 1 u |
201 | c = matrixFromVector RowMajor (dim v) v | 206 | c = matrixFromVector RowMajor (dim v) v |
202 | 207 | ||
208 | dot :: (Field t, Num t) => Vector t -> Vector t -> t | ||
203 | dot u v = dat (multiply RowMajor r c) `at` 0 | 209 | dot u v = dat (multiply RowMajor r c) `at` 0 |
204 | where r = matrixFromVector RowMajor (dim u) u | 210 | where r = matrixFromVector RowMajor (dim u) u |
205 | c = matrixFromVector RowMajor 1 v | 211 | c = matrixFromVector RowMajor 1 v |