Class MatrixUtils


  • public class MatrixUtils
    extends Object
    A set of DoubleMatrix2D shorthands and utility methods.
    • Constructor Summary

      Constructors 
      Constructor Description
      MatrixUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double computeOrthogonality​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)
      Computes the orthogonality of matrix A.
      static double computeSparseness​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A)
      Computers sparseness of matrix A as a fraction of non-zero elements to the total number of elements.
      static double frobeniusNorm​(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 int maxInRow​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, int row)
      Finds the index of the first maximum element in given row of A.
      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.DoubleMatrix2D normalizeColumnL1​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)
      Normalizes column vectors of matrix A so that their L1 norm is equal to 1.0.
      static org.carrot2.math.mahout.matrix.DoubleMatrix2D normalizeColumnL2​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)
      Normalizes column vectors of matrix A so that their L2 norm (Euclidean distance) is equal to 1.0.
      static org.carrot2.math.mahout.matrix.DoubleMatrix2D normalizeSparseColumnL2​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] work)
      Normalizes column vectors of a sparse matrix A so that their L2 norm (Euclidean distance) is equal to 1.0.
      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.
      static double[] sumRows​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A, double[] sums)
      Calculates the sum of rows of matrix A.
    • Constructor Detail

      • MatrixUtils

        public MatrixUtils()
    • Method Detail

      • normalizeColumnL2

        public static org.carrot2.math.mahout.matrix.DoubleMatrix2D normalizeColumnL2​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A,
                                                                                      double[] work)
        Normalizes column vectors of matrix A so that their L2 norm (Euclidean distance) is equal to 1.0.
        Parameters:
        A - matrix to normalize
        work - a temporary array of A.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 is null, 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 matrix A so that their L2 norm (Euclidean distance) is equal to 1.0.
        Parameters:
        A - matrix to normalize
        work - a temporary array of A.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 is null, 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 matrix A so that their L1 norm is equal to 1.0.
        Parameters:
        A - matrix to normalize
        work - a temporary array of A.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 is null, 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 matrix A as 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 of A.columns() integers in which indices of the first minimum element will be stored. If this parameter is null a new array will be allocated.
        minValues - an array of A.columns() doubles in which values of each column's minimum elements will be stored. If this parameter is null a 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 of A.columns() integers in which indices of the first maximum element will be stored. If this parameter is null a new array will be allocated.
        maxValues - an array of A.columns() doubles in which values of each column's maximum elements will be stored. If this parameter is null a 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 of A.
        Parameters:
        A - the matrix to search
        row - the row to search
        Returns:
        index of the first maximum element or -1 if the input matrix is null or has zero size.
      • sumRows

        public static double[] sumRows​(org.carrot2.math.mahout.matrix.DoubleMatrix2D A,
                                       double[] sums)
        Calculates the sum of rows of matrix A.
        Parameters:
        sums - an array to store the results. If the array is null or does not match the number of rows in matrix A, 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 permute
        comparator - to use
        Returns:
        view of the provided matrix with rows permuted according to the order defined by the provided comparator.