From 3fb3a81e63540497a81b717c615f711ce031c76f Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Sat, 16 Jan 2010 18:17:53 +0000 Subject: vecdisp, scalar, docs --- lib/Data/Packed.hs | 5 +++++ lib/Data/Packed/Matrix.hs | 38 +++++++++++++++++++++++++++++++++----- lib/Data/Packed/Vector.hs | 17 +++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) (limited to 'lib/Data') diff --git a/lib/Data/Packed.hs b/lib/Data/Packed.hs index 7d6d200..7ffdd0b 100644 --- a/lib/Data/Packed.hs +++ b/lib/Data/Packed.hs @@ -34,6 +34,7 @@ class (Element e) => Container c e where conj :: RealFloat e => c (Complex e) -> c (Complex e) real :: c Double -> c e complex :: c e -> c (Complex Double) + scalar :: e -> c e instance Container Vector Double where toComplex = Data.Packed.Internal.toComplex @@ -42,6 +43,7 @@ instance Container Vector Double where conj = Data.Packed.Internal.conj real = id complex = Data.Packed.comp + scalar x = fromList [x] instance Container Vector (Complex Double) where toComplex = undefined -- can't match @@ -50,6 +52,7 @@ instance Container Vector (Complex Double) where conj = undefined real = Data.Packed.comp complex = id + scalar x = fromList [x] instance Container Matrix Double where toComplex = uncurry $ liftMatrix2 $ curry Data.Packed.toComplex @@ -60,6 +63,7 @@ instance Container Matrix Double where conj = liftMatrix Data.Packed.Internal.conj real = id complex = Data.Packed.comp + scalar x = (1><1) [x] instance Container Matrix (Complex Double) where toComplex = undefined @@ -68,6 +72,7 @@ instance Container Matrix (Complex Double) where conj = undefined real = Data.Packed.comp complex = id + scalar x = (1><1) [x] -- | converts a real vector into a complex representation (with zero imaginary parts) 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 ( extractRows, ident, diag, diagRect, takeDiag, liftMatrix, liftMatrix2, - format, dispf, disps, vecdisp, + format, dispf, disps, loadMatrix, saveMatrix, fromFile, fileDimensions, readMatrix, fromArray2D ) where @@ -230,7 +230,7 @@ this function the user can easily define any desired display function: @import Text.Printf(printf)@ -@disp = putStrLn . format \" \" (printf \"%.2f\")@ +@disp = putStr . format \" \" (printf \"%.2f\")@ -} format :: (Element t) => String -> (t -> String) -> Matrix t -> String @@ -249,11 +249,32 @@ dispC d m = disp m (shfc d) ------------------------------------------------------------------- -- display utilities --- | Print a matrix with \"autoscaling\" and a given number of decimal places. +{- | Show a matrix with \"autoscaling\" and a given number of decimal places. + +@disp = putStr . disps 2 + +\> disp $ 120 * (3><4) [1..] +3x4 E3 + 0.12 0.24 0.36 0.48 + 0.60 0.72 0.84 0.96 + 1.08 1.20 1.32 1.44 +@ +-} disps :: Int -> Matrix Double -> String disps d x = sdims x ++ " " ++ formatScaled d x --- | Print a matrix with a given number of decimal places. +{- | Show a matrix with a given number of decimal places. + +@disp = putStr . dispf 3 + +\> disp (1/3 + ident 4) +4x4 +1.333 0.333 0.333 0.333 +0.333 1.333 0.333 0.333 +0.333 0.333 1.333 0.333 +0.333 0.333 0.333 1.333 +@ +-} dispf :: Int -> Matrix Double -> String dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x @@ -272,7 +293,14 @@ formatScaled dec t = "E"++show o++"\n" ++ ss o = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t fmt = '%':show (dec+3) ++ '.':show dec ++"f" --- | Print a vector using a function for printing matrices. +{- | Show a vector using a function for showing matrices. + +@disp = putStr . vecdisp (dispf 2) + +\> disp (linspace 10 (0,1)) +10 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 +@ +-} vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String vecdisp f v = ((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 ( dim, (@>), subVector, join, constant, linspace, + vecdisp, vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, mapVector, zipVector, fscanfVector, fprintfVector, freadVector, fwriteVector, @@ -74,3 +75,19 @@ constant = constantD -- about 2x faster buildVector :: Element a => Int -> (Int -> a) -> Vector a buildVector len f = fromList $ map f [0 .. (len - 1)] + + +{- | Show a vector using a function for showing matrices. + +@disp = putStr . vecdisp ('dispf' 2) + +\> disp ('linspace' 10 (0,1)) +10 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 +@ +-} +vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String +vecdisp f v + = ((show (dim v) ++ " |> ") ++) . (++"\n") + . unwords . lines . tail . dropWhile (not . (`elem` " \n")) + . f . trans . reshape 1 + $ v -- cgit v1.2.3