public class DenseMatrix extends java.lang.Object implements DataMatrix, java.io.Serializable
A big reason that we do not adopt original DenseMatrix from M4J libraray is because the latter using one-dimensional array to store data, which will often cause OutOfMemory exception due to the limit of maximum length of a one-dimensional Java array.
Modifier and Type | Field and Description |
---|---|
double[][] |
data
read data
|
int |
numColumns
dimension
|
int |
numRows
dimension
|
int |
topN
dimension
|
Constructor and Description |
---|
DenseMatrix(DenseMatrix mat)
Construct a dense matrix by copying data from a given matrix
|
DenseMatrix(double[][] array)
Construct a dense matrix by copying data from a given 2D array
|
DenseMatrix(double[][] array,
int numRows,
int numColumns)
Construct a dense matrix by a shallow copy of a data array
|
DenseMatrix(int numRows,
int numColumns)
Construct a dense matrix with specified dimensions
|
DenseMatrix(int numRows,
int numColumns,
int topN)
Construct a dense matrix with specified dimensions
|
Modifier and Type | Method and Description |
---|---|
DenseMatrix |
add(DenseMatrix mat)
Do
A + B matrix operation |
DenseMatrix |
add(double val)
Do
A + c matrix operation, where c is a constant. |
void |
add(int row,
int column,
double val)
Add a value to entry [row, column]
|
DenseMatrix |
add(SparseMatrix mat)
Do
A + B matrix operation |
DenseMatrix |
addEqual(DenseMatrix mat)
Do
A + B matrix operation |
DenseMatrix |
addEqual(double val)
Do
A + c matrix operation, where c is a constant. |
DenseMatrix |
addEqual(SparseMatrix mat)
Do
A + B matrix operation |
DenseMatrix |
cholesky() |
void |
clear()
Clear and reset all entries to 0.
|
DenseMatrix |
clone()
Make a deep copy of current matrix
|
static double |
colMult(DenseMatrix m,
int mcol,
DenseMatrix n,
int ncol)
Inner product of two column vectors
|
DenseVector |
column(int column)
Return a copy of column data as a dense vector.
|
double |
columnMean(int column)
Compute mean of a column of the current matrix.
|
DenseMatrix |
cov() |
static DenseMatrix |
eye(int dim)
Construct an identity matrix
|
double |
get(int row,
int column)
Get the value at entry [row, column]
|
double[][] |
getData() |
DenseMatrix |
getSubMatrix(int rowStart,
int rowEnd,
int colStart,
int colEnd)
Return a sub matrix of this matrix.
|
static DenseMatrix |
hadamardProduct(DenseMatrix M,
DenseMatrix N)
Return Hadamard product of two matrices.
|
void |
init()
Initialize a dense matrix with small random values in (0, 1)
|
void |
init(double range)
Initialize a dense matrix with small random values in (0, range)
|
void |
init(double mean,
double sigma)
Initialize a dense matrix with small Guassian values
|
DenseMatrix |
inv()
NOTE: this implementation (adopted from PREA package) is slightly faster than
inverse , especially when
numRows is large. |
DenseMatrix |
inverse()
Deprecated.
use
inv instead which is slightly faster |
static DenseMatrix |
khatriRaoProduct(DenseMatrix M,
DenseMatrix N)
Return Khatri-Rao product of two matrices.
|
static DenseMatrix |
kroneckerProduct(DenseMatrix M,
DenseMatrix N)
Return Kronecker product of two arbitrary matrices
|
DenseMatrix |
minus(DenseMatrix mat)
Do
A - B matrix operation |
DenseMatrix |
minus(double val)
Do
A - c matrix operation, where c is a constant. |
DenseMatrix |
minus(SparseMatrix mat)
Do
A - B matrix operation |
DenseMatrix |
minusEqual(DenseMatrix mat)
Do
A - B matrix operation |
DenseMatrix |
minusEqual(double val)
Do
A - c matrix operation, where c is a constant. |
DenseMatrix |
minusEqual(SparseMatrix mat)
Do
A - B matrix operation |
DenseMatrix |
mult(DenseMatrix mat)
Matrix multiplication with a dense matrix
|
DenseVector |
mult(DenseVector vec)
Do
matrix x vector between current matrix and a given vector |
DenseMatrix |
mult(SparseMatrix mat)
Matrix multiplication with a sparse matrix
|
static DenseMatrix |
mult(SparseMatrix sm,
DenseMatrix dm)
Matrix multiplication of a sparse matrix by a dense matrix
|
DenseVector |
mult(SparseVector vec) |
double |
norm() |
int |
numColumns() |
int |
numRows() |
DenseMatrix |
pinv() |
static double |
product(DenseMatrix m,
int mrow,
DenseMatrix n,
int ncol)
Dot product of row x col between two matrices.
|
DenseVector |
row(int rowId)
Return a copy of row data as a dense vector.
|
DenseVector |
row(int rowId,
boolean deep)
Return a vector of a specific row.
|
static double |
rowMult(DenseMatrix m,
int mrow,
DenseMatrix n,
int nrow)
Inner product of two row vectors
|
DenseMatrix |
scale(double val)
Return a new matrix by scaling the current matrix.
|
DenseMatrix |
scaleEqual(double val)
Return this matrix by scaling the current matrix.
|
void |
set(int row,
int column,
double val)
Set a value to entry [row, column]
|
void |
setAll(double val)
Set a value to all entries
|
void |
setRow(int row,
DenseVector vals)
Set values of one dense vector to a specific row.
|
void |
setRow(int row,
double val)
Set one value to a specific row.
|
int |
size() |
double |
sum() |
double |
sumOfColumn(int col)
Return the sum of data entries in a column.
|
double |
sumOfRow(int row)
Return the sum of data entries in a row
|
SVD |
svd() |
java.lang.String |
toString() |
DenseMatrix |
transMult() |
DenseMatrix |
transpose() |
public int numRows
public int numColumns
public int topN
public double[][] data
public DenseMatrix(int numRows, int numColumns)
numRows
- number of rowsnumColumns
- number of columnspublic DenseMatrix(int numRows, int numColumns, int topN)
numRows
- number of rowsnumColumns
- number of columnstopN
- numnber of top Npublic DenseMatrix(double[][] array)
array
- data arraypublic DenseMatrix(double[][] array, int numRows, int numColumns)
array
- the data arraynumColumns
- number of columnsnumRows
- number of rowspublic DenseMatrix(DenseMatrix mat)
mat
- input matrixpublic DenseMatrix clone()
clone
in class java.lang.Object
public static DenseMatrix eye(int dim)
dim
- dimensionpublic void init(double mean, double sigma)
NOTE: small initial values make it easier to train a model; otherwise a very small learning rate may be needed (especially when the number of factors is large) which can cause bad performance.
mean
- mean of the gaussian functionsigma
- sigma of the gaussian functionpublic void init(double range)
range
- max of the rangepublic void init()
public int numRows()
public int numColumns()
public DenseVector row(int rowId)
rowId
- row idpublic DenseVector row(int rowId, boolean deep)
rowId
- row iddeep
- whether to copy data or only shallow copy for executing speedup purposepublic DenseMatrix getSubMatrix(int rowStart, int rowEnd, int colStart, int colEnd)
rowStart
- the row index to startrowEnd
- the row index to endcolStart
- the column index to startcolEnd
- the column index to endpublic DenseVector column(int column)
column
- column idpublic double columnMean(int column)
column
- column idpublic double norm()
public static double rowMult(DenseMatrix m, int mrow, DenseMatrix n, int nrow)
m
- the first matrixmrow
- row of the first matrixn
- the second matrixnrow
- row of the second matrixpublic static double colMult(DenseMatrix m, int mcol, DenseMatrix n, int ncol)
m
- the first matrixmcol
- column of the first matrixn
- the second matrixncol
- column of the second matrixpublic static double product(DenseMatrix m, int mrow, DenseMatrix n, int ncol) throws LibrecException
m
- the first matrixmrow
- row id of the first matrixn
- the second matrixncol
- column id of the second matrixLibrecException
- if m.numColumns != n.numRows
public static DenseMatrix kroneckerProduct(DenseMatrix M, DenseMatrix N)
M
- a dense matrixN
- an other dense matrixpublic static DenseMatrix khatriRaoProduct(DenseMatrix M, DenseMatrix N) throws java.lang.Exception
M
- a dense matrixN
- an other dense matrixjava.lang.Exception
- if error occurspublic static DenseMatrix hadamardProduct(DenseMatrix M, DenseMatrix N) throws java.lang.Exception
M
- a dense matrixN
- an other dense matrixjava.lang.Exception
- if The dimensions of two matrices are not consistentpublic DenseMatrix transMult()
A^T A
public DenseMatrix mult(DenseMatrix mat) throws LibrecException
mat
- a dense matrixLibrecException
- if this.numColumns != mat.numRows
public DenseMatrix mult(SparseMatrix mat) throws LibrecException
mat
- a sparse matrixLibrecException
- if this.numColumns != mat.numRows
public DenseVector mult(DenseVector vec) throws LibrecException
matrix x vector
between current matrix and a given vectorvec
- a given vectormatrix x vector
LibrecException
- if this.numColumns != vec.size
public DenseVector mult(SparseVector vec)
public static DenseMatrix mult(SparseMatrix sm, DenseMatrix dm) throws LibrecException
sm
- a sparse matrixdm
- a dense matrixLibrecException
- if sm.numColumns != dm.numRows
public double get(int row, int column)
get
in interface DataMatrix
column
- column indexrow
- row indexpublic void set(int row, int column, double val)
set
in interface DataMatrix
row
- row indexcolumn
- column indexval
- the value to be setpublic void setAll(double val)
val
- the value to be setpublic double sumOfRow(int row)
row
- row indexpublic double sumOfColumn(int col)
col
- column indexpublic double sum()
public DenseMatrix scale(double val)
val
- a given valuepublic DenseMatrix scaleEqual(double val)
val
- a given value for scalingpublic void add(int row, int column, double val)
val
- the value to be addedrow
- row indexcolumn
- column indexpublic DenseMatrix add(DenseMatrix mat) throws LibrecException
A + B
matrix operationmat
- another matrixC = A + B
LibrecException
- if numRows != mat.numRows
or
numColumns != mat.numColumns
public DenseMatrix addEqual(DenseMatrix mat) throws LibrecException
A + B
matrix operationmat
- another matrixA = A + B
LibrecException
- if numRows != mat.numRows
or
numColumns != mat.numColumns
public DenseMatrix add(SparseMatrix mat) throws LibrecException
A + B
matrix operationmat
- another matricC = A + B
LibrecException
- if numRows != mat.numRows
or
numColumns != mat.numColumns
public DenseMatrix addEqual(SparseMatrix mat) throws LibrecException
A + B
matrix operationmat
- another matrixA = A + B
LibrecException
- if numRows != mat.numRows
or
numColumns != mat.numColumns
public DenseMatrix add(double val)
A + c
matrix operation, where c
is a constant. Each entries will be added by c
val
- the value to be addedC = A + c
public DenseMatrix addEqual(double val)
A + c
matrix operation, where c
is a constant. Each entries will be added by c
val
- the value to be addedA = A + c
public DenseMatrix minus(DenseMatrix mat) throws LibrecException
A - B
matrix operationmat
- another matrixC = A - B
LibrecException
- if numRows != mat.numRows
or
numColumns != mat.numColumns
public DenseMatrix minusEqual(DenseMatrix mat) throws LibrecException
A - B
matrix operationmat
- another matrixA = A - B
LibrecException
- if numRows != mat.numRows
or
numColumns != mat.numColumns
public DenseMatrix minus(SparseMatrix mat) throws LibrecException
A - B
matrix operationmat
- another matrixC = A - B
LibrecException
- if numRows != mat.numRows
or
numColumns != mat.numColumns
public DenseMatrix minusEqual(SparseMatrix mat) throws LibrecException
A - B
matrix operationmat
- another matrixC = A - B
LibrecException
- if numRows != mat.numRows
or
numColumns != mat.numColumns
public DenseMatrix minus(double val)
A - c
matrix operation, where c
is a constant. Each entries will be added by c
val
- the value to for minusC = A - c
public DenseMatrix minusEqual(double val)
A - c
matrix operation, where c
is a constant. Each entries will be added by c
val
- the value to for minusA = A - c
public DenseMatrix cholesky()
public DenseMatrix transpose()
public DenseMatrix cov()
public DenseMatrix inverse()
inv
instead which is slightly fasterpublic DenseMatrix inv()
inverse
, especially when
numRows
is large.public DenseMatrix pinv() throws LibrecException
LibrecException
- if error occurs during multpublic SVD svd()
public void setRow(int row, double val)
row
- row idval
- value to be setpublic void setRow(int row, DenseVector vals)
row
- row idvals
- values of a dense vectorpublic void clear()
public java.lang.String toString()
toString
in class java.lang.Object
public double[][] getData()
Copyright © 2017. All Rights Reserved.