Package elki.math.linearalgebra
Class VMath
- java.lang.Object
-
- elki.math.linearalgebra.VMath
-
@Title("Vector and Matrix Math Library") public final class VMath extends java.lang.Object
Class providing basic vector mathematics, for low-level vectors stored asdouble[]
. While this is less nice syntactically, it reduces memory usage and VM overhead.- Since:
- 0.5.0
- Author:
- Erich Schubert
-
-
Field Summary
Fields Modifier and Type Field Description private static double
DELTA
A small number to handle numbers near 0 as 0.protected static java.lang.String
ERR_DIMENSIONS
Error message when dimensionalities do not agree.protected static java.lang.String
ERR_INVALID_RANGE
Error message when min > max is given as a range.protected static java.lang.String
ERR_MATRIX_DIMENSIONS
Error message when matrix dimensionalities do not agree.protected static java.lang.String
ERR_MATRIX_INNERDIM
Error message when matrix dimensionalities do not agree.protected static java.lang.String
ERR_MATRIX_NONSQUARE
Error when a non-square matrix is used with determinant.protected static java.lang.String
ERR_MATRIX_NOT_SPD
When a symmetric positive definite matrix is required.protected static java.lang.String
ERR_MATRIX_RANK_DEFICIENT
When a matrix is rank deficient.protected static java.lang.String
ERR_SINGULAR
Error with a singular matrix.protected static java.lang.String
ERR_VEC_DIMENSIONS
Error message when vector dimensionalities do not agree.
-
Constructor Summary
Constructors Modifier Constructor Description private
VMath()
Fake constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static boolean
almostEquals(double[][] m1, double[][] m2)
Compare two matrices with a delta parameter to take numerical errors into account.static boolean
almostEquals(double[][] m1, double[][] m2, double maxdelta)
Compare two matrices with a delta parameter to take numerical errors into account.static boolean
almostEquals(double[] m1, double[] m2)
Compare two matrices with a delta parameter to take numerical errors into account.static boolean
almostEquals(double[] m1, double[] m2, double maxdelta)
Compare two matrices with a delta parameter to take numerical errors into account.static double
angle(double[] v1, double[] v2)
Compute the cosine of the angle between two vectors, where the smaller angle between those vectors is viewed.static double
angle(double[] v1, double[] v2, double[] o)
Compute the cosine of the angle between two vectors, where the smaller angle between those vectors is viewed.static double[][]
appendColumns(double[][] m1, double[][] m2)
Returns a matrix which consists of this matrix and the specified columns.static int
argmax(double[] v)
Find the maximum value.static int
argmax(double[] v, int start, int end)
Find the maximum value.static int
argmin(double[] v)
Find the minimum value.static int
argmin(double[] v, int start, int end)
Find the minimum value.static void
clear(double[] v1)
Reset the vector to 0.static void
clear(double[][] m)
Reset the matrix to 0.static double[]
columnPackedCopy(double[][] m1)
Make a one-dimensional column packed copy of the internal array.static double[]
copy(double[] v)
Returns a copy of this vector.static double[][]
copy(double[][] m1)
Make a deep copy of a matrix.static double[][]
diagonal(double[] v1)
Returns a quadratic matrix consisting of zeros and of the given values on the diagonal.static double
dot(double[] v1, double[] v2)
Returns the dot product (scalar product) of two vectors, v1·v2 = v1T v2.static boolean
equals(double[][] m1, double[][] m2)
Test for equalitystatic boolean
equals(double[] v1, double[] v2)
Compare for equality.static double
euclideanLength(double[] v1)
Euclidean length of the vector sqrt(v1T v1).static double[]
getCol(double[][] m1, int col)
Get a column from a matrix as vector.static int
getColumnDimensionality(double[][] m1)
Returns the dimensionality of the columns of this matrix.static double[]
getDiagonal(double[][] m1)
getDiagonal returns array of diagonal-elements.static double[][]
getMatrix(double[][] m1, int[] r, int[] c)
Get a submatrix.static double[][]
getMatrix(double[][] m1, int[] r, int c0, int c1)
Get a submatrix.static double[][]
getMatrix(double[][] m1, int r0, int r1, int[] c)
Get a submatrix.static double[][]
getMatrix(double[][] m1, int r0, int r1, int c0, int c1)
Get a submatrix.static double[]
getRow(double[][] m1, int r)
Returns ther
th row of this matrix as vector.static int
getRowDimensionality(double[][] m1)
Returns the dimensionality of the rows of this matrix.static int
hashCode(double[] v1)
Compute the hash code for the vector.static int
hashCode(double[][] m1)
Compute hash codestatic double[][]
identity(int m, int n)
Generate unit / identity / "eye" matrix.static double[][]
inverse(double[][] A)
Matrix inverse or pseudoinversestatic double
mahalanobisDistance(double[][] B, double[] a, double[] c)
Matrix multiplication, (a-c)T * B * (a-c)static double[][]
minus(double[][] m1, double[][] m2)
Component-wise matrix subtraction m3 = m1 - m2.static double[]
minus(double[] v1, double s1)
Subtract component-wise v1 - s1.static double[]
minus(double[] v1, double[] v2)
Computes component-wise v1 - v2.static double[][]
minusEquals(double[][] m1, double[][] m2)
Component-wise matrix subtraction: m1 = m1 - m2, overwriting the existing matrix m1.static double[]
minusEquals(double[] v1, double s1)
Subtract component-wise in-place v1 = v1 - s1, overwriting the vector v1.static double[]
minusEquals(double[] v1, double[] v2)
Computes component-wise v1 = v1 - v2, overwriting the vector v1.static double[][]
minusTimes(double[][] m1, double[][] m2, double s2)
Component-wise matrix operation: m3 = m1 - m2 * s2static double[]
minusTimes(double[] v1, double[] v2, double s2)
Computes component-wise v1 - v2 * s2.static double[][]
minusTimesEquals(double[][] m1, double[][] m2, double s2)
Component-wise matrix operation: m1 = m1 - m2 * s2, overwriting the existing matrix m1.static double[]
minusTimesEquals(double[] v1, double[] v2, double s2)
Computes component-wise v1 = v1 - v2 * s2, overwriting the vector v1.static double[]
normalize(double[] v1)
Normalizes v1 to the length of 1.0.static void
normalizeColumns(double[][] m1)
Normalizes the columns of this matrix to length of 1.0.static double[]
normalizeEquals(double[] v1)
Normalizes v1 to the length of 1.0 in place.static double
normF(double[][] elements)
Frobenius normstatic double[][]
orthonormalize(double[][] m1)
Returns an orthonormalization of this matrix.static double[]
overwriteTimes(double[] v1, double[] v2, double s)
Multiply component-wise v1 = v2 * s, overwriting the vector v1.static double[][]
plus(double[][] m1, double[][] m2)
Component-wise matrix sum: m3 = m1 + m2.static double[]
plus(double[] v1, double s1)
Computes component-wise v1 + s1.static double[]
plus(double[] v1, double[] v2)
Computes component-wise v1 + v2 for vectors.static double[][]
plusEquals(double[][] m1, double[][] m2)
Component-wise matrix operation: m1 = m1 + m2, overwriting the existing matrix m1.static double[]
plusEquals(double[] v1, double s1)
Computes component-wise v1 = v1 + s1, overwriting the vector v1.static double[]
plusEquals(double[] v1, double[] v2)
Computes component-wise v1 = v1 + v2, overwriting the vector v1.static double[][]
plusTimes(double[][] m1, double[][] m2, double s2)
Component-wise matrix operation: m3 = m1 + m2 * s2.static double[]
plusTimes(double[] v1, double[] v2, double s2)
Computes component-wise v1 + v2 * s2.static double[][]
plusTimesEquals(double[][] m1, double[][] m2, double s2)
Component-wise matrix operation: m1 = m1 + m2 * s2, overwriting the existing matrix m1.static double[]
plusTimesEquals(double[] v1, double[] v2, double s2)
Computes component-wise v1 = v1 + v2 * s2, overwriting the vector v1.static double[]
rotate90Equals(double[] v1)
Rotate the two-dimensional vector by 90 degrees.static double[]
rowPackedCopy(double[][] m1)
Make a one-dimensional row packed copy of the internal array.static double
scalarProduct(double[] v1, double[] v2)
Returns the scalar product (dot product) of two vectors, <v1,v2> = v1T v2.static void
setCol(double[][] m1, int c, double[] column)
Sets thec
th column of this matrix to the specified column.static void
setMatrix(double[][] m1, int[] r, int[] c, double[][] m2)
Set a submatrix.static void
setMatrix(double[][] m1, int[] r, int c0, int c1, double[][] m2)
Set a submatrix.static void
setMatrix(double[][] m1, int r0, int r1, int[] c, double[][] m2)
Set a submatrix.static void
setMatrix(double[][] m1, int r0, int r1, int c0, int c1, double[][] m2)
Set a submatrix.static void
setRow(double[][] m1, int r, double[] row)
Sets ther
th row of this matrix to the specified vector.static double[]
solve(double[][] A, double[] b)
Solve A*X = bstatic double[][]
solve(double[][] A, double[][] B)
Solve A*X = Bstatic double
squareSum(double[] v1)
Squared Euclidean length of the vector v1T v1 = v1·v1.static double
sum(double[] v1)
Sum of the vector components.static double[][]
times(double[][] m1, double s1)
Multiply a matrix by a scalar component-wise, m3 = m1 * s1.static double[]
times(double[][] m1, double[] v2)
Matrix with vector multiplication, m1 * v2.static double[][]
times(double[][] m1, double[][] m2)
Matrix multiplication, m1 * m2.static double[]
times(double[] v1, double s1)
Multiply component-wise v1 * s1.static double[][]
times(double[] v1, double[][] m2)
Deprecated.this is fairly inefficient memory layout, rewriting your codestatic double[][]
timesEquals(double[][] m1, double s1)
Multiply a matrix by a scalar component-wise in place, m1 = m1 * s1, overwriting the existing matrix m1.static double[]
timesEquals(double[] v1, double s)
Multiply component-wise v1 = v1 * s1, overwriting the vector v1.static double[]
timesMinus(double[] v1, double s1, double[] v2)
Computes component-wise v1 * s1 - v2.static double[]
timesMinusEquals(double[] v1, double s1, double[] v2)
Computes component-wise v1 = v1 * s1 - v2, overwriting the vector v1.static double[]
timesMinusTimes(double[] v1, double s1, double[] v2, double s2)
Computes component-wise v1 * s1 - v2 * s2.static double[]
timesMinusTimesEquals(double[] v1, double s1, double[] v2, double s2)
Computes component-wise v1 = v1 * s1 - v2 * s2, overwriting the vector v1.static double[]
timesPlus(double[] v1, double s1, double[] v2)
Computes component-wise v1 * s1 + v2.static double[]
timesPlusEquals(double[] v1, double s1, double[] v2)
Computes component-wise v1 = v1 * s1 + v2, overwriting the vector v1.static double[]
timesPlusTimes(double[] v1, double s1, double[] v2, double s2)
Computes component-wise v1 * s1 + v2 * s2.static double[]
timesPlusTimesEquals(double[] v1, double s1, double[] v2, double s2)
Computes component-wise v1 = v1 * s1 + v2 * s2, overwriting the vector v1.static double[][]
timesTranspose(double[][] m1, double[][] m2)
Matrix multiplication, m1 * m2Tstatic double[][]
timesTranspose(double[] v1, double[] v2)
Vectors to matrix multiplication, v1 * v2T.static double[][]
timesTranspose(double[] v1, double[][] m2)
Deprecated.this is fairly inefficient memory layout, rewriting your codestatic double[][]
transpose(double[] v)
Transpose vector to a matrix without copying.static double[][]
transpose(double[][] m1)
Matrix transposestatic double[][]
transposeDiagonalTimes(double[][] m1, double[] d2, double[][] m3)
Matrix multiplication with diagonal, m1^T * d2 * m3static double[]
transposeTimes(double[][] m1, double[] v2)
Transposed matrix with vector multiplication, m1T * v2static double[][]
transposeTimes(double[][] m1, double[][] m2)
Matrix multiplication, m1T * m2static double
transposeTimes(double[] v1, double[] v2)
Vector scalar product (dot product), v1T v2 = v1·v2 = <v1,v2>.static double[][]
transposeTimes(double[] v1, double[][] m2)
Vector to matrix multiplication, v1T m2.static double
transposeTimesTimes(double[] v1, double[][] m2, double[] v3)
Matrix multiplication, v1T * m2 * v3static double[][]
transposeTimesTranspose(double[][] m1, double[][] m2)
Matrix multiplication, m1T * m2T.static double[][]
unitMatrix(int dim)
Returns the unit / identity / "eye" matrix of the specified dimension.static double[]
unitVector(int dimensionality, int i)
Returns the ith unit vector of the specified dimensionality.static double[][]
zeroMatrix(int dim)
Returns the zero matrix of the specified dimension.
-
-
-
Field Detail
-
DELTA
private static final double DELTA
A small number to handle numbers near 0 as 0.- See Also:
- Constant Field Values
-
ERR_VEC_DIMENSIONS
protected static final java.lang.String ERR_VEC_DIMENSIONS
Error message when vector dimensionalities do not agree.- See Also:
- Constant Field Values
-
ERR_MATRIX_DIMENSIONS
protected static final java.lang.String ERR_MATRIX_DIMENSIONS
Error message when matrix dimensionalities do not agree.- See Also:
- Constant Field Values
-
ERR_MATRIX_INNERDIM
protected static final java.lang.String ERR_MATRIX_INNERDIM
Error message when matrix dimensionalities do not agree.- See Also:
- Constant Field Values
-
ERR_DIMENSIONS
protected static final java.lang.String ERR_DIMENSIONS
Error message when dimensionalities do not agree.- See Also:
- Constant Field Values
-
ERR_INVALID_RANGE
protected static final java.lang.String ERR_INVALID_RANGE
Error message when min > max is given as a range.- See Also:
- Constant Field Values
-
ERR_MATRIX_NONSQUARE
protected static final java.lang.String ERR_MATRIX_NONSQUARE
Error when a non-square matrix is used with determinant.- See Also:
- Constant Field Values
-
ERR_SINGULAR
protected static final java.lang.String ERR_SINGULAR
Error with a singular matrix.- See Also:
- Constant Field Values
-
ERR_MATRIX_NOT_SPD
protected static final java.lang.String ERR_MATRIX_NOT_SPD
When a symmetric positive definite matrix is required.- See Also:
- Constant Field Values
-
ERR_MATRIX_RANK_DEFICIENT
protected static final java.lang.String ERR_MATRIX_RANK_DEFICIENT
When a matrix is rank deficient.- See Also:
- Constant Field Values
-
-
Method Detail
-
unitVector
public static double[] unitVector(int dimensionality, int i)
Returns the ith unit vector of the specified dimensionality.- Parameters:
dimensionality
- the dimensionality of the vectori
- the index- Returns:
- the ith unit vector of the specified dimensionality
-
copy
public static double[] copy(double[] v)
Returns a copy of this vector.- Parameters:
v
- original vector- Returns:
- a copy of this vector
-
transpose
public static double[][] transpose(double[] v)
Transpose vector to a matrix without copying.- Parameters:
v
- Vector- Returns:
- Matrix
-
plus
public static double[] plus(double[] v1, double[] v2)
Computes component-wise v1 + v2 for vectors.- Parameters:
v1
- first vectorv2
- second vector- Returns:
- the sum v1 + v2
-
plusTimes
public static double[] plusTimes(double[] v1, double[] v2, double s2)
Computes component-wise v1 + v2 * s2.- Parameters:
v1
- first vectorv2
- second vectors2
- the scalar- Returns:
- the result of v1 + v2 * s2
-
timesPlus
public static double[] timesPlus(double[] v1, double s1, double[] v2)
Computes component-wise v1 * s1 + v2.- Parameters:
v1
- first vectors1
- the scalar for v1v2
- second vector- Returns:
- the result of v1 * s1 + v2
-
timesPlusTimes
public static double[] timesPlusTimes(double[] v1, double s1, double[] v2, double s2)
Computes component-wise v1 * s1 + v2 * s2.- Parameters:
v1
- first vectors1
- the scalar for v1v2
- second vectors2
- the scalar for v2- Returns:
- the result of v1 * s1 + v2 * s2
-
plusEquals
public static double[] plusEquals(double[] v1, double[] v2)
Computes component-wise v1 = v1 + v2, overwriting the vector v1.- Parameters:
v1
- first vector (overwritten)v2
- second vector- Returns:
- v1 = v1 + v2
-
plusTimesEquals
public static double[] plusTimesEquals(double[] v1, double[] v2, double s2)
Computes component-wise v1 = v1 + v2 * s2, overwriting the vector v1.- Parameters:
v1
- first vector (overwritten)v2
- another vectors2
- scalar factor for v2- Returns:
- v1 = v1 + v2 * s2
-
timesPlusEquals
public static double[] timesPlusEquals(double[] v1, double s1, double[] v2)
Computes component-wise v1 = v1 * s1 + v2, overwriting the vector v1.- Parameters:
v1
- first vector (overwritten)s1
- scalar factor for v1v2
- another vector- Returns:
- v1 = v1 * s1 + v2
-
timesPlusTimesEquals
public static double[] timesPlusTimesEquals(double[] v1, double s1, double[] v2, double s2)
Computes component-wise v1 = v1 * s1 + v2 * s2, overwriting the vector v1.- Parameters:
v1
- first vector (overwritten)s1
- scalar for v1v2
- another vectors2
- scalar for v2- Returns:
- v1 = v1 * s1 + v2 * s2
-
plus
public static double[] plus(double[] v1, double s1)
Computes component-wise v1 + s1.- Parameters:
v1
- vector to add tos1
- constant value to add- Returns:
- v1 + s1
-
plusEquals
public static double[] plusEquals(double[] v1, double s1)
Computes component-wise v1 = v1 + s1, overwriting the vector v1.- Parameters:
v1
- vector to add to (overwritten)s1
- constant value to add- Returns:
- Modified vector
-
minus
public static double[] minus(double[] v1, double[] v2)
Computes component-wise v1 - v2.- Parameters:
v1
- first vectorv2
- the vector to be subtracted from this vector- Returns:
- v1 - v2
-
minusTimes
public static double[] minusTimes(double[] v1, double[] v2, double s2)
Computes component-wise v1 - v2 * s2.- Parameters:
v1
- first vectorv2
- the vector to be subtracted from this vectors2
- the scaling factor for v2- Returns:
- v1 - v2 * s2
-
timesMinus
public static double[] timesMinus(double[] v1, double s1, double[] v2)
Computes component-wise v1 * s1 - v2.- Parameters:
v1
- first vectors1
- the scaling factor for v1v2
- the vector to be subtracted from this vector- Returns:
- v1 * s1 - v2
-
timesMinusTimes
public static double[] timesMinusTimes(double[] v1, double s1, double[] v2, double s2)
Computes component-wise v1 * s1 - v2 * s2.- Parameters:
v1
- first vectors1
- the scaling factor for v1v2
- the vector to be subtracted from this vectors2
- the scaling factor for v2- Returns:
- v1 * s1 - v2 * s2
-
minusEquals
public static double[] minusEquals(double[] v1, double[] v2)
Computes component-wise v1 = v1 - v2, overwriting the vector v1.- Parameters:
v1
- vectorv2
- another vector- Returns:
- v1 = v1 - v2
-
minusTimesEquals
public static double[] minusTimesEquals(double[] v1, double[] v2, double s2)
Computes component-wise v1 = v1 - v2 * s2, overwriting the vector v1.- Parameters:
v1
- vectorv2
- another vectors2
- scalar for v2- Returns:
- v1 = v1 - v2 * s2
-
timesMinusEquals
public static double[] timesMinusEquals(double[] v1, double s1, double[] v2)
Computes component-wise v1 = v1 * s1 - v2, overwriting the vector v1.- Parameters:
v1
- vectors1
- scalar for v1v2
- another vector- Returns:
- v1 = v1 * s1 - v2
-
timesMinusTimesEquals
public static double[] timesMinusTimesEquals(double[] v1, double s1, double[] v2, double s2)
Computes component-wise v1 = v1 * s1 - v2 * s2, overwriting the vector v1.- Parameters:
v1
- vectors1
- scalar for v1v2
- another vectors2
- Scalar- Returns:
- v1 = v1 * s1 - v2 * s2
-
minus
public static double[] minus(double[] v1, double s1)
Subtract component-wise v1 - s1.- Parameters:
v1
- original vectors1
- Value to subtract- Returns:
- v1 - s1
-
minusEquals
public static double[] minusEquals(double[] v1, double s1)
Subtract component-wise in-place v1 = v1 - s1, overwriting the vector v1.- Parameters:
v1
- original vectors1
- Value to subtract- Returns:
- v1 = v1 - s1
-
times
public static double[] times(double[] v1, double s1)
Multiply component-wise v1 * s1.- Parameters:
v1
- original vectors1
- the scalar to be multiplied- Returns:
- v1 * s1
-
timesEquals
public static double[] timesEquals(double[] v1, double s)
Multiply component-wise v1 = v1 * s1, overwriting the vector v1.- Parameters:
v1
- original vectors
- scalar- Returns:
- v1 = v1 * s1
-
overwriteTimes
public static double[] overwriteTimes(double[] v1, double[] v2, double s)
Multiply component-wise v1 = v2 * s, overwriting the vector v1.- Parameters:
v1
- output vectorv2
- input vectors
- scalar- Returns:
- v1 with new values
-
times
@Deprecated public static double[][] times(double[] v1, double[][] m2)
Deprecated.this is fairly inefficient memory layout, rewriting your codeMatrix multiplication: v1 * m2 (m2 must have one row only).Note: this is an unusual operation, m2 must be a costly column matrix.
This method is equivalent to the
timesTranspose(double[], double[])
method with m2 being the second vector as matrix, but transposed.- Parameters:
v1
- vectorm2
- other matrix, must have one row.- Returns:
- Matrix product, v1 * m2
-
transposeTimes
public static double[][] transposeTimes(double[] v1, double[][] m2)
Vector to matrix multiplication, v1T m2.- Parameters:
v1
- vectorm2
- other matrix- Returns:
- Matrix product, v1T * m2
-
transposeTimes
public static double transposeTimes(double[] v1, double[] v2)
Vector scalar product (dot product), v1T v2 = v1·v2 = <v1,v2>.- Parameters:
v1
- vectorv2
- other vector- Returns:
- scalar result of matrix product, v1T * v2
-
timesTranspose
@Deprecated public static double[][] timesTranspose(double[] v1, double[][] m2)
Deprecated.this is fairly inefficient memory layout, rewriting your codeVector with transposed matrix multiplication, v1 * m2T (m2 must have one row only).Note: this is an unusual operation, m2 must be a costly column matrix.
This method is equivalent to the
timesTranspose(double[], double[])
method with m2 being the second vector as matrix.- Parameters:
v1
- vectorm2
- other matrix- Returns:
- Matrix product, v1 * m2T
-
timesTranspose
public static double[][] timesTranspose(double[] v1, double[] v2)
Vectors to matrix multiplication, v1 * v2T.- Parameters:
v1
- vectorv2
- other vector- Returns:
- Matrix product, v1 * v2T
-
scalarProduct
public static double scalarProduct(double[] v1, double[] v2)
Returns the scalar product (dot product) of two vectors, <v1,v2> = v1T v2.This is the same as
transposeTimes(double[], double[])
.- Parameters:
v1
- vectorv2
- other vector- Returns:
- double the scalar product of vectors v1 and v2
-
dot
public static double dot(double[] v1, double[] v2)
Returns the dot product (scalar product) of two vectors, v1·v2 = v1T v2.This is the same as
transposeTimes(double[], double[])
.- Parameters:
v1
- vectorv2
- other vector- Returns:
- double the scalar product of vectors v1 and v2
-
sum
public static double sum(double[] v1)
Sum of the vector components.- Parameters:
v1
- vector- Returns:
- sum of this vector
-
squareSum
public static double squareSum(double[] v1)
Squared Euclidean length of the vector v1T v1 = v1·v1.- Parameters:
v1
- vector- Returns:
- squared Euclidean length of this vector
-
euclideanLength
public static double euclideanLength(double[] v1)
Euclidean length of the vector sqrt(v1T v1).- Parameters:
v1
- vector- Returns:
- Euclidean length of this vector
-
argmax
public static int argmax(double[] v)
Find the maximum value.- Parameters:
v
- Vector- Returns:
- Position of maximum
-
argmax
public static int argmax(double[] v, int start, int end)
Find the maximum value.- Parameters:
v
- Vectorstart
- First to considerend
- End of range (exclusive)- Returns:
- Position of maximum, beginning at array index 0
-
argmin
public static int argmin(double[] v)
Find the minimum value.- Parameters:
v
- Vector- Returns:
- Position of minimum
-
argmin
public static int argmin(double[] v, int start, int end)
Find the minimum value.- Parameters:
v
- Vectorstart
- First to considerend
- End of range (exclusive)- Returns:
- Position of minimum, beginning at array index 0
-
normalize
public static double[] normalize(double[] v1)
Normalizes v1 to the length of 1.0.- Parameters:
v1
- vector- Returns:
- normalized copy of v1
-
normalizeEquals
public static double[] normalizeEquals(double[] v1)
Normalizes v1 to the length of 1.0 in place.- Parameters:
v1
- vector (overwritten)- Returns:
- normalized v1
-
hashCode
public static int hashCode(double[] v1)
Compute the hash code for the vector.- Parameters:
v1
- elements- Returns:
- hash code
-
equals
public static boolean equals(double[] v1, double[] v2)
Compare for equality.- Parameters:
v1
- first vectorv2
- second vector- Returns:
- comparison result
-
clear
public static void clear(double[] v1)
Reset the vector to 0.- Parameters:
v1
- vector
-
clear
public static void clear(double[][] m)
Reset the matrix to 0.- Parameters:
m
- Matrix
-
rotate90Equals
public static double[] rotate90Equals(double[] v1)
Rotate the two-dimensional vector by 90 degrees.- Parameters:
v1
- first vector- Returns:
- modified v1, rotated by 90 degrees
-
unitMatrix
public static double[][] unitMatrix(int dim)
Returns the unit / identity / "eye" matrix of the specified dimension.- Parameters:
dim
- the dimensionality of the unit matrix- Returns:
- the unit matrix of the specified dimension
-
zeroMatrix
public static double[][] 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
-
identity
public static double[][] identity(int m, int n)
Generate unit / identity / "eye" 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
public static double[][] diagonal(double[] v1)
Returns a quadratic matrix consisting of zeros and of the given values on the diagonal.- Parameters:
v1
- the values on the diagonal- Returns:
- the resulting matrix
-
copy
public static double[][] copy(double[][] m1)
Make a deep copy of a matrix.- Parameters:
m1
- Input matrix- Returns:
- a new matrix containing the same values as this matrix
-
rowPackedCopy
public static double[] rowPackedCopy(double[][] m1)
Make a one-dimensional row packed copy of the internal array.- Parameters:
m1
- Input matrix- Returns:
- Matrix elements packed in a one-dimensional array by rows.
-
columnPackedCopy
public static double[] columnPackedCopy(double[][] m1)
Make a one-dimensional column packed copy of the internal array.- Parameters:
m1
- Input matrix- Returns:
- Matrix elements packed in a one-dimensional array by columns.
-
getMatrix
public static double[][] getMatrix(double[][] m1, int r0, int r1, int c0, int c1)
Get a submatrix.- Parameters:
m1
- Input matrixr0
- Initial row indexr1
- Final row index (exclusive)c0
- Initial column indexc1
- Final column index (exclusive)- Returns:
- m1(r0:r1-1,c0:c1-1)
-
getMatrix
public static double[][] getMatrix(double[][] m1, int[] r, int[] c)
Get a submatrix.- Parameters:
m1
- Input matrixr
- Array of row indices.c
- Array of column indices.- Returns:
- m1(r(:),c(:))
-
getMatrix
public static double[][] getMatrix(double[][] m1, int[] r, int c0, int c1)
Get a submatrix.- Parameters:
m1
- Input matrixr
- Array of row indices.c0
- Initial column indexc1
- Final column index (exclusive)- Returns:
- m1(r(:),c0:c1-1)
-
getMatrix
public static double[][] getMatrix(double[][] m1, int r0, int r1, int[] c)
Get a submatrix.- Parameters:
m1
- Input matrixr0
- Initial row indexr1
- Final row index (exclusive)c
- Array of column indices.- Returns:
- m1(r0:r1-1,c(:))
-
setMatrix
public static void setMatrix(double[][] m1, int r0, int r1, int c0, int c1, double[][] m2)
Set a submatrix.- Parameters:
m1
- Original matrixr0
- Initial row indexr1
- Final row index (exclusive)c0
- Initial column indexc1
- Final column index (exclusive)m2
- New values for m1(r0:r1-1,c0:c1-1)
-
setMatrix
public static void setMatrix(double[][] m1, int[] r, int[] c, double[][] m2)
Set a submatrix.- Parameters:
m1
- Original matrixr
- Array of row indices.c
- Array of column indices.m2
- New values for m1(r(:),c(:))
-
setMatrix
public static void setMatrix(double[][] m1, int[] r, int c0, int c1, double[][] m2)
Set a submatrix.- Parameters:
m1
- Input matrixr
- Array of row indices.c0
- Initial column indexc1
- Final column index (exclusive)m2
- New values for m1(r(:),c0:c1-1)
-
setMatrix
public static void setMatrix(double[][] m1, int r0, int r1, int[] c, double[][] m2)
Set a submatrix.- Parameters:
m1
- Input matrixr0
- Initial row indexr1
- Final row indexc
- Array of column indices.m2
- New values for m1(r0:r1-1,c(:))
-
getRow
public static double[] getRow(double[][] m1, int r)
Returns ther
th row of this matrix as vector.- Parameters:
m1
- Input matrixr
- the index of the row to be returned- Returns:
- the
r
th row of this matrix
-
setRow
public static void setRow(double[][] m1, int r, double[] row)
Sets ther
th row of this matrix to the specified vector.- Parameters:
m1
- Original matrixr
- the index of the column to be setrow
- the value of the column to be set
-
getCol
public static double[] getCol(double[][] m1, int col)
Get a column from a matrix as vector.- Parameters:
m1
- Matrix to extract the column fromcol
- Column number- Returns:
- Column
-
setCol
public static void setCol(double[][] m1, int c, double[] column)
Sets thec
th column of this matrix to the specified column.- Parameters:
m1
- Input matrixc
- the index of the column to be setcolumn
- the value of the column to be set
-
transpose
public static double[][] transpose(double[][] m1)
Matrix transpose- Parameters:
m1
- Input matrix- Returns:
- m1T as copy
-
plus
public static double[][] plus(double[][] m1, double[][] m2)
Component-wise matrix sum: m3 = m1 + m2.- Parameters:
m1
- Input matrixm2
- another matrix- Returns:
- m1 + m1 in a new Matrix
-
plusTimes
public static double[][] plusTimes(double[][] m1, double[][] m2, double s2)
Component-wise matrix operation: m3 = m1 + m2 * s2.- Parameters:
m1
- Input matrixm2
- another matrixs2
- scalar- Returns:
- m1 + m2 * s2 in a new matrix
-
plusEquals
public static double[][] plusEquals(double[][] m1, double[][] m2)
Component-wise matrix operation: m1 = m1 + m2, overwriting the existing matrix m1.- Parameters:
m1
- input matrix (overwritten)m2
- another matrix- Returns:
- m1 = m1 + m2
-
plusTimesEquals
public static double[][] plusTimesEquals(double[][] m1, double[][] m2, double s2)
Component-wise matrix operation: m1 = m1 + m2 * s2, overwriting the existing matrix m1.- Parameters:
m1
- input matrix (overwritten)m2
- another matrixs2
- scalar for s2- Returns:
- m1 = m1 + m2 * s2, overwriting m1
-
minus
public static double[][] minus(double[][] m1, double[][] m2)
Component-wise matrix subtraction m3 = m1 - m2.- Parameters:
m1
- Input matrixm2
- another matrix- Returns:
- m1 - m2 in a new matrix
-
minusTimes
public static double[][] minusTimes(double[][] m1, double[][] m2, double s2)
Component-wise matrix operation: m3 = m1 - m2 * s2- Parameters:
m1
- Input matrixm2
- another matrixs2
- Scalar- Returns:
- m1 - m2 * s2 in a new Matrix
-
minusEquals
public static double[][] minusEquals(double[][] m1, double[][] m2)
Component-wise matrix subtraction: m1 = m1 - m2, overwriting the existing matrix m1.- Parameters:
m1
- Input matrixm2
- another matrix- Returns:
- m1 - m2, overwriting m1
-
minusTimesEquals
public static double[][] minusTimesEquals(double[][] m1, double[][] m2, double s2)
Component-wise matrix operation: m1 = m1 - m2 * s2, overwriting the existing matrix m1.- Parameters:
m1
- Input matrixm2
- another matrixs2
- Scalar- Returns:
- m1 = m1 - s2 * m2, overwriting m1
-
times
public static double[][] times(double[][] m1, double s1)
Multiply a matrix by a scalar component-wise, m3 = m1 * s1.- Parameters:
m1
- Input matrixs1
- scalar- Returns:
- m1 * s1, in a new matrix
-
timesEquals
public static double[][] timesEquals(double[][] m1, double s1)
Multiply a matrix by a scalar component-wise in place, m1 = m1 * s1, overwriting the existing matrix m1.- Parameters:
m1
- Input matrixs1
- scalar- Returns:
- m1 = m1 * s1, overwriting m1
-
times
public static double[][] times(double[][] m1, double[][] m2)
Matrix multiplication, m1 * m2.- Parameters:
m1
- Input matrixm2
- another matrix- Returns:
- Matrix product, m1 * m2
-
times
public static double[] times(double[][] m1, double[] v2)
Matrix with vector multiplication, m1 * v2.- Parameters:
m1
- Input matrixv2
- a vector- Returns:
- Matrix product, m1 * v2
-
transposeTimes
public static double[] transposeTimes(double[][] m1, double[] v2)
Transposed matrix with vector multiplication, m1T * v2- Parameters:
m1
- Input matrixv2
- another matrix- Returns:
- Matrix product, m1T * v2
-
transposeTimes
public static double[][] transposeTimes(double[][] m1, double[][] m2)
Matrix multiplication, m1T * m2- Parameters:
m1
- Input matrixm2
- another matrix- Returns:
- Matrix product, m1T * m2
-
transposeTimesTimes
public static double transposeTimesTimes(double[] v1, double[][] m2, double[] v3)
Matrix multiplication, v1T * m2 * v3- Parameters:
v1
- vector on the leftm2
- matrixv3
- vector on the right- Returns:
- Matrix product, v1T * m2 * v3
-
timesTranspose
public static double[][] timesTranspose(double[][] m1, double[][] m2)
Matrix multiplication, m1 * m2T- Parameters:
m1
- Input matrixm2
- another matrix- Returns:
- Matrix product, m1 * m2T
-
transposeTimesTranspose
public static double[][] transposeTimesTranspose(double[][] m1, double[][] m2)
Matrix multiplication, m1T * m2T. Computed as (m2*m1)T- Parameters:
m1
- Input matrixm2
- another matrix- Returns:
- Matrix product, m1T * m2T
-
transposeDiagonalTimes
public static double[][] transposeDiagonalTimes(double[][] m1, double[] d2, double[][] m3)
Matrix multiplication with diagonal, m1^T * d2 * m3- Parameters:
m1
- Left matrixd2
- Diagonal entriesm3
- Right matrix- Returns:
- m1^T * d2 * m3
-
mahalanobisDistance
@Reference(authors="P. C. Mahalanobis", title="On the generalized distance in statistics", booktitle="Proceedings of the National Institute of Sciences of India. 2 (1)", bibkey="journals/misc/Mahalanobis36") public static double mahalanobisDistance(double[][] B, double[] a, double[] c)
Matrix multiplication, (a-c)T * B * (a-c)Note: it may (or may not) be more efficient to materialize (a-c), then use
transposeTimesTimes(a_minus_c, B, a_minus_c)
instead.- Parameters:
B
- matrixa
- First vectorc
- Center vector- Returns:
- Matrix product, (a-c)T * B * (a-c)
-
getDiagonal
public static double[] getDiagonal(double[][] m1)
getDiagonal returns array of diagonal-elements.- Parameters:
m1
- Input matrix- Returns:
- values on the diagonal of the Matrix
-
normalizeColumns
public static void normalizeColumns(double[][] m1)
Normalizes the columns of this matrix to length of 1.0. Note: if a column has length 0, it will remain unmodified.- Parameters:
m1
- Input matrix
-
appendColumns
public static double[][] appendColumns(double[][] m1, double[][] m2)
Returns a matrix which consists of this matrix and the specified columns.- Parameters:
m1
- Input matrixm2
- the columns to be appended- Returns:
- the new matrix with the appended columns
-
orthonormalize
public static double[][] orthonormalize(double[][] m1)
Returns an orthonormalization of this matrix.- Parameters:
m1
- Input matrix- Returns:
- the orthonormalized matrix
-
solve
public static double[][] solve(double[][] A, double[][] B)
Solve A*X = B- Parameters:
B
- right hand side- Returns:
- solution if A is square, least squares solution otherwise
-
solve
public static double[] solve(double[][] A, double[] b)
Solve A*X = b- Parameters:
b
- right hand side- Returns:
- solution if A is square, least squares solution otherwise
-
inverse
public static double[][] inverse(double[][] A)
Matrix inverse or pseudoinverse- Parameters:
A
- matrix to invert- Returns:
- inverse(A) if A is square, pseudoinverse otherwise.
-
normF
public static double normF(double[][] elements)
Frobenius norm- Parameters:
elements
- Matrix- Returns:
- sqrt of sum of squares of all elements.
-
hashCode
public static int hashCode(double[][] m1)
Compute hash code- Parameters:
m1
- Input matrix- Returns:
- Hash code
-
equals
public static boolean equals(double[][] m1, double[][] m2)
Test for equality- Parameters:
m1
- Input matrixm2
- Other matrix- Returns:
- Equality
-
almostEquals
public static boolean almostEquals(double[][] m1, double[][] m2, double maxdelta)
Compare two matrices with a delta parameter to take numerical errors into account.- Parameters:
m1
- Input matrixm2
- other matrix to compare withmaxdelta
- maximum delta allowed- Returns:
- true if delta smaller than maximum
-
almostEquals
public static boolean almostEquals(double[][] m1, double[][] m2)
Compare two matrices with a delta parameter to take numerical errors into account.- Parameters:
m1
- Input matrixm2
- other matrix to compare with- Returns:
- almost equals with delta
DELTA
-
almostEquals
public static boolean almostEquals(double[] m1, double[] m2, double maxdelta)
Compare two matrices with a delta parameter to take numerical errors into account.- Parameters:
m1
- Input matrixm2
- other matrix to compare withmaxdelta
- maximum delta allowed- Returns:
- true if delta smaller than maximum
-
almostEquals
public static boolean almostEquals(double[] m1, double[] m2)
Compare two matrices with a delta parameter to take numerical errors into account.- Parameters:
m1
- Input matrixm2
- other matrix to compare with- Returns:
- almost equals with delta
DELTA
-
getRowDimensionality
public static int getRowDimensionality(double[][] m1)
Returns the dimensionality of the rows of this matrix.- Parameters:
m1
- Input matrix- Returns:
- the number of rows.
-
getColumnDimensionality
public static int getColumnDimensionality(double[][] m1)
Returns the dimensionality of the columns of this matrix.- Parameters:
m1
- Input matrix- Returns:
- the number of columns.
-
angle
public static double angle(double[] v1, double[] v2)
Compute the cosine of the angle between two vectors, where the smaller angle between those vectors is viewed.- Parameters:
v1
- first vectorv2
- second vector- Returns:
- cosine of the smaller angle
-
angle
public static double angle(double[] v1, double[] v2, double[] o)
Compute the cosine of the angle between two vectors, where the smaller angle between those vectors is viewed.- Parameters:
v1
- first vectorv2
- second vectoro
- Origin- Returns:
- cosine of the smaller angle
-
-