Package com.imsl.math
Class SparseMatrix
java.lang.Object
com.imsl.math.SparseMatrix
- All Implemented Interfaces:
Serializable
Sparse matrix of type
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classTheSparseArrayclass uses public fields to hold the data for a sparse matrix in the Java Sparse Array format. -
Constructor Summary
ConstructorsConstructorDescriptionSparseMatrix(int nRows, int nColumns) Creates a new instance ofSparseMatrix.SparseMatrix(int nRows, int nColumns, int[][] index, double[][] values) Constructs a sparse matrix from SparseArray (Java Sparse Array) data.Creates a new instance ofSparseMatrixwhich is a copy of anotherSparseMatrix.Constructs a sparse matrix from aSparseArrayobject. -
Method Summary
Modifier and TypeMethodDescriptionstatic SparseMatrixadd(double alpha, double beta, SparseMatrix A, SparseMatrix B) Performs element-wise addition of two real sparse matricesA,Bof typeSparseMatrix, \(C \leftarrow \alpha A + \beta B.\)voidCheck that the matrix is square.doubleReturns the Frobenius norm of the matrix.doubleget(int iRow, int jColumn) Returns the value of an element in the matrix.intReturns the number of columns in the matrix.longReturns the number of nonzeros in the matrix.intReturns the number of rows in the matrix.doubleReturns 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 arrayxand sparse matrixA, \(x^TA \).static double[]multiply(SparseMatrix A, double[] x) Multiply sparse matrixAand column arrayx, \(A x\).static SparseMatrixmultiply(SparseMatrix A, SparseMatrix B) Multiply two sparse matrices A and B, \( C \leftarrow AB\).static double[]multiplySymmetric(SparseMatrix A, double[] x) Multiply sparse symmetric matrixAand column vectorx.doubleoneNorm()Returns the matrix one norm of the sparse matrix.doubleplusEquals(int iRow, int jColumn, double x) Adds a value to an element in the matrix.voidset(int iRow, int jColumn, double x) Sets the value of an element in the matrix.double[][]Returns the sparse matrix as a dense matrix.Returns the sparse matrix in theSparseArrayform.Returns the transpose of the matrix.
-
Constructor Details
-
SparseMatrix
public SparseMatrix(int nRows, int nColumns) Creates a new instance ofSparseMatrix. Initially this is the zero matrix.- Parameters:
nRows- anintcontaining the number of rows in the sparse matrix.nColumns- anintcontaining the number of columns in the sparse matrix.
-
SparseMatrix
Creates a new instance ofSparseMatrixwhich is a copy of anotherSparseMatrix.- Parameters:
A- theSparseMatrixobject containing the sparse matrix to be copied.
-
SparseMatrix
Constructs a sparse matrix from aSparseArrayobject.- Parameters:
jsa- is aSparseArrayused to initialize the sparse matrix. The fieldnumberOfNonZerosinjsais not used for initialization, so it does not have to be set.
-
SparseMatrix
public SparseMatrix(int nRows, int nColumns, int[][] index, double[][] values) Constructs a sparse matrix from SparseArray (Java Sparse Array) data.- Parameters:
nRows- anintcontaining the number of rows in the sparse matrix.nColumns- anintcontaining the number of columns in the sparse matrix.index- anintjagged array containing the column indices of all nonzero elements corresponding to the compressed representation of the sparse matrix invalues. The size ofindexmust be identical to the size ofvalues. 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 invalues.values- adoublejagged array containing the compressed representation of a real sparse matrix of sizenRowsbynColumns. The number of rows invaluesmust benRows. The i-th row contains all nonzero elements of row i of the full sparse matrix.
-
-
Method Details
-
getNumberOfRows
public int getNumberOfRows()Returns the number of rows in the matrix.- Returns:
- an
intcontaining the number of rows in the matrix.
-
getNumberOfColumns
public int getNumberOfColumns()Returns the number of columns in the matrix.- Returns:
- an
intcontaining the number of columns in the matrix.
-
getNumberOfNonZeros
public long getNumberOfNonZeros()Returns the number of nonzeros in the matrix.- Returns:
- a
longcontaining the number of nonzeros in the matrix.
-
get
public double get(int iRow, int jColumn) Returns the value of an element in the matrix.- Parameters:
iRow- anintcontaining the row index of the element.jColumn- anintcontaining the column index of the element.- Returns:
- a
doublecontaining the value of theiRow-th andjColumn-th element. If the element was never set, its value is zero.
-
set
public void set(int iRow, int jColumn, double x) Sets the value of an element in the matrix.- Parameters:
iRow- anintcontaining the row index of the element.jColumn- anintcontaining the column index of the element.x- adoublecontaining the value of theiRow-th andjColumn-th element.
-
plusEquals
public double plusEquals(int iRow, int jColumn, double x) Adds a value to an element in the matrix.- Parameters:
iRow- anintcontaining the row index of the element.jColumn- anintcontaining the column index of the element.x- adoublecontaining the value to be added to theiRow-th andjColumn-th element.- Returns:
- a
doublecontaining the updated value of the element, which equals its old value plus x.
-
checkSquareMatrix
public void checkSquareMatrix()Check that the matrix is square.- Throws:
IllegalArgumentException- is thrown if the matrix is not square.
-
add
Performs element-wise addition of two real sparse matricesA,Bof typeSparseMatrix, \(C \leftarrow \alpha A + \beta B.\)- Parameters:
alpha- adoublescalar.beta- adoublescalar.A- aSparseMatrixmatrix.B- aSparseMatrixmatrix.- Returns:
- a
SparseMatrixmatrix representing the computed sum. - Throws:
IllegalArgumentException- This exception is thrown when the matrices are not of the same size.
-
multiply
public double[] multiply(double[] x) Multiply the matrix by a vector.- Parameters:
x- adoublecolumn vector.- Returns:
- a
doublevector representing the product of this matrix times x. - Throws:
IllegalArgumentException- This exception is thrown if the number of columns in theSparseMatrixobject is not equal to the number of elements in the input column vector.
-
multiply
Multiply sparse matrixAand column arrayx, \(A x\).- Parameters:
A- aSparseMatrixmatrix.x- adoublecolumn array.- Returns:
- a
doublevector representing the product of the arguments, \(A x\). - Throws:
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.
-
multiply
Multiply row arrayxand sparse matrixA, \(x^TA \).- Parameters:
x- adoublerow array.A- aSparseMatrixmatrix.- Returns:
- a
doublevector representing the product of the arguments, \( x^T A \). - Throws:
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.
-
multiplySymmetric
Multiply sparse symmetric matrixAand column vectorx.- Parameters:
A- aSparseMatrixsparse symmetric matrix, where only the lower triangular part of the matrix is to be used.x- adoublevector.- Returns:
- a
doublevector representing the product of the arguments, \(Ax\). - Throws:
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.
-
multiply
Multiply two sparse matrices A and B, \( C \leftarrow AB\).- Parameters:
A- aSparseMatrixsparse matrix.B- aSparseMatrixsparse matrix.- Returns:
- the
SparseMatrixproduct AB of A and B. - Throws:
IllegalArgumentException- This exception is thrown when the number of columns of matrix A is not equal to the number of rows of matrix B.
-
transpose
Returns the transpose of the matrix.- Returns:
- a
SparseMatrixobject which is the transpose of the constructedSparseMatrixobject.
-
oneNorm
public double oneNorm()Returns the matrix one norm of the sparse matrix.- Returns:
- a
doublevalue equal to the maximum of the column sums of the absolute values of the array elements.
-
infinityNorm
public double infinityNorm()Returns the infinity norm of the matrix.- Returns:
- a
doublescalar value equal to the maximum of the row sums of the absolute values of the array elements of the sparse matrix.
-
frobeniusNorm
public double frobeniusNorm()Returns the Frobenius norm of the matrix.- Returns:
- a
doublescalar value equal to the Frobenius norm of the matrix.
-
toDenseMatrix
public double[][] toDenseMatrix()Returns the sparse matrix as a dense matrix.- Returns:
- a rectangular Java array of type
doublecontaining 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.
-
toSparseArray
Returns the sparse matrix in theSparseArrayform.
-