diff options
Diffstat (limited to 'lib/Data')
-rw-r--r-- | lib/Data/Packed.hs | 5 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 38 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 17 |
3 files changed, 55 insertions, 5 deletions
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 | |||
34 | conj :: RealFloat e => c (Complex e) -> c (Complex e) | 34 | conj :: RealFloat e => c (Complex e) -> c (Complex e) |
35 | real :: c Double -> c e | 35 | real :: c Double -> c e |
36 | complex :: c e -> c (Complex Double) | 36 | complex :: c e -> c (Complex Double) |
37 | scalar :: e -> c e | ||
37 | 38 | ||
38 | instance Container Vector Double where | 39 | instance Container Vector Double where |
39 | toComplex = Data.Packed.Internal.toComplex | 40 | toComplex = Data.Packed.Internal.toComplex |
@@ -42,6 +43,7 @@ instance Container Vector Double where | |||
42 | conj = Data.Packed.Internal.conj | 43 | conj = Data.Packed.Internal.conj |
43 | real = id | 44 | real = id |
44 | complex = Data.Packed.comp | 45 | complex = Data.Packed.comp |
46 | scalar x = fromList [x] | ||
45 | 47 | ||
46 | instance Container Vector (Complex Double) where | 48 | instance Container Vector (Complex Double) where |
47 | toComplex = undefined -- can't match | 49 | toComplex = undefined -- can't match |
@@ -50,6 +52,7 @@ instance Container Vector (Complex Double) where | |||
50 | conj = undefined | 52 | conj = undefined |
51 | real = Data.Packed.comp | 53 | real = Data.Packed.comp |
52 | complex = id | 54 | complex = id |
55 | scalar x = fromList [x] | ||
53 | 56 | ||
54 | instance Container Matrix Double where | 57 | instance Container Matrix Double where |
55 | toComplex = uncurry $ liftMatrix2 $ curry Data.Packed.toComplex | 58 | toComplex = uncurry $ liftMatrix2 $ curry Data.Packed.toComplex |
@@ -60,6 +63,7 @@ instance Container Matrix Double where | |||
60 | conj = liftMatrix Data.Packed.Internal.conj | 63 | conj = liftMatrix Data.Packed.Internal.conj |
61 | real = id | 64 | real = id |
62 | complex = Data.Packed.comp | 65 | complex = Data.Packed.comp |
66 | scalar x = (1><1) [x] | ||
63 | 67 | ||
64 | instance Container Matrix (Complex Double) where | 68 | instance Container Matrix (Complex Double) where |
65 | toComplex = undefined | 69 | toComplex = undefined |
@@ -68,6 +72,7 @@ instance Container Matrix (Complex Double) where | |||
68 | conj = undefined | 72 | conj = undefined |
69 | real = Data.Packed.comp | 73 | real = Data.Packed.comp |
70 | complex = id | 74 | complex = id |
75 | scalar x = (1><1) [x] | ||
71 | 76 | ||
72 | 77 | ||
73 | -- | converts a real vector into a complex representation (with zero imaginary parts) | 78 | -- | 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 ( | |||
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 | ||