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 SparseMatrixprotected longcapacity()intReturns the cardinality (the number of non-zero elements) of this sparse matrix.doubledensity()Returns the density (non-zero elements divided by total elements) of this sparse matrix.static SparseMatrixdiagonal(int size, double diagonal) voideachNonZero(MatrixProcedure procedure) Applies givenprocedureto each non-zero element of this matrix.voideachNonZeroInColumn(int j, VectorProcedure procedure) Applies the givenprocedureto each non-zero element of the specified column of this matrix.voideachNonZeroInRow(int i, VectorProcedure procedure) Applies the givenprocedureto each non-zero element of the specified row of this matrix.protected voidensureCardinalityIsCorrect(long rows, long columns, long cardinality) doublefoldNonZero(MatrixAccumulator accumulator) Folds non-zero elements of this matrix with givenaccumulator.doublefoldNonZeroInColumn(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.doublefoldNonZeroInRow(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 SparseMatrixfrom1DArray(int rows, int columns, double[] array) Creates a newSparseMatrixfrom the given 1Darraywith compressing (copying) the underlying array.static SparseMatrixfrom2DArray(double[][] array) Creates a newSparseMatrixfrom the given 2Darraywith compressing (copying) the underlying array.static SparseMatrixParsesSparseMatrixfrom the given CSV string.static SparseMatrixParsesSparseMatrixfrom the given Matrix Market string.doubleget(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 doublegetOrElse(int i, int j, double defaultValue) Gets the specified element, or adefaultValueif 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 SparseMatrixidentity(int size) Creates an identitySparseMatrixof the givensize.booleanabstract booleanChecks whether or not this sparse matrix row-major.booleanisZeroAt(int i, int j) Whether or not the specified element is zero.multiply(double value) Scales this matrix by givenvalue(v).abstract booleannonZeroAt(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 SparseMatrixstatic SparseMatrixrandomSymmetric(int size, double density, Random random) Creates a random symmetricSparseMatrixof the givensize.toMatrixMarket(NumberFormat formatter) Converts this matrix into the Matrix Market string using the given numberformatter.static SparseMatrixzero(int rows, int columns) static SparseMatrixzero(int rows, int columns, int capacity) Methods inherited from class 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, withSolverMethods inherited from interface 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 identitySparseMatrixof the givensize. -
random
-
randomSymmetric
Creates a random symmetricSparseMatrixof the givensize. -
from1DArray
Creates a newSparseMatrixfrom the given 1Darraywith compressing (copying) the underlying array. -
from2DArray
Creates a newSparseMatrixfrom the given 2Darraywith compressing (copying) the underlying array. -
block
-
fromCSV
ParsesSparseMatrixfrom the given CSV string.- Parameters:
csv- the CSV string representing a matrix- Returns:
- a parsed matrix
-
fromMatrixMarket
ParsesSparseMatrixfrom the given Matrix Market string.- Parameters:
mm- the string in Matrix Market format- Returns:
- a parsed matrix
-
get
-
getOrElse
public abstract double getOrElse(int i, int j, double defaultValue) Gets the specified element, or adefaultValueif 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
-
getColumn
-
multiply
-
add
-
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:
trueif specified element is zero,falseotherwise
-
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:
trueif specified element is not zero,falseotherwise
-
eachNonZero
Applies givenprocedureto each non-zero element of this matrix.- Parameters:
procedure- the matrix procedure
-
eachNonZeroInRow
Applies the givenprocedureto each non-zero element of the specified row of this matrix.- Parameters:
i- the row index.procedure- theVectorProcedure.
-
eachNonZeroInColumn
Applies the givenprocedureto 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:MatrixConverts this matrix into the Matrix Market string using the given numberformatter.- Specified by:
toMatrixMarketin classMatrix- Returns:
- a string in Matrix Market format representing this matrix;
-
ensureCardinalityIsCorrect
protected void ensureCardinalityIsCorrect(long rows, long columns, long cardinality)
-