Package org.la4j.matrix
Class SparseMatrix
- java.lang.Object
-
- org.la4j.Matrix
-
- org.la4j.matrix.SparseMatrix
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Double>
- Direct Known Subclasses:
ColumnMajorSparseMatrix,RowMajorSparseMatrix
public abstract class SparseMatrix extends Matrix
-
-
Field Summary
Fields Modifier and Type Field Description protected intcardinality
-
Constructor Summary
Constructors Constructor Description SparseMatrix(int rows, int columns)SparseMatrix(int rows, int columns, int cardinality)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Matrixadd(double value)Adds givenvalue(v) to every element of this matrix (A).static SparseMatrixblock(Matrix a, Matrix b, Matrix c, Matrix d)protected longcapacity()intcardinality()Returns 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 SparseMatrixfromCSV(java.lang.String csv)ParsesSparseMatrixfrom the given CSV string.static SparseMatrixfromMatrixMarket(java.lang.String mm)ParsesSparseMatrixfrom the given Matrix Market string.doubleget(int i, int j)Gets the specified element of this matrix.VectorgetColumn(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.VectorgetRow(int i)Copies the specified row of this matrix into the vector.static SparseMatrixidentity(int size)Creates an identitySparseMatrixof the givensize.booleanisColumnMajor()abstract booleanisRowMajor()Checks whether or not this sparse matrix row-major.booleanisZeroAt(int i, int j)Whether or not the specified element is zero.Matrixmultiply(double value)Scales this matrix by givenvalue(v).abstract booleannonZeroAt(int i, int j)Whether or not the specified element is not zero.ColumnMajorMatrixIteratornonZeroColumnMajorIterator()Returns a non-zero column-major matrix iterator.MatrixIteratornonZeroIterator()Returns a non-zero matrix iterator.VectorIteratornonZeroIteratorOfColumn(int j)Returns a non-zero vector iterator of the given columnj.VectorIteratornonZeroIteratorOfRow(int i)Returns a non-zero vector iterator of the given rowi.RowMajorMatrixIteratornonZeroRowMajorIterator()Returns a non-zero row-major matrix iterator.static SparseMatrixrandom(int rows, int columns, double density, java.util.Random random)static SparseMatrixrandomSymmetric(int size, double density, java.util.Random random)Creates a random symmetricSparseMatrixof the givensize.java.lang.StringtoMatrixMarket(java.text.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 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
-
-
-
-
Method Detail
-
zero
public static SparseMatrix zero(int rows, int columns)
-
zero
public static SparseMatrix zero(int rows, int columns, int capacity)
-
diagonal
public static SparseMatrix diagonal(int size, double diagonal)
-
identity
public static SparseMatrix identity(int size)
Creates an identitySparseMatrixof the givensize.
-
random
public static SparseMatrix random(int rows, int columns, double density, java.util.Random random)
-
randomSymmetric
public static SparseMatrix randomSymmetric(int size, double density, java.util.Random random)
Creates a random symmetricSparseMatrixof the givensize.
-
from1DArray
public static SparseMatrix from1DArray(int rows, int columns, double[] array)
Creates a newSparseMatrixfrom the given 1Darraywith compressing (copying) the underlying array.
-
from2DArray
public static SparseMatrix from2DArray(double[][] array)
Creates a newSparseMatrixfrom the given 2Darraywith compressing (copying) the underlying array.
-
block
public static SparseMatrix block(Matrix a, Matrix b, Matrix c, Matrix d)
-
fromCSV
public static SparseMatrix fromCSV(java.lang.String csv)
ParsesSparseMatrixfrom the given CSV string.- Parameters:
csv- the CSV string representing a matrix- Returns:
- a parsed matrix
-
fromMatrixMarket
public static SparseMatrix fromMatrixMarket(java.lang.String mm)
ParsesSparseMatrixfrom 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:MatrixGets the specified element of this matrix.
-
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
public Vector getRow(int i)
Description copied from class:MatrixCopies the specified row of this matrix into the vector.
-
getColumn
public Vector getColumn(int j)
Description copied from class:MatrixCopies the specified column of this matrix into the vector.
-
multiply
public Matrix multiply(double value)
Description copied from class:MatrixScales this matrix by givenvalue(v).
-
add
public Matrix add(double value)
Description copied from class:MatrixAdds 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:
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
public void eachNonZero(MatrixProcedure procedure)
Applies givenprocedureto each non-zero element of this matrix.- Parameters:
procedure- the matrix procedure
-
eachNonZeroInRow
public void eachNonZeroInRow(int i, VectorProcedure procedure)Applies the givenprocedureto each non-zero element of the specified row of this matrix.- Parameters:
i- the row index.procedure- theVectorProcedure.
-
eachNonZeroInColumn
public void eachNonZeroInColumn(int j, VectorProcedure procedure)Applies the givenprocedureto each non-zero element of the specified column of this matrix.- Parameters:
j- the column index.procedure- theVectorProcedure.
-
foldNonZero
public double foldNonZero(MatrixAccumulator accumulator)
Folds non-zero elements of this matrix with givenaccumulator.- Parameters:
accumulator- the matrix accumulator- Returns:
- the accumulated value
-
foldNonZeroInRow
public double foldNonZeroInRow(int i, VectorAccumulator accumulator)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
public double foldNonZeroInColumn(int j, VectorAccumulator accumulator)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
public double[] foldNonZeroInColumns(VectorAccumulator accumulator)
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
public double[] foldNonZeroInRows(VectorAccumulator accumulator)
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
public MatrixIterator nonZeroIterator()
Returns a non-zero matrix iterator.- Returns:
- a non-zero matrix iterator
-
nonZeroRowMajorIterator
public RowMajorMatrixIterator nonZeroRowMajorIterator()
Returns a non-zero row-major matrix iterator.- Returns:
- a non-zero row-major matrix iterator.
-
nonZeroColumnMajorIterator
public ColumnMajorMatrixIterator nonZeroColumnMajorIterator()
Returns a non-zero column-major matrix iterator.- Returns:
- a non-zero column major matrix iterator.
-
nonZeroIteratorOfRow
public VectorIterator nonZeroIteratorOfRow(int i)
Returns a non-zero vector iterator of the given rowi.- Returns:
- a non-zero vector iterator
-
nonZeroIteratorOfColumn
public VectorIterator nonZeroIteratorOfColumn(int j)
Returns a non-zero vector iterator of the given columnj.- Returns:
- a non-zero vector iterator
-
toMatrixMarket
public java.lang.String toMatrixMarket(java.text.NumberFormat formatter)
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)
-
-