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.hs43
1 files changed, 23 insertions, 20 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index 44204a1..b1e7670 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -220,12 +220,12 @@ class (Storable a, Floating a) => Element a where
220 constantD :: a -> Int -> Vector a 220 constantD :: a -> Int -> Vector a
221 221
222instance Element Double where 222instance Element Double where
223 subMatrixD = subMatrixR 223 subMatrixD = subMatrix'
224 transdata = transdata' 224 transdata = transdata'
225 constantD = constant' 225 constantD = constant'
226 226
227instance Element (Complex Double) where 227instance Element (Complex Double) where
228 subMatrixD = subMatrixC 228 subMatrixD = subMatrix'
229 transdata = transdata' 229 transdata = transdata'
230 constantD = constant' 230 constantD = constant'
231 231
@@ -269,29 +269,32 @@ constant' v n = unsafePerformIO $ do
269 269
270---------------------------------------------------------------------- 270----------------------------------------------------------------------
271 271
272-- | extraction of a submatrix from a real matrix
273subMatrixR :: (Int,Int) -> (Int,Int) -> Matrix Double -> Matrix Double
274subMatrixR (r0,c0) (rt,ct) x' = unsafePerformIO $ do
275 r <- createMatrix RowMajor rt ct
276 let x = cmat x'
277 app2 (c_submatrixR (fi r0) (fi $ r0+rt-1) (fi c0) (fi $ c0+ct-1)) mat x mat r "subMatrixR"
278 return r
279foreign import ccall "auxi.h submatrixR" c_submatrixR :: CInt -> CInt -> CInt -> CInt -> TMM
280
281-- | extraction of a submatrix from a complex matrix
282subMatrixC :: (Int,Int) -> (Int,Int) -> Matrix (Complex Double) -> Matrix (Complex Double)
283subMatrixC (r0,c0) (rt,ct) x =
284 reshape ct . asComplex . flatten .
285 subMatrixR (r0,2*c0) (rt,2*ct) .
286 reshape (2*cols x) . asReal . flatten $ x
287
288-- | Extracts a submatrix from a matrix. 272-- | Extracts a submatrix from a matrix.
289subMatrix :: Element a 273subMatrix :: Element a
290 => (Int,Int) -- ^ (r0,c0) starting position 274 => (Int,Int) -- ^ (r0,c0) starting position
291 -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix 275 -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix
292 -> Matrix a -- ^ input matrix 276 -> Matrix a -- ^ input matrix
293 -> Matrix a -- ^ result 277 -> Matrix a -- ^ result
294subMatrix = subMatrixD 278subMatrix (r0,c0) (rt,ct) m
279 | 0 <= r0 && 0 < rt && r0+rt <= (rows m) &&
280 0 <= c0 && 0 < ct && c0+ct <= (cols m) = subMatrixD (r0,c0) (rt,ct) m
281 | otherwise = error $ "wrong subMatrix "++
282 show ((r0,c0),(rt,ct))++" of "++show(rows m)++"x"++ show (cols m)
283
284subMatrix'' (r0,c0) (rt,ct) c v = unsafePerformIO $ do
285 w <- createVector (rt*ct)
286 withForeignPtr (fptr v) $ \p ->
287 withForeignPtr (fptr w) $ \q -> do
288 let go (-1) _ = return ()
289 go !i (-1) = go (i-1) (ct-1)
290 go !i !j = do x <- peekElemOff p ((i+r0)*c+j+c0)
291 pokeElemOff q (i*ct+j) x
292 go i (j-1)
293 go (rt-1) (ct-1)
294 return w
295
296subMatrix' (r0,c0) (rt,ct) (MC _r c v) = MC rt ct $ subMatrix'' (r0,c0) (rt,ct) c v
297subMatrix' (r0,c0) (rt,ct) m = trans $ subMatrix' (c0,r0) (ct,rt) (trans m)
295 298
296-------------------------------------------------------------------------- 299--------------------------------------------------------------------------
297 300
@@ -316,4 +319,4 @@ fromFile filename (r,c) = do
316 app1 (c_gslReadMatrix charname) mat res "gslReadMatrix" 319 app1 (c_gslReadMatrix charname) mat res "gslReadMatrix"
317 --free charname -- TO DO: free the auxiliary CString 320 --free charname -- TO DO: free the auxiliary CString
318 return res 321 return res
319foreign import ccall "auxi.h matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM 322foreign import ccall "matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM