|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.jutil.math.matrix.NMatrix | +--org.jutil.math.matrix.Matrix
A class of 2D matrices. This version doesn't try that hard to perform stable calculations. If you need that, use the non-existing nummath package. If you don't know what it means, use this version :) Matrix also doesn't report any overflow (standard Java behavior).
Field Summary | |
static java.lang.String |
CVS_REVISION
MvDMvDMvD : some operations don't have 2 sensible names for use as a mutator and inspector. |
Constructor Summary | |
Matrix(double[] elements)
public behavior pre elements != null; pre elements.length > 0; post getNbRows() == elements.length; post isSquare(); post isDiagonal(); post (\forall int i; i>=0 && i Initialize a new diagonal Matrix with the given array of diagonal element. |
|
Matrix(double[][] elements)
public behavior pre elements != null; pre elements.length > 0; pre (\exist int nbColumns; nbColumns > 0; (\forall int i; i>=0 && i elements[i].length == nbColumns)); post getNbRows() == elements.length; post getNbColumns() == elements[0].length; post (\forall int i; i>=0 && i Initialize a new Matrix from the given 2D array of doubles. |
|
Matrix(int rows,
int columns)
public invariant getNbDimensions() == 2; public behavior post getNbColumns() == columns; post getNbRows() == rows; post (\forall int i; i>0 && i < getNbRows(); (\forall int j; j > 0 && j < getNbColumns(); elementAt(i,j) == 0)); Initialize a new Matrix with the given number of rows and columns |
Method Summary | |
void |
add(Matrix other)
public behavior pre other != null; pre sameDimensions(other); post (\forall int i; i>=1 && i <= getNbRows(); (\forall int j; j>=1 && j <= getNbColumns(); elementAt(i,j) == \old(elementAt(i,j)) + other.elementAt(i,j))); Add the given matrix to this matrix. |
java.lang.Object |
clone()
public behavior post equals(\result); post \result instanceof Matrix; Return a clone |
void |
divide(double factor)
public behavior post (\forall int i; i>=1 && i<= getNbRows(); (\forall int j; j>=1 && j<= getNbColumns(); elementAt(i,j) == \old(elementAt(i,j)) / factor)); Divide this matrix by a given factor. |
double |
elementAt(int[] index)
pre validIndex(index); Return the element at the given index. |
double |
elementAt(int row,
int column)
public behavior pre validIndex(row, column); post elementAt({row, column}); Return the element at the given row and column. |
boolean |
equals(java.lang.Object other)
public behavior post !(other instanceof Matrix) ==> false; post !(sameDimensions((Matrix)other)) ==> false; post (other instanceof Matrix) && (sameDimensions((Matrix)other)) ==> \result == (\forall int i; i>=1 i<=getNbRows(); (\forall int j; j>=1 j<=getNbColumns(); ((Matrix)\result).elementAt(i,j) == elementAt(i,j); post \result != this; Check whether the given object is equal to this matrix. |
Column |
getColumn(int index)
public behavior pre index > 0 && index <= getNbColumns(); post \result != null; post \result.size() == getNbRows(); post (\forall int i; i>0 && i < getNbRows(); \result.elementAt(i) == elementAt(i,index)); Return the column with the given index. |
int[] |
getDimensions()
post \result.length == getNbDimensions(); Return the dimensions of this matrix |
int |
getNbColumns()
public behavior post \result == getDimensions()[1]; Return the number of columns of this matrix. |
int |
getNbDimensions()
public invariant getDimensions().length == getNbDimensions(); public invariant (\forall int i; i>=0 && i Return the number of dimensions of this matrix. |
int |
getNbRows()
public behavior post \result == getDimensions()[0]; Return the number of rows of this matrix. |
Row |
getRow(int index)
public behavior pre index > 0 && index <= getNbRows(); post \result != null; post \result.size() == getNbColumns(); post (\forall int i; i>0 && i < getNbColumns(); \result.elementAt(i) == elementAt(i,index)); Return the row with the given index. |
boolean |
isDiagonal()
public behavior post \result == (\forall int i; i>=1 && i<=getNbRows(); (\forall int j; j>=1 && j<=getNbColumns(); (j != i) => elementAt(i,j) == 0)); Check whether or not this matrix is a diagonal matrix. |
boolean |
isLowerTriangular()
public behavior post \result == (\forall int i; i>=1 && i<=getNbRows(); (\forall int j; j>=1 && j<=getNbColumns(); (i > j) => elementAt(i,j) == 0)); Check whether or not this matrix is lower triangular |
boolean |
isPermutationMatrix()
post \result == isSquare() && // 1 non-zero element in each row (\forall int i; i>=1 && i<=getNbRows(); (\num_of int j; j>=1 && j<=getNbColumns() && elementAt(i,j) != 0;1) == 1) && // 1 non-zero element in each column (\forall int i; i>=1 && i<=getNbColumns(); (\num_of int j; j>=1 && j<=getNbRows() && elementAt(j,i) != 0;1) == 1) && // each element is 0 or 1 (\forall int i; i>=1 && i<=getNbRows(); (\forall int j; j>=1 && j<=getNbColumns(); elementAt(i,j) == 0 || elementAt(i,j) == 1)); Check whether or not this matrix is a permutation matrix. |
boolean |
isSquare()
public behavior post getNbColumns() == getNbRows(); Check whether or not this matrix is square. |
boolean |
isSymmetric()
public behavior post \result == isSquare() && (\forall int i; i>=1 && i<=getNbRows(); (\forall int j; j>=1 && j<=getNbColumns(); elementAt(i,j) == elementAt(j,i))); Check whether or not this matrix is symmetric. |
boolean |
isUpperTriangular()
public behavior post \result == (\forall int i; i>=1 && i<=getNbRows(); (\forall int j; j>=1 && j<=getNbColumns(); (i < j) => elementAt(i,j) == 0)); Check whether or not this matrix is upper triangular |
void |
leftGivens(double c,
double s,
int i,
int k)
public behavior pre (1 <= i) && (i < k) && (k <= getNbRows()); post getColumn(i).equals(\old(getColumn(i).times(c).minus(getColumn(k).times(s)))); post getColumn(k).equals(\old(getColumn(i).times(s).plus(getColumn(k).times(c)))); |
Matrix |
minus(Matrix other)
public behavior pre other != null; pre sameDimensions(other); post \result != null; post \result.sameDimensions(this); post (\forall int i; i>=1 && i <= getNbRows(); (\forall int j; j>=1 && j <= getNbColumns(); \result.elementAt(i,j) == elementAt(i,j) - other.elementAt(i,j))); Return a new matrix that equals this matrix minus the given matrix. |
void |
multiply(double factor)
public behavior post (\forall int i; i>=1 && i<= getNbRows(); (\forall int j; j>=1 && j<= getNbColumns(); elementAt(i,j) == \old(elementAt(i,j)) * factor)); Multiply this matrix by a given factor. |
Matrix |
plus(Matrix other)
public behavior pre other != null; pre sameDimensions(other); post \result != null; post \result.sameDimensions(this); post (\forall int i; i>=1 && i <= getNbRows(); (\forall int j; j>=1 && j <= getNbColumns(); \result.elementAt(i,j) == elementAt(i,j) + other.elementAt(i,j))); Return a new matrix that is the sum of this matrix and the given matrix |
Matrix |
returnTranspose()
public behavior post \result.sameDimensions(this); post (\forall int i; i>0 && i<= getNbRows(); (\forall int j; j>0 && j Return the transpose of this matrix. |
void |
rightGivens(double c,
double s,
int i,
int k)
public behavior pre (1 <= i) && (i < k) && (k <= getNbColumns()); post getColumn(i).equals(\old(getColumn(i).times(c).minus(getColumn(k).times(s)))); post getColumn(k).equals(\old(getColumn(i).times(s).plus(getColumn(k).times(c)))); |
boolean |
sameDimensions(Matrix other)
public behavior pre other != null post getNbColumns() == other.getNbColumns(); post getNbRows() == other.getNbRows(); Check wether the given matrix has the same dimensions as this one. |
void |
setColumn(int i,
Column column)
public behavior pre i>=1 && i<=getNbColumns(); pre column != null; pre column.size() == getNbRows(); post getColumn(i).equals(column); Set the i-th column of this matrix |
void |
setElementAt(int[] index,
double value)
pre validIndex(index); post elementAt(index) == value; Set the element at the given index to the given value. |
void |
setElementAt(int row,
int column,
double value)
public behavior pre validIndex(row, column); post elementAt(row, column) == value; Set the element at the given row and column to the given value. |
void |
setRow(int i,
Row row)
public behavior pre i>=1 && i<=getNbRows(); pre row != null; pre row.size() == getNbColumns(); post getRow(i).equals(row); Set the i-th row of this matrix |
void |
setSubMatrix(int row,
int column,
Matrix matrix)
public behavior pre matrix != null; pre validIndex(row, column); pre validIndex(row + matrix.getNbRows() - 1, column + matrix.getNbColumns() - 1); post subMatrix(row, column, row + matrix.getNbRows() - 1, column + matrix.getNbColumns() - 1).equals(matrix); Replace a submatrix of this matrix with the given matrix at the given coordinates. |
Matrix |
subMatrix(int x1,
int y1,
int x2,
int y2)
public behavior pre validIndex(x1, y1); pre validIndex(x2, y2); pre x2 >= x1; pre y2 >= y1; post \fresh(\result); post \result.getNbRows() == x2 - x1 + 1; post \result.getNbColumns() == y2 - y1 + 1; post (\forall int i; i>=x1 && i <=x2; (\forall int j; j>=y1 && j <=y2; \result.elementAt(i - x1 + 1, j - y1 + 1) == elementAt(i, j))); Return the submatrix starting from (x1,y1) to (x2,y2). |
void |
subtract(Matrix other)
public behavior pre other != null; pre sameDimensions(other); post (\forall int i; i>=1 && i <= getNbRows(); (\forall int j; j>=1 && j <= getNbColumns(); elementAt(i,j) == \old(elementAt(i,j)) - other.elementAt(i,j))); Subtract the given matrix from this matrix. |
Matrix |
times(double factor)
public behavior post \result != null; post \result.sameDimensions(this); post (\forall int i; i>=1 && i<= getNbRows(); (\forall int j; j>=1 && j<= getNbColumns(); \result.elementAt(i,j) == elementAt(i,j) * factor)); Return a matrix that equals this matrix times a given factor. |
Matrix |
times(Matrix other)
public behavior pre other != null; pre other.getNbRows() == getNbColumns(); pre other.getNbColumns() == getNbRows(); post \result != null; post (\forall int i; i>=1 && i<= getNbRows(); (\forall int j; j>=1 && j<= other.getNbColumns(); \result.elementAt(i,j) == (\sum int k; k>=1 && k<= getNbColumns(); elementAt(i,k) * other.elementAt(k,j)))); post \result.getNbRows() == getNbRows(); post \result.getNbColumns() == other.getNbColumns(); post \fresh(result); // MvDMvDMvD We should put this everywhere we return a matrix // since matrices are mutable. |
java.lang.String |
toString()
see superclass |
void |
transpose()
public behavior post getNbColumns() == \old(getNbRows()); post getNbRows() == \old(getNbColumns()); post (\forall int i; i>=1 && i<=getNbRows(); (\forall int j; j>=1 && j Transpose this matrix. |
static Matrix |
unity(int size)
public behavior pre size > 0; post \fresh(\result); post \result.getNbRows() == size; post \result.getNbColumns() == size; post (\forall int i; i>=1 && i<=size; (\forall int j; j>=1 && j<=size; ((i==j) => \result.elementAt(i,j)==1) && ((i!=j) => \result.elementAt(i,j)==0))); Return a new unity matrix of the given size |
boolean |
validIndex(int row,
int column)
public behavior post \result == (row > 0) && (row <= getNbRows()) && (column > 0) && (column <= getNbColumns()); Check whether or not the given row and column point to a valid position for this Matrix. |
Methods inherited from class org.jutil.math.matrix.NMatrix |
validIndex |
Methods inherited from class java.lang.Object |
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final java.lang.String CVS_REVISION
Constructor Detail |
public Matrix(int rows, int columns)
columns
- The number of columns for the new Matrix.rows
- The number of rows for the new Matrix.public Matrix(double[][] elements)
elements
- A 2D array containing the elements of the new Matrix.
The first dimension is for the rows, the second for the columns.public Matrix(double[] elements)
elements
- An array containing the diagonal elements of the new Matrix.Method Detail |
public static Matrix unity(int size)
size
- The number of rows/columns of the matrixpublic int getNbDimensions()
NMatrix
getNbDimensions
in class NMatrix
superclass
public int[] getDimensions()
NMatrix
getDimensions
in class NMatrix
superclass
public int getNbRows()
public int getNbColumns()
public double elementAt(int[] index)
NMatrix
elementAt
in class NMatrix
superclass
public double elementAt(int row, int column)
row
- The row of the element to be retrieved.column
- The column of the element to be retrieved.public void setElementAt(int row, int column, double value)
row
- The row of the element to be retrieved.column
- The column of the element to be retrieved.value
- The new value for the element at the given row and column.public void setElementAt(int[] index, double value)
NMatrix
setElementAt
in class NMatrix
superclass
public boolean validIndex(int row, int column)
row
- The row of the position.column
- The row of the position.public Column getColumn(int index)
index
- The index of the requested column.public void setColumn(int i, Column column)
i
- The index of the column to be set.column
- The column which will be the new i-th column of this matrixpublic Row getRow(int index)
index
- The index of the requested row.public void setRow(int i, Row row)
i
- The index of the row to be set.column
- The row which will be the new i-th row of this matrixpublic void add(Matrix other)
other
- The matrix to be added.public Matrix plus(Matrix other)
other
- The matrix to be added.public void subtract(Matrix other)
other
- The matrix to be added.public Matrix minus(Matrix other)
other
- The matrix to be added.public Matrix times(Matrix other)
other
- The matrix to multiply this matrix by.public Matrix times(double factor)
factor
- The factor to multiply this matrix with.public void multiply(double factor)
factor
- The factor to multiply this matrix with.public void divide(double factor)
factor
- The factor to divide this matrix by.public void transpose()
public void leftGivens(double c, double s, int i, int k)
Perform a givens rotation on this matrix from the left side with the givens matrix defined by c, s, i and k as follows (m = getNbRows()).
1 i k m 1 1 . . . 0 . . . 0 . . . 0 . . . . . . . . . . . . . . . . . . . . . i 0 . . . c . . . s . . . 0 . . . . . . . . . . . . . . . . . . . . . k 0 . . .-s . . . c . . . 0 . . . . . . . . . . . . . . . . . . . . . m 0 . . . 0 . . . 0 . . . 1
This matrix (A
) will be transformed into G*A
.
c
- The value of cc
- The value of si
- The value of ik
- The value of kpublic void rightGivens(double c, double s, int i, int k)
Perform a givens rotation on this matrix from the right side with the givens matrix defined by c, s, i and k as follows (n = getNbColumns()).
1 i k n 1 1 . . . 0 . . . 0 . . . 0 . . . . . . . . . . . . . . . . . . . . . i 0 . . . c . . . s . . . 0 . . . . . . . . . . . . . . . . . . . . . k 0 . . .-s . . . c . . . 0 . . . . . . . . . . . . . . . . . . . . . n 0 . . . 0 . . . 0 . . . 1
This matrix (A
) will be transformed into A*G
.
c
- The value of cc
- The value of si
- The value of ik
- The value of kpublic Matrix returnTranspose()
public boolean sameDimensions(Matrix other)
other
- The other matrixpublic Matrix subMatrix(int x1, int y1, int x2, int y2)
x1
- The row from which to start.y1
- The column from which to start.x2
- The end row.y2
- The end column.public void setSubMatrix(int row, int column, Matrix matrix)
row
- The row of the top-left element of the submatrix that will be replaced.column
- The column of the top-left element of the submatrix that will be replaced.matrix
- The matrix to put into this matrixpublic boolean isUpperTriangular()
public boolean isLowerTriangular()
public boolean isDiagonal()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.Object clone()
clone
in class java.lang.Object
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public boolean isSquare()
public boolean isSymmetric()
public boolean isPermutationMatrix()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |