summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal/Matrix.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-06-23 20:21:03 +0000
committerAlberto Ruiz <aruiz@um.es>2007-06-23 20:21:03 +0000
commit3d5d6f06598aac00906c93ac5358e68697c47fc7 (patch)
tree77a76afcd561b8beee33c39b4eafe72b4fa10b86 /lib/Data/Packed/Internal/Matrix.hs
parent978e6d038239af50d70bae2c303f4e45b1879b7a (diff)
more refactoring
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs10
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
29data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) 29data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq)
30 30
31-- | 2D array
32data Matrix t = M { rows :: Int 31data 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
44cdat m = if fortran m `xor` isTrans m then tdat m else dat m 43cdat m = if fortran m `xor` isTrans m then tdat m else dat m
45fdat m = if fortran m `xor` isTrans m then dat m else tdat m 44fdat m = if fortran m `xor` isTrans m then dat m else tdat m
46 45
46trans :: Matrix t -> Matrix t
47trans m = m { rows = cols m 47trans 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
57mat d m f = f (rows m) (cols m) (ptr (d m)) 57mat d m f = f (rows m) (cols m) (ptr (d m))
58 58
59toLists :: (Storable t) => Matrix t -> [[t]]
59toLists m = partit (cols m) . toList . cdat $ m 60toLists m = partit (cols m) . toList . cdat $ m
60 61
61instance (Show a, Storable a) => (Show (Matrix a)) where 62instance (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
96reshape :: (Field t) => Int -> Vector t -> Matrix t
95reshape c v = matrixFromVector RowMajor c v 97reshape c v = matrixFromVector RowMajor c v
96 98
97singleton x = reshape 1 (fromList [x]) 99singleton 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 138liftMatrix :: (Vector a -> Vector b) -> Matrix a -> Matrix b
137liftMatrix f m = m { dat = f (dat m), tdat = f (tdat m) } -- check sizes 139liftMatrix f m = m { dat = f (dat m), tdat = f (tdat m) } -- check sizes
140
141liftMatrix2 :: (Field t) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
138liftMatrix2 f m1 m2 = reshape (cols m1) (f (cdat m1) (cdat m2)) -- check sizes 142liftMatrix2 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
197outer' u v = dat (outer u v) 201outer' u v = dat (outer u v)
198 202
203outer :: (Num t, Field t) => Vector t -> Vector t -> Matrix t
199outer u v = multiply RowMajor r c 204outer 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
208dot :: (Field t, Num t) => Vector t -> Vector t -> t
203dot u v = dat (multiply RowMajor r c) `at` 0 209dot 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