matrix
Class Matrix

java.lang.Object
  extended by matrix.Matrix

public class Matrix
extends java.lang.Object

An instance of this class represents a matrix of numbers as double values.


Constructor Summary
Matrix(int rows, int columns)
          Creates a matrix with the indicated numbers of rows and columns.
 
Method Summary
 Matrix add(Matrix other)
          Adds this matrix to another.
 boolean equals(java.lang.Object other)
          Tests for equality of this matrix with another.
 double get(int row, int column)
          Gets the element at the indicated row and column in this matrix.
 int getColumns()
          Getter for the number of columns in this matrix.
 int getRows()
          Getter for the number of rows in this matrix.
 Matrix multiply(Matrix other)
          Multiples this matrix with another.
 void set(int row, int column, double element)
          Sets the element at the indicated row and column in this matrix.
static Matrix toMatrix(java.lang.String string, int rows, int columns)
          Creates a matrix from a data string.
 java.lang.String toString()
          Creates a visual representation of this matrix as a string.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Matrix

public Matrix(int rows,
              int columns)
Creates a matrix with the indicated numbers of rows and columns.

Parameters:
rows - the number of rows in the matrix
columns - the number of columns in the matrix
Method Detail

getRows

public int getRows()
Getter for the number of rows in this matrix.

Returns:
the number of rows in this matrix

getColumns

public int getColumns()
Getter for the number of columns in this matrix.

Returns:
the number of columns in this matrix

get

public double get(int row,
                  int column)
Gets the element at the indicated row and column in this matrix.

Parameters:
row - the row position for the element. It must be the case that 0 ≤ row < getRows().
column - the column position for the element. It must be the case that 0 ≤ column < getColumns().
Returns:
the element at the indicated row and column
Throws:
MatrixException - if row or column is out of bounds

set

public void set(int row,
                int column,
                double element)
Sets the element at the indicated row and column in this matrix.

Parameters:
row - the row position for the element. It must be the case that 0 ≤ row < getRows().
column - the column position for the element. It must be the case that 0 ≤ column < getColumns().
element - the value to set in the matrix
Throws:
MatrixException - if row or column is out of bounds

equals

public boolean equals(java.lang.Object other)
Tests for equality of this matrix with another. Matrices are equal if they have the same dimensions and all elements are equal by ==. Note that since the parameter type for the other matrix is Object, its reference must be cast to Matrix. The parameter's type is Object so that this method overrides the equals method in the Object superclass.

Overrides:
equals in class java.lang.Object
Parameters:
other - the other matrix to be tested for equality with this matrix
Returns:
true if the other matrix is equal to this matrix, false otherwise

add

public Matrix add(Matrix other)
Adds this matrix to another. This matrix and the other matrix must have the same dimensions.

Parameters:
other - the other matrix to add
Returns:
a new matrix that is the sum of this matrix and other
Throws:
MatrixException - if this matrix and the other matrix do not have the same dimensions

multiply

public Matrix multiply(Matrix other)
Multiples this matrix with another. The number of columns in this matrix must match the number of rows in the other.

Parameters:
other - the other matrix to multiply
Returns:
a new matrix that is the product of this matrix and other
Throws:
MatrixException - if the number of columns in this matrix does not match the number of rows in the other

toMatrix

public static Matrix toMatrix(java.lang.String string,
                              int rows,
                              int columns)
                       throws java.io.IOException
Creates a matrix from a data string. Note that this method is written without knowing the representation details of the matrix. Only the constructor and public API method set are used.

Parameters:
string - A string containing blank-separated matrix data which must parse as double values, or a NumberFormatException will be thrown. Each row must be terminated by end-of-line character '\n'.
rows - The number of rows in the matrix. If the number of rows in the data string is not the same, a MatrixException will be thrown.
columns - The number of columns in the matrix. If the number of columns in the data string is not the same, a MatrixException will be thrown.
Returns:
the created matrix.
Throws:
java.io.IOException

toString

public java.lang.String toString()
Creates a visual representation of this matrix as a string. The opposite of toMatrix, this method can be used for debugging. Note that this method is written without knowing the representation details of the matrix. Only the public API methods getRows, getColumns, and get are used.

Overrides:
toString in class java.lang.Object
Returns:
the string representation of this matrix.