java.lang.Object
de.lmu.ifi.dbs.elki.math.linearalgebra.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.
- Author:
- Elke Achtert, Erich Schubert
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleA small number to handle numbers near 0 as 0.protected final double[][]Array for internal storage of elements.static final StringError when matrix dimensions do not agree.static final StringError: matrix not square.static final StringError: matrix indexes incorrect -
Constructor Summary
ConstructorsConstructorDescriptionMatrix(double[][] elements) Constructs a matrix from a 2-D array.Matrix(double[] values, int m) Construct a matrix from a one-dimensional packed arrayMatrix(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 ofRationalNumbers.Constructor, cloning an existing matrix. -
Method Summary
Modifier and TypeMethodDescriptionbooleanalmostEquals(Object obj) Compare two matrices with a delta parameter to take numerical errors into account.booleanalmostEquals(Object obj, double maxdelta) Compare two matrices with a delta parameter to take numerical errors into account.final MatrixappendColumns(Matrix columns) Returns a matrix which consists of this matrix and the specified columns.final MatrixcheatToAvoidSingularity(double constant) Adds a given value to the diagonal entries if the entry is smaller than the constant.protected voidCheck if size(A) == size(B)clone()Clone the Matrix object.final doublecond()Matrix condition (2 norm)static final MatrixconstructWithCopy(double[][] A) Construct a matrix from a copy of a 2-D array.final Matrixcopy()Make a deep copy of a matrix.final doubledet()Matrix determinantstatic final Matrixdiagonal(double[] diagonal) Returns a quadratic Matrix consisting of zeros and of the given values on the diagonal.static final MatrixReturns a quadratic Matrix consisting of zeros and of the given values on the diagonal.booleanfinal MatrixReturns a matrix derived by Gauss-Jordan-elimination using RationalNumbers for the transformations.final doubleget(int i, int j) Get a single element.final double[][]Copy the internal two-dimensional array.final double[][]Access the internal two-dimensional array.final VectorgetCol(int j) Returns thejth column of this matrix as vector.final intReturns the dimensionality of the columns of this matrix.final double[]Make a one-dimensional column packed copy of the internal array.final MatrixgetMatrix(int[] r, int[] c) Get a submatrix.final MatrixgetMatrix(int[] r, int j0, int j1) Get a submatrix.final MatrixgetMatrix(int i0, int i1, int[] c) Get a submatrix.final MatrixgetMatrix(int i0, int i1, int j0, int j1) Get a submatrix.final VectorgetRow(int i) Returns theith row of this matrix as vector.final intReturns the dimensionality of the rows of this matrix.final double[]Make a one-dimensional row packed copy of the internal array.inthashCode()static final Matrixidentity(int m, int n) Generate identity matrixfinal Matrixincrement(int i, int j, double s) Increments a single element.final Matrixinverse()Matrix inverse or pseudoinversefinal booleanReturns true, if this matrix is symmetric, false otherwise.final MatrixC = A - Bfinal MatrixA = A - Bfinal MatrixminusTimes(Matrix B, double s) C = A - s * Bfinal MatrixminusTimesEquals(Matrix B, double s) A = A - s * Bfinal doublenorm1()One normfinal doublenorm2()Two normfinal voidNormalizes the columns of this matrix to length of 1.0.final doublenormF()Frobenius normfinal doublenormInf()Infinity normfinal MatrixReturns an orthonormalization of this matrix.final MatrixC = A + Bfinal MatrixplusEquals(Matrix B) A = A + Bfinal MatrixC = A + s * Bfinal MatrixplusTimesEquals(Matrix B, double s) A = A + s * Bstatic final Matrixrandom(int m, int n) Generate matrix with random elementsfinal intrank()Matrix rankfinal Matrixset(int i, int j, double s) Set a single element.final voidSets thejth column of this matrix to the specified column.final voidSet a submatrix.final voidSet a submatrix.final voidSet a submatrix.final voidSet a submatrix.final voidSets thejth row of this matrix to the specified vector.final MatrixSolve A*X = Bfinal Matrixtimes(double s) Multiply a matrix by a scalar, C = s*Afinal MatrixLinear algebraic matrix multiplication, A * Bfinal VectorLinear algebraic matrix multiplication, A * Bfinal MatrixtimesEquals(double s) Multiply a matrix by a scalar in place, A = s*Afinal MatrixLinear algebraic matrix multiplication, A * B^TtoString()toString returns String-representation of Matrix.final doubletrace()Matrix trace.final MatrixMatrix transpose.final MatrixLinear algebraic matrix multiplication, AT * Bfinal VectorLinear algebraic matrix multiplication, AT * Bfinal MatrixLinear algebraic matrix multiplication, A^T * B^T.static final MatrixunitMatrix(int dim) Returns the unit matrix of the specified dimension.static final MatrixzeroMatrix(int dim) Returns the zero matrix of the specified dimension.
-
Field Details
-
DELTA
public static final double DELTAA small number to handle numbers near 0 as 0.- See Also:
-
ERR_NOTSQUARE
Error: matrix not square.- See Also:
-
ERR_REINDEX
Error: matrix indexes incorrect- See Also:
-
ERR_MATRIX_DIMENSIONS
Error when matrix dimensions do not agree.- See Also:
-
elements
protected final double[][] elementsArray for internal storage of elements.
-
-
Constructor Details
-
Matrix
public Matrix(int m, int n) Constructs an m-by-n matrix of zeros.- Parameters:
m- number of rowsn- number of columns
-
Matrix
public Matrix(int m, int n, double s) Constructs an m-by-n constant matrix.- Parameters:
m- number of rowsn- number of columnss- 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
Constructs a Matrix for a given array of arrays ofRationalNumbers.- 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.
-
Matrix
Constructor, cloning an existing matrix.- Parameters:
mat- Matrix to clone
-
-
Method Details
-
constructWithCopy
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
-
unitMatrix
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
Returns the zero matrix of the specified dimension.- Parameters:
dim- the dimensionality of the unit matrix- Returns:
- the zero matrix of the specified dimension
-
random
Generate matrix with random elements- Parameters:
m- Number of rows.n- Number of columns.- Returns:
- An m-by-n matrix with uniformly distributed random elements.
-
identity
Generate identity matrix- Parameters:
m- Number of rows.n- Number of columns.- Returns:
- An m-by-n matrix with ones on the diagonal and zeros elsewhere.
-
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
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
-
copy
Make a deep copy of a matrix.- Returns:
- a new matrix containing the same values as this matrix
-
clone
Clone the Matrix object. -
getArrayRef
public final double[][] getArrayRef()Access the internal two-dimensional array.- Returns:
- Pointer to the two-dimensional array of matrix elements.
-
getArrayCopy
public final double[][] getArrayCopy()Copy the internal two-dimensional array.- Returns:
- Two-dimensional array copy of matrix elements.
-
getRowDimensionality
public final int getRowDimensionality()Returns the dimensionality of the rows of this matrix.- Returns:
- m, the number of rows.
-
getColumnDimensionality
public final int getColumnDimensionality()Returns the dimensionality of the columns of this matrix.- Returns:
- n, the number of columns.
-
get
public final 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 error
-
set
Set a single element.- Parameters:
i- Row index.j- Column index.s- A(i,j).- Returns:
- modified matrix
- Throws:
ArrayIndexOutOfBoundsException- on bounds error
-
increment
Increments a single element.- Parameters:
i- the row indexj- the column indexs- the increment value: A(i,j) = A(i.j) + s.- Returns:
- modified matrix
- Throws:
ArrayIndexOutOfBoundsException- on bounds error
-
getRowPackedCopy
public final double[] getRowPackedCopy()Make a one-dimensional row packed copy of the internal array.- Returns:
- Matrix elements packed in a one-dimensional array by rows.
-
getColumnPackedCopy
public final double[] getColumnPackedCopy()Make a one-dimensional column packed copy of the internal array.- Returns:
- Matrix elements packed in a one-dimensional array by columns.
-
getMatrix
Get a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexj0- Initial column indexj1- Final column index- Returns:
- A(i0:i1,j0:j1)
- Throws:
ArrayIndexOutOfBoundsException- Submatrix indices
-
getMatrix
Get a submatrix.- Parameters:
r- Array of row indices.c- Array of column indices.- Returns:
- A(r(:),c(:))
- Throws:
ArrayIndexOutOfBoundsException- Submatrix indices
-
getMatrix
Get a submatrix.- Parameters:
r- Array of row indices.j0- Initial column indexj1- Final column index- Returns:
- A(r(:),j0:j1)
- Throws:
ArrayIndexOutOfBoundsException- Submatrix indices
-
getMatrix
Get a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexc- Array of column indices.- Returns:
- A(i0:i1,c(:))
- Throws:
ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
Set a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexj0- Initial column indexj1- Final column indexX- A(i0:i1,j0:j1)- Throws:
ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
Set a submatrix.- Parameters:
r- Array of row indices.c- Array of column indices.X- A(r(:),c(:))- Throws:
ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
Set a submatrix.- Parameters:
r- Array of row indices.j0- Initial column indexj1- Final column indexX- A(r(:),j0:j1)- Throws:
ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
Set a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexc- Array of column indices.X- A(i0:i1,c(:))- Throws:
ArrayIndexOutOfBoundsException- Submatrix indices
-
getRow
Returns theith row of this matrix as vector.- Parameters:
i- the index of the row to be returned- Returns:
- the
ith row of this matrix
-
setRow
Sets thejth row of this matrix to the specified vector.- Parameters:
j- the index of the column to be setrow- the value of the column to be set
-
getCol
Returns thejth column of this matrix as vector.- Parameters:
j- the index of the column to be returned- Returns:
- the
jth column of this matrix
-
setCol
Sets thejth column of this matrix to the specified column.- Parameters:
j- the index of the column to be setcolumn- the value of the column to be set
-
transpose
Matrix transpose.- Returns:
- AT
-
plus
C = A + B- Parameters:
B- another matrix- Returns:
- A + B in a new Matrix
-
plusTimes
C = A + s * B- Parameters:
B- another matrixs- scalar- Returns:
- A + s * B in a new Matrix
-
plusEquals
A = A + B- Parameters:
B- another matrix- Returns:
- A + B in this Matrix
-
plusTimesEquals
A = A + s * B- Parameters:
B- another matrixs- Scalar- Returns:
- A + s * B in this Matrix
-
minus
C = A - B- Parameters:
B- another matrix- Returns:
- A - B in a new Matrix
-
minusTimes
C = A - s * B- Parameters:
B- another matrixs- Scalar- Returns:
- A - s * B in a new Matrix
-
minusEquals
A = A - B- Parameters:
B- another matrix- Returns:
- A - B in this Matrix
-
minusTimesEquals
A = A - s * B- Parameters:
B- another matrixs- Scalar- Returns:
- A - s * B in this Matrix
-
times
Multiply a matrix by a scalar, C = s*A- Parameters:
s- scalar- Returns:
- s*A
-
timesEquals
Multiply a matrix by a scalar in place, A = s*A- Parameters:
s- scalar- Returns:
- replace A by s*A
-
times
Linear algebraic matrix multiplication, A * B- Parameters:
B- another matrix- Returns:
- Matrix product, A * B
- Throws:
IllegalArgumentException- Matrix inner dimensions must agree.
-
times
Linear algebraic matrix multiplication, A * B- Parameters:
B- a vector- Returns:
- Matrix product, A * B
- Throws:
IllegalArgumentException- Matrix inner dimensions must agree.
-
transposeTimes
Linear algebraic matrix multiplication, AT * B- Parameters:
B- another matrix- Returns:
- Matrix product, AT * B
- Throws:
IllegalArgumentException- Matrix inner dimensions must agree.
-
transposeTimes
Linear algebraic matrix multiplication, AT * B- Parameters:
B- another matrix- Returns:
- Matrix product, AT * B
- Throws:
IllegalArgumentException- Matrix inner dimensions must agree.
-
timesTranspose
Linear algebraic matrix multiplication, A * B^T- Parameters:
B- another matrix- Returns:
- Matrix product, A * B^T
- Throws:
IllegalArgumentException- Matrix inner dimensions must agree.
-
transposeTimesTranspose
Linear algebraic matrix multiplication, A^T * B^T. Computed as (B*A)^T- Parameters:
B- another matrix- Returns:
- Matrix product, A^T * B^T
- Throws:
IllegalArgumentException- Matrix inner dimensions must agree.
-
solve
Solve A*X = B- Parameters:
B- right hand side- Returns:
- solution if A is square, least squares solution otherwise
-
inverse
Matrix inverse or pseudoinverse- Returns:
- inverse(A) if A is square, pseudoinverse otherwise.
-
det
public final double det()Matrix determinant- Returns:
- determinant
-
rank
public final int rank()Matrix rank- Returns:
- effective numerical rank, obtained from SVD.
-
cond
public final double cond()Matrix condition (2 norm)- Returns:
- ratio of largest to smallest singular value.
-
trace
public final double trace()Matrix trace.- Returns:
- sum of the diagonal elements.
-
norm1
public final double norm1()One norm- Returns:
- maximum column sum.
-
norm2
public final double norm2()Two norm- Returns:
- maximum singular value.
-
normInf
public final double normInf()Infinity norm- Returns:
- maximum row sum.
-
normF
public final double normF()Frobenius norm- Returns:
- sqrt of sum of squares of all elements.
-
normalizeColumns
public final void normalizeColumns()Normalizes the columns of this matrix to length of 1.0. -
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
-
isSymmetric
public final boolean isSymmetric()Returns true, if this matrix is symmetric, false otherwise.- Returns:
- true, if this matrix is symmetric, false otherwise
-
appendColumns
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
Returns an orthonormalization of this matrix.- Returns:
- the orthonormalized matrix
-
cheatToAvoidSingularity
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
-
checkMatrixDimensions
Check if size(A) == size(B) -
hashCode
public int hashCode() -
equals
-
almostEquals
Compare two matrices with a delta parameter to take numerical errors into account.- Parameters:
obj- other object to compare withmaxdelta- maximum delta allowed- Returns:
- true if delta smaller than maximum
-
almostEquals
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
-
toString
toString returns String-representation of Matrix.
-