Legend:
Library
Module
Module type
Parameter
Class
Class type
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.
Type of sparse matrices. It is defined in types.ml as record type.
Create sparse matrices
val zeros : ?density:float ->('a, 'b)kind->int ->int ->('a, 'b)t
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.
val binary : ?density:float ->('a, 'b)kind->int ->int ->('a, 'b)t
binary m n creates an m by n random matrix where 10% ~ 15% elements are 1.
val uniform :
?density:float ->?scale:float ->('a, 'b)kind->int ->int ->('a, 'b)t
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 sequential : ('a, 'b)kind->int ->int ->('a, 'b)t
clone 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 concat_vertical : ('a, 'b)t->('a, 'b)t->('a, 'b)t
concat_vertical x y not implemented yet
val concat_horizontal : ('a, 'b)t->('a, 'b)t->('a, 'b)t
concat_horizontal x y not implemented yet
Iterate elements, columns, and rows
val iteri : (int ->int ->'a-> unit)->('a, 'b)t-> unit
iteri 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.
iter 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
val mapi : (int ->int ->'a->'a)->('a, 'b)t->('a, 'b)t
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.
fold 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.
filteri 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) array
Similar to filteri, but the coordinates of the elements are not passed to the function f : float -> bool.
val iteri_rows : (int ->('a, 'b)t-> unit)->('a, 'b)t-> unit
iteri_rows f x iterates every row in x and applies function f : int -> mat -> unit to each of them.
val iter_rows : (('a, 'b)t-> unit)->('a, 'b)t-> unit
Similar to iteri_rows except row number is not passed to f.
val iteri_cols : (int ->('a, 'b)t-> unit)->('a, 'b)t-> unit
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.
val iter_cols : (('a, 'b)t-> unit)->('a, 'b)t-> unit
Similar to iteri_cols except col number is not passed to f.
val mapi_rows : (int ->('a, 'b)t->'c)->('a, 'b)t->'c array
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.
val map_rows : (('a, 'b)t->'c)->('a, 'b)t->'c array
Similar to mapi_rows except row number is not passed to f.
val mapi_cols : (int ->('a, 'b)t->'c)->('a, 'b)t->'c array
mapi_cols f x maps every column in x to a type 'a value by applying function f : int -> mat -> 'a.
val map_cols : (('a, 'b)t->'c)->('a, 'b)t->'c array
Similar to mapi_cols except column number is not passed to f.
val fold_rows : ('c->('a, 'b)t->'c)->'c->('a, 'b)t->'c
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.
val fold_cols : ('c->('a, 'b)t->'c)->'c->('a, 'b)t->'c
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-> unit
iteri_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.
less_equal x y returns true if all the elements in x are not greater than the corresponding elements in y.
Randomisation functions
val permutation_matrix : ('a, 'b)kind->int ->('a, 'b)t
permutation_matrix m returns an m by m permutation matrix.
val draw_rows :
?replacement:bool ->('a, 'b)t->int ->('a, 'b)t * int array
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.
val draw_cols :
?replacement:bool ->('a, 'b)t->int ->('a, 'b)t * int array
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).