summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Matrix.hs53
1 files changed, 50 insertions, 3 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 4f2ad90..5a4b919 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -1,3 +1,4 @@
1{-# OPTIONS_GHC -fglasgow-exts #-}
1----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
2-- | 3-- |
3-- Module : Data.Packed.Matrix 4-- Module : Data.Packed.Matrix
@@ -21,17 +22,21 @@ module Data.Packed.Matrix (
21 trans, conjTrans, 22 trans, conjTrans,
22 asRow, asColumn, 23 asRow, asColumn,
23 fromRows, toRows, fromColumns, toColumns, 24 fromRows, toRows, fromColumns, toColumns,
24 fromBlocks, joinVert, joinHoriz, 25 fromBlocks,
25 flipud, fliprl, 26 flipud, fliprl,
26 subMatrix, takeRows, dropRows, takeColumns, dropColumns, 27 subMatrix, takeRows, dropRows, takeColumns, dropColumns,
27 ident, diag, diagRect, takeDiag, 28 ident, diag, diagRect, takeDiag,
28 liftMatrix, liftMatrix2, 29 liftMatrix, liftMatrix2,
30 dispR, readMatrix, fromArray2D
29) where 31) where
30 32
31import Data.Packed.Internal 33import Data.Packed.Internal
32import Foreign(Storable) 34import Foreign(Storable)
33import Complex 35import Complex
34import Data.Packed.Vector 36import Data.Packed.Vector
37import Numeric(showGFloat)
38import Data.List(transpose,intersperse)
39import Data.Array
35 40
36-- | creates a matrix from a vertical list of matrices 41-- | creates a matrix from a vertical list of matrices
37joinVert :: Field t => [Matrix t] -> Matrix t 42joinVert :: Field t => [Matrix t] -> Matrix t
@@ -160,5 +165,47 @@ asRow v = reshape (dim v) v
160asColumn :: Field a => Vector a -> Matrix a 165asColumn :: Field a => Vector a -> Matrix a
161asColumn v = reshape 1 v 166asColumn v = reshape 1 v
162 167
163------------------------------------------------ 168-----------------------------------------------------
164 169
170fromArray2D :: (Field e) => Array (Int, Int) e -> Matrix e
171fromArray2D m = (r><c) (elems m)
172 where ((r0,c0),(r1,c1)) = bounds m
173 r = r1-r0+1
174 c = c1-c0+1
175
176------------------------------------------------------
177-- shows a Double with n digits after the decimal point
178shf :: (RealFloat a) => Int -> a -> String
179shf dec n | abs n < 1e-10 = "0."
180 | abs (n - (fromIntegral.round $ n)) < 1e-10 = show (round n) ++"."
181 | otherwise = showGFloat (Just dec) n ""
182-- shows a Complex Double as a pair, with n digits after the decimal point
183shfc n z@ (a:+b)
184 | magnitude z <1e-10 = "0."
185 | abs b < 1e-10 = shf n a
186 | abs a < 1e-10 = shf n b ++"i"
187 | b > 0 = shf n a ++"+"++shf n b ++"i"
188 | otherwise = shf n a ++shf n b ++"i"
189
190dsp' :: String -> [[String]] -> String
191dsp' sep as = unlines . map unwords' $ transpose mtp where
192 mt = transpose as
193 longs = map (maximum . map length) mt
194 mtp = zipWith (\a b -> map (pad a) b) longs mt
195 pad n str = replicate (n - length str) ' ' ++ str
196 unwords' = concat . intersperse sep
197
198format :: (Field t) => String -> (t -> String) -> Matrix t -> String
199format sep f m = dsp' sep . map (map f) . toLists $ m
200
201disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m
202
203dispR :: Int -> Matrix Double -> IO ()
204dispR d m = disp m (shf d)
205
206dispC :: Int -> Matrix (Complex Double) -> IO ()
207dispC d m = disp m (shfc d)
208
209-- | creates a matrix from a table of numbers.
210readMatrix :: String -> Matrix Double
211readMatrix = fromLists . map (map read). map words . filter (not.null) . lines