diff options
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 40 |
1 files changed, 23 insertions, 17 deletions
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 @@ | |||
16 | -- #hide | 16 | -- #hide |
17 | 17 | ||
18 | module Data.Packed.Internal.Matrix( | 18 | module Data.Packed.Internal.Matrix( |
19 | Matrix(..), | 19 | Matrix(..), rows, cols, |
20 | MatrixOrder(..), orderOf, | 20 | MatrixOrder(..), orderOf, |
21 | createMatrix, withMatrix, mat, | 21 | createMatrix, withMatrix, mat, |
22 | cmat, fmat, | 22 | cmat, fmat, |
@@ -77,17 +77,23 @@ import Foreign.C.String | |||
77 | data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) | 77 | data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) |
78 | 78 | ||
79 | -- | Matrix representation suitable for GSL and LAPACK computations. | 79 | -- | Matrix representation suitable for GSL and LAPACK computations. |
80 | data Matrix t = MC { rows :: {-# UNPACK #-} !Int | 80 | data Matrix t = MC { irows :: {-# UNPACK #-} !Int |
81 | , cols :: {-# UNPACK #-} !Int | 81 | , icols :: {-# UNPACK #-} !Int |
82 | , cdat :: {-# UNPACK #-} !(Vector t) } | 82 | , cdat :: {-# UNPACK #-} !(Vector t) } |
83 | 83 | ||
84 | | MF { rows :: {-# UNPACK #-} !Int | 84 | | MF { irows :: {-# UNPACK #-} !Int |
85 | , cols :: {-# UNPACK #-} !Int | 85 | , icols :: {-# UNPACK #-} !Int |
86 | , fdat :: {-# UNPACK #-} !(Vector t) } | 86 | , fdat :: {-# UNPACK #-} !(Vector t) } |
87 | 87 | ||
88 | -- MC: preferred by C, fdat may require a transposition | 88 | -- MC: preferred by C, fdat may require a transposition |
89 | -- MF: preferred by LAPACK, cdat may require a transposition | 89 | -- MF: preferred by LAPACK, cdat may require a transposition |
90 | 90 | ||
91 | rows :: Matrix t -> Int | ||
92 | rows = irows | ||
93 | |||
94 | cols :: Matrix t -> Int | ||
95 | cols = icols | ||
96 | |||
91 | xdat MC {cdat = d } = d | 97 | xdat MC {cdat = d } = d |
92 | xdat MF {fdat = d } = d | 98 | xdat MF {fdat = d } = d |
93 | 99 | ||
@@ -97,16 +103,16 @@ orderOf MC{} = RowMajor | |||
97 | 103 | ||
98 | -- | Matrix transpose. | 104 | -- | Matrix transpose. |
99 | trans :: Matrix t -> Matrix t | 105 | trans :: Matrix t -> Matrix t |
100 | trans MC {rows = r, cols = c, cdat = d } = MF {rows = c, cols = r, fdat = d } | 106 | trans MC {irows = r, icols = c, cdat = d } = MF {irows = c, icols = r, fdat = d } |
101 | trans MF {rows = r, cols = c, fdat = d } = MC {rows = c, cols = r, cdat = d } | 107 | trans MF {irows = r, icols = c, fdat = d } = MC {irows = c, icols = r, cdat = d } |
102 | 108 | ||
103 | cmat :: (Element t) => Matrix t -> Matrix t | 109 | cmat :: (Element t) => Matrix t -> Matrix t |
104 | cmat m@MC{} = m | 110 | cmat m@MC{} = m |
105 | cmat MF {rows = r, cols = c, fdat = d } = MC {rows = r, cols = c, cdat = transdata r d c} | 111 | cmat MF {irows = r, icols = c, fdat = d } = MC {irows = r, icols = c, cdat = transdata r d c} |
106 | 112 | ||
107 | fmat :: (Element t) => Matrix t -> Matrix t | 113 | fmat :: (Element t) => Matrix t -> Matrix t |
108 | fmat m@MF{} = m | 114 | fmat m@MF{} = m |
109 | fmat MC {rows = r, cols = c, cdat = d } = MF {rows = r, cols = c, fdat = transdata c d r} | 115 | fmat MC {irows = r, icols = c, cdat = d } = MF {irows = r, icols = c, fdat = transdata c d r} |
110 | 116 | ||
111 | -- C-Haskell matrix adapter | 117 | -- C-Haskell matrix adapter |
112 | mat :: Adapt (CInt -> CInt -> Ptr t -> r) (Matrix t) r | 118 | mat :: Adapt (CInt -> CInt -> Ptr t -> r) (Matrix t) r |
@@ -170,13 +176,13 @@ infixl 9 @@> | |||
170 | -- | i<0 || i>=r || j<0 || j>=c = error "matrix indexing out of range" | 176 | -- | i<0 || i>=r || j<0 || j>=c = error "matrix indexing out of range" |
171 | -- | otherwise = cdat m `at` (i*c+j) | 177 | -- | otherwise = cdat m `at` (i*c+j) |
172 | 178 | ||
173 | MC {rows = r, cols = c, cdat = v} @@> (i,j) | 179 | MC {irows = r, icols = c, cdat = v} @@> (i,j) |
174 | | safe = if i<0 || i>=r || j<0 || j>=c | 180 | | safe = if i<0 || i>=r || j<0 || j>=c |
175 | then error "matrix indexing out of range" | 181 | then error "matrix indexing out of range" |
176 | else v `at` (i*c+j) | 182 | else v `at` (i*c+j) |
177 | | otherwise = v `at` (i*c+j) | 183 | | otherwise = v `at` (i*c+j) |
178 | 184 | ||
179 | MF {rows = r, cols = c, fdat = v} @@> (i,j) | 185 | MF {irows = r, icols = c, fdat = v} @@> (i,j) |
180 | | safe = if i<0 || i>=r || j<0 || j>=c | 186 | | safe = if i<0 || i>=r || j<0 || j>=c |
181 | then error "matrix indexing out of range" | 187 | then error "matrix indexing out of range" |
182 | else v `at` (j*r+i) | 188 | else v `at` (j*r+i) |
@@ -184,18 +190,18 @@ MF {rows = r, cols = c, fdat = v} @@> (i,j) | |||
184 | {-# INLINE (@@>) #-} | 190 | {-# INLINE (@@>) #-} |
185 | 191 | ||
186 | -- Unsafe matrix access without range checking | 192 | -- Unsafe matrix access without range checking |
187 | atM' MC {cols = c, cdat = v} i j = v `at'` (i*c+j) | 193 | atM' MC {icols = c, cdat = v} i j = v `at'` (i*c+j) |
188 | atM' MF {rows = r, fdat = v} i j = v `at'` (j*r+i) | 194 | atM' MF {irows = r, fdat = v} i j = v `at'` (j*r+i) |
189 | {-# INLINE atM' #-} | 195 | {-# INLINE atM' #-} |
190 | 196 | ||
191 | ------------------------------------------------------------------ | 197 | ------------------------------------------------------------------ |
192 | 198 | ||
193 | matrixFromVector RowMajor c v = MC { rows = r, cols = c, cdat = v } | 199 | matrixFromVector RowMajor c v = MC { irows = r, icols = c, cdat = v } |
194 | where (d,m) = dim v `divMod` c | 200 | where (d,m) = dim v `divMod` c |
195 | r | m==0 = d | 201 | r | m==0 = d |
196 | | otherwise = error "matrixFromVector" | 202 | | otherwise = error "matrixFromVector" |
197 | 203 | ||
198 | matrixFromVector ColumnMajor c v = MF { rows = r, cols = c, fdat = v } | 204 | matrixFromVector ColumnMajor c v = MF { irows = r, icols = c, fdat = v } |
199 | where (d,m) = dim v `divMod` c | 205 | where (d,m) = dim v `divMod` c |
200 | r | m==0 = d | 206 | r | m==0 = d |
201 | | otherwise = error "matrixFromVector" | 207 | | otherwise = error "matrixFromVector" |
@@ -223,8 +229,8 @@ singleton x = reshape 1 (fromList [x]) | |||
223 | 229 | ||
224 | -- | application of a vector function on the flattened matrix elements | 230 | -- | application of a vector function on the flattened matrix elements |
225 | liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b | 231 | liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b |
226 | liftMatrix f MC { cols = c, cdat = d } = matrixFromVector RowMajor c (f d) | 232 | liftMatrix f MC { icols = c, cdat = d } = matrixFromVector RowMajor c (f d) |
227 | liftMatrix f MF { cols = c, fdat = d } = matrixFromVector ColumnMajor c (f d) | 233 | liftMatrix f MF { icols = c, fdat = d } = matrixFromVector ColumnMajor c (f d) |
228 | 234 | ||
229 | -- | application of a vector function on the flattened matrices elements | 235 | -- | application of a vector function on the flattened matrices elements |
230 | liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | 236 | liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t |