Class DenseMatrix

Object
org.apache.spark.mllib.linalg.DenseMatrix
All Implemented Interfaces:
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:
  • Constructor Details

    • 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 Details

    • 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, 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, 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
    • fromML

      public static DenseMatrix fromML(DenseMatrix m)
      Convert new linalg type to spark.mllib type. Light copy; only copies references
      Parameters:
      m - (undocumented)
      Returns:
      (undocumented)
    • 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
      Returns:
      (undocumented)
    • numNonzeros

      public int numNonzeros()
      Description copied from interface: Matrix
      Find the number of non-zero active values.
      Specified by:
      numNonzeros in interface Matrix
      Returns:
      (undocumented)
    • numActives

      public int numActives()
      Description copied from interface: Matrix
      Find the number of values stored explicitly. These values can be zero as well.
      Specified by:
      numActives in interface Matrix
      Returns:
      (undocumented)
    • toSparse

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

      public scala.collection.Iterator<Vector> colIter()
      Description copied from interface: Matrix
      Returns an iterator of column vectors. This operation could be expensive, depending on the underlying storage.
      Specified by:
      colIter in interface Matrix
      Returns:
      (undocumented)
    • asML

      public DenseMatrix asML()
      Description copied from interface: Matrix
      Convert this matrix to the new mllib-local representation. This does NOT copy the data; it copies references.
      Specified by:
      asML in interface Matrix
      Returns:
      (undocumented)