|
|
|||||||||||||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.lmu.ifi.dbs.elki.math.linearalgebra.Matrix
public class Matrix
The Matrix Class represents real-valued matrices.
For a MatrixM
we have therefore M ∈$real;<sup>m × n</sup>
,
where m
and n
are the number of rows and columns, respectively.
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 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. |
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 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 |
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 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. |
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 |
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 |
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 |
---|
private static final long serialVersionUID
public static final double DELTA
private double[][] elements
private int rowdimension
private int columndimension
Constructor Detail |
---|
private Matrix()
public Matrix(int m, int n)
m
- number of rowsn
- number of columspublic Matrix(int m, int n, double s)
m
- number of rowsn
- number of columss
- 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.Method Detail |
---|
public static Matrix constructWithCopy(double[][] A)
A
- Two-dimensional array of doubles.
IllegalArgumentException
- All rows must have the same lengthpublic Matrix copy()
public Object clone()
clone
in class Object
public double[][] getArray()
public double[][] getArrayCopy()
public double[] getColumnPackedCopy()
public double[] getRowPackedCopy()
public int getRowDimensionality()
public int getColumnDimensionality()
public double get(int i, int j)
i
- Row index.j
- Column index.
ArrayIndexOutOfBoundsException
public 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 Matrix getMatrix(int[] r, int[] c)
r
- Array of row indices.c
- Array of column indices.
ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int i0, int i1, int[] c)
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.
ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int[] r, int j0, int j1)
r
- Array of row indices.j0
- Initial column indexj1
- Final column index
ArrayIndexOutOfBoundsException
- Submatrix indicespublic void set(int i, int j, double s)
i
- Row index.j
- Column index.s
- A(i,j).
ArrayIndexOutOfBoundsException
public void 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
public 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 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 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 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 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 Vector getColumnVector(int j)
j
th column of this matrix as vector.
j
- the index of the column to be returned
j
th column of this matrixpublic 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 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 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 Matrix transpose()
public double norm1()
public double norm2()
public double normInf()
public double normF()
public Matrix uminus()
public Matrix plus(Matrix B)
B
- another matrix
public Matrix plusEquals(Matrix B)
B
- another matrix
public Matrix minus(Matrix B)
B
- another matrix
public Matrix minusEquals(Matrix B)
B
- another matrix
public Matrix arrayTimes(Matrix B)
B
- another matrix
public Matrix arrayTimesEquals(Matrix B)
B
- another matrix
public Matrix arrayRightDivide(Matrix B)
B
- another matrix
public Matrix arrayRightDivideEquals(Matrix B)
B
- another matrix
public Matrix arrayLeftDivide(Matrix B)
B
- another matrix
public Matrix arrayLeftDivideEquals(Matrix B)
B
- another matrix
public Matrix times(double s)
s
- scalar
public Matrix timesEquals(double s)
s
- scalar
public Matrix times(Matrix B)
B
- another matrix
IllegalArgumentException
- Matrix inner dimensions must agree.public LUDecomposition lu()
LUDecomposition
public QRDecomposition qr()
QRDecomposition
public CholeskyDecomposition chol()
CholeskyDecomposition
public SingularValueDecomposition svd()
SingularValueDecomposition
public EigenvalueDecomposition eig()
EigenvalueDecomposition
public Matrix solve(Matrix B)
B
- right hand side
public Matrix solveTranspose(Matrix B)
B
- right hand side
public Matrix inverse()
public double det()
public int rank()
public double cond()
public double trace()
public static Matrix random(int m, int n)
m
- Number of rows.n
- Number of colums.
public static Matrix identity(int m, int n)
m
- Number of rows.n
- Number of colums.
public void print(int w, int d)
w
- Column width.d
- Number of digits after the decimal.public void print(PrintWriter output, int w, int d)
output
- Output stream.w
- Column width.d
- Number of digits after the decimal.public void print(NumberFormat format, int width)
format
- A Formatting object for individual elements.width
- Field width for each column.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
public void print(PrintWriter output, NumberFormat format, int width)
output
- the output stream.format
- A formatting object to format the matrix elementswidth
- Column width.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
public static Matrix read(BufferedReader input) throws IOException
input
- the input stream.
IOException
- on input errorprivate void checkMatrixDimensions(Matrix B)
public String toString(int w, int d)
w
- column widthd
- number of digits after the decimal
public String toString()
toString
in class Object
public String toString(String pre)
pre<\code> is prefixed.
- Parameters:
pre
- the prefix of each line
- Returns:
- a string representation of this matrix
public String toString(NumberFormat nf)
nf
- NumberFormat to specify output precision
public String toString(String pre, NumberFormat nf)
pre<\code> is prefixed.
- Parameters:
nf
- number format for output accuracypre
- the prefix of each line
- Returns:
- a string representation of this matrix
public double[] getDiagonal()
public void scaleColumns(double scale)
scale
- the factor to scale the columnpublic void normalizeColumns()
public void scaleColumn(int j, double scale)
j
- the index of the column to be scaledscale
- the factor to scale the columnpublic double distanceCov(Matrix B)
B
- Matrix to compute distance from this (A)
public double angle(int colA, Matrix B, int colB)
colA
- the column of A to compute angle forB
- second MatrixcolB
- the column of B to compute angle for
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 euclideanNorm(int col)
col
- The column to compute euclidean norm of
public static Matrix diagonal(double[] diagonal)
diagonal
- the values on the diagonal
public static Matrix diagonal(Vector diagonal)
diagonal
- the values on the diagonal
public void swapRow(int i, int j)
i
- the first rowj
- the second rowpublic void swapColumn(int i, int j)
i
- the first columnj
- the second columnpublic Matrix projection(Matrix v)
v
- the subspace matrix
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 rowspublic static Matrix unitMatrix(int dim)
dim
- the dimensionality of the unit matrix
public static Matrix zeroMatrix(int dim)
dim
- the dimensionality of the unit matrix
public 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 Matrix exactGaussJordanElimination()
private RationalNumber[][] exactGaussElimination()
private static RationalNumber[][] exactGaussElimination(RationalNumber[][] gauss)
gauss
- an array of arrays of RationalNumber
public boolean isSymmetric()
public String dimensionInfo()
public Matrix completeBasis()
public Matrix completeToOrthonormalBasis()
public Matrix appendColumns(Matrix columns)
columns
- the columns to be appended
public Matrix orthonormalize()
public Matrix cheatToAvoidSingularity(double constant)
constant
- value to add to the diagonal entries
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
|
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |