|
getEigenvalues()
, getEigenVector()
,
<code>getV(), getD()
and getVT()
methods of the
<code>EigenDecomposition interface support solving eigenproblems of the form
AX = lambda X where lambda is a real scalar.
</p>
<p>The getSingularValues()
, getU()
, getS()
and
<code>getV() methods of the SingularValueDecomposition
interface
allow to solve singular values problems of the form AXi = lambda Yi where lambda is a
real scalar, and where the Xi and Yi vectors form orthogonal bases of their respective
vector spaces (which may have different dimensions).
</p>
</subsection>
<subsection name="3.6 Non-real fields (complex, fractions ...)" href="field">
<p>
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.
The fields already supported by the library are:
<ul>
<li>Complex
<li>Fraction
<li>BigFraction
<li>BigReal
</ul>
</p>
</subsection>
</section>
</body>
</document>
Here is a short list of links related to this Commons Math linear.xml source code file:
Commons Math example source code file (linear.xml)
The Commons Math linear.xml source code<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
<!-- $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $ -->
<document url="linear.html">
<properties>
<title>The Commons Math User Guide - Linear Algebra
</properties>
<body>
<section name="3 Linear Algebra">
<subsection name="3.1 Overview" href="overview">
<p>
Linear algebra support in commons-math provides operations on real matrices
(both dense and sparse matrices are supported) and vectors. It features basic
operations (addition, subtraction ...) and decomposition algorithms that can
be used to solve linear systems either in exact sense and in least squares sense.
</p>
</subsection>
<subsection name="3.2 Real matrices" href="real_matrices">
<p>
The <a href="../apidocs/org/apache/commons/math/linear/RealMatrix.html">
RealMatrix</a> interface represents a matrix with real numbers as
entries. The following basic matrix operations are supported:
<ul>
<li>Matrix addition, subtraction, multiplication
<li>Scalar addition and multiplication
<li>transpose
<li>Norm and Trace
<li>Operation on a vector
</ul>
</p>
<p>
Example:
<source>
// Create a real matrix with two rows and three columns
double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
RealMatrix m = new Array2DRowRealMatrix(matrixData);
// One more with three rows, two columns
double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};
RealMatrix n = new Array2DRowRealMatrix(matrixData2);
// Note: The constructor copies the input double[][] array.
// Now multiply m by n
RealMatrix p = m.multiply(n);
System.out.println(p.getRowDimension()); // 2
System.out.println(p.getColumnDimension()); // 2
// Invert p, using LU decomposition
RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
</source>
</p>
<p>
The three main implementations of the interface are <a
href="../apidocs/org/apache/commons/math/linear/Array2DRowRealMatrix.html">
Array2DRowRealMatrix</a> and
<li>Element by element multiplication, division
<li>Scalar addition, subtraction, multiplication, division and power
<li>Mapping of mathematical functions (cos, sin ...)
<li>Dot product, outer product
<li>Distance and norm according to norms L1, L2 and Linf
</ul>
</p>
<p>
The <a href="../apidocs/org/apache/commons/math/linear/RealVectorFormat.html">
RealVectorFormat</a> class handles input/output of vectors in a customizable
textual format.
</p>
</subsection>
<subsection name="3.4 Solving linear systems" href="solve">
<p>
The <code>solve() methods of the
interface support solving linear systems of equations of the form AX=B, either
in linear sense or in least square sense. A <code>RealMatrix instance is
used to represent the coefficient matrix of the system. Solving the system is a
two phases process: first the coefficient matrix is decomposed in some way and
then a solver built from the decomposition solves the system. This allows to
compute the decomposition and build the solver only once if several systems have
to be solved with the same coefficient matrix.
</p>
<p>
For example, to solve the linear system
<pre>
2x + 3y - 2z = 1
-x + 7y + 6x = -2
4x - 3y - 5z = 1
</pre>
Start by decomposing the coefficient matrix A (in this case using LU decomposition)
and build a solver
<source>
RealMatrix coefficients =
new Array2DRowRealMatrix(new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } },
false);
DecompositionSolver solver = new LUDecompositionImpl(coefficients).getSolver();
</source>
Next create a <code>RealVector array to represent the constant
vector B and use <code>solve(RealVector) to solve the system
<source>
RealVector constants = new RealVectorImpl(new double[] { 1, -2, 1 }, false);
RealVector solution = solver.solve(constants);
</source>
The <code>solution vector will contain values for x
(<code>solution.getEntry(0)), y ( | Decomposition algorithms | ||
Name | coefficients matrix | problem type | |
LU | square | exact solution only | |
Cholesky | symmetric positive definite | exact solution only | |
QR | any | least squares solution | |
eigen decomposition | square | exact solution only | |
SVD | any | least squares solution |
... this post is sponsored by my books ... | |
![]() #1 New Release! |
![]() FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.