\n\n\n\nSparseMatrix\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n
\n
\n
\n\n\n\n
\n
org.jscience.mathematics.vector
\n

## Class SparseMatrix<F extends Field<F>>

\n
\n
\n
\n
\n
\n
• \n
\n
All Implemented Interfaces:
\n
javolution.lang.Immutable, javolution.lang.Realtime, javolution.lang.ValueType, GroupAdditive<Matrix<F>>, Ring<Matrix<F>>, Structure<Matrix<F>>, VectorSpace<Matrix<F>,F>
\n
\n
\n
\n
`public final class SparseMatrix<F extends Field<F>>\nextends Matrix<F>`
\n

This class represents a matrix made of `sparse\n vectors` (as rows). To create a sparse matrix made of column vectors the \n `transpose()` method can be used. \n For example:

``\n        SparseVector<Rational> column0 = SparseVector.valueOf(...);\n        SparseVector<Rational> column1 = SparseVector.valueOf(...);\n        SparseMatrix<Rational> M = SparseMatrix.valueOf(column0, column1).transpose();\n     ``

\n

As for any concrete `structure`, this class is declared `final` (otherwise most\n operations would have to be overridden to return the appropriate type).\n Specialized dense matrix should sub-class `Matrix` directly.\n For example:

``\n        // Extension through composition.\n        final class BandMatrix <F extends Field<F>> extends Matrix<F> {\n             private SparseMatrix<F> _value;\n             ...\n             public BandMatrix opposite() { // Returns the right type.\n                 return BandMatrix.valueOf(_value.opposite());\n             }\n             ...\n        }``
\n

\n
• \n
\n
\n
\n
\n
• \n\n
\n
• \n\n\n

### Method Summary

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Methods
Modifier and TypeMethod and Description
`SparseMatrix<F>``adjoint()`\n
Returns the adjoint of this matrix.
\n
`F``cofactor(int i,\n int j)`\n
Returns the cofactor of an element in this matrix.
\n
`SparseMatrix<F>``copy()`\n
Returns a copy of this matrix \n `allocated` \n by the calling thread (possibly on the stack).
\n
`F``determinant()`\n
Returns the determinant of this matrix.
\n
`F``get(int i,\n int j)`\n
Returns a single element from this matrix.
\n
`SparseVector<F>``getColumn(int j)`\n
Returns the column identified by the specified index in this matrix.
\n
`SparseVector<F>``getDiagonal()`\n
Returns the diagonal vector.
\n
`int``getNumberOfColumns()`\n
Returns the number of columns `n` for this matrix.
\n
`int``getNumberOfRows()`\n
Returns the number of rows `m` for this matrix.
\n
`SparseVector<F>``getRow(int i)`\n
Returns the row identified by the specified index in this matrix.
\n
`F``getZero()`\n
Returns the value of the non-set elements for this sparse matrix.
\n
`SparseMatrix<F>``inverse()`\n
Returns the inverse of this matrix (must be square).
\n
`SparseMatrix<F>``minus(Matrix<F> that)`\n
Returns the difference between this matrix and the one specified.
\n
`SparseMatrix<F>``opposite()`\n
Returns the negation of this matrix.
\n
`SparseMatrix<F>``plus(Matrix<F> that)`\n
Returns the sum of this matrix with the one specified.
\n
`Matrix<F>``solve(Matrix<F> y)`\n
Solves this matrix for the specified matrix (returns `x`\n such as `this \xc2\xb7 x = y`).
\n
`SparseMatrix<F>``tensor(Matrix<F> that)`\n
Returns the linear algebraic matrix tensor product of this matrix\n and another (Kronecker product).
\n
`SparseMatrix<F>``times(F k)`\n
Returns the product of this matrix by the specified factor.
\n
`SparseMatrix<F>``times(Matrix<F> that)`\n
Returns the product of this matrix with the one specified.
\n
`SparseVector<F>``times(Vector<F> v)`\n
Returns the product of this matrix by the specified vector.
\n
`SparseMatrix<F>``transpose()`\n
Returns the transpose of this matrix.
\n
`static <F extends Field<F>> SparseMatrix<F>``valueOf(List<SparseVector<F>> rows)`\n
Returns a sparse matrix holding the row vectors from the specified \n collection (column vectors if `transposed`).
\n
`static <F extends Field<F>> SparseMatrix<F>``valueOf(Matrix<F> that,\n F zero)`\n
Returns a sparse matrix equivalent to the specified matrix but with \n the zero elements removed using the default object equality comparator.
\n
`static <F extends Field<F>> SparseMatrix<F>``valueOf(Matrix<F> that,\n F zero,\n javolution.util.FastComparator<? super F> comparator)`\n
Returns a sparse matrix equivalent to the specified matrix but with \n the zero elements removed using the specified object equality comparator.
\n
`static <F extends Field<F>> SparseMatrix<F>``valueOf(SparseVector<F>... rows)`\n
Returns a sparse matrix holding the specified row vectors \n (column vectors if `transposed`).
\n
`static <F extends Field<F>> SparseMatrix<F>``valueOf(Vector<F> diagonal,\n F zero)`\n
Returns the sparse square matrix having the specified diagonal\n vector.
\n
`SparseVector<F>``vectorization()`\n
Returns the vectorization of this matrix.
\n
\n
\n
• \n\n\n

### Methods inherited from class org.jscience.mathematics.vector.Matrix

\n`divide, equals, equals, hashCode, isSquare, pow, pseudoInverse, solve, toString, toText, trace`
• \n
\n
\n
• \n\n\n

### Methods inherited from class java.lang.Object

\n`getClass, notify, notifyAll, wait, wait, wait`
• \n
\n
• \n
\n
• \n
\n
\n
\n
\n
• \n\n
\n
• \n\n\n

### Method Detail

\n\n\n\n\n\n
\n
• \n

#### valueOf

\n
`public static <F extends Field<F>> SparseMatrix<F> valueOf(Vector<F> diagonal,\n                                           F zero)`
\n
Returns the sparse square matrix having the specified diagonal\n vector. This method is typically used to create an identity matrix.\n For example:`\n SparseMatrix<Real> IDENTITY = Matrix.valueOf(\n DenseVector.valueOf({Real.ONE, Real.ONE, Real.ONE}), Real.ZERO);\n `
\n
Parameters:
`diagonal` - the diagonal vector.
`zero` - value of non-diagonal elements.
\n
Returns:
a square matrix with `diagonal` on the diagonal and\n `zero` elsewhere.
\n
• \n
\n\n\n\n
\n
• \n

#### valueOf

\n
`public static <F extends Field<F>> SparseMatrix<F> valueOf(SparseVector<F>... rows)`
\n
Returns a sparse matrix holding the specified row vectors \n (column vectors if `transposed`).
\n
Parameters:
`rows` - the row vectors.
\n
Returns:
the matrix having the specified rows.
\n
Throws:
\n
`DimensionException` - if the rows do not have the same dimension.
\n
• \n
\n\n\n\n
\n
• \n

#### valueOf

\n
`public static <F extends Field<F>> SparseMatrix<F> valueOf(List<SparseVector<F>> rows)`
\n
Returns a sparse matrix holding the row vectors from the specified \n collection (column vectors if `transposed`).
\n
Parameters:
`rows` - the list of row vectors.
\n
Returns:
the matrix having the specified rows.
\n
Throws:
\n
`DimensionException` - if the rows do not have the same dimension.
\n
• \n
\n\n\n\n\n\n
\n
• \n

#### valueOf

\n
`public static <F extends Field<F>> SparseMatrix<F> valueOf(Matrix<F> that,\n                                           F zero)`
\n
Returns a sparse matrix equivalent to the specified matrix but with \n the zero elements removed using the default object equality comparator.
\n
Parameters:
`that` - the matrix to convert.
`zero` - the zero element for the sparse vector to return.
\n
Returns:
`SparseMatrix.valueOf(that, zero, FastComparator.DEFAULT)` or a dense matrix holding the same elements
\n
• \n
\n\n\n\n\n\n
\n
• \n

#### valueOf

\n
`public static <F extends Field<F>> SparseMatrix<F> valueOf(Matrix<F> that,\n                                           F zero,\n                                           javolution.util.FastComparator<? super F> comparator)`
\n
Returns a sparse matrix equivalent to the specified matrix but with \n the zero elements removed using the specified object equality comparator.
\n
Parameters:
`that` - the matrix to convert.
`zero` - the zero element for the sparse vector to return.
`comparator` - the comparator used to determinate zero equality.
\n
Returns:
`that` or a dense matrix holding the same elements\n as the specified matrix.
\n
• \n
\n\n\n\n
\n
• \n

#### getZero

\n
`public F getZero()`
\n
Returns the value of the non-set elements for this sparse matrix.
\n
Returns:
the element corresponding to zero.
\n
• \n
\n\n\n\n
\n
• \n

#### getNumberOfRows

\n
`public int getNumberOfRows()`
\n
Description copied from class: `Matrix`
\n
Returns the number of rows `m` for this matrix.
\n
\n
Specified by:
\n
`getNumberOfRows` in class `Matrix<F extends Field<F>>`
\n
Returns:
m, the number of rows.
\n
• \n
\n\n\n\n
\n
• \n

#### getNumberOfColumns

\n
`public int getNumberOfColumns()`
\n
Description copied from class: `Matrix`
\n
Returns the number of columns `n` for this matrix.
\n
\n
Specified by:
\n
`getNumberOfColumns` in class `Matrix<F extends Field<F>>`
\n
Returns:
n, the number of columns.
\n
• \n
\n\n\n\n
\n
• \n

#### get

\n
`public F get(int i,\n    int j)`
\n
Description copied from class: `Matrix`
\n
Returns a single element from this matrix.
\n
\n
Specified by:
\n
`get` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`i` - the row index (range [0..m[).
`j` - the column index (range [0..n[).
\n
Returns:
\n
• \n
\n\n\n\n
\n
• \n

#### getRow

\n
`public SparseVector<F> getRow(int i)`
\n
Description copied from class: `Matrix`
\n
Returns the row identified by the specified index in this matrix.
\n
\n
Specified by:
\n
`getRow` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`i` - the row index (range [0..m[).
\n
Returns:
the vector holding the specified row.
\n
• \n
\n\n\n\n
\n
• \n

#### getColumn

\n
`public SparseVector<F> getColumn(int j)`
\n
Description copied from class: `Matrix`
\n
Returns the column identified by the specified index in this matrix.
\n
\n
Specified by:
\n
`getColumn` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`j` - the column index (range [0..n[).
\n
Returns:
the vector holding the specified column.
\n
• \n
\n\n\n\n
\n
• \n

#### getDiagonal

\n
`public SparseVector<F> getDiagonal()`
\n
Description copied from class: `Matrix`
\n
Returns the diagonal vector.
\n
\n
Specified by:
\n
`getDiagonal` in class `Matrix<F extends Field<F>>`
\n
Returns:
the vector holding the diagonal elements.
\n
• \n
\n\n\n\n
\n
• \n

#### opposite

\n
`public SparseMatrix<F> opposite()`
\n
Description copied from class: `Matrix`
\n
Returns the negation of this matrix.
\n
\n
Specified by:
\n
`opposite` in interface `GroupAdditive<Matrix<F extends Field<F>>>`
\n
Specified by:
\n
`opposite` in class `Matrix<F extends Field<F>>`
\n
Returns:
`-this`.
\n
• \n
\n\n\n\n
\n
• \n

#### plus

\n
`public SparseMatrix<F> plus(Matrix<F> that)`
\n
Description copied from class: `Matrix`
\n
Returns the sum of this matrix with the one specified.
\n
\n
Specified by:
\n
`plus` in interface `GroupAdditive<Matrix<F extends Field<F>>>`
\n
Specified by:
\n
`plus` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`that` - the matrix to be added.
\n
Returns:
`this + that`.
\n
• \n
\n\n\n\n
\n
• \n

#### minus

\n
`public SparseMatrix<F> minus(Matrix<F> that)`
\n
Description copied from class: `Matrix`
\n
Returns the difference between this matrix and the one specified.
\n
\n
Overrides:
\n
`minus` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`that` - the matrix to be subtracted.
\n
Returns:
`this - that`.
\n
• \n
\n\n\n\n\n\n
\n
• \n

#### times

\n
`public SparseMatrix<F> times(F k)`
\n
Description copied from class: `Matrix`
\n
Returns the product of this matrix by the specified factor.
\n
\n
Specified by:
\n
`times` in interface `VectorSpace<Matrix<F extends Field<F>>,F extends Field<F>>`
\n
Specified by:
\n
`times` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`k` - the coefficient multiplier.
\n
Returns:
`this \xc2\xb7 k`
\n
• \n
\n\n\n\n
\n
• \n

#### times

\n
`public SparseVector<F> times(Vector<F> v)`
\n
Description copied from class: `Matrix`
\n
Returns the product of this matrix by the specified vector.
\n
\n
Specified by:
\n
`times` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`v` - the vector.
\n
Returns:
`this \xc2\xb7 v`
\n
• \n
\n\n\n\n
\n
• \n

#### times

\n
`public SparseMatrix<F> times(Matrix<F> that)`
\n
Description copied from class: `Matrix`
\n
Returns the product of this matrix with the one specified.
\n
\n
Specified by:
\n
`times` in interface `Ring<Matrix<F extends Field<F>>>`
\n
Specified by:
\n
`times` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`that` - the matrix multiplier.
\n
Returns:
`this \xc2\xb7 that`.
\n
• \n
\n\n\n\n
\n
• \n

#### inverse

\n
`public SparseMatrix<F> inverse()`
\n
Description copied from class: `Matrix`
\n
Returns the inverse of this matrix (must be square).
\n
\n
Specified by:
\n
`inverse` in class `Matrix<F extends Field<F>>`
\n
Returns:
`1 / this`
\n
• \n
\n\n\n\n
\n
• \n

#### determinant

\n
`public F determinant()`
\n
Description copied from class: `Matrix`
\n
Returns the determinant of this matrix.
\n
\n
Specified by:
\n
`determinant` in class `Matrix<F extends Field<F>>`
\n
Returns:
this matrix determinant.
\n
• \n
\n\n\n\n
\n
• \n

#### solve

\n
`public Matrix<F> solve(Matrix<F> y)`
\n
Description copied from class: `Matrix`
\n
Solves this matrix for the specified matrix (returns `x`\n such as `this \xc2\xb7 x = y`).
\n
\n
Overrides:
\n
`solve` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`y` - the matrix for which the solution is calculated.
\n
Returns:
`x` such as `this \xc2\xb7 x = y`
\n
• \n
\n\n\n\n
\n
• \n

#### transpose

\n
`public SparseMatrix<F> transpose()`
\n
Description copied from class: `Matrix`
\n
Returns the transpose of this matrix.
\n
\n
Specified by:
\n
`transpose` in class `Matrix<F extends Field<F>>`
\n
Returns:
`A\'`.
\n
• \n
\n\n\n\n
\n
• \n

#### cofactor

\n
`public F cofactor(int i,\n         int j)`
\n
Description copied from class: `Matrix`
\n
Returns the cofactor of an element in this matrix. It is the value\n obtained by evaluating the determinant formed by the elements not in\n that particular row or column.
\n
\n
Specified by:
\n
`cofactor` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`i` - the row index.
`j` - the column index.
\n
Returns:
the cofactor of `THIS[i,j]`.
\n
• \n
\n\n\n\n
\n
• \n

\n
`public SparseMatrix<F> adjoint()`
\n
Description copied from class: `Matrix`
\n
Returns the adjoint of this matrix. It is obtained by replacing each\n element in this matrix with its cofactor and applying a + or - sign\n according (-1)**(i+j), and then finding the transpose of the resulting\n matrix.
\n
\n
Specified by:
\n
`adjoint` in class `Matrix<F extends Field<F>>`
\n
Returns:
\n
• \n
\n\n\n\n
\n
• \n

#### tensor

\n
`public SparseMatrix<F> tensor(Matrix<F> that)`
\n
Description copied from class: `Matrix`
\n
Returns the linear algebraic matrix tensor product of this matrix\n and another (Kronecker product). The default implementation returns\n a `DenseMatrix`.
\n
\n
Specified by:
\n
`tensor` in class `Matrix<F extends Field<F>>`
\n
Parameters:
`that` - the second matrix.
\n
Returns:
`this ⊗ that`
\n Wikipedia: Kronecker Product
\n
• \n
\n\n\n\n
\n
• \n

#### vectorization

\n
`public SparseVector<F> vectorization()`
\n
Description copied from class: `Matrix`
\n
Returns the vectorization of this matrix. The vectorization of \n a matrix is the column vector obtain by stacking the columns of the\n matrix on top of one another. The default implementation returns \n a `DenseVector`.
\n
\n
Specified by:
\n
`vectorization` in class `Matrix<F extends Field<F>>`
\n
Returns:
the vectorization of this matrix.
\n Wikipedia: Vectorization.
\n
• \n
\n\n\n\n
\n
• \n

#### copy

\n
`public SparseMatrix<F> copy()`
\n
Description copied from class: `Matrix`
\n
Returns a copy of this matrix \n `allocated` \n by the calling thread (possibly on the stack).
\n
\n
Specified by:
\n
`copy` in interface `javolution.lang.ValueType`
\n
Specified by:
\n
`copy` in class `Matrix<F extends Field<F>>`
\n
Returns:
an identical and independant copy of this matrix.
\n
• \n
\n
• \n
\n
• \n
\n
\n
\n\n\n
\n\n\n\n\n
\n
\n
\n\n\n