4
Licensed to the Apache Software Foundation (ASF) under one or more
5
contributor license agreements. See the NOTICE file distributed with
6
this work for additional information regarding copyright ownership.
7
The ASF licenses this file to You under the Apache License, Version 2.0
8
(the "License"); you may not use this file except in compliance with
9
the License. You may obtain a copy of the License at
11
http://www.apache.org/licenses/LICENSE-2.0
13
Unless required by applicable law or agreed to in writing, software
14
distributed under the License is distributed on an "AS IS" BASIS,
15
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
See the License for the specific language governing permissions and
17
limitations under the License.
20
<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
21
<!-- $Revision: 619641 $ $Date: 2008-02-07 14:17:35 -0700 (Thu, 07 Feb 2008) $ -->
22
<document url="linear.html">
25
<title>The Commons Math User Guide - Linear Algebra</title>
29
<section name="3 Linear Algebra">
30
<subsection name="3.1 Overview" href="overview">
32
Currently, numerical linear algebra support in commons-math is
33
limited to basic operations on real matrices and solving linear systems.
36
<subsection name="3.2 Real matrices" href="real_matrices">
38
The <a href="../apidocs/org/apache/commons/math/linear/RealMatrix.html">
39
RealMatrix</a> interface represents a matrix with real numbers as
40
entries. The following basic matrix operations are supported:
42
<li>Matrix addition, subtraction, mutiplication</li>
43
<li>Scalar addition and multiplication</li>
44
<li>Inverse and transpose</li>
45
<li>Determinants and singularity testing</li>
46
<li>LU decomposition</li>
47
<li>Norm and Trace</li>
48
<li>Operation on a vector</li>
54
// Create a real matrix with two rows and three columns
55
double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
56
RealMatrix m = new RealMatrixImpl(matrixData);
58
// One more with three rows, two columns
59
double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};
60
RealMatrix n = new RealMatrixImpl(matrixData2);
62
// Note: The constructor copies the input double[][] array.
64
// Now multiply m by n
65
RealMatrix p = m.multiply(n);
66
System.out.println(p.getRowDimension()); // 2
67
System.out.println(p.getRowDimension()); // 2
70
RealMatrix pInverse = p.inverse();
74
<subsection name="3.3 Solving linear systems" href="solve">
76
The <code>solve()</code> methods of the <code>RealMatrix</code> interface
77
support solving linear systems of equations. In each case, the
78
<code>RealMatrix</code> represents the coefficient matrix of the system.
79
For example, to solve the linear system
85
Start by creating a RealMatrix to store the coefficients
87
double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
88
RealMatrix coefficients = new RealMatrixImpl(coefficientsData);
90
Next create a <code>double[]</code> array to represent the constant
91
vector and use <code>solve(double[])</code> to solve the system
93
double[] constants = {1, -2, 1};
94
double[] solution = coefficients.solve(constants);
96
The <code>solution</code> array will contain values for x
97
(<code>solution[0]</code>), y (<code>solution[1]</code>),
98
and z (<code>solution[2]</code>) that solve the system.
101
If the coefficient matrix is not square or singular, an
102
<a href="../apidocs/org/apache/commons/math/linear/InvalidMatrixException.html">
103
InvalidMatrixException</a> is thrown.
106
It is possible to solve multiple systems with the same coefficient matrix
107
in one method call. To do this, create a matrix whose column vectors correspond
108
to the constant vectors for the systems to be solved and use
109
<code>solve(RealMatrix),</code> which returns a matrix with column
110
vectors representing the solutions.