SparseMatrix

@Serializable
class SparseMatrix(val rows: Int, val cols: Int, val values: DoubleArray, val columnIndices: IntArray, val rowPointers: IntArray)(source)

Represents a sparse matrix using the Compressed Sparse Row (CSR) format.

The CSR format stores a sparse matrix using three arrays:

  • values: stores the non-zero values of the matrix

  • columnIndices: stores the column indices of the non-zero values

  • rowPointers: stores the starting position of each row in the values array

This format is memory-efficient for sparse matrices where most elements are zero.

Constructors

Link copied to clipboard
constructor(rows: Int, cols: Int, values: DoubleArray, columnIndices: IntArray, rowPointers: IntArray)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val cols: Int

The number of columns in the matrix.

Link copied to clipboard

Array containing the column indices of the non-zero values.

Link copied to clipboard

Array containing the starting position of each row in the values array.

Link copied to clipboard
val rows: Int

The number of rows in the matrix.

Link copied to clipboard

Array containing the non-zero values of the matrix.

Functions

Link copied to clipboard
Link copied to clipboard

Creates a copy of this sparse matrix.

Link copied to clipboard

Returns the density of this sparse matrix (ratio of non-zero elements to total elements).

Link copied to clipboard

Calculates the Frobenius norm of the sparse matrix. The Frobenius norm is the square root of the sum of the squares of all the non-zero elements in the matrix.

Link copied to clipboard
operator fun get(i: Int, j: Int): Double

Gets the value at the specified position in the matrix.

Link copied to clipboard
fun isSymmetric(tolerance: Double = 1.0E-10): Boolean

Checks if this sparse matrix is symmetric within a specified tolerance.

Link copied to clipboard
operator fun minus(other: SparseMatrix): SparseMatrix

Extension function to subtract a SparseMatrix from another SparseMatrix.

Link copied to clipboard
operator fun SparseMatrix.minus(other: Matrix): Matrix

Extension function to subtract a Matrix from a SparseMatrix.

Link copied to clipboard

Returns the number of non-zero elements in this sparse matrix.

Link copied to clipboard
operator fun plus(other: SparseMatrix): SparseMatrix

Extension function to add two SparseMatrices.

Link copied to clipboard
operator fun SparseMatrix.plus(other: Matrix): Matrix

Adds this sparse matrix to another matrix and returns the resulting matrix.

Link copied to clipboard
Link copied to clipboard
operator fun times(scalar: Double): SparseMatrix

Multiplies this sparse matrix by a scalar value.

operator fun times(other: SparseMatrix): SparseMatrix

Multiplies this sparse matrix with another sparse matrix and returns the resulting sparse matrix.

Link copied to clipboard
operator fun SparseMatrix.times(other: Matrix): Matrix

Multiplies this sparse matrix with another matrix and returns the resulting matrix.

Link copied to clipboard

Creates a dense Matrix representation of this sparse matrix.

Link copied to clipboard

Creates a transposed version of this sparse matrix.