public class Matrix extends Object
Modifier and Type | Field and Description |
---|---|
(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.
|
static String |
ERR_MATRIX_DIMENSIONS
Error when matrix dimensions do not agree.
|
private static String |
ERR_MATRIX_INNERDIM
Error when matrix inner dimensions do not agree.
|
static String |
ERR_NOTRECTANGULAR
Error: matrix not rectangular.
|
static String |
ERR_REINDEX
Error: matrix indexes incorrect
|
static double |
SINGULARITY_CHEAT
Small value to increment diagonally of a matrix in order to avoid
singularity before building the inverse.
|
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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(Matrix B)
Check if size(A) == size(B)
|
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.
|
boolean |
equals(Object obj) |
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.
|
Vector |
getCol(int j)
Returns the
j th column of this matrix as vector. |
int |
getColumnDimensionality()
Returns the dimensionality of the columns of this matrix.
|
double[] |
getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.
|
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.
|
Vector |
getRow(int i)
Returns the
i th row of this matrix as vector. |
int |
getRowDimensionality()
Returns the dimensionality of the rows of this matrix.
|
double[] |
getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.
|
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. |
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(double s)
C = A + s
|
Matrix |
plus(Matrix B)
C = A + B
|
Matrix |
plusDiagonal(double s)
C = A + s
|
Matrix |
plusDiagonalEquals(double s)
A = A + s
|
Matrix |
plusEquals(double s)
A = A + s
|
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
|
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.
|
Matrix |
robustInverse()
Matrix inverse for square matrixes.
|
Matrix |
set(int i,
int j,
double s)
Set a single element.
|
void |
setCol(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,
Vector row)
Sets the
j th row of this matrix to the specified vector. |
Matrix |
solve(Matrix B)
Solve A*X = B
|
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.
|
public static final double DELTA
public static final double SINGULARITY_CHEAT
public static final String ERR_NOTRECTANGULAR
public static final String ERR_REINDEX
public static final String ERR_MATRIX_DIMENSIONS
private static final String ERR_MATRIX_INNERDIM
protected final double[][] elements
final int columndimension
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
matrixIllegalArgumentException
- if not all rows conform in the same lengthpublic 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 clonepublic 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 matrixpublic static final Matrix zeroMatrix(int dim)
dim
- the dimensionality of the unit matrixpublic 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 diagonalpublic static final Matrix diagonal(Vector diagonal)
diagonal
- the values on the diagonalpublic final Matrix copy()
public final double[][] getArrayRef()
public final double[][] getArrayCopy()
public final int getRowDimensionality()
public final int getColumnDimensionality()
public final double get(int i, int j)
i
- Row index.j
- Column index.ArrayIndexOutOfBoundsException
- on bounds errorpublic final Matrix set(int i, int j, double s)
i
- Row index.j
- Column index.s
- A(i,j).ArrayIndexOutOfBoundsException
- on bounds errorpublic final Matrix increment(int i, int j, double s)
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 indexArrayIndexOutOfBoundsException
- 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 indexArrayIndexOutOfBoundsException
- 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 Vector getRow(int i)
i
th row of this matrix as vector.i
- the index of the row to be returnedi
th row of this matrixpublic final void setRow(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 Vector getCol(int j)
j
th column of this matrix as vector.j
- the index of the column to be returnedj
th column of this matrixpublic final void setCol(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()
public final Matrix plus(Matrix B)
B
- another matrixpublic final Matrix plus(double s)
s
- scalar valuepublic final Matrix plusDiagonal(double s)
s
- scalar valuepublic final Matrix plusTimes(Matrix B, double s)
B
- another matrixs
- scalarpublic final Matrix plusEquals(Matrix B)
B
- another matrixpublic final Matrix plusEquals(double s)
s
- constant to add to every cellpublic final Matrix plusDiagonalEquals(double s)
s
- constant to add to the diagonalpublic final Matrix plusTimesEquals(Matrix B, double s)
B
- another matrixs
- Scalarpublic final Matrix minus(Matrix B)
B
- another matrixpublic final Matrix minusTimes(Matrix B, double s)
B
- another matrixs
- Scalarpublic final Matrix minusEquals(Matrix B)
B
- another matrixpublic final Matrix minusTimesEquals(Matrix B, double s)
B
- another matrixs
- Scalarpublic final Matrix times(double s)
s
- scalarpublic final Matrix timesEquals(double s)
s
- scalarpublic final Matrix times(Matrix B)
B
- another matrixIllegalArgumentException
- Matrix inner dimensions must agree.public final Vector times(Vector B)
B
- a vectorIllegalArgumentException
- Matrix inner dimensions must agree.public final Vector transposeTimes(Vector B)
B
- another matrixIllegalArgumentException
- Matrix inner dimensions must agree.public final Matrix transposeTimes(Matrix B)
B
- another matrixIllegalArgumentException
- Matrix inner dimensions must agree.public final Matrix timesTranspose(Matrix B)
B
- another matrixIllegalArgumentException
- Matrix inner dimensions must agree.public final Matrix transposeTimesTranspose(Matrix B)
B
- another matrixIllegalArgumentException
- Matrix inner dimensions must agree.public final Matrix solve(Matrix B)
B
- right hand sidepublic final Matrix inverse()
public final Matrix robustInverse()
public final double det()
public final int rank()
public final double cond()
public final double trace()
public final double norm1()
public final double norm2()
public final double normInf()
public final double normF()
public final 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 independencepublic final boolean isSymmetric()
public final Matrix completeBasis()
public final Matrix completeToOrthonormalBasis()
public final Matrix appendColumns(Matrix columns)
columns
- the columns to be appendedpublic final Matrix orthonormalize()
public final Matrix cheatToAvoidSingularity(double constant)
constant
- value to add to the diagonal entriespublic static final Matrix read(BufferedReader input) throws IOException
input
- the input stream.IOException
- on input errorprotected void checkMatrixDimensions(Matrix B)
public boolean almostEquals(Object obj, double maxdelta)
obj
- other object to compare withmaxdelta
- maximum delta allowedpublic boolean almostEquals(Object obj)
obj
- other object to compare withDELTA
Copyright © 2015 ELKI Development Team, Lehr- und Forschungseinheit für Datenbanksysteme, Ludwig-Maximilians-Universität München. License information.