Top Description Inners Fields Constructors Methods
org.apache.commons.math3.linear

public Class EigenDecomposition

extends Object
Class Inheritance
Imports
org.apache.commons.math3.complex.Complex, org.apache.commons.math3.exception.MathArithmeticException, .MathUnsupportedOperationException, .MaxCountExceededException, .DimensionMismatchException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.util.Precision, .FastMath

Calculates the eigen decomposition of a real matrix.

The eigen decomposition of matrix A is a set of two matrices: V and D such that A = V × D × VT. A, V and D are all m × m matrices.

This class is similar in spirit to the EigenvalueDecomposition class from the JAMA library, with the following changes:

As of 3.1, this class supports general real matrices (both symmetric and non-symmetric):

If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is diagonal and the eigenvector matrix V is orthogonal, i.e. A = V.multiply(D.multiply(V.transpose())) and V.multiply(V.transpose()) equals the identity matrix.

If A is not symmetric, then the eigenvalue matrix D is block diagonal with the real eigenvalues in 1-by-1 blocks and any complex eigenvalues, lambda + i*mu, in 2-by-2 blocks:

   [lambda, mu    ]
   [   -mu, lambda]
The columns of V represent the eigenvectors in the sense that A*V = V*D, i.e. A.multiply(V) equals V.multiply(D). The matrix V may be badly conditioned, or even singular, so the validity of the equation A = V*D*inverse(V) depends upon the condition of V.

This implementation is based on the paper by A. Drubrulle, R.S. Martin and J.H. Wilkinson "The Implicit QL Algorithm" in Wilksinson and Reinsch (1971) Handbook for automatic computation, vol. 2, Linear algebra, Springer-Verlag, New-York

Since
2.0 (changed to concrete class in 3.0)
See Also
MathWorld, Wikipedia

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
EigenDecomposition.Solver

Specialized solver.

Field Summary

Modifier and TypeField and Description
private RealMatrix
cachedD

Cached value of D.

private RealMatrix
cachedV

Cached value of V.

private RealMatrix
cachedVt

Cached value of Vt.

private ArrayRealVector[]
eigenvectors

Eigenvectors.

private static final double
EPSILON

Internally used epsilon criteria.

private double[]
imagEigenvalues

Imaginary part of the realEigenvalues.

private final boolean
isSymmetric

Whether the matrix is symmetric.

private double[]
main

Main diagonal of the tridiagonal matrix.

private byte
maxIter

Maximum number of iterations accepted in the implicit QL transformation

private double[]
realEigenvalues

Real part of the realEigenvalues.

private double[]
secondary

Secondary diagonal of the tridiagonal matrix.

private TriDiagonalTransformer
transformer

Transformer to tridiagonal (may be null if matrix is already tridiagonal).

Constructor Summary

AccessConstructor and Description
public
EigenDecomposition(final RealMatrix
Matrix to decompose.
matrix
)

Calculates the eigen decomposition of the given real matrix.

public
EigenDecomposition(final RealMatrix
Matrix to decompose.
matrix
,
final double
Dummy parameter (present for backward compatibility only).
splitTolerance
)
Deprecated in 3.1 (to be removed in 4.0) due to unused parameter

Calculates the eigen decomposition of the given real matrix.

public
EigenDecomposition(final double[]
Main diagonal of the symmetric tridiagonal form.
main
,
final double[]
Secondary of the tridiagonal form.
secondary
)

Calculates the eigen decomposition of the symmetric tridiagonal matrix.

public
EigenDecomposition(final double[]
Main diagonal of the symmetric tridiagonal form.
main
,
final double[]
Secondary of the tridiagonal form.
secondary
,
final double
Dummy parameter (present for backward compatibility only).
splitTolerance
)
Deprecated in 3.1 (to be removed in 4.0) due to unused parameter

Calculates the eigen decomposition of the symmetric tridiagonal matrix.

Method Summary

Modifier and TypeMethod and Description
private Complex

Returns:

result of the complex division
cdiv
(final double
real part of the first number
xr
,
final double
imaginary part of the first number
xi
,
final double
real part of the second number
yr
,
final double
imaginary part of the second number
yi
)

Performs a division of two complex numbers.

private void
findEigenVectors(final double[][]
Householder matrix of the transformation to tridiagonal form.
householderMatrix
)

Find eigenvalues and eigenvectors (Dubrulle et al., 1971)

private void
findEigenVectorsFromSchur(final SchurTransformer
the schur transformation of the matrix
schur
)

Find eigenvectors from a matrix transformed to Schur form.

public RealMatrix

Returns:

the D matrix.
getD
()

Gets the block diagonal matrix D of the decomposition.

public double

Returns:

the determinant of the matrix.
getDeterminant
()

Computes the determinant of the matrix.

public RealVector

Returns:

a copy of the ith eigenvector of the original matrix.
getEigenvector
(final int
Index of the eigenvector (counting from 0).
i
)

Gets a copy of the ith eigenvector of the original matrix.

public double

Returns:

the imaginary part of the ith eigenvalue of the original matrix.
getImagEigenvalue
(final int
Index of the eigenvalue (counting from 0).
i
)

Gets the imaginary part of the ith eigenvalue of the original matrix.

public double[]

Returns:

a copy of the imaginary parts of the eigenvalues of the original matrix.
getImagEigenvalues
()

Gets a copy of the imaginary parts of the eigenvalues of the original matrix.

public double

Returns:

real part of the ith eigenvalue of the original matrix.
getRealEigenvalue
(final int
index of the eigenvalue (counting from 0)
i
)

Returns the real part of the ith eigenvalue of the original matrix.

public double[]

Returns:

a copy of the real parts of the eigenvalues of the original matrix.
getRealEigenvalues
()

Gets a copy of the real parts of the eigenvalues of the original matrix.

public DecompositionSolver

Returns:

a solver
getSolver
()

Gets a solver for finding the A × X = B solution in exact linear sense.

public RealMatrix

Returns:

the square-root of the matrix.
getSquareRoot
()

Computes the square-root of the matrix.

public RealMatrix

Returns:

the V matrix.
getV
()

Gets the matrix V of the decomposition.

public RealMatrix

Returns:

the transpose of the V matrix.
getVT
()

Gets the transpose of the matrix V of the decomposition.

public boolean

Returns:

true if the eigen values are complex, false otherwise
hasComplexEigenvalues
()

Returns whether the calculated eigen values are complex or real.

private SchurTransformer

Returns:

the Shur transform for this matrix
transformToSchur
(final RealMatrix
Matrix to transform.
matrix
)

Transforms the matrix to Schur form and calculates the eigenvalues.

private void
transformToTridiagonal(final RealMatrix
Matrix to transform.
matrix
)

Transforms the matrix to tridiagonal form.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachedDback to summary
private RealMatrix cachedD

Cached value of D.

cachedVback to summary
private RealMatrix cachedV

Cached value of V.

cachedVtback to summary
private RealMatrix cachedVt

Cached value of Vt.

eigenvectorsback to summary
private ArrayRealVector[] eigenvectors

Eigenvectors.

EPSILONback to summary
private static final double EPSILON

Internally used epsilon criteria.

imagEigenvaluesback to summary
private double[] imagEigenvalues

Imaginary part of the realEigenvalues.

isSymmetricback to summary
private final boolean isSymmetric

Whether the matrix is symmetric.

mainback to summary
private double[] main

Main diagonal of the tridiagonal matrix.

maxIterback to summary
private byte maxIter

Maximum number of iterations accepted in the implicit QL transformation

realEigenvaluesback to summary
private double[] realEigenvalues

Real part of the realEigenvalues.

secondaryback to summary
private double[] secondary

Secondary diagonal of the tridiagonal matrix.

transformerback to summary
private TriDiagonalTransformer transformer

Transformer to tridiagonal (may be null if matrix is already tridiagonal).

Constructor Detail

EigenDecompositionback to summary
public EigenDecomposition(final RealMatrix matrix) throws MathArithmeticException

Calculates the eigen decomposition of the given real matrix.

Supports decomposition of a general matrix since 3.1.

Parameters
matrix:RealMatrix

Matrix to decompose.

Exceptions
MathArithmeticException:
if the decomposition of a general matrix results in a matrix with zero norm
MaxCountExceededException:
if the algorithm fails to converge.
Since
3.1
EigenDecompositionback to summary
public EigenDecomposition(final RealMatrix matrix, final double splitTolerance) throws MathArithmeticException

Deprecated

in 3.1 (to be removed in 4.0) due to unused parameter

Calculates the eigen decomposition of the given real matrix.

Parameters
matrix:RealMatrix

Matrix to decompose.

splitTolerance:double

Dummy parameter (present for backward compatibility only).

Annotations
@Deprecated
Exceptions
MathArithmeticException:
if the decomposition of a general matrix results in a matrix with zero norm
MaxCountExceededException:
if the algorithm fails to converge.
EigenDecompositionback to summary
public EigenDecomposition(final double[] main, final double[] secondary)

Calculates the eigen decomposition of the symmetric tridiagonal matrix. The Householder matrix is assumed to be the identity matrix.

Parameters
main:double[]

Main diagonal of the symmetric tridiagonal form.

secondary:double[]

Secondary of the tridiagonal form.

Exceptions
MaxCountExceededException:
if the algorithm fails to converge.
Since
3.1
EigenDecompositionback to summary
public EigenDecomposition(final double[] main, final double[] secondary, final double splitTolerance)

Deprecated

in 3.1 (to be removed in 4.0) due to unused parameter

Calculates the eigen decomposition of the symmetric tridiagonal matrix. The Householder matrix is assumed to be the identity matrix.

Parameters
main:double[]

Main diagonal of the symmetric tridiagonal form.

secondary:double[]

Secondary of the tridiagonal form.

splitTolerance:double

Dummy parameter (present for backward compatibility only).

Annotations
@Deprecated
Exceptions
MaxCountExceededException:
if the algorithm fails to converge.

Method Detail

cdivback to summary
private Complex cdiv(final double xr, final double xi, final double yr, final double yi)

Performs a division of two complex numbers.

Parameters
xr:double

real part of the first number

xi:double

imaginary part of the first number

yr:double

real part of the second number

yi:double

imaginary part of the second number

Returns:Complex

result of the complex division

findEigenVectorsback to summary
private void findEigenVectors(final double[][] householderMatrix)

Find eigenvalues and eigenvectors (Dubrulle et al., 1971)

Parameters
householderMatrix:double[][]

Householder matrix of the transformation to tridiagonal form.

findEigenVectorsFromSchurback to summary
private void findEigenVectorsFromSchur(final SchurTransformer schur) throws MathArithmeticException

Find eigenvectors from a matrix transformed to Schur form.

Parameters
schur:SchurTransformer

the schur transformation of the matrix

Exceptions
MathArithmeticException:
if the Schur form has a norm of zero
getDback to summary
public RealMatrix getD()

Gets the block diagonal matrix D of the decomposition. D is a block diagonal matrix. Real eigenvalues are on the diagonal while complex values are on 2x2 blocks { {real +imaginary}, {-imaginary, real} }.

Returns:RealMatrix

the D matrix.

See Also
getRealEigenvalues(), getImagEigenvalues()
getDeterminantback to summary
public double getDeterminant()

Computes the determinant of the matrix.

Returns:double

the determinant of the matrix.

getEigenvectorback to summary
public RealVector getEigenvector(final int i)

Gets a copy of the ith eigenvector of the original matrix.

Parameters
i:int

Index of the eigenvector (counting from 0).

Returns:RealVector

a copy of the ith eigenvector of the original matrix.

See Also
getD()
getImagEigenvalueback to summary
public double getImagEigenvalue(final int i)

Gets the imaginary part of the ith eigenvalue of the original matrix.

Parameters
i:int

Index of the eigenvalue (counting from 0).

Returns:double

the imaginary part of the ith eigenvalue of the original matrix.

See Also
getD(), getImagEigenvalues(), getRealEigenvalue(int)
getImagEigenvaluesback to summary
public double[] getImagEigenvalues()

Gets a copy of the imaginary parts of the eigenvalues of the original matrix.

Returns:double[]

a copy of the imaginary parts of the eigenvalues of the original matrix.

See Also
getD(), getImagEigenvalue(int), getRealEigenvalues()
getRealEigenvalueback to summary
public double getRealEigenvalue(final int i)

Returns the real part of the ith eigenvalue of the original matrix.

Parameters
i:int

index of the eigenvalue (counting from 0)

Returns:double

real part of the ith eigenvalue of the original matrix.

See Also
getD(), getRealEigenvalues(), getImagEigenvalue(int)
getRealEigenvaluesback to summary
public double[] getRealEigenvalues()

Gets a copy of the real parts of the eigenvalues of the original matrix.

Returns:double[]

a copy of the real parts of the eigenvalues of the original matrix.

See Also
getD(), getRealEigenvalue(int), getImagEigenvalues()
getSolverback to summary
public DecompositionSolver getSolver()

Gets a solver for finding the A × X = B solution in exact linear sense.

Since 3.1, eigen decomposition of a general matrix is supported, but the DecompositionSolver only supports real eigenvalues.

Returns:DecompositionSolver

a solver

Exceptions
MathUnsupportedOperationException:
if the decomposition resulted in complex eigenvalues
getSquareRootback to summary
public RealMatrix getSquareRoot()

Computes the square-root of the matrix. This implementation assumes that the matrix is symmetric and positive definite.

Returns:RealMatrix

the square-root of the matrix.

Exceptions
MathUnsupportedOperationException:
if the matrix is not symmetric or not positive definite.
Since
3.1
getVback to summary
public RealMatrix getV()

Gets the matrix V of the decomposition. V is an orthogonal matrix, i.e. its transpose is also its inverse. The columns of V are the eigenvectors of the original matrix. No assumption is made about the orientation of the system axes formed by the columns of V (e.g. in a 3-dimension space, V can form a left- or right-handed system).

Returns:RealMatrix

the V matrix.

getVTback to summary
public RealMatrix getVT()

Gets the transpose of the matrix V of the decomposition. V is an orthogonal matrix, i.e. its transpose is also its inverse. The columns of V are the eigenvectors of the original matrix. No assumption is made about the orientation of the system axes formed by the columns of V (e.g. in a 3-dimension space, V can form a left- or right-handed system).

Returns:RealMatrix

the transpose of the V matrix.

hasComplexEigenvaluesback to summary
public boolean hasComplexEigenvalues()

Returns whether the calculated eigen values are complex or real.

The method performs a zero check for each element of the getImagEigenvalues() array and returns true if any element is not equal to zero.

Returns:boolean

true if the eigen values are complex, false otherwise

Since
3.1
transformToSchurback to summary
private SchurTransformer transformToSchur(final RealMatrix matrix)

Transforms the matrix to Schur form and calculates the eigenvalues.

Parameters
matrix:RealMatrix

Matrix to transform.

Returns:SchurTransformer

the Shur transform for this matrix

transformToTridiagonalback to summary
private void transformToTridiagonal(final RealMatrix matrix)

Transforms the matrix to tridiagonal form.

Parameters
matrix:RealMatrix

Matrix to transform.

org.apache.commons.math3.linear back to summary

private Class EigenDecomposition.Solver

extends Object
implements DecompositionSolver
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.linear.DecompositionSolver

Specialized solver.

Field Summary

Modifier and TypeField and Description
private final ArrayRealVector[]
eigenvectors

Eigenvectors.

private double[]
imagEigenvalues

Imaginary part of the realEigenvalues.

private double[]
realEigenvalues

Real part of the realEigenvalues.

Constructor Summary

AccessConstructor and Description
private
Solver(final double[]
Real parts of the eigenvalues.
realEigenvalues
,
final double[]
Imaginary parts of the eigenvalues.
imagEigenvalues
,
final ArrayRealVector[]
Eigenvectors.
eigenvectors
)

Builds a solver from decomposed matrix.

Method Summary

Modifier and TypeMethod and Description
private double

Returns:

the norm of ith (complex) eigenvalue.
eigenvalueNorm
(int
which eigenvalue to find the norm of
i
)

public RealMatrix

Returns:

the inverse matrix.
getInverse
()

Implements org.apache.commons.math3.linear.DecompositionSolver.getInverse.

Get the inverse of the decomposed matrix.

public boolean

Returns:

true if the decomposed matrix is non-singular.
isNonSingular
()

Implements org.apache.commons.math3.linear.DecompositionSolver.isNonSingular.

Checks whether the decomposed matrix is non-singular.

public RealVector

Returns:

a Vector X that minimizes the two norm of A × X - B.
solve
(final RealVector
Right-hand side of the equation A × X = B.
b
)

Implements org.apache.commons.math3.linear.DecompositionSolver.solve.

Solves the linear equation A × X = B for symmetric matrices A.

public RealMatrix
solve(RealMatrix
right-hand side of the equation A × X = B
b
)

Implements org.apache.commons.math3.linear.DecompositionSolver.solve.

Solve the linear equation A × X = B for matrices A.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

eigenvectorsback to summary
private final ArrayRealVector[] eigenvectors

Eigenvectors.

imagEigenvaluesback to summary
private double[] imagEigenvalues

Imaginary part of the realEigenvalues.

realEigenvaluesback to summary
private double[] realEigenvalues

Real part of the realEigenvalues.

Constructor Detail

Solverback to summary
private Solver(final double[] realEigenvalues, final double[] imagEigenvalues, final ArrayRealVector[] eigenvectors)

Builds a solver from decomposed matrix.

Parameters
realEigenvalues:double[]

Real parts of the eigenvalues.

imagEigenvalues:double[]

Imaginary parts of the eigenvalues.

eigenvectors:ArrayRealVector[]

Eigenvectors.

Method Detail

eigenvalueNormback to summary
private double eigenvalueNorm(int i)
Parameters
i:int

which eigenvalue to find the norm of

Returns:double

the norm of ith (complex) eigenvalue.

getInverseback to summary
public RealMatrix getInverse()

Implements org.apache.commons.math3.linear.DecompositionSolver.getInverse.

Get the inverse of the decomposed matrix.

Returns:RealMatrix

the inverse matrix.

Exceptions
SingularMatrixException:
if the decomposed matrix is singular.
isNonSingularback to summary
public boolean isNonSingular()

Implements org.apache.commons.math3.linear.DecompositionSolver.isNonSingular.

Checks whether the decomposed matrix is non-singular.

Returns:boolean

true if the decomposed matrix is non-singular.

solveback to summary
public RealVector solve(final RealVector b)

Implements org.apache.commons.math3.linear.DecompositionSolver.solve.

Solves the linear equation A × X = B for symmetric matrices A.

This method only finds exact linear solutions, i.e. solutions for which ||A × X - B|| is exactly 0.

Parameters
b:RealVector

Right-hand side of the equation A × X = B.

Returns:RealVector

a Vector X that minimizes the two norm of A × X - B.

Exceptions
DimensionMismatchException:
if the matrices dimensions do not match.
SingularMatrixException:
if the decomposed matrix is singular.
solveback to summary
public RealMatrix solve(RealMatrix b)

Implements org.apache.commons.math3.linear.DecompositionSolver.solve.

Doc from org.apache.commons.math3.linear.DecompositionSolver.solve.

Solve the linear equation A × X = B for matrices A.

The A matrix is implicit, it is provided by the underlying decomposition algorithm.

Parameters
b:RealMatrix

right-hand side of the equation A × X = B

Returns:RealMatrix

a matrix X that minimizes the two norm of A × X - B