comparison libs/commons-math-2.1/docs/userguide/linear.html @ 10:5f2c5fb36e93

commons-math-2.1 added
author dwinter
date Tue, 04 Jan 2011 10:00:53 +0100
parents
children
comparison
equal deleted inserted replaced
9:e63a64652f4d 10:5f2c5fb36e93
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
3
4
5
6
7
8
9
10
11
12
13 <html xmlns="http://www.w3.org/1999/xhtml">
14 <head>
15 <title>Math - The Commons Math User Guide - Linear Algebra</title>
16 <style type="text/css" media="all">
17 @import url("../css/maven-base.css");
18 @import url("../css/maven-theme.css");
19 @import url("../css/site.css");
20 </style>
21 <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
22 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
23 </head>
24 <body class="composite">
25 <div id="banner">
26 <span id="bannerLeft">
27
28 Commons Math User Guide
29
30 </span>
31 <div class="clear">
32 <hr/>
33 </div>
34 </div>
35 <div id="breadcrumbs">
36
37
38
39
40
41
42
43
44 <div class="xright">
45
46
47
48
49
50
51
52 </div>
53 <div class="clear">
54 <hr/>
55 </div>
56 </div>
57 <div id="leftColumn">
58 <div id="navcolumn">
59
60
61
62
63
64
65
66
67 <h5>User Guide</h5>
68 <ul>
69
70 <li class="none">
71 <a href="../userguide/index.html">Contents</a>
72 </li>
73
74 <li class="none">
75 <a href="../userguide/overview.html">Overview</a>
76 </li>
77
78 <li class="none">
79 <a href="../userguide/stat.html">Statistics</a>
80 </li>
81
82 <li class="none">
83 <a href="../userguide/random.html">Data Generation</a>
84 </li>
85
86 <li class="none">
87 <strong>Linear Algebra</strong>
88 </li>
89
90 <li class="none">
91 <a href="../userguide/analysis.html">Numerical Analysis</a>
92 </li>
93
94 <li class="none">
95 <a href="../userguide/special.html">Special Functions</a>
96 </li>
97
98 <li class="none">
99 <a href="../userguide/utilities.html">Utilities</a>
100 </li>
101
102 <li class="none">
103 <a href="../userguide/complex.html">Complex Numbers</a>
104 </li>
105
106 <li class="none">
107 <a href="../userguide/distribution.html">Distributions</a>
108 </li>
109
110 <li class="none">
111 <a href="../userguide/fraction.html">Fractions</a>
112 </li>
113
114 <li class="none">
115 <a href="../userguide/transform.html">Transform Methods</a>
116 </li>
117
118 <li class="none">
119 <a href="../userguide/geometry.html">3D Geometry</a>
120 </li>
121
122 <li class="none">
123 <a href="../userguide/optimization.html">Optimization</a>
124 </li>
125
126 <li class="none">
127 <a href="../userguide/ode.html">Ordinary Differential Equations</a>
128 </li>
129
130 <li class="none">
131 <a href="../userguide/genetics.html">Genetic Algorithms</a>
132 </li>
133 </ul>
134 <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
135 <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
136 </a>
137
138
139
140
141
142
143
144
145 </div>
146 </div>
147 <div id="bodyColumn">
148 <div id="contentBox">
149 <div class="section"><h2><a name="a3_Linear_Algebra"></a>3 Linear Algebra</h2>
150 <div class="section"><h3><a name="a3.1_Overview"></a>3.1 Overview</h3>
151 <p>
152 Linear algebra support in commons-math provides operations on real matrices
153 (both dense and sparse matrices are supported) and vectors. It features basic
154 operations (addition, subtraction ...) and decomposition algorithms that can
155 be used to solve linear systems either in exact sense and in least squares sense.
156 </p>
157 </div>
158 <div class="section"><h3><a name="a3.2_Real_matrices"></a>3.2 Real matrices</h3>
159 <p>
160 The <a href="../apidocs/org/apache/commons/math/linear/RealMatrix.html">
161 RealMatrix</a> interface represents a matrix with real numbers as
162 entries. The following basic matrix operations are supported:
163 <ul><li>Matrix addition, subtraction, multiplication</li>
164 <li>Scalar addition and multiplication</li>
165 <li>transpose</li>
166 <li>Norm and Trace</li>
167 <li>Operation on a vector</li>
168 </ul>
169 </p>
170 <p>
171 Example:
172 <div class="source"><pre>
173 // Create a real matrix with two rows and three columns
174 double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
175 RealMatrix m = new Array2DRowRealMatrix(matrixData);
176
177 // One more with three rows, two columns
178 double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};
179 RealMatrix n = new Array2DRowRealMatrix(matrixData2);
180
181 // Note: The constructor copies the input double[][] array.
182
183 // Now multiply m by n
184 RealMatrix p = m.multiply(n);
185 System.out.println(p.getRowDimension()); // 2
186 System.out.println(p.getColumnDimension()); // 2
187
188 // Invert p, using LU decomposition
189 RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
190 </pre>
191 </div>
192 </p>
193 <p>
194 The three main implementations of the interface are <a href="../apidocs/org/apache/commons/math/linear/Array2DRowRealMatrix.html">
195 Array2DRowRealMatrix</a> and <a href="../apidocs/org/apache/commons/math/linear/BlockRealMatrix.html">
196 BlockRealMatrix</a> for dense matrices (the second one being more suited to
197 dimensions above 50 or 100) and <a href="../apidocs/org/apache/commons/math/linear/SparseRealMatrix.html">
198 SparseRealMatrix</a> for sparse matrices.
199 </p>
200 </div>
201 <div class="section"><h3><a name="a3.3_Real_vectors"></a>3.3 Real vectors</h3>
202 <p>
203 The <a href="../apidocs/org/apache/commons/math/linear/RealVector.html">
204 RealVector</a> interface represents a vector with real numbers as
205 entries. The following basic matrix operations are supported:
206 <ul><li>Vector addition, subtraction</li>
207 <li>Element by element multiplication, division</li>
208 <li>Scalar addition, subtraction, multiplication, division and power</li>
209 <li>Mapping of mathematical functions (cos, sin ...)</li>
210 <li>Dot product, outer product</li>
211 <li>Distance and norm according to norms L1, L2 and Linf</li>
212 </ul>
213 </p>
214 <p>
215 The <a href="../apidocs/org/apache/commons/math/linear/RealVectorFormat.html">
216 RealVectorFormat</a> class handles input/output of vectors in a customizable
217 textual format.
218 </p>
219 </div>
220 <div class="section"><h3><a name="a3.4_Solving_linear_systems"></a>3.4 Solving linear systems</h3>
221 <p>
222 The <code>solve()</code> methods of the <a href="../apidocs/org/apache/commons/math/linear/DecompositionSolver.html">DecompositionSolver</a>
223 interface support solving linear systems of equations of the form AX=B, either
224 in linear sense or in least square sense. A <code>RealMatrix</code> instance is
225 used to represent the coefficient matrix of the system. Solving the system is a
226 two phases process: first the coefficient matrix is decomposed in some way and
227 then a solver built from the decomposition solves the system. This allows to
228 compute the decomposition and build the solver only once if several systems have
229 to be solved with the same coefficient matrix.
230 </p>
231 <p>
232 For example, to solve the linear system
233 <pre>
234 2x + 3y - 2z = 1
235 -x + 7y + 6x = -2
236 4x - 3y - 5z = 1
237 </pre>
238 Start by decomposing the coefficient matrix A (in this case using LU decomposition)
239 and build a solver
240 <div class="source"><pre>
241 RealMatrix coefficients =
242 new Array2DRowRealMatrix(new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } },
243 false);
244 DecompositionSolver solver = new LUDecompositionImpl(coefficients).getSolver();
245 </pre>
246 </div>
247
248 Next create a <code>RealVector</code> array to represent the constant
249 vector B and use <code>solve(RealVector)</code> to solve the system
250 <div class="source"><pre>
251 RealVector constants = new RealVectorImpl(new double[] { 1, -2, 1 }, false);
252 RealVector solution = solver.solve(constants);
253 </pre>
254 </div>
255
256 The <code>solution</code> vector will contain values for x
257 (<code>solution.getEntry(0)</code>), y (<code>solution.getEntry(1)</code>),
258 and z (<code>solution.getEntry(2)</code>) that solve the system.
259 </p>
260 <p>
261 Each type of decomposition has its specific semantics and constraints on
262 the coefficient matrix as shown in the following table. For algorithms that
263 solve AX=B in least squares sense the value returned for X is such that the
264 residual AX-B has minimal norm. If an exact solution exist (i.e. if for some
265 X the residual AX-B is exactly 0), then this exact solution is also the solution
266 in least square sense. This implies that algorithms suited for least squares
267 problems can also be used to solve exact problems, but the reverse is not true.
268 </p>
269 <p><table class="bodyTable"><tr class="a"><td><font size="+2">Decomposition algorithms</font></td>
270 </tr>
271 <tr class="b"><font size="+1"><td>Name</td>
272 <td>coefficients matrix</td>
273 <td>problem type</td>
274 </font></tr>
275 <tr class="a"><td><a href="../apidocs/org/apache/commons/math/linear/LUDecomposition.html">LU</a></td>
276 <td>square</td>
277 <td>exact solution only</td>
278 </tr>
279 <tr class="b"><td><a href="../apidocs/org/apache/commons/math/linear/CholeskyDecomposition.html">Cholesky</a></td>
280 <td>symmetric positive definite</td>
281 <td>exact solution only</td>
282 </tr>
283 <tr class="a"><td><a href="../apidocs/org/apache/commons/math/linear/QRDecomposition.html">QR</a></td>
284 <td>any</td>
285 <td>least squares solution</td>
286 </tr>
287 <tr class="b"><td><a href="../apidocs/org/apache/commons/math/linear/EigenDecomposition.html">eigen decomposition</a></td>
288 <td>square</td>
289 <td>exact solution only</td>
290 </tr>
291 <tr class="a"><td><a href="../apidocs/org/apache/commons/math/linear/SingularValueDecomposition.html">SVD</a></td>
292 <td>any</td>
293 <td>least squares solution</td>
294 </tr>
295 </table>
296 </p>
297 <p>
298 It is possible to use a simple array of double instead of a <code>RealVector</code>.
299 In this case, the solution will be provided also as an array of double.
300 </p>
301 <p>
302 It is possible to solve multiple systems with the same coefficient matrix
303 in one method call. To do this, create a matrix whose column vectors correspond
304 to the constant vectors for the systems to be solved and use <code>solve(RealMatrix),</code>
305 which returns a matrix with column vectors representing the solutions.
306 </p>
307 </div>
308 <div class="section"><h3><a name="a3.5_Eigenvalueseigenvectors_and_singular_valuessingular_vectors"></a>3.5 Eigenvalues/eigenvectors and singular values/singular vectors</h3>
309 <p>
310 Decomposition algorithms may be used for themselves and not only for linear system solving.
311 This is of prime interest with eigen decomposition and singular value decomposition.
312 </p>
313 <p>
314 The <code>getEigenvalue()</code>, <code>getEigenvalues()</code>, <code>getEigenVector()</code>,
315 <code>getV()</code>, <code>getD()</code> and <code>getVT()</code> methods of the
316 <code>EigenDecomposition</code> interface support solving eigenproblems of the form
317 AX = lambda X where lambda is a real scalar.
318 </p>
319 <p>The <code>getSingularValues()</code>, <code>getU()</code>, <code>getS()</code> and
320 <code>getV()</code> methods of the <code>SingularValueDecomposition</code> interface
321 allow to solve singular values problems of the form AXi = lambda Yi where lambda is a
322 real scalar, and where the Xi and Yi vectors form orthogonal bases of their respective
323 vector spaces (which may have different dimensions).
324 </p>
325 </div>
326 <div class="section"><h3><a name="a3.6_Non-real_fields_complex_fractions_..."></a>3.6 Non-real fields (complex, fractions ...)</h3>
327 <p>
328 In addition to the real field, matrices and vectors using non-real <a href="../apidocs/org/apache/commons/math/FieldElement.html">field elements</a> can be used.
329 The fields already supported by the library are:
330 <ul><li><a href="../apidocs/org/apache/commons/math/complex/Complex.html">Complex</a></li>
331 <li><a href="../apidocs/org/apache/commons/math/fraction/Fraction.html">Fraction</a></li>
332 <li><a href="../apidocs/org/apache/commons/math/fraction/BigFraction.html">BigFraction</a></li>
333 <li><a href="../apidocs/org/apache/commons/math/util/BigReal.html">BigReal</a></li>
334 </ul>
335 </p>
336 </div>
337 </div>
338
339 </div>
340 </div>
341 <div class="clear">
342 <hr/>
343 </div>
344 <div id="footer">
345 <div class="xright">&#169;
346 2003-2010
347
348
349
350
351
352
353
354
355
356 </div>
357 <div class="clear">
358 <hr/>
359 </div>
360 </div>
361 </body>
362 </html>