Sparse matrix module
Sparse real matrix: this module supports the operations on sparse matrices of real numbers. The module is partly built atop of GSL fucntions. Because GSL only has limited support for sparse matrices. There are some hacks and workarounds in the code.
In the future, I might use a pure OCaml implementation to replace the current solution. At the moment, use with care and let me know if you find bugs.
zeros m n creates an m by n matrix where all the elements are zeros.
This operation is very fast since it only allocates a small amount of memory.
The memory will grow automatically as more elements are inserted.
ones m n creates an m by n matrix where all the elements are ones.
This operation can be very slow if matrix size is big. You might consider to
use dense matrix for better performance in this case.
uniform m n creates an m by n matrix where 10% ~ 15% elements
follow a uniform distribution in (0,1) interval. uniform ~scale:a m n
adjusts the interval to (0,a).
val shape : ('a, 'b) t ‑> int * intIf x is an m by n matrix, shape x returns (m,n), i.e., the size
of two dimensions of x.
val numel : ('a, 'b) t ‑> intnumel x returns the number of elements in matrix x. It is equivalent
to (row_num x) * (col_num x).
val nnz_rows : ('a, 'b) t ‑> int arraynnz_rows x returns the number of non-zero rows in matrix x. A non-zero
row means there is at least one non-zero element in that row.
val density : ('a, 'b) t ‑> floatdensity x returns the density of non-zero element. This operation is
equivalent to nnz x divided by numel x.
val insert : ('a, 'b) t ‑> int ‑> int ‑> 'a ‑> unitval fill : ('a, 'b) t ‑> 'a ‑> unitclone x makes an exact copy of matrix x. Note that the clone becomes
mutable no matter w is mutable or not. This is expecially useful if you
want to modify certain elements in an immutable matrix from math operations.
rows x a returns the rows (defined in an int array a) of x. The
returned rows will be combined into a new sparse matrix. The order of rows in
the new matrix is the same as that in the array a.
val iteri : (int ‑> int ‑> 'a ‑> unit) ‑> ('a, 'b) t ‑> unititeri f x iterates all the elements in x and applies the user defined
function f : int -> int -> float -> 'a. f i j v takes three parameters,
i and j are the coordinates of current element, and v is its value.
val iter : ('a ‑> unit) ‑> ('a, 'b) t ‑> unititer f x is the same as as iteri f x except the coordinates of the
current element is not passed to the function f : float -> 'a
mapi f x maps each element in x to a new value by applying
f : int -> int -> float -> float. The first two parameters are the
coordinates of the element, and the third parameter is the value.
map f x is similar to mapi f x except the coordinates of the
current element is not passed to the function f : float -> float
val foldi : (int ‑> int ‑> 'c ‑> 'a ‑> 'c) ‑> 'c ‑> ('a, 'b) t ‑> 'cval fold : ('c ‑> 'a ‑> 'c) ‑> 'c ‑> ('a, 'b) t ‑> 'cfold f a x folds all the elements in x with the function
f : 'a -> float -> 'a. For an m by n matrix x, the order of folding
is from (0,0) to (m-1,n-1), row by row.
val filteri : (int ‑> int ‑> 'a ‑> bool) ‑> ('a, 'b) t ‑> (int * int) arrayfilteri f x uses f : int -> int -> float -> bool to filter out certain
elements in x. An element will be included if f returns true. The
returned result is a list of coordinates of the selected elements.
val filter : ('a ‑> bool) ‑> ('a, 'b) t ‑> (int * int) arraySimilar to filteri, but the coordinates of the elements are not passed to
the function f : float -> bool.
iteri_cols f x iterates every column in x and applies function
f : int -> mat -> unit to each of them. Column number is passed to f as
the first parameter.
mapi_rows f x maps every row in x to a type 'a value by applying
function f : int -> mat -> 'a to each of them. The results is an array of
all the returned values.
fold_rows f a x folds all the rows in x using function f. The order
of folding is from the first row to the last one.
fold_cols f a x folds all the columns in x using function f. The
order of folding is from the first column to the last one.
val iteri_nz : (int ‑> int ‑> 'a ‑> unit) ‑> ('a, 'b) t ‑> unititeri_nz f x iterates all the non-zero elements in x by applying the
function f : int -> int -> float -> 'a. It is much faster than iteri.
val iter_nz : ('a ‑> unit) ‑> ('a, 'b) t ‑> unitSimilar to iter_nz except the coordinates of elements are not passed to f.
mapi_nz f x is similar to mapi f x but only applies f to non-zero
elements in x. The zeros in x will remain the same in the new matrix.
val foldi_nz : (int ‑> int ‑> 'c ‑> 'a ‑> 'c) ‑> 'c ‑> ('a, 'b) t ‑> 'cval fold_nz : ('c ‑> 'a ‑> 'c) ‑> 'c ‑> ('a, 'b) t ‑> 'cfold_nz f a x is similar to fold f a x but only applies to non-zero
rows in x. zero rows will be simply skipped in folding.
val filteri_nz : (int ‑> int ‑> 'a ‑> bool) ‑> ('a, 'b) t ‑> (int * int) arrayfilteri_nz f x is similar to filter f x but only applies f to
non-zero elements in x.
val filter_nz : ('a ‑> bool) ‑> ('a, 'b) t ‑> (int * int) arrayfilter_nz f x is similar to filteri_nz except that the coordinates of
matrix elements are not passed to f.
fold_rows_nz f a x is similar to fold_rows but only folds non-zero
rows in x using function f. Zero rows will be dropped in iterating x.
fold_cols_nz f a x is similar to fold_cols but only folds non-zero
columns in x using function f. Zero columns will be dropped in iterating x.
val exists : ('a ‑> bool) ‑> ('a, 'b) t ‑> boolexists f x checks all the elements in x using f. If at least one
element satisfies f then the function returns true otherwise false.
val not_exists : ('a ‑> bool) ‑> ('a, 'b) t ‑> boolnot_exists f x checks all the elements in x, the function returns
true only if all the elements fail to satisfy f : float -> bool.
val for_all : ('a ‑> bool) ‑> ('a, 'b) t ‑> boolfor_all f x checks all the elements in x, the function returns true
if and only if all the elements pass the check of function f.
val exists_nz : ('a ‑> bool) ‑> ('a, 'b) t ‑> boolexists_nz f x is similar to exists but only checks non-zero elements.
val not_exists_nz : ('a ‑> bool) ‑> ('a, 'b) t ‑> boolnot_exists_nz f x is similar to not_exists but only checks non-zero elements.
val for_all_nz : ('a ‑> bool) ‑> ('a, 'b) t ‑> boolfor_all_nz f x is similar to for_all_nz but only checks non-zero elements.
val is_positive : ('a, 'b) t ‑> boolis_positive x returns true if all the elements in x are positive.
val is_negative : ('a, 'b) t ‑> boolis_negative x returns true if all the elements in x are negative.
val is_nonpositive : ('a, 'b) t ‑> boolis_nonpositive returns true if all the elements in x are non-positive.
val is_nonnegative : ('a, 'b) t ‑> boolis_nonnegative returns true if all the elements in x are non-negative.
greater_equal x y returns true if all the elements in x are not
smaller than the corresponding elements in y.
less_equal x y returns true if all the elements in x are not
greater than the corresponding elements in y.
draw_rows x m draws m rows randomly from x. The row indices are also
returned in an int array along with the selected rows. The parameter
replacement indicates whether the drawing is by replacement or not.
draw_cols x m draws m cols randomly from x. The column indices are
also returned in an int array along with the selected columns. The parameter
replacement indicates whether the drawing is by replacement or not.
shuffle x shuffles all the elements in x by first shuffling along the
rows then shuffling along columns. It is equivalent to shuffle_cols (shuffle_rows x).
val to_array : ('a, 'b) t ‑> (int array * 'a) arrayval to_dense : ('a, 'b) t ‑> ('a, 'b) Owl_dense_matrix_generic.tto_dense x converts x into a dense matrix.
val of_dense : ('a, 'b) Owl_dense_matrix_generic.t ‑> ('a, 'b) tof_dense x returns a sparse matrix from the dense matrix x.
val pp_spmat : ('a, 'b) t ‑> unitpp_spmat x pretty prints matrix x with headings. Toplevel uses this
function to print out the matrices.
val save : ('a, 'b) t ‑> string ‑> unitsave x f saves the matrix x to a file with the name f. The format
is binary by using Marshal module to serialise the matrix.
load k f loads a sparse matrix from file f. The file must be previously
saved by using save function.
val minmax : (float, 'a) t ‑> float * floatminmax x returns both the minimum and minimum values in x.
val average : ('a, 'b) t ‑> 'aaverage x returns the average value of all the elements in x. It is
equivalent to calculate sum x divided by numel x
average_rows x returns the average value of all row vectors in x. It is
equivalent to div_scalar (sum_rows x) (float_of_int (row_num x)).
average_cols x returns the average value of all column vectors in x.
It is equivalent to div_scalar (sum_cols x) (float_of_int (col_num x)).
abs x returns a new matrix where each element has the absolute value of
that in the original matrix x.
neg x returns a new matrix where each element has the negative value of
that in the original matrix x.
val l1norm : (float, 'b) t ‑> floatval l2norm : (float, 'b) t ‑> float