summaryrefslogtreecommitdiff
path: root/packages/base/src/Internal/Matrix.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2015-06-30 14:38:52 +0200
committerAlberto Ruiz <aruiz@um.es>2015-06-30 14:38:52 +0200
commit4ada25636995115f2b26707870f611a138f4e20b (patch)
tree07ade269d5b98ae8284d2064bb3a8ea928e3e405 /packages/base/src/Internal/Matrix.hs
parent4730254f061832591d4a44c86d3bdfa4620f4322 (diff)
subMatrix changed to non copying slice
Diffstat (limited to 'packages/base/src/Internal/Matrix.hs')
-rw-r--r--packages/base/src/Internal/Matrix.hs18
1 files changed, 4 insertions, 14 deletions
diff --git a/packages/base/src/Internal/Matrix.hs b/packages/base/src/Internal/Matrix.hs
index df56207..5163421 100644
--- a/packages/base/src/Internal/Matrix.hs
+++ b/packages/base/src/Internal/Matrix.hs
@@ -70,7 +70,7 @@ is1d (size->(r,c)) = r==1 || c==1
70{-# INLINE is1d #-} 70{-# INLINE is1d #-}
71 71
72-- data is not contiguous 72-- data is not contiguous
73isSlice m@(size->(r,c)) = (c < xRow m || r < xCol m) && min r c > 1 73isSlice m@(size->(r,c)) = r*c < dim (xdat m)
74{-# INLINE isSlice #-} 74{-# INLINE isSlice #-}
75 75
76orderOf :: Matrix t -> MatrixOrder 76orderOf :: Matrix t -> MatrixOrder
@@ -359,26 +359,16 @@ instance Element Z where
359 359
360------------------------------------------------------------------- 360-------------------------------------------------------------------
361 361
362-- | reference to a rectangular slice of a matrix (no data copy)
362subMatrix :: Element a 363subMatrix :: Element a
363 => (Int,Int) -- ^ (r0,c0) starting position
364 -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix
365 -> Matrix a -- ^ input matrix
366 -> Matrix a -- ^ result
367subMatrix (r0,c0) (rt,ct) m
368 | 0 <= r0 && 0 <= rt && r0+rt <= rows m &&
369 0 <= c0 && 0 <= ct && c0+ct <= cols m = unsafePerformIO $ extractR RowMajor m 0 (idxs[r0,r0+rt-1]) 0 (idxs[c0,c0+ct-1])
370 | otherwise = error $ "wrong subMatrix "++show ((r0,c0),(rt,ct))++" of "++shSize m
371
372
373sliceMatrix :: Element a
374 => (Int,Int) -- ^ (r0,c0) starting position 364 => (Int,Int) -- ^ (r0,c0) starting position
375 -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix 365 -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix
376 -> Matrix a -- ^ input matrix 366 -> Matrix a -- ^ input matrix
377 -> Matrix a -- ^ result 367 -> Matrix a -- ^ result
378sliceMatrix (r0,c0) (rt,ct) m 368subMatrix (r0,c0) (rt,ct) m
379 | 0 <= r0 && 0 <= rt && r0+rt <= rows m && 369 | 0 <= r0 && 0 <= rt && r0+rt <= rows m &&
380 0 <= c0 && 0 <= ct && c0+ct <= cols m = res 370 0 <= c0 && 0 <= ct && c0+ct <= cols m = res
381 | otherwise = error $ "wrong sliceMatrix "++show ((r0,c0),(rt,ct))++" of "++shSize m 371 | otherwise = error $ "wrong subMatrix "++show ((r0,c0),(rt,ct))++" of "++shSize m
382 where 372 where
383 p = r0 * xRow m + c0 * xCol m 373 p = r0 * xRow m + c0 * xCol m
384 tot | rowOrder m = ct + (rt-1) * xRow m 374 tot | rowOrder m = ct + (rt-1) * xRow m