Class CooGetSet
java.lang.Object
org.flag4j.linalg.ops.sparse.coo.CooGetSet
A utility class that aids in getting or setting specified elements of a sparse COO tensor, matrix, or vector.
All methods in this class guarantee all results will be properly lexicographically sorted by indices.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidcooInsertNewValue(T value, int[] index, T[] srcEntries, int[][] srcIndices, int insertionPoint, T[] destEntries, int[][] destIndices) Inserts a new value into a sparse COO tensor.static <T> voidcooInsertNewValue(T value, int rowIdx, int colIdx, T[] srcEntries, int[] srcRowIndices, int[] srcColIndices, int insertionPoint, T[] destEntries, int[] destRowIndices, int[] destColIndices) Inserts a new value into a sparse COO matrix.static <T> voidcooInsertNewValue(T value, int index, T[] srcEntries, int[] srcIndices, int insertionPoint, T[] destEntries, int[] destIndices) Inserts a new value into a sparse COO vector.static <T> SparseVectorData<T> getCol(Shape shape, T[] entries, int[] rowIndices, int[] colIndices, int colIdx, int start, int end) Gets a specified column of a COO matrix betweenstart(inclusive) andend(exclusive).static doublegetCoo(double[] entries, int[][] indices, int[] target) Gets an element of a sparse COO tensor at the specified index.static floatgetCoo(float[] entries, int[][] indices, int[] target) Gets an element of a sparse COO tensor at the specified index.static intgetCoo(int[] entries, int[][] indices, int[] target) Gets an element of a sparse COO tensor at the specified index.static <T> TgetCoo(T[] entries, int[][] indices, int[] target) Gets an element of a sparse COO tensor at the specified index.static <V> VgetCoo(V[] entries, int[] indices, int index) Gets the specified element from a sparse COO vector.static <V> VgetCoo(V[] entries, int[] rowIndices, int[] colIndices, int row, int col) Gets the specified element from a sparse COO matrix.static <T> SparseVectorData<T> Gets the elements of a COO matrix along the specified diagonal.static <T> SparseVectorData<T> getRow(Shape shape, T[] entries, int[] rowIndices, int[] colIndices, int rowIdx, int start, int end) Gets a specified row of a COO matrix betweenstart(inclusive) andend(exclusive).static <T> SparseMatrixData<T> getSlice(Shape shape, T[] entries, int[] rowIndices, int[] colIndices, int rowStart, int rowEnd, int colStart, int colEnd) Extracts a specified slice from a sparse COO matrix.static <T> SparseMatrixData<T> Gets the lower-triangular portion of a sparse COO matrix with a possible diagonal offset.static <T> SparseMatrixData<T> Gets the upper-triangular portion of a sparse COO matrix with a possible diagonal offset.static <T> SparseMatrixData<T> setCol(Shape srcShape, T[] srcEntries, int[] rowIndices, int[] colIndices, int colIdx, int size, T[] col, int[] indices) Sets a column of a sparse matrix to the values in a sparse tensor.static <T> SparseMatrixData<T> setRow(Shape srcShape, T[] srcEntries, int[] rowIndices, int[] colIndices, int rowIdx, int size, T[] row, int[] indices) Sets a specified row of a real sparse COO matrix to the values in a sparse COO vector.static <T> SparseMatrixData<T> setSlice(Shape shape1, T[] src1Entries, int[] src1RowIndices, int[] src1ColIndices, Shape shape2, T[] src2Entries, int[] src2RowIndices, int[] src2ColIndices, int row, int col) Copies a sparse matrix and sets a slice of the sparse COO matrix to the data of another sparse COO matrix.
-
Method Details
-
setRow
public static <T> SparseMatrixData<T> setRow(Shape srcShape, T[] srcEntries, int[] rowIndices, int[] colIndices, int rowIdx, int size, T[] row, int[] indices) Sets a specified row of a real sparse COO matrix to the values in a sparse COO vector.- Parameters:
srcShape- Shape of the matrix to set row in.srcEntries- Non-zero data of the COO matrix.rowIndices- Non-zero row indices of the COO matrix.colIndices- Non-zero column indices of the COO matrix.rowIdx- Index of the row to set.size- Full size of the COO vector.row- Non-zero data of the COO vector containing new row values.indices- Non-zero indices of the COO vector containing new row values.- Returns:
- Sparse matrix data containing the data for the COO matrix resulting from setting the specified row of the provided COO matrix to the provided COO vector.
- Throws:
IllegalArgumentException- IfsrcShape.get(1) != size.
-
setCol
public static <T> SparseMatrixData<T> setCol(Shape srcShape, T[] srcEntries, int[] rowIndices, int[] colIndices, int colIdx, int size, T[] col, int[] indices) Sets a column of a sparse matrix to the values in a sparse tensor.- Parameters:
srcShape- Shape of the matrix to set column in.srcEntries- Non-zero data of the COO matrix.rowIndices- Non-zero row indices of the COO matrix.colIndices- Non-zero column indices of the COO matrix.colIdx- Index of the column to set.size- Full size of the COO vector.col- Non-zero data of the COO vector containing new column values.indices- Non-zero indices of the COO vector containing new column values.- Returns:
- A copy of the
srcmatrix with the specified column set to thecolsparse vector. - Throws:
IllegalArgumentException- If thesrcmatrix does not have the same number of rows as total data in thecolvector.
-
getCoo
public static <V> V getCoo(V[] entries, int[] indices, int index) Gets the specified element from a sparse COO vector.- Parameters:
entries- Non-zero values of the sparse COO vector.indices- Non-zero indices of the sparse COO vector.index- Index of the value to get from the vector.- Returns:
- The value in the sparse COO vector at the specified index if it exists. If the value is not found
within the non-zero data,
nullwill be returned.
-
getCoo
public static <V> V getCoo(V[] entries, int[] rowIndices, int[] colIndices, int row, int col) Gets the specified element from a sparse COO matrix.- Parameters:
entries- Non-zero values of the sparse COO matrix from which to get element.rowIndices- Non-zero row indices for the sparse COO matrix.colIndices- Non-zero column indices for the sparse COO matrix.row- Row index of the value to get from the sparse matrix.col- Column index of the value to get from the sparse matrix.- Returns:
- The value in the sparse COO matrix at the specified row and column indices if it exists. If the value is not found
within the non-zero data,
nullwill be returned.
-
getCoo
public static <T> T getCoo(T[] entries, int[][] indices, int[] target) Gets an element of a sparse COO tensor at the specified index. If no non-zero value exists, thennullis returned.- Parameters:
entries- Non-zero data of the COO tensor.indices- Non-zero indices o the COO tensor.target- Target index to search for inindices.- Returns:
- The value in
datawhich has an index matching the target. That is, if someidxis found such thatArrays.equals(indices[idx], target), thendata[idx]is returned. If no suchidxid found, thennullis returned.
-
cooInsertNewValue
public static <T> void cooInsertNewValue(T value, int[] index, T[] srcEntries, int[][] srcIndices, int insertionPoint, T[] destEntries, int[][] destIndices) Inserts a new value into a sparse COO tensor. This assumes there is no non-zero value already at the specified index.- Parameters:
value- Value to insert into the tensor.index- Non-zero index for new value.srcEntries- Non-zero data of the source tensor. Unmodified.srcIndices- Non-zero indices of the source tensor. Assumed to be rectangular. Unmodified.insertionPoint- Index insrcEntriesandsrcIndicesto insertvalueandindex.destEntries- Destination for storing the result of inserting thevalueintosrcEntries.destIndices- Destination for storing the result of inserting theindexintosrcIndices.- Throws:
IllegalArgumentException- IfdestEntries.length != srcEntries.length + 1ordestIndices.length != srcIndices.length + 1IllegalArgumentException- Ifindex.length != srcIndices[0].length.
-
cooInsertNewValue
public static <T> void cooInsertNewValue(T value, int rowIdx, int colIdx, T[] srcEntries, int[] srcRowIndices, int[] srcColIndices, int insertionPoint, T[] destEntries, int[] destRowIndices, int[] destColIndices) Inserts a new value into a sparse COO matrix. This assumes there is no non-zero value at the specified row and column.- Parameters:
value- Value to insert into the matrix.rowIdx- index for the value to insert.srcEntries- Non-zero data of the source matrix. Unmodified.srcRowIndices- Non-zero row indices of the source matrix. Unmodified.insertionPoint- Index insrcEntries,srcRowIndices,srcColIndicesto insertvalue,rowIdx, andcolIdxdestEntries- Destination for storing the result of inserting thevalueintosrcEntries.destRowIndices- Destination for storing the result of inserting therowIdxintosrcRowIndices.destColIndices- Destination for storing the result of inserting thecolIdxintosrcColIndices.- Throws:
IllegalArgumentException- IfdestEntries.length != srcEntries.length + 1ordestIndices.length != srcRowIndices.length + 1ordestColIndices.length != srcColIndices.length + 1.
-
cooInsertNewValue
public static <T> void cooInsertNewValue(T value, int index, T[] srcEntries, int[] srcIndices, int insertionPoint, T[] destEntries, int[] destIndices) Inserts a new value into a sparse COO vector. This assumes there is no non-zero value at the specified index.- Parameters:
value- Value to insert into the vector.index- Index for the value to insert.srcEntries- Non-zero data of the source vector. Unmodified.srcIndices- Non-zero indices of the source vector. Unmodified.insertionPoint- Index insrcEntriesandsrcIndicesto insertvalueandindex.destEntries- Destination for storing the result of inserting thevalueintosrcEntries.destIndices- Destination for storing the result of inserting theindexintosrcIndices.- Throws:
IllegalArgumentException- IfdestEntries.length != srcEntries.length + 1ordestIndices.length != srcIndices.length + 1.
-
getCoo
public static double getCoo(double[] entries, int[][] indices, int[] target) Gets an element of a sparse COO tensor at the specified index. If no non-zero value exists, thennullis returned.- Parameters:
entries- Non-zero data of the COO tensor.indices- Non-zero indices o the COO tensor.target- Target index to search for inindices.- Returns:
- The value in
datawhich has an index matching the target. That is, if someidxis found such thatArrays.equals(indices[idx], target), thendata[idx]is returned. If no suchidxid found, thennullis returned.
-
getCoo
public static float getCoo(float[] entries, int[][] indices, int[] target) Gets an element of a sparse COO tensor at the specified index. If no non-zero value exists, thennullis returned.- Parameters:
entries- Non-zero data of the COO tensor.indices- Non-zero indices o the COO tensor.target- Target index to search for inindices.- Returns:
- The value in
datawhich has an index matching the target. That is, if someidxis found such thatArrays.equals(indices[idx], target), thendata[idx]is returned. If no suchidxid found, thennullis returned.
-
getCoo
public static int getCoo(int[] entries, int[][] indices, int[] target) Gets an element of a sparse COO tensor at the specified index. If no non-zero value exists, thennullis returned.- Parameters:
entries- Non-zero data of the COO tensor.indices- Non-zero indices o the COO tensor.target- Target index to search for inindices.- Returns:
- The value in
datawhich has an index matching the target. That is, if someidxis found such thatArrays.equals(indices[idx], target), thendata[idx]is returned. If no suchidxid found, thennullis returned.
-
getTriU
public static <T> SparseMatrixData<T> getTriU(int diagOffset, Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Gets the upper-triangular portion of a sparse COO matrix with a possible diagonal offset. The remaining values will be zero.- Parameters:
diagOffset- Diagonal offset indicating which diagonal to extract values at or above.- If
diagOffset == 0then the properly upper-triangular portion of the matrix is extracted. - If
diagOffset == kwherek > 0then the values at and above the kth super-diagonal. - If
diagOffset == kwherek < 0then the values at and above the kth sub-diagonal.
- If
shape- Shape of the COO matrix.entries- Non-zero data of the COO matrix.rowIndices- Row indices of the COO matrix.colIndices- Column indices of the COO matrix.- Returns:
- A data container containing the resulting upper-triangular non-zero data, row indices, and column indices.
-
getTriL
public static <T> SparseMatrixData<T> getTriL(int diagOffset, Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Gets the lower-triangular portion of a sparse COO matrix with a possible diagonal offset. The remaining values will be zero.- Parameters:
diagOffset- Diagonal offset indicating which diagonal to extract values at or below.- If
diagOffset == 0then the properly lower-triangular portion of the matrix is extracted. - If
diagOffset == kwherek > 0then the values at and below the kth super-diagonal. - If
diagOffset == kwherek < 0then the values at and below the kth sub-diagonal.
- If
shape- Shape of the COO matrix.entries- Non-zero data of the COO matrix.rowIndices- Row indices of the COO matrix.colIndices- Column indices of the COO matrix.- Returns:
- A data container containing the resulting lower-triangular non-zero data, row indices, and column indices.
-
setSlice
public static <T> SparseMatrixData<T> setSlice(Shape shape1, T[] src1Entries, int[] src1RowIndices, int[] src1ColIndices, Shape shape2, T[] src2Entries, int[] src2RowIndices, int[] src2ColIndices, int row, int col) Copies a sparse matrix and sets a slice of the sparse COO matrix to the data of another sparse COO matrix.- Parameters:
shape1- Shape of the first matrix.src1Entries- Non-zero data of the matrix to set slice within.src1RowIndices- Row indices of the matrix to set slice within.src1ColIndices- Column indices of the matrix to set slice within.shape2- Shape of the first matrix.src2Entries- Non-zero data of the matrix to copy into the specified slice.src2RowIndices- Row indices of the matrix to copy into the specified slice.src2ColIndices- Column indices of the matrix to copy into the specified slice.row- Starting row index of slice.col- Starting column index of slice.- Returns:
- A sparse data container containing the result of setting the slice in the source matrix.
- Throws:
IndexOutOfBoundsException- If thevaluesmatrix does not fit in thesrcmatrix given the row and column index.
-
getSlice
public static <T> SparseMatrixData<T> getSlice(Shape shape, T[] entries, int[] rowIndices, int[] colIndices, int rowStart, int rowEnd, int colStart, int colEnd) Extracts a specified slice from a sparse COO matrix.- Parameters:
shape- Shape of the COO matrix.entries- Non-zero data of the COO matrix.rowIndices- Row indices of the COO matrix.colIndices- Column indices of the COO matrix.rowStart- Starting row index of the slice (inclusive).rowEnd- Ending row index of the slice (exclusive).colStart- Staring column index of a slice (inclusive).colEnd- Ending column index of the slice (exclusive).- Returns:
- A sparse data container containing the specified slice extracted from the COO matrix.
- Throws:
IndexOutOfBoundsException- If the specified slice does not fit into the matrix.
-
getDiag
public static <T> SparseVectorData<T> getDiag(Shape shape, T[] entries, int[] rowIndices, int[] colIndices, int diagOffset) Gets the elements of a COO matrix along the specified diagonal.- Parameters:
shape- Shape of the COO matrix.entries- Non-zero data of the COO matrix.rowIndices- Non-zero row indices of the COO matrix.colIndices- Non-zero column indices of the COO matrix.diagOffset- The diagonal to get within the COO matrix.- If
diagOffset == 0: Then the elements of the principle diagonal are collected. - If
diagOffset < 0: Then the elements of the sub-diagonaldiagOffsetbelow the principle diagonal are collected. - If
diagOffset > 0: Then the elements of the super-diagonaldiagOffsetabove the principle diagonal are collected.
- If
- Returns:
- A sparse vector data object containing the non-zero data and indices along the specified diagonal of the COO matrix.
-
getRow
public static <T> SparseVectorData<T> getRow(Shape shape, T[] entries, int[] rowIndices, int[] colIndices, int rowIdx, int start, int end) Gets a specified row of a COO matrix betweenstart(inclusive) andend(exclusive).- Parameters:
shape- Shape of the COO matrix.entries- Non-zero data of the COO matrix.rowIndices- Non-zero row indices of the COO matrix.colIndices- Non-zero column indices of the COO matrix.rowIdx- Index of the row of this matrix to get.colStart- Starting column of the row (inclusive).colEnd- Ending column of the row (exclusive).- Returns:
- The row at index
rowIdxof this matrix between thestartandendindices. - Throws:
IndexOutOfBoundsException- If eitherendarestartout of bounds for the shape of this matrix.IllegalArgumentException- Ifendis less thanstart.
-
getCol
public static <T> SparseVectorData<T> getCol(Shape shape, T[] entries, int[] rowIndices, int[] colIndices, int colIdx, int start, int end) Gets a specified column of a COO matrix betweenstart(inclusive) andend(exclusive).- Parameters:
shape- Shape of the COO matrix.entries- Non-zero data of the COO matrix.rowIndices- Non-zero row indices of the COO matrix.colIndices- Non-zero column indices of the COO matrix.colIdx- Index of the column of this matrix to get.colStart- Starting column of the row (inclusive).colEnd- Ending column of the row (exclusive).- Returns:
- The column at index
colIdxof this matrix between thestartandendindices. - Throws:
IndexOutOfBoundsException- If eitherendarestartout of bounds for the shape of this matrix.IllegalArgumentException- Ifendis less thanstart.
-