org.apache.spark.mllib.linalg
Class SparseMatrix

Object
  extended by org.apache.spark.mllib.linalg.SparseMatrix
All Implemented Interfaces:
java.io.Serializable, Matrix

public class SparseMatrix
extends Object
implements Matrix

Column-major sparse matrix. The entry values are stored in Compressed Sparse Column (CSC) format. For example, the following matrix


   1.0 0.0 4.0
   0.0 3.0 5.0
   2.0 0.0 6.0
 
is stored as values: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0], rowIndices=[0, 2, 1, 0, 1, 2], colPointers=[0, 2, 3, 6].

param: numRows number of rows param: numCols number of columns param: colPtrs the index corresponding to the start of a new column (if not transposed) param: rowIndices the row index of the entry (if not transposed). They must be in strictly increasing order for each column param: values nonzero matrix entries in column major (if not transposed) param: isTransposed whether the matrix is transposed. If true, the matrix can be considered Compressed Sparse Row (CSR) format, where colPtrs behaves as rowPtrs, and rowIndices behave as colIndices, and values are stored in row major.

See Also:
Serialized Form

Constructor Summary
SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values)
          Column-major sparse matrix.
SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values, boolean isTransposed)
           
 
Method Summary
 double apply(int i, int j)
          Gets the (i, j)-th element.
 int[] colPtrs()
           
 SparseMatrix copy()
          Get a deep copy of the matrix.
static SparseMatrix fromCOO(int numRows, int numCols, scala.collection.Iterable<scala.Tuple3<Object,Object,Object>> entries)
          Generate a SparseMatrix from Coordinate List (COO) format.
 boolean isTransposed()
          Flag that keeps track whether the matrix is transposed or not.
 int numCols()
          Number of columns.
 int numRows()
          Number of rows.
 int[] rowIndices()
           
static SparseMatrix spdiag(Vector vector)
          Generate a diagonal matrix in SparseMatrix format from the supplied values.
static SparseMatrix speye(int n)
          Generate an Identity Matrix in SparseMatrix format.
static SparseMatrix sprand(int numRows, int numCols, double density, java.util.Random rng)
          Generate a SparseMatrix consisting of i.i.d.
static SparseMatrix sprandn(int numRows, int numCols, double density, java.util.Random rng)
          Generate a SparseMatrix consisting of i.i.d.
 DenseMatrix toDense()
          Generate a DenseMatrix from the given SparseMatrix.
 SparseMatrix transpose()
          Transpose the Matrix.
 double[] values()
           
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.spark.mllib.linalg.Matrix
multiply, multiply, multiply, toArray, toString, toString
 

Constructor Detail

SparseMatrix

public SparseMatrix(int numRows,
                    int numCols,
                    int[] colPtrs,
                    int[] rowIndices,
                    double[] values,
                    boolean isTransposed)

SparseMatrix

public SparseMatrix(int numRows,
                    int numCols,
                    int[] colPtrs,
                    int[] rowIndices,
                    double[] values)
Column-major sparse matrix. The entry values are stored in Compressed Sparse Column (CSC) format. For example, the following matrix

   1.0 0.0 4.0
   0.0 3.0 5.0
   2.0 0.0 6.0
 
is stored as values: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0], rowIndices=[0, 2, 1, 0, 1, 2], colPointers=[0, 2, 3, 6].

Parameters:
numRows - number of rows
numCols - number of columns
colPtrs - the index corresponding to the start of a new column
rowIndices - the row index of the entry. They must be in strictly increasing order for each column
values - non-zero matrix entries in column major
Method Detail

fromCOO

public static SparseMatrix fromCOO(int numRows,
                                   int numCols,
                                   scala.collection.Iterable<scala.Tuple3<Object,Object,Object>> entries)
Generate a SparseMatrix from Coordinate List (COO) format. Input must be an array of (i, j, value) tuples. Entries that have duplicate values of i and j are added together. Tuples where value is equal to zero will be omitted.

Parameters:
numRows - number of rows of the matrix
numCols - number of columns of the matrix
entries - Array of (i, j, value) tuples
Returns:
The corresponding SparseMatrix

speye

public static SparseMatrix speye(int n)
Generate an Identity Matrix in SparseMatrix format.

Parameters:
n - number of rows and columns of the matrix
Returns:
SparseMatrix with size n x n and values of ones on the diagonal

sprand

public static SparseMatrix sprand(int numRows,
                                  int numCols,
                                  double density,
                                  java.util.Random rng)
Generate a SparseMatrix consisting of i.i.d. uniform random numbers. The number of non-zero elements equal the ceiling of numRows x numCols x density

Parameters:
numRows - number of rows of the matrix
numCols - number of columns of the matrix
density - the desired density for the matrix
rng - a random number generator
Returns:
SparseMatrix with size numRows x numCols and values in U(0, 1)

sprandn

public static SparseMatrix sprandn(int numRows,
                                   int numCols,
                                   double density,
                                   java.util.Random rng)
Generate a SparseMatrix consisting of i.i.d. gaussian random numbers.

Parameters:
numRows - number of rows of the matrix
numCols - number of columns of the matrix
density - the desired density for the matrix
rng - a random number generator
Returns:
SparseMatrix with size numRows x numCols and values in N(0, 1)

spdiag

public static SparseMatrix spdiag(Vector vector)
Generate a diagonal matrix in SparseMatrix format from the supplied values.

Parameters:
vector - a Vector that will form the values on the diagonal of the matrix
Returns:
Square SparseMatrix with size values.length x values.length and non-zero values on the diagonal

numRows

public int numRows()
Description copied from interface: Matrix
Number of rows.

Specified by:
numRows in interface Matrix

numCols

public int numCols()
Description copied from interface: Matrix
Number of columns.

Specified by:
numCols in interface Matrix

colPtrs

public int[] colPtrs()

rowIndices

public int[] rowIndices()

values

public double[] values()

isTransposed

public boolean isTransposed()
Description copied from interface: Matrix
Flag that keeps track whether the matrix is transposed or not. False by default.

Specified by:
isTransposed in interface Matrix

apply

public double apply(int i,
                    int j)
Description copied from interface: Matrix
Gets the (i, j)-th element.

Specified by:
apply in interface Matrix

copy

public SparseMatrix copy()
Description copied from interface: Matrix
Get a deep copy of the matrix.

Specified by:
copy in interface Matrix

transpose

public SparseMatrix transpose()
Description copied from interface: Matrix
Transpose the Matrix. Returns a new `Matrix` instance sharing the same underlying data.

Specified by:
transpose in interface Matrix

toDense

public DenseMatrix toDense()
Generate a DenseMatrix from the given SparseMatrix. The new matrix will have isTransposed set to false.

Returns:
(undocumented)