|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object de.lmu.ifi.dbs.elki.math.linearalgebra.Matrix
public class Matrix
A two-dimensional matrix class, where the data is stored as two-dimensional array. Implementation note: this class contains various optimizations that theoretically the java hotspot compiler should optimize on its own. However, they do show up a hotspots in the profiler (in cpu=times mode), so it does make a difference at least when optimizing other parts of ELKI.
Field Summary | |
---|---|
(package private) int |
columndimension
Column dimension. |
static double |
DELTA
A small number to handle numbers near 0 as 0. |
protected double[][] |
elements
Array for internal storage of elements. |
private static long |
serialVersionUID
Serial version |
Constructor Summary | |
---|---|
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(Matrix mat)
Constructor, cloning an existing matrix. |
|
Matrix(RationalNumber[][] q)
Constructs a Matrix for a given array of arrays of RationalNumber s. |
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. |
Matrix |
appendColumns(Matrix columns)
Returns a matrix which consists of this matrix and the specified columns. |
Matrix |
cheatToAvoidSingularity(double constant)
Adds a given value to the diagonal entries if the entry is smaller than the constant. |
protected void |
checkMatrixDimensions(MatrixLike<?> B)
Check if size(A) == size(B) |
CholeskyDecomposition |
chol()
Cholesky Decomposition |
Matrix |
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)
|
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[][] |
getArrayCopy()
Copy the internal two-dimensional array. |
double[][] |
getArrayRef()
Access the internal two-dimensional array. |
Matrix |
getColumn(int j)
Returns the j th 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 j th 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 i th 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 i th row of this matrix as vector. |
int |
hashCode()
|
static Matrix |
identity(int m,
int n)
Generate identity matrix |
Matrix |
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 |
Matrix |
minusTimes(Matrix B,
double s)
C = A - s * B |
Matrix |
minusTimesEquals(Matrix B,
double s)
A = A - s * 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 |
Matrix |
plusTimes(Matrix B,
double s)
C = A + s * B |
Matrix |
plusTimesEquals(Matrix B,
double s)
A = A + s * B |
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 column of this and the colB column of B. |
double |
scalarProduct(int colA,
Vector B)
Returns the scalar product of the colA column of this and the colB column of B. |
Matrix |
set(int i,
int j,
double s)
Set a single element. |
void |
setColumn(int j,
Matrix column)
Sets the j th column of this matrix to the specified column. |
void |
setColumnVector(int j,
Vector column)
Sets the j th 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. |
void |
setRow(int j,
Matrix row)
Sets the j th row of this matrix to the specified vector. |
void |
setRowVector(int j,
Vector row)
Sets the j th row of this matrix to the specified vector. |
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 |
Matrix |
times(double s)
Multiply a matrix by a scalar, C = s*A |
Matrix |
times(Matrix B)
Linear algebraic matrix multiplication, A * B |
Vector |
times(Vector 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. |
double |
trace()
Matrix trace. |
Matrix |
transpose()
Matrix transpose. |
Matrix |
transposeTimes(Matrix B)
Linear algebraic matrix multiplication, AT * B |
Vector |
transposeTimes(Vector B)
Linear algebraic matrix multiplication, AT * B |
Matrix |
transposeTimesTranspose(Matrix B)
Linear algebraic matrix multiplication, A^T * B^T. |
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 |
---|
private static final long serialVersionUID
public static final double DELTA
protected final double[][] elements
final int columndimension
Constructor Detail |
---|
public Matrix(int m, int n)
m
- number of rowsn
- number of columnspublic Matrix(int m, int n, double s)
m
- number of rowsn
- number of columnss
- A scalar value defining the constant value in the matrixpublic Matrix(double[][] elements)
elements
- an array of arrays of doubles defining the values of the
matrix
IllegalArgumentException
- if not all rows conform in the same lengthpublic Matrix(RationalNumber[][] q)
RationalNumber
s.
q
- an array of arrays of RationalNumbers. q is not checked for
consistency (i.e. whether all rows are of equal length)public Matrix(double[] values, int m)
values
- One-dimensional array of doubles, packed by columns (ala
Fortran).m
- Number of rows.
IllegalArgumentException
- Array length must be a multiple of m.public Matrix(Matrix mat)
mat
- Matrix to cloneMethod Detail |
---|
public static final Matrix constructWithCopy(double[][] A)
A
- Two-dimensional array of doubles.
IllegalArgumentException
- All rows must have the same lengthpublic static final Matrix unitMatrix(int dim)
dim
- the dimensionality of the unit matrix
public static final Matrix zeroMatrix(int dim)
dim
- the dimensionality of the unit matrix
public static final Matrix random(int m, int n)
m
- Number of rows.n
- Number of columns.
public static final Matrix identity(int m, int n)
m
- Number of rows.n
- Number of columns.
public static final Matrix diagonal(double[] diagonal)
diagonal
- the values on the diagonal
public static final Matrix diagonal(Vector diagonal)
diagonal
- the values on the diagonal
public final Matrix copy()
copy
in interface MatrixLike<Matrix>
public Matrix clone()
clone
in interface MatrixLike<Matrix>
clone
in class Object
public final double[][] getArrayRef()
public final double[][] getArrayCopy()
public final int getRowDimensionality()
getRowDimensionality
in interface MatrixLike<Matrix>
public final int getColumnDimensionality()
getColumnDimensionality
in interface MatrixLike<Matrix>
public final double get(int i, int j)
get
in interface MatrixLike<Matrix>
i
- Row index.j
- Column index.
ArrayIndexOutOfBoundsException
- on bounds errorpublic final Matrix set(int i, int j, double s)
set
in interface MatrixLike<Matrix>
i
- Row index.j
- Column index.s
- A(i,j).
ArrayIndexOutOfBoundsException
- on bounds errorpublic final Matrix increment(int i, int j, double s)
increment
in interface MatrixLike<Matrix>
i
- the row indexj
- the column indexs
- the increment value: A(i,j) = A(i.j) + s.
ArrayIndexOutOfBoundsException
- on bounds errorpublic final double[] getRowPackedCopy()
public final double[] getColumnPackedCopy()
public final Matrix getMatrix(int i0, int i1, int j0, int j1)
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column index
ArrayIndexOutOfBoundsException
- Submatrix indicespublic final Matrix getMatrix(int[] r, int[] c)
r
- Array of row indices.c
- Array of column indices.
ArrayIndexOutOfBoundsException
- Submatrix indicespublic final Matrix getMatrix(int[] r, int j0, int j1)
r
- Array of row indices.j0
- Initial column indexj1
- Final column index
ArrayIndexOutOfBoundsException
- Submatrix indicespublic final Matrix getMatrix(int i0, int i1, int[] c)
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.
ArrayIndexOutOfBoundsException
- Submatrix indicespublic final void setMatrix(int i0, int i1, int j0, int j1, Matrix X)
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column indexX
- A(i0:i1,j0:j1)
ArrayIndexOutOfBoundsException
- Submatrix indicespublic final void setMatrix(int[] r, int[] c, Matrix X)
r
- Array of row indices.c
- Array of column indices.X
- A(r(:),c(:))
ArrayIndexOutOfBoundsException
- Submatrix indicespublic final void setMatrix(int[] r, int j0, int j1, Matrix X)
r
- Array of row indices.j0
- Initial column indexj1
- Final column indexX
- A(r(:),j0:j1)
ArrayIndexOutOfBoundsException
- Submatrix indicespublic final void setMatrix(int i0, int i1, int[] c, Matrix X)
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.X
- A(i0:i1,c(:))
ArrayIndexOutOfBoundsException
- Submatrix indicespublic final Matrix getRow(int i)
i
th row of this matrix.
i
- the index of the row to be returned
i
th row of this matrixpublic final Vector getRowVector(int i)
i
th row of this matrix as vector.
i
- the index of the row to be returned
i
th row of this matrixpublic final void setRow(int j, Matrix row)
j
th row of this matrix to the specified vector.
j
- the index of the row to be setrow
- the value of the row to be setpublic final void setRowVector(int j, Vector row)
j
th row of this matrix to the specified vector.
j
- the index of the column to be setrow
- the value of the column to be setpublic final Matrix getColumn(int j)
j
th column of this matrix.
j
- the index of the column to be returned
j
th column of this matrixpublic final Vector getColumnVector(int j)
j
th column of this matrix as vector.
getColumnVector
in interface MatrixLike<Matrix>
j
- the index of the column to be returned
j
th column of this matrixpublic final void setColumn(int j, Matrix column)
j
th column of this matrix to the specified column.
j
- the index of the column to be setcolumn
- the value of the column to be setpublic final void setColumnVector(int j, Vector column)
j
th column of this matrix to the specified column.
j
- the index of the column to be setcolumn
- the value of the column to be setpublic final Matrix transpose()
transpose
in interface MatrixLike<Matrix>
public final Matrix plus(Matrix B)
plus
in interface MatrixLike<Matrix>
B
- another matrix
public final Matrix plusTimes(Matrix B, double s)
plusTimes
in interface MatrixLike<Matrix>
B
- another matrixs
- scalar
public final Matrix plusEquals(Matrix B)
plusEquals
in interface MatrixLike<Matrix>
B
- another matrix
public final Matrix plusTimesEquals(Matrix B, double s)
plusTimesEquals
in interface MatrixLike<Matrix>
B
- another matrixs
- Scalar
public final Matrix minus(Matrix B)
minus
in interface MatrixLike<Matrix>
B
- another matrix
public final Matrix minusTimes(Matrix B, double s)
minusTimes
in interface MatrixLike<Matrix>
B
- another matrixs
- Scalar
public final Matrix minusEquals(Matrix B)
minusEquals
in interface MatrixLike<Matrix>
B
- another matrix
public final Matrix minusTimesEquals(Matrix B, double s)
minusTimesEquals
in interface MatrixLike<Matrix>
B
- another matrixs
- Scalar
public final Matrix times(double s)
times
in interface MatrixLike<Matrix>
s
- scalar
public final Matrix timesEquals(double s)
timesEquals
in interface MatrixLike<Matrix>
s
- scalar
public final Matrix times(Matrix B)
B
- another matrix
IllegalArgumentException
- Matrix inner dimensions must agree.public final Vector times(Vector B)
B
- a vector
IllegalArgumentException
- Matrix inner dimensions must agree.public final Vector transposeTimes(Vector B)
B
- another matrix
IllegalArgumentException
- Matrix inner dimensions must agree.public final Matrix transposeTimes(Matrix B)
B
- another matrix
IllegalArgumentException
- Matrix inner dimensions must agree.public final Matrix timesTranspose(Matrix B)
B
- another matrix
IllegalArgumentException
- Matrix inner dimensions must agree.public final Matrix transposeTimesTranspose(Matrix B)
B
- another matrix
IllegalArgumentException
- Matrix inner dimensions must agree.public double scalarProduct(int colA, Matrix B, int colB)
colA
- The column of A to compute scalar product forB
- second MatrixcolB
- The column of B to compute scalar product for
public double scalarProduct(int colA, Vector B)
colA
- The column of A to compute scalar product forB
- Vector
public final LUDecomposition lu()
LUDecomposition
public final QRDecomposition qr()
QRDecomposition
public final CholeskyDecomposition chol()
CholeskyDecomposition
public final SingularValueDecomposition svd()
SingularValueDecomposition
public final EigenvalueDecomposition eig()
EigenvalueDecomposition
public final Matrix solve(Matrix B)
B
- right hand side
public final Matrix solveTranspose(Matrix B)
B
- right hand side
public final Matrix inverse()
public final double det()
public final int rank()
public final double cond()
public final double trace()
public double norm1()
public final double norm2()
public double normInf()
public double normF()
public final double distanceCov(Matrix B)
B
- Matrix to compute distance from this (A)
public final double[] getDiagonal()
public void normalizeColumns()
public final boolean linearlyIndependent(Matrix columnMatrix)
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.
columnMatrix
- the column matrix to be tested for linear independence
public final Matrix exactGaussJordanElimination()
private final RationalNumber[][] exactGaussElimination()
private static final RationalNumber[][] exactGaussElimination(RationalNumber[][] gauss)
gauss
- an array of arrays of RationalNumber
public final boolean isSymmetric()
public final Matrix completeBasis()
public final Matrix completeToOrthonormalBasis()
public final Matrix appendColumns(Matrix columns)
columns
- the columns to be appended
public final Matrix orthonormalize()
public final Matrix cheatToAvoidSingularity(double constant)
constant
- value to add to the diagonal entries
public static final Matrix read(BufferedReader input) throws IOException
input
- the input stream.
IOException
- on input errorprotected void checkMatrixDimensions(MatrixLike<?> B)
public int hashCode()
hashCode
in class Object
public boolean equals(Object obj)
equals
in class Object
public boolean almostEquals(Object obj, double maxdelta)
obj
- other object to compare withmaxdelta
- maximum delta allowed
public boolean almostEquals(Object obj)
obj
- other object to compare with
DELTA
public String dimensionInfo()
public String toString()
toString
in class Object
|
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |