vec_math
Class Matrix

java.lang.Object
  extended by vec_math.Matrix
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
FileMatrix, QuadMatrix

public class Matrix
extends Object
implements Cloneable

Class to provide basic matrix functions, like multiplication (matrix or scalar), addition, transposing... If a matrix is constructed only with rows and columns specified, a Null matrix of the specified range is created. Construction with an additional (double) array fills both, row and columns. Calls to routines like setRows(double[][]) only validates the row-like representation of the matrix (colVal = false). Routines like evalRows() updates the row-like representation of the matrix from the column representation.


Nested Class Summary
static class Matrix.CloneTest
          Testing cloning.
 
Field Summary
private  VectorG[] col
           
protected  boolean colValid
          The flag indicating a valid column-representation of the matrix.
private  int mcol
          The number of columns in the matrix.
private  int nrow
          The number of rows in the matrix.
private  VectorG[] row
          The vectors defining the rows of the matrix.
protected  boolean rowValid
          The flag indicating a valid row-representation of the matrix.
 
Constructor Summary
protected Matrix()
          Constructs an empty matrix.
  Matrix(int n, int m)
          Constructs an empty matrix with the number of rows and columns specified.
  Matrix(Matrix mat)
          Constructs a new matrix and copies the row and col vectors into the new matrix.
 
Method Summary
 Object clone()
          Returns a clone of the actual matrix.
 int columns()
          Return the number of columns.
 boolean equals(Object what)
          Checks if two matrices are equal.
 boolean evalCol()
          Twin function to evalRow().
 boolean evalRow()
          Derives the row-like representation of the matrix out of a valid column-like.
protected  void exchangeCol(int m1, int m2, boolean rowset)
          Pendant to exchangeRow.
protected  void exchangeRow(int n1, int n2, boolean colset)
          Exchanges rows n1 with n2.
private static VectorG extract(VectorG[] vector, int n)
          Extracts a row/column from a column/row representation of a matrix.
 VectorG getColumn(int m)
          Returns the specified column as an nvector.
 double getElement(int rw, int cl)
          Returns the element in question.
protected  VectorG getRawColumn(int m)
          Always return the column, even if not valid.
protected  VectorG getRawRow(int n)
          Always return the specified row, even if invalid.
 VectorG getRow(int n)
          Returns the specified row as an nvector.
 Matrix mult(double scalar)
          Returns the scalar product of the actual matrix with scalar.
static Matrix mult(Matrix m, Matrix n)
          Returns the matrix product of this x that, provided that this.mcol equals that.nrow.
protected  void multOneCol(int m, double fac)
          Same as row operation.
protected  void multOneRow(int n, double fac)
          Multiplies row number n (0->nrow-1) with fac.
 int rows()
          Return the number of rows.
 void setCols(VectorG[] newcol)
          Same restrictions as in setRows.
 void setElement(int rw, int cl, double val)
          Sets the element a_row,col.
protected  void setOneCol(int m, VectorG newcol)
          Same restrictions as in setOneRow.
protected  boolean setOneRow(int n, VectorG newrow)
          Changes only one row, specified by it's number n.
 void setRows(VectorG[] newrow)
          Sets the matrix via its row-like representation.
 String toString()
          Returns a string representation of the matrix.
static Matrix transpose(Matrix m)
          Returns the transposed of the argument Matrix.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nrow

private int nrow
The number of rows in the matrix.


mcol

private int mcol
The number of columns in the matrix.


rowValid

protected boolean rowValid
The flag indicating a valid row-representation of the matrix.


row

private VectorG[] row
The vectors defining the rows of the matrix.


colValid

protected boolean colValid
The flag indicating a valid column-representation of the matrix.


col

private VectorG[] col
Constructor Detail

Matrix

protected Matrix()
Constructs an empty matrix.


Matrix

public Matrix(int n,
              int m)
Constructs an empty matrix with the number of rows and columns specified. The matrix is a valid zero-matrix.

Parameters:
n - The number of rows.
m - The number of columns.

Matrix

public Matrix(Matrix mat)
Constructs a new matrix and copies the row and col vectors into the new matrix. Note that the matrix elements are not cloned.

See Also:
clone()
Method Detail

clone

public Object clone()
Returns a clone of the actual matrix. The vectors constituing the matrix are also cloned.

Overrides:
clone in class Object

setElement

public void setElement(int rw,
                       int cl,
                       double val)
Sets the element a_row,col. Does not check for approbiate range!


getElement

public double getElement(int rw,
                         int cl)
Returns the element in question.


rows

public int rows()
Return the number of rows.


columns

public int columns()
Return the number of columns.


setRows

public void setRows(VectorG[] newrow)
Sets the matrix via its row-like representation. If the matrix is still invalid, i.e. its row and column representation is invalid, the matrix is changed to represent the new row-like representation handed over in the argument. In this case the number of rows is deduced from the dimension of the nvector array, and the number of columns is derived from the dimension of the first row. If, however, the matrix's dimension are already fixed the array size and the nvector dimension must coincide with the predefined NxM size to allow updating to occur.


getRow

public VectorG getRow(int n)
Returns the specified row as an nvector. If the matrix does not contain a valid row representation the row is reconstructed from the column representation without evaluating the rows. Thus, if the user intends to call this method multiple times he/she should consider evaluating the row representation prior to a call to this method with evalRow().


getRawRow

protected VectorG getRawRow(int n)
Always return the specified row, even if invalid.


setOneRow

protected boolean setOneRow(int n,
                            VectorG newrow)
Changes only one row, specified by it's number n. Numbering starts with 0. If n is <0 or >nrow, or row.length != mcol or rowValid = false, nothing is changed!


setCols

public void setCols(VectorG[] newcol)
Same restrictions as in setRows.


getColumn

public VectorG getColumn(int m)
Returns the specified column as an nvector. If the matrix does not contain a valid column representation the column is reconstructed from the row representation without evaluating the columns. Thus, if the user intends to call this method multiple times he/she should consider evaluating the column representation prior to a call to this method with evalCol().


getRawColumn

protected VectorG getRawColumn(int m)
Always return the column, even if not valid.


setOneCol

protected void setOneCol(int m,
                         VectorG newcol)
Same restrictions as in setOneRow.


multOneRow

protected void multOneRow(int n,
                          double fac)
Multiplies row number n (0->nrow-1) with fac.


multOneCol

protected void multOneCol(int m,
                          double fac)
Same as row operation.


exchangeRow

protected void exchangeRow(int n1,
                           int n2,
                           boolean colset)
Exchanges rows n1 with n2. If col and colValid are true, the columns array is updated, too. If col is false, colValid is unset.


exchangeCol

protected void exchangeCol(int m1,
                           int m2,
                           boolean rowset)
Pendant to exchangeRow.


extract

private static final VectorG extract(VectorG[] vector,
                                     int n)
Extracts a row/column from a column/row representation of a matrix. This is done by first querying the length of the vector array to determine the dimension of the return vector. Then, this vector is filled with the n-th coordinate of each matrix vector within the argument array.
No range checks or validity checks are applied within this method.

Parameters:
vector - A row or column representation of the matrix.
n - The index to extract.

evalRow

public boolean evalRow()
Derives the row-like representation of the matrix out of a valid column-like. If attempt fails, false is returned.


evalCol

public boolean evalCol()
Twin function to evalRow().


transpose

public static Matrix transpose(Matrix m)
Returns the transposed of the argument Matrix. The returned matrix is cloned.


mult

public Matrix mult(double scalar)
Returns the scalar product of the actual matrix with scalar. Note: The original matrix is destroyed!


mult

public static Matrix mult(Matrix m,
                          Matrix n)
Returns the matrix product of this x that, provided that this.mcol equals that.nrow. New matrix is of range (this.nrow x that.mcol) and has both of its representations set. If the resulting matrix is a quad matrix, it can readily be cast.


toString

public String toString()
Returns a string representation of the matrix. This is a multi-line string, where each line represents a matrix row.

Overrides:
toString in class Object

equals

public boolean equals(Object what)
Checks if two matrices are equal. They must have identical dimensions and identical elements. Two matrices are equal, even if one is a row and the other one a column matrix, as long as their elements are identical.

Overrides:
equals in class Object