Class Matrix

java.lang.Object
  extended by Matrix

 class Matrix
extends Object

This class introduces matrices and enables basic operations with them. The elements are stored as a field of Fractions.

Since:
2014-04-01
Version:
1.0
Author:
Pavel Sikora

Constructor Summary
Matrix()
          Constructs a null matrix of dimension 0.
Matrix(Fraction[] M, int r, int c)
          Constructs a matrix from a field of Fractions.
Matrix(int n)
          Constructs a rectangular identity matrix of a given dimension.
Matrix(int r, int c)
          Constructs a zero matrix of given dimensions.
 
Method Summary
(package private)  void addColumn(int toWhich, int which, Fraction coeff)
          Adds a multiple of one column of this matrix to another column.
(package private)  void addRow(int toWhich, int which, Fraction coeff)
          Adds a multiple of one row of this matrix to another row.
(package private)  void copy(Matrix I)
          Copies another matrix onto this matrix.
(package private)  void deleteZeroRows()
          Deletes all zero rows in this matrix.
(package private)  Fraction det(String label, boolean verbose)
          Computes the determinant of this matrix.
(package private)  Fraction detGram(char label, boolean verbose)
          Computes Gram determinant of this matrix.
(package private)  Fraction get(int r, int c)
          The getter of this class - returns the value of a given element ar,c.
(package private)  int getC()
          Number of columns of this Matrix.
(package private)  Matrix getConstTerms()
          Crops the matrix of constant terms of equation system, ie.
(package private)  Matrix getEchelonForm()
          Converts this matrix into its echelon form.
(package private)  Matrix getEchelonForm(boolean verbose)
          Converts this matrix into its echelon form.
(package private)  Matrix getEchelonForm(char label, int maxRank, boolean verbose)
          Converts this matrix into a semi-echelon form.
(package private)  Matrix getEchelonForm(int maxRank)
          Converts this matrix into a semi-echelon form.
(package private)  int getR()
          Number of rows of this Matrix.
(package private)  Matrix getSystemCoeffMatrix()
          Crops the matrix of equation system coefficients, ie.
(package private)  SquareRootMatrix gramianMatrixForOutput(Matrix B)
          Computes the Gramian matrix.
(package private)  SquareRootMatrix gramianMatrixProduct(Matrix B)
          Computes product of Gramian matrix with its transposed self, ie.
(package private)  boolean isSolvable()
          Checks the solvability of this matrix.
(package private)  boolean isSolvable(boolean verbose)
          Checks the solvability of this matrix.
(package private)  void mergeFromBottom(Matrix N)
          Merges another matrix to this matrix from bottom.
(package private)  void mergeFromRight(Matrix N)
          Merges another matrix to this matrix from right.
(package private)  void multiplyColumn(int which, Fraction coeff)
          Multiplies a column of this matrix by a given coefficient.
(package private)  void multiplyRow(int which, Fraction coeff)
          Multiplies a row of this matrix by a given coefficient.
(package private)  Matrix orthogonalizeWithGramSchmidt(char label, boolean verbose)
          Orthogonalizes sequence of vectors using the Gram-Schmidt orthogonalization process.
(package private)  int rank()
          Rank of the matrix.
(package private)  int rank(boolean verbose)
          Rank of the matrix.
(package private)  int rank(char label, boolean verbose)
          Rank of the matrix.
(package private)  Vector[] rowsToVectors()
          Converts rows of this matrix into a field of vectors.
(package private)  void set(Fraction F, int r, int c)
          The setter of this class - sets given element ar,c to a given value.
(package private)  Matrix solve()
          Solves this matrix, or better converts it to a solved form.
(package private)  Matrix solve(char label, boolean verbose)
          Solves this matrix, or better converts it to a solved form.
(package private)  Matrix solve(char label, boolean verbose, boolean solveOnly)
          Solves this matrix, or better converts it to a solved form.
(package private)  Matrix solveOnly(char label, boolean verbose)
          Solves this matrix, or better converts it to a solved form.
(package private)  Matrix submatrix(int rowStart, int rowEnd, int colStart, int colEnd)
          Crops a submatrix from this matrix.
(package private)  void swapColumns(int c1, int c2)
          Swaps two columns of this matrix.
(package private)  void swapRows(int r1, int r2)
          Swaps two rows of this matrix.
(package private)  String toTeX()
          Prints this matrix formatted as TeX code.
(package private)  String toText()
          Prints this matrix formatted as text.
(package private)  Matrix transpose()
          Transposes this matrix.
 
Methods inherited from class
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Matrix

Matrix()
Constructs a null matrix of dimension 0.


Matrix

Matrix(Fraction[] M,
       int r,
       int c)
Constructs a matrix from a field of Fractions.

Parameters:
M - field of Fractions, containing the matrix's elements
r - number of matrix's rows
c - number of matrix's columns

Matrix

Matrix(int n)
Constructs a rectangular identity matrix of a given dimension.

Parameters:
n - the dimension of the matrix, ie. number of its rows and columns

Matrix

Matrix(int r,
       int c)
Constructs a zero matrix of given dimensions.

Parameters:
r - number of matrix's rows
c - number of matrix's columns
Method Detail

addColumn

void addColumn(int toWhich,
               int which,
               Fraction coeff)
Adds a multiple of one column of this matrix to another column.

Parameters:
toWhich - the column which the other column is added to
which - the column which is added to the other column
coeff - the coefficient by which the added column is multiplied before it is added

addRow

void addRow(int toWhich,
            int which,
            Fraction coeff)
Adds a multiple of one row of this matrix to another row.

Parameters:
toWhich - the row which the other row is added to
which - the row which is added to the other row
coeff - the coefficient by which the added row is multiplied before it is added

copy

void copy(Matrix I)
Copies another matrix onto this matrix. This is the correct way of assigning matrices, not by =.

Parameters:
I - the image matrix
Throws:
NullPointerException - Throws an exception if the image matrix has never been initiated.

deleteZeroRows

void deleteZeroRows()
Deletes all zero rows in this matrix. If this matrix consists only of zero rows, it will become a null matrix (having no rows, no columns and no data).


det

Fraction det(String label,
             boolean verbose)
Computes the determinant of this matrix.

Parameters:
label - label of the matrix
verbose - determines, if the method “talks” (creates any text and TeX output)
Returns:
determinant of this matrix
Throws:
UnsupportedOperationException - Throws an exception if the matrix is not rectangular.
UnsupportedOperationException - Throws an exception if the matrix is a null matrix.

detGram

Fraction detGram(char label,
                 boolean verbose)
Computes Gram determinant of this matrix.

Parameters:
verbose - determines, if the method “talks” (creates any text and TeX output)
Returns:
Gram determinant of this matrix

get

Fraction get(int r,
             int c)
The getter of this class - returns the value of a given element ar,c.

Parameters:
r - the row index of the desired element
c - the column index of the desired element
Returns:
the value (a Fraction) of the element on the rth row and the cth column
Throws:
IndexOutOfBoundsException - Throws an exception if the row index isn't of <1,number of rows>.
IndexOutOfBoundsException - Throws an exception if the column index isn't of <1,number of columns>.

getC

int getC()
Number of columns of this Matrix.

Returns:
a number of columns

getConstTerms

Matrix getConstTerms()
Crops the matrix of constant terms of equation system, ie. the last column.

Returns:
the matrix of constant terms of equation system, ie. the last column
Throws:
IllegalArgumentException - Throws an exception if the matrices has less than two columns, and therefore isn't a matrix of any equation system.

getEchelonForm

Matrix getEchelonForm()
Converts this matrix into its echelon form. If any rows will become zero rows during the process, they will be deleted. This method “talks” (creates text and TeX output).

Returns:
echelon form of this matrix (ie. every row begins with more leading zeroes than its predecessor)

getEchelonForm

Matrix getEchelonForm(boolean verbose)
Converts this matrix into its echelon form. If any rows will become zero rows during the process, they will be deleted.

Parameters:
verbose - determines, if the method “talks” (creates any text and TeX output)
Returns:
echelon form of this matrix (ie. every row begins with more leading zeroes than its predecessor)

getEchelonForm

Matrix getEchelonForm(char label,
                      int maxRank,
                      boolean verbose)
Converts this matrix into a semi-echelon form. That means that only a given number of columns from the left will be in echelon form, the rest needs not. If any rows will become zero rows during the process, they will be deleted.

Parameters:
label - label of the matrix
maxRank - the number of columns we want to have in echelon form
verbose - determines, if the method “talks” (creates any text and TeX output)
Returns:
a matrix with first maxRank columns in echelon form (ie. every row begins with more leading zeroes than its predecessor)

getEchelonForm

Matrix getEchelonForm(int maxRank)
Converts this matrix into a semi-echelon form. That means that only a given number of columns from the left will be in echelon form, the rest needs not. If any rows will become zero rows during the process, they will be deleted. This method “talks” (creates text and TeX output).

Parameters:
maxRank - the number of columns we want to have in echelon form
Returns:
echelon form of this matrix (ie. every row begins with more leading zeroes than its predecessor)

getR

int getR()
Number of rows of this Matrix.

Returns:
a number of rows

getSystemCoeffMatrix

Matrix getSystemCoeffMatrix()
Crops the matrix of equation system coefficients, ie. all columns but the last.

Returns:
the matrix of equation system coefficients, ie. all columns but the last
Throws:
IllegalArgumentException - Throws an exception if the matrices has less than two columns, and therefore isn't a matrix of any equation system.

gramianMatrixForOutput

SquareRootMatrix gramianMatrixForOutput(Matrix B)
Computes the Gramian matrix. This matrix is in fact never used in computations (see gramianMatrixProduct(Matrix B)) but is output as a text. The input format is the same as in gramianMatrixProduct(Matrix B).)

Parameters:
B - the other matrix
Returns:
the Gramian matrix of two sequences of vectors, ie. the matrix of their mutual scalar products
Throws:
UnsupportedOperationException - Throws an exception, if the matrices have different number of columns, ie. the dimensions of vectors are not equal.

gramianMatrixProduct

SquareRootMatrix gramianMatrixProduct(Matrix B)
Computes product of Gramian matrix with its transposed self, ie. A*AT. What is very important is that the inputs are not the matrices themselves! Instead, both matrices are constituted from the vectors that would constitute the Gramian matrix. The vectors will be in rows. Ie., if we have two subspaces P, Q with difference spaces, say, L(a,b), or L(u,v,w), respectively, the elements of the first matrix won't be scalar products au, av, ..., bw, but rather coefficients a1, a2, ..., an, b1, b2, ..., bn. Similarly with the other matrix. This is for practical reasons. Even more important is that the vectors must not be normalized (that will be taken care of in this method).

Parameters:
B - the other matrix
Returns:
matrix A*AT, where A is Gramian matrix of the given two sequences of vectors
Throws:
UnsupportedOperationException - Throws an exception, if the matrices have different number of columns, ie. the dimensions of vectors are not equal.
See Also:
gramianMatrixForOutput(Matrix B)

isSolvable

boolean isSolvable()
Checks the solvability of this matrix. This method doesn't “talk” (creates no text nor TeX output).

Returns:
true if the matrix is solvable, false otherwise

isSolvable

boolean isSolvable(boolean verbose)
Checks the solvability of this matrix.

Parameters:
verbose - determines, if the method “talks” (creates any text and TeX output)
Returns:
true if the matrix is solvable, false otherwise

mergeFromBottom

void mergeFromBottom(Matrix N)
Merges another matrix to this matrix from bottom.

Parameters:
N - the matrix to be merged from bottom
Throws:
UnsupportedOperationException - Throws an exception if the matrices don't have the same number of columns.

mergeFromRight

void mergeFromRight(Matrix N)
Merges another matrix to this matrix from right.

Parameters:
N - the matrix to be merged from right
Throws:
UnsupportedOperationException - Throws an exception if the matrices don't have the same number of rows.

multiplyColumn

void multiplyColumn(int which,
                    Fraction coeff)
Multiplies a column of this matrix by a given coefficient.

Parameters:
which - the column to be multiplied
coeff - the coefficient by which the column is multiplied

multiplyRow

void multiplyRow(int which,
                 Fraction coeff)
Multiplies a row of this matrix by a given coefficient.

Parameters:
which - the row to be multiplied
coeff - the coefficient by which the row is multiplied

orthogonalizeWithGramSchmidt

Matrix orthogonalizeWithGramSchmidt(char label,
                                    boolean verbose)
Orthogonalizes sequence of vectors using the Gram-Schmidt orthogonalization process. The process is carried upon a matrix of vectors (in rows).

Returns:
matrix of vectors (in rows) that constitute an orthogonal sequence

rank

int rank()
Rank of the matrix. This method “talks” (creates text and TeX output).

Returns:
the rank of this matrix, ie. the maximum number of linearly independent rows

rank

int rank(boolean verbose)
Rank of the matrix.

Parameters:
verbose - determines, if the method “talks” (creates any text and TeX output)
Returns:
the rank of this matrix, ie. the maximum number of linearly independent rows

rank

int rank(char label,
         boolean verbose)
Rank of the matrix.

Parameters:
verbose - determines, if the method “talks” (creates any text and TeX output)
Returns:
the rank of this matrix, ie. the maximum number of linearly independent rows

rowsToVectors

Vector[] rowsToVectors()
Converts rows of this matrix into a field of vectors.

Returns:
a field of vectors; each vector represents one row of this matrix (if this matrix is null, new Vector[0] will be returned)

set

void set(Fraction F,
         int r,
         int c)
The setter of this class - sets given element ar,c to a given value.

Parameters:
F - the desired value (a Fraction) of the element on the rth row and the cth column
r - the row index of the desired element
c - the column index of the desired element
Throws:
IndexOutOfBoundsException - Throws an exception if the row index isn't of <1,number of rows>.
IndexOutOfBoundsException - Throws an exception if the column index isn't of <1,number of columns>.

solve

Matrix solve()
Solves this matrix, or better converts it to a solved form. A solved form is a pseudo-identity matrix, completed in such manner that the corresponding matrix of equation system coefficients is rectangular (ie. matrix of dimensions n×n+1). A pseudo-identity matrix is a form of matrix in which the left rectangular submatrix n×n, where n is the rank of the whole matrix, is identity matrix. This method doesn't “talk” (creates no text nor TeX output).

Returns:
solved form of this matrix; the rightmost column contains a numerical solution; if any free variables have been assigned
Throws:
UnsupportedOperationException - Throws an exception if the matrix is not solvable.

solve

Matrix solve(char label,
             boolean verbose)
Solves this matrix, or better converts it to a solved form. A solved form is a pseudo-identity matrix, completed in such manner that the corresponding matrix of equation system coefficients is rectangular (ie. matrix of dimensions n×n+1). A pseudo-identity matrix is a form of matrix in which the left rectangular submatrix n×n, where n is the rank of the whole matrix, is identity matrix.

Parameters:
verbose - determines, if the method “talks” (creates any text and TeX output); if it does, it prints full output
Returns:
solved form of this matrix; the rightmost column contains a numerical solution; if any free variables have been assigned
Throws:
UnsupportedOperationException - Throws an exception if the matrix is not solvable.

solve

Matrix solve(char label,
             boolean verbose,
             boolean solveOnly)
Solves this matrix, or better converts it to a solved form. A solved form is a pseudo-identity matrix, completed in such manner that the corresponding matrix of equation system coefficients is rectangular (ie. matrix of dimensions n×n+1). A pseudo-identity matrix is a form of matrix in which the left rectangular submatrix n×n, where n is the rank of the whole matrix, is identity matrix.

Parameters:
verbose - determines, if the method “talks” (creates any text and TeX output)
solveOnly - determines, if the method prints only the solving stage without preparations or full solution
Returns:
solved form of this matrix; the rightmost column contains a numerical solution; if any free variables have been assigned
Throws:
UnsupportedOperationException - Throws an exception if the matrix is not solvable.

solveOnly

Matrix solveOnly(char label,
                 boolean verbose)
Solves this matrix, or better converts it to a solved form. A solved form is a pseudo-identity matrix, completed in such manner that the corresponding matrix of equation system coefficients is rectangular (ie. matrix of dimensions n×n+1). A pseudo-identity matrix is a form of matrix in which the left rectangular submatrix n×n, where n is the rank of the whole matrix, is identity matrix.

Parameters:
verbose - determines, if the method “talks” (creates any text and TeX output); if it does, it prints only the solving stage without preparations
Returns:
solved form of this matrix; the rightmost column contains a numerical solution; if any free variables have been assigned
Throws:
UnsupportedOperationException - Throws an exception if the matrix is not solvable.

submatrix

Matrix submatrix(int rowStart,
                 int rowEnd,
                 int colStart,
                 int colEnd)
Crops a submatrix from this matrix.

Parameters:
rowStart - the initial row of the submatrix (its row index in the original matrix)
rowEnd - the terminal row of the submatrix (its row index in the original matrix)
colStart - the initial column of the submatrix (its column index in the original matrix)
colEnd - the terminal column of the submatrix (its column index in the original matrix)
Throws:
UnsupportedOperationException - Throws an exception if the index of initial row (or column) is larger than the index of terminal row (or column).
UnsupportedOperationException - Throws an exception if the row indices aren't of <1,number of rows>.
UnsupportedOperationException - Throws an exception if the column indices aren't of <1,number of columns>.

swapColumns

void swapColumns(int c1,
                 int c2)
Swaps two columns of this matrix.

Parameters:
c1 - the first swapped column
c2 - the other swapped column

swapRows

void swapRows(int r1,
              int r2)
Swaps two rows of this matrix.

Parameters:
r1 - the first swapped row
r2 - the other swapped row

toTeX

String toTeX()
Prints this matrix formatted as TeX code.

Returns:
TeX code for this matrix

toText

String toText()
Prints this matrix formatted as text.

Returns:
plain text description of this matrix; the rows will begin with ( and end with ).

transpose

Matrix transpose()
Transposes this matrix.

Returns:
transpose of this matrix