Class SparseStore<N extends Comparable<N>>
java.lang.Object
org.ojalgo.matrix.store.AbstractStore<N>
org.ojalgo.matrix.store.FactoryStore<N>
org.ojalgo.matrix.store.SparseStore<N>
- All Implemented Interfaces:
Consumer<Access2D<?>>, Group, Group.Additive<MatrixStore<N>>, NormedVectorSpace<MatrixStore<N>, N>, Operation, Operation.Addition<MatrixStore<N>>, Operation.Multiplication<MatrixStore<N>>, Operation.Subtraction<MatrixStore<N>>, ScalarOperation, ScalarOperation.Addition<MatrixStore<N>, N>, ScalarOperation.Division<MatrixStore<N>, N>, ScalarOperation.Multiplication<MatrixStore<N>, N>, ScalarOperation.Subtraction<MatrixStore<N>, N>, VectorSpace<MatrixStore<N>, N>, Matrix2D<N, MatrixStore<N>>, ElementsSupplier<N>, MatrixStore<N>, SparseStructure2D, TransformableRegion<N>, Access1D<N>, Access1D.Aggregatable<N>, Access1D.Collectable<N,Mutate1D>, Access1D.Sliceable<N>, Access1D.Visitable<N>, Access2D<N>, Access2D.Aggregatable<N>, Access2D.Collectable<N, TransformableRegion<N>>, Access2D.Sliceable<N>, Access2D.Visitable<N>, Mutate1D, Mutate1D.Fillable<N>, Mutate1D.Modifiable<N>, Mutate2D, Mutate2D.Exchangeable, Mutate2D.Fillable<N>, Mutate2D.Modifiable<N>, Mutate2D.ModifiableReceiver<N>, Mutate2D.Receiver<N>, Operate2D<N, ElementsSupplier<N>>, Structure1D, Structure2D, Structure2D.Logical<Access2D<N>, MatrixStore<N>>, Structure2D.ReducibleTo1D<ElementsSupplier<N>>
public final class SparseStore<N extends Comparable<N>>
extends FactoryStore<N>
implements TransformableRegion<N>, SparseStructure2D
A sparse matrix (this implementation) is not thread safe. Mutating the matrix (in particular creating
new/more non-zero elements) from multiple threads will result in unpredictable behaviour (even if the
threads update different elements). To mutate the matrix in a concurrent/multi-threaded environment you
need to do one of the following:
- Use the
TransformableRegion.synchronised()method to create a synchronised wrapper of the matrix. - Use the
SparseStore.Builderto build the matrix.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classSparseStore.Builder<N extends Comparable<N>>May be a preferable way to build a sparse matrix if: You don't know the number of nonzero elements in advance.static final classSparseStore.Factory<N extends Comparable<N>>Nested classes/interfaces inherited from interface Access2D
Access2D.Aggregatable<N>, Access2D.Collectable<N,R>, Access2D.ColumnView<N>, Access2D.ElementView<N>, Access2D.RowView<N>, Access2D.SelectionView<N>, Access2D.Sliceable<N>, Access2D.Visitable<N> Nested classes/interfaces inherited from interface Group
Group.Additive<T>, Group.Multiplicative<T>Nested classes/interfaces inherited from interface Mutate1D
Mutate1D.Fillable<N>, Mutate1D.Mixable<N>, Mutate1D.Modifiable<N>, Mutate1D.ModifiableReceiver<N>, Mutate1D.Receiver<N>, Mutate1D.SortableNested classes/interfaces inherited from interface Mutate2D
Mutate2D.Exchangeable, Mutate2D.Fillable<N>, Mutate2D.Mixable<N>, Mutate2D.Modifiable<N>, Mutate2D.ModifiableReceiver<N>, Mutate2D.Receiver<N>Nested classes/interfaces inherited from interface Operation
Operation.Addition<T>, Operation.Division<T>, Operation.Multiplication<T>, Operation.Subtraction<T>Nested classes/interfaces inherited from interface ScalarOperation
ScalarOperation.Addition<T,N>, ScalarOperation.Division<T, N>, ScalarOperation.Multiplication<T, N>, ScalarOperation.Subtraction<T, N> Nested classes/interfaces inherited from interface Structure1D
Structure1D.BasicMapper<T>, Structure1D.IndexMapper<T>, Structure1D.IntIndex, Structure1D.Logical<S,B>, Structure1D.LongIndex, Structure1D.LoopCallback Nested classes/interfaces inherited from interface Structure2D
Structure2D.IntRowColumn, Structure2D.Logical<S,B>, Structure2D.LongRowColumn, Structure2D.ReducibleTo1D<R>, Structure2D.Reshapable, Structure2D.RowColumnKey<R, C>, Structure2D.RowColumnMapper<R, C> Nested classes/interfaces inherited from interface TransformableRegion
TransformableRegion.FillByMultiplying<N> -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final SparseStore.Factory<ComplexNumber> static final SparseStore.Factory<Quaternion> private final SparseArray<N> private final int[]private final int[]private final TransformableRegion.FillByMultiplying<N> static final SparseStore.Factory<RationalNumber> static final SparseStore.Factory<Double> static final SparseStore.Factory<Double> static final SparseStore.Factory<Quadruple> -
Constructor Summary
ConstructorsConstructorDescriptionSparseStore(PhysicalStore.Factory<N, ?> factory, int nbRows, int nbCols, IntBinaryOperator initial) -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int row, int col, double addend) voidadd(long row, long col, double addend) voidadd(long row, long col, Comparable<?> addend) intprivate static <N extends Scalar<N>>
voiddoGenericColumnAXPY(SparseArray<N> elements, long colX, long colY, N a, TransformableRegion<N> y) private static voiddoPrimitiveColumnAXPY(SparseArray<Double> elements, long colX, long colY, double a, TransformableRegion<Double> y) doubledoubleValue(int row, int col) Extracts one element of this matrix as a double.booleanstatic <N extends Comparable<N>>
SparseStore.Factory<N> factory(PhysicalStore.Factory<N, ?> physicalFactory) voidfillByMultiplying(Access1D<N> left, Access1D<N> right) intfirstInColumn(int col) The default value is simply0, and if all elements are zeros thenthis.countRows().intfirstInRow(int row) The default value is simply0, and if all elements are zeros thenthis.countColumns().get(int row, int col) (package private) SparseArray<N> inthashCode()longintlimitOfColumn(int col) The default value is simplythis.countRows(), and if all elements are zeros then0.intlimitOfRow(int row) The default value is simplythis.countColumns(), and if all elements are zeros then0.voidmodifyAll(UnaryFunction<N> modifier) voidmodifyMatching(BinaryFunction<N> function, Access1D<N> right) voidmodifyMatching(Access1D<N> left, BinaryFunction<N> function) voidmodifyOne(long row, long col, UnaryFunction<N> modifier) multiply(double scalar) multiply(MatrixStore<N> right) (package private) static <N extends Comparable<N>>
voidmultiply(SparseStore<N> left, SparseStore<N> right, TransformableRegion<N> target) voidmultiply(Access1D<N> right, TransformableRegion<N> target) multiplyBoth(Access1D<N> leftAndRight) Assumes [leftAndRight] is a vector and will calulate [leftAndRight]H[this][leftAndRight]ElementView2D<N, ?> nonzeros()Similar toAccess1D.elements()but avoids elements that are structurally known to be zero.premultiply(Access1D<N> left) Thepremultiplymethod differs frommultiplyin 3 ways: The matrix positions are swapped - left/right. It does NOT return a MatrixStore but an ElementsSupplier instead. It accepts an Access1D as the argument left matrix.voidreduceColumns(Aggregator aggregator, Mutate1D receiver) voidreduceRows(Aggregator aggregator, Mutate1D receiver) regionByColumns(int... columns) regionByLimits(int rowLimit, int columnLimit) regionByOffsets(int rowOffset, int columnOffset) regionByRows(int... rows) voidreset()Reset this mutable structure to some standard (all zeros) initial state.voidset(int row, int col, double value) voidset(long row, long col, Comparable<?> value) voidsupplyTo(TransformableRegion<N> receiver) toCSC()toCSR()(package private) voidupdateNonZeros(int row, int col) private voidupdateNonZeros(long row, long col) voidvisitColumn(long row, long col, VoidFunction<N> visitor) voidvisitRow(long row, long col, VoidFunction<N> visitor) Methods inherited from class FactoryStore
one, physical, zeroMethods inherited from class AbstractStore
buildColumn, buildColumn, buildColumn, buildRow, buildRow, buildRow, cast, countColumns, countRows, getColDim, getComponentType, getMaxDim, getMinDim, getRowDim, isPrimitive, toStringMethods inherited from interface Access1D
asCollectable1D, asKeyed1D, asList, axpy, dot, select, supplyTo, toList, toRawCopy1DMethods inherited from interface Access1D.Collectable
collectMethods inherited from interface Access1D.Visitable
visitAll, visitRangeMethods inherited from interface Access2D
asCollectable2D, asKeyed2D, byteValue, byteValue, byteValue, byteValue, columns, doubleValue, doubleValue, doubleValue, elements, floatValue, floatValue, floatValue, floatValue, get, intValue, intValue, intValue, intValue, longValue, longValue, longValue, longValue, rows, shortValue, shortValue, shortValue, shortValue, toRawCopy2DMethods inherited from interface Access2D.Aggregatable
aggregateColumn, aggregateDiagonal, aggregateRowMethods inherited from interface Access2D.Collectable
collectMethods inherited from interface Access2D.Sliceable
sliceColumn, sliceDiagonal, sliceRowMethods inherited from interface Access2D.Visitable
visitColumn, visitDiagonal, visitDiagonal, visitOne, visitRowMethods inherited from interface ElementsSupplier
onAny, onColumns, onColumns, onMatching, onMatching, onRows, onRowsMethods inherited from interface MatrixStore
above, above, above, add, add, add, aggregateAll, aggregateColumn, aggregateDiagonal, aggregateRange, aggregateRow, below, below, below, bidiagonal, column, column, columns, columns, conjugate, copy, diagonal, diagonally, divide, divide, equals, get, get, hermitian, hessenberg, isHermitian, isNormal, isSmall, isSmall, left, left, left, limits, logical, negate, norm, offsets, onAll, operate, power, reduceColumns, reduceRows, repeat, right, right, right, row, row, rows, rows, select, select, signum, sliceColumn, sliceDiagonal, sliceRange, sliceRow, subtract, subtract, subtract, superimpose, superimpose, supplyTo, symmetric, toScalar, transpose, triangular, tridiagonal, visitOneMethods inherited from interface Mutate1D.Fillable
fillAll, fillAll, fillCompatible, fillMatching, fillMatching, fillMatching, fillMatching, fillRange, fillRangeMethods inherited from interface Mutate1D.Modifiable
add, add, add, add, add, modifyRangeMethods inherited from interface Mutate2D
set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, set, setMethods inherited from interface Mutate2D.Fillable
fillColumn, fillColumn, fillColumn, fillColumn, fillColumn, fillColumn, fillCompatible, fillDiagonal, fillDiagonal, fillDiagonal, fillDiagonal, fillDiagonal, fillDiagonal, fillRow, fillRow, fillRow, fillRow, fillRow, fillRowMethods inherited from interface Mutate2D.Modifiable
add, add, add, add, add, add, add, add, add, add, add, add, add, modifyColumn, modifyColumn, modifyDiagonal, modifyDiagonal, modifyMatchingInColumns, modifyMatchingInColumns, modifyMatchingInRows, modifyMatchingInRows, modifyOne, modifyRow, modifyRowMethods inherited from interface Mutate2D.ModifiableReceiver
modifyCompatible, modifyCompatibleMethods inherited from interface Mutate2D.Receiver
accept, isAcceptableMethods inherited from interface Operate2D
onAll, onAll, onAll, onAll, onAll, onCompatible, onCompatibleMethods inherited from interface ScalarOperation.Addition
addMethods inherited from interface ScalarOperation.Division
divideMethods inherited from interface ScalarOperation.Multiplication
multiplyMethods inherited from interface ScalarOperation.Subtraction
subtractMethods inherited from interface SparseStructure2D
densityMethods inherited from interface Structure2D
count, countColumns, countRows, getColDim, getMaxDim, getMinDim, getRowDim, isEmpty, isFat, isScalar, isSquare, isTall, isVector, sizeMethods inherited from interface TransformableRegion
exchangeColumns, exchangeRows, modifyAny, synchronised
-
Field Details
-
C128
-
H256
-
Q128
-
R032
-
R064
-
R128
-
myElements
-
myFirsts
private final int[] myFirsts -
myLimits
private final int[] myLimits -
myMultiplier
-
-
Constructor Details
-
SparseStore
SparseStore(PhysicalStore.Factory<N, ?> factory, int nbRows, int nbCols, IntBinaryOperator initial)
-
-
Method Details
-
factory
public static <N extends Comparable<N>> SparseStore.Factory<N> factory(PhysicalStore.Factory<N, ?> physicalFactory) -
doGenericColumnAXPY
private static <N extends Scalar<N>> void doGenericColumnAXPY(SparseArray<N> elements, long colX, long colY, N a, TransformableRegion<N> y) -
doPrimitiveColumnAXPY
private static void doPrimitiveColumnAXPY(SparseArray<Double> elements, long colX, long colY, double a, TransformableRegion<Double> y) -
multiply
static <N extends Comparable<N>> void multiply(SparseStore<N> left, SparseStore<N> right, TransformableRegion<N> target) -
add
public void add(int row, int col, double addend) - Specified by:
addin interfaceMutate2D.Modifiable<N extends Comparable<N>>
-
add
- Specified by:
addin interfaceMutate2D.Modifiable<N extends Comparable<N>>
-
add
public void add(long row, long col, double addend) - Specified by:
addin interfaceMutate2D.Modifiable<N extends Comparable<N>>
-
countNonzeros
public int countNonzeros()- Specified by:
countNonzerosin interfaceSparseStructure2D
-
doubleValue
public double doubleValue(int row, int col) Description copied from interface:Access2DExtracts one element of this matrix as a double.- Specified by:
doubleValuein interfaceAccess2D<N extends Comparable<N>>- Specified by:
doubleValuein interfaceMatrixStore<N extends Comparable<N>>- Parameters:
row- A row index.col- A column index.- Returns:
- One matrix element
-
equals
- Overrides:
equalsin classFactoryStore<N extends Comparable<N>>
-
fillByMultiplying
- Specified by:
fillByMultiplyingin interfaceTransformableRegion<N extends Comparable<N>>
-
firstInColumn
public int firstInColumn(int col) Description copied from interface:Structure2DThe default value is simply0, and if all elements are zeros thenthis.countRows().- Specified by:
firstInColumnin interfaceStructure2D- Parameters:
col- The column index- Returns:
- The row index of the first non-zero element in the specified column
-
firstInRow
public int firstInRow(int row) Description copied from interface:Structure2DThe default value is simply0, and if all elements are zeros thenthis.countColumns().- Specified by:
firstInRowin interfaceStructure2D- Returns:
- The column index of the first non-zero element in the specified row
-
get
- Specified by:
getin interfaceMatrixStore<N extends Comparable<N>>
-
hashCode
public int hashCode()- Overrides:
hashCodein classFactoryStore<N extends Comparable<N>>
-
indexOfLargest
public long indexOfLargest()- Specified by:
indexOfLargestin interfaceAccess1D.Aggregatable<N extends Comparable<N>>- Specified by:
indexOfLargestin interfaceMatrixStore<N extends Comparable<N>>
-
limitOfColumn
public int limitOfColumn(int col) Description copied from interface:Structure2DThe default value is simplythis.countRows(), and if all elements are zeros then0.- Specified by:
limitOfColumnin interfaceStructure2D- Overrides:
limitOfColumnin classAbstractStore<N extends Comparable<N>>- Returns:
- The row index of the first zero element, after all non-zeros, in the specified column (index of the last non-zero + 1)
-
limitOfRow
public int limitOfRow(int row) Description copied from interface:Structure2DThe default value is simplythis.countColumns(), and if all elements are zeros then0.- Specified by:
limitOfRowin interfaceStructure2D- Overrides:
limitOfRowin classAbstractStore<N extends Comparable<N>>- Returns:
- The column index of the first zero element, after all non-zeros, in the specified row (index of the last non-zero + 1)
-
modifyAll
- Specified by:
modifyAllin interfaceMutate1D.Modifiable<N extends Comparable<N>>
-
modifyMatching
- Specified by:
modifyMatchingin interfaceMutate1D.Modifiable<N extends Comparable<N>>
-
modifyMatching
- Specified by:
modifyMatchingin interfaceMutate1D.Modifiable<N extends Comparable<N>>
-
modifyOne
- Specified by:
modifyOnein interfaceMutate2D.Modifiable<N extends Comparable<N>>
-
multiply
- Specified by:
multiplyin interfaceMatrixStore<N extends Comparable<N>>
-
multiply
- Specified by:
multiplyin interfaceMatrixStore<N extends Comparable<N>>- Specified by:
multiplyin interfaceScalarOperation.Multiplication<MatrixStore<N extends Comparable<N>>, N extends Comparable<N>>- Returns:
this * scalarMultiplicand.
-
multiply
- Specified by:
multiplyin interfaceMatrixStore<N extends Comparable<N>>- Specified by:
multiplyin interfaceOperation.Multiplication<N extends Comparable<N>>- Parameters:
right- The multiplicand- Returns:
this * multiplicand.
-
multiply
- Specified by:
multiplyin interfaceMatrixStore<N extends Comparable<N>>- Specified by:
multiplyin interfaceScalarOperation.Multiplication<MatrixStore<N extends Comparable<N>>, N extends Comparable<N>>- Returns:
this * multiplicand.
-
multiplyBoth
Description copied from interface:MatrixStoreAssumes [leftAndRight] is a vector and will calulate [leftAndRight]H[this][leftAndRight]- Specified by:
multiplyBothin interfaceMatrixStore<N extends Comparable<N>>- Overrides:
multiplyBothin classAbstractStore<N extends Comparable<N>>- Parameters:
leftAndRight- The argument vector- Returns:
- A scalar (extracted from the resulting 1 x 1 matrix)
-
nonzeros
Description copied from interface:Access1DSimilar toAccess1D.elements()but avoids elements that are structurally known to be zero. (That does not eliminate all zero-values from this view.) With an arbitrary (dense) unstructured implementation theAccess1D.nonzeros()andAccess1D.elements()methods do the same thing! Only some specific implementations are able to actually exploit structure/sparsity to view fewer elements.- Specified by:
nonzerosin interfaceAccess1D<N extends Comparable<N>>- Specified by:
nonzerosin interfaceAccess2D<N extends Comparable<N>>
-
premultiply
Description copied from interface:MatrixStoreThepremultiplymethod differs frommultiplyin 3 ways:- The matrix positions are swapped - left/right.
- It does NOT return a MatrixStore but an ElementsSupplier instead.
- It accepts an Access1D as the argument left matrix.
- Specified by:
premultiplyin interfaceMatrixStore<N extends Comparable<N>>- Parameters:
left- The left matrix- Returns:
- The matrix product
-
reduceColumns
- Specified by:
reduceColumnsin interfaceAccess2D.Aggregatable<N extends Comparable<N>>
-
reduceRows
- Specified by:
reduceRowsin interfaceAccess2D.Aggregatable<N extends Comparable<N>>
-
regionByColumns
- Specified by:
regionByColumnsin interfaceTransformableRegion<N extends Comparable<N>>- Returns:
- A consumer (sub)region
-
regionByLimits
- Specified by:
regionByLimitsin interfaceTransformableRegion<N extends Comparable<N>>- Returns:
- A consumer (sub)region
-
regionByOffsets
- Specified by:
regionByOffsetsin interfaceTransformableRegion<N extends Comparable<N>>- Returns:
- A consumer (sub)region
-
regionByRows
- Specified by:
regionByRowsin interfaceTransformableRegion<N extends Comparable<N>>- Returns:
- A consumer (sub)region
-
regionByTransposing
- Specified by:
regionByTransposingin interfaceTransformableRegion<N extends Comparable<N>>- Returns:
- A transposed consumer region
-
reset
-
set
-
set
-
supplyTo
- Specified by:
supplyToin interfaceAccess2D.Collectable<N extends Comparable<N>, TransformableRegion<N extends Comparable<N>>>- Specified by:
supplyToin interfaceMatrixStore<N extends Comparable<N>>
-
toCSC
- Specified by:
toCSCin interfaceSparseStructure2D
-
toCSR
- Specified by:
toCSRin interfaceSparseStructure2D
-
visitColumn
- Specified by:
visitColumnin interfaceAccess2D.Visitable<N extends Comparable<N>>
-
visitRow
- Specified by:
visitRowin interfaceAccess2D.Visitable<N extends Comparable<N>>
-
updateNonZeros
private void updateNonZeros(long row, long col) -
getElements
SparseArray<N> getElements() -
updateNonZeros
void updateNonZeros(int row, int col)
-