summaryrefslogtreecommitdiff
path: root/packages/base/src/Data
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-16 13:35:35 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-16 13:35:35 +0200
commit1838c4248679b7476bb8716a76171712dc3cd335 (patch)
treee3909ac3354eaf917bb1ebc5a7366412b6ab8f0f /packages/base/src/Data
parenta2d99e7d0e83fcedf3a856cdb927309e28a8eddd (diff)
linear algebra moved
Diffstat (limited to 'packages/base/src/Data')
-rw-r--r--packages/base/src/Data/Packed/IO.hs141
1 files changed, 141 insertions, 0 deletions
diff --git a/packages/base/src/Data/Packed/IO.hs b/packages/base/src/Data/Packed/IO.hs
new file mode 100644
index 0000000..dbb2943
--- /dev/null
+++ b/packages/base/src/Data/Packed/IO.hs
@@ -0,0 +1,141 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Data.Packed.IO
4-- Copyright : (c) Alberto Ruiz 2010
5-- License : BSD3
6--
7-- Maintainer : Alberto Ruiz
8-- Stability : provisional
9--
10-- Display, formatting and IO functions for numeric 'Vector' and 'Matrix'
11--
12-----------------------------------------------------------------------------
13{-# OPTIONS_HADDOCK hide #-}
14
15module Data.Packed.IO (
16 dispf, disps, dispcf, vecdisp, latexFormat, format,
17 readMatrix, fromArray2D
18) where
19
20import Data.Packed
21import Data.Packed.Development
22import Text.Printf(printf)
23import Data.List(intersperse)
24import Data.Complex
25
26{- | Creates a string from a matrix given a separator and a function to show each entry. Using
27this function the user can easily define any desired display function:
28
29@import Text.Printf(printf)@
30
31@disp = putStr . format \" \" (printf \"%.2f\")@
32
33-}
34format :: (Element t) => String -> (t -> String) -> Matrix t -> String
35format sep f m = table sep . map (map f) . toLists $ m
36
37{- | Show a matrix with \"autoscaling\" and a given number of decimal places.
38
39>>> putStr . disps 2 $ 120 * (3><4) [1..]
403x4 E3
41 0.12 0.24 0.36 0.48
42 0.60 0.72 0.84 0.96
43 1.08 1.20 1.32 1.44
44
45-}
46disps :: Int -> Matrix Double -> String
47disps d x = sdims x ++ " " ++ formatScaled d x
48
49{- | Show a matrix with a given number of decimal places.
50
51>>> dispf 2 (1/3 + ident 3)
52"3x3\n1.33 0.33 0.33\n0.33 1.33 0.33\n0.33 0.33 1.33\n"
53
54>>> putStr . dispf 2 $ (3><4)[1,1.5..]
553x4
561.00 1.50 2.00 2.50
573.00 3.50 4.00 4.50
585.00 5.50 6.00 6.50
59
60>>> putStr . unlines . tail . lines . dispf 2 . asRow $ linspace 10 (0,1)
610.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00
62
63-}
64dispf :: Int -> Matrix Double -> String
65dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x
66
67sdims x = show (rows x) ++ "x" ++ show (cols x)
68
69formatFixed d x = format " " (printf ("%."++show d++"f")) $ x
70
71isInt = all lookslikeInt . toList . flatten
72
73formatScaled dec t = "E"++show o++"\n" ++ ss
74 where ss = format " " (printf fmt. g) t
75 g x | o >= 0 = x/10^(o::Int)
76 | otherwise = x*10^(-o)
77 o | rows t == 0 || cols t == 0 = 0
78 | otherwise = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t
79 fmt = '%':show (dec+3) ++ '.':show dec ++"f"
80
81{- | Show a vector using a function for showing matrices.
82
83>>> putStr . vecdisp (dispf 2) $ linspace 10 (0,1)
8410 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00
85
86-}
87vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String
88vecdisp f v
89 = ((show (dim v) ++ " |> ") ++) . (++"\n")
90 . unwords . lines . tail . dropWhile (not . (`elem` " \n"))
91 . f . trans . reshape 1
92 $ v
93
94{- | Tool to display matrices with latex syntax.
95
96>>> latexFormat "bmatrix" (dispf 2 $ ident 2)
97"\\begin{bmatrix}\n1 & 0\n\\\\\n0 & 1\n\\end{bmatrix}"
98
99-}
100latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc.
101 -> String -- ^ Formatted matrix, with elements separated by spaces and newlines
102 -> String
103latexFormat del tab = "\\begin{"++del++"}\n" ++ f tab ++ "\\end{"++del++"}"
104 where f = unlines . intersperse "\\\\" . map unwords . map (intersperse " & " . words) . tail . lines
105
106-- | Pretty print a complex number with at most n decimal digits.
107showComplex :: Int -> Complex Double -> String
108showComplex d (a:+b)
109 | isZero a && isZero b = "0"
110 | isZero b = sa
111 | isZero a && isOne b = s2++"i"
112 | isZero a = sb++"i"
113 | isOne b = sa++s3++"i"
114 | otherwise = sa++s1++sb++"i"
115 where
116 sa = shcr d a
117 sb = shcr d b
118 s1 = if b<0 then "" else "+"
119 s2 = if b<0 then "-" else ""
120 s3 = if b<0 then "-" else "+"
121
122shcr d a | lookslikeInt a = printf "%.0f" a
123 | otherwise = printf ("%."++show d++"f") a
124
125
126lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx
127 where shx = show x
128
129isZero x = show x `elem` ["0.0","-0.0"]
130isOne x = show x `elem` ["1.0","-1.0"]
131
132-- | Pretty print a complex matrix with at most n decimal digits.
133dispcf :: Int -> Matrix (Complex Double) -> String
134dispcf d m = sdims m ++ "\n" ++ format " " (showComplex d) m
135
136--------------------------------------------------------------------
137
138-- | reads a matrix from a string containing a table of numbers.
139readMatrix :: String -> Matrix Double
140readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
141