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 int
cardinality
-
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 Matrix
add(double value)
Adds givenvalue
(v) to every element of this matrix (A).static SparseMatrix
block(Matrix a, Matrix b, Matrix c, Matrix d)
protected long
capacity()
int
cardinality()
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
fromCSV(java.lang.String csv)
ParsesSparseMatrix
from the given CSV string.static SparseMatrix
fromMatrixMarket(java.lang.String mm)
ParsesSparseMatrix
from the given Matrix Market string.double
get(int i, int j)
Gets the specified element of this matrix.Vector
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.Vector
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
isColumnMajor()
abstract boolean
isRowMajor()
Checks whether or not this sparse matrix row-major.boolean
isZeroAt(int i, int j)
Whether or not the specified element is zero.Matrix
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.ColumnMajorMatrixIterator
nonZeroColumnMajorIterator()
Returns a non-zero column-major matrix iterator.MatrixIterator
nonZeroIterator()
Returns a non-zero matrix iterator.VectorIterator
nonZeroIteratorOfColumn(int j)
Returns a non-zero vector iterator of the given columnj
.VectorIterator
nonZeroIteratorOfRow(int i)
Returns a non-zero vector iterator of the given rowi
.RowMajorMatrixIterator
nonZeroRowMajorIterator()
Returns a non-zero row-major matrix iterator.static SparseMatrix
random(int rows, int columns, double density, java.util.Random random)
static SparseMatrix
randomSymmetric(int size, double density, java.util.Random random)
Creates a random symmetricSparseMatrix
of the givensize
.java.lang.String
toMatrixMarket(java.text.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
-
-
-
-
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 identitySparseMatrix
of 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 symmetricSparseMatrix
of the givensize
.
-
from1DArray
public static SparseMatrix from1DArray(int rows, int columns, double[] array)
Creates a newSparseMatrix
from the given 1Darray
with compressing (copying) the underlying array.
-
from2DArray
public static SparseMatrix from2DArray(double[][] array)
Creates a newSparseMatrix
from the given 2Darray
with 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)
ParsesSparseMatrix
from 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)
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
public Vector getRow(int i)
Description copied from class:Matrix
Copies the specified row of this matrix into the vector.
-
getColumn
public Vector getColumn(int j)
Description copied from class:Matrix
Copies the specified column of this matrix into the vector.
-
multiply
public Matrix multiply(double value)
Description copied from class:Matrix
Scales this matrix by givenvalue
(v).
-
add
public Matrix add(double value)
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
public void eachNonZero(MatrixProcedure procedure)
Applies givenprocedure
to each non-zero element of this matrix.- Parameters:
procedure
- the matrix procedure
-
eachNonZeroInRow
public void eachNonZeroInRow(int i, VectorProcedure procedure)
Applies the givenprocedure
to 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 givenprocedure
to 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: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)
-
-