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.hs9
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
74xdat MC {cdat = d } = d 74xdat MC {cdat = d } = d
75xdat MF {fdat = d } = d 75xdat MF {fdat = d } = d
76 76
77orderOf :: Matrix t -> MatrixOrder
77orderOf MF{} = ColumnMajor 78orderOf MF{} = ColumnMajor
78orderOf MC{} = RowMajor 79orderOf MC{} = RowMajor
79 80
@@ -82,12 +83,16 @@ trans :: Matrix t -> Matrix t
82trans MC {rows = r, cols = c, cdat = d } = MF {rows = c, cols = r, fdat = d } 83trans MC {rows = r, cols = c, cdat = d } = MF {rows = c, cols = r, fdat = d }
83trans MF {rows = r, cols = c, fdat = d } = MC {rows = c, cols = r, cdat = d } 84trans MF {rows = r, cols = c, fdat = d } = MC {rows = c, cols = r, cdat = d }
84 85
86cmat :: (Element t) => Matrix t -> Matrix t
85cmat m@MC{} = m 87cmat m@MC{} = m
86cmat MF {rows = r, cols = c, fdat = d } = MC {rows = r, cols = c, cdat = transdata r d c} 88cmat MF {rows = r, cols = c, fdat = d } = MC {rows = r, cols = c, cdat = transdata r d c}
87 89
90fmat :: (Element t) => Matrix t -> Matrix t
88fmat m@MF{} = m 91fmat m@MF{} = m
89fmat MC {rows = r, cols = c, cdat = d } = MF {rows = r, cols = c, fdat = transdata c d r} 92fmat MC {rows = r, cols = c, cdat = d } = MF {rows = r, cols = c, fdat = transdata c d r}
90 93
94-- C-Haskell matrix adapter
95mat :: Adapt (CInt -> CInt -> Ptr t -> r) (Matrix t) r
91mat = withMatrix 96mat = withMatrix
92 97
93withMatrix a f = 98withMatrix 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
160atM' MC {cols = c, cdat = v} i j = v `at'` (i*c+j) 165atM' MC {cols = c, cdat = v} i j = v `at'` (i*c+j)
161atM' MF {rows = r, fdat = v} i j = v `at'` (j*r+i) 166atM' 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
182createMatrix :: (Storable a) => MatrixOrder -> Int -> Int -> IO (Matrix a)
176createMatrix order r c = do 183createMatrix 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)