Package org.la4j.matrix.sparse
Class CCSMatrix
- java.lang.Object
-
- org.la4j.Matrix
-
- org.la4j.matrix.SparseMatrix
-
- org.la4j.matrix.ColumnMajorSparseMatrix
-
- org.la4j.matrix.sparse.CCSMatrix
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Double>
public class CCSMatrix extends ColumnMajorSparseMatrix
This is a CCS (Compressed Column Storage) matrix class.
-
-
Field Summary
Fields Modifier and Type Field Description private int[]
columnPointers
private static byte
MATRIX_TAG
private static int
MINIMUM_SIZE
private int[]
rowIndices
private double[]
values
-
Fields inherited from class org.la4j.matrix.SparseMatrix
cardinality
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
align(int cardinality)
Matrix
blankOfShape(int rows, int columns)
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rows
xcolumns
.static CCSMatrix
block(Matrix a, Matrix b, Matrix c, Matrix d)
ColumnMajorMatrixIterator
columnMajorIterator()
Returns a column-major matrix iterator.Matrix
copyOfShape(int rows, int columns)
Copies this matrix into the new matrix with specified dimensions:rows
andcolumns
.static CCSMatrix
diagonal(int size, double diagonal)
void
each(MatrixProcedure procedure)
Applies givenprocedure
to each element of this matrix.void
eachInColumn(int j, VectorProcedure procedure)
Applies givenprocedure
to each element of specified column of this matrix.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.static CCSMatrix
from1DArray(int rows, int columns, double[] array)
Creates a newCCSMatrix
from the given 1Darray
with compressing (copying) the underlying array.static CCSMatrix
from2DArray(double[][] array)
Creates a newCCSMatrix
from the given 2Darray
with compressing (copying) the underlying array.static CCSMatrix
fromBinary(byte[] array)
DecodesCCSMatrix
from the given bytearray
.static CCSMatrix
fromCSV(java.lang.String csv)
ParsesCCSMatrix
from the given CSV string.static CCSMatrix
fromMatrixMarket(java.lang.String mm)
ParsesCCSMatrix
from the given Matrix Market string.Vector
getColumn(int j)
Copies the specified column of this matrix into the vector.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.private void
growUp()
static CCSMatrix
identity(int size)
Creates an identityCCSMatrix
of the givensize
.private void
insert(int k, int i, int j, double value)
VectorIterator
iteratorOfColumn(int j)
Returns a vector iterator of the given column {code j}.java.util.Iterator<java.lang.Integer>
iteratorOrNonZeroColumns()
double
max()
Searches for the maximum value of the elements of this matrix.double
maxInColumn(int j)
Searches for the maximum value of specified column in this matrix.double
min()
Searches for the minimum value of the elements of this matrix.double
minInColumn(int j)
Searches for the minimum value of specified column in this matrix.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.VectorIterator
nonZeroIteratorOfColumn(int j)
Returns a non-zero vector iterator of the given columnj
.static CCSMatrix
random(int rows, int columns, double density, java.util.Random random)
static CCSMatrix
randomSymmetric(int size, double density, java.util.Random random)
Creates a random symmetricCCSMatrix
of the givensize
.private void
remove(int k, int j)
private int
searchForRowIndex(int i, int left, int right)
Matrix
select(int[] rowIndices, int[] columnIndices)
Returns a CCSMatrix with the selected rows and columns.void
set(int i, int j, double value)
Sets the specified element of this matrix to givenvalue
.void
setAll(double value)
Sets all elements of this matrix to the givenvalue
.<T extends Matrix>
Tto(MatrixFactory<T> factory)
Converts this matrix using the givenfactory
.byte[]
toBinary()
Encodes this matrix into a byte array.void
updateAt(int i, int j, MatrixFunction function)
Updates the specified element of this matrix by applying givenfunction
.static CCSMatrix
zero(int rows, int columns)
static CCSMatrix
zero(int rows, int columns, int capacity)
-
Methods inherited from class org.la4j.matrix.ColumnMajorSparseMatrix
apply, apply, apply, isRowMajor, iterator, nonZeroIterator, rotate, transpose
-
Methods inherited from class org.la4j.matrix.SparseMatrix
add, capacity, cardinality, density, eachNonZeroInRow, ensureCardinalityIsCorrect, foldNonZero, foldNonZeroInColumn, foldNonZeroInColumns, foldNonZeroInRow, foldNonZeroInRows, get, isColumnMajor, isZeroAt, multiply, nonZeroIteratorOfRow, nonZeroRowMajorIterator, toMatrixMarket
-
Methods inherited from class org.la4j.Matrix
add, blank, blankOfColumns, blankOfRows, columns, constant, copy, copyOfColumns, copyOfRows, determinant, diagonalProduct, divide, eachInRow, ensureDimensionsAreCorrect, ensureIndexesAreInBounds, equals, equals, euclideanNorm, fail, fold, foldColumn, foldColumns, foldRow, foldRows, hadamardProduct, hashCode, infinityNorm, insert, insert, insert, insert, insertColumn, insertRow, is, is, iteratorOfRow, kroneckerProduct, manhattanNorm, maxInRow, minInRow, mkString, mkString, mkString, multiply, multiply, multiplyByItsTranspose, non, non, norm, power, product, random, randomSymmetric, rank, removeColumn, removeFirstColumn, removeFirstRow, removeLastColumn, removeLastRow, removeRow, rowMajorIterator, rows, setColumn, setColumn, setRow, setRow, shuffle, slice, sliceBottomRight, sliceTopLeft, subtract, subtract, sum, swapColumns, swapRows, toColumnMajorSparseMatrix, toColumnVector, toCSV, toCSV, toDenseMatrix, toMatrixMarket, toRowMajorSparseMatrix, toRowVector, toSparseMatrix, toString, trace, transform, transformColumn, transformRow, unit, update, updateColumn, updateRow, withDecompositor, withInverter, withSolver
-
-
-
-
Field Detail
-
MATRIX_TAG
private static final byte MATRIX_TAG
- See Also:
- Constant Field Values
-
MINIMUM_SIZE
private static final int MINIMUM_SIZE
- See Also:
- Constant Field Values
-
values
private double[] values
-
rowIndices
private int[] rowIndices
-
columnPointers
private int[] columnPointers
-
-
Method Detail
-
zero
public static CCSMatrix zero(int rows, int columns)
-
zero
public static CCSMatrix zero(int rows, int columns, int capacity)
-
diagonal
public static CCSMatrix diagonal(int size, double diagonal)
-
identity
public static CCSMatrix identity(int size)
Creates an identityCCSMatrix
of the givensize
.
-
random
public static CCSMatrix random(int rows, int columns, double density, java.util.Random random)
-
randomSymmetric
public static CCSMatrix randomSymmetric(int size, double density, java.util.Random random)
Creates a random symmetricCCSMatrix
of the givensize
.
-
from1DArray
public static CCSMatrix from1DArray(int rows, int columns, double[] array)
Creates a newCCSMatrix
from the given 1Darray
with compressing (copying) the underlying array.
-
from2DArray
public static CCSMatrix from2DArray(double[][] array)
Creates a newCCSMatrix
from the given 2Darray
with compressing (copying) the underlying array.
-
fromBinary
public static CCSMatrix fromBinary(byte[] array)
DecodesCCSMatrix
from the given bytearray
.- Parameters:
array
- the byte array representing a matrix- Returns:
- a decoded matrix
-
fromCSV
public static CCSMatrix fromCSV(java.lang.String csv)
ParsesCCSMatrix
from the given CSV string.- Parameters:
csv
- the CSV string representing a matrix- Returns:
- a parsed matrix
-
fromMatrixMarket
public static CCSMatrix fromMatrixMarket(java.lang.String mm)
ParsesCCSMatrix
from the given Matrix Market string.- Parameters:
mm
- the string in Matrix Market format- Returns:
- a parsed matrix
-
getOrElse
public double getOrElse(int i, int j, double defaultValue)
Description copied from class:SparseMatrix
Gets the specified element, or adefaultValue
if there is no actual element at (i
,j
) in this sparse matrix.- Specified by:
getOrElse
in classSparseMatrix
- 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
-
set
public void set(int i, int j, double value)
Description copied from class:Matrix
Sets the specified element of this matrix to givenvalue
.
-
setAll
public void setAll(double value)
Description copied from class:Matrix
Sets all elements of this matrix to the givenvalue
.
-
getColumn
public Vector getColumn(int j)
Description copied from class:Matrix
Copies the specified column of this matrix into the vector.- Overrides:
getColumn
in classSparseMatrix
- Parameters:
j
- the column index- Returns:
- the column represented as vector
-
getRow
public Vector getRow(int i)
Description copied from class:Matrix
Copies the specified row of this matrix into the vector.- Overrides:
getRow
in classSparseMatrix
- Parameters:
i
- the row index- Returns:
- the row represented as vector
-
copyOfShape
public Matrix copyOfShape(int rows, int columns)
Description copied from class:Matrix
Copies this matrix into the new matrix with specified dimensions:rows
andcolumns
.- Specified by:
copyOfShape
in classMatrix
- Parameters:
rows
- the number of rows in new matrixcolumns
- the number of columns in new matrix- Returns:
- the copy of this matrix with new size
-
eachNonZero
public void eachNonZero(MatrixProcedure procedure)
Description copied from class:SparseMatrix
Applies givenprocedure
to each non-zero element of this matrix.- Overrides:
eachNonZero
in classSparseMatrix
- Parameters:
procedure
- the matrix procedure
-
each
public void each(MatrixProcedure procedure)
Description copied from class:Matrix
Applies givenprocedure
to each element of this matrix.
-
eachInColumn
public void eachInColumn(int j, VectorProcedure procedure)
Description copied from class:Matrix
Applies givenprocedure
to each element of specified column of this matrix.- Overrides:
eachInColumn
in classMatrix
- Parameters:
j
- the column indexprocedure
- the vector procedure
-
eachNonZeroInColumn
public void eachNonZeroInColumn(int j, VectorProcedure procedure)
Description copied from class:SparseMatrix
Applies the givenprocedure
to each non-zero element of the specified column of this matrix.- Overrides:
eachNonZeroInColumn
in classSparseMatrix
- Parameters:
j
- the column index.procedure
- theVectorProcedure
.
-
updateAt
public void updateAt(int i, int j, MatrixFunction function)
Description copied from class:Matrix
Updates the specified element of this matrix by applying givenfunction
.
-
nonZeroAt
public boolean nonZeroAt(int i, int j)
Description copied from class:SparseMatrix
Whether or not the specified element is not zero.- Specified by:
nonZeroAt
in classSparseMatrix
- Parameters:
i
- element's row indexj
- element's column index- Returns:
true
if specified element is not zero,false
otherwise
-
searchForRowIndex
private int searchForRowIndex(int i, int left, int right)
-
insert
private void insert(int k, int i, int j, double value)
-
remove
private void remove(int k, int j)
-
growUp
private void growUp()
-
align
private int align(int cardinality)
-
max
public double max()
Description copied from class:Matrix
Searches for the maximum value of the elements of this matrix.
-
min
public double min()
Description copied from class:Matrix
Searches for the minimum value of the elements of this matrix.
-
maxInColumn
public double maxInColumn(int j)
Description copied from class:Matrix
Searches for the maximum value of specified column in this matrix.- Overrides:
maxInColumn
in classMatrix
- Parameters:
j
- the column index- Returns:
- maximum value of specified column in this matrix
-
minInColumn
public double minInColumn(int j)
Description copied from class:Matrix
Searches for the minimum value of specified column in this matrix.- Overrides:
minInColumn
in classMatrix
- Parameters:
j
- the column index- Returns:
- minimum value of specified column in this matrix
-
select
public Matrix select(int[] rowIndices, int[] columnIndices)
Returns a CCSMatrix with the selected rows and columns.
-
to
public <T extends Matrix> T to(MatrixFactory<T> factory)
Description copied from class:Matrix
Converts this matrix using the givenfactory
.
-
blankOfShape
public Matrix blankOfShape(int rows, int columns)
Description copied from class:Matrix
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rows
xcolumns
.- Specified by:
blankOfShape
in classMatrix
- Returns:
- blank matrix
-
iteratorOrNonZeroColumns
public java.util.Iterator<java.lang.Integer> iteratorOrNonZeroColumns()
- Specified by:
iteratorOrNonZeroColumns
in classColumnMajorSparseMatrix
-
columnMajorIterator
public ColumnMajorMatrixIterator columnMajorIterator()
Description copied from class:Matrix
Returns a column-major matrix iterator.- Overrides:
columnMajorIterator
in classMatrix
- Returns:
- a column-major matrix iterator.
-
nonZeroColumnMajorIterator
public ColumnMajorMatrixIterator nonZeroColumnMajorIterator()
Description copied from class:SparseMatrix
Returns a non-zero column-major matrix iterator.- Overrides:
nonZeroColumnMajorIterator
in classSparseMatrix
- Returns:
- a non-zero column major matrix iterator.
-
nonZeroIteratorOfColumn
public VectorIterator nonZeroIteratorOfColumn(int j)
Description copied from class:SparseMatrix
Returns a non-zero vector iterator of the given columnj
.- Overrides:
nonZeroIteratorOfColumn
in classSparseMatrix
- Returns:
- a non-zero vector iterator
-
iteratorOfColumn
public VectorIterator iteratorOfColumn(int j)
Description copied from class:Matrix
Returns a vector iterator of the given column {code j}.- Overrides:
iteratorOfColumn
in classMatrix
- Returns:
- a vector iterator
-
-