Environment for
DeveLoping
KDD-Applications
Supported by Index-Structures

de.lmu.ifi.dbs.elki.math.linearalgebra
Class Matrix

java.lang.Object
  extended by de.lmu.ifi.dbs.elki.math.linearalgebra.Matrix
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
Vector

public class Matrix
extends Object
implements Cloneable, Serializable

The Matrix Class represents real-valued matrices.

For a Matrix M we have therefore M &isin;$real;<sup>m &times; n</sup>, where m and n are the number of rows and columns, respectively.

See Also:
Serialized Form

Field Summary
private  int columndimension
          Column dimension.
static double DELTA
          A small number to handle numbers near 0 as 0.
private  double[][] elements
          Array for internal storage of elements.
private  int rowdimension
          Row dimension.
private static long serialVersionUID
          Serial version
 
Constructor Summary
private Matrix()
          Basic-constructor for use in complex constructors only.
  Matrix(double[][] elements)
          Constructs a matrix from a 2-D array.
  Matrix(double[] values, int m)
          Construct a matrix from a one-dimensional packed array
  Matrix(int m, int n)
          Constructs an m-by-n matrix of zeros.
  Matrix(int m, int n, double s)
          Constructs an m-by-n constant matrix.
  Matrix(RationalNumber[][] q)
          Constructs a Matrix for a given array of arrays of RationalNumbers.
 
Method Summary
 boolean almostEquals(Object obj)
          Compare two matrices with a delta parameter to take numerical errors into account.
 boolean almostEquals(Object obj, double maxdelta)
          Compare two matrices with a delta parameter to take numerical errors into account.
 double angle(int colA, Matrix B, int colB)
          Returns the angle of the colA col of this and the colB col of B.
 Matrix appendColumns(Matrix columns)
          Returns a matrix which consists of this matrix and the specified columns.
 Matrix arrayLeftDivide(Matrix B)
          Element-by-element left division, C = A.
 Matrix arrayLeftDivideEquals(Matrix B)
          Element-by-element left division in place, A = A.
 Matrix arrayRightDivide(Matrix B)
          Element-by-element right division, C = A.
 Matrix arrayRightDivideEquals(Matrix B)
          Element-by-element right division in place, A = A.
 Matrix arrayTimes(Matrix B)
          Element-by-element multiplication, C = A.
 Matrix arrayTimesEquals(Matrix B)
          Element-by-element multiplication in place, A = A.
 Matrix cheatToAvoidSingularity(double constant)
          Adds a given value to the diagonal entries if the entry is smaller than the constant.
private  void checkMatrixDimensions(Matrix B)
          Check if size(A) == size(B) *
 CholeskyDecomposition chol()
          Cholesky Decomposition
 Object clone()
          Clone the Matrix object.
 Matrix completeBasis()
          Completes this d x c basis of a subspace of R^d to a d x d basis of R^d, i.e. appends c-d columns to this basis.
 Matrix completeToOrthonormalBasis()
          Completes this d x c basis of a subspace of R^d to a d x d basis of R^d, i.e. appends c-d columns to this basis.
 double cond()
          Matrix condition (2 norm)
static Matrix constructWithCopy(double[][] A)
          Construct a matrix from a copy of a 2-D array.
 Matrix copy()
          Make a deep copy of a matrix.
 double det()
          Matrix determinant
static Matrix diagonal(double[] diagonal)
          Returns a quadratic Matrix consisting of zeros and of the given values on the diagonal.
static Matrix diagonal(Vector diagonal)
          Returns a quadratic Matrix consisting of zeros and of the given values on the diagonal.
 String dimensionInfo()
          Returns the dimensionality of this matrix as a string.
 double distanceCov(Matrix B)
          distanceCov returns distance of two Matrices A and B, i.e. the root of the sum of the squared distances Aij-Bij.
 EigenvalueDecomposition eig()
          Eigenvalue Decomposition
 boolean equals(Object obj)
           
 double euclideanNorm(int col)
          Returns the euclidean norm of the col column.
private  RationalNumber[][] exactGaussElimination()
          Perform an exact Gauss-elimination of this Matrix using RationalNumbers to yield highest possible accuracy.
private static RationalNumber[][] exactGaussElimination(RationalNumber[][] gauss)
          Perform recursive Gauss-elimination on the given matrix of RationalNumbers.
 Matrix exactGaussJordanElimination()
          Returns a matrix derived by Gauss-Jordan-elimination using RationalNumbers for the transformations.
 double get(int i, int j)
          Get a single element.
 double[][] getArray()
          Access the internal two-dimensional array.
 double[][] getArrayCopy()
          Copy the internal two-dimensional array.
 Matrix getColumn(int j)
          Returns the jth column of this matrix.
 int getColumnDimensionality()
          Returns the dimensionality of the columns of this matrix.
 double[] getColumnPackedCopy()
          Make a one-dimensional column packed copy of the internal array.
 Vector getColumnVector(int j)
          Returns the jth column of this matrix as vector.
 double[] getDiagonal()
          getDiagonal returns array of diagonal-elements.
 Matrix getMatrix(int[] r, int[] c)
          Get a submatrix.
 Matrix getMatrix(int[] r, int j0, int j1)
          Get a submatrix.
 Matrix getMatrix(int i0, int i1, int[] c)
          Get a submatrix.
 Matrix getMatrix(int i0, int i1, int j0, int j1)
          Get a submatrix.
 Matrix getRow(int i)
          Returns the ith row of this matrix.
 int getRowDimensionality()
          Returns the dimensionality of the rows of this matrix.
 double[] getRowPackedCopy()
          Make a one-dimensional row packed copy of the internal array.
 Vector getRowVector(int i)
          Returns the ith row of this matrix as vector.
 int hashCode()
           
static Matrix identity(int m, int n)
          Generate identity matrix
 void increment(int i, int j, double s)
          Increments a single element.
 Matrix inverse()
          Matrix inverse or pseudoinverse
 boolean isSymmetric()
          Returns true, if this matrix is symmetric, false otherwise.
 boolean linearlyIndependent(Matrix columnMatrix)
          Returns true if the specified column matrix a is linearly independent to the columns of this matrix.
 LUDecomposition lu()
          LU Decomposition
 Matrix minus(Matrix B)
          C = A - B
 Matrix minusEquals(Matrix B)
          A = A - B
 double norm1()
          One norm
 double norm2()
          Two norm
 void normalizeColumns()
          Normalizes the columns of this matrix to length of 1.0.
 double normF()
          Frobenius norm
 double normInf()
          Infinity norm
 Matrix orthonormalize()
          Returns an orthonormalization of this matrix.
 Matrix plus(Matrix B)
          C = A + B
 Matrix plusEquals(Matrix B)
          A = A + B
 void print(int w, int d)
          Print the matrix to stdout.
 void print(NumberFormat format, int width)
          Print the matrix to stdout.
 void print(PrintWriter output, int w, int d)
          Print the matrix to the output stream.
 void print(PrintWriter output, NumberFormat format, int width)
          Print the matrix to the output stream.
 Matrix projection(Matrix v)
          Projects this row vector into the subspace formed by the specified matrix v.
 QRDecomposition qr()
          QR Decomposition
static Matrix random(int m, int n)
          Generate matrix with random elements
 int rank()
          Matrix rank
static Matrix read(BufferedReader input)
          Read a matrix from a stream.
 double scalarProduct(int colA, Matrix B, int colB)
          Returns the scalar product of the colA cols of this and the colB col of B.
 void scaleColumn(int j, double scale)
          Scales the specified column with the specified factor.
 void scaleColumns(double scale)
          Scales the columns of this matrix with the specified factor.
 void set(int i, int j, double s)
          Set a single element.
 void setColumn(int j, Matrix column)
          Sets the jth column of this matrix to the specified column.
 void setMatrix(int[] r, int[] c, Matrix X)
          Set a submatrix.
 void setMatrix(int[] r, int j0, int j1, Matrix X)
          Set a submatrix.
 void setMatrix(int i0, int i1, int[] c, Matrix X)
          Set a submatrix.
 void setMatrix(int i0, int i1, int j0, int j1, Matrix X)
          Set a submatrix.
 Matrix solve(Matrix B)
          Solve A*X = B
 Matrix solveTranspose(Matrix B)
          Solve X*A = B, which is also A'*X' = B'
 SingularValueDecomposition svd()
          Singular Value Decomposition
 void swapColumn(int i, int j)
          Swaps the specified columns of this matrix.
 void swapRow(int i, int j)
          Swaps the specified rows of this matrix.
 Matrix times(double s)
          Multiply a matrix by a scalar, C = s*A
 Matrix times(Matrix B)
          Linear algebraic matrix multiplication, A * B
 Matrix timesEquals(double s)
          Multiply a matrix by a scalar in place, A = s*A
 Matrix timesTranspose(Matrix B)
          Linear algebraic matrix multiplication, A * B^T
 String toString()
          toString returns String-representation of Matrix.
 String toString(int w, int d)
          Returns a string representation of this matrix.
 String toString(NumberFormat nf)
          returns String-representation of Matrix.
 String toString(String pre)
          Returns a string representation of this matrix.
 String toString(String pre, NumberFormat nf)
          Returns a string representation of this matrix.
 double trace()
          Matrix trace.
 Matrix transpose()
          Matrix transpose.
 Matrix transposeTimes(Matrix B)
          Linear algebraic matrix multiplication, AT * B
 Matrix uminus()
          Unary minus
static Matrix unitMatrix(int dim)
          Returns the unit matrix of the specified dimension.
static Matrix zeroMatrix(int dim)
          Returns the zero matrix of the specified dimension.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Serial version

See Also:
Constant Field Values

DELTA

public static final double DELTA
A small number to handle numbers near 0 as 0.

See Also:
Constant Field Values

elements

private double[][] elements
Array for internal storage of elements.


rowdimension

private int rowdimension
Row dimension.


columndimension

private int columndimension
Column dimension.

Constructor Detail

Matrix

private Matrix()
Basic-constructor for use in complex constructors only.


Matrix

public Matrix(int m,
              int n)
Constructs an m-by-n matrix of zeros.

Parameters:
m - number of rows
n - number of colums

Matrix

public Matrix(int m,
              int n,
              double s)
Constructs an m-by-n constant matrix.

Parameters:
m - number of rows
n - number of colums
s - A scalar value defining the constant value in the matrix

Matrix

public Matrix(double[][] elements)
Constructs a matrix from a 2-D array.

Parameters:
elements - an array of arrays of doubles defining the values of the matrix
Throws:
IllegalArgumentException - if not all rows conform in the same length

Matrix

public Matrix(RationalNumber[][] q)
Constructs a Matrix for a given array of arrays of RationalNumbers.

Parameters:
q - an array of arrays of RationalNumbers. q is not checked for consistency (i.e. whether all rows are of equal length)

Matrix

public Matrix(double[] values,
              int m)
Construct a matrix from a one-dimensional packed array

Parameters:
values - One-dimensional array of doubles, packed by columns (ala Fortran).
m - Number of rows.
Throws:
IllegalArgumentException - Array length must be a multiple of m.
Method Detail

constructWithCopy

public static Matrix constructWithCopy(double[][] A)
Construct a matrix from a copy of a 2-D array.

Parameters:
A - Two-dimensional array of doubles.
Returns:
new matrix
Throws:
IllegalArgumentException - All rows must have the same length

copy

public Matrix copy()
Make a deep copy of a matrix.

Returns:
a new matrix containing the same values as this matrix

clone

public Object clone()
Clone the Matrix object.

Overrides:
clone in class Object

getArray

public double[][] getArray()
Access the internal two-dimensional array.

Returns:
Pointer to the two-dimensional array of matrix elements.

getArrayCopy

public double[][] getArrayCopy()
Copy the internal two-dimensional array.

Returns:
Two-dimensional array copy of matrix elements.

getColumnPackedCopy

public double[] getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.

Returns:
Matrix elements packed in a one-dimensional array by columns.

getRowPackedCopy

public double[] getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.

Returns:
Matrix elements packed in a one-dimensional array by rows.

getRowDimensionality

public int getRowDimensionality()
Returns the dimensionality of the rows of this matrix.

Returns:
m, the number of rows.

getColumnDimensionality

public int getColumnDimensionality()
Returns the dimensionality of the columns of this matrix.

Returns:
n, the number of columns.

get

public double get(int i,
                  int j)
Get a single element.

Parameters:
i - Row index.
j - Column index.
Returns:
A(i,j)
Throws:
ArrayIndexOutOfBoundsException - on bounds erro

getMatrix

public Matrix getMatrix(int i0,
                        int i1,
                        int j0,
                        int j1)
Get a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index
Returns:
A(i0:i1,j0:j1)
Throws:
ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public Matrix getMatrix(int[] r,
                        int[] c)
Get a submatrix.

Parameters:
r - Array of row indices.
c - Array of column indices.
Returns:
A(r(:),c(:))
Throws:
ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public Matrix getMatrix(int i0,
                        int i1,
                        int[] c)
Get a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
c - Array of column indices.
Returns:
A(i0:i1,c(:))
Throws:
ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public Matrix getMatrix(int[] r,
                        int j0,
                        int j1)
Get a submatrix.

Parameters:
r - Array of row indices.
j0 - Initial column index
j1 - Final column index
Returns:
A(r(:),j0:j1)
Throws:
ArrayIndexOutOfBoundsException - Submatrix indices

set

public void set(int i,
                int j,
                double s)
Set a single element.

Parameters:
i - Row index.
j - Column index.
s - A(i,j).
Throws:
ArrayIndexOutOfBoundsException - on bounds error

increment

public void increment(int i,
                      int j,
                      double s)
Increments a single element.

Parameters:
i - the row index
j - the column index
s - the increment value: A(i,j) = A(i.j) + s.
Throws:
ArrayIndexOutOfBoundsException - on bounds error

setMatrix

public void setMatrix(int i0,
                      int i1,
                      int j0,
                      int j1,
                      Matrix X)
Set a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index
X - A(i0:i1,j0:j1)
Throws:
ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int[] r,
                      int[] c,
                      Matrix X)
Set a submatrix.

Parameters:
r - Array of row indices.
c - Array of column indices.
X - A(r(:),c(:))
Throws:
ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int[] r,
                      int j0,
                      int j1,
                      Matrix X)
Set a submatrix.

Parameters:
r - Array of row indices.
j0 - Initial column index
j1 - Final column index
X - A(r(:),j0:j1)
Throws:
ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int i0,
                      int i1,
                      int[] c,
                      Matrix X)
Set a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
c - Array of column indices.
X - A(i0:i1,c(:))
Throws:
ArrayIndexOutOfBoundsException - Submatrix indices

getColumn

public Matrix getColumn(int j)
Returns the jth column of this matrix.

Parameters:
j - the index of the column to be returned
Returns:
the jth column of this matrix

getColumnVector

public Vector getColumnVector(int j)
Returns the jth column of this matrix as vector.

Parameters:
j - the index of the column to be returned
Returns:
the jth column of this matrix

getRow

public Matrix getRow(int i)
Returns the ith row of this matrix.

Parameters:
i - the index of the row to be returned
Returns:
the ith row of this matrix

getRowVector

public Vector getRowVector(int i)
Returns the ith row of this matrix as vector.

Parameters:
i - the index of the row to be returned
Returns:
the ith row of this matrix

setColumn

public void setColumn(int j,
                      Matrix column)
Sets the jth column of this matrix to the specified column.

Parameters:
j - the index of the column to be set
column - the value of the column to be set

transpose

public Matrix transpose()
Matrix transpose.

Returns:
AT

norm1

public double norm1()
One norm

Returns:
maximum column sum.

norm2

public double norm2()
Two norm

Returns:
maximum singular value.

normInf

public double normInf()
Infinity norm

Returns:
maximum row sum.

normF

public double normF()
Frobenius norm

Returns:
sqrt of sum of squares of all elements.

uminus

public Matrix uminus()
Unary minus

Returns:
-A

plus

public Matrix plus(Matrix B)
C = A + B

Parameters:
B - another matrix
Returns:
A + B in a new Matrix

plusEquals

public Matrix plusEquals(Matrix B)
A = A + B

Parameters:
B - another matrix
Returns:
A + B in this Matrix

minus

public Matrix minus(Matrix B)
C = A - B

Parameters:
B - another matrix
Returns:
A - B in a new Matrix

minusEquals

public Matrix minusEquals(Matrix B)
A = A - B

Parameters:
B - another matrix
Returns:
A - B in this Matrix

arrayTimes

public Matrix arrayTimes(Matrix B)
Element-by-element multiplication, C = A.*B

Parameters:
B - another matrix
Returns:
A.*B

arrayTimesEquals

public Matrix arrayTimesEquals(Matrix B)
Element-by-element multiplication in place, A = A.*B

Parameters:
B - another matrix
Returns:
A.*B

arrayRightDivide

public Matrix arrayRightDivide(Matrix B)
Element-by-element right division, C = A./B

Parameters:
B - another matrix
Returns:
A./B

arrayRightDivideEquals

public Matrix arrayRightDivideEquals(Matrix B)
Element-by-element right division in place, A = A./B

Parameters:
B - another matrix
Returns:
A./B

arrayLeftDivide

public Matrix arrayLeftDivide(Matrix B)
Element-by-element left division, C = A.\B

Parameters:
B - another matrix
Returns:
A.\B

arrayLeftDivideEquals

public Matrix arrayLeftDivideEquals(Matrix B)
Element-by-element left division in place, A = A.\B

Parameters:
B - another matrix
Returns:
A.\B

times

public Matrix times(double s)
Multiply a matrix by a scalar, C = s*A

Parameters:
s - scalar
Returns:
s*A

timesEquals

public Matrix timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A

Parameters:
s - scalar
Returns:
replace A by s*A

times

public Matrix times(Matrix B)
Linear algebraic matrix multiplication, A * B

Parameters:
B - another matrix
Returns:
Matrix product, A * B
Throws:
IllegalArgumentException - Matrix inner dimensions must agree.

transposeTimes

public Matrix transposeTimes(Matrix B)
Linear algebraic matrix multiplication, AT * B

Parameters:
B - another matrix
Returns:
Matrix product, AT * B
Throws:
IllegalArgumentException - Matrix inner dimensions must agree.

timesTranspose

public Matrix timesTranspose(Matrix B)
Linear algebraic matrix multiplication, A * B^T

Parameters:
B - another matrix
Returns:
Matrix product, A * B^T
Throws:
IllegalArgumentException - Matrix inner dimensions must agree.

lu

public LUDecomposition lu()
LU Decomposition

Returns:
LUDecomposition
See Also:
LUDecomposition

qr

public QRDecomposition qr()
QR Decomposition

Returns:
QRDecomposition
See Also:
QRDecomposition

chol

public CholeskyDecomposition chol()
Cholesky Decomposition

Returns:
CholeskyDecomposition
See Also:
CholeskyDecomposition

svd

public SingularValueDecomposition svd()
Singular Value Decomposition

Returns:
SingularValueDecomposition
See Also:
SingularValueDecomposition

eig

public EigenvalueDecomposition eig()
Eigenvalue Decomposition

Returns:
EigenvalueDecomposition
See Also:
EigenvalueDecomposition

solve

public Matrix solve(Matrix B)
Solve A*X = B

Parameters:
B - right hand side
Returns:
solution if A is square, least squares solution otherwise

solveTranspose

public Matrix solveTranspose(Matrix B)
Solve X*A = B, which is also A'*X' = B'

Parameters:
B - right hand side
Returns:
solution if A is square, least squares solution otherwise.

inverse

public Matrix inverse()
Matrix inverse or pseudoinverse

Returns:
inverse(A) if A is square, pseudoinverse otherwise.

det

public double det()
Matrix determinant

Returns:
determinant

rank

public int rank()
Matrix rank

Returns:
effective numerical rank, obtained from SVD.

cond

public double cond()
Matrix condition (2 norm)

Returns:
ratio of largest to smallest singular value.

trace

public double trace()
Matrix trace.

Returns:
sum of the diagonal elements.

random

public static Matrix random(int m,
                            int n)
Generate matrix with random elements

Parameters:
m - Number of rows.
n - Number of colums.
Returns:
An m-by-n matrix with uniformly distributed random elements.

identity

public static Matrix identity(int m,
                              int n)
Generate identity matrix

Parameters:
m - Number of rows.
n - Number of colums.
Returns:
An m-by-n matrix with ones on the diagonal and zeros elsewhere.

print

public void print(int w,
                  int d)
Print the matrix to stdout. Line the elements up in columns with a Fortran-like 'Fw.d' style format.

Parameters:
w - Column width.
d - Number of digits after the decimal.

print

public void print(PrintWriter output,
                  int w,
                  int d)
Print the matrix to the output stream. Line the elements up in columns with a Fortran-like 'Fw.d' style format.

Parameters:
output - Output stream.
w - Column width.
d - Number of digits after the decimal.

print

public void print(NumberFormat format,
                  int width)
Print the matrix to stdout. Line the elements up in columns. Use the format object, and right justify within columns of width characters. Note that is the matrix is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.

Parameters:
format - A Formatting object for individual elements.
width - Field width for each column.
See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)

print

public void print(PrintWriter output,
                  NumberFormat format,
                  int width)
Print the matrix to the output stream. Line the elements up in columns. Use the format object, and right justify within columns of width characters. Note that is the matrix is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.

Parameters:
output - the output stream.
format - A formatting object to format the matrix elements
width - Column width.
See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)

read

public static Matrix read(BufferedReader input)
                   throws IOException
Read a matrix from a stream. The format is the same the print method, so printed matrices can be read back in (provided they were printed using US Locale). Elements are separated by whitespace, all the elements for each row appear on a single line, the last row is followed by a blank line.

Parameters:
input - the input stream.
Returns:
New matrix
Throws:
IOException - on input error

checkMatrixDimensions

private void checkMatrixDimensions(Matrix B)
Check if size(A) == size(B) *


toString

public String toString(int w,
                       int d)
Returns a string representation of this matrix.

Parameters:
w - column width
d - number of digits after the decimal
Returns:
a string representation of this matrix

toString

public String toString()
toString returns String-representation of Matrix.

Overrides:
toString in class Object

toString

public String toString(String pre)
Returns a string representation of this matrix. In each line the specified String pre<\code> is prefixed.

Parameters:
pre - the prefix of each line
Returns:
a string representation of this matrix

toString

public String toString(NumberFormat nf)
returns String-representation of Matrix.

Parameters:
nf - NumberFormat to specify output precision
Returns:
String representation of this Matrix in precision as specified by given NumberFormat

toString

public String toString(String pre,
                       NumberFormat nf)
Returns a string representation of this matrix. In each line the specified String pre<\code> is prefixed.

Parameters:
nf - number format for output accuracy
pre - the prefix of each line
Returns:
a string representation of this matrix

getDiagonal

public double[] getDiagonal()
getDiagonal returns array of diagonal-elements.

Returns:
double[] the values on the diagonal of the Matrix

scaleColumns

public void scaleColumns(double scale)
Scales the columns of this matrix with the specified factor.

Parameters:
scale - the factor to scale the column

normalizeColumns

public void normalizeColumns()
Normalizes the columns of this matrix to length of 1.0.


scaleColumn

public void scaleColumn(int j,
                        double scale)
Scales the specified column with the specified factor.

Parameters:
j - the index of the column to be scaled
scale - the factor to scale the column

distanceCov

public double distanceCov(Matrix B)
distanceCov returns distance of two Matrices A and B, i.e. the root of the sum of the squared distances Aij-Bij.

Parameters:
B - Matrix to compute distance from this (A)
Returns:
distance of Matrices

angle

public double angle(int colA,
                    Matrix B,
                    int colB)
Returns the angle of the colA col of this and the colB col of B.

Parameters:
colA - the column of A to compute angle for
B - second Matrix
colB - the column of B to compute angle for
Returns:
double angle of the first cols of this and B

scalarProduct

public double scalarProduct(int colA,
                            Matrix B,
                            int colB)
Returns the scalar product of the colA cols of this and the colB col of B.

Parameters:
colA - The column of A to compute scalar product for
B - second Matrix
colB - The column of B to compute scalar product for
Returns:
double The scalar product of the first cols of this and B

euclideanNorm

public double euclideanNorm(int col)
Returns the euclidean norm of the col column.

Parameters:
col - The column to compute euclidean norm of
Returns:
double

diagonal

public static Matrix diagonal(double[] diagonal)
Returns a quadratic Matrix consisting of zeros and of the given values on the diagonal.

Parameters:
diagonal - the values on the diagonal
Returns:
the resulting matrix

diagonal

public static Matrix diagonal(Vector diagonal)
Returns a quadratic Matrix consisting of zeros and of the given values on the diagonal.

Parameters:
diagonal - the values on the diagonal
Returns:
the resulting matrix

swapRow

public void swapRow(int i,
                    int j)
Swaps the specified rows of this matrix.

Parameters:
i - the first row
j - the second row

swapColumn

public void swapColumn(int i,
                       int j)
Swaps the specified columns of this matrix.

Parameters:
i - the first column
j - the second column

projection

public Matrix projection(Matrix v)
Projects this row vector into the subspace formed by the specified matrix v.

Parameters:
v - the subspace matrix
Returns:
the projection of p into the subspace formed by v
Throws:
IllegalArgumentException - if this matrix is no row vector, i.e. this matrix has more than one column or this matrix and v have different length of rows

unitMatrix

public static Matrix unitMatrix(int dim)
Returns the unit matrix of the specified dimension.

Parameters:
dim - the dimensionality of the unit matrix
Returns:
the unit matrix of the specified dimension

zeroMatrix

public static Matrix zeroMatrix(int dim)
Returns the zero matrix of the specified dimension.

Parameters:
dim - the dimensionality of the unit matrix
Returns:
the zero matrix of the specified dimension

linearlyIndependent

public boolean linearlyIndependent(Matrix columnMatrix)
Returns true if the specified column matrix a is linearly independent to the columns of this matrix. Linearly independence is given, if the matrix resulting from appending a to this matrix has full rank.

Parameters:
columnMatrix - the column matrix to be tested for linear independence
Returns:
true if the specified column matrix is linearly independent to the columns of this matrix

exactGaussJordanElimination

public Matrix exactGaussJordanElimination()
Returns a matrix derived by Gauss-Jordan-elimination using RationalNumbers for the transformations.

Returns:
a matrix derived by Gauss-Jordan-elimination using RationalNumbers for the transformations

exactGaussElimination

private RationalNumber[][] exactGaussElimination()
Perform an exact Gauss-elimination of this Matrix using RationalNumbers to yield highest possible accuracy.

Returns:
an array of arrays of RationalNumbers representing the Gauss-eliminated form of this Matrix

exactGaussElimination

private static RationalNumber[][] exactGaussElimination(RationalNumber[][] gauss)
Perform recursive Gauss-elimination on the given matrix of RationalNumbers.

Parameters:
gauss - an array of arrays of RationalNumber
Returns:
recursive derived Gauss-elimination-form of the given matrix of RationalNumbers

isSymmetric

public boolean isSymmetric()
Returns true, if this matrix is symmetric, false otherwise.

Returns:
true, if this matrix is symmetric, false otherwise

dimensionInfo

public String dimensionInfo()
Returns the dimensionality of this matrix as a string.

Returns:
the dimensionality of this matrix as a string

completeBasis

public Matrix completeBasis()
Completes this d x c basis of a subspace of R^d to a d x d basis of R^d, i.e. appends c-d columns to this basis.

Returns:
the appended columns

completeToOrthonormalBasis

public Matrix completeToOrthonormalBasis()
Completes this d x c basis of a subspace of R^d to a d x d basis of R^d, i.e. appends c-d columns to this basis.

Returns:
the appended columns

appendColumns

public Matrix appendColumns(Matrix columns)
Returns a matrix which consists of this matrix and the specified columns.

Parameters:
columns - the columns to be appended
Returns:
the new matrix with the appended columns

orthonormalize

public Matrix orthonormalize()
Returns an orthonormalization of this matrix.

Returns:
the orthonormalized matrix

cheatToAvoidSingularity

public Matrix cheatToAvoidSingularity(double constant)
Adds a given value to the diagonal entries if the entry is smaller than the constant.

Parameters:
constant - value to add to the diagonal entries
Returns:
a new Matrix differing from this Matrix by the given value added to the diagonal entries

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

almostEquals

public boolean almostEquals(Object obj,
                            double maxdelta)
Compare two matrices with a delta parameter to take numerical errors into account.

Parameters:
obj - other object to compare with
maxdelta - maximum delta allowed
Returns:
true if delta smaller than maximum

almostEquals

public boolean almostEquals(Object obj)
Compare two matrices with a delta parameter to take numerical errors into account.

Parameters:
obj - other object to compare with
Returns:
almost equals with delta DELTA

Release 0.3 (2010-03-31_1612)