assignments.a05
Class Matrix

java.lang.Object
  extended by assignments.a05.Matrix

public class Matrix
extends Object

The Matrix class represents a 2D matrix.

See Also:
Mathworld

Field Summary
private  ArrayList<ArrayList<Double>> matrix
          The instance variable that stores the matrix data
 
Constructor Summary
Matrix(int rows, int columns)
          Creates a new matrix with the specified number of rows and columns and zeroes in every cell.
 
Method Summary
 double get(int i, int j)
          Gets the value of the element at row i and column j.
 int getColumns()
          Returns the number of columns in this matrix.
 int getRows()
          Returns the number of rows in this matrix.
 Matrix multiply(Matrix right)
          Multiplies this matrix by the matrix stored in 'right'.
 void set(int i, int j, double value)
          Sets the value of the element at row i and column j.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

matrix

private ArrayList<ArrayList<Double>> matrix
The instance variable that stores the matrix data

Constructor Detail

Matrix

public Matrix(int rows,
              int columns)
Creates a new matrix with the specified number of rows and columns and zeroes in every cell.

Parameters:
rows - The number of rows in this matrix
columns - The number of columns in this matrix
Throws:
IllegalArgumentException - if the number of rows or columns is not positive
Method Detail

getRows

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

Returns:
the number of rows in this matrix

getColumns

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

Returns:
the number of columns in this matrix

get

public double get(int i,
                  int j)
Gets the value of the element at row i and column j.

Parameters:
i - The row number
j - The column number
Returns:
The value at row i and column j

set

public void set(int i,
                int j,
                double value)
Sets the value of the element at row i and column j.

Parameters:
i - The row number
j - The column number
value - The value to set the element at row i and column j to

multiply

public Matrix multiply(Matrix right)
Multiplies this matrix by the matrix stored in 'right'. The number of columns of this matrix must equal the number of rows in 'right'. The resulting matrix will have the number of rows that this matrix has and the number of columns in 'right'. The ith row and jth column of the resulting matrix is calculated as the dot product of the ith row of this matrix with the jth column of 'right'. For example:
      [ r00 r01 ]   [ a00 a01 a02 ]   [ b00 b01 ]
      [ r10 r11 ] = [ a10 a11 a12 ] x [ b10 b11 ]
                                      [ b20 b21 ]
                                      
                  = [ (a00xb00 + a01xb10 + a02xb20) (a00xb01 + a01xb11 + a02xb21) ]
                    [ (a10xb00 + a11xb10 + a12xb20) (a10xb01 + a11xb11 + a12xb21) ]
 

Parameters:
right - the matrix to multiply this matrix by
Returns:
the result of the matrix multiplication