Package jline.util.matrix
Class RowView
java.lang.Object
jline.util.matrix.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 invalid input: '<' 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
- Author:
- QORE Lab, Imperial College London
-
Method Summary
Modifier and TypeMethodDescriptiondoubledotProduct(Matrix columnVector) Computes the dot product of this row with a column vector.doubleget(int col) Returns the value at the specified column in this row.intgetNonZeroCol(int i) Returns the column index of the i-th non-zero element in this row.intReturns the number of non-zero elements in this row.doublegetNonZeroValue(int i) Returns the value of the i-th non-zero element in this row.intReturns the total number of columns in the row (including zeros).intReturns the row index that this view represents.booleanChecks if this row has any non-zero elements.
-
Method Details
-
dotProduct
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.getNumCols())- Returns:
- the scalar result of the dot product
- Throws:
IllegalArgumentException- if dimensions don't match
-
get
public 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
public 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
-
getNonZeroCount
public int getNonZeroCount()Returns the number of non-zero elements in this row.- Returns:
- the count of non-zero elements
-
getNonZeroValue
public 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
-
getNumCols
public int getNumCols()Returns the total number of columns in the row (including zeros).- Returns:
- the number of columns
-
getRowIndex
public int getRowIndex()Returns the row index that this view represents.- Returns:
- the row index
-
hasNonZeros
public boolean hasNonZeros()Checks if this row has any non-zero elements.- Returns:
- true if the row contains at least one non-zero element
-