Package org.carrot2.math.matrix
Class MatrixUtils
java.lang.Object
org.carrot2.math.matrix.MatrixUtils
public class MatrixUtils extends Object
A set of
DoubleMatrix2D shorthands and utility methods.-
Constructor Summary
Constructors Constructor Description MatrixUtils() -
Method Summary
Modifier and Type Method Description static doublecomputeOrthogonality(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)Computes the orthogonality of matrix A.static doublecomputeSparseness(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)Computers sparseness of matrixAas a fraction of non-zero elements to the total number of elements.static doublefrobeniusNorm(org.carrot2.math.mahout.matrix.DoubleMatrix2D matrix)Calculates the Frobenius norm of a matrix.static int[]maxInColumns(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int[] indices, double[] maxValues)Finds the first maximum element in each column of matrix A.static int[]maxInColumns(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int[] indices, double[] maxValues, org.carrot2.math.mahout.function.DoubleFunction transform)static intmaxInRow(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int row)Finds the index of the first maximum element in given row ofA.static int[]minInColumns(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int[] indices, double[] minValues)Finds the first minimum element in each column of matrix A.static org.carrot2.math.mahout.matrix.DoubleMatrix2DnormalizeColumnL1(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)Normalizes column vectors of matrixAso that their L1 norm is equal to 1.0.static org.carrot2.math.mahout.matrix.DoubleMatrix2DnormalizeColumnL2(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)Normalizes column vectors of matrixAso that their L2 norm (Euclidean distance) is equal to 1.0.static org.carrot2.math.mahout.matrix.DoubleMatrix2DnormalizeSparseColumnL2(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)Normalizes column vectors of a sparse matrixAso that their L2 norm (Euclidean distance) is equal to 1.0.static org.carrot2.math.mahout.matrix.DoubleMatrix2DsortedRowsView(org.carrot2.math.mahout.matrix.DoubleMatrix2D matrix, com.carrotsearch.hppc.sorting.IndirectComparator comparator)Returns view of the provided matrix with rows permuted according to the order defined by the provided comparator.static double[]sumRows(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] sums)Calculates the sum of rows of matrixA.
-
Constructor Details
-
MatrixUtils
public MatrixUtils()
-
-
Method Details
-
normalizeColumnL2
public static org.carrot2.math.mahout.matrix.DoubleMatrix2D normalizeColumnL2(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)Normalizes column vectors of matrixAso that their L2 norm (Euclidean distance) is equal to 1.0.- Parameters:
A- matrix to normalizework- a temporary array ofA.columns()doubles that will be overwritten with column's original L2 norms. Supply a non-null pointer to avoid continuous allocation/freeing of memory when doing calculations in a loop. If this parameter isnull, a new array will be allocated every time this method is called.- Returns:
- A with length-normalized columns (for convenience only)
-
normalizeSparseColumnL2
public static org.carrot2.math.mahout.matrix.DoubleMatrix2D normalizeSparseColumnL2(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)Normalizes column vectors of a sparse matrixAso that their L2 norm (Euclidean distance) is equal to 1.0.- Parameters:
A- matrix to normalizework- a temporary array ofA.columns()doubles that will be overwritten with column's original L2 norms. Supply a non-null pointer to avoid continuous allocation/freeing of memory when doing calculations in a loop. If this parameter isnull, a new array will be allocated every time this method is called.- Returns:
- A with length-normalized columns (for convenience only)
-
normalizeColumnL1
public static org.carrot2.math.mahout.matrix.DoubleMatrix2D normalizeColumnL1(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)Normalizes column vectors of matrixAso that their L1 norm is equal to 1.0.- Parameters:
A- matrix to normalizework- a temporary array ofA.columns()doubles that will be overwritten with column's original L1 norms. Supply a non-null pointer to avoid continuous allocation/freeing of memory when doing calculations in a loop. If this parameter isnull, a new array will be allocated every time this method is called.- Returns:
- A with L1-normalized columns (for convenience only)
-
computeOrthogonality
public static double computeOrthogonality(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)Computes the orthogonality of matrix A. The orthogonality is computed as a sum of k*(k-1)/2 inner products of A's column vectors, k being the number of columns of A, and then normalized to the 0.0 - 1.0 range.- Parameters:
A- matrix to compute orthogonality for, must be column length-normalized- Returns:
- orthogonality of matrix A. 0.0 denotes a perfect orthogonality between every pair of A's column. 1.0 indicates that all columns of A are parallel.
-
computeSparseness
public static double computeSparseness(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)Computers sparseness of matrixAas a fraction of non-zero elements to the total number of elements.- Returns:
- sparseness of
A, which is a value between 0.0 (all elements are zero) and 1.0 (all elements are non-zero)
-
minInColumns
public static int[] minInColumns(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int[] indices, double[] minValues)Finds the first minimum element in each column of matrix A. When calculating minimum values for each column this version should perform better than scanning each column separately.- Parameters:
indices- an array ofA.columns()integers in which indices of the first minimum element will be stored. If this parameter isnulla new array will be allocated.minValues- an array ofA.columns()doubles in which values of each column's minimum elements will be stored. If this parameter isnulla new array will be allocated.- Returns:
- for each column of A the index of the minimum element
-
maxInColumns
public static int[] maxInColumns(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int[] indices, double[] maxValues)Finds the first maximum element in each column of matrix A. When calculating maximum values for each column this version should perform better than scanning each column separately.- Parameters:
indices- an array ofA.columns()integers in which indices of the first maximum element will be stored. If this parameter isnulla new array will be allocated.maxValues- an array ofA.columns()doubles in which values of each column's maximum elements will be stored. If this parameter isnulla new array will be allocated.- Returns:
- for each column of A the index of the maximum element
-
maxInColumns
public static int[] maxInColumns(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int[] indices, double[] maxValues, org.carrot2.math.mahout.function.DoubleFunction transform) -
maxInRow
public static int maxInRow(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int row)Finds the index of the first maximum element in given row ofA.- Parameters:
A- the matrix to searchrow- the row to search- Returns:
- index of the first maximum element or -1 if the input matrix is
nullor has zero size.
-
sumRows
public static double[] sumRows(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] sums)Calculates the sum of rows of matrixA.- Parameters:
sums- an array to store the results. If the array isnullor does not match the number of rows in matrixA, a new array will be created.- Returns:
- sums of rows of
A
-
frobeniusNorm
public static double frobeniusNorm(org.carrot2.math.mahout.matrix.DoubleMatrix2D matrix)Calculates the Frobenius norm of a matrix.- See Also:
- Frobenius norm
-
sortedRowsView
public static org.carrot2.math.mahout.matrix.DoubleMatrix2D sortedRowsView(org.carrot2.math.mahout.matrix.DoubleMatrix2D matrix, com.carrotsearch.hppc.sorting.IndirectComparator comparator)Returns view of the provided matrix with rows permuted according to the order defined by the provided comparator.- Parameters:
matrix- to permutecomparator- to use- Returns:
- view of the provided matrix with rows permuted according to the order defined by the provided comparator.
-