public class SparseMatrix extends Object implements Serializable
double
.
The class represents a general real sparse matrix.
It is intended to be efficiently and easily updated.
A SparseMatrix
can be constructed from a set of arrays, or it can
be abstractly created as an empty array and then incrementally built into final form.
It is usually easier to create an empty SparseMatrix
of set size
and then use the set
method to set the elements of the array. When
setting the elements of the sparse array, their positions should be thought of
as their positions in the dense array. Elements can be set in any order, but
only the elements actually set are stored.
This class includes methods to update the sparse matrix.
There are also methods to multiply a sparse matrix and a vector
or to multiply two sparse matrices.
To solve a sparse linear system use SparseCholesky
or SuperLU
.
Modifier and Type | Class and Description |
---|---|
static class |
SparseMatrix.SparseArray
The
SparseArray class uses public fields to hold
the data for a sparse matrix in the Java Sparse Array format. |
Constructor and Description |
---|
SparseMatrix(int nRows,
int nColumns)
Creates a new instance of
SparseMatrix . |
SparseMatrix(int nRows,
int nColumns,
int[][] index,
double[][] values)
Constructs a sparse matrix from SparseArray (Java Sparse Array) data.
|
SparseMatrix(SparseMatrix.SparseArray jsa)
Constructs a sparse matrix from a
SparseArray object. |
SparseMatrix(SparseMatrix A)
Creates a new instance of
SparseMatrix which is a copy of another SparseMatrix . |
Modifier and Type | Method and Description |
---|---|
static SparseMatrix |
add(double alpha,
double beta,
SparseMatrix A,
SparseMatrix B)
Performs element-wise addition of two real sparse matrices
A , B of type
SparseMatrix ,
\(C \leftarrow \alpha A + \beta B.\) |
void |
checkSquareMatrix()
Check that the matrix is square.
|
double |
frobeniusNorm()
Returns the Frobenius norm of the matrix.
|
double |
get(int iRow,
int jColumn)
Returns the value of an element in the matrix.
|
int |
getNumberOfColumns()
Returns the number of columns in the matrix.
|
long |
getNumberOfNonZeros()
Returns the number of nonzeros in the matrix.
|
int |
getNumberOfRows()
Returns the number of rows in the matrix.
|
double |
infinityNorm()
Returns the infinity norm of the matrix.
|
double[] |
multiply(double[] x)
Multiply the matrix by a vector.
|
static double[] |
multiply(double[] x,
SparseMatrix A)
Multiply row array
x and sparse matrix A ,
\(x^TA \). |
static double[] |
multiply(SparseMatrix A,
double[] x)
Multiply sparse matrix
A and column array x ,
\(A x\). |
static SparseMatrix |
multiply(SparseMatrix A,
SparseMatrix B)
Multiply two sparse matrices A and B, \( C \leftarrow AB\).
|
static double[] |
multiplySymmetric(SparseMatrix A,
double[] x)
Multiply sparse symmetric matrix
A and column vector x . |
double |
oneNorm()
Returns the matrix one norm of the sparse matrix.
|
double |
plusEquals(int iRow,
int jColumn,
double x)
Adds a value to an element in the matrix.
|
void |
set(int iRow,
int jColumn,
double x)
Sets the value of an element in the matrix.
|
double[][] |
toDenseMatrix()
Returns the sparse matrix as a dense matrix.
|
SparseMatrix.SparseArray |
toSparseArray()
Returns the sparse matrix in the
SparseArray form. |
SparseMatrix |
transpose()
Returns the transpose of the matrix.
|
public SparseMatrix(int nRows, int nColumns)
SparseMatrix
.
Initially this is the zero matrix.nRows
- an int
containing the number of rows in the sparse matrix.nColumns
- an int
containing the number of columns in the sparse matrix.public SparseMatrix(SparseMatrix A)
SparseMatrix
which is a copy of another SparseMatrix
.A
- the SparseMatrix
object containing the sparse matrix to be copied.public SparseMatrix(SparseMatrix.SparseArray jsa)
SparseArray
object.jsa
- is a SparseArray
used to initialize the sparse matrix.
The field numberOfNonZeros
in jsa
is not used for initialization,
so it does not have to be set.public SparseMatrix(int nRows, int nColumns, int[][] index, double[][] values)
nRows
- an int
containing the number of rows in the sparse matrix.nColumns
- an int
containing the number of columns in the sparse matrix.index
- an int
jagged array containing the column
indices of all nonzero elements corresponding to the compressed
representation of the sparse matrix in values
.
The size of index
must be identical to the size of
values
. The i-th row contains the column
indices of all nonzero elements of row i of the
sparse matrix. The j-th element of row
i is the column index of the value located at the
same position in values
.values
- a double
jagged array containing the compressed
representation of a real sparse matrix of size nRows
by
nColumns
. The number of rows in values
must
be nRows
. The i-th row contains all
nonzero elements of row i of the full sparse matrix.public int getNumberOfRows()
int
containing the number of rows in the matrix.public int getNumberOfColumns()
int
containing the number of columns in the matrix.public long getNumberOfNonZeros()
long
containing the number of nonzeros in the matrix.public double get(int iRow, int jColumn)
iRow
- an int
containing the row index of the element.jColumn
- an int
containing the column index of the element.double
containing the value
of the iRow
-th and jColumn
-th element.
If the element was never set, its value is zero.public void set(int iRow, int jColumn, double x)
iRow
- an int
containing the row index of the element.jColumn
- an int
containing the column index of the element.x
- a double
containing the value of the iRow
-th
and jColumn
-th element.public double plusEquals(int iRow, int jColumn, double x)
iRow
- an int
containing the row index of the element.jColumn
- an int
containing the column index of the element.x
- a double
containing the value to be added to the
iRow
-th and jColumn
-th element.double
containing the updated value of the element,
which equals its old value plus x.public void checkSquareMatrix()
IllegalArgumentException
- is thrown if the matrix is not square.public static SparseMatrix add(double alpha, double beta, SparseMatrix A, SparseMatrix B)
A
, B
of type
SparseMatrix
,
\(C \leftarrow \alpha A + \beta B.\)alpha
- a double
scalar.beta
- a double
scalar.A
- a SparseMatrix
matrix.B
- a SparseMatrix
matrix.SparseMatrix
matrix representing the computed sum.IllegalArgumentException
- This exception is thrown when the matrices are not of the same size.public double[] multiply(double[] x)
x
- a double
column vector.double
vector representing the product of
this matrix times x.IllegalArgumentException
- This exception is thrown if the number
of columns in the SparseMatrix
object is not equal
to the number of elements in the input column vector.public static double[] multiply(SparseMatrix A, double[] x)
A
and column array x
,
\(A x\).A
- a SparseMatrix
matrix.x
- a double
column array.double
vector representing the product of the
arguments, \(A x\).IllegalArgumentException
- This exception is thrown when the number
of columns in the input matrix is not equal
to the number of elements in the input column vector.public static double[] multiply(double[] x, SparseMatrix A)
x
and sparse matrix A
,
\(x^TA \).x
- a double
row array.A
- a SparseMatrix
matrix.double
vector representing the product of the
arguments, \( x^T A \).IllegalArgumentException
- This exception is thrown when
the number of elements in the
input vector is not equal to the number of rows of the matrix.public static double[] multiplySymmetric(SparseMatrix A, double[] x)
A
and column vector x
.A
- a SparseMatrix
sparse symmetric matrix,
where only the lower triangular part of the matrix is to be used.x
- a double
vector.double
vector representing the product of the
arguments, \(Ax\).IllegalArgumentException
- This exception is thrown when
the input matrix is not square or the number of columns
in the input matrix is not equal to the number of elements in the input
column vector.public static SparseMatrix multiply(SparseMatrix A, SparseMatrix B)
A
- a SparseMatrix
sparse matrix.B
- a SparseMatrix
sparse matrix.SparseMatrix
product AB of A and B.IllegalArgumentException
- This exception is thrown when
the number of columns of matrix A
is not equal to the number of rows of matrix B.public SparseMatrix transpose()
SparseMatrix
object which is the
transpose of the constructed SparseMatrix
object.public double oneNorm()
double
value equal to the maximum of the column
sums of the absolute values of the array elements.public double infinityNorm()
double
scalar value equal to the maximum
of the row sums of the absolute values of the
array elements of the sparse matrix.public double frobeniusNorm()
double
scalar value equal to the
Frobenius norm of the matrix.public double[][] toDenseMatrix()
double
containing
this matrix with all of the zeros explicitly present.
The number of rows and columns in the returned matrix is the
same as in the sparse matrix.public SparseMatrix.SparseArray toSparseArray()
SparseArray
form.Copyright © 2020 Rogue Wave Software. All rights reserved.