org.apache.commons.math3.linear
Class SymmetricMatrix

java.lang.Object
  extended by org.apache.commons.math3.linear.RealLinearOperator
      extended by org.apache.commons.math3.linear.AbstractRealMatrix
          extended by org.apache.commons.math3.linear.SymmetricMatrix
All Implemented Interfaces:
Serializable, AnyMatrix, RealMatrix

public class SymmetricMatrix
extends AbstractRealMatrix
implements Serializable

Implementation of a symmetric matrix.
Operation are optimized to take advantage of symmetric properties of this kind of matrix.

Version:
$Id: SymmetricMatrix.java 16069 2016-05-16 14:04:07Z bignon $
See Also:
Serialized Form

Nested Class Summary
static class SymmetricMatrix.SYMMETRIC
          Enumerate to fill in a symmetric matrix only by its lower/upper part.
 
Constructor Summary
SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo, int n, int ld)
          Creates a matrix with the supplied dimensions.
SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo, int n, int ld, double[] d)
          Creates a matrix given the supplied dimensions and data array.
SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo, int n, int ld, double[][] d)
          Creates a matrix given the supplied dimensions and data array 2D.
SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo, int n, int ld, double[] d, boolean copyArray)
          Creates a matrix given the supplied dimensions and data array.
SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo, int n, int ld, RealMatrix m)
          Creates a symmetric matrix using a real matrix.
 
Method Summary
 SymmetricMatrix add(DiagonalMatrix m)
          Compute the sum of this and m.
 RealMatrix add(RealMatrix m)
          Returns the sum of this and m.
 SymmetricMatrix add(SymmetricMatrix m)
          Compute the sum of this and m.
 void addToEntry(int row, int column, double increment)
          Adds (in place) the specified value to the specified entry of this matrix.
 SymmetricMatrix copy()
          Returns a (deep) copy of this.
static SymmetricMatrix createDiagonalMatrixWithArray(int dim, int lda, double[] coeff)
          Creates an instance of SymmetricMatrix which is diagonal of input element.
 SymmetricMatrix createMatrix(int lda, int dim)
          Create a new RealMatrix of the same type as the instance with the supplied row and column dimensions.
 int getColumnDimension()
          Returns the number of columns of this matrix.
 double getEntry(int row, int column)
          Get the entry in the specified row and column.
 int getRowDimension()
          Returns the number of rows of this matrix.
 boolean isSymmetric()
          Checks if this is a symmetric matrix
 RealMatrix multiply(DiagonalMatrix m)
          Returns the result of postmultiplying this by m.
 RealMatrix multiply(RealMatrix m)
          Returns the result of postmultiplying this by m.
 RealMatrix multiply(SymmetricMatrix m)
          Returns the result of postmultiplying this by m.
 void multiplyEntry(int row, int column, double factor)
          Multiplies (in place) the specified entry of this matrix by the specified value.
static SymmetricMatrix newByCopy(SymmetricMatrix.SYMMETRIC uplo, int n, int ld, SymmetricMatrix.SYMMETRIC uploMat, RealMatrix mat)
          Create a symmetric matrix by copy of a triangle of a matrix.
static SymmetricMatrix newByCopyAndSymmetrize(SymmetricMatrix.SYMMETRIC uplo, int n, int ld, RealMatrix mat)
          Create a symmetric matrix by computation of the mean of the 2 triangles of a matrix.
static SymmetricMatrix newByCopyOfPackedStorage(SymmetricMatrix.SYMMETRIC uplo, int n, int ld, double[] d)
          Creates a symmetric matrix using one of the triangles stored in 'packed' storage.
 SymmetricMatrix scalarMultiply(double d)
          Returns the result of multiplying each entry of this by d.
 void setEntry(int row, int column, double value)
          Set the entry in the specified row and column.
 void setSubMatrix(double[][] subMatrix, int row, int column)
          Replace the submatrix starting at row, column using data in the input subMatrix array.
 void setSubMatrixWithMatrix(RealMatrix subMatrix, int row, int column)
          Method setSubMatrix using a RealMatrix for subMatrix instead of an array
 SymmetricMatrix subtract(DiagonalMatrix m)
          Compute this minus m.
 RealMatrix subtract(RealMatrix m)
          Returns this minus m.
 SymmetricMatrix subtract(SymmetricMatrix m)
          Compute this minus m.
static void symmetricMatrixOperation(double alpha, SymmetricMatrix symA, RealMatrix matB, double beta, RealMatrix matC)
          Performs the matrix-matrix operations C := alpha*A*B + beta*C where alpha and beta are scalars, A is a symmetric matrix and B and C are n by n matrices.
 RealMatrix transpose()
          Returns the transpose of this matrix.
 
Methods inherited from class org.apache.commons.math3.linear.AbstractRealMatrix
copySubMatrix, copySubMatrix, equals, getColumn, getColumnMatrix, getColumnVector, getData, getFrobeniusNorm, getNorm, getRow, getRowMatrix, getRowVector, getSubMatrix, getSubMatrix, getTrace, hashCode, isAntisymmetric, isDiagonal, isInvertible, isOrthogonal, isSquare, isSymmetric, operate, operate, power, preMultiply, preMultiply, preMultiply, scalarAdd, setColumn, setColumnMatrix, setColumnVector, setRow, setRowMatrix, setRowVector, toString, walkInColumnOrder, walkInColumnOrder, walkInColumnOrder, walkInColumnOrder, walkInOptimizedOrder, walkInOptimizedOrder, walkInOptimizedOrder, walkInOptimizedOrder, walkInRowOrder, walkInRowOrder, walkInRowOrder, walkInRowOrder
 
Methods inherited from class org.apache.commons.math3.linear.RealLinearOperator
isTransposable, operateTranspose
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SymmetricMatrix

public SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo,
                       int n,
                       int ld)
Creates a matrix with the supplied dimensions.

Parameters:
uplo - the triangle that will be used
n - the dimension of the matrix
ld - the leading dimension of the matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements

SymmetricMatrix

public SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo,
                       int n,
                       int ld,
                       double[] d,
                       boolean copyArray)
Creates a matrix given the supplied dimensions and data array.

Parameters:
uplo - the triangle that will be used
n - the dimension of the matrix
ld - the leading dimension of the matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements
d - the array to be viewed as a conventional full storage symmetric matrix
copyArray - if the array must be copied

SymmetricMatrix

public SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo,
                       int n,
                       int ld,
                       double[] d)
                throws NonSquareMatrixException
Creates a matrix given the supplied dimensions and data array.

Parameters:
uplo - the triangle that will be used
n - the dimension of the matrix
ld - the leading dimension of the matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements
d - the array to be viewed as a conventional full storage symmetric matrix
Throws:
NonSquareMatrixException - if matrix is not square

SymmetricMatrix

public SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo,
                       int n,
                       int ld,
                       double[][] d)
                throws NonSquareMatrixException
Creates a matrix given the supplied dimensions and data array 2D. If data array has no symmetric elements, symmetrization is performed by calling the method newByCopy(SYMMETRIC, int, int, SYMMETRIC, RealMatrix).

Parameters:
uplo - the triangle that will be used
n - the dimension of the matrix
ld - the leading dimension of the created matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements
d - data for the new matrix
Throws:
NonSquareMatrixException - if matrix is not square

SymmetricMatrix

public SymmetricMatrix(SymmetricMatrix.SYMMETRIC uplo,
                       int n,
                       int ld,
                       RealMatrix m)
                throws NonSquareMatrixException
Creates a symmetric matrix using a real matrix.

Parameters:
uplo - the triangle that will be used
n - the dimension of the matrix
ld - the leading dimension of the created matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements
m - the real matrix to be used
Throws:
NonSquareMatrixException - if matrix is not square
Method Detail

newByCopy

public static SymmetricMatrix newByCopy(SymmetricMatrix.SYMMETRIC uplo,
                                        int n,
                                        int ld,
                                        SymmetricMatrix.SYMMETRIC uploMat,
                                        RealMatrix mat)
                                 throws NonSquareMatrixException
Create a symmetric matrix by copy of a triangle of a matrix.

Parameters:
uplo - the triangle to use
n - the dimension of the created matrix
ld - the leading dimension of the created matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements
uploMat - the triangle to use from the input matrix
mat - the matrix from which the data are extracted
Returns:
The symmetric matrix according to the input triangle
Throws:
NonSquareMatrixException - if matrix is not square

newByCopyAndSymmetrize

public static SymmetricMatrix newByCopyAndSymmetrize(SymmetricMatrix.SYMMETRIC uplo,
                                                     int n,
                                                     int ld,
                                                     RealMatrix mat)
Create a symmetric matrix by computation of the mean of the 2 triangles of a matrix.

Parameters:
uplo - the triangle to use
n - the dimension of the created matrix
ld - the leading dimension of the created matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements
mat - the matrix from which the data are extracted
Returns:
The symmetric matrix based on the mean of the 2 triangles

newByCopyOfPackedStorage

public static SymmetricMatrix newByCopyOfPackedStorage(SymmetricMatrix.SYMMETRIC uplo,
                                                       int n,
                                                       int ld,
                                                       double[] d)
                                                throws NonSquareMatrixException
Creates a symmetric matrix using one of the triangles stored in 'packed' storage. The size of the packed storage form can be higher than the number of elements really used to construct the matrix.

Parameters:
uplo - lower/upper known part of the matrix
n - the dimension of the created matrix
ld - the leading dimension of the created matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements
d - data in 'packed storage'
Returns:
The symmetric matrix according to the packed storage
Throws:
NonSquareMatrixException - if matrix is not square

createMatrix

public SymmetricMatrix createMatrix(int lda,
                                    int dim)
                             throws NotStrictlyPositiveException
Create a new RealMatrix of the same type as the instance with the supplied row and column dimensions.

Specified by:
createMatrix in interface RealMatrix
Specified by:
createMatrix in class AbstractRealMatrix
Parameters:
lda - the number of rows in the new matrix
dim - the number of columns in the new matrix
Returns:
a new matrix of the same type as the instance
Throws:
NotStrictlyPositiveException - if row or column dimension is not positive.

createDiagonalMatrixWithArray

public static SymmetricMatrix createDiagonalMatrixWithArray(int dim,
                                                            int lda,
                                                            double[] coeff)
                                                     throws NotStrictlyPositiveException
Creates an instance of SymmetricMatrix which is diagonal of input element.

Parameters:
dim - the dimension of the created matrix
lda - the leading dimension of the created matrix. ld is normaly equal to n. It can be set larger in some case for performance improvements
coeff - value of diagonals elements
Returns:
The diagonal matrix of diagonal value coeff
Throws:
NotStrictlyPositiveException - if n or ld are not positive

copy

public SymmetricMatrix copy()
Returns a (deep) copy of this.

Specified by:
copy in interface RealMatrix
Specified by:
copy in class AbstractRealMatrix
Returns:
matrix copy

transpose

public RealMatrix transpose()
Returns the transpose of this matrix.

Specified by:
transpose in interface RealMatrix
Overrides:
transpose in class AbstractRealMatrix
Returns:
transpose matrix

symmetricMatrixOperation

public static void symmetricMatrixOperation(double alpha,
                                            SymmetricMatrix symA,
                                            RealMatrix matB,
                                            double beta,
                                            RealMatrix matC)
Performs the matrix-matrix operations C := alpha*A*B + beta*C where alpha and beta are scalars, A is a symmetric matrix and B and C are n by n matrices. This general method aims to perform all elementary operations involving instances of SymmetricMatrix, DiagonalMatrix or RealMatrix

Parameters:
alpha - the first value to use in above linear combination
symA - the input symmetric matrix
matB - the input matrix, instance of RealMatrix
beta - the second value to use in above linear combination
matC - the input/output matrix, instance of RealMatrix

add

public SymmetricMatrix add(SymmetricMatrix m)
                    throws DimensionMismatchException
Compute the sum of this and m.

Parameters:
m - matrix to be added
Returns:
this + m
Throws:
DimensionMismatchException - if m is not the same size as this

add

public SymmetricMatrix add(DiagonalMatrix m)
                    throws IllegalArgumentException
Compute the sum of this and m.

Parameters:
m - Diagonal matrix to be added
Returns:
this + m
Throws:
IllegalArgumentException - if m is not the same size as this

add

public RealMatrix add(RealMatrix m)
               throws IllegalArgumentException
Returns the sum of this and m.

Specified by:
add in interface RealMatrix
Overrides:
add in class AbstractRealMatrix
Parameters:
m - matrix to be added
Returns:
this + m
Throws:
MatrixDimensionMismatchException - if m is not the same size as this.
IllegalArgumentException

subtract

public SymmetricMatrix subtract(SymmetricMatrix m)
                         throws IllegalArgumentException
Compute this minus m.

Parameters:
m - matrix to be subtracted
Returns:
this - m
Throws:
IllegalArgumentException - if m is not the same size as this

subtract

public SymmetricMatrix subtract(DiagonalMatrix m)
                         throws IllegalArgumentException
Compute this minus m.

Parameters:
m - Diagonal matrix to be added
Returns:
this + m
Throws:
IllegalArgumentException - if m is not the same size as this

subtract

public RealMatrix subtract(RealMatrix m)
                    throws IllegalArgumentException
Returns this minus m.

Specified by:
subtract in interface RealMatrix
Overrides:
subtract in class AbstractRealMatrix
Parameters:
m - matrix to be subtracted
Returns:
this - m
Throws:
MatrixDimensionMismatchException - if m is not the same size as this.
IllegalArgumentException

multiply

public RealMatrix multiply(SymmetricMatrix m)
                    throws IllegalArgumentException
Returns the result of postmultiplying this by m.

Parameters:
m - matrix to postmultiply by
Returns:
this*m
Throws:
IllegalArgumentException - if columnDimension(this) != rowDimension(m)

multiply

public RealMatrix multiply(DiagonalMatrix m)
                    throws IllegalArgumentException
Returns the result of postmultiplying this by m.

Parameters:
m - Diagonal matrix to postmultiply by
Returns:
this*m
Throws:
IllegalArgumentException - if columnDimension(this) != rowDimension(m)

multiply

public RealMatrix multiply(RealMatrix m)
                    throws IllegalArgumentException
Returns the result of postmultiplying this by m.

Specified by:
multiply in interface RealMatrix
Overrides:
multiply in class AbstractRealMatrix
Parameters:
m - matrix to postmultiply by
Returns:
this * m
Throws:
DimensionMismatchException - if columnDimension(this) != rowDimension(m)
IllegalArgumentException

scalarMultiply

public SymmetricMatrix scalarMultiply(double d)
Returns the result of multiplying each entry of this by d.

Specified by:
scalarMultiply in interface RealMatrix
Overrides:
scalarMultiply in class AbstractRealMatrix
Parameters:
d - value to multiply all entries by
Returns:
d * this

setSubMatrix

public void setSubMatrix(double[][] subMatrix,
                         int row,
                         int column)
                  throws NoDataException,
                         OutOfRangeException,
                         DimensionMismatchException,
                         NullArgumentException
Replace the submatrix starting at row, column using data in the input subMatrix array. Indexes are 0-based.

Example:
Starting with

 1  2  3  4
 5  6  7  8
 9  0  1  2
 
and subMatrix = {{3, 4} {5,6}}, invoking setSubMatrix(subMatrix,1,1)) will result in
 1  2  3  4
 5  3  4  8
 9  5  6  2
 

Specified by:
setSubMatrix in interface RealMatrix
Overrides:
setSubMatrix in class AbstractRealMatrix
Parameters:
subMatrix - array containing the submatrix replacement data
row - row coordinate of the top, left element to be replaced
column - column coordinate of the top, left element to be replaced
Throws:
NoDataException - if subMatrix is empty.
OutOfRangeException - if subMatrix does not fit into this matrix from element in (row, column).
DimensionMismatchException - if subMatrix is not rectangular (not all rows have the same length) or empty.
NullArgumentException - if subMatrix is null.

setSubMatrixWithMatrix

public void setSubMatrixWithMatrix(RealMatrix subMatrix,
                                   int row,
                                   int column)
                            throws NoDataException,
                                   OutOfRangeException,
                                   DimensionMismatchException,
                                   NullArgumentException
Method setSubMatrix using a RealMatrix for subMatrix instead of an array

Parameters:
subMatrix - matrix containing the submatrix replacement data
row - row coordinate of the top, left element to be replaced
column - column coordinate of the top, left element to be replaced
Throws:
NoDataException - if subMatrix is empty.
OutOfRangeException - if subMatrix does not fit into this matrix from element in (row, column).
DimensionMismatchException - if subMatrix is not rectangular (not all rows have the same length) or empty.
NullArgumentException - if subMatrix is null.
Since:
3.1

getEntry

public double getEntry(int row,
                       int column)
                throws OutOfRangeException
Get the entry in the specified row and column. Row and column indices start at 0.

Specified by:
getEntry in interface RealMatrix
Specified by:
getEntry in class AbstractRealMatrix
Parameters:
row - Row index of entry to be fetched.
column - Column index of entry to be fetched.
Returns:
the matrix entry at (row, column).
Throws:
OutOfRangeException - if the row or column index is not valid.

setEntry

public void setEntry(int row,
                     int column,
                     double value)
              throws OutOfRangeException
Set the entry in the specified row and column. Row and column indices start at 0.

Specified by:
setEntry in interface RealMatrix
Specified by:
setEntry in class AbstractRealMatrix
Parameters:
row - Row index of entry to be set.
column - Column index of entry to be set.
value - the new value of the entry.
Throws:
OutOfRangeException - if the row or column index is not valid

addToEntry

public void addToEntry(int row,
                       int column,
                       double increment)
                throws OutOfRangeException
Adds (in place) the specified value to the specified entry of this matrix. Row and column indices start at 0.

Specified by:
addToEntry in interface RealMatrix
Overrides:
addToEntry in class AbstractRealMatrix
Parameters:
row - Row index of the entry to be modified.
column - Column index of the entry to be modified.
increment - value to add to the matrix entry.
Throws:
OutOfRangeException - if the row or column index is not valid.

multiplyEntry

public void multiplyEntry(int row,
                          int column,
                          double factor)
                   throws OutOfRangeException
Multiplies (in place) the specified entry of this matrix by the specified value. Row and column indices start at 0.

Specified by:
multiplyEntry in interface RealMatrix
Overrides:
multiplyEntry in class AbstractRealMatrix
Parameters:
row - Row index of the entry to be modified.
column - Column index of the entry to be modified.
factor - Multiplication factor for the matrix entry.
Throws:
OutOfRangeException - if the row or column index is not valid.

getRowDimension

public int getRowDimension()
Returns the number of rows of this matrix.

Specified by:
getRowDimension in interface AnyMatrix
Specified by:
getRowDimension in class AbstractRealMatrix
Returns:
the number of rows.

getColumnDimension

public int getColumnDimension()
Returns the number of columns of this matrix.

Specified by:
getColumnDimension in interface AnyMatrix
Specified by:
getColumnDimension in class AbstractRealMatrix
Returns:
the number of columns.

isSymmetric

public boolean isSymmetric()
Checks if this is a symmetric matrix

Specified by:
isSymmetric in interface RealMatrix
Overrides:
isSymmetric in class AbstractRealMatrix
Returns:
true if this is a symmetric matrix


Copyright © 2016 CNES. All Rights Reserved.