SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.triangle
Class SymmetricMatrix

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixMathImpl<T>
      extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixStorageImpl<SymmetricMatrix>
          extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.triangle.SymmetricMatrix
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Matrix, MatrixAccessor, MatrixRing, Densifiable, MatrixDimension
Direct Known Subclasses:
BorderedHessian, Hessian, HilbertMatrix

public class SymmetricMatrix
extends MatrixStorageImpl<SymmetricMatrix>
implements Densifiable

A symmetric matrix is a square matrix such that its transpose equals to itself, i.e.,

A[i][j] = A[j][i]

We implement this class by storing the data using an lower triangular matrix, e.g., LowerTriangularMatrix. This will reduce the storage cost by almost half compared to DenseMatrix.

We choose to use lower triangular matrix instead of upper triangular matrix for visual reason. A double[] data arrangement resembles a lower triangular matrix.

For example,

         new double[][]{
                  {1},
                  {2, 3},
                  {4, 5, 6},
                  {7, 8, 9, 10},
                  {11, 12, 13, 14, 15}});
 

See Also:
Wikipedia: Symmetric matrix

Field Summary
 int dim
          dimension; SymmetricMatrix is always square
 
Constructor Summary
SymmetricMatrix(double[][] data)
          Construct a symmetric matrix from a 2D double[][] array.
SymmetricMatrix(int dim)
          Construct a symmetric matrix of dimension dim * dim.
SymmetricMatrix(SymmetricMatrix S)
          Copy constructor performing a deep copy.
 
Method Summary
 SymmetricMatrix add(SymmetricMatrix that)
           
 SymmetricMatrix deepCopy()
          The implementation can return an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
 boolean equals(java.lang.Object obj)
          Check if two matrices (of different implementations) are equal.
 double get(int row, int col)
          Get the matrix entry at [row, col].
 DenseVector getColumn(int col)
          Get a specified column as a vector.
 DenseVector getRow(int row)
          Get a specified row as a vector.
 int hashCode()
           
 SymmetricMatrix minus(SymmetricMatrix that)
           
 SymmetricMatrix ONE()
          Get an identity matrix that has the same dimension as this matrix.
 SymmetricMatrix scaled(double scalar)
          scalar * this
 void set(int row, int col, double value)
          Set the matrix entry at [row, col] to value.
 SymmetricMatrix t()
          t(A) = A.
 DenseMatrix toDense()
          Densify a matrix, i.e., convert a matrix implementation to a standard dense matrix, DenseMatrix.
 java.lang.String toString()
           
 SymmetricMatrix ZERO()
          The additive element 0 in the group, such that for all elements a in the group, the equation 0 + a = a + 0 = a holds.
 
Methods inherited from class com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixStorageImpl
getMatrixData, setMatrixData
 
Methods inherited from class com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixMathImpl
add, call, getSample, minus, multiply, multiply, nCols, nRows, opposite, setColumn, setColumn, setRow, setRow
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

dim

public final int dim
dimension; SymmetricMatrix is always square

Constructor Detail

SymmetricMatrix

public SymmetricMatrix(int dim)
Construct a symmetric matrix of dimension dim * dim. Space for the matrix data is not yet allocated.

Parameters:
dim - symmetric matrix dimension, i.e., the number of rows or number of columns

SymmetricMatrix

public SymmetricMatrix(double[][] data)
Construct a symmetric matrix from a 2D double[][] array. The array contains only the lower triangular part (main diagonal inclusive) of the whole matrix.

Parameters:
data - the 2D double[][] array

SymmetricMatrix

public SymmetricMatrix(SymmetricMatrix S)
Copy constructor performing a deep copy.

Parameters:
S - the matrix to be copied
Method Detail

deepCopy

public SymmetricMatrix deepCopy()
Description copied from interface: Matrix
The implementation can return an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.

Override the return type.

Specified by:
deepCopy in interface DeepCopyable
Specified by:
deepCopy in interface Matrix
Returns:
an independent copy of Matrix instance

toDense

public DenseMatrix toDense()
Description copied from interface: Densifiable
Densify a matrix, i.e., convert a matrix implementation to a standard dense matrix, DenseMatrix.

This is equivalent to

new DenseMatrix(Matrix A)

Individual matrix implementation may optimize the conversion by taking advantage of having access to the class' private members. In other words, toDense() is in general more efficient than explicit copy construction.

As an example, for some long matrix computations, we may not be sure about the return type. We can always cast it to the standard matrix. That is,

DenseMatrix A = B.multiply(C).add(D).minus(E).toDense();

This statement always works regardless of the types of B, C, D, and E.

Specified by:
toDense in interface Densifiable
Returns:
a matrix representation in DenseMatrix

set

public void set(int row,
                int col,
                double value)
         throws MatrixAccessException
Description copied from interface: MatrixAccessor
Set the matrix entry at [row, col] to value.

This is the only method that may change the entries of a matrix.

Specified by:
set in interface MatrixAccessor
Overrides:
set in class MatrixStorageImpl<SymmetricMatrix>
Parameters:
row - the row index
col - the column index
value - the value to set A[row, col] to
Throws:
MatrixAccessException - if row or col is out of range

get

public double get(int row,
                  int col)
           throws MatrixAccessException
Description copied from interface: MatrixAccessor
Get the matrix entry at [row, col].

Specified by:
get in interface MatrixAccessor
Overrides:
get in class MatrixStorageImpl<SymmetricMatrix>
Parameters:
row - the row index
col - the column index
Returns:
A[row, col]
Throws:
MatrixAccessException - if row or col is out of range

getRow

public DenseVector getRow(int row)
                   throws MatrixAccessException
Description copied from interface: MatrixAccessor
Get a specified row as a vector.

Specified by:
getRow in interface MatrixAccessor
Overrides:
getRow in class MatrixMathImpl<SymmetricMatrix>
Parameters:
row - the row index
Returns:
a vector A[row, ]
Throws:
MatrixAccessException - when row < 1, or when row > number of rows

getColumn

public DenseVector getColumn(int col)
                      throws MatrixAccessException
Description copied from interface: MatrixAccessor
Get a specified column as a vector.

Specified by:
getColumn in interface MatrixAccessor
Overrides:
getColumn in class MatrixMathImpl<SymmetricMatrix>
Parameters:
col - the column index
Returns:
a vector A[, col]
Throws:
MatrixAccessException - when col < 1, or when col > number of columns

add

public SymmetricMatrix add(SymmetricMatrix that)

minus

public SymmetricMatrix minus(SymmetricMatrix that)

ZERO

public SymmetricMatrix ZERO()
Description copied from interface: AbelianGroup
The additive element 0 in the group, such that for all elements a in the group, the equation
0 + a = a + 0 = a
holds.

Specified by:
ZERO in interface AbelianGroup<Matrix>
Specified by:
ZERO in interface MatrixRing
Returns:
0

ONE

public SymmetricMatrix ONE()
Description copied from interface: MatrixRing
Get an identity matrix that has the same dimension as this matrix.

For a non-square matrix, it zeros out the rows (columns) with index > nCols (nRows).

Specified by:
ONE in interface Monoid<Matrix>
Specified by:
ONE in interface MatrixRing
Returns:
an identity matrix

t

public SymmetricMatrix t()
t(A) = A.

Specified by:
t in interface MatrixRing
Overrides:
t in class MatrixMathImpl<SymmetricMatrix>
Returns:
itself

scaled

public SymmetricMatrix scaled(double scalar)
Description copied from interface: Matrix
scalar * this

Specified by:
scaled in interface Matrix
Overrides:
scaled in class MatrixMathImpl<SymmetricMatrix>
Parameters:
scalar - a double
Returns:
scalar * this

toString

public java.lang.String toString()
Overrides:
toString in class MatrixMathImpl<SymmetricMatrix>

equals

public boolean equals(java.lang.Object obj)
Description copied from class: MatrixStorageImpl
Check if two matrices (of different implementations) are equal. They are equal iff two matrices have the same values, entry-by-entry.

Overrides:
equals in class MatrixStorageImpl<SymmetricMatrix>
Returns:
true iff two matrices have the same values, entry-by-entry

hashCode

public int hashCode()
Overrides:
hashCode in class MatrixStorageImpl<SymmetricMatrix>

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.