diff options
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 13ffc34..8a074a6 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -74,6 +74,7 @@ data Matrix t = MC { rows :: {-# UNPACK #-} !Int | |||
74 | xdat MC {cdat = d } = d | 74 | xdat MC {cdat = d } = d |
75 | xdat MF {fdat = d } = d | 75 | xdat MF {fdat = d } = d |
76 | 76 | ||
77 | orderOf :: Matrix t -> MatrixOrder | ||
77 | orderOf MF{} = ColumnMajor | 78 | orderOf MF{} = ColumnMajor |
78 | orderOf MC{} = RowMajor | 79 | orderOf MC{} = RowMajor |
79 | 80 | ||
@@ -82,12 +83,16 @@ trans :: Matrix t -> Matrix t | |||
82 | trans MC {rows = r, cols = c, cdat = d } = MF {rows = c, cols = r, fdat = d } | 83 | trans MC {rows = r, cols = c, cdat = d } = MF {rows = c, cols = r, fdat = d } |
83 | trans MF {rows = r, cols = c, fdat = d } = MC {rows = c, cols = r, cdat = d } | 84 | trans MF {rows = r, cols = c, fdat = d } = MC {rows = c, cols = r, cdat = d } |
84 | 85 | ||
86 | cmat :: (Element t) => Matrix t -> Matrix t | ||
85 | cmat m@MC{} = m | 87 | cmat m@MC{} = m |
86 | cmat MF {rows = r, cols = c, fdat = d } = MC {rows = r, cols = c, cdat = transdata r d c} | 88 | cmat MF {rows = r, cols = c, fdat = d } = MC {rows = r, cols = c, cdat = transdata r d c} |
87 | 89 | ||
90 | fmat :: (Element t) => Matrix t -> Matrix t | ||
88 | fmat m@MF{} = m | 91 | fmat m@MF{} = m |
89 | fmat MC {rows = r, cols = c, cdat = d } = MF {rows = r, cols = c, fdat = transdata c d r} | 92 | fmat MC {rows = r, cols = c, cdat = d } = MF {rows = r, cols = c, fdat = transdata c d r} |
90 | 93 | ||
94 | -- C-Haskell matrix adapter | ||
95 | mat :: Adapt (CInt -> CInt -> Ptr t -> r) (Matrix t) r | ||
91 | mat = withMatrix | 96 | mat = withMatrix |
92 | 97 | ||
93 | withMatrix a f = | 98 | withMatrix a f = |
@@ -156,7 +161,7 @@ MF {rows = r, cols = c, fdat = v} @@> (i,j) | |||
156 | | otherwise = v `at` (j*r+i) | 161 | | otherwise = v `at` (j*r+i) |
157 | {-# INLINE (@@>) #-} | 162 | {-# INLINE (@@>) #-} |
158 | 163 | ||
159 | -- | Unsafe matrix access without range checking | 164 | -- Unsafe matrix access without range checking |
160 | atM' MC {cols = c, cdat = v} i j = v `at'` (i*c+j) | 165 | atM' MC {cols = c, cdat = v} i j = v `at'` (i*c+j) |
161 | atM' MF {rows = r, fdat = v} i j = v `at'` (j*r+i) | 166 | atM' MF {rows = r, fdat = v} i j = v `at'` (j*r+i) |
162 | {-# INLINE atM' #-} | 167 | {-# INLINE atM' #-} |
@@ -173,6 +178,8 @@ matrixFromVector ColumnMajor c v = MF { rows = r, cols = c, fdat = v } | |||
173 | r | m==0 = d | 178 | r | m==0 = d |
174 | | otherwise = error "matrixFromVector" | 179 | | otherwise = error "matrixFromVector" |
175 | 180 | ||
181 | -- allocates memory for a new matrix | ||
182 | createMatrix :: (Storable a) => MatrixOrder -> Int -> Int -> IO (Matrix a) | ||
176 | createMatrix order r c = do | 183 | createMatrix order r c = do |
177 | p <- createVector (r*c) | 184 | p <- createVector (r*c) |
178 | return (matrixFromVector order c p) | 185 | return (matrixFromVector order c p) |