diff options
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 43 |
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 | ||
222 | instance Element Double where | 222 | instance Element Double where |
223 | subMatrixD = subMatrixR | 223 | subMatrixD = subMatrix' |
224 | transdata = transdata' | 224 | transdata = transdata' |
225 | constantD = constant' | 225 | constantD = constant' |
226 | 226 | ||
227 | instance Element (Complex Double) where | 227 | instance 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 | ||
273 | subMatrixR :: (Int,Int) -> (Int,Int) -> Matrix Double -> Matrix Double | ||
274 | subMatrixR (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 | ||
279 | foreign import ccall "auxi.h submatrixR" c_submatrixR :: CInt -> CInt -> CInt -> CInt -> TMM | ||
280 | |||
281 | -- | extraction of a submatrix from a complex matrix | ||
282 | subMatrixC :: (Int,Int) -> (Int,Int) -> Matrix (Complex Double) -> Matrix (Complex Double) | ||
283 | subMatrixC (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. |
289 | subMatrix :: Element a | 273 | subMatrix :: 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 |
294 | subMatrix = subMatrixD | 278 | subMatrix (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 | |||
284 | subMatrix'' (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 | |||
296 | subMatrix' (r0,c0) (rt,ct) (MC _r c v) = MC rt ct $ subMatrix'' (r0,c0) (rt,ct) c v | ||
297 | subMatrix' (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 |
319 | foreign import ccall "auxi.h matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM | 322 | foreign import ccall "matrix_fscanf" c_gslReadMatrix:: Ptr CChar -> TM |