SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal
Class DiagonalMatrix

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixMathImpl<T>
      extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixStorageImpl<T>
          extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalMatrix
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Matrix, MatrixAccessor, MatrixRing, Densifiable, MatrixDimension

public class DiagonalMatrix
extends MatrixStorageImpl<T>

This class represents a matrix with non-zero entries only on the main diagonal.

See Also:
Wikipedia: Diagonal matrix

Constructor Summary
DiagonalMatrix(DiagonalMatrix that)
          Copy constructor.
DiagonalMatrix(double[] data)
          Construct a diagonal matrix from a double[] array.
DiagonalMatrix(int dim)
          Construct a diagonal matrix of dimension dim * dim.
 
Method Summary
 DiagonalMatrix add(DiagonalMatrix that)
          this + that Add with a DiagonalMatrix.
 DiagonalMatrix 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.
 DenseVector diagonal()
          Get a copy of the diagonal of the matrix.
 boolean equals(java.lang.Object obj)
          Check if two matrices (of different implementations) are equal.
 DenseVector getColumn(int col)
          Get a specified column as a vector.
 DenseVector getRow(int row)
          Get a specified row as a vector.
 int hashCode()
           
 DiagonalMatrix minus(DiagonalMatrix that)
          this - that Subtract a DiagonalMatrix.
 DiagonalMatrix multiply(DiagonalMatrix that)
          this %*% that Multiply by a DiagonalMatrix.
 Vector multiply(Vector v)
          Right multiply this matrix, A by a vector.
 DiagonalMatrix ONE()
          Get an identity matrix that has the same dimension as this matrix.
 DiagonalMatrix scaled(double scalar)
          scalar * this
 DenseVector subDiagonal()
          Get a copy of the sub-diagonal of the matrix.
 DenseVector superDiagonal()
          Get a copy of the super-diagonal of the matrix.
 DiagonalMatrix t()
          The transpose of a diagonal matrix is the same as itself.
 DenseMatrix toDense()
          Densify a matrix, i.e., convert a matrix implementation to a standard dense matrix, DenseMatrix.
 java.lang.String toString()
           
 DiagonalMatrix 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
get, getMatrixData, set, setMatrixData
 
Methods inherited from class com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixMathImpl
add, call, getSample, minus, multiply, nCols, nRows, opposite, setColumn, setColumn, setRow, setRow
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DiagonalMatrix

public DiagonalMatrix(double[] data)
Construct a diagonal matrix from a double[] array.

For example,

         new double[][]{
              {1, 2, 3, 4, 5},
          }
 
is
              | 1  0  0  0  0 |
              | 0  2  0  0  0 |
              | 0  0  3  0  0 |
              | 0  0  0  4  0 |
              | 0  0  0  0  5 |
 

Parameters:
data - the 1D array input

DiagonalMatrix

public DiagonalMatrix(int dim)
Construct a diagonal matrix of dimension dim * dim.

Parameters:
dim - dimension; for a square matrix, it is the number of rows or columns

DiagonalMatrix

public DiagonalMatrix(DiagonalMatrix that)
Copy constructor.

Parameters:
that - a DiagonalMatrix
Method Detail

deepCopy

public DiagonalMatrix 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.

Returns:
an independent copy of Matrix instance

add

public DiagonalMatrix add(DiagonalMatrix that)
this + that

Add with a DiagonalMatrix.

Parameters:
that - another matrix
Returns:
the sum of this and that

minus

public DiagonalMatrix minus(DiagonalMatrix that)
this - that

Subtract a DiagonalMatrix.

Parameters:
that - another matrix
Returns:
the difference between this and that

multiply

public DiagonalMatrix multiply(DiagonalMatrix that)
this %*% that

Multiply by a DiagonalMatrix.

Parameters:
that - another matrix
Returns:
the product of this and that

multiply

public Vector multiply(Vector v)
Description copied from interface: Matrix
Right multiply this matrix, A by a vector.

Specified by:
multiply in interface Matrix
Overrides:
multiply in class MatrixMathImpl<DiagonalMatrix>
Parameters:
v - a vector
Returns:
A %*% v

scaled

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

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

t

public DiagonalMatrix t()
The transpose of a diagonal matrix is the same as itself.

Specified by:
t in interface MatrixRing
Overrides:
t in class MatrixMathImpl<DiagonalMatrix>
Returns:
a copy

ZERO

public DiagonalMatrix 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.

Returns:
0

ONE

public DiagonalMatrix 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).

Returns:
an identity matrix

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

diagonal

public DenseVector diagonal()
Get a copy of the diagonal of the matrix.

Returns:
a copy of the diagonal

superDiagonal

public DenseVector superDiagonal()
Get a copy of the super-diagonal of the matrix.

Returns:
a copy of the super-diagonal

subDiagonal

public DenseVector subDiagonal()
Get a copy of the sub-diagonal of the matrix.

Returns:
a copy of the sub-diagonal

getRow

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

Specified by:
getRow in interface MatrixAccessor
Overrides:
getRow in class MatrixMathImpl<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>
Parameters:
row - the row index
Returns:
a vector A[row, ]

getColumn

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

Specified by:
getColumn in interface MatrixAccessor
Overrides:
getColumn in class MatrixMathImpl<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>
Parameters:
col - the column index
Returns:
a vector A[, col]

toString

public java.lang.String toString()
Overrides:
toString in class MatrixMathImpl<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>

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<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>
Returns:
true iff two matrices have the same values, entry-by-entry

hashCode

public int hashCode()
Overrides:
hashCode in class MatrixStorageImpl<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>

SuanShu, a Java numerical and statistical library

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