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