Package org.la4j.matrix
Class SparseMatrix
java.lang.Object
org.la4j.Matrix
org.la4j.matrix.SparseMatrix
- Direct Known Subclasses:
ColumnMajorSparseMatrix
,RowMajorSparseMatrix
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSparseMatrix
(int rows, int columns) SparseMatrix
(int rows, int columns, int cardinality) -
Method Summary
Modifier and TypeMethodDescriptionadd
(double value) Adds givenvalue
(v) to every element of this matrix (A).static SparseMatrix
protected long
capacity()
int
Returns the cardinality (the number of non-zero elements) of this sparse matrix.double
density()
Returns the density (non-zero elements divided by total elements) of this sparse matrix.static SparseMatrix
diagonal
(int size, double diagonal) void
eachNonZero
(MatrixProcedure procedure) Applies givenprocedure
to each non-zero element of this matrix.void
eachNonZeroInColumn
(int j, VectorProcedure procedure) Applies the givenprocedure
to each non-zero element of the specified column of this matrix.void
eachNonZeroInRow
(int i, VectorProcedure procedure) Applies the givenprocedure
to each non-zero element of the specified row of this matrix.protected void
ensureCardinalityIsCorrect
(long rows, long columns, long cardinality) double
foldNonZero
(MatrixAccumulator accumulator) Folds non-zero elements of this matrix with givenaccumulator
.double
foldNonZeroInColumn
(int j, VectorAccumulator accumulator) Folds non-zero elements of the specified column in this matrix with the givenaccumulator
.double[]
foldNonZeroInColumns
(VectorAccumulator accumulator) Folds non-zero elements (in a column-by-column manner) of this matrix with givenaccumulator
.double
foldNonZeroInRow
(int i, VectorAccumulator accumulator) Folds non-zero elements of the specified row in this matrix with the givenaccumulator
.double[]
foldNonZeroInRows
(VectorAccumulator accumulator) Folds non-zero elements (in a row-by-row manner) of this matrix with givenaccumulator
.static SparseMatrix
from1DArray
(int rows, int columns, double[] array) Creates a newSparseMatrix
from the given 1Darray
with compressing (copying) the underlying array.static SparseMatrix
from2DArray
(double[][] array) Creates a newSparseMatrix
from the given 2Darray
with compressing (copying) the underlying array.static SparseMatrix
ParsesSparseMatrix
from the given CSV string.static SparseMatrix
ParsesSparseMatrix
from the given Matrix Market string.double
get
(int i, int j) Gets the specified element of this matrix.getColumn
(int j) Copies the specified column of this matrix into the vector.abstract double
getOrElse
(int i, int j, double defaultValue) Gets the specified element, or adefaultValue
if there is no actual element at (i
,j
) in this sparse matrix.getRow
(int i) Copies the specified row of this matrix into the vector.static SparseMatrix
identity
(int size) Creates an identitySparseMatrix
of the givensize
.boolean
abstract boolean
Checks whether or not this sparse matrix row-major.boolean
isZeroAt
(int i, int j) Whether or not the specified element is zero.multiply
(double value) Scales this matrix by givenvalue
(v).abstract boolean
nonZeroAt
(int i, int j) Whether or not the specified element is not zero.Returns a non-zero column-major matrix iterator.Returns a non-zero matrix iterator.nonZeroIteratorOfColumn
(int j) Returns a non-zero vector iterator of the given columnj
.nonZeroIteratorOfRow
(int i) Returns a non-zero vector iterator of the given rowi
.Returns a non-zero row-major matrix iterator.static SparseMatrix
static SparseMatrix
randomSymmetric
(int size, double density, Random random) Creates a random symmetricSparseMatrix
of the givensize
.toMatrixMarket
(NumberFormat formatter) Converts this matrix into the Matrix Market string using the given numberformatter
.static SparseMatrix
zero
(int rows, int columns) static SparseMatrix
zero
(int rows, int columns, int capacity) Methods inherited from class org.la4j.Matrix
add, apply, apply, apply, blank, blankOfColumns, blankOfRows, blankOfShape, columnMajorIterator, columns, constant, copy, copyOfColumns, copyOfRows, copyOfShape, determinant, diagonalProduct, divide, each, eachInColumn, eachInRow, ensureDimensionsAreCorrect, ensureIndexesAreInBounds, equals, equals, euclideanNorm, fail, fold, foldColumn, foldColumns, foldRow, foldRows, hadamardProduct, hashCode, infinityNorm, insert, insert, insert, insert, insertColumn, insertRow, is, is, iterator, iteratorOfColumn, iteratorOfRow, kroneckerProduct, manhattanNorm, max, maxInColumn, maxInRow, min, minInColumn, minInRow, mkString, mkString, mkString, multiply, multiply, multiplyByItsTranspose, non, non, norm, power, product, random, randomSymmetric, rank, removeColumn, removeFirstColumn, removeFirstRow, removeLastColumn, removeLastRow, removeRow, rotate, rowMajorIterator, rows, select, set, setAll, setColumn, setColumn, setRow, setRow, shuffle, slice, sliceBottomRight, sliceTopLeft, subtract, subtract, sum, swapColumns, swapRows, to, toBinary, toColumnMajorSparseMatrix, toColumnVector, toCSV, toCSV, toDenseMatrix, toMatrixMarket, toRowMajorSparseMatrix, toRowVector, toSparseMatrix, toString, trace, transform, transformColumn, transformRow, transpose, unit, update, updateAt, updateColumn, updateRow, withDecompositor, withInverter, withSolver
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
cardinality
protected int cardinality
-
-
Constructor Details
-
SparseMatrix
public SparseMatrix(int rows, int columns) -
SparseMatrix
public SparseMatrix(int rows, int columns, int cardinality)
-
-
Method Details
-
zero
-
zero
-
diagonal
-
identity
Creates an identitySparseMatrix
of the givensize
. -
random
-
randomSymmetric
Creates a random symmetricSparseMatrix
of the givensize
. -
from1DArray
Creates a newSparseMatrix
from the given 1Darray
with compressing (copying) the underlying array. -
from2DArray
Creates a newSparseMatrix
from the given 2Darray
with compressing (copying) the underlying array. -
block
-
fromCSV
ParsesSparseMatrix
from the given CSV string.- Parameters:
csv
- the CSV string representing a matrix- Returns:
- a parsed matrix
-
fromMatrixMarket
ParsesSparseMatrix
from the given Matrix Market string.- Parameters:
mm
- the string in Matrix Market format- Returns:
- a parsed matrix
-
get
public double get(int i, int j) Description copied from class:Matrix
Gets the specified element of this matrix. -
getOrElse
public abstract double getOrElse(int i, int j, double defaultValue) Gets the specified element, or adefaultValue
if there is no actual element at (i
,j
) in this sparse matrix.- Parameters:
i
- the element's row indexj
- the element's column indexdefaultValue
- the default value- Returns:
- the element of this vector or a default value
-
isRowMajor
public abstract boolean isRowMajor()Checks whether or not this sparse matrix row-major. -
isColumnMajor
public boolean isColumnMajor() -
cardinality
public int cardinality()Returns the cardinality (the number of non-zero elements) of this sparse matrix.- Returns:
- the cardinality of this matrix
-
density
public double density()Returns the density (non-zero elements divided by total elements) of this sparse matrix.- Returns:
- the density of this matrix
-
capacity
protected long capacity()- Returns:
- a capacity of this sparse matrix
-
getRow
Description copied from class:Matrix
Copies the specified row of this matrix into the vector. -
getColumn
Description copied from class:Matrix
Copies the specified column of this matrix into the vector. -
multiply
Description copied from class:Matrix
Scales this matrix by givenvalue
(v). -
add
Description copied from class:Matrix
Adds givenvalue
(v) to every element of this matrix (A). -
isZeroAt
public boolean isZeroAt(int i, int j) Whether or not the specified element is zero.- Parameters:
i
- element's row indexj
- element's column index- Returns:
true
if specified element is zero,false
otherwise
-
nonZeroAt
public abstract boolean nonZeroAt(int i, int j) Whether or not the specified element is not zero.- Parameters:
i
- element's row indexj
- element's column index- Returns:
true
if specified element is not zero,false
otherwise
-
eachNonZero
Applies givenprocedure
to each non-zero element of this matrix.- Parameters:
procedure
- the matrix procedure
-
eachNonZeroInRow
Applies the givenprocedure
to each non-zero element of the specified row of this matrix.- Parameters:
i
- the row index.procedure
- theVectorProcedure
.
-
eachNonZeroInColumn
Applies the givenprocedure
to each non-zero element of the specified column of this matrix.- Parameters:
j
- the column index.procedure
- theVectorProcedure
.
-
foldNonZero
Folds non-zero elements of this matrix with givenaccumulator
.- Parameters:
accumulator
- the matrix accumulator- Returns:
- the accumulated value
-
foldNonZeroInRow
Folds non-zero elements of the specified row in this matrix with the givenaccumulator
.- Parameters:
i
- the row index.accumulator
- theVectorAccumulator
.- Returns:
- the accumulated value.
-
foldNonZeroInColumn
Folds non-zero elements of the specified column in this matrix with the givenaccumulator
.- Parameters:
j
- the column index.accumulator
- theVectorAccumulator
.- Returns:
- the accumulated value.
-
foldNonZeroInColumns
Folds non-zero elements (in a column-by-column manner) of this matrix with givenaccumulator
.- Parameters:
accumulator
- the matrix accumulator- Returns:
- the accumulated vector
-
foldNonZeroInRows
Folds non-zero elements (in a row-by-row manner) of this matrix with givenaccumulator
.- Parameters:
accumulator
- the matrix accumulator- Returns:
- the accumulated vector
-
nonZeroIterator
Returns a non-zero matrix iterator.- Returns:
- a non-zero matrix iterator
-
nonZeroRowMajorIterator
Returns a non-zero row-major matrix iterator.- Returns:
- a non-zero row-major matrix iterator.
-
nonZeroColumnMajorIterator
Returns a non-zero column-major matrix iterator.- Returns:
- a non-zero column major matrix iterator.
-
nonZeroIteratorOfRow
Returns a non-zero vector iterator of the given rowi
.- Returns:
- a non-zero vector iterator
-
nonZeroIteratorOfColumn
Returns a non-zero vector iterator of the given columnj
.- Returns:
- a non-zero vector iterator
-
toMatrixMarket
Description copied from class:Matrix
Converts this matrix into the Matrix Market string using the given numberformatter
.- Specified by:
toMatrixMarket
in classMatrix
- Returns:
- a string in Matrix Market format representing this matrix;
-
ensureCardinalityIsCorrect
protected void ensureCardinalityIsCorrect(long rows, long columns, long cardinality)
-