summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal/Matrix.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs40
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
18module Data.Packed.Internal.Matrix( 18module 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
77data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) 77data 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.
80data Matrix t = MC { rows :: {-# UNPACK #-} !Int 80data 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
91rows :: Matrix t -> Int
92rows = irows
93
94cols :: Matrix t -> Int
95cols = icols
96
91xdat MC {cdat = d } = d 97xdat MC {cdat = d } = d
92xdat MF {fdat = d } = d 98xdat MF {fdat = d } = d
93 99
@@ -97,16 +103,16 @@ orderOf MC{} = RowMajor
97 103
98-- | Matrix transpose. 104-- | Matrix transpose.
99trans :: Matrix t -> Matrix t 105trans :: Matrix t -> Matrix t
100trans MC {rows = r, cols = c, cdat = d } = MF {rows = c, cols = r, fdat = d } 106trans MC {irows = r, icols = c, cdat = d } = MF {irows = c, icols = r, fdat = d }
101trans MF {rows = r, cols = c, fdat = d } = MC {rows = c, cols = r, cdat = d } 107trans MF {irows = r, icols = c, fdat = d } = MC {irows = c, icols = r, cdat = d }
102 108
103cmat :: (Element t) => Matrix t -> Matrix t 109cmat :: (Element t) => Matrix t -> Matrix t
104cmat m@MC{} = m 110cmat m@MC{} = m
105cmat MF {rows = r, cols = c, fdat = d } = MC {rows = r, cols = c, cdat = transdata r d c} 111cmat MF {irows = r, icols = c, fdat = d } = MC {irows = r, icols = c, cdat = transdata r d c}
106 112
107fmat :: (Element t) => Matrix t -> Matrix t 113fmat :: (Element t) => Matrix t -> Matrix t
108fmat m@MF{} = m 114fmat m@MF{} = m
109fmat MC {rows = r, cols = c, cdat = d } = MF {rows = r, cols = c, fdat = transdata c d r} 115fmat 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
112mat :: Adapt (CInt -> CInt -> Ptr t -> r) (Matrix t) r 118mat :: 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
173MC {rows = r, cols = c, cdat = v} @@> (i,j) 179MC {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
179MF {rows = r, cols = c, fdat = v} @@> (i,j) 185MF {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
187atM' MC {cols = c, cdat = v} i j = v `at'` (i*c+j) 193atM' MC {icols = c, cdat = v} i j = v `at'` (i*c+j)
188atM' MF {rows = r, fdat = v} i j = v `at'` (j*r+i) 194atM' MF {irows = r, fdat = v} i j = v `at'` (j*r+i)
189{-# INLINE atM' #-} 195{-# INLINE atM' #-}
190 196
191------------------------------------------------------------------ 197------------------------------------------------------------------
192 198
193matrixFromVector RowMajor c v = MC { rows = r, cols = c, cdat = v } 199matrixFromVector 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
198matrixFromVector ColumnMajor c v = MF { rows = r, cols = c, fdat = v } 204matrixFromVector 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
225liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b 231liftMatrix :: (Element a, Element b) => (Vector a -> Vector b) -> Matrix a -> Matrix b
226liftMatrix f MC { cols = c, cdat = d } = matrixFromVector RowMajor c (f d) 232liftMatrix f MC { icols = c, cdat = d } = matrixFromVector RowMajor c (f d)
227liftMatrix f MF { cols = c, fdat = d } = matrixFromVector ColumnMajor c (f d) 233liftMatrix 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
230liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t 236liftMatrix2 :: (Element t, Element a, Element b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t