package spurs

  1. Overview
  2. Docs

Module Spurs.CsmatSource

Sourcetype compressed_storage =
  1. | CSR
  2. | CSC
Sourceval show_compressed_storage : compressed_storage -> Ppx_deriving_runtime.string
Sourcetype 'a t = {
  1. mutable storage : compressed_storage;
  2. mutable nrows : int;
  3. mutable ncols : int;
  4. indptr : int Common.Dynarray.t;
  5. indices : int Common.Dynarray.t;
  6. data : 'a Common.Dynarray.t;
}
Sourceval equal : 'a. ('a -> 'a -> Ppx_deriving_runtime.bool) -> 'a t -> 'a t -> Ppx_deriving_runtime.bool

Sparse Matrices

In the CSR (Compressed Sparse Row) format, a matrix is represented by three vectors: indptr, indices, and data.

These vectors satisfy the following relation:

  for i in [0, nrows]:
    A(i, indices[indptr[i] .. indptr[i + 1]]) = data[indptr[i] .. indptr[i + 1]]

In the CSC (Compressed Sparse Column) format, the relation becomes:

  for i in [0, ncols]:
    A(indices[indptr[i] .. indptr[i + 1]], i) = data[indptr[i] .. indptr[i + 1]]
Sourceval get_storage : 'a t -> compressed_storage
Sourceval get_nrows : 'a t -> int
Sourceval get_ncols : 'a t -> int
Sourceval get_indptr : 'a t -> int Common.Dynarray.t
Sourceval get_indices : 'a t -> int Common.Dynarray.t
Sourceval get_data : 'a t -> 'a Common.Dynarray.t
Sourceval copy : 'a t -> 'a t
Sourceval print_float_matrix : float t -> unit
Sourceval print_int_matrix : int t -> unit
Sourceval inner_dims : 'a t -> int
Sourceval outer_dims : 'a t -> int
Sourceval set_outer_dims : 'a t -> int -> unit
Sourceval set_inner_dims : 'a t -> int -> unit
Sourceval nnz : 'a t -> int
Sourceexception MatrixException of string

Creation Functions

Sourceval check_structure : int -> int -> int Common.Dynarray.t -> int Common.Dynarray.t -> (unit, string) Result.t

Check the structure of CsMat components, ensuring:

  • indptr is of length outer_dim() + 1
  • indices and data have the same length, nnz == indptr[outer_dims()]
  • indices is sorted for each outer slice
  • indices are lower than inner_dims()
Sourceval new_checked_dyn : compressed_storage -> (int * int) -> int Common.Dynarray.t -> int Common.Dynarray.t -> 'a Common.Dynarray.t -> ('a t, string) result
Sourceval new_checked : compressed_storage -> (int * int) -> int array -> int array -> 'a array -> ('a t, string) result
Sourceval try_new_csr : (int * int) -> int array -> int array -> 'a array -> ('a t, string) result
Sourceval try_new_csc : (int * int) -> int array -> int array -> 'a array -> ('a t, string) result
Sourceval new_csr : (int * int) -> int array -> int array -> 'a array -> 'a t

new_csr indptr indices data creates a new CSR matrix. Raises an exception if the inputs do not describe a valid CSR matrix.

See new_csc for the CSC equivalent.

Sourceval new_csc : (int * int) -> int array -> int array -> 'a array -> 'a t

new_csc indptr indices data creates a new CSC matrix. Raises an exception if the inputs do not describe a valid CSC matrix.

See new_csr for the CSR equivalent.

Sourceval new_from_unsorted : compressed_storage -> (int * int) -> int array -> int array -> 'a array -> ('a t, string) result

Create a new matrix.

Returns Some matrix if the inputs represent a valid sparse matrix, or None if the inputs are invalid.

Sourceval new_csr_from_unsorted : (int * int) -> int array -> int array -> 'a array -> ('a t, string) result

Try to create a CSR matrix.

If necessary, the indices will be sorted.

Sourceval new_csc_from_unsorted : (int * int) -> int array -> int array -> 'a array -> ('a t, string) result

Try to create a CSC matrix.

If necessary, the indices will be sorted.

Sourceval iteroi : (int -> int -> 'a -> 'b) -> 'a t -> unit

Iterates through the matrix, calling f outer inner x on each element.

Sourceval to_other_storage : 'a t -> 'a t

Create a matrix mathematically equal to this one, but with the opposite storage: CSR → CSC, or CSC → CSR.

Sourceval transpose_mut : 'a t -> unit

Transpose a matrix in-place.

Does not create a new matrix!

Sourceval transpose : 'a t -> 'a t

Return the transpose of this matrix, in the other format.

Does not modify the original matrix.

Sourceval csr_from_dense : ?epsilon:float -> float array array -> float t

Create a CSR matrix from a dense matrix, ignoring elements lower than epsilon.

Sourceval csc_from_dense : ?epsilon:float -> float array array -> float t

Create a CSC matrix from a dense matrix, ignoring elements less than epsilon.

Common Matrices

Sourceval eye_csr : int -> float t

Identity matrix, stored as a CSR.

Sourceval eye_csc : int -> float t

Identity matrix, stored as a CSC.

Sourceval empty : compressed_storage -> int -> ('a t, string) result

Create an empty matrix for building purposes

Sourceval zero : (int * int) -> 'a t

Create a new CSR matrix representing the zero matrix.

Matrix Operations

Sourceval scale_inplace : float t -> float -> unit

Scale the values in a sparse matrix inplace

Sourceval scale : float t -> float -> float t

Return a new sparse matrix, scaled by c

Indexing and Iteration

Sourceval get_outer : 'a t -> int -> 'a Csvec.t option

Return the inner vector at outer index outer.

Sourceval get_outer_exn : 'a t -> int -> 'a Csvec.t

Same as get_outer, but raises an exception if the outer index is invalid.

Sourceval itero : (int -> 'a Csvec.t -> 'b) -> 'a t -> unit

Calls f outer v on each outer dimension, where v is the corresponding sparse vector.

Try to find the value at the given outer and inner indices.

Returns None if the indexing is invalid, otherwise returns Some NNZ index.

Sourceval nnz_index_outer_inner : 'a t -> int -> int -> Common.Nnz_index.t option
Sourceval nnz_index : 'a t -> int -> int -> Common.Nnz_index.t option

Find the non-zero index of the element specified by row and column.

This search is logarithmic in the number of non-zeros in the corresponding outer slice. Once available, the `nnz_index` type allows retrieval with O(1) complexity.

Returns None if the element is not found, otherwise returns Some NNZ index.

Sourceval get_nnz : 'a t -> Common.Nnz_index.t -> 'a

Index a sparse matrix using an Nnz_index.t.

Raises an exception if the index is out of bounds.

Sourceval set_nnz : 'a t -> Common.Nnz_index.t -> 'a -> unit

Reassign an index of a sparse matrix using an Nnz_index.t.

Raises an exception if the index is out of bounds.

Sourceval get : 'a t -> (int * int) -> 'a option

Index a sparse matrix using row and column.

Has the same complexity as nnz_index.

Returns None if the row and column are invalid, otherwise returns Some value at that position.

Sourceval set : 'a t -> (int * int) -> 'a -> unit option

Reassign an element using row and column.

Has the same complexity as nnz_index.

Returns None if the row and column are invalid, otherwise sets the value at that position.

Sourceval (.!!()) : 'a t -> Common.Nnz_index.t -> 'a
Sourceval (.!!()<-) : 'a t -> Common.Nnz_index.t -> 'a -> unit
Sourceval (.@()) : 'a t -> (int * int) -> 'a option
Sourceval (.@()<-) : 'a t -> (int * int) -> 'a -> unit

Modifying and building matrices

Sourceval append_outer : ?epsilon:float -> float t -> float array -> unit

Append an outer dimension to an existing matrix, extending the size of the outer dimension by one.

Raises an exception if the vector to add does not have compatible dimension.

Sourceval insert_outer_inner : 'a t -> int -> int -> 'a -> unit
Sourceval insert : 'a t -> int -> int -> 'a -> unit

Insert an element in the matrix. If the element is already present, its value is overwritten.

This is not an efficient operation. However, it is efficient if the elements are inserted in order according to the formatting (for example, row-by-row for CSR matrices)

If the index is out of bounds, the matrix will be resized to the necessary size.

Miscellaneous Functions & Nice-To-Haves

Sourceval density : 'a t -> float

Returns the density (proportion non-zero) of a matrix

Sourceval diag : 'a t -> 'a Csvec.t

Get the diagonal of a sparse matrix. Returns as a sparse vector

Sourceval into_csr : 'a t -> 'a t

Create a new CSR matrix equivalent to this one. If this is a CSR matrix, it is returned as a value. For a version that copies, see to_csr

Sourceval to_csr : 'a t -> 'a t

Create a new CSR matrix equivalent to this one. If this is a CSR matrix, create a copy.

Sourceval into_csc : 'a t -> 'a t

Create a new CSC matrix equivalent to this one. If this is a CSC matrix, it is returned as a value. For a version that copies, see to_csc

Sourceval to_csc : 'a t -> 'a t

Create a new CSC matrix equivalent to this one. If this is a CSC matrix, create a copy.

Sourceval is_csr : 'a t -> bool

Returns true if the input matrix is in CSR format

Sourceval is_csc : 'a t -> bool

Returns true if the input matrix is in CSC format

Sourceval map : ('a -> 'a) -> 'a t -> 'a t

Returns a new sparse matrix with the elements mapped by f

Sourceval map_inplace : ('a -> 'a) -> 'a t -> unit

Maps f over the sparse matrix in-place.

Sourceval max_outer_nnz : 'a t -> int

Returns the maximum number of nonzeros in each outer dimension

Sourceval to_dense : float t -> float array array

Converts into a dense matrix.

Sourceval degrees : 'a t -> int array

Returns a vector containing the degree of each vertex, ie the number of neighbors of each vertex. We do not count diagonal entries as a neighbor.

Sourceval to_inner_onehot : float t -> float t

Generate a one-hot matrix, compressing the inner dimension.

Returns a matrix with the same size, the same CSR/CSC type, and a single value of 1.0 within each populated inner vector.

Sourceval to_col : 'a Csvec.t -> 'a t

Convert this vector into a new matrix with only one column.

Sourceval to_row : 'a Csvec.t -> 'a t

Convert this vector into a new matrix with only one row.