Skip to content

SparseMatrix

Johnnoe edited this page Aug 27, 2023 · 4 revisions

Sparse Matrix Documentation

Description

A matrix representation using an Array of Arrays representation, where each non-zero is represented using a Value-ColumnIndex pair

Methods

Method Description
Static Methods
static SparseMatrix identity(size) Creates a sparse identity matrix with the size.
static SparseMatrix zero(size) Creates a zero matrix with the size.
Data Manipulation
void reserve_per_row(columns) Pre-allocates/Reserves a number of columns per row. This should be used before inserting elements to reduce re-allocating repeatedly.
void set_dimensions(rows, columns) Truncates or expands the matrix to fit the dimensions.
Vector2i get_dimensions() Returns a Vector2i, with x = row_number, y = column_number of the matrix.
void set_element(row, column, value) Inserts a non-zero element into the matrix. This is most efficient inserting left-to-right, as appending to the end of a vector does not require array rotations to make space.
double get_element(row, column) Returns the element if it is non-zero. Otherwise returns zero.
SparseMatrix transposed() Returns the transposed version of the matrix.
SparseMatrix clone() Returns an identical copy of the matrix.
Array[SparseMatrixCoordinate] to_coordinate_array() Creates an array of tuples (row, column, value), representing the non-zeros of the matrix.
DenseMatrix to_dense() Returns the matrix as a DenseMatrix, with all zeros and non-zeros being populated.
Algebra Methods
SparseMatrix multiply_sparse(other) Multiplies itself with another SparseMatrix, with the other on the RHS, returning a SparseMatrix.
DenseMatrix multiply_sparse_to_dense(other) Multiplies itself with another SparseMatrix, but the return value is in the DenseMatrix format.
DenseMatrix multiply_dense(other) Multiplies by a DenseMatrix, returning a DenseMatrix.
VectorN multiply_vector(vector) Multiplies by a vector, returning a vector. The vector is treated like a column vector multiplied on the RHS.
SparseMatrix add_sparse(other) Adds the other matrix and itself, returning another Sparse Matrix.
SparseMatrix subtract_sparse(other) Subtracts the other matrix from it, returning a Sparse Matrix.
DenseMatrix add_dense(other) Adds it to a DenseMatrix, and returns a DenseMatrix.
DenseMatrix subtract_dense(other) Subtracts the DenseMatrix from it, returning a DenseMatrix
void multiply_scalar_in_place(scalar) Multiplies the matrix by the scalar, in-place, without creating a copy. If a copy is desired, call matrix.clone(), and then multiply that copy.
VectorN solve_iterative_cg(B, initial_guess, max_iterations) This performs an iterative solve using the Conjugate Gradient Method, starting from the initial_guess. This method is guaranteed to converge on Positive-Definite matrices, but may converge otherwise as well. If initial_guess is null, a zero vector is chosen by default. If max_iterations is negative, then it will be ignored.
double norm_squared() Returns the Frobenius norm squared (equal to summing all values squared).
double norm() Returns the Frobenius norm (equal to the square root of summing all values squared).
Clone this wiki locally