Package org.apache.spark.ml.linalg
Class DenseMatrix
Object
org.apache.spark.ml.linalg.DenseMatrix
- All Implemented Interfaces:
Serializable,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 Summary
ConstructorsConstructorDescriptionDenseMatrix(int numRows, int numCols, double[] values) Column-major dense matrix.DenseMatrix(int numRows, int numCols, double[] values, boolean isTransposed) -
Method Summary
Modifier and TypeMethodDescriptiondoubleapply(int i, int j) Gets the (i, j)-th element.scala.collection.Iterator<Vector>colIter()Returns an iterator of column vectors.copy()Get a deep copy of the matrix.static DenseMatrixGenerate a diagonal matrix inDenseMatrixformat from the supplied values.booleanstatic DenseMatrixeye(int n) Generate an Identity Matrix inDenseMatrixformat.voidforeachActive(scala.Function3<Object, Object, Object, scala.runtime.BoxedUnit> f) Applies a functionfto all the active elements of dense and sparse matrix.inthashCode()booleanFlag that keeps track whether the matrix is transposed or not.intFind the number of values stored explicitly.intnumCols()Number of columns.intFind the number of non-zero active values.intnumRows()Number of rows.static DenseMatrixones(int numRows, int numCols) Generate aDenseMatrixconsisting of ones.static DenseMatrixGenerate aDenseMatrixconsisting ofi.i.d.uniform random numbers.static DenseMatrixGenerate aDenseMatrixconsisting ofi.i.d.gaussian random numbers.Transpose the Matrix.double[]values()static DenseMatrixzeros(int numRows, int numCols) Generate aDenseMatrixconsisting of zeros.Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.spark.ml.linalg.Matrix
compressed, compressedColMajor, compressedRowMajor, getDenseSizeInBytes, getSparseSizeInBytes, isColMajor, isRowMajor, multiply, multiply, multiply, rowIter, toArray, toDense, toDenseColMajor, toDenseRowMajor, toSparse, toSparseColMajor, toSparseRowMajor, toString, toString
-
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
is stored as1.0 2.0 3.0 4.0 5.0 6.0[1.0, 3.0, 5.0, 2.0, 4.0, 6.0].- Parameters:
numRows- number of rowsnumCols- number of columnsvalues- matrix entries in column major
-
-
Method Details
-
zeros
Generate aDenseMatrixconsisting of zeros.- Parameters:
numRows- number of rows of the matrixnumCols- number of columns of the matrix- Returns:
DenseMatrixwith sizenumRowsxnumColsand values of zeros
-
ones
Generate aDenseMatrixconsisting of ones.- Parameters:
numRows- number of rows of the matrixnumCols- number of columns of the matrix- Returns:
DenseMatrixwith sizenumRowsxnumColsand values of ones
-
eye
Generate an Identity Matrix inDenseMatrixformat.- Parameters:
n- number of rows and columns of the matrix- Returns:
DenseMatrixwith sizenxnand values of ones on the diagonal
-
rand
Generate aDenseMatrixconsisting ofi.i.d.uniform random numbers.- Parameters:
numRows- number of rows of the matrixnumCols- number of columns of the matrixrng- a random number generator- Returns:
DenseMatrixwith sizenumRowsxnumColsand values in U(0, 1)
-
randn
Generate aDenseMatrixconsisting ofi.i.d.gaussian random numbers.- Parameters:
numRows- number of rows of the matrixnumCols- number of columns of the matrixrng- a random number generator- Returns:
DenseMatrixwith sizenumRowsxnumColsand values in N(0, 1)
-
diag
Generate a diagonal matrix inDenseMatrixformat from the supplied values.- Parameters:
vector- aVectorthat will form the values on the diagonal of the matrix- Returns:
- Square
DenseMatrixwith sizevalues.lengthxvalues.lengthandvalueson the diagonal
-
numRows
public int numRows()Description copied from interface:MatrixNumber of rows. -
numCols
public int numCols()Description copied from interface:MatrixNumber of columns. -
values
public double[] values() -
isTransposed
public boolean isTransposed()Description copied from interface:MatrixFlag that keeps track whether the matrix is transposed or not. False by default.- Specified by:
isTransposedin interfaceMatrix
-
equals
-
hashCode
public int hashCode() -
apply
public double apply(int i, int j) Description copied from interface:MatrixGets the (i, j)-th element. -
copy
Description copied from interface:MatrixGet a deep copy of the matrix. -
transpose
Description copied from interface:MatrixTranspose the Matrix. Returns a newMatrixinstance sharing the same underlying data. -
foreachActive
Description copied from interface:MatrixApplies a functionfto all the active elements of dense and sparse matrix. The ordering of the elements are not defined.- Specified by:
foreachActivein interfaceMatrix- Parameters:
f- the function takes three parameters where the first two parameters are the row and column indices respectively with the typeInt, and the final parameter is the corresponding value in the matrix with typeDouble.
-
numNonzeros
public int numNonzeros()Description copied from interface:MatrixFind the number of non-zero active values.- Specified by:
numNonzerosin interfaceMatrix- Returns:
- (undocumented)
-
numActives
public int numActives()Description copied from interface:MatrixFind the number of values stored explicitly. These values can be zero as well.- Specified by:
numActivesin interfaceMatrix- Returns:
- (undocumented)
-
colIter
Description copied from interface:MatrixReturns an iterator of column vectors. This operation could be expensive, depending on the underlying storage.
-