module Owl_sparse_real:sig..end
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 spmat
types.ml as record type.val zeros : int -> int -> spmatzeros 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.val ones : int -> int -> spmatones 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 eye : int -> spmateye m creates an m by m identity matrix.val binary : int -> int -> spmatbinary m n creates an m by n random matrix where 10% ~ 15% elements are 1.val uniform : ?scale:float -> int -> int -> spmatuniform 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 uniform_int : ?a:int -> ?b:int -> int -> int -> spmatuniform ~a ~b m n creates an m by n matrix where 10% ~ 15% elements
follow a uniform distribution in [a, b] interval. By default, a = 0 and
b = 100.val linspace : float -> float -> int -> spmatlinspace a b n linearly divides the interval [a,b] into n pieces by
creating an m by 1 row vector. E.g., linspace 0. 5. 5 will create a
row vector [0;1;2;3;4;5].val shape : spmat -> int * intx is an m by n matrix, shape x returns (m,n), i.e., the size
of two dimensions of x.val row_num : spmat -> introw_num x returns the number of rows in matrix x.val col_num : spmat -> intcol_num x returns the number of columns in matrix x.val row_num_nz : spmat -> introw_num_nz x returns the number of non-zero rows in matrix x.val col_num_nz : spmat -> intcol_num_nz x returns the number of non-zero columns in matrix x.val numel : spmat -> intnumel x returns the number of elements in matrix x. It is equivalent
to (row_num x) * (col_num x).val nnz : spmat -> intnnz x returns the number of non-zero elements in matrix x.val nnz_rows : spmat -> 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 nnz_cols : spmat -> int arraynnz_cols x returns the number of non-zero cols in matrix x.val density : spmat -> floatdensity x returns the density of non-zero element. This operation is
equivalent to nnz x divided by numel x.val get : spmat -> int -> int -> floatget x i j returns the value of element (i,j) of x.val set : spmat -> int -> int -> float -> unitset x i j a sets the element (i,j) of x to value a.val reset : spmat -> unitreset x resets all the elements in x to 0.val clone : spmat -> spmatclone 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.val transpose : spmat -> spmattranspose x transposes an m by n matrix to n by m one.val diag : spmat -> spmatdiag x returns the diagonal elements of x.val trace : spmat -> floattrace x returns the sum of diagonal elements in x.val row : spmat -> int -> spmatrow x i returns the row i of x.val col : spmat -> int -> spmatcol x j returns the column j of x.val rows : spmat -> int array -> spmatrows 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 cols : spmat -> int array -> spmatrows, cols x a returns the columns (specified in array a)
of x in a new sparse matrix.val iteri : (int -> int -> float -> unit) -> spmat -> 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 : (float -> unit) -> spmat -> 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 -> 'aval mapi : (int -> int -> float -> float) ->
spmat -> spmatmapi 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.val map : (float -> float) -> spmat -> spmatmap f x is similar to mapi f x except the coordinates of the
current element is not passed to the function f : float -> floatval fold : ('a -> float -> 'a) -> 'a -> spmat -> 'afold 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 -> float -> bool) -> spmat -> (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 : (float -> bool) -> spmat -> (int * int) arrayfilteri, but the coordinates of the elements are not passed to
the function f : float -> bool.val iteri_rows : (int -> spmat -> unit) -> spmat -> unititeri_rows f x iterates every row in x and applies function
f : int -> spmat -> unit to each of them.val iter_rows : (spmat -> unit) -> spmat -> unititeri_rows except row number is not passed to f.val iteri_cols : (int -> spmat -> unit) -> spmat -> unititeri_cols f x iterates every column in x and applies function
f : int -> spmat -> unit to each of them. Column number is passed to f as
the first parameter.val iter_cols : (spmat -> unit) -> spmat -> unititeri_cols except col number is not passed to f.val mapi_rows : (int -> spmat -> 'a) -> spmat -> 'a arraymapi_rows f x maps every row in x to a type 'a value by applying
function f : int -> spmat -> 'a to each of them. The results is an array of
all the returned values.val map_rows : (spmat -> 'a) -> spmat -> 'a arraymapi_rows except row number is not passed to f.val mapi_cols : (int -> spmat -> 'a) -> spmat -> 'a arraymapi_cols f x maps every column in x to a type 'a value by applying
function f : int -> spmat -> 'a.val map_cols : (spmat -> 'a) -> spmat -> 'a arraymapi_cols except column number is not passed to f.val fold_rows : ('a -> spmat -> 'a) -> 'a -> spmat -> 'afold_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 : ('a -> spmat -> 'a) -> 'a -> spmat -> 'afold_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 -> float -> unit) -> spmat -> 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 : (float -> unit) -> spmat -> unititer_nz except the coordinates of elements are not passed to f.val mapi_nz : (int -> int -> float -> float) ->
spmat -> spmatmapi_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 map_nz : (float -> float) -> spmat -> spmatmapi_nz except the coordinates of elements are not passed to f.val fold_nz : ('a -> float -> 'a) -> 'a -> spmat -> 'afold_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 -> float -> bool) -> spmat -> (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 : (float -> bool) -> spmat -> (int * int) arrayfilter_nz f x is similar to filteri_nz except that the coordinates of
matrix elements are not passed to f.val iteri_rows_nz : (int -> spmat -> unit) -> spmat -> unititeri_rows_nz f x is similar to iteri_rows but only applies f to
non-zero rows in x.val iter_rows_nz : (spmat -> unit) -> spmat -> unititeri_rows_nz except that row numbers are not passed to f.val iteri_cols_nz : (int -> spmat -> unit) -> spmat -> unititeri_cols_nz f x is similar to iteri_cols but only applies f to
non-zero columns in x.val iter_cols_nz : (spmat -> unit) -> spmat -> unititeri_cols_nz except that column numbers are not passed to f.val mapi_rows_nz : (int -> spmat -> 'a) -> spmat -> 'a arraymapi_rows_nz f x applies f only to the non-zero rows in x.val map_rows_nz : (spmat -> 'a) -> spmat -> 'a arraymapi_rows_nz, but row numbers are not passed to f.val mapi_cols_nz : (int -> spmat -> 'a) -> spmat -> 'a arraymapi_cols_nz f x applies f only to the non-zero columns in x.val map_cols_nz : (spmat -> 'a) -> spmat -> 'a arraymapi_cols_nz, but columns numbers are not passed to f.val fold_rows_nz : ('a -> spmat -> 'a) -> 'a -> spmat -> 'afold_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.val fold_cols_nz : ('a -> spmat -> 'a) -> 'a -> spmat -> 'afold_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 : (float -> bool) -> spmat -> 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 : (float -> bool) -> spmat -> 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 : (float -> bool) -> spmat -> 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 : (float -> bool) -> spmat -> boolexists_nz f x is similar to exists but only checks non-zero elements.val not_exists_nz : (float -> bool) -> spmat -> boolnot_exists_nz f x is similar to not_exists but only checks non-zero elements.val for_all_nz : (float -> bool) -> spmat -> boolfor_all_nz f x is similar to for_all_nz but only checks non-zero elements.val mul_scalar : spmat -> float -> spmatmul_scalar x a multiplies every element in x by a constant factor a.val div_scalar : spmat -> float -> spmatdiv_scalar x a divides every element in x by a constant factor a.val add : spmat -> spmat -> spmatadd x y adds two matrices x and y. Both must have the same dimensions.val sub : spmat -> spmat -> spmatsub x y subtracts the matrix x from y. Both must have the same dimensions.val mul : spmat -> spmat -> spmatmul x y performs an element-wise multiplication, so both x and y
must have the same dimensions.val div : spmat -> spmat -> spmatdiv x y performs an element-wise division, so both x and y
must have the same dimensions.val dot : spmat -> spmat -> spmatdot x y calculates the dot product of an m by n matrix x and
another n by p matrix y.val abs : spmat -> spmatabs x returns a new matrix where each element has the absolute value of
that in the original matrix x.val neg : spmat -> spmatneg x returns a new matrix where each element has the negative value of
that in the original matrix x.val sum : spmat -> floatsum x returns the summation of all the elements in x.val average : spmat -> floataverage x returns the average value of all the elements in x. It is
equivalent to calculate sum x divided by numel xval power : spmat -> float -> spmatpower x a calculates the power of a of each element in x.val is_zero : spmat -> boolis_zero x returns true if all the elements in x are zeros.val is_positive : spmat -> boolis_positive x returns true if all the elements in x are positive.val is_negative : spmat -> boolis_negative x returns true if all the elements in x are negative.val is_nonnegative : spmat -> boolis_nonnegative returns true if all the elements in x are non-negative.val min : spmat -> floatmin x returns the minimum value of all elements in x.val max : spmat -> floatmax x returns the maximum value of all elements in x.val minmax : spmat -> float * floatminmax x returns both the minimum and minimum values in x.val sum_rows : spmat -> spmatsum_rows x returns the summation of all the row vectors in x.val sum_cols : spmat -> spmatsum_cols returns the summation of all the column vectors in x.val average_rows : spmat -> spmataverage_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)).val average_cols : spmat -> spmataverage_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)).val is_equal : spmat -> spmat -> boolis_equal x y returns true if two matrices x and y are equal.val is_unequal : spmat -> spmat -> boolis_unequal x y returns true if there is at least one element in x is
not equal to that in y.val is_greater : spmat -> spmat -> boolis_greater x y returns true if all the elements in x are greater than
the corresponding elements in y.val is_smaller : spmat -> spmat -> boolis_smaller x y returns true if all the elements in x are smaller than
the corresponding elements in y.val equal_or_greater : spmat -> spmat -> boolequal_or_greater x y returns true if all the elements in x are not
smaller than the corresponding elements in y.val equal_or_smaller : spmat -> spmat -> boolequal_or_smaller x y returns true if all the elements in x are not
greater than the corresponding elements in y.val permutation_matrix : int -> spmatpermutation_matrix m returns an m by m permutation matrix.val draw_rows : ?replacement:bool ->
spmat -> int -> spmat * int arraydraw_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 ->
spmat -> int -> spmat * int arraydraw_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.val shuffle_rows : spmat -> spmatshuffle_rows x shuffles all the rows in matrix x.val shuffle_cols : spmat -> spmatshuffle_cols x shuffles all the columns in matrix x.val shuffle : spmat -> spmatshuffle 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_dense : spmat -> Owl_dense_real.matto_dense x converts x into a dense matrix.val of_dense : Owl_dense_real.mat -> spmatof_dense x returns a sparse matrix from the dense matrix x.val print : spmat -> unitprint x pretty prints matrix x without headings.val pp_spmat : spmat -> unitpp_spmat x pretty prints matrix x with headings. Toplevel uses this
function to print out the matrices.val save : spmat -> 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.val load : string -> spmatload f loads a sparse matrix from file f. The file must be previously
saved by using save function.val (+@) : spmat -> spmat -> spmatadd x y, i.e., x +@ yval (-@) : spmat -> spmat -> spmatsub x y, i.e., x -@ yval ( *@ ) : spmat -> spmat -> spmatmul x y, i.e., x *@ yval (/@) : spmat -> spmat -> spmatdiv x y, i.e., x /@ yval ($@) : spmat -> spmat -> spmatdot x y, i.e., x $@ yval ( **@ ) : spmat -> float -> spmatpower x a, i.e., x **@ aval ( *$ ) : spmat -> float -> spmatmul_scalar x a, i.e., x *$ aval (/$) : spmat -> float -> spmatdiv_scalar x a, i.e., x /$ aval ( $* ) : float -> spmat -> spmatmul_scalar x a, i.e., x $* aval ($/) : float -> spmat -> spmatdiv_scalar x a, i.e., x $/ aval (=@) : spmat -> spmat -> boolis_equal x y, i.e., x =@ yval (>@) : spmat -> spmat -> boolis_greater x y, i.e., x >@ yval (<@) : spmat -> spmat -> boolis_smaller x y, i.e., x <@ yval (<>@) : spmat -> spmat -> boolis_unequal x y, i.e., x <>@ yval (>=@) : spmat -> spmat -> boolequal_or_greater x y, i.e., x >=@ yval (<=@) : spmat -> spmat -> boolequal_or_smaller x y, i.e., x <=@ yval (@@) : (float -> float) -> spmat -> spmatmap f x, i.e., f @@ x