summaryrefslogtreecommitdiff
path: root/packages/base/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src')
-rw-r--r--packages/base/src/Data/Packed/Internal/Numeric.hs15
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Util.hs10
2 files changed, 18 insertions, 7 deletions
diff --git a/packages/base/src/Data/Packed/Internal/Numeric.hs b/packages/base/src/Data/Packed/Internal/Numeric.hs
index 67d047c..44d18b3 100644
--- a/packages/base/src/Data/Packed/Internal/Numeric.hs
+++ b/packages/base/src/Data/Packed/Internal/Numeric.hs
@@ -69,7 +69,7 @@ type instance ArgOf Matrix a = a -> a -> a
69 69
70data Extractor 70data Extractor
71 = All 71 = All
72 | Range Int Int 72 | Range Int Int Int
73 | Pos (Vector I) 73 | Pos (Vector I)
74 | PosCyc (Vector I) 74 | PosCyc (Vector I)
75 | Take Int 75 | Take Int
@@ -89,16 +89,19 @@ infixl 9 ??
89 89
90extractError m e = error $ printf "can't extract %s from matrix %dx%d" (show e) (rows m) (cols m) 90extractError m e = error $ printf "can't extract %s from matrix %dx%d" (show e) (rows m) (cols m)
91 91
92m ?? e@(Range a b,_) | a < 0 || b >= rows m = extractError m e 92m ?? (Range a s b,e) | s /= 1 = m ?? (Pos (idxs [a,a+s .. b]), e)
93m ?? e@(_,Range a b) | a < 0 || b >= cols m = extractError m e 93m ?? (e,Range a s b) | s /= 1 = m ?? (e, Pos (idxs [a,a+s .. b]))
94
95m ?? e@(Range a _ b,_) | a < 0 || b >= rows m = extractError m e
96m ?? e@(_,Range a _ b) | a < 0 || b >= cols m = extractError m e
94 97
95m ?? e@(Pos vs,_) | minElement vs < 0 || maxElement vs >= fromIntegral (rows m) = extractError m e 98m ?? e@(Pos vs,_) | minElement vs < 0 || maxElement vs >= fromIntegral (rows m) = extractError m e
96m ?? e@(_,Pos vs) | minElement vs < 0 || maxElement vs >= fromIntegral (cols m) = extractError m e 99m ?? e@(_,Pos vs) | minElement vs < 0 || maxElement vs >= fromIntegral (cols m) = extractError m e
97 100
98m ?? (All,All) = m 101m ?? (All,All) = m
99 102
100m ?? (Range a b,e) | a > b = m ?? (Take 0,e) 103m ?? (Range a _ b,e) | a > b = m ?? (Take 0,e)
101m ?? (e,Range a b) | a > b = m ?? (e,Take 0) 104m ?? (e,Range a _ b) | a > b = m ?? (e,Take 0)
102 105
103m ?? (Take n,e) 106m ?? (Take n,e)
104 | n <= 0 = (0><cols m) [] ?? (All,e) 107 | n <= 0 = (0><cols m) [] ?? (All,e)
@@ -132,7 +135,7 @@ m ?? (er,ec) = extractR m moder rs modec cs
132 mkExt n (PosCyc ks) 135 mkExt n (PosCyc ks)
133 | n == 0 = mkExt n (Take 0) 136 | n == 0 = mkExt n (Take 0)
134 | otherwise = pos (cmod n ks) 137 | otherwise = pos (cmod n ks)
135 mkExt _ (Range mn mx) = ran mn mx 138 mkExt _ (Range mn _ mx) = ran mn mx
136 mkExt _ (Take k) = ran 0 (k-1) 139 mkExt _ (Take k) = ran 0 (k-1)
137 mkExt n (Drop k) = ran k (n-1) 140 mkExt n (Drop k) = ran k (n-1)
138 mkExt n _ = ran 0 (n-1) -- All 141 mkExt n _ = ran 0 (n-1) -- All
diff --git a/packages/base/src/Numeric/LinearAlgebra/Util.hs b/packages/base/src/Numeric/LinearAlgebra/Util.hs
index f09bdb5..8d9f842 100644
--- a/packages/base/src/Numeric/LinearAlgebra/Util.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Util.hs
@@ -312,6 +312,8 @@ size = size'
312>>> vector [1..10] ! 3 312>>> vector [1..10] ! 3
3134.0 3134.0
314 314
315On a matrix it gets the k-th row as a vector:
316
315>>> matrix 5 [1..15] ! 1 317>>> matrix 5 [1..15] ! 1
316fromList [6.0,7.0,8.0,9.0,10.0] 318fromList [6.0,7.0,8.0,9.0,10.0]
317 319
@@ -479,12 +481,18 @@ instance Testable (Matrix I) where
479 checkT _ = test 481 checkT _ = test
480 482
481test :: (Bool, IO()) 483test :: (Bool, IO())
482test = (and ok, print ok) 484test = (and ok, return ())
483 where 485 where
484 m = (3><4) [1..12] :: Matrix I 486 m = (3><4) [1..12] :: Matrix I
487 r = (2><3) [1,2,3,4,3,2]
488 c = (3><2) [0,4,4,1,2,3]
489 p = (9><10) [0..89] :: Matrix I
490 ep = (2><3) [10,24,32,44,31,23]
485 md = fromInt m :: Matrix Double 491 md = fromInt m :: Matrix Double
486 ok = [ tr m <> m == toInt (tr md <> md) 492 ok = [ tr m <> m == toInt (tr md <> md)
487 , m <> tr m == toInt (md <> tr md) 493 , m <> tr m == toInt (md <> tr md)
488 , m ?? (Take 2, Take 3) == remap (asColumn (range 2)) (asRow (range 3)) m 494 , m ?? (Take 2, Take 3) == remap (asColumn (range 2)) (asRow (range 3)) m
495 , remap r (tr c) p == ep
496 , tr p ?? (PosCyc (idxs[-5,13]), Pos (idxs[3,7,1])) == (2><3) [35,75,15,33,73,13]
489 ] 497 ]
490 498