edu.northwestern.at.utils.math.matrix
Class AbstractMatrix

java.lang.Object
  extended by edu.northwestern.at.utils.math.matrix.AbstractMatrix
All Implemented Interfaces:
Matrix, java.io.Serializable
Direct Known Subclasses:
DenseMatrix, SparseMatrix

public abstract class AbstractMatrix
extends java.lang.Object
implements Matrix

Abstract class which implements Matrix interface.

Provides concrete implementations of all the Matrix interface methods except two: get and set. All the Matrix interface methods can be written in terms of get anc set. To implement a new concrete matrix subclass all that is needed is to override the get and set methods with concrete versions.

See the DenseMatrix and SparseMatrix classes of two concrete subclasses derived by extending AbstractMatrix.

See Also:
Serialized Form

Field Summary
protected  int columns
          Number of columns in matrix.
protected  int rows
          Number of rows in matrix.
 
Constructor Summary
protected AbstractMatrix()
          Don't allow instantiation without size specification.
  AbstractMatrix(int rows, int columns)
          Create a matrix of the specified size.
 
Method Summary
 int columns()
          Get number of columns in the matrix.
 double[][] get()
          Get all matrix elements as a two-dimensional double array.
abstract  double get(int row, int column)
          Gets value of element at given row and column.
 Matrix getColumn(int column)
          Get entire column as a matrix.
 double[] getColumnData(int column)
          Get entire column as an array of doubles.
 Matrix getColumns(int[] columns)
          Extract columns.
 Matrix getColumns(int firstColumn, int lastColumn)
          Extract columns.
 Matrix getCopy()
          Get all elements as a new matrix.
 Matrix getRow(int row)
          Get entire row as a matrix.
 double[] getRowData(int row)
          Get entire row as an array of doubles .
 Matrix getRows(int[] rows)
          Extract rows.
 Matrix getRows(int firstRow, int lastRow)
          Extract rows.
 Matrix getSubMatrix(int[] rows, int[] columns)
          Extract a submatrix.
 Matrix getSubMatrix(int[] rows, int firstColumn, int lastColumn)
          Extract a submatrix.
 Matrix getSubMatrix(int firstRow, int lastRow, int[] columns)
          Extract a submatrix.
 Matrix getSubMatrix(int firstRow, int firstColumn, int lastRow, int lastColumn)
          Extract a submatrix.
 int rows()
          Get number of rows in the matrix.
 void set(double[][] values)
          Set all elements of a matrix from a double array.
abstract  void set(int row, int column, double value)
          Set an element at the given position to a new value.
 void set(Matrix matrix)
          Set all elements from another matrix.
 void setColumn(int column, Matrix columnMatrix)
          Set entire column of values from a column matrix.
 void setColumnData(int column, double[] columnData)
          Set entire column from an array of doubles .
 void setColumnData(int column, int[] columnData)
          Set entire column from an array of ints .
 void setRow(int row, Matrix rowMatrix)
          Set entire row of values from a row matrix.
 void setRowData(int row, double[] rowData)
          Set entire row from an array of doubles .
 java.lang.String toString()
          Return matrix contents as a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

rows

protected int rows
Number of rows in matrix.


columns

protected int columns
Number of columns in matrix.

Constructor Detail

AbstractMatrix

protected AbstractMatrix()
Don't allow instantiation without size specification.


AbstractMatrix

public AbstractMatrix(int rows,
                      int columns)
Create a matrix of the specified size.

Parameters:
rows - Number of rows.
columns - Number of columns.

A subclass constructor should call this constructor and then create the matrix storage for the specified number of rows and columns.

Method Detail

rows

public int rows()
Get number of rows in the matrix.

Specified by:
rows in interface Matrix
Returns:
Number of rows in the matrix.

columns

public int columns()
Get number of columns in the matrix.

Specified by:
columns in interface Matrix
Returns:
Number of columns in the matrix.

set

public abstract void set(int row,
                         int column,
                         double value)
Set an element at the given position to a new value.

Specified by:
set in interface Matrix
Parameters:
row - Row in which the element occurs.
column - Column in which the element occurs.
value - The new value to be set.

Must be overridden by a subclass.


set

public void set(Matrix matrix)
         throws MatrixMismatchedSizeException
Set all elements from another matrix.

Specified by:
set in interface Matrix
Parameters:
matrix - Matrix whose value to copy to this matrix.
Throws:
MatrixMismatchedSizeException - If the source matrix and this one don't have the same number of rows and columns.

getCopy

public Matrix getCopy()
Get all elements as a new matrix.

Specified by:
getCopy in interface Matrix
Returns:
Copy of all elements as another matrix.

Essentially creates a deep clone of the current matrix as another AbstractMatrix .


set

public void set(double[][] values)
Set all elements of a matrix from a double array.

Specified by:
set in interface Matrix
Parameters:
values - The double[][] from which values should be set.

If there are fewer values entries than the size of the matrix, the other elements are set to zero. If there are more values entries than the size of the matrix, the extra values are ignored.


get

public abstract double get(int row,
                           int column)
Gets value of element at given row and column.

Specified by:
get in interface Matrix
Parameters:
row - Row in which the element occurs.
column - Column in which the element occurs.
Returns:
The value at the given position.

Must be overridden by a subclass.


getRow

public Matrix getRow(int row)
Get entire row as a matrix.

Specified by:
getRow in interface Matrix
Parameters:
row - Row to retrieve.
Returns:
Matrix containing row.

getRowData

public double[] getRowData(int row)
Get entire row as an array of doubles .

Specified by:
getRowData in interface Matrix
Parameters:
row - Row to retrieve.
Returns:
Array of doubles containing row data.

setRow

public void setRow(int row,
                   Matrix rowMatrix)
Set entire row of values from a row matrix.

Specified by:
setRow in interface Matrix
Parameters:
row - Row to set.
rowMatrix - Matrix containing row data.

If the length of the first row in rowMatrix exceeds the number of columns in this matrix, the extra values are ignored. If rowMatrix's first row is shorter than the number of columns in this matrix, the remaining row elements are set to zero.


setRowData

public void setRowData(int row,
                       double[] rowData)
Set entire row from an array of doubles .

Specified by:
setRowData in interface Matrix
Parameters:
row - Row to set.
rowData - Array of doubles containing row data.

If the length of rowData exceeds the number of columns in the matrix, the extra values are ignored. If rowData is shorter than the number of columns in the matrix, the remaining row elements are set to zero.


getColumn

public Matrix getColumn(int column)
Get entire column as a matrix.

Specified by:
getColumn in interface Matrix
Parameters:
column - Column to retrieve.
Returns:
Matrix containing column.

getColumnData

public double[] getColumnData(int column)
Get entire column as an array of doubles.

Specified by:
getColumnData in interface Matrix
Parameters:
column - Column to retrieve.
Returns:
Array of doubles containing column data.

setColumn

public void setColumn(int column,
                      Matrix columnMatrix)
Set entire column of values from a column matrix.

Specified by:
setColumn in interface Matrix
Parameters:
column - Column to set.
columnMatrix - Matrix containing column data.

If the length of the first column in columnMatrix exceeds the number of rows in this matrix, the extra values are ignored. If columnMatrix's first column is shorter than the number of rows in this matrix, the remaining column elements are set to zero.


setColumnData

public void setColumnData(int column,
                          double[] columnData)
Set entire column from an array of doubles .

Specified by:
setColumnData in interface Matrix
Parameters:
column - Column to set.
columnData - Array of doubles containing column data.

If the length of columnData exceeds the number of rows in the matrix, the extra values are ignored. If columnData is shorter than the number of rows in this matrix, the remaining column elements are set to zero.


setColumnData

public void setColumnData(int column,
                          int[] columnData)
Set entire column from an array of ints .

Specified by:
setColumnData in interface Matrix
Parameters:
column - Column to set.
columnData - Array of ints containing column data.

If the length of columnData exceeds the number of rows in the matrix, the extra values are ignored. If columnData is shorter than the number of rows in this matrix, the remaining column elements are set to zero.


get

public double[][] get()
Get all matrix elements as a two-dimensional double array.

Specified by:
get in interface Matrix
Returns:
Copy of all elements as a two-dimensional double array.

getSubMatrix

public Matrix getSubMatrix(int firstRow,
                           int firstColumn,
                           int lastRow,
                           int lastColumn)
Extract a submatrix.

Specified by:
getSubMatrix in interface Matrix
Parameters:
firstRow - First row index.
firstColumn - First column index.
lastRow - Last row index.
lastColumn - Last column index.
Returns:
Matrix containing rows firstRow through lastRow and columns firstColumn through lastColumn .
Throws:
MatrixMismatchedSizeException - If requested matrix indices are bad.

getSubMatrix

public Matrix getSubMatrix(int[] rows,
                           int[] columns)
Extract a submatrix.

Specified by:
getSubMatrix in interface Matrix
Parameters:
rows - Array of row indices.
columns - Array of column indices.
Returns:
Matrix containing rows and columns defined by indices.
Throws:
MatrixMismatchedSizeException - If requested matrix indices are bad.

getSubMatrix

public Matrix getSubMatrix(int firstRow,
                           int lastRow,
                           int[] columns)
Extract a submatrix.

Specified by:
getSubMatrix in interface Matrix
Parameters:
firstRow - First row index.
lastRow - Last row index
columns - Array of column indices.
Returns:
Matrix defined by specified row and column indices.
Throws:
MismatchedSizeException - If requested matrix indices are bad.

getSubMatrix

public Matrix getSubMatrix(int[] rows,
                           int firstColumn,
                           int lastColumn)
Extract a submatrix.

Specified by:
getSubMatrix in interface Matrix
Parameters:
rows - Array of row indices.
firstColumn - First column index.
lastColumn - Last column index.
Returns:
Submatrix defined by row and column indices.
Throws:
MismatchedSizeException - If requested matrix indices are bad.

getColumns

public Matrix getColumns(int firstColumn,
                         int lastColumn)
Extract columns.

Specified by:
getColumns in interface Matrix
Parameters:
firstColumn - First column index.
lastColumn - Last column index.
Returns:
Matrix containing all rows for specified column range.
Throws:
MismatchedSizeException - If requested matrix indices are bad.

getColumns

public Matrix getColumns(int[] columns)
Extract columns.

Specified by:
getColumns in interface Matrix
Parameters:
columns - Indices of columns to extract.
Returns:
Matrix containing all rows for specified column indices.
Throws:
MismatchedSizeException - If requested matrix indices are bad.

getRows

public Matrix getRows(int firstRow,
                      int lastRow)
Extract rows.

Specified by:
getRows in interface Matrix
Parameters:
firstRow - First row index.
lastRow - Last row index.
Returns:
Matrix containing all columns for specified row range.
Throws:
MismatchedSizeException - If requested matrix indices are bad.

getRows

public Matrix getRows(int[] rows)
Extract rows.

Specified by:
getRows in interface Matrix
Parameters:
rows - Indices of rows to extract.
Returns:
Matrix containing all columns for specified row indices.
Throws:
MismatchedSizeException - If requested matrix indices are bad.

toString

public java.lang.String toString()
Return matrix contents as a string.

Specified by:
toString in interface Matrix
Overrides:
toString in class java.lang.Object
Returns:
The matrix contents as a string.