From f2d385ab680809e08e7c73c70e996284e05eae8c Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Sun, 1 Jun 2008 12:09:50 +0000 Subject: added updateMatrix --- lib/Data/Packed/Internal/Matrix.hs | 27 +++++++++++++++++++++++++++ lib/Data/Packed/Matrix.hs | 1 + 2 files changed, 28 insertions(+) (limited to 'lib/Data/Packed') diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 86c5915..1c58f6e 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs @@ -385,3 +385,30 @@ fromFile filename (r,c) = do --free charname -- TO DO: free the auxiliary CString return res foreign import ccall "auxi.h matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM + +---------------------------------------------------------------------------- + +{- | creates a new matrix with the given position updated with a modification function + +@> updateMatrix (0,2) (const 57) (ident 3 :: Matrix Double) +(3><3) + [ 1.0, 0.0, 57.0 + , 0.0, 1.0, 0.0 + , 0.0, 0.0, 1.0 ]@ + +-} +updateMatrix :: Storable t + => (Int,Int) -- ^ position (row,column) + -> (t -> t) -- ^ modification function + -> Matrix t -- ^ source matrix + -> Matrix t -- ^ result + +updateMatrix (i,j) f (m@MC {rows = r, cols = c, cdat = v}) + | i<0 || i>=r || j<0 || j>=c = error $ "updateMatrix out of range(size="++show (r,c)++", pos="++show (i,j)++")" + | otherwise = let pos = i*c+j + in m { cdat = updateVector pos f v } + +updateMatrix (i,j) f (m@MF {rows = r, cols = c, fdat = v}) + | i<0 || i>=r || j<0 || j>=c = error $ "updateMatrix out of range(size="++show (r,c)++", pos="++show (i,j)++")" + | otherwise = let pos = j*r+i + in m { fdat = updateVector pos f v } diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 62d28b1..b2e58b5 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -28,6 +28,7 @@ module Data.Packed.Matrix ( subMatrix, takeRows, dropRows, takeColumns, dropColumns, extractRows, ident, diag, diagRect, takeDiag, + updateMatrix, liftMatrix, liftMatrix2, format, readMatrix, fromFile, fromArray2D ) where -- cgit v1.2.3