diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-01-14 13:17:44 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-01-14 13:17:44 +0000 |
commit | b8b9e8a91b51e6689a071dbc05f3da857c762e0d (patch) | |
tree | 0db5532668c77a7934dd181195aeb84c9f22ddc2 | |
parent | dfd2cb93e1f6c9fe42ddfd6775ecd1ab7c980ccf (diff) |
dispf, disps
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | hmatrix.cabal | 8 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 40 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 4 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests.hs | 11 |
6 files changed, 65 insertions, 5 deletions
@@ -1,3 +1,8 @@ | |||
1 | 0.8.2.0 | ||
2 | ======= | ||
3 | |||
4 | - display utilities: dispf, disps | ||
5 | |||
1 | 0.8.1.0 | 6 | 0.8.1.0 |
2 | ======= | 7 | ======= |
3 | 8 | ||
@@ -106,3 +106,5 @@ in the Haskell mailing lists for their help. | |||
106 | - Erik de Castro Lopo added buildVector and buildMatrix, which take a | 106 | - Erik de Castro Lopo added buildVector and buildMatrix, which take a |
107 | size parameter(s) and a function that maps vector/matrix indices | 107 | size parameter(s) and a function that maps vector/matrix indices |
108 | to the values at that position. | 108 | to the values at that position. |
109 | |||
110 | - Jean-Francois Tremblay discovered an error in the tutorial. | ||
diff --git a/hmatrix.cabal b/hmatrix.cabal index 76ead58..af726ad 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal | |||
@@ -1,5 +1,5 @@ | |||
1 | Name: hmatrix | 1 | Name: hmatrix |
2 | Version: 0.8.1.1 | 2 | Version: 0.8.2.0 |
3 | License: GPL | 3 | License: GPL |
4 | License-file: LICENSE | 4 | License-file: LICENSE |
5 | Author: Alberto Ruiz | 5 | Author: Alberto Ruiz |
@@ -64,14 +64,14 @@ flag unsafe | |||
64 | 64 | ||
65 | library | 65 | library |
66 | if flag(splitBase) | 66 | if flag(splitBase) |
67 | build-depends: base >= 3 && < 5, array < 0.3.0 | 67 | build-depends: base >= 3 && < 5, array |
68 | else | 68 | else |
69 | build-depends: base < 3 | 69 | build-depends: base < 3 |
70 | 70 | ||
71 | Build-Depends: haskell98 < 1.0.1.1, | 71 | Build-Depends: haskell98, |
72 | QuickCheck, HUnit, | 72 | QuickCheck, HUnit, |
73 | storable-complex, | 73 | storable-complex, |
74 | process < 1.0.1.2 | 74 | process |
75 | 75 | ||
76 | Extensions: ForeignFunctionInterface, | 76 | Extensions: ForeignFunctionInterface, |
77 | CPP | 77 | CPP |
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 | |||
40 | import Data.List(transpose,intersperse) | 40 | import Data.List(transpose,intersperse) |
41 | import Data.Array | 41 | import Data.Array |
42 | import System.Process(readProcess) | 42 | import System.Process(readProcess) |
43 | import 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 |
45 | joinVert :: Element t => [Matrix t] -> Matrix t | 46 | joinVert :: Element t => [Matrix t] -> Matrix t |
@@ -245,6 +246,43 @@ dispC :: Int -> Matrix (Complex Double) -> IO () | |||
245 | dispC d m = disp m (shfc d) | 246 | dispC 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. | ||
253 | disps :: Int -> Matrix Double -> String | ||
254 | disps d x = sdims x ++ " " ++ formatScaled d x | ||
255 | |||
256 | -- | Print a matrix with a given number of decimal places. | ||
257 | dispf :: Int -> Matrix Double -> String | ||
258 | dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x | ||
259 | |||
260 | sdims x = show (rows x) ++ "x" ++ show (cols x) | ||
261 | |||
262 | formatFixed d x = format " " (printf ("%."++show d++"f")) $ x | ||
263 | |||
264 | isInt = all lookslikeInt . toList . flatten where | ||
265 | lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx | ||
266 | where shx = show x | ||
267 | |||
268 | formatScaled 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. | ||
276 | vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String | ||
277 | vecdisp 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. |
249 | readMatrix :: String -> Matrix Double | 287 | readMatrix :: String -> Matrix Double |
250 | readMatrix = fromLists . map (map read). map words . filter (not.null) . lines | 288 | readMatrix = 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) |
34 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ | 34 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ |
35 | |||
36 | Logarithmic spacing can be defined as follows: | ||
37 | |||
38 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | ||
35 | -} | 39 | -} |
36 | linspace :: Int -> (Double, Double) -> Vector Double | 40 | linspace :: Int -> (Double, Double) -> Vector Double |
37 | linspace n (a,b) = add a $ scale s $ fromList [0 .. fromIntegral n-1] | 41 | linspace n (a,b) = add a $ scale s $ fromList [0 .. fromIntegral n-1] |
diff --git a/lib/Numeric/LinearAlgebra/Tests.hs b/lib/Numeric/LinearAlgebra/Tests.hs index 9557ac3..063ae86 100644 --- a/lib/Numeric/LinearAlgebra/Tests.hs +++ b/lib/Numeric/LinearAlgebra/Tests.hs | |||
@@ -321,6 +321,8 @@ manymult n = foldl1' (<>) (map rot2 angles) where | |||
321 | where c = cos a | 321 | where c = cos a |
322 | s = sin a | 322 | s = sin a |
323 | 323 | ||
324 | multb n = foldl1' (<>) (replicate (10^6) (ident n :: Matrix Double)) | ||
325 | |||
324 | -------------------------------- | 326 | -------------------------------- |
325 | 327 | ||
326 | multBench = do | 328 | multBench = do |
@@ -328,6 +330,15 @@ multBench = do | |||
328 | let b = ident 2000 :: Matrix Double | 330 | let b = ident 2000 :: Matrix Double |
329 | a `seq` b `seq` putStrLn "" | 331 | a `seq` b `seq` putStrLn "" |
330 | time "product of 1M different 3x3 matrices" (manymult (10^6)) | 332 | time "product of 1M different 3x3 matrices" (manymult (10^6)) |
333 | putStrLn "" | ||
334 | time "product of 1M constant 1x1 matrices" (multb 1) | ||
335 | time "product of 1M constant 3x3 matrices" (multb 3) | ||
336 | --time "product of 1M constant 5x5 matrices" (multb 5) | ||
337 | time "product of 1M const. 10x10 matrices" (multb 10) | ||
338 | --time "product of 1M const. 15x15 matrices" (multb 15) | ||
339 | time "product of 1M const. 20x20 matrices" (multb 20) | ||
340 | --time "product of 1M const. 25x25 matrices" (multb 25) | ||
341 | putStrLn "" | ||
331 | time "product (1000 x 1000)<>(1000 x 1000)" (a<>a) | 342 | time "product (1000 x 1000)<>(1000 x 1000)" (a<>a) |
332 | time "product (2000 x 2000)<>(2000 x 2000)" (b<>b) | 343 | time "product (2000 x 2000)<>(2000 x 2000)" (b<>b) |
333 | 344 | ||