diff options
Diffstat (limited to 'packages/base/src')
-rw-r--r-- | packages/base/src/Data/Packed/Numeric.hs | 24 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/HMatrix.hs | 32 |
2 files changed, 54 insertions, 2 deletions
diff --git a/packages/base/src/Data/Packed/Numeric.hs b/packages/base/src/Data/Packed/Numeric.hs index 7aa53f1..6027f43 100644 --- a/packages/base/src/Data/Packed/Numeric.hs +++ b/packages/base/src/Data/Packed/Numeric.hs | |||
@@ -160,7 +160,29 @@ instance Mul Vector Matrix Vector where | |||
160 | 160 | ||
161 | -------------------------------------------------------------------------------- | 161 | -------------------------------------------------------------------------------- |
162 | 162 | ||
163 | -- | least squares solution of a linear system, similar to the \\ operator of Matlab\/Octave (based on linearSolveSVD) | 163 | {- | Least squares solution of a linear system, similar to the \\ operator of Matlab\/Octave (based on linearSolveSVD) |
164 | |||
165 | @ | ||
166 | a = (3><2) | ||
167 | [ 1.0, 2.0 | ||
168 | , 2.0, 4.0 | ||
169 | , 2.0, -1.0 ] | ||
170 | @ | ||
171 | |||
172 | @ | ||
173 | v = vector [13.0,27.0,1.0] | ||
174 | @ | ||
175 | |||
176 | >>> let x = a <\> v | ||
177 | >>> x | ||
178 | fromList [3.0799999999999996,5.159999999999999] | ||
179 | |||
180 | >>> a #> x | ||
181 | fromList [13.399999999999999,26.799999999999997,1.0] | ||
182 | |||
183 | It also admits multiple right-hand sides stored as columns in a matrix. | ||
184 | |||
185 | -} | ||
164 | infixl 7 <\> | 186 | infixl 7 <\> |
165 | (<\>) :: (LSDiv c, Field t) => Matrix t -> c t -> c t | 187 | (<\>) :: (LSDiv c, Field t) => Matrix t -> c t -> c t |
166 | (<\>) = linSolve | 188 | (<\>) = linSolve |
diff --git a/packages/base/src/Numeric/LinearAlgebra/HMatrix.hs b/packages/base/src/Numeric/LinearAlgebra/HMatrix.hs index d2cae6c..c0cc622 100644 --- a/packages/base/src/Numeric/LinearAlgebra/HMatrix.hs +++ b/packages/base/src/Numeric/LinearAlgebra/HMatrix.hs | |||
@@ -194,7 +194,37 @@ mul :: Numeric t => Matrix t -> Matrix t -> Matrix t | |||
194 | mul = mXm | 194 | mul = mXm |
195 | 195 | ||
196 | 196 | ||
197 | -- | Solve a linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, returning Nothing for a singular system. For underconstrained or overconstrained systems use 'linearSolveLS' or 'linearSolveSVD'. | 197 | {- | Solve a linear system (for square coefficient matrix and several right-hand sides) using the LU decomposition, returning Nothing for a singular system. For underconstrained or overconstrained systems use 'linearSolveLS' or 'linearSolveSVD'. |
198 | |||
199 | @ | ||
200 | a = (2><2) | ||
201 | [ 1.0, 2.0 | ||
202 | , 3.0, 5.0 ] | ||
203 | @ | ||
204 | |||
205 | @ | ||
206 | b = (2><3) | ||
207 | [ 6.0, 1.0, 10.0 | ||
208 | , 15.0, 3.0, 26.0 ] | ||
209 | @ | ||
210 | |||
211 | >>> linearSolve a b | ||
212 | Just (2><3) | ||
213 | [ -1.4802973661668753e-15, 0.9999999999999997, 1.999999999999997 | ||
214 | , 3.000000000000001, 1.6653345369377348e-16, 4.000000000000002 ] | ||
215 | |||
216 | >>> let Just x = it | ||
217 | >>> disp 5 x | ||
218 | 2x3 | ||
219 | -0.00000 1.00000 2.00000 | ||
220 | 3.00000 0.00000 4.00000 | ||
221 | |||
222 | >>> a <> x | ||
223 | (2><3) | ||
224 | [ 6.0, 1.0, 10.0 | ||
225 | , 15.0, 3.0, 26.0 ] | ||
226 | |||
227 | -} | ||
198 | linearSolve m b = A.mbLinearSolve m b | 228 | linearSolve m b = A.mbLinearSolve m b |
199 | 229 | ||
200 | -- | return an orthonormal basis of the null space of a matrix. See also 'nullspaceSVD'. | 230 | -- | return an orthonormal basis of the null space of a matrix. See also 'nullspaceSVD'. |