How it works...

In our experience, most of the difficulties with SparseMatrices come from a lack of understanding of the difference between Compressed Row Storage (CRS) and Compressed Column Storage (CCS). We highly recommend that the reader researches this topic in depth to clearly understand the differences.

In short, the CCS format is used by Spark for the transposed target matrix:

  1. There are two distinct signatures for this method call constructor:
    • SparseMatrix (int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values)
    • SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values, boolean isTransposed)

In option number two, we are indicating that the matrix is declared as transposed already, so the matrix will be treated differently.

  1. The method inherits from the following which makes their concrete methods available to all routines:
    • interface class java.lang.Object
    • java.io.Serializable
    • Matrix
  2. There are several method calls that are of interest:
    • Generate the diagonal matrix from the supplied values in the vector:
static SparseMatrix spdiag(Vector vector)
    • Create an identity matrix. An identity matrix is a matrix that has diagonals as 1 and any other element as 0:
static speye(int n)
    • Keep track of whether the matrix is transposed:
boolean isTransposed()
    • Create a matrix with a set of random numbers - drawn from uniform distribution:
static SparseMatrix sprand(int numRows, int numCols, java.util.Random rng)
    • Create a matrix with a set of random numbers - drawn from gaussian distribution:
static SparseMatrix sprandn(int numRows, int numCols, java.util.Random rng)
    • Transpose the matrix:
SparseMatrix transpose()
    • Make a deep copy of the vector
SparseMatrix Copy()
    • Convert to a SparseVector. You will do this if your vector is long and the density decreases after a number of operations (for example, zero out non-contributing members):
DenseMatrix toDense()
    • Find the number of non-zero elements. This is useful so you can convert on-the-fly to the SparseVector if the density ID is low:
Int numNonzeros()
    • Get all the values stored in the Matrix:
Double[] Values()
    • There are other calls corresponding to the specific operation for the SparseMatrix. The following is a sample, but we strongly recommend that you familiarize yourself with the manual pages (see the There's more... section):
      1. Get Row Indexes: int rowIndices()
      2. Check for transposition: booleanisTransposed()
      3. Get Column pointers: int[]colPtrs()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.215.212