diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/base/hmatrix.cabal | 13 | ||||
-rw-r--r-- | packages/base/src/Internal/Element.hs | 81 | ||||
-rw-r--r-- | packages/base/src/Internal/Numeric.hs | 78 |
3 files changed, 86 insertions, 86 deletions
diff --git a/packages/base/hmatrix.cabal b/packages/base/hmatrix.cabal index c573b49..a487928 100644 --- a/packages/base/hmatrix.cabal +++ b/packages/base/hmatrix.cabal | |||
@@ -41,15 +41,13 @@ library | |||
41 | 41 | ||
42 | hs-source-dirs: src | 42 | hs-source-dirs: src |
43 | 43 | ||
44 | exposed-modules: | 44 | exposed-modules: Numeric.LinearAlgebra |
45 | Numeric.LinearAlgebra | ||
46 | Numeric.LinearAlgebra.Devel | 45 | Numeric.LinearAlgebra.Devel |
47 | Numeric.LinearAlgebra.Data | 46 | Numeric.LinearAlgebra.Data |
48 | Numeric.LinearAlgebra.HMatrix | 47 | Numeric.LinearAlgebra.HMatrix |
49 | Numeric.LinearAlgebra.Static | 48 | Numeric.LinearAlgebra.Static |
50 | 49 | ||
51 | other-modules: | 50 | other-modules: Internal.Tools |
52 | Internal.Tools | ||
53 | Internal.Vector | 51 | Internal.Vector |
54 | Internal.Devel | 52 | Internal.Devel |
55 | Internal.Vectorized | 53 | Internal.Vectorized |
@@ -67,14 +65,13 @@ library | |||
67 | Internal.Sparse | 65 | Internal.Sparse |
68 | Internal.Convolution | 66 | Internal.Convolution |
69 | Internal.Chain | 67 | Internal.Chain |
68 | Numeric.Vector | ||
70 | Internal.CG | 69 | Internal.CG |
70 | Numeric.Matrix | ||
71 | Internal.Util | 71 | Internal.Util |
72 | Internal.Modular | 72 | Internal.Modular |
73 | Internal.Static | 73 | Internal.Static |
74 | Numeric.Vector | 74 | |
75 | Numeric.Matrix | ||
76 | |||
77 | |||
78 | C-sources: src/Internal/C/lapack-aux.c | 75 | C-sources: src/Internal/C/lapack-aux.c |
79 | src/Internal/C/vector-aux.c | 76 | src/Internal/C/vector-aux.c |
80 | 77 | ||
diff --git a/packages/base/src/Internal/Element.hs b/packages/base/src/Internal/Element.hs index 6fc2981..0f61370 100644 --- a/packages/base/src/Internal/Element.hs +++ b/packages/base/src/Internal/Element.hs | |||
@@ -24,9 +24,10 @@ module Internal.Element where | |||
24 | import Internal.Tools | 24 | import Internal.Tools |
25 | import Internal.Vector | 25 | import Internal.Vector |
26 | import Internal.Matrix | 26 | import Internal.Matrix |
27 | import Internal.Vectorized | ||
27 | import qualified Internal.ST as ST | 28 | import qualified Internal.ST as ST |
28 | import Data.Array | 29 | import Data.Array |
29 | 30 | import Text.Printf | |
30 | import Data.Vector.Storable(fromList) | 31 | import Data.Vector.Storable(fromList) |
31 | import Data.List(transpose,intersperse) | 32 | import Data.List(transpose,intersperse) |
32 | import Foreign.Storable(Storable) | 33 | import Foreign.Storable(Storable) |
@@ -78,7 +79,83 @@ instance (Element a, Read a) => Read (Matrix a) where | |||
78 | breakAt c l = (a++[c],tail b) where | 79 | breakAt c l = (a++[c],tail b) where |
79 | (a,b) = break (==c) l | 80 | (a,b) = break (==c) l |
80 | 81 | ||
81 | ------------------------------------------------------------------ | 82 | -------------------------------------------------------------------------------- |
83 | |||
84 | data Extractor | ||
85 | = All | ||
86 | | Range Int Int Int | ||
87 | | Pos (Vector I) | ||
88 | | PosCyc (Vector I) | ||
89 | | Take Int | ||
90 | | TakeLast Int | ||
91 | | Drop Int | ||
92 | | DropLast Int | ||
93 | deriving Show | ||
94 | |||
95 | |||
96 | -- | ||
97 | infixl 9 ?? | ||
98 | (??) :: Element t => Matrix t -> (Extractor,Extractor) -> Matrix t | ||
99 | |||
100 | minEl = toScalarI Min | ||
101 | maxEl = toScalarI Max | ||
102 | cmodi = vectorMapValI ModVS | ||
103 | |||
104 | extractError m e = error $ printf "can't extract %s from matrix %dx%d" (show e) (rows m) (cols m) | ||
105 | |||
106 | m ?? (Range a s b,e) | s /= 1 = m ?? (Pos (idxs [a,a+s .. b]), e) | ||
107 | m ?? (e,Range a s b) | s /= 1 = m ?? (e, Pos (idxs [a,a+s .. b])) | ||
108 | |||
109 | m ?? e@(Range a _ b,_) | a < 0 || b >= rows m = extractError m e | ||
110 | m ?? e@(_,Range a _ b) | a < 0 || b >= cols m = extractError m e | ||
111 | |||
112 | m ?? e@(Pos vs,_) | dim vs>0 && (minEl vs < 0 || maxEl vs >= fi (rows m)) = extractError m e | ||
113 | m ?? e@(_,Pos vs) | dim vs>0 && (minEl vs < 0 || maxEl vs >= fi (cols m)) = extractError m e | ||
114 | |||
115 | m ?? (All,All) = m | ||
116 | |||
117 | m ?? (Range a _ b,e) | a > b = m ?? (Take 0,e) | ||
118 | m ?? (e,Range a _ b) | a > b = m ?? (e,Take 0) | ||
119 | |||
120 | m ?? (Take n,e) | ||
121 | | n <= 0 = (0><cols m) [] ?? (All,e) | ||
122 | | n >= rows m = m ?? (All,e) | ||
123 | |||
124 | m ?? (e,Take n) | ||
125 | | n <= 0 = (rows m><0) [] ?? (e,All) | ||
126 | | n >= cols m = m ?? (e,All) | ||
127 | |||
128 | m ?? (Drop n,e) | ||
129 | | n <= 0 = m ?? (All,e) | ||
130 | | n >= rows m = (0><cols m) [] ?? (All,e) | ||
131 | |||
132 | m ?? (e,Drop n) | ||
133 | | n <= 0 = m ?? (e,All) | ||
134 | | n >= cols m = (rows m><0) [] ?? (e,All) | ||
135 | |||
136 | m ?? (TakeLast n, e) = m ?? (Drop (rows m - n), e) | ||
137 | m ?? (e, TakeLast n) = m ?? (e, Drop (cols m - n)) | ||
138 | |||
139 | m ?? (DropLast n, e) = m ?? (Take (rows m - n), e) | ||
140 | m ?? (e, DropLast n) = m ?? (e, Take (cols m - n)) | ||
141 | |||
142 | m ?? (er,ec) = extractR m moder rs modec cs | ||
143 | where | ||
144 | (moder,rs) = mkExt (rows m) er | ||
145 | (modec,cs) = mkExt (cols m) ec | ||
146 | ran a b = (0, idxs [a,b]) | ||
147 | pos ks = (1, ks) | ||
148 | mkExt _ (Pos ks) = pos ks | ||
149 | mkExt n (PosCyc ks) | ||
150 | | n == 0 = mkExt n (Take 0) | ||
151 | | otherwise = pos (cmodi (fi n) ks) | ||
152 | mkExt _ (Range mn _ mx) = ran mn mx | ||
153 | mkExt _ (Take k) = ran 0 (k-1) | ||
154 | mkExt n (Drop k) = ran k (n-1) | ||
155 | mkExt n _ = ran 0 (n-1) -- All | ||
156 | |||
157 | -------------------------------------------------------------------------------- | ||
158 | |||
82 | 159 | ||
83 | -- | creates a matrix from a vertical list of matrices | 160 | -- | creates a matrix from a vertical list of matrices |
84 | joinVert :: Element t => [Matrix t] -> Matrix t | 161 | joinVert :: Element t => [Matrix t] -> Matrix t |
diff --git a/packages/base/src/Internal/Numeric.hs b/packages/base/src/Internal/Numeric.hs index 86a4a4c..af665a4 100644 --- a/packages/base/src/Internal/Numeric.hs +++ b/packages/base/src/Internal/Numeric.hs | |||
@@ -27,9 +27,8 @@ import Internal.Conversion | |||
27 | import Internal.Vectorized | 27 | import Internal.Vectorized |
28 | import Internal.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ,multiplyI) | 28 | import Internal.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ,multiplyI) |
29 | import Data.Vector.Storable(fromList) | 29 | import Data.Vector.Storable(fromList) |
30 | import Text.Printf(printf) | ||
31 | 30 | ||
32 | ------------------------------------------------------------------- | 31 | -------------------------------------------------------------------------------- |
33 | 32 | ||
34 | type family IndexOf (c :: * -> *) | 33 | type family IndexOf (c :: * -> *) |
35 | 34 | ||
@@ -41,80 +40,7 @@ type family ArgOf (c :: * -> *) a | |||
41 | type instance ArgOf Vector a = a -> a | 40 | type instance ArgOf Vector a = a -> a |
42 | type instance ArgOf Matrix a = a -> a -> a | 41 | type instance ArgOf Matrix a = a -> a -> a |
43 | 42 | ||
44 | -------------------------------------------------------------------------- | 43 | -------------------------------------------------------------------------------- |
45 | |||
46 | data Extractor | ||
47 | = All | ||
48 | | Range Int Int Int | ||
49 | | Pos (Vector I) | ||
50 | | PosCyc (Vector I) | ||
51 | | Take Int | ||
52 | | TakeLast Int | ||
53 | | Drop Int | ||
54 | | DropLast Int | ||
55 | deriving Show | ||
56 | |||
57 | |||
58 | -- | ||
59 | infixl 9 ?? | ||
60 | (??) :: Element t => Matrix t -> (Extractor,Extractor) -> Matrix t | ||
61 | |||
62 | |||
63 | extractError m e = error $ printf "can't extract %s from matrix %dx%d" (show e) (rows m) (cols m) | ||
64 | |||
65 | m ?? (Range a s b,e) | s /= 1 = m ?? (Pos (idxs [a,a+s .. b]), e) | ||
66 | m ?? (e,Range a s b) | s /= 1 = m ?? (e, Pos (idxs [a,a+s .. b])) | ||
67 | |||
68 | m ?? e@(Range a _ b,_) | a < 0 || b >= rows m = extractError m e | ||
69 | m ?? e@(_,Range a _ b) | a < 0 || b >= cols m = extractError m e | ||
70 | |||
71 | m ?? e@(Pos vs,_) | minElement vs < 0 || maxElement vs >= fromIntegral (rows m) = extractError m e | ||
72 | m ?? e@(_,Pos vs) | minElement vs < 0 || maxElement vs >= fromIntegral (cols m) = extractError m e | ||
73 | |||
74 | m ?? (All,All) = m | ||
75 | |||
76 | m ?? (Range a _ b,e) | a > b = m ?? (Take 0,e) | ||
77 | m ?? (e,Range a _ b) | a > b = m ?? (e,Take 0) | ||
78 | |||
79 | m ?? (Take n,e) | ||
80 | | n <= 0 = (0><cols m) [] ?? (All,e) | ||
81 | | n >= rows m = m ?? (All,e) | ||
82 | |||
83 | m ?? (e,Take n) | ||
84 | | n <= 0 = (rows m><0) [] ?? (e,All) | ||
85 | | n >= cols m = m ?? (e,All) | ||
86 | |||
87 | m ?? (Drop n,e) | ||
88 | | n <= 0 = m ?? (All,e) | ||
89 | | n >= rows m = (0><cols m) [] ?? (All,e) | ||
90 | |||
91 | m ?? (e,Drop n) | ||
92 | | n <= 0 = m ?? (e,All) | ||
93 | | n >= cols m = (rows m><0) [] ?? (e,All) | ||
94 | |||
95 | m ?? (TakeLast n, e) = m ?? (Drop (rows m - n), e) | ||
96 | m ?? (e, TakeLast n) = m ?? (e, Drop (cols m - n)) | ||
97 | |||
98 | m ?? (DropLast n, e) = m ?? (Take (rows m - n), e) | ||
99 | m ?? (e, DropLast n) = m ?? (e, Take (cols m - n)) | ||
100 | |||
101 | m ?? (er,ec) = extractR m moder rs modec cs | ||
102 | where | ||
103 | (moder,rs) = mkExt (rows m) er | ||
104 | (modec,cs) = mkExt (cols m) ec | ||
105 | ran a b = (0, idxs [a,b]) | ||
106 | pos ks = (1, ks) | ||
107 | mkExt _ (Pos ks) = pos ks | ||
108 | mkExt n (PosCyc ks) | ||
109 | | n == 0 = mkExt n (Take 0) | ||
110 | | otherwise = pos (cmod n ks) | ||
111 | mkExt _ (Range mn _ mx) = ran mn mx | ||
112 | mkExt _ (Take k) = ran 0 (k-1) | ||
113 | mkExt n (Drop k) = ran k (n-1) | ||
114 | mkExt n _ = ran 0 (n-1) -- All | ||
115 | |||
116 | ------------------------------------------------------------------- | ||
117 | |||
118 | 44 | ||
119 | -- | Basic element-by-element functions for numeric containers | 45 | -- | Basic element-by-element functions for numeric containers |
120 | class Element e => Container c e | 46 | class Element e => Container c e |