summaryrefslogtreecommitdiff
path: root/lib/Data/Packed
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-01-14 13:17:44 +0000
committerAlberto Ruiz <aruiz@um.es>2010-01-14 13:17:44 +0000
commitb8b9e8a91b51e6689a071dbc05f3da857c762e0d (patch)
tree0db5532668c77a7934dd181195aeb84c9f22ddc2 /lib/Data/Packed
parentdfd2cb93e1f6c9fe42ddfd6775ecd1ab7c980ccf (diff)
dispf, disps
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r--lib/Data/Packed/Matrix.hs40
-rw-r--r--lib/Data/Packed/Vector.hs4
2 files changed, 43 insertions, 1 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 6a6942d..7e197d1 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, 32 format, dispf, disps, vecdisp,
33 loadMatrix, saveMatrix, fromFile, fileDimensions, 33 loadMatrix, saveMatrix, fromFile, fileDimensions,
34 readMatrix, fromArray2D 34 readMatrix, fromArray2D
35) where 35) where
@@ -40,6 +40,7 @@ import Data.Packed.Vector
40import Data.List(transpose,intersperse) 40import Data.List(transpose,intersperse)
41import Data.Array 41import Data.Array
42import System.Process(readProcess) 42import System.Process(readProcess)
43import Text.Printf(printf)
43 44
44-- | creates a matrix from a vertical list of matrices 45-- | creates a matrix from a vertical list of matrices
45joinVert :: Element t => [Matrix t] -> Matrix t 46joinVert :: Element t => [Matrix t] -> Matrix t
@@ -245,6 +246,43 @@ dispC :: Int -> Matrix (Complex Double) -> IO ()
245dispC d m = disp m (shfc d) 246dispC d m = disp m (shfc d)
246-} 247-}
247 248
249-------------------------------------------------------------------
250-- display utilities
251
252-- | Print a matrix with \"autoscaling\" and a given number of decimal places.
253disps :: Int -> Matrix Double -> String
254disps d x = sdims x ++ " " ++ formatScaled d x
255
256-- | Print a matrix with a given number of decimal places.
257dispf :: Int -> Matrix Double -> String
258dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x
259
260sdims x = show (rows x) ++ "x" ++ show (cols x)
261
262formatFixed d x = format " " (printf ("%."++show d++"f")) $ x
263
264isInt = all lookslikeInt . toList . flatten where
265 lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx
266 where shx = show x
267
268formatScaled dec t = "E"++show o++"\n" ++ ss
269 where ss = format " " (printf fmt. g) t
270 g x | o >= 0 = x/10^(o::Int)
271 | otherwise = x*10^(-o)
272 o = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t
273 fmt = '%':show (dec+3) ++ '.':show dec ++"f"
274
275-- | Print a vector using a function for printing matrices.
276vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String
277vecdisp f v
278 = ((show (dim v) ++ " |> ") ++) . (++"\n")
279 . unwords . lines . tail . dropWhile (not . (`elem` " \n"))
280 . f . trans . reshape 1
281 $ v
282
283
284--------------------------------------------------------------------
285
248-- | reads a matrix from a string containing a table of numbers. 286-- | reads a matrix from a string containing a table of numbers.
249readMatrix :: String -> Matrix Double 287readMatrix :: String -> Matrix Double
250readMatrix = fromLists . map (map read). map words . filter (not.null) . lines 288readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index 457da71..ed0e49c 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -32,6 +32,10 @@ import Numeric.GSL.Vector
32 32
33@\> linspace 5 (-3,7) 33@\> linspace 5 (-3,7)
345 |> [-3.0,-0.5,2.0,4.5,7.0]@ 345 |> [-3.0,-0.5,2.0,4.5,7.0]@
35
36Logarithmic spacing can be defined as follows:
37
38@logspace n (a,b) = 10 ** linspace n (a,b)@
35-} 39-}
36linspace :: Int -> (Double, Double) -> Vector Double 40linspace :: Int -> (Double, Double) -> Vector Double
37linspace n (a,b) = add a $ scale s $ fromList [0 .. fromIntegral n-1] 41linspace n (a,b) = add a $ scale s $ fromList [0 .. fromIntegral n-1]