summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-02-03 15:25:44 +0000
committerAlberto Ruiz <aruiz@um.es>2010-02-03 15:25:44 +0000
commitab5a2c1c8b4ecd7f8d9d9823d9976410aa94bb18 (patch)
tree566df5edcf5c1f431bc5d5ce1c1e0a46767c7361 /lib/Data/Packed/Matrix.hs
parent25592aea904c4f29ea1c06210adb52dae99ba95c (diff)
dispcf, latexFormat
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs75
1 files changed, 42 insertions, 33 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index a8e998f..dced49f 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -28,7 +28,7 @@ module Data.Packed.Matrix (
28 extractRows, 28 extractRows,
29 ident, diag, diagRect, takeDiag, 29 ident, diag, diagRect, takeDiag,
30 liftMatrix, liftMatrix2, liftMatrix2Auto, 30 liftMatrix, liftMatrix2, liftMatrix2Auto,
31 format, dispf, disps, 31 dispf, disps, dispcf, showComplex, latexFormat, format,
32 loadMatrix, saveMatrix, fromFile, fileDimensions, 32 loadMatrix, saveMatrix, fromFile, fileDimensions,
33 readMatrix, fromArray2D 33 readMatrix, fromArray2D
34) where 34) where
@@ -39,7 +39,8 @@ import Data.Packed.Vector
39import Data.Array 39import Data.Array
40import System.Process(readProcess) 40import System.Process(readProcess)
41import Text.Printf(printf) 41import Text.Printf(printf)
42import Data.List(transpose) 42import Data.List(transpose,intersperse)
43import Data.Complex
43 44
44-- | creates a matrix from a vertical list of matrices 45-- | creates a matrix from a vertical list of matrices
45joinVert :: Element t => [Matrix t] -> Matrix t 46joinVert :: Element t => [Matrix t] -> Matrix t
@@ -232,22 +233,9 @@ fromArray2D m = (r><c) (elems m)
232 r = r1-r0+1 233 r = r1-r0+1
233 c = c1-c0+1 234 c = c1-c0+1
234 235
235------------------------------------------------------
236{-
237-- shows a Double with n digits after the decimal point
238shf :: (RealFloat a) => Int -> a -> String
239shf dec n | abs n < 1e-10 = "0."
240 | abs (n - (fromIntegral.round $ n)) < 1e-10 = show (round n) ++"."
241 | otherwise = showGFloat (Just dec) n ""
242-- shows a Complex Double as a pair, with n digits after the decimal point
243shfc n z@ (a:+b)
244 | magnitude z <1e-10 = "0."
245 | abs b < 1e-10 = shf n a
246 | abs a < 1e-10 = shf n b ++"i"
247 | b > 0 = shf n a ++"+"++shf n b ++"i"
248 | otherwise = shf n a ++shf n b ++"i"
249 236
250-} 237-------------------------------------------------------------------
238-- display utilities
251 239
252 240
253{- | Creates a string from a matrix given a separator and a function to show each entry. Using 241{- | Creates a string from a matrix given a separator and a function to show each entry. Using
@@ -261,19 +249,6 @@ this function the user can easily define any desired display function:
261format :: (Element t) => String -> (t -> String) -> Matrix t -> String 249format :: (Element t) => String -> (t -> String) -> Matrix t -> String
262format sep f m = table sep . map (map f) . toLists $ m 250format sep f m = table sep . map (map f) . toLists $ m
263 251
264{-
265disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m
266
267dispR :: Int -> Matrix Double -> IO ()
268dispR d m = disp m (shf d)
269
270dispC :: Int -> Matrix (Complex Double) -> IO ()
271dispC d m = disp m (shfc d)
272-}
273
274-------------------------------------------------------------------
275-- display utilities
276
277{- | Show a matrix with \"autoscaling\" and a given number of decimal places. 252{- | Show a matrix with \"autoscaling\" and a given number of decimal places.
278 253
279@disp = putStr . disps 2 254@disp = putStr . disps 2
@@ -307,9 +282,7 @@ sdims x = show (rows x) ++ "x" ++ show (cols x)
307 282
308formatFixed d x = format " " (printf ("%."++show d++"f")) $ x 283formatFixed d x = format " " (printf ("%."++show d++"f")) $ x
309 284
310isInt = all lookslikeInt . toList . flatten where 285isInt = all lookslikeInt . toList . flatten
311 lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx
312 where shx = show x
313 286
314formatScaled dec t = "E"++show o++"\n" ++ ss 287formatScaled dec t = "E"++show o++"\n" ++ ss
315 where ss = format " " (printf fmt. g) t 288 where ss = format " " (printf fmt. g) t
@@ -333,6 +306,42 @@ vecdisp f v
333 . f . trans . reshape 1 306 . f . trans . reshape 1
334 $ v 307 $ v
335 308
309-- | Tool to display matrices with latex syntax.
310latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc.
311 -> String -- ^ Formatted matrix, with elements separated by spaces and newlines
312 -> String
313latexFormat del tab = "\\begin{"++del++"}\n" ++ f tab ++ "\\end{"++del++"}"
314 where f = unlines . intersperse "\\\\" . map unwords . map (intersperse " & " . words) . tail . lines
315
316-- | Pretty print a complex number with at most n decimal digits.
317showComplex :: Int -> Complex Double -> String
318showComplex d (a:+b)
319 | isZero a && isZero b = "0"
320 | isZero b = sa
321 | isZero a && isOne b = s2++"i"
322 | isZero a = sb++"i"
323 | isOne b = sa++s3++"i"
324 | otherwise = sa++s1++sb++"i"
325 where
326 sa = shcr d a
327 sb = shcr d b
328 s1 = if b<0 then "" else "+"
329 s2 = if b<0 then "-" else ""
330 s3 = if b<0 then "-" else "+"
331
332shcr d a | lookslikeInt a = printf "%.0f" a
333 | otherwise = printf ("%."++show d++"f") a
334
335
336lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx
337 where shx = show x
338
339isZero x = show x `elem` ["0.0","-0.0"]
340isOne x = show x `elem` ["1.0","-1.0"]
341
342-- | Pretty print a complex matrix with with at most n decimal digits.
343dispcf :: Int -> Matrix (Complex Double) -> String
344dispcf d m = sdims m ++ "\n" ++ format " " (showComplex d) m
336 345
337-------------------------------------------------------------------- 346--------------------------------------------------------------------
338 347