summaryrefslogtreecommitdiff
path: root/packages/base/src/Data/Packed/IO.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Data/Packed/IO.hs')
-rw-r--r--packages/base/src/Data/Packed/IO.hs166
1 files changed, 0 insertions, 166 deletions
diff --git a/packages/base/src/Data/Packed/IO.hs b/packages/base/src/Data/Packed/IO.hs
deleted file mode 100644
index b0999a8..0000000
--- a/packages/base/src/Data/Packed/IO.hs
+++ /dev/null
@@ -1,166 +0,0 @@
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, loadMatrix, loadMatrix', saveMatrix
18) where
19
20import Data.Packed
21import Text.Printf(printf)
22import Data.List(intersperse)
23import Data.Complex
24import Numeric.Vectorized(vectorScan,saveMatrix)
25import Data.Packed.Internal
26
27{- | Creates a string from a matrix given a separator and a function to show each entry. Using
28this function the user can easily define any desired display function:
29
30@import Text.Printf(printf)@
31
32@disp = putStr . format \" \" (printf \"%.2f\")@
33
34-}
35format :: (Element t) => String -> (t -> String) -> Matrix t -> String
36format sep f m = table sep . map (map f) . toLists $ m
37
38{- | Show a matrix with \"autoscaling\" and a given number of decimal places.
39
40>>> putStr . disps 2 $ 120 * (3><4) [1..]
413x4 E3
42 0.12 0.24 0.36 0.48
43 0.60 0.72 0.84 0.96
44 1.08 1.20 1.32 1.44
45
46-}
47disps :: Int -> Matrix Double -> String
48disps d x = sdims x ++ " " ++ formatScaled d x
49
50{- | Show a matrix with a given number of decimal places.
51
52>>> dispf 2 (1/3 + ident 3)
53"3x3\n1.33 0.33 0.33\n0.33 1.33 0.33\n0.33 0.33 1.33\n"
54
55>>> putStr . dispf 2 $ (3><4)[1,1.5..]
563x4
571.00 1.50 2.00 2.50
583.00 3.50 4.00 4.50
595.00 5.50 6.00 6.50
60
61>>> putStr . unlines . tail . lines . dispf 2 . asRow $ linspace 10 (0,1)
620.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00
63
64-}
65dispf :: Int -> Matrix Double -> String
66dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x
67
68sdims x = show (rows x) ++ "x" ++ show (cols x)
69
70formatFixed d x = format " " (printf ("%."++show d++"f")) $ x
71
72isInt = all lookslikeInt . toList . flatten
73
74formatScaled dec t = "E"++show o++"\n" ++ ss
75 where ss = format " " (printf fmt. g) t
76 g x | o >= 0 = x/10^(o::Int)
77 | otherwise = x*10^(-o)
78 o | rows t == 0 || cols t == 0 = 0
79 | otherwise = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t
80 fmt = '%':show (dec+3) ++ '.':show dec ++"f"
81
82{- | Show a vector using a function for showing matrices.
83
84>>> putStr . vecdisp (dispf 2) $ linspace 10 (0,1)
8510 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00
86
87-}
88vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String
89vecdisp f v
90 = ((show (dim v) ++ " |> ") ++) . (++"\n")
91 . unwords . lines . tail . dropWhile (not . (`elem` " \n"))
92 . f . trans . reshape 1
93 $ v
94
95{- | Tool to display matrices with latex syntax.
96
97>>> latexFormat "bmatrix" (dispf 2 $ ident 2)
98"\\begin{bmatrix}\n1 & 0\n\\\\\n0 & 1\n\\end{bmatrix}"
99
100-}
101latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc.
102 -> String -- ^ Formatted matrix, with elements separated by spaces and newlines
103 -> String
104latexFormat del tab = "\\begin{"++del++"}\n" ++ f tab ++ "\\end{"++del++"}"
105 where f = unlines . intersperse "\\\\" . map unwords . map (intersperse " & " . words) . tail . lines
106
107-- | Pretty print a complex number with at most n decimal digits.
108showComplex :: Int -> Complex Double -> String
109showComplex d (a:+b)
110 | isZero a && isZero b = "0"
111 | isZero b = sa
112 | isZero a && isOne b = s2++"i"
113 | isZero a = sb++"i"
114 | isOne b = sa++s3++"i"
115 | otherwise = sa++s1++sb++"i"
116 where
117 sa = shcr d a
118 sb = shcr d b
119 s1 = if b<0 then "" else "+"
120 s2 = if b<0 then "-" else ""
121 s3 = if b<0 then "-" else "+"
122
123shcr d a | lookslikeInt a = printf "%.0f" a
124 | otherwise = printf ("%."++show d++"f") a
125
126
127lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx
128 where shx = show x
129
130isZero x = show x `elem` ["0.0","-0.0"]
131isOne x = show x `elem` ["1.0","-1.0"]
132
133-- | Pretty print a complex matrix with at most n decimal digits.
134dispcf :: Int -> Matrix (Complex Double) -> String
135dispcf d m = sdims m ++ "\n" ++ format " " (showComplex d) m
136
137--------------------------------------------------------------------
138
139-- | reads a matrix from a string containing a table of numbers.
140readMatrix :: String -> Matrix Double
141readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
142
143--------------------------------------------------------------------------------
144
145apparentCols :: FilePath -> IO Int
146apparentCols s = f . dropWhile null . map words . lines <$> readFile s
147 where
148 f [] = 0
149 f (x:_) = length x
150
151
152-- | load a matrix from an ASCII file formatted as a 2D table.
153loadMatrix :: FilePath -> IO (Matrix Double)
154loadMatrix f = do
155 v <- vectorScan f
156 c <- apparentCols f
157 if (dim v `mod` c /= 0)
158 then
159 error $ printf "loadMatrix: %d elements and %d columns in file %s"
160 (dim v) c f
161 else
162 return (reshape c v)
163
164
165loadMatrix' name = mbCatch (loadMatrix name)
166