jsat.linear
Class SparseMatrix
- java.lang.Object
-
- jsat.linear.Matrix
-
- jsat.linear.SparseMatrix
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable
public class SparseMatrix extends Matrix
Creates a new Sparse Matrix where each row is backed by a sparse vector.
This implementation does not support theQRorlup()decompositions.
transposeMultiply(jsat.linear.Matrix, jsat.linear.Matrix, java.util.concurrent.ExecutorService)currently does not use multiple cores.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description SparseMatrix(int rows, int cols)Creates a new sparse matrixSparseMatrix(int rows, int cols, int rowCapacity)Creates a new sparse matrixSparseMatrix(SparseVector[] rows)Creates a new Sparse Matrix backed by the given array of SpareVectors.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidchangeSize(int newRows, int newCols)This method alters the size of a matrix, either adding or subtracting rows from the internal structure of the matrix.SparseMatrixclone()intcols()Returns the number of columns stored in this matrixdoubleget(int i, int j)Returns the value stored at at the matrix position Ai,jVecgetRowView(int r)Obtains a vector that is backed by this, at very little memory cost.voidincrement(int i, int j, double value)Alters the current matrix at index (i,j) to be equal to Ai,j = Ai,j + valuebooleanisSparce()Returnstrueif the matrix is sparse,falseotherwiseMatrix[]lup()Matrix[]lup(java.util.concurrent.ExecutorService threadPool)voidmultiply(Matrix B, Matrix C)Alters the matrix C to be equal to C = C+A*Bvoidmultiply(Matrix B, Matrix C, java.util.concurrent.ExecutorService threadPool)Alters the matrix C to be equal to C = C+A*Bvoidmultiply(Vec b, double z, Vec c)If this matrix is Am x n, and b has a length of n, and c has a length of m, then this will mutate c to store c = c + A*b*zvoidmultiplyTranspose(Matrix B, Matrix C)Alters the matrix C to be equal to C = C+A*BTvoidmultiplyTranspose(Matrix B, Matrix C, java.util.concurrent.ExecutorService threadPool)Alters the matrix C to be equal to C = C+A*BTvoidmutableAdd(double c)Alters the current matrix to store the value A+cvoidmutableAdd(double c, java.util.concurrent.ExecutorService threadPool)Alters the current matrix to store the value A+cvoidmutableAdd(double c, Matrix B)Alters the current matrix to store the value A+c*BvoidmutableAdd(double c, Matrix B, java.util.concurrent.ExecutorService threadPool)Alters the current matrix to store the value A+c*BvoidmutableMultiply(double c)Alters the current matrix to be equal to A*cvoidmutableMultiply(double c, java.util.concurrent.ExecutorService threadPool)Alters the current matrix to be equal to A*cvoidmutableTranspose()Transposes the current matrix in place, altering its value.longnnz()Returns the number of non zero values stored in this matrix.Matrix[]qr()Matrix[]qr(java.util.concurrent.ExecutorService threadPool)introws()Returns the number of rows stored in this matrixvoidset(int i, int j, double value)Sets the value stored at at the matrix position Ai,jvoidswapRows(int r1, int r2)Alters the current matrix by swapping the values stored in two different rows.voidtranspose(Matrix C)Overwrites the values stored in matrix C to store the value of A'voidtransposeMultiply(double c, Vec b, Vec x)Alters the vector x to be equal to x = x + A'*b*cvoidtransposeMultiply(Matrix B, Matrix C)Alters the matrix C so that C = C + A'*BvoidtransposeMultiply(Matrix B, Matrix C, java.util.concurrent.ExecutorService threadPool)Alters the matrix C so that C = C + A'*BvoidzeroOut()Alters the current matrix so that all values are equal to zero.-
Methods inherited from class jsat.linear.Matrix
add, add, add, add, canBeMutated, canMultiply, copyTo, diag, diagMult, diagMult, equals, equals, eye, getColumn, getColumnView, getRow, isSquare, isSymmetric, isSymmetric, multiply, multiply, multiply, multiply, multiply, multiplyTranspose, multiplyTranspose, mutableAdd, mutableAdd, mutableSubtract, mutableSubtract, mutableSubtract, mutableSubtract, mutableSubtract, mutableSubtract, OuterProductUpdate, OuterProductUpdate, pascal, random, sameDimensions, subtract, subtract, subtract, subtract, toString, transpose, transposeMultiply, transposeMultiply, transposeMultiply, updateRow
-
-
-
-
Constructor Detail
-
SparseMatrix
public SparseMatrix(int rows, int cols, int rowCapacity)Creates a new sparse matrix- Parameters:
rows- the number of rows for the matrixcols- the number of columns for the matrixrowCapacity- the initial capacity for non zero values for each row
-
SparseMatrix
public SparseMatrix(SparseVector[] rows)
Creates a new Sparse Matrix backed by the given array of SpareVectors. Altering the array of any object in it will also alter the this matrix.- Parameters:
rows- the array to back this SparseMatrix
-
SparseMatrix
public SparseMatrix(int rows, int cols)Creates a new sparse matrix- Parameters:
rows- the number of rows for the matrixcols- the number of columns for the matrix
-
-
Method Detail
-
mutableAdd
public void mutableAdd(double c, Matrix B)Description copied from class:MatrixAlters the current matrix to store the value A+c*B- Specified by:
mutableAddin classMatrix- Parameters:
c- the scalar constant to multiple B byB- the matrix to add to this
-
mutableAdd
public void mutableAdd(double c, Matrix B, java.util.concurrent.ExecutorService threadPool)Description copied from class:MatrixAlters the current matrix to store the value A+c*B- Specified by:
mutableAddin classMatrix- Parameters:
c- the scalar constant to multiple B byB- the matrix to add to thisthreadPool- the source of threads to do computation in parallel
-
mutableAdd
public void mutableAdd(double c)
Description copied from class:MatrixAlters the current matrix to store the value A+c- Specified by:
mutableAddin classMatrix- Parameters:
c- the scalar constant to add to this
-
mutableAdd
public void mutableAdd(double c, java.util.concurrent.ExecutorService threadPool)Description copied from class:MatrixAlters the current matrix to store the value A+c- Specified by:
mutableAddin classMatrix- Parameters:
c- the scalar constant to add to thisthreadPool- the source of threads to do computation in parallel
-
multiply
public void multiply(Vec b, double z, Vec c)
Description copied from class:MatrixIf this matrix is Am x n, and b has a length of n, and c has a length of m, then this will mutate c to store c = c + A*b*z
-
multiply
public void multiply(Matrix B, Matrix C)
Description copied from class:MatrixAlters the matrix C to be equal to C = C+A*B
-
multiply
public void multiply(Matrix B, Matrix C, java.util.concurrent.ExecutorService threadPool)
Description copied from class:MatrixAlters the matrix C to be equal to C = C+A*B
-
mutableMultiply
public void mutableMultiply(double c)
Description copied from class:MatrixAlters the current matrix to be equal to A*c- Specified by:
mutableMultiplyin classMatrix- Parameters:
c- the scalar constant to multiply by
-
mutableMultiply
public void mutableMultiply(double c, java.util.concurrent.ExecutorService threadPool)Description copied from class:MatrixAlters the current matrix to be equal to A*c- Specified by:
mutableMultiplyin classMatrix- Parameters:
c- the scalar constant to multiply bythreadPool- the source of threads to do computation in parallel
-
lup
public Matrix[] lup(java.util.concurrent.ExecutorService threadPool)
-
qr
public Matrix[] qr(java.util.concurrent.ExecutorService threadPool)
-
mutableTranspose
public void mutableTranspose()
Description copied from class:MatrixTransposes the current matrix in place, altering its value. Only valid for square matrices- Specified by:
mutableTransposein classMatrix
-
transpose
public void transpose(Matrix C)
Description copied from class:MatrixOverwrites the values stored in matrix C to store the value of A'
-
transposeMultiply
public void transposeMultiply(Matrix B, Matrix C)
Description copied from class:MatrixAlters the matrix C so that C = C + A'*B- Specified by:
transposeMultiplyin classMatrix- Parameters:
B- the matrix to multiply byC- the matrix to add the result to
-
transposeMultiply
public void transposeMultiply(Matrix B, Matrix C, java.util.concurrent.ExecutorService threadPool)
Description copied from class:MatrixAlters the matrix C so that C = C + A'*B- Specified by:
transposeMultiplyin classMatrix- Parameters:
B- the matrix to multiply byC- the matrix to place the results inthreadPool- the source of threads to do computation in parallel
-
transposeMultiply
public void transposeMultiply(double c, Vec b, Vec x)Description copied from class:MatrixAlters the vector x to be equal to x = x + A'*b*c- Specified by:
transposeMultiplyin classMatrix- Parameters:
c- the scalar constant to multiply byb- the vector to multiply byx- the vector the add the result to
-
getRowView
public Vec getRowView(int r)
Description copied from class:MatrixObtains a vector that is backed by this, at very little memory cost. Mutations to this vector will alter the values stored in the matrix, and vice versa.- Overrides:
getRowViewin classMatrix- Parameters:
r- the row to obtain a view of- Returns:
- a vector backed by the specified row of the matrix
-
get
public double get(int i, int j)Description copied from class:MatrixReturns the value stored at at the matrix position Ai,j
-
set
public void set(int i, int j, double value)Description copied from class:MatrixSets the value stored at at the matrix position Ai,j
-
increment
public void increment(int i, int j, double value)Description copied from class:MatrixAlters the current matrix at index (i,j) to be equal to Ai,j = Ai,j + value
-
rows
public int rows()
Description copied from class:MatrixReturns the number of rows stored in this matrix
-
cols
public int cols()
Description copied from class:MatrixReturns the number of columns stored in this matrix
-
isSparce
public boolean isSparce()
Description copied from class:MatrixReturnstrueif the matrix is sparse,falseotherwise
-
swapRows
public void swapRows(int r1, int r2)Description copied from class:MatrixAlters the current matrix by swapping the values stored in two different rows.
-
zeroOut
public void zeroOut()
Description copied from class:MatrixAlters the current matrix so that all values are equal to zero.
-
clone
public SparseMatrix clone()
-
nnz
public long nnz()
Description copied from class:MatrixReturns the number of non zero values stored in this matrix. This is mostly useful for sparse matrices.
-
changeSize
public void changeSize(int newRows, int newCols)Description copied from class:MatrixThis method alters the size of a matrix, either adding or subtracting rows from the internal structure of the matrix. Every resize call may cause a new allocation internally, and should not be called for excessive changing of a matrix. All added rows/ columns will have values of zero. If a row / column is removed, it is always the bottom/right most row / column removed. Values of the removed rows / columns will be lost.- Specified by:
changeSizein classMatrix- Parameters:
newRows- the new number of rows, must be positivenewCols- the new number of columns, must be positive.
-
multiplyTranspose
public void multiplyTranspose(Matrix B, Matrix C)
Description copied from class:MatrixAlters the matrix C to be equal to C = C+A*BT- Specified by:
multiplyTransposein classMatrix- Parameters:
B- the matrix to multiply this withC- the matrix to add the result to
-
multiplyTranspose
public void multiplyTranspose(Matrix B, Matrix C, java.util.concurrent.ExecutorService threadPool)
Description copied from class:MatrixAlters the matrix C to be equal to C = C+A*BT- Specified by:
multiplyTransposein classMatrix- Parameters:
B- the matrix to multiply this withC- the matrix to add the result tothreadPool- the source of threads to do computation in parallel
-
-
DataMelt 3.0 © DataMelt by jWork.ORG