diff options
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 75 |
1 files changed, 42 insertions, 33 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index a8e998f..dced49f 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -28,7 +28,7 @@ module Data.Packed.Matrix ( | |||
28 | extractRows, | 28 | extractRows, |
29 | ident, diag, diagRect, takeDiag, | 29 | ident, diag, diagRect, takeDiag, |
30 | liftMatrix, liftMatrix2, liftMatrix2Auto, | 30 | liftMatrix, liftMatrix2, liftMatrix2Auto, |
31 | format, dispf, disps, | 31 | dispf, disps, dispcf, showComplex, latexFormat, format, |
32 | loadMatrix, saveMatrix, fromFile, fileDimensions, | 32 | loadMatrix, saveMatrix, fromFile, fileDimensions, |
33 | readMatrix, fromArray2D | 33 | readMatrix, fromArray2D |
34 | ) where | 34 | ) where |
@@ -39,7 +39,8 @@ import Data.Packed.Vector | |||
39 | import Data.Array | 39 | import Data.Array |
40 | import System.Process(readProcess) | 40 | import System.Process(readProcess) |
41 | import Text.Printf(printf) | 41 | import Text.Printf(printf) |
42 | import Data.List(transpose) | 42 | import Data.List(transpose,intersperse) |
43 | import Data.Complex | ||
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 |
@@ -232,22 +233,9 @@ fromArray2D m = (r><c) (elems m) | |||
232 | r = r1-r0+1 | 233 | r = r1-r0+1 |
233 | c = c1-c0+1 | 234 | c = c1-c0+1 |
234 | 235 | ||
235 | ------------------------------------------------------ | ||
236 | {- | ||
237 | -- shows a Double with n digits after the decimal point | ||
238 | shf :: (RealFloat a) => Int -> a -> String | ||
239 | shf dec n | abs n < 1e-10 = "0." | ||
240 | | abs (n - (fromIntegral.round $ n)) < 1e-10 = show (round n) ++"." | ||
241 | | otherwise = showGFloat (Just dec) n "" | ||
242 | -- shows a Complex Double as a pair, with n digits after the decimal point | ||
243 | shfc n z@ (a:+b) | ||
244 | | magnitude z <1e-10 = "0." | ||
245 | | abs b < 1e-10 = shf n a | ||
246 | | abs a < 1e-10 = shf n b ++"i" | ||
247 | | b > 0 = shf n a ++"+"++shf n b ++"i" | ||
248 | | otherwise = shf n a ++shf n b ++"i" | ||
249 | 236 | ||
250 | -} | 237 | ------------------------------------------------------------------- |
238 | -- display utilities | ||
251 | 239 | ||
252 | 240 | ||
253 | {- | Creates a string from a matrix given a separator and a function to show each entry. Using | 241 | {- | Creates a string from a matrix given a separator and a function to show each entry. Using |
@@ -261,19 +249,6 @@ this function the user can easily define any desired display function: | |||
261 | format :: (Element t) => String -> (t -> String) -> Matrix t -> String | 249 | format :: (Element t) => String -> (t -> String) -> Matrix t -> String |
262 | format sep f m = table sep . map (map f) . toLists $ m | 250 | format sep f m = table sep . map (map f) . toLists $ m |
263 | 251 | ||
264 | {- | ||
265 | disp m f = putStrLn $ "matrix ("++show (rows m) ++"x"++ show (cols m) ++")\n"++format " | " f m | ||
266 | |||
267 | dispR :: Int -> Matrix Double -> IO () | ||
268 | dispR d m = disp m (shf d) | ||
269 | |||
270 | dispC :: Int -> Matrix (Complex Double) -> IO () | ||
271 | dispC d m = disp m (shfc d) | ||
272 | -} | ||
273 | |||
274 | ------------------------------------------------------------------- | ||
275 | -- display utilities | ||
276 | |||
277 | {- | Show a matrix with \"autoscaling\" and a given number of decimal places. | 252 | {- | Show a matrix with \"autoscaling\" and a given number of decimal places. |
278 | 253 | ||
279 | @disp = putStr . disps 2 | 254 | @disp = putStr . disps 2 |
@@ -307,9 +282,7 @@ sdims x = show (rows x) ++ "x" ++ show (cols x) | |||
307 | 282 | ||
308 | formatFixed d x = format " " (printf ("%."++show d++"f")) $ x | 283 | formatFixed d x = format " " (printf ("%."++show d++"f")) $ x |
309 | 284 | ||
310 | isInt = all lookslikeInt . toList . flatten where | 285 | isInt = all lookslikeInt . toList . flatten |
311 | lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx | ||
312 | where shx = show x | ||
313 | 286 | ||
314 | formatScaled dec t = "E"++show o++"\n" ++ ss | 287 | formatScaled dec t = "E"++show o++"\n" ++ ss |
315 | where ss = format " " (printf fmt. g) t | 288 | where ss = format " " (printf fmt. g) t |
@@ -333,6 +306,42 @@ vecdisp f v | |||
333 | . f . trans . reshape 1 | 306 | . f . trans . reshape 1 |
334 | $ v | 307 | $ v |
335 | 308 | ||
309 | -- | Tool to display matrices with latex syntax. | ||
310 | latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc. | ||
311 | -> String -- ^ Formatted matrix, with elements separated by spaces and newlines | ||
312 | -> String | ||
313 | latexFormat del tab = "\\begin{"++del++"}\n" ++ f tab ++ "\\end{"++del++"}" | ||
314 | where f = unlines . intersperse "\\\\" . map unwords . map (intersperse " & " . words) . tail . lines | ||
315 | |||
316 | -- | Pretty print a complex number with at most n decimal digits. | ||
317 | showComplex :: Int -> Complex Double -> String | ||
318 | showComplex d (a:+b) | ||
319 | | isZero a && isZero b = "0" | ||
320 | | isZero b = sa | ||
321 | | isZero a && isOne b = s2++"i" | ||
322 | | isZero a = sb++"i" | ||
323 | | isOne b = sa++s3++"i" | ||
324 | | otherwise = sa++s1++sb++"i" | ||
325 | where | ||
326 | sa = shcr d a | ||
327 | sb = shcr d b | ||
328 | s1 = if b<0 then "" else "+" | ||
329 | s2 = if b<0 then "-" else "" | ||
330 | s3 = if b<0 then "-" else "+" | ||
331 | |||
332 | shcr d a | lookslikeInt a = printf "%.0f" a | ||
333 | | otherwise = printf ("%."++show d++"f") a | ||
334 | |||
335 | |||
336 | lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx | ||
337 | where shx = show x | ||
338 | |||
339 | isZero x = show x `elem` ["0.0","-0.0"] | ||
340 | isOne x = show x `elem` ["1.0","-1.0"] | ||
341 | |||
342 | -- | Pretty print a complex matrix with with at most n decimal digits. | ||
343 | dispcf :: Int -> Matrix (Complex Double) -> String | ||
344 | dispcf d m = sdims m ++ "\n" ++ format " " (showComplex d) m | ||
336 | 345 | ||
337 | -------------------------------------------------------------------- | 346 | -------------------------------------------------------------------- |
338 | 347 | ||