ru.sscc.matrix
Class DenseMatrix

java.lang.Object
  |
  +--ru.sscc.matrix.RealMatrix
        |
        +--ru.sscc.matrix.DenseMatrix

public class DenseMatrix
extends RealMatrix

An implementation of a rectangular dense matrix. In simple case the matrix is stored in the container "by rows", e.g. (i,j)th entry of the matrix is mapped to the (i*nColumns+j)-th entry of the matrix container. The "by columns" ordering is also possible. In common case, the position of (i,j)th entry in the underlying container is calculated by the formula startIndex + i*rowStep + j*columnStep

Common operations upon the matrix are implemented: matrix-by-vector multiplication, transposition, matrix assignment, and creation of a submatrix. A transposed matrix and submatrix share their bodies with the parent matrix.

See Also:
Serialized Form

Field Summary
 int columnStep
          The distance between two neighbour entries of the same row.
 int rowStep
          The distance between two neighbour entries of the same column.
 int startIndex
          The index of the first matrix entry in the container.
 
Fields inherited from class ru.sscc.matrix.RealMatrix
entries, nColumns, nRows
 
Constructor Summary
  DenseMatrix(int nRows, int nColumns)
          Constructs an instance with a clear double[] type matrix container and the "by rows" ordering.
  DenseMatrix(int nRows, int nColumns, boolean byRows)
          Constructs an instance with a clear double[] type matrix container with selection of "by rows" or "by columns" ordering.
  DenseMatrix(RealContainer entries, int nRows, int nColumns)
          Constructs an instance based on a user's real data container and ordered "by rows".
  DenseMatrix(RealContainer entries, int nRows, int nColumns, boolean byRows)
          Constructs an instance based on a user's real data container with selection of "by rows" or "by columns" ordering.
protected DenseMatrix(RealContainer entries, int nRows, int nColumns, int startIndex, int rowStep, int columnStep)
          Constructs an instance based on a submatrix of a user's real data container.
  DenseMatrix(RealMatrix matrix)
          Constructs "by rows" ordered instance as a copy of a real matrix.
  DenseMatrix(RealMatrix matrix, boolean byRows)
          Constructs an instance as a copy of a real matrix with selection of "by rows" or "by columns" ordering.
 
Method Summary
 double add(int i, int j, double value)
          Adds the value of a matrix entry at the position (i,j).
 void assign(DenseMatrix matrix)
          Assignment: this = matrix (a dense matrix on the right).
 void assign(RealMatrix matrix)
          Assignment: this = matrix (an arbitrary real matrix on the right).
 java.lang.Object clone()
          Clones the matrix.
 double get(int i, int j)
          Gets a value of a matrix entry at the position (i,j).
 RealVector getColumn(int j)
          Returns the j-th column of the matrix (the vector is constructed upon the matrix container).
 RealVector getRow(int i)
          Returns the i-th row of the matrix (the vector is constructed upon the matrix container).
 double mul(int i, int j, double value)
          Multiplies a matrix entry at the position (i,j) by a value.
 void multiply(RealVector source, RealVector target)
          Multiplies the matrix by a column-vector.
 void multiplyAndAdd(double multiple, RealVector source, RealVector target)
          Calculates y += a * Ax, eg multiplies the matrix by a source column-vector, then multiplies the result by a real number and adds the result to the target vector.
 void multiplyAndAddT(double multiple, RealVector source, RealVector target)
          Calculates y += a * ATx, eg multiplies the transposed matrix by a source column-vector, then multiplies the result by a real number and adds the result to the target vector.
 void multiplyT(RealVector source, RealVector target)
          Multiplies the transposed matrix by a column-vector.
 double norm()
          Calculates the euclidean matrix norm (the square root of the sum of squared entries).
 double normMax()
          Calculates the matrix norm subordinated to the vector max-norm.
 double normSum()
          Calculates the matrix norm subordinated to the vector sum-norm.
protected  int pos(int i, int j)
          Returns the position of (i,j)th matrix entry in the container.
 void reuse()
          Allows to use the matrix in algebraic operations.
 double set(int i, int j, double value)
          Sets the value of a matrix entry at the position (i,j).
 DenseMatrix submatrix(int i, int j, int nRows, int nColumns)
          Generates a submatrix that starts from (i,j)th position of the matrix.
 DenseMatrix submatrix(int i, int j, int nRows, int nColumns, int rowInc, int columnInc)
          Generates a submatrix that starts from (i,j)th position of the matrix and is stored in it with specified increments.
 DenseMatrix transpose()
          Generates the transposed matrix based on the same container.
 
Methods inherited from class ru.sscc.matrix.RealMatrix
columnsNumber, ensureAlgebraic, ensureDimensions, ensureDimensions, ensureSquareMatrix, getContainer, lock, multiplyNN, multiplyNT, multiplyTN, multiplyTT, relativeAccuracy, rowsNumber, unlock
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

startIndex

public final int startIndex
The index of the first matrix entry in the container.

rowStep

public final int rowStep
The distance between two neighbour entries of the same column.

columnStep

public final int columnStep
The distance between two neighbour entries of the same row.
Constructor Detail

DenseMatrix

public DenseMatrix(int nRows,
                   int nColumns)
Constructs an instance with a clear double[] type matrix container and the "by rows" ordering.
Parameters:
nRows - a number of matrix rows
nColumns - a number of matrix columns

DenseMatrix

public DenseMatrix(int nRows,
                   int nColumns,
                   boolean byRows)
Constructs an instance with a clear double[] type matrix container with selection of "by rows" or "by columns" ordering.
Parameters:
nRows - a number of matrix rows
nColumns - a number of matrix columns
byRows - equals true if "by rows" ordering is used

DenseMatrix

public DenseMatrix(RealMatrix matrix)
Constructs "by rows" ordered instance as a copy of a real matrix.
Parameters:
matrix - the real matrix to get a copy from

DenseMatrix

public DenseMatrix(RealMatrix matrix,
                   boolean byRows)
Constructs an instance as a copy of a real matrix with selection of "by rows" or "by columns" ordering.
Parameters:
matrix - the real matrix to get a copy from

DenseMatrix

public DenseMatrix(RealContainer entries,
                   int nRows,
                   int nColumns)
Constructs an instance based on a user's real data container and ordered "by rows".
Parameters:
entries - a container of matrix entries
nRows - a number of matrix rows
nColumns - a number of matrix columns

DenseMatrix

public DenseMatrix(RealContainer entries,
                   int nRows,
                   int nColumns,
                   boolean byRows)
Constructs an instance based on a user's real data container with selection of "by rows" or "by columns" ordering.
Parameters:
entries - a container of matrix entries
nRows - a number of matrix rows
nColumns - a number of matrix columns
byRows - equals true if "by rows" ordering is used

DenseMatrix

protected DenseMatrix(RealContainer entries,
                      int nRows,
                      int nColumns,
                      int startIndex,
                      int rowStep,
                      int columnStep)
Constructs an instance based on a submatrix of a user's real data container.
Parameters:
entries - a container of matrix entries
nRows - a number of matrix rows
nColumns - a number of matrix columns
startIndex - an index of the first matrix entry in the container
rowStep - an interrow distance
columnStep - an intercolumn distance
Method Detail

submatrix

public final DenseMatrix submatrix(int i,
                                   int j,
                                   int nRows,
                                   int nColumns)
Generates a submatrix that starts from (i,j)th position of the matrix.
Parameters:
i - the first row number
j - the first column number
nRows - a number of submatrix rows
nColumns - a number of submatrix columns

submatrix

public final DenseMatrix submatrix(int i,
                                   int j,
                                   int nRows,
                                   int nColumns,
                                   int rowInc,
                                   int columnInc)
Generates a submatrix that starts from (i,j)th position of the matrix and is stored in it with specified increments.
Parameters:
i - the first row number
j - the first column number
nRows - a number of submatrix rows
nColumns - a number of submatrix columns
rowInc - an increment between neighbour rows
columnInc - an increment between neighbour columns

transpose

public final DenseMatrix transpose()
Generates the transposed matrix based on the same container. Throws IllegalStateException if the matrix is nonalgebraic.

multiply

public void multiply(RealVector source,
                     RealVector target)
Multiplies the matrix by a column-vector. Throws IllegalStateException if the matrix is nonalgebraic.
Overrides:
multiply in class RealMatrix
Parameters:
source - a source vector to multiply
target - a target vector to write the result

multiplyT

public void multiplyT(RealVector source,
                      RealVector target)
Multiplies the transposed matrix by a column-vector. Throws IllegalStateException if the matrix is nonalgebraic.
Overrides:
multiplyT in class RealMatrix
Parameters:
source - a source vector to multiply
target - a target vector to write the result

multiplyAndAdd

public void multiplyAndAdd(double multiple,
                           RealVector source,
                           RealVector target)
Calculates y += a * Ax, eg multiplies the matrix by a source column-vector, then multiplies the result by a real number and adds the result to the target vector. Throws IllegalStateException if the matrix is nonalgebraic.
Parameters:
multiple - a multiple
source - a source vector to multiply
target - a target vector to add the result

multiplyAndAddT

public void multiplyAndAddT(double multiple,
                            RealVector source,
                            RealVector target)
Calculates y += a * ATx, eg multiplies the transposed matrix by a source column-vector, then multiplies the result by a real number and adds the result to the target vector. Throws IllegalStateException if the matrix is nonalgebraic.
Parameters:
multiple - a multiple
source - a source vector to multiply
target - a target vector to add the result

getRow

public final RealVector getRow(int i)
Returns the i-th row of the matrix (the vector is constructed upon the matrix container).

getColumn

public final RealVector getColumn(int j)
Returns the j-th column of the matrix (the vector is constructed upon the matrix container).

get

public double get(int i,
                  int j)
Gets a value of a matrix entry at the position (i,j).
Overrides:
get in class RealMatrix
Parameters:
i - a zero based row number
j - a zero based column number
Returns:
an entry value at the required position

set

public double set(int i,
                  int j,
                  double value)
Sets the value of a matrix entry at the position (i,j).
Overrides:
set in class RealMatrix
Parameters:
i - a zero based row number
j - a zero based column number
value - a value to set
Returns:
the set value

add

public double add(int i,
                  int j,
                  double value)
Adds the value of a matrix entry at the position (i,j).
Overrides:
add in class RealMatrix
Parameters:
i - a zero based row number
j - a zero based column number
value - a value to set
Returns:
the addition result

mul

public double mul(int i,
                  int j,
                  double value)
Multiplies a matrix entry at the position (i,j) by a value.
Overrides:
mul in class RealMatrix
Parameters:
i - a zero based row number
j - a zero based column number
value - a value to set
Returns:
the multiplication result

reuse

public final void reuse()
Allows to use the matrix in algebraic operations.

pos

protected final int pos(int i,
                        int j)
Returns the position of (i,j)th matrix entry in the container.

clone

public java.lang.Object clone()
Clones the matrix. If the cloned matrix is a submatrix of a dense metrix, the cloned matrix will contain the submatrix entries only.
Overrides:
clone in class RealMatrix

assign

public void assign(RealMatrix matrix)
Assignment: this = matrix (an arbitrary real matrix on the right).
Parameters:
matrix - the matrix to get copy from
Throws:
java.lang.IllegalStateException - is thrown if this matrix isn't algebraic
java.lang.IllegalArgumentException - is thrown if the dimensions of matrices are incompatible

assign

public void assign(DenseMatrix matrix)
Assignment: this = matrix (a dense matrix on the right).
Parameters:
matrix - the matrix to get copy from
Throws:
java.lang.IllegalStateException - is thrown if this matrix isn't algebraic
java.lang.IllegalArgumentException - is thrown if the dimensions of matrices are incompatible

norm

public double norm()
Calculates the euclidean matrix norm (the square root of the sum of squared entries).

normMax

public double normMax()
Calculates the matrix norm subordinated to the vector max-norm.

normSum

public double normSum()
Calculates the matrix norm subordinated to the vector sum-norm.