Skip navigation links de.lmu.ifi.dbs.elki.math.linearalgebra

## Class VMath

• java.lang.Object
• de.lmu.ifi.dbs.elki.math.linearalgebra.VMath

• @Title(value="Vector and Matrix Math Library")
public final class VMath
extends java.lang.Object
Class providing basic vector mathematics, for low-level vectors stored as double[]. 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 and 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 and Description
private  VMath()
Fake constructor.
• ### Method Summary

All Methods
Modifier and Type Method and 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 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 equality
static 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 the rth 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 code
static double[][] identity(int m, int n)
Generate unit / identity / "eye" matrix.
static double[][] inverse(double[][] A)
Matrix inverse or pseudoinverse
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.
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 * s2
static 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 norm
static 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 the cth 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 the rth row of this matrix to the specified vector.
static double[] solve(double[][] A, double[] b)
Solve A*X = b
static double[][] solve(double[][] A, double[][] B)
Solve A*X = B
static 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 code
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.
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 * m2T
static 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 code
static double[][] transpose(double[] v)
Transpose vector to a matrix without copying.
static double[][] transpose(double[][] m1)
Matrix transpose
static double[][] transposeDiagonalTimes(double[][] m1, double[] d2, double[][] m3)
Matrix multiplication with diagonal, m1^T * d2 * m3
static double[] transposeTimes(double[][] m1, double[] v2)
Transposed matrix with vector multiplication, m1T * v2
static double[][] transposeTimes(double[][] m1, double[][] m2)
Matrix multiplication, m1T * m2
static 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 * v3
static 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.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### 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
• ### Constructor Detail

• #### VMath

private VMath()
Fake constructor. Static class.
• ### 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 vector
i - 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 vector
v2 - 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 vector
v2 - second vector
s2 - 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 vector
s1 - the scalar for v1
v2 - 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 vector
s1 - the scalar for v1
v2 - second vector
s2 - 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 vector
s2 - 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 v1
v2 - 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 v1
v2 - another vector
s2 - 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 to
s1 - 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 vector
v2 - 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 vector
v2 - the vector to be subtracted from this vector
s2 - 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 vector
s1 - the scaling factor for v1
v2 - 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 vector
s1 - the scaling factor for v1
v2 - the vector to be subtracted from this vector
s2 - 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 - vector
v2 - 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 - vector
v2 - another vector
s2 - 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 - vector
s1 - scalar for v1
v2 - 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 - vector
s1 - scalar for v1
v2 - another vector
s2 - Scalar
Returns:
v1 = v1 * s1 - v2 * s2
• #### minus

public static double[] minus(double[] v1,
double s1)
Subtract component-wise v1 - s1.
Parameters:
v1 - original vector
s1 - 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 vector
s1 - Value to subtract
Returns:
v1 = v1 - s1
• #### times

public static double[] times(double[] v1,
double s1)
Multiply component-wise v1 * s1.
Parameters:
v1 - original vector
s1 - 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 vector
s - 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 vector
v2 - input vector
s - 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 code
Matrix 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 - vector
m2 - 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 - vector
m2 - 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 - vector
v2 - 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 code
Vector 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 - vector
m2 - other matrix
Returns:
Matrix product, v1 * m2T
• #### timesTranspose

public static double[][] timesTranspose(double[] v1,
double[] v2)
Vectors to matrix multiplication, v1 * v2T.
Parameters:
v1 - vector
v2 - 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 - vector
v2 - 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 - vector
v2 - 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
• #### 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 vector
v2 - 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 matrix
r0 - Initial row index
r1 - Final row index (exclusive)
c0 - Initial column index
c1 - 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 matrix
r - 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 matrix
r - Array of row indices.
c0 - Initial column index
c1 - 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 matrix
r0 - Initial row index
r1 - 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 matrix
r0 - Initial row index
r1 - Final row index (exclusive)
c0 - Initial column index
c1 - 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 matrix
r - 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 matrix
r - Array of row indices.
c0 - Initial column index
c1 - 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 matrix
r0 - Initial row index
r1 - Final row index
c - Array of column indices.
m2 - New values for m1(r0:r1-1,c(:))
• #### getRow

public static double[] getRow(double[][] m1,
int r)
Returns the rth row of this matrix as vector.
Parameters:
m1 - Input matrix
r - the index of the row to be returned
Returns:
the rth row of this matrix
• #### setRow

public static void setRow(double[][] m1,
int r,
double[] row)
Sets the rth row of this matrix to the specified vector.
Parameters:
m1 - Original matrix
r - the index of the column to be set
row - 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 from
col - Column number
Returns:
Column
• #### setCol

public static void setCol(double[][] m1,
int c,
double[] column)
Sets the cth column of this matrix to the specified column.
Parameters:
m1 - Input matrix
c - the index of the column to be set
column - 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 matrix
m2 - 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 matrix
m2 - another matrix
s2 - 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 matrix
s2 - 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 matrix
m2 - 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 matrix
m2 - another matrix
s2 - 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 matrix
m2 - 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 matrix
m2 - another matrix
s2 - 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 matrix
s1 - 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 matrix
s1 - scalar
Returns:
m1 = m1 * s1, overwriting m1
• #### times

public static double[][] times(double[][] m1,
double[][] m2)
Matrix multiplication, m1 * m2.
Parameters:
m1 - Input matrix
m2 - 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 matrix
v2 - 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 matrix
v2 - another matrix
Returns:
Matrix product, m1T * v2
• #### transposeTimes

public static double[][] transposeTimes(double[][] m1,
double[][] m2)
Matrix multiplication, m1T * m2
Parameters:
m1 - Input matrix
m2 - 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 left
m2 - matrix
v3 - 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 matrix
m2 - 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 matrix
m2 - 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 matrix
d2 - Diagonal entries
m3 - 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 - matrix
a - First vector
c - 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 matrix
m2 - 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 matrix
m2 - 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 matrix
m2 - other matrix to compare with
maxdelta - 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 matrix
m2 - 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 matrix
m2 - other matrix to compare with
maxdelta - 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 matrix
m2 - 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 vector
v2 - 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 vector
v2 - second vector
o - Origin
Returns:
cosine of the smaller angle
Skip navigation links
ELKI version 0.7.5

Copyright © 2019 ELKI Development Team. License information.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5280671,"math_prob":0.9802886,"size":25077,"snap":"2021-21-2021-25","text_gpt3_token_len":6922,"char_repetition_ratio":0.33665696,"word_repetition_ratio":0.44408163,"special_character_ratio":0.29967698,"punctuation_ratio":0.122802615,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99802816,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-19T06:37:05Z\",\"WARC-Record-ID\":\"<urn:uuid:339dfe2e-4acb-4634-97fa-afb820b778c4>\",\"Content-Length\":\"128296\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9cafa614-0c79-45ef-8b65-e840a76a4265>\",\"WARC-Concurrent-To\":\"<urn:uuid:ca31bdd1-d39f-4b80-8f01-096e64f81c83>\",\"WARC-IP-Address\":\"185.199.109.153\",\"WARC-Target-URI\":\"https://elki-project.github.io/releases/current/doc/de/lmu/ifi/dbs/elki/math/linearalgebra/VMath.html\",\"WARC-Payload-Digest\":\"sha1:KRWEX55NKLPRHNVPE6T4S2SFQ4SSYIVQ\",\"WARC-Block-Digest\":\"sha1:AZJFRP4IWIUI2AZ5NIPBUSMESKINTX4M\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487643703.56_warc_CC-MAIN-20210619051239-20210619081239-00613.warc.gz\"}"}