jumbo.euclid
Class IntMatrix

java.lang.Object
  |
  +--jumbo.euclid.Status
        |
        +--jumbo.euclid.IntMatrix
Direct Known Subclasses:
IntSquareMatrix

public class IntMatrix
extends Status

IntMatrix - rectangular integer matrix class

IntMatrix represents a rectangular m-x-n matrix. The basic matrix algebra for non-square matrices is represented here and this class is also a base for square matrices.

Read the signature of each member function carefully as some MODIFY the object and some CREATE A NEW ONE. Among the reasons for this is that subclassing (e.g to IntSquareMatrix) is easier with one of these forms in certain cases. Note that if you modify an object, then all references to it will refer to the changed object


Field Summary
protected  int cols
          number of columns
protected  int[][] flmat
          the matrix
protected  int rows
          number of rows
 
Constructor Summary
IntMatrix()
          Default matrix, with cols = rows = 0
IntMatrix(int[][] m)
          COPY from an existing matrix - check that it is rectangular
IntMatrix(int r, int c)
          A rows*cols matrix set to 0 (rows or cols < 0 defaults to 0)
IntMatrix(int r, int c, int f)
          initalises all elements in the array with a given int[]
IntMatrix(int rows, int cols, int[] array)
          Formed by feeding in an existing 1-D array to a rowsXcols matrix.
IntMatrix(IntMatrix m)
          copy constructor - COPIES the other matrix
IntMatrix(IntMatrix m, int lowrow, int hirow, int lowcol, int hicol)
          submatrix of another matrix; fails if lowrow > hirow, lowrow < 0, etc.
 
Method Summary
 void appendColumnData(IntArray f)
          append data to matrix columnwise
 void appendColumnData(IntMatrix m)
          append data to matrix columnwise
 void appendRowData(IntArray f)
          append data to matrix rowwise
 void appendRowData(IntMatrix m)
          append data to matrix rowwise
 void clearMatrix()
          clear matrix
 IntMatrix clone(IntMatrix m)
           
 void deleteColumn(int col)
          delete column from matrix and close up
 void deleteColumns(int low, int high)
          delete 2 or more adjacent columns (inclusive) from matrix and close up
 void deleteRow(int row)
          delete row from matrix and close up
 void deleteRows(int low, int high)
          delete 2 or more adjacent rows (inclusive) from matrix and close up; if (high > rows-1 high -> rows-1; or low < 0, low -> 0
 int elementAt(Int2 rowcol)
          extracts a given element from the matrix
 int elementAt(int row, int col)
          extracts a given element from the matrix
 IntMatrix elementsInRange(IntRange r)
          produce a mask of those elements which fall in a range (1) else (0)
 boolean equals(IntMatrix m)
          are two matrices equal in all elements?
 IntArray extractColumnData(int col)
          get column data from matrix
 IntArray extractRowData(int row)
          get row data from matrix
 IntMatrix extractSubMatrixData(int low_row, int high_row, int low_col, int high_col)
          extract a IntMatrix submatrix from a IntMatrix
 int getCols()
          get number of columns
 int[][] getMatrix()
           
 int[] getMatrixAsArray()
          get matrix as int[] (in C order: m(0,0), m(0,1) ...)
 int getRows()
          get number of rows
 IntMatrix getTranspose()
          transpose matrix - creates new Matrix
 Int2 indexOfLargestElement()
          get index of largest element
 int indexOfLargestElementInColumn(int jcol)
          get index of largest element in column
 int indexOfLargestElementInRow(int irow)
          get index of largest element in row (or -1 if default matrix)
 Int2 indexOfSmallestElement()
          get index of smallest element
 int indexOfSmallestElementInColumn(int jcol)
          get index of smallest elem in column
 int indexOfSmallestElementInRow(int irow)
          get index of smallest element in row
 void insertColumnData(int after_col, IntArray f)
          add data as column or column block into matrix and expand
 void insertColumnData(int after_col, IntMatrix m)
          add data as column or column block into matrix and expand
 void insertColumns(int after_col, int delta_cols)
          insert a hole into the matric and expand
 void insertRowData(int after_row, IntArray f)
          insert row of data into matrix and expand
 void insertRowData(int after_row, IntMatrix m)
          insert 2 or more adjacent rows of data into matrix and expand
 void insertRows(int after_row, int delta_rows)
          make space for new rows in matrix and expand
 boolean isSquare()
          is the matrix square?
 int largestElement()
          get largest element
 int largestElementInColumn(int jcol)
          get largest element in a column
 int largestElementInRow(int irow)
          get largest element in a row
static void main(java.lang.String[] args)
          tests IntMatrix routines = new IntMatrix
 IntArray multiply(IntArray f)
          matrix multiplication of a COLUMN vector
 IntMatrix multiply(IntMatrix m)
          matrix multiplication - multiplies conformable matrices to give NEW matrix result = 'this' * m; (order matters)
 void multiplyBy(int f)
          matrix multiplication by a scalar - MODIFIES matrix
 void negative()
          unary minus - negate all elements of matrix; MODIFIES matrix
 IntMatrix plus(IntMatrix m2)
          matrix addition - adds conformable matrices giving NEW matrix
 IntMatrix reorderColumnsBy(IntSet is)
          reorder the columns of a matrix.
 IntMatrix reorderRowsBy(IntSet is)
          reorder the rows of a matrix.
 void replaceColumnData(int starting_col, int[] f)
          and from int[]
 boolean replaceColumnData(int column, IntArray f)
          replace data in a single column - return false if impossible
 void replaceColumnData(int start_column, IntMatrix m)
          replace data in a block of columns
 void replaceRowData(int row, int[] f)
          and using a int[]
 void replaceRowData(int row, IntArray f)
          overwrite existing row of data
 void replaceRowData(int after_row, IntMatrix m)
          overwrite existing block of rows; if too big, copying is truncated
 void replaceSubMatrixData(int low_row, int low_col, IntMatrix m)
          replaces the data starting at (low_row, low_col) and extending by the dimensions for the matrix m
 void setAllElements(int f)
          initialise matrix to given int
 void setElementAt(int row, int col, int f)
          inserts a given element to the matrix - MODIFIES matrix
 void shallowCopy(IntMatrix m)
          shallowCopy
 int smallestElement()
          get smallest element
 int smallestElementInColumn(int jcol)
          get smallest element in a column
 int smallestElementInRow(int irow)
          get smallest element in a row
 IntMatrix subtract(IntMatrix m2)
          matrix subtraction - subtracts conformable matrices giving NEW matrix
 java.lang.String toString()
          output matrix - very crude...
 
Methods inherited from class jumbo.euclid.Status
NYI
 
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

cols

protected int cols
number of columns

flmat

protected int[][] flmat
the matrix
Constructor Detail

IntMatrix

public IntMatrix()
Default matrix, with cols = rows = 0

IntMatrix

public IntMatrix(int r,
                 int c)
A rows*cols matrix set to 0 (rows or cols < 0 defaults to 0)

IntMatrix

public IntMatrix(int rows,
                 int cols,
                 int[] array)
          throws BadArgumentException
Formed by feeding in an existing 1-D array to a rowsXcols matrix. THE COLUMN IS THE FASTEST MOVING INDEX, i.e. the matrix is filled as flmat(0,0), flmat(0,1) ... C-LIKE. COPIES the array
Throws:
BadArgumentException - size of array is not rows*cols

IntMatrix

public IntMatrix(int r,
                 int c,
                 int f)
initalises all elements in the array with a given int[]

IntMatrix

public IntMatrix(IntMatrix m,
                 int lowrow,
                 int hirow,
                 int lowcol,
                 int hicol)
          throws BadArgumentException
submatrix of another matrix; fails if lowrow > hirow, lowrow < 0, etc. COPIES the parts of m
Throws:
BadArgumentException - impossible value of hirow, hicol, lowrow, lowcol

IntMatrix

public IntMatrix(IntMatrix m)
copy constructor - COPIES the other matrix

IntMatrix

public IntMatrix(int[][] m)
          throws NonRectMatrixException
COPY from an existing matrix - check that it is rectangular
Throws:
NonRectMatrixException - m has rows of different lengths
Method Detail

shallowCopy

public void shallowCopy(IntMatrix m)
shallowCopy

clone

public IntMatrix clone(IntMatrix m)

getRows

public int getRows()
get number of rows

getCols

public int getCols()
get number of columns

getMatrix

public int[][] getMatrix()

getMatrixAsArray

public int[] getMatrixAsArray()
get matrix as int[] (in C order: m(0,0), m(0,1) ...)

equals

public boolean equals(IntMatrix m)
               throws UnequalMatricesException
are two matrices equal in all elements?
Throws:
UnequalMatricesException - m and this are different sizes

plus

public IntMatrix plus(IntMatrix m2)
               throws UnequalMatricesException
matrix addition - adds conformable matrices giving NEW matrix
Throws:
UnequalMatricesException - m and this are different sizes

subtract

public IntMatrix subtract(IntMatrix m2)
                   throws UnequalMatricesException
matrix subtraction - subtracts conformable matrices giving NEW matrix
Throws:
UnequalMatricesException - m and this are different sizes

negative

public void negative()
unary minus - negate all elements of matrix; MODIFIES matrix

multiply

public IntMatrix multiply(IntMatrix m)
                   throws UnequalMatricesException
matrix multiplication - multiplies conformable matrices to give NEW matrix result = 'this' * m; (order matters)
Throws:
UnequalMatricesException - m and this are different sizes

multiplyBy

public void multiplyBy(int f)
matrix multiplication by a scalar - MODIFIES matrix

multiply

public IntArray multiply(IntArray f)
                  throws UnequalMatricesException
matrix multiplication of a COLUMN vector
Throws:
UnequalMatricesException - f.size() differs from cols

elementAt

public int elementAt(int row,
                     int col)
extracts a given element from the matrix

elementAt

public int elementAt(Int2 rowcol)
extracts a given element from the matrix

setElementAt

public void setElementAt(int row,
                         int col,
                         int f)
inserts a given element to the matrix - MODIFIES matrix

largestElement

public int largestElement()
get largest element

indexOfLargestElement

public Int2 indexOfLargestElement()
get index of largest element

largestElementInColumn

public int largestElementInColumn(int jcol)
get largest element in a column

indexOfLargestElementInColumn

public int indexOfLargestElementInColumn(int jcol)
get index of largest element in column

largestElementInRow

public int largestElementInRow(int irow)
get largest element in a row

indexOfLargestElementInRow

public int indexOfLargestElementInRow(int irow)
get index of largest element in row (or -1 if default matrix)

smallestElement

public int smallestElement()
get smallest element

indexOfSmallestElement

public Int2 indexOfSmallestElement()
get index of smallest element

smallestElementInColumn

public int smallestElementInColumn(int jcol)
get smallest element in a column

indexOfSmallestElementInColumn

public int indexOfSmallestElementInColumn(int jcol)
get index of smallest elem in column

smallestElementInRow

public int smallestElementInRow(int irow)
get smallest element in a row

indexOfSmallestElementInRow

public int indexOfSmallestElementInRow(int irow)
get index of smallest element in row

extractColumnData

public IntArray extractColumnData(int col)
get column data from matrix

extractRowData

public IntArray extractRowData(int row)
get row data from matrix

clearMatrix

public void clearMatrix()
clear matrix

setAllElements

public void setAllElements(int f)
initialise matrix to given int

getTranspose

public IntMatrix getTranspose()
transpose matrix - creates new Matrix

isSquare

public boolean isSquare()
is the matrix square?

deleteColumn

public void deleteColumn(int col)
delete column from matrix and close up

deleteColumns

public void deleteColumns(int low,
                          int high)
delete 2 or more adjacent columns (inclusive) from matrix and close up

deleteRow

public void deleteRow(int row)
delete row from matrix and close up

deleteRows

public void deleteRows(int low,
                       int high)
delete 2 or more adjacent rows (inclusive) from matrix and close up; if (high > rows-1 high -> rows-1; or low < 0, low -> 0

replaceColumnData

public boolean replaceColumnData(int column,
                                 IntArray f)
replace data in a single column - return false if impossible

replaceColumnData

public void replaceColumnData(int starting_col,
                              int[] f)
and from int[]

replaceColumnData

public void replaceColumnData(int start_column,
                              IntMatrix m)
replace data in a block of columns

insertColumns

public void insertColumns(int after_col,
                          int delta_cols)
insert a hole into the matric and expand

insertColumnData

public void insertColumnData(int after_col,
                             IntArray f)
add data as column or column block into matrix and expand

insertColumnData

public void insertColumnData(int after_col,
                             IntMatrix m)
add data as column or column block into matrix and expand

insertRows

public void insertRows(int after_row,
                       int delta_rows)
make space for new rows in matrix and expand

replaceRowData

public void replaceRowData(int row,
                           IntArray f)
                    throws UnequalMatricesException
overwrite existing row of data
Throws:
UnequalMatricesException - f.size() and cols differ

replaceRowData

public void replaceRowData(int row,
                           int[] f)
                    throws UnequalMatricesException
and using a int[]
Throws:
UnequalMatricesException - f.length and cols differ

replaceRowData

public void replaceRowData(int after_row,
                           IntMatrix m)
                    throws UnequalMatricesException
overwrite existing block of rows; if too big, copying is truncated
Throws:
UnequalMatricesException - m.rows and this.rows differ

insertRowData

public void insertRowData(int after_row,
                          IntMatrix m)
                   throws UnequalMatricesException
insert 2 or more adjacent rows of data into matrix and expand
Throws:
UnequalMatricesException - m.cols and this.colsdiffer

insertRowData

public void insertRowData(int after_row,
                          IntArray f)
                   throws UnequalMatricesException
insert row of data into matrix and expand
Throws:
UnequalMatricesException - f.size() and this.cols differ

appendColumnData

public void appendColumnData(IntArray f)
                      throws UnequalMatricesException
append data to matrix columnwise
Throws:
UnequalMatricesException - f.size() and this.rows differ

appendColumnData

public void appendColumnData(IntMatrix m)
                      throws UnequalMatricesException
append data to matrix columnwise
Throws:
UnequalMatricesException - m.rows and this.rows differ

appendRowData

public void appendRowData(IntArray f)
                   throws UnequalMatricesException
append data to matrix rowwise
Throws:
UnequalMatricesException - m.cols and this.cols differ

appendRowData

public void appendRowData(IntMatrix m)
                   throws UnequalMatricesException
append data to matrix rowwise
Throws:
UnequalMatricesException - m.cols and this.cols differ

replaceSubMatrixData

public void replaceSubMatrixData(int low_row,
                                 int low_col,
                                 IntMatrix m)
replaces the data starting at (low_row, low_col) and extending by the dimensions for the matrix m

reorderColumnsBy

public IntMatrix reorderColumnsBy(IntSet is)
                           throws UnequalMatricesException,
                                  java.lang.ArrayIndexOutOfBoundsException
reorder the columns of a matrix.
Throws:
UnequalMatricesException - is.size() and this.cols differ
java.lang.ArrayIndexOutOfBoundsException - an element of is >= this.cols

reorderRowsBy

public IntMatrix reorderRowsBy(IntSet is)
                        throws UnequalMatricesException,
                               java.lang.ArrayIndexOutOfBoundsException
reorder the rows of a matrix. Deleting rows is allowed
Throws:
UnequalMatricesException - is.size() and this.rows differ
java.lang.ArrayIndexOutOfBoundsException - an element of is >= this.rows

extractSubMatrixData

public IntMatrix extractSubMatrixData(int low_row,
                                      int high_row,
                                      int low_col,
                                      int high_col)
                               throws BadArgumentException
extract a IntMatrix submatrix from a IntMatrix
Throws:
BadArgumentException - low/high_row/col are outside range of this

elementsInRange

public IntMatrix elementsInRange(IntRange r)
produce a mask of those elements which fall in a range (1) else (0)

toString

public java.lang.String toString()
output matrix - very crude...
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)
tests IntMatrix routines = new IntMatrix