package spurs

  1. Overview
  2. Docs

Module Spurs.CsvecSource

Sourcetype 'a t = {
  1. dim : int;
  2. indices : int Common.Dynarray.t;
  3. data : 'a Common.Dynarray.t;
}

A sparse vector, storing the indices of its non-zero data.

It contains a sorted indices array and a corresponding data array.

Sourceval equal : 'a. ('a -> 'a -> Ppx_deriving_runtime.bool) -> 'a t -> 'a t -> Ppx_deriving_runtime.bool
Sourceval get_dim : 'a t -> int
Sourceval get_indices : 'a t -> int Common.Dynarray.t
Sourceval get_data : 'a t -> 'a Common.Dynarray.t
Sourceval copy : 'a t -> 'a t
Sourceexception VectorException of string

Creation functions for CsVec

Sourceval new_trusted : int -> int array -> 'a array -> 'a t
Sourceval new_checked : int -> int Dynarray.t -> 'a Common.Dynarray.t -> ('a t, string) result
Sourceval try_new_csvec : int -> int array -> 'a array -> ('a t, string) result

Create a sparse vector.

Returns an error:

  • If indices and data lengths differ
  • If the indices are out of order
  • If the data is longer than n
Sourceval new_csvec : int -> int array -> 'a array -> 'a t

Same as try_new_csvec, but raises an error if invalid

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

Creates a CsVec, sorting indices and data if necessary.

Returns an error if the lengths mismatch.

Sourceval empty : int -> 'a t

Create an empty CsVec of size n for building purposes

Indexing and Iteration Functions

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

Try to index a vector at a specific index. Returns Some nnz if index exists.

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

Access element at given NNZ index, with constant complexity.

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

Set element at given NNZ index, with constant complexity.

Sourceval get : 'a t -> int -> 'a option

Access element at given index, with logarithmic complexity.

Sourceval set : 'a t -> int -> 'a -> unit option

Set element at given index, with logarithmic complexity. Returns None if the index does not exist already as a non-zero..

Sourceval fold : ('acc -> int -> 'a -> 'acc) -> 'acc -> 'a t -> 'acc

fold f acc v calls f acc index data, through each index-data pair in v

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

iter f v calls f index data on each index-data pair in v

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

map f v returns a new vector with data mapped by f

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

map_inplace f v maps f onto v.data in place.

Sourceval count : ('a -> bool) -> 'a t -> int

Count how many elements make f return true

Sourceval is_empty : 'a t -> bool

Returns whether a vector is empty.

Building vectors

Sourceval append : 'a t -> int -> 'a -> unit

Append an element to the sparse vector. The append should preserve the structure.

Raises an exception if:

  • ind is lower or equal to the last element of v.indices
  • ind is greater than v.dim

Miscellaneous

Sourceval check_structure : 'a t -> (unit, string) result

Check the sparse structure, namely that:

  • indices are sorted
  • all indices are less than dim

Conversions

Sourceval to_dense : float t -> float array

Convert this vector into a dense array.

The rest of the vector is filled with zeroes.

Sourceval to_hashtbl : 'a t -> (int, 'a) Hashtbl.t

Convert this vector into a index -> value hashtbl

Normalization

Sourceval l1_norm : float t -> float

Compute the L1-norm of v

Sourceval squared_l2_norm : float t -> float

Compute the squared L2-norm of v

Sourceval l2_norm : float t -> float

Compute the L2-norm of v

Sourceval norm : float t -> float -> float

Compute the p-order norm of v

Sourceval normalize : float t -> unit

Divides the vector by its own L2-norm in-place. The zero vector is left unchanged

OCaml

Innovation. Community. Security.