org.carrot2.matrix
Class MatrixUtils

java.lang.Object
  extended by org.carrot2.matrix.MatrixUtils

public class MatrixUtils
extends Object

A set of DoubleMatrix2D shorthands and utility methods.


Constructor Summary
MatrixUtils()
           
 
Method Summary
static double computeOrthogonality(org.apache.mahout.math.matrix.DoubleMatrix2D A)
          Computes the orthogonality of matrix A.
static double computeSparseness(org.apache.mahout.math.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.apache.mahout.math.matrix.DoubleMatrix2D matrix)
          Calculates the Frobenius norm of a matrix.
static int[] maxInColumns(org.apache.mahout.math.matrix.DoubleMatrix2D A, int[] indices, double[] maxValues)
          Finds the first maximum element in each column of matrix A.
static int[] maxInColumns(org.apache.mahout.math.matrix.DoubleMatrix2D A, int[] indices, double[] maxValues, org.apache.mahout.math.function.DoubleFunction transform)
           
static int maxInRow(org.apache.mahout.math.matrix.DoubleMatrix2D A, int row)
          Finds the index of the first maximum element in given row of A.
static int[] minInColumns(org.apache.mahout.math.matrix.DoubleMatrix2D A, int[] indices, double[] minValues)
          Finds the first minimum element in each column of matrix A.
static org.apache.mahout.math.matrix.DoubleMatrix2D normalizeColumnL1(org.apache.mahout.math.matrix.DoubleMatrix2D A, double[] work)
          Normalizes column vectors of matrix A so that their L1 norm is equal to 1.0.
static org.apache.mahout.math.matrix.DoubleMatrix2D normalizeColumnL2(org.apache.mahout.math.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.apache.mahout.math.matrix.DoubleMatrix2D normalizeSparseColumnL2(org.apache.mahout.math.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.apache.mahout.math.matrix.DoubleMatrix2D sortedRowsView(org.apache.mahout.math.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.apache.mahout.math.matrix.DoubleMatrix2D A, double[] sums)
          Calculates the sum of rows of matrix A.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MatrixUtils

public MatrixUtils()
Method Detail

normalizeColumnL2

public static org.apache.mahout.math.matrix.DoubleMatrix2D normalizeColumnL2(org.apache.mahout.math.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.apache.mahout.math.matrix.DoubleMatrix2D normalizeSparseColumnL2(org.apache.mahout.math.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.apache.mahout.math.matrix.DoubleMatrix2D normalizeColumnL1(org.apache.mahout.math.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.apache.mahout.math.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.apache.mahout.math.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.apache.mahout.math.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.apache.mahout.math.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 scannning each column separately.

Parameters:
A -
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.apache.mahout.math.matrix.DoubleMatrix2D A,
                                 int[] indices,
                                 double[] maxValues,
                                 org.apache.mahout.math.function.DoubleFunction transform)

maxInRow

public static int maxInRow(org.apache.mahout.math.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.apache.mahout.math.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.apache.mahout.math.matrix.DoubleMatrix2D matrix)
Calculates the Frobenius norm of a matrix.

See Also:
Frobenius norm

sortedRowsView

public static org.apache.mahout.math.matrix.DoubleMatrix2D sortedRowsView(org.apache.mahout.math.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.


Copyright (c) Dawid Weiss, Stanislaw Osinski