summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Data/Packed/Matrix.hs75
-rw-r--r--lib/Numeric/LinearAlgebra/Util.hs12
2 files changed, 43 insertions, 44 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
diff --git a/lib/Numeric/LinearAlgebra/Util.hs b/lib/Numeric/LinearAlgebra/Util.hs
index 92b66e3..24317e3 100644
--- a/lib/Numeric/LinearAlgebra/Util.hs
+++ b/lib/Numeric/LinearAlgebra/Util.hs
@@ -25,15 +25,13 @@ module Numeric.LinearAlgebra.Util(
25 (#),(&), (//), blocks, 25 (#),(&), (//), blocks,
26 rand, 26 rand,
27 splitEvery, 27 splitEvery,
28 table, 28 table
29 latexFormat
30) where 29) where
31 30
32import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/)) 31import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/))
33import Data.Packed.Internal.Common(table,splitEvery) 32import Data.Packed.Internal.Common(table,splitEvery)
34import System.Random(randomIO) 33import System.Random(randomIO)
35 34
36
37infixl 7 <> 35infixl 7 <>
38-- | Matrix product ('multiply') 36-- | Matrix product ('multiply')
39(<>) :: Field t => Matrix t -> Matrix t -> Matrix t 37(<>) :: Field t => Matrix t -> Matrix t -> Matrix t
@@ -121,12 +119,4 @@ row = asRow . vector
121col :: [Double] -> Matrix Double 119col :: [Double] -> Matrix Double
122col = asColumn . vector 120col = asColumn . vector
123 121
124-- | Tool to display matrices with latex syntax.
125latexFormat :: Element t
126 => String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc.
127 -> (t->String) -- ^ formatting function for the elements: (printf \"%.2f\"), etc.
128 -> Matrix t -- ^ input matrix
129 -> String
130latexFormat t shf m = "\\begin{"++t++"}\n" ++ dispg " & " " \\\\" shf m ++ "\\end{"++t++"}"
131 where dispg w l shfun x = unlines $ map (++l) $ lines $ format w shfun x
132 122