Class RowView
-
- All Implemented Interfaces:
public class RowView
A lightweight view into a row of a sparse matrix that doesn't copy data. This provides efficient access to matrix row elements without the overhead of creating a new Matrix object and copying all the data.
This is particularly useful for large sparse matrices where you need to access row elements frequently, such as in iterative algorithms.
Example usage:
Matrix bigMatrix = ...; // 1000 x 1M sparse matrix RowView row = bigMatrix.getRowView(5); // Efficient access - only processes non-zero elements for (int i = 0; i < row.getNonZeroCount(); i++) { int col = row.getNonZeroCol(i); double value = row.getNonZeroValue(i); // process (col, value) pair } // Or get any element (returns 0.0 for non-stored elements) double val = row.get(100);
- Since:
1.0
QORE Lab, Imperial College London
-
-
Field Summary
Fields Modifier and Type Field Description public final int
rowIndex
public final int
numCols
public final int
nonZeroCount
-
Method Summary
Modifier and Type Method Description int
getRowIndex()
Returns the row index that this view represents. int
getNumCols()
Returns the total number of columns in the row (including zeros). int
getNonZeroCount()
Returns the number of non-zero elements in this row. double
dotProduct(Matrix columnVector)
Computes the dot product of this row with a column vector. double
get(int col)
Returns the value at the specified column in this row. int
getNonZeroCol(int i)
Returns the column index of the i-th non-zero element in this row. double
getNonZeroValue(int i)
Returns the value of the i-th non-zero element in this row. boolean
hasNonZeros()
Checks if this row has any non-zero elements. -
-
Method Detail
-
getRowIndex
int getRowIndex()
Returns the row index that this view represents.
- Returns:
the row index
-
getNumCols
int getNumCols()
Returns the total number of columns in the row (including zeros).
- Returns:
the number of columns
-
getNonZeroCount
int getNonZeroCount()
Returns the number of non-zero elements in this row.
- Returns:
the count of non-zero elements
-
dotProduct
double dotProduct(Matrix columnVector)
Computes the dot product of this row with a column vector. This is optimized to only iterate through the non-zero elements of the row.
- Parameters:
columnVector
- the column vector to multiply with (must have getNumRows() == this.- Returns:
the scalar result of the dot product
-
get
double get(int col)
Returns the value at the specified column in this row. This method searches through the non-zero elements, so it's O(nnz_in_row). For frequent access to multiple elements, consider using the non-zero iterators.
- Parameters:
col
- the column index- Returns:
the value at (row, col), or 0.0 if not stored
-
getNonZeroCol
int getNonZeroCol(int i)
Returns the column index of the i-th non-zero element in this row.
- Parameters:
i
- the index into the non-zero elements (0 to getNonZeroCount()-1)- Returns:
the column index
-
getNonZeroValue
double getNonZeroValue(int i)
Returns the value of the i-th non-zero element in this row.
- Parameters:
i
- the index into the non-zero elements (0 to getNonZeroCount()-1)- Returns:
the value
-
hasNonZeros
boolean hasNonZeros()
Checks if this row has any non-zero elements.
- Returns:
true if the row contains at least one non-zero element
-
-
-
-