diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-01-16 18:17:53 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-01-16 18:17:53 +0000 |
commit | 3fb3a81e63540497a81b717c615f711ce031c76f (patch) | |
tree | a2f4b09636df3ca904b328b35c140ab4c22be093 /lib/Data/Packed | |
parent | b8b9e8a91b51e6689a071dbc05f3da857c762e0d (diff) |
vecdisp, scalar, docs
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 38 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 17 |
2 files changed, 50 insertions, 5 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 7e197d1..fc988af 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -29,7 +29,7 @@ module Data.Packed.Matrix ( | |||
29 | extractRows, | 29 | extractRows, |
30 | ident, diag, diagRect, takeDiag, | 30 | ident, diag, diagRect, takeDiag, |
31 | liftMatrix, liftMatrix2, | 31 | liftMatrix, liftMatrix2, |
32 | format, dispf, disps, vecdisp, | 32 | format, dispf, disps, |
33 | loadMatrix, saveMatrix, fromFile, fileDimensions, | 33 | loadMatrix, saveMatrix, fromFile, fileDimensions, |
34 | readMatrix, fromArray2D | 34 | readMatrix, fromArray2D |
35 | ) where | 35 | ) where |
@@ -230,7 +230,7 @@ this function the user can easily define any desired display function: | |||
230 | 230 | ||
231 | @import Text.Printf(printf)@ | 231 | @import Text.Printf(printf)@ |
232 | 232 | ||
233 | @disp = putStrLn . format \" \" (printf \"%.2f\")@ | 233 | @disp = putStr . format \" \" (printf \"%.2f\")@ |
234 | 234 | ||
235 | -} | 235 | -} |
236 | format :: (Element t) => String -> (t -> String) -> Matrix t -> String | 236 | format :: (Element t) => String -> (t -> String) -> Matrix t -> String |
@@ -249,11 +249,32 @@ dispC d m = disp m (shfc d) | |||
249 | ------------------------------------------------------------------- | 249 | ------------------------------------------------------------------- |
250 | -- display utilities | 250 | -- display utilities |
251 | 251 | ||
252 | -- | Print a matrix with \"autoscaling\" and a given number of decimal places. | 252 | {- | Show a matrix with \"autoscaling\" and a given number of decimal places. |
253 | |||
254 | @disp = putStr . disps 2 | ||
255 | |||
256 | \> disp $ 120 * (3><4) [1..] | ||
257 | 3x4 E3 | ||
258 | 0.12 0.24 0.36 0.48 | ||
259 | 0.60 0.72 0.84 0.96 | ||
260 | 1.08 1.20 1.32 1.44 | ||
261 | @ | ||
262 | -} | ||
253 | disps :: Int -> Matrix Double -> String | 263 | disps :: Int -> Matrix Double -> String |
254 | disps d x = sdims x ++ " " ++ formatScaled d x | 264 | disps d x = sdims x ++ " " ++ formatScaled d x |
255 | 265 | ||
256 | -- | Print a matrix with a given number of decimal places. | 266 | {- | Show a matrix with a given number of decimal places. |
267 | |||
268 | @disp = putStr . dispf 3 | ||
269 | |||
270 | \> disp (1/3 + ident 4) | ||
271 | 4x4 | ||
272 | 1.333 0.333 0.333 0.333 | ||
273 | 0.333 1.333 0.333 0.333 | ||
274 | 0.333 0.333 1.333 0.333 | ||
275 | 0.333 0.333 0.333 1.333 | ||
276 | @ | ||
277 | -} | ||
257 | dispf :: Int -> Matrix Double -> String | 278 | dispf :: Int -> Matrix Double -> String |
258 | dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x | 279 | dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x |
259 | 280 | ||
@@ -272,7 +293,14 @@ formatScaled dec t = "E"++show o++"\n" ++ ss | |||
272 | o = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t | 293 | o = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t |
273 | fmt = '%':show (dec+3) ++ '.':show dec ++"f" | 294 | fmt = '%':show (dec+3) ++ '.':show dec ++"f" |
274 | 295 | ||
275 | -- | Print a vector using a function for printing matrices. | 296 | {- | Show a vector using a function for showing matrices. |
297 | |||
298 | @disp = putStr . vecdisp (dispf 2) | ||
299 | |||
300 | \> disp (linspace 10 (0,1)) | ||
301 | 10 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 | ||
302 | @ | ||
303 | -} | ||
276 | vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String | 304 | vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String |
277 | vecdisp f v | 305 | vecdisp f v |
278 | = ((show (dim v) ++ " |> ") ++) . (++"\n") | 306 | = ((show (dim v) ++ " |> ") ++) . (++"\n") |
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index ed0e49c..6f1096f 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs | |||
@@ -18,6 +18,7 @@ module Data.Packed.Vector ( | |||
18 | dim, (@>), | 18 | dim, (@>), |
19 | subVector, join, | 19 | subVector, join, |
20 | constant, linspace, | 20 | constant, linspace, |
21 | vecdisp, | ||
21 | vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, | 22 | vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, |
22 | mapVector, zipVector, | 23 | mapVector, zipVector, |
23 | fscanfVector, fprintfVector, freadVector, fwriteVector, | 24 | fscanfVector, fprintfVector, freadVector, fwriteVector, |
@@ -74,3 +75,19 @@ constant = constantD -- about 2x faster | |||
74 | buildVector :: Element a => Int -> (Int -> a) -> Vector a | 75 | buildVector :: Element a => Int -> (Int -> a) -> Vector a |
75 | buildVector len f = | 76 | buildVector len f = |
76 | fromList $ map f [0 .. (len - 1)] | 77 | fromList $ map f [0 .. (len - 1)] |
78 | |||
79 | |||
80 | {- | Show a vector using a function for showing matrices. | ||
81 | |||
82 | @disp = putStr . vecdisp ('dispf' 2) | ||
83 | |||
84 | \> disp ('linspace' 10 (0,1)) | ||
85 | 10 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 | ||
86 | @ | ||
87 | -} | ||
88 | vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String | ||
89 | vecdisp f v | ||
90 | = ((show (dim v) ++ " |> ") ++) . (++"\n") | ||
91 | . unwords . lines . tail . dropWhile (not . (`elem` " \n")) | ||
92 | . f . trans . reshape 1 | ||
93 | $ v | ||