public class GrowingSparseMatrix extends Object implements SparseMatrix
Matrix
based on the Yale Sparse Matrix Format. Each
row is allocated a pair of arrays which keeps the non-zero column values in
column order. Lookups are O(log n) where n is the number of non-zero values
for the largest row. Calls to set and setColumn can expand the matrix by
both rows and columns.
Calls to getRowVector
and getColumnVector
return a snapshot of the matrix data
at the time of the call. Subsequent updates to the matrix will not be
reflected in these vectors. The returned vectors are immutable and any calls
to mutating operations will throw an UnsupportedOperationException
.
This class is not thread-safe.
Matrix.Type
Constructor and Description |
---|
GrowingSparseMatrix()
Create a new empty
GrowingSparseMatrix . |
GrowingSparseMatrix(int rows,
int columns)
Create a new empty
GrowingSparseMatrix with the specified
dimensions. |
Modifier and Type | Method and Description |
---|---|
int |
columns()
Returns the number of columns in this
Matrix . |
double |
get(int row,
int col)
Returns the value of the
Matrix at the provided row and column. |
double[] |
getColumn(int column)
Returns the entire column.
|
SparseDoubleVector |
getColumnVector(int column)
Returns a
DoubleVector of the contents of the column. |
double[] |
getRow(int row)
Returns the entire row.
|
SparseDoubleVector |
getRowVector(int row)
Returns a
DoubleVector of the contents of the row. |
int |
rows()
Returns the number of rows in this
Matrix . |
void |
set(int row,
int col,
double val)
Sets the location at the row and column to the provided value.
|
void |
setColumn(int column,
double[] values)
Sets the values for the column of this
Matrix using the provided
array. |
void |
setColumn(int column,
DoubleVector values)
Sets the values for the column of this
Matrix using the provided
DoubleVector . |
void |
setRow(int row,
double[] columns)
Sets the values for the row of this
Matrix using the provided
array. |
void |
setRow(int row,
DoubleVector columns)
Sets the values for the row of this
Matrix using the provided
DoubleVector . |
double[][] |
toDenseArray()
Converts the
Matrix to a two dimensional array. |
public GrowingSparseMatrix()
GrowingSparseMatrix
.public GrowingSparseMatrix(int rows, int columns)
GrowingSparseMatrix
with the specified
dimensions.rows
- the number of rows in the matrixcolumns
- the number of columns in the matrixpublic double get(int row, int col)
Matrix
at the provided row and column.public double[] getColumn(int column)
public SparseDoubleVector getColumnVector(int column)
DoubleVector
of the contents of the column.getColumnVector
in interface Matrix
getColumnVector
in interface SparseMatrix
column
- The column to return a DoubleVector
forDoubleVector
representing the column at column
public double[] getRow(int row)
public SparseDoubleVector getRowVector(int row)
DoubleVector
of the contents of the row. The length of
the returned row vector reflects the size of matrix at the time of the
call, which may be different from earlier calls to columns()
.getRowVector
in interface Matrix
getRowVector
in interface SparseMatrix
row
- the index of row to returnSparseDoubleVector
of the row's datapublic int columns()
Matrix
.public void set(int row, int col, double val)
The size of the matrix will be expanded if either row or col is larger than the largest previously seen row or column value. When the matrix is expanded by either dimension, the values for the new row/column will all be assumed to be zero.
public void setColumn(int column, double[] values)
Matrix
using the provided
array. Note that the array itself is not made internal to the instance
itself.public void setColumn(int column, DoubleVector values)
Matrix
using the provided
DoubleVector
. Note that the DoubleVector
itself is not
made internal to the instance itself.public void setRow(int row, double[] columns)
Matrix
using the provided
array. Note that the array itself is not made internal to the instance
itself.
The size of the matrix will be expanded if either row or
col is larger than the largest previously seen row or column value.
When the matrix is expanded by either dimension, the values for the new
row/column will all be assumed to be zero.public void setRow(int row, DoubleVector columns)
Matrix
using the provided
DoubleVector
. Note that the DoubleVector
itself is not
made internal to the instance itself.
The size of the matrix will be expanded if either row or
col is larger than the largest previously seen row or column value.
When the matrix is expanded by either dimension, the values for the new
row/column will all be assumed to be zero.public double[][] toDenseArray()
Matrix
to a two dimensional array. Note that for
large matrices, this may exhaust all available memory.toDenseArray
in interface Matrix
Matrix
.Copyright © 2012. All Rights Reserved.