org.apache.spark.mllib.linalg
Class DenseMatrix

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

public class DenseMatrix
extends Object
implements Matrix

Column-major dense matrix. The entry values are stored in a single array of doubles with columns listed in sequence. For example, the following matrix


   1.0 2.0
   3.0 4.0
   5.0 6.0
 
is stored as [1.0, 3.0, 5.0, 2.0, 4.0, 6.0].

param: numRows number of rows param: numCols number of columns param: values matrix entries in column major if not transposed or in row major otherwise param: isTransposed whether the matrix is transposed. If true, values stores the matrix in row major.

See Also:
Serialized Form

Constructor Summary
DenseMatrix(int numRows, int numCols, double[] values)
          Column-major dense matrix.
DenseMatrix(int numRows, int numCols, double[] values, boolean isTransposed)
           
 
Method Summary
 double apply(int i, int j)
          Gets the (i, j)-th element.
 DenseMatrix copy()
          Get a deep copy of the matrix.
static DenseMatrix diag(Vector vector)
          Generate a diagonal matrix in DenseMatrix format from the supplied values.
 boolean equals(Object o)
           
static DenseMatrix eye(int n)
          Generate an Identity Matrix in DenseMatrix format.
 int hashCode()
           
 boolean isTransposed()
          Flag that keeps track whether the matrix is transposed or not.
 int numCols()
          Number of columns.
 int numRows()
          Number of rows.
static DenseMatrix ones(int numRows, int numCols)
          Generate a DenseMatrix consisting of ones.
static DenseMatrix rand(int numRows, int numCols, java.util.Random rng)
          Generate a DenseMatrix consisting of i.i.d. uniform random numbers.
static DenseMatrix randn(int numRows, int numCols, java.util.Random rng)
          Generate a DenseMatrix consisting of i.i.d. gaussian random numbers.
 SparseMatrix toSparse()
          Generate a SparseMatrix from the given DenseMatrix.
 DenseMatrix transpose()
          Transpose the Matrix.
 double[] values()
           
static DenseMatrix zeros(int numRows, int numCols)
          Generate a DenseMatrix consisting of zeros.
 
Methods inherited from class Object
getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.spark.mllib.linalg.Matrix
multiply, multiply, multiply, toArray, toString, toString
 

Constructor Detail

DenseMatrix

public DenseMatrix(int numRows,
                   int numCols,
                   double[] values,
                   boolean isTransposed)

DenseMatrix

public DenseMatrix(int numRows,
                   int numCols,
                   double[] values)
Column-major dense matrix. The entry values are stored in a single array of doubles with columns listed in sequence. For example, the following matrix

   1.0 2.0
   3.0 4.0
   5.0 6.0
 
is stored as [1.0, 3.0, 5.0, 2.0, 4.0, 6.0].

Parameters:
numRows - number of rows
numCols - number of columns
values - matrix entries in column major
Method Detail

zeros

public static DenseMatrix zeros(int numRows,
                                int numCols)
Generate a DenseMatrix consisting of zeros.

Parameters:
numRows - number of rows of the matrix
numCols - number of columns of the matrix
Returns:
DenseMatrix with size numRows x numCols and values of zeros

ones

public static DenseMatrix ones(int numRows,
                               int numCols)
Generate a DenseMatrix consisting of ones.

Parameters:
numRows - number of rows of the matrix
numCols - number of columns of the matrix
Returns:
DenseMatrix with size numRows x numCols and values of ones

eye

public static DenseMatrix eye(int n)
Generate an Identity Matrix in DenseMatrix format.

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

rand

public static DenseMatrix rand(int numRows,
                               int numCols,
                               java.util.Random rng)
Generate a DenseMatrix consisting of i.i.d. uniform random numbers.

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

randn

public static DenseMatrix randn(int numRows,
                                int numCols,
                                java.util.Random rng)
Generate a DenseMatrix consisting of i.i.d. gaussian random numbers.

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

diag

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

Parameters:
vector - a Vector that will form the values on the diagonal of the matrix
Returns:
Square DenseMatrix with size values.length x values.length and 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

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

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

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 DenseMatrix copy()
Description copied from interface: Matrix
Get a deep copy of the matrix.

Specified by:
copy in interface Matrix

transpose

public DenseMatrix 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

toSparse

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

Returns:
(undocumented)