diff --git a/owl.opam b/owl.opam index 2a44f3871..6a4f9f886 100644 --- a/owl.opam +++ b/owl.opam @@ -30,7 +30,6 @@ depends: [ "ctypes" {>= "0.16.0"} "dune" {>= "2.0.0"} "dune-configurator" - "eigen" {>= "0.3.0"} "owl-base" {= version} "stdio" {build} "npy" diff --git a/src/owl/dune b/src/owl/dune index 57c0debca..c5ab628ba 100644 --- a/src/owl/dune +++ b/src/owl/dune @@ -4,8 +4,6 @@ (copy_files# dense/*) -(copy_files# sparse/*) - (copy_files# maths/*) (copy_files# maths/cdflib/*) diff --git a/src/owl/owl.ml b/src/owl/owl.ml index eb67500db..110508f08 100644 --- a/src/owl/owl.ml +++ b/src/owl/owl.ml @@ -22,7 +22,6 @@ let complex64 = Bigarray.complex64 module Const = Owl_const module Exception = Owl_exception module Dense = Owl_dense -module Sparse = Owl_sparse module Maths = Owl_maths module Stats = Owl_stats module Linalg = Owl_linalg diff --git a/src/owl/sparse/owl_sparse.ml b/src/owl/sparse/owl_sparse.ml deleted file mode 100644 index ed36d9915..000000000 --- a/src/owl/sparse/owl_sparse.ml +++ /dev/null @@ -1,10 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** Sparse data structures: matrix & ndarray *) - -module Ndarray = Owl_sparse_ndarray -module Matrix = Owl_sparse_matrix -module Dok_matrix = Owl_sparse_dok_matrix diff --git a/src/owl/sparse/owl_sparse_common.ml b/src/owl/sparse/owl_sparse_common.ml deleted file mode 100644 index 746ec614c..000000000 --- a/src/owl/sparse/owl_sparse_common.ml +++ /dev/null @@ -1,459 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -open Bigarray -open Eigen.Types - -type ('a, 'b) kind = ('a, 'b) Bigarray.kind - -type ('a, 'b) eigen_mat = ('a, 'b) spmat - -(* interface to eigen functions *) - -let _eigen_create : type a b. float -> (a, b) kind -> int -> int -> (a, b) eigen_mat = - fun reserve k m n -> - match k with - | Float32 -> SPMAT_S (Eigen.Sparse.S.create ~reserve m n) - | Float64 -> SPMAT_D (Eigen.Sparse.D.create ~reserve m n) - | Complex32 -> SPMAT_C (Eigen.Sparse.C.create ~reserve m n) - | Complex64 -> SPMAT_Z (Eigen.Sparse.Z.create ~reserve m n) - | _ -> failwith "_eigen_create: unsupported operation" - - -let _eigen_eye : type a b. (a, b) kind -> int -> (a, b) eigen_mat = - fun k m -> - match k with - | Float32 -> SPMAT_S (Eigen.Sparse.S.eye m) - | Float64 -> SPMAT_D (Eigen.Sparse.D.eye m) - | Complex32 -> SPMAT_C (Eigen.Sparse.C.eye m) - | Complex64 -> SPMAT_Z (Eigen.Sparse.Z.eye m) - | _ -> failwith "_eigen_create: unsupported operation" - - -let _eigen_nnz : type a b. (a, b) eigen_mat -> int = - fun x -> - match x with - | SPMAT_S x -> - Eigen.Sparse.S.( - prune x (Owl_const.zero Float32) 0.; - nnz x) - | SPMAT_D x -> - Eigen.Sparse.D.( - prune x (Owl_const.zero Float64) 0.; - nnz x) - | SPMAT_C x -> - Eigen.Sparse.C.( - prune x (Owl_const.zero Complex32) 0.; - nnz x) - | SPMAT_Z x -> - Eigen.Sparse.Z.( - prune x (Owl_const.zero Complex64) 0.; - nnz x) - - -let _eigen_set : type a b. (a, b) eigen_mat -> int -> int -> a -> unit = - fun x i j a -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.set x i j a - | SPMAT_D x -> Eigen.Sparse.D.set x i j a - | SPMAT_C x -> Eigen.Sparse.C.set x i j a - | SPMAT_Z x -> Eigen.Sparse.Z.set x i j a - - -let _eigen_get : type a b. (a, b) eigen_mat -> int -> int -> a = - fun x i j -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.get x i j - | SPMAT_D x -> Eigen.Sparse.D.get x i j - | SPMAT_C x -> Eigen.Sparse.C.get x i j - | SPMAT_Z x -> Eigen.Sparse.Z.get x i j - - -let _eigen_insert : type a b. (a, b) eigen_mat -> int -> int -> a -> unit = - fun x i j a -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.insert x i j a - | SPMAT_D x -> Eigen.Sparse.D.insert x i j a - | SPMAT_C x -> Eigen.Sparse.C.insert x i j a - | SPMAT_Z x -> Eigen.Sparse.Z.insert x i j a - - -let _eigen_reset : type a b. (a, b) eigen_mat -> unit = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.reset x - | SPMAT_D x -> Eigen.Sparse.D.reset x - | SPMAT_C x -> Eigen.Sparse.C.reset x - | SPMAT_Z x -> Eigen.Sparse.Z.reset x - - -let _eigen_copy : type a b. (a, b) eigen_mat -> (a, b) eigen_mat = - fun x -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.clone x) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.clone x) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.clone x) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.clone x) - - -let _eigen_transpose : type a b. (a, b) eigen_mat -> (a, b) eigen_mat = - fun x -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.transpose x) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.transpose x) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.transpose x) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.transpose x) - - -let _eigen_diagonal : type a b. (a, b) eigen_mat -> (a, b) eigen_mat = - fun x -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.diagonal x) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.diagonal x) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.diagonal x) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.diagonal x) - - -let _eigen_trace : type a b. (a, b) eigen_mat -> a = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.trace x - | SPMAT_D x -> Eigen.Sparse.D.trace x - | SPMAT_C x -> Eigen.Sparse.C.trace x - | SPMAT_Z x -> Eigen.Sparse.Z.trace x - - -let _eigen_row : type a b. (a, b) eigen_mat -> int -> (a, b) eigen_mat = - fun x i -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.row x i) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.row x i) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.row x i) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.row x i) - - -let _eigen_col : type a b. (a, b) eigen_mat -> int -> (a, b) eigen_mat = - fun x j -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.col x j) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.col x j) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.col x j) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.col x j) - - -let _eigen_is_compressed : type a b. (a, b) eigen_mat -> bool = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.is_compressed x - | SPMAT_D x -> Eigen.Sparse.D.is_compressed x - | SPMAT_C x -> Eigen.Sparse.C.is_compressed x - | SPMAT_Z x -> Eigen.Sparse.Z.is_compressed x - - -let _eigen_compress : type a b. (a, b) eigen_mat -> unit = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.compress x - | SPMAT_D x -> Eigen.Sparse.D.compress x - | SPMAT_C x -> Eigen.Sparse.C.compress x - | SPMAT_Z x -> Eigen.Sparse.Z.compress x - - -let _eigen_uncompress : type a b. (a, b) eigen_mat -> unit = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.uncompress x - | SPMAT_D x -> Eigen.Sparse.D.uncompress x - | SPMAT_C x -> Eigen.Sparse.C.uncompress x - | SPMAT_Z x -> Eigen.Sparse.Z.uncompress x - - -let _eigen_valueptr : type a b. (a, b) eigen_mat -> (a, b, c_layout) Array1.t = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.valueptr x - | SPMAT_D x -> Eigen.Sparse.D.valueptr x - | SPMAT_C x -> Eigen.Sparse.C.valueptr x - | SPMAT_Z x -> Eigen.Sparse.Z.valueptr x - - -let _eigen_innerindexptr - : type a b. (a, b) eigen_mat -> (int64, int64_elt, c_layout) Array1.t - = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.innerindexptr x - | SPMAT_D x -> Eigen.Sparse.D.innerindexptr x - | SPMAT_C x -> Eigen.Sparse.C.innerindexptr x - | SPMAT_Z x -> Eigen.Sparse.Z.innerindexptr x - - -let _eigen_outerindexptr - : type a b. (a, b) eigen_mat -> (int64, int64_elt, c_layout) Array1.t - = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.outerindexptr x - | SPMAT_D x -> Eigen.Sparse.D.outerindexptr x - | SPMAT_C x -> Eigen.Sparse.C.outerindexptr x - | SPMAT_Z x -> Eigen.Sparse.Z.outerindexptr x - - -let _eigen_print : type a b. (a, b) eigen_mat -> unit = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.print x - | SPMAT_D x -> Eigen.Sparse.D.print x - | SPMAT_C x -> Eigen.Sparse.C.print x - | SPMAT_Z x -> Eigen.Sparse.Z.print x - - -let _eigen_prune : type a b. (a, b) eigen_mat -> a -> float -> unit = - fun x a b -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.prune x a b - | SPMAT_D x -> Eigen.Sparse.D.prune x a b - | SPMAT_C x -> Eigen.Sparse.C.prune x a b - | SPMAT_Z x -> Eigen.Sparse.Z.prune x a b - - -let _eigen_is_zero : type a b. (a, b) eigen_mat -> bool = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.is_zero x - | SPMAT_D x -> Eigen.Sparse.D.is_zero x - | SPMAT_C x -> Eigen.Sparse.C.is_zero x - | SPMAT_Z x -> Eigen.Sparse.Z.is_zero x - - -let _eigen_is_positive : type a b. (a, b) eigen_mat -> bool = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.is_positive x - | SPMAT_D x -> Eigen.Sparse.D.is_positive x - | SPMAT_C x -> Eigen.Sparse.C.is_positive x - | SPMAT_Z x -> Eigen.Sparse.Z.is_positive x - - -let _eigen_is_negative : type a b. (a, b) eigen_mat -> bool = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.is_negative x - | SPMAT_D x -> Eigen.Sparse.D.is_negative x - | SPMAT_C x -> Eigen.Sparse.C.is_negative x - | SPMAT_Z x -> Eigen.Sparse.Z.is_negative x - - -let _eigen_is_nonpositive : type a b. (a, b) eigen_mat -> bool = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.is_nonpositive x - | SPMAT_D x -> Eigen.Sparse.D.is_nonpositive x - | SPMAT_C x -> Eigen.Sparse.C.is_nonpositive x - | SPMAT_Z x -> Eigen.Sparse.Z.is_nonpositive x - - -let _eigen_is_nonnegative : type a b. (a, b) eigen_mat -> bool = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.is_nonnegative x - | SPMAT_D x -> Eigen.Sparse.D.is_nonnegative x - | SPMAT_C x -> Eigen.Sparse.C.is_nonnegative x - | SPMAT_Z x -> Eigen.Sparse.Z.is_nonnegative x - - -let _eigen_equal : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> bool = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> Eigen.Sparse.S.is_equal x y - | SPMAT_D x, SPMAT_D y -> Eigen.Sparse.D.is_equal x y - | SPMAT_C x, SPMAT_C y -> Eigen.Sparse.C.is_equal x y - | SPMAT_Z x, SPMAT_Z y -> Eigen.Sparse.Z.is_equal x y - - -let _eigen_not_equal : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> bool = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> Eigen.Sparse.S.is_unequal x y - | SPMAT_D x, SPMAT_D y -> Eigen.Sparse.D.is_unequal x y - | SPMAT_C x, SPMAT_C y -> Eigen.Sparse.C.is_unequal x y - | SPMAT_Z x, SPMAT_Z y -> Eigen.Sparse.Z.is_unequal x y - - -let _eigen_greater : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> bool = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> Eigen.Sparse.S.is_greater x y - | SPMAT_D x, SPMAT_D y -> Eigen.Sparse.D.is_greater x y - | SPMAT_C x, SPMAT_C y -> Eigen.Sparse.C.is_greater x y - | SPMAT_Z x, SPMAT_Z y -> Eigen.Sparse.Z.is_greater x y - - -let _eigen_less : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> bool = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> Eigen.Sparse.S.is_smaller x y - | SPMAT_D x, SPMAT_D y -> Eigen.Sparse.D.is_smaller x y - | SPMAT_C x, SPMAT_C y -> Eigen.Sparse.C.is_smaller x y - | SPMAT_Z x, SPMAT_Z y -> Eigen.Sparse.Z.is_smaller x y - - -let _eigen_greater_equal : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> bool = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> Eigen.Sparse.S.equal_or_greater x y - | SPMAT_D x, SPMAT_D y -> Eigen.Sparse.D.equal_or_greater x y - | SPMAT_C x, SPMAT_C y -> Eigen.Sparse.C.equal_or_greater x y - | SPMAT_Z x, SPMAT_Z y -> Eigen.Sparse.Z.equal_or_greater x y - - -let _eigen_less_equal : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> bool = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> Eigen.Sparse.S.equal_or_smaller x y - | SPMAT_D x, SPMAT_D y -> Eigen.Sparse.D.equal_or_smaller x y - | SPMAT_C x, SPMAT_C y -> Eigen.Sparse.C.equal_or_smaller x y - | SPMAT_Z x, SPMAT_Z y -> Eigen.Sparse.Z.equal_or_smaller x y - - -let _eigen_add : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> (a, b) eigen_mat = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> SPMAT_S (Eigen.Sparse.S.add x y) - | SPMAT_D x, SPMAT_D y -> SPMAT_D (Eigen.Sparse.D.add x y) - | SPMAT_C x, SPMAT_C y -> SPMAT_C (Eigen.Sparse.C.add x y) - | SPMAT_Z x, SPMAT_Z y -> SPMAT_Z (Eigen.Sparse.Z.add x y) - - -let _eigen_sub : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> (a, b) eigen_mat = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> SPMAT_S (Eigen.Sparse.S.sub x y) - | SPMAT_D x, SPMAT_D y -> SPMAT_D (Eigen.Sparse.D.sub x y) - | SPMAT_C x, SPMAT_C y -> SPMAT_C (Eigen.Sparse.C.sub x y) - | SPMAT_Z x, SPMAT_Z y -> SPMAT_Z (Eigen.Sparse.Z.sub x y) - - -let _eigen_mul : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> (a, b) eigen_mat = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> SPMAT_S (Eigen.Sparse.S.mul x y) - | SPMAT_D x, SPMAT_D y -> SPMAT_D (Eigen.Sparse.D.mul x y) - | SPMAT_C x, SPMAT_C y -> SPMAT_C (Eigen.Sparse.C.mul x y) - | SPMAT_Z x, SPMAT_Z y -> SPMAT_Z (Eigen.Sparse.Z.mul x y) - - -let _eigen_div : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> (a, b) eigen_mat = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> SPMAT_S (Eigen.Sparse.S.div x y) - | SPMAT_D x, SPMAT_D y -> SPMAT_D (Eigen.Sparse.D.div x y) - | SPMAT_C x, SPMAT_C y -> SPMAT_C (Eigen.Sparse.C.div x y) - | SPMAT_Z x, SPMAT_Z y -> SPMAT_Z (Eigen.Sparse.Z.div x y) - - -let _eigen_gemm : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> (a, b) eigen_mat = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> SPMAT_S (Eigen.Sparse.S.gemm x y) - | SPMAT_D x, SPMAT_D y -> SPMAT_D (Eigen.Sparse.D.gemm x y) - | SPMAT_C x, SPMAT_C y -> SPMAT_C (Eigen.Sparse.C.gemm x y) - | SPMAT_Z x, SPMAT_Z y -> SPMAT_Z (Eigen.Sparse.Z.gemm x y) - - -let _eigen_add_scalar : type a b. (a, b) eigen_mat -> a -> (a, b) eigen_mat = - fun x a -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.add_scalar x a) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.add_scalar x a) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.add_scalar x a) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.add_scalar x a) - - -let _eigen_sub_scalar : type a b. (a, b) eigen_mat -> a -> (a, b) eigen_mat = - fun x a -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.sub_scalar x a) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.sub_scalar x a) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.sub_scalar x a) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.sub_scalar x a) - - -let _eigen_mul_scalar : type a b. (a, b) eigen_mat -> a -> (a, b) eigen_mat = - fun x a -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.mul_scalar x a) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.mul_scalar x a) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.mul_scalar x a) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.mul_scalar x a) - - -let _eigen_div_scalar : type a b. (a, b) eigen_mat -> a -> (a, b) eigen_mat = - fun x a -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.div_scalar x a) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.div_scalar x a) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.div_scalar x a) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.div_scalar x a) - - -let _eigen_min : type a b. (a, b) eigen_mat -> a = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.min x - | SPMAT_D x -> Eigen.Sparse.D.min x - | _ -> failwith "_eigen_min: unsupported operation" - - -let _eigen_max : type a b. (a, b) eigen_mat -> a = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.max x - | SPMAT_D x -> Eigen.Sparse.D.max x - | _ -> failwith "_eigen_max: unsupported operation" - - -let _eigen_min2 : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> (a, b) eigen_mat = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> SPMAT_S (Eigen.Sparse.S.min2 x y) - | SPMAT_D x, SPMAT_D y -> SPMAT_D (Eigen.Sparse.D.min2 x y) - | _ -> failwith "_eigen_min2: unsupported operation" - - -let _eigen_max2 : type a b. (a, b) eigen_mat -> (a, b) eigen_mat -> (a, b) eigen_mat = - fun x y -> - match x, y with - | SPMAT_S x, SPMAT_S y -> SPMAT_S (Eigen.Sparse.S.max2 x y) - | SPMAT_D x, SPMAT_D y -> SPMAT_D (Eigen.Sparse.D.max2 x y) - | _ -> failwith "_eigen_max2: unsupported operation" - - -let _eigen_sum : type a b. (a, b) eigen_mat -> a = - fun x -> - match x with - | SPMAT_S x -> Eigen.Sparse.S.sum x - | SPMAT_D x -> Eigen.Sparse.D.sum x - | SPMAT_C x -> Eigen.Sparse.C.sum x - | SPMAT_Z x -> Eigen.Sparse.Z.sum x - - -let _eigen_abs : type a b. (a, b) eigen_mat -> (a, b) eigen_mat = - fun x -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.abs x) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.abs x) - | _ -> failwith "_eigen_abs: unsupported operation" - - -let _eigen_neg : type a b. (a, b) eigen_mat -> (a, b) eigen_mat = - fun x -> - match x with - | SPMAT_S x -> SPMAT_S (Eigen.Sparse.S.neg x) - | SPMAT_D x -> SPMAT_D (Eigen.Sparse.D.neg x) - | SPMAT_C x -> SPMAT_C (Eigen.Sparse.C.neg x) - | SPMAT_Z x -> SPMAT_Z (Eigen.Sparse.Z.neg x) - -(* ends here *) diff --git a/src/owl/sparse/owl_sparse_dok_matrix.ml b/src/owl/sparse/owl_sparse_dok_matrix.ml deleted file mode 100644 index 0b503a30f..000000000 --- a/src/owl/sparse/owl_sparse_dok_matrix.ml +++ /dev/null @@ -1,74 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type ('a, 'b) kind = ('a, 'b) Bigarray.kind - -type ('a, 'b) t = - { mutable m : int - ; (* number of rows *) - mutable n : int - ; (* number of columns *) - mutable k : ('a, 'b) kind - ; (* type of sparse matrices *) - mutable d : (int * int, 'a) Hashtbl.t (* hashtbl for storing data *) - } - -let zeros ?(density = 0.30) k m n = - { m - ; n - ; k - ; d = - (let c = int_of_float (float_of_int (m * n) *. density) in - Hashtbl.create c) - } - - -let row_num x = x.m - -let col_num x = x.n - -let shape x = x.m, x.n - -let numel x = x.m * x.n - -let prune x r = Hashtbl.filter_map_inplace (fun _ v -> if v = r then None else Some v) x.d - -let nnz x = - let _ = prune x (Owl_const.zero x.k) in - Hashtbl.((stats x.d).num_bindings) - - -let density x = float_of_int (nnz x) /. float_of_int (numel x) - -let kind x = x.k - -let _check_boundary i j m n = - if i < 0 || i >= m || j < 0 || j >= n then failwith "error: index beyond the boundary" - - -let set x i j a = - _check_boundary i j x.m x.n; - let _a0 = Owl_const.zero x.k in - match Hashtbl.mem x.d (i, j) with - | true -> if a <> _a0 then Hashtbl.replace x.d (i, j) a else Hashtbl.remove x.d (i, j) - | false -> if a <> _a0 then Hashtbl.add x.d (i, j) a - - -let get x i j = - _check_boundary i j x.m x.n; - match Hashtbl.mem x.d (i, j) with - | true -> Hashtbl.find x.d (i, j) - | false -> Owl_const.zero x.k - - -let reset x = Hashtbl.reset x.d - -let copy x = { m = x.m; n = x.n; k = x.k; d = Hashtbl.copy x.d } - -let iteri_nz f x = Hashtbl.iter (fun (i, j) v -> f i j v) x.d - -let save x f = Owl_io.marshal_to_file x f - -let load _k f = Owl_io.marshal_from_file f diff --git a/src/owl/sparse/owl_sparse_dok_matrix.mli b/src/owl/sparse/owl_sparse_dok_matrix.mli deleted file mode 100644 index 737748565..000000000 --- a/src/owl/sparse/owl_sparse_dok_matrix.mli +++ /dev/null @@ -1,48 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type ('a, 'b) t - -type ('a, 'b) kind = ('a, 'b) Bigarray.kind - -(** {5 Create sparse matrices} *) - -val zeros : ?density:float -> ('a, 'b) kind -> int -> int -> ('a, 'b) t - -(** {5 Obtain the basic properties} *) - -val shape : ('a, 'b) t -> int * int - -val row_num : ('a, 'b) t -> int - -val col_num : ('a, 'b) t -> int - -val numel : ('a, 'b) t -> int - -val nnz : ('a, 'b) t -> int - -val density : ('a, 'b) t -> float - -val kind : ('a, 'b) t -> ('a, 'b) kind - -(** {5 Manipulate a matrix} *) - -val set : ('a, 'b) t -> int -> int -> 'a -> unit - -val get : ('a, 'b) t -> int -> int -> 'a - -val reset : ('a, 'b) t -> unit - -val copy : ('a, 'b) t -> ('a, 'b) t - -(** {5 Iterate elements, columns, and rows} *) - -val iteri_nz : (int -> int -> 'a -> unit) -> ('a, 'b) t -> unit - -(** {5 Input/Output and helper functions} *) - -val save : ('a, 'b) t -> string -> unit - -val load : ('a, 'b) kind -> string -> ('a, 'b) t diff --git a/src/owl/sparse/owl_sparse_matrix.ml b/src/owl/sparse/owl_sparse_matrix.ml deleted file mode 100644 index bec630b1e..000000000 --- a/src/owl/sparse/owl_sparse_matrix.ml +++ /dev/null @@ -1,40 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** Sparse matrix: module aliases *) - -module Operator = struct - include Owl_operator.Make_Basic (Owl_sparse_matrix_generic) - include Owl_operator.Make_Matrix (Owl_sparse_matrix_generic) -end - -module Generic = struct - include Owl_sparse_matrix_generic - include Operator -end - -module S = struct - include Owl_sparse_matrix_s - include Operator -end - -module D = struct - include Owl_sparse_matrix_d - include Operator -end - -module C = struct - include Owl_sparse_matrix_c - include Operator -end - -module Z = struct - include Owl_sparse_matrix_z - include Operator -end - -module DOK = struct - include Owl_sparse_dok_matrix -end diff --git a/src/owl/sparse/owl_sparse_matrix_c.ml b/src/owl/sparse/owl_sparse_matrix_c.ml deleted file mode 100644 index 7f2d80f76..000000000 --- a/src/owl/sparse/owl_sparse_matrix_c.ml +++ /dev/null @@ -1,62 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** [ Complex sparse matrix ] - The default format is compressed row storage (CRS). - *) - -open Bigarray -module M = Owl_sparse_matrix_generic -include M - -type elt = Complex.t - -type mat = (Complex.t, Bigarray.complex32_elt) Owl_sparse_matrix_generic.t - -(* overload functions in Owl_sparse_matrix *) - -let zeros m n = M.zeros Complex32 m n - -let ones m n = M.ones Complex32 m n - -let eye m = M.eye Complex32 m - -let binary m n = M.binary Complex32 m n - -let uniform ?(scale = 1.) m n = M.uniform ~scale Complex32 m n - -let sequential m n = M.sequential Complex32 m n - -let permutation_matrix m = M.permutation_matrix Complex32 m - -let of_array m n x = M.of_array Complex32 m n x - -let load f = M.load Complex32 f - -(* specific functions for complex32 matrix *) - -let _random_basic f m n = - let c = int_of_float (float_of_int (m * n) *. 0.15) in - let x = M.zeros ~density:0.2 Complex32 m n in - let l = Owl_stats.choose (Array.init (m * n) (fun i -> i)) c in - for k = 0 to c - 1 do - let i = l.(k) / n in - let j = l.(k) - (i * n) in - insert x i j (f ()) - done; - x - - -let uniform_int ?(a = 1) ?(b = 99) m n = - _random_basic - (fun () -> - let re = Owl_stats.uniform_int_rvs ~a ~b |> float_of_int in - let im = Owl_stats.uniform_int_rvs ~a ~b |> float_of_int in - Complex.{ re; im }) - m - n - - -(** ends here *) diff --git a/src/owl/sparse/owl_sparse_matrix_c.mli b/src/owl/sparse/owl_sparse_matrix_c.mli deleted file mode 100644 index 3baec1b77..000000000 --- a/src/owl/sparse/owl_sparse_matrix_c.mli +++ /dev/null @@ -1,266 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type elt = Complex.t - -type mat = (Complex.t, Bigarray.complex32_elt) Owl_sparse_matrix_generic.t - -(** {5 Create sparse matrices} *) - -val zeros : int -> int -> mat - -val ones : int -> int -> mat - -val eye : int -> mat - -val binary : int -> int -> mat - -val uniform : ?scale:float -> int -> int -> mat - -val uniform_int : ?a:int -> ?b:int -> int -> int -> mat - -val sequential : int -> int -> mat - -(** {5 Obtain the basic properties of a matrix} *) - -val shape : mat -> int * int - -val row_num : mat -> int - -val col_num : mat -> int - -val row_num_nz : mat -> int - -val col_num_nz : mat -> int - -val numel : mat -> int - -val nnz : mat -> int - -val nnz_rows : mat -> int array - -val nnz_cols : mat -> int array - -val density : mat -> float - -(** {5 Manipulate a matrix} *) - -val insert : mat -> int -> int -> elt -> unit - -val set : mat -> int -> int -> elt -> unit - -val get : mat -> int -> int -> elt - -val reset : mat -> unit - -val fill : mat -> elt -> unit - -val copy : mat -> mat - -val transpose : mat -> mat - -val diag : mat -> mat - -val row : mat -> int -> mat - -val col : mat -> int -> mat - -val rows : mat -> int array -> mat - -val cols : mat -> int array -> mat - -val prune : mat -> elt -> float -> unit - -(** {5 Iterate elements, columns, and rows} *) - -val iteri : (int -> int -> elt -> unit) -> mat -> unit - -val iter : (elt -> unit) -> mat -> unit - -val mapi : (int -> int -> elt -> elt) -> mat -> mat - -val map : (elt -> elt) -> mat -> mat - -val foldi : (int -> int -> 'a -> elt -> 'a) -> 'a -> mat -> 'a - -val fold : ('a -> elt -> 'a) -> 'a -> mat -> 'a - -val filteri : (int -> int -> elt -> bool) -> mat -> (int * int) array - -val filter : (elt -> bool) -> mat -> (int * int) array - -val iteri_rows : (int -> mat -> unit) -> mat -> unit - -val iter_rows : (mat -> unit) -> mat -> unit - -val iteri_cols : (int -> mat -> unit) -> mat -> unit - -val iter_cols : (mat -> unit) -> mat -> unit - -val mapi_rows : (int -> mat -> 'a) -> mat -> 'a array - -val map_rows : (mat -> 'a) -> mat -> 'a array - -val mapi_cols : (int -> mat -> 'a) -> mat -> 'a array - -val map_cols : (mat -> 'a) -> mat -> 'a array - -val fold_rows : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val fold_cols : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val iteri_nz : (int -> int -> elt -> unit) -> mat -> unit - -val iter_nz : (elt -> unit) -> mat -> unit - -val mapi_nz : (int -> int -> elt -> elt) -> mat -> mat - -val map_nz : (elt -> elt) -> mat -> mat - -val foldi_nz : (int -> int -> 'a -> elt -> 'a) -> 'a -> mat -> 'a - -val fold_nz : ('a -> elt -> 'a) -> 'a -> mat -> 'a - -val filteri_nz : (int -> int -> elt -> bool) -> mat -> (int * int) array - -val filter_nz : (elt -> bool) -> mat -> (int * int) array - -val iteri_rows_nz : (int -> mat -> unit) -> mat -> unit - -val iter_rows_nz : (mat -> unit) -> mat -> unit - -val iteri_cols_nz : (int -> mat -> unit) -> mat -> unit - -val iter_cols_nz : (mat -> unit) -> mat -> unit - -val mapi_rows_nz : (int -> mat -> 'a) -> mat -> 'a array - -val map_rows_nz : (mat -> 'a) -> mat -> 'a array - -val mapi_cols_nz : (int -> mat -> 'a) -> mat -> 'a array - -val map_cols_nz : (mat -> 'a) -> mat -> 'a array - -val fold_rows_nz : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val fold_cols_nz : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -(** {5 Examine elements and compare two matrices} *) - -val exists : (elt -> bool) -> mat -> bool - -val not_exists : (elt -> bool) -> mat -> bool - -val for_all : (elt -> bool) -> mat -> bool - -val exists_nz : (elt -> bool) -> mat -> bool - -val not_exists_nz : (elt -> bool) -> mat -> bool - -val for_all_nz : (elt -> bool) -> mat -> bool - -val is_zero : mat -> bool - -val is_positive : mat -> bool - -val is_negative : mat -> bool - -val is_nonnegative : mat -> bool - -val equal : mat -> mat -> bool - -val not_equal : mat -> mat -> bool - -val greater : mat -> mat -> bool - -val less : mat -> mat -> bool - -val greater_equal : mat -> mat -> bool - -val less_equal : mat -> mat -> bool - -(** {5 Randomisation functions} *) - -val permutation_matrix : int -> mat - -val draw_rows : ?replacement:bool -> mat -> int -> mat * int array - -val draw_cols : ?replacement:bool -> mat -> int -> mat * int array - -val shuffle_rows : mat -> mat - -val shuffle_cols : mat -> mat - -val shuffle : mat -> mat - -(** {5 Input/Output and helper functions} *) - -val to_array : mat -> (int array * elt) array - -val of_array : int -> int -> (int array * elt) array -> mat - -val to_dense : mat -> Owl_dense_matrix_c.mat - -val of_dense : Owl_dense_matrix_c.mat -> mat - -val print : mat -> unit - -val save : mat -> string -> unit - -val load : string -> mat - -(** {5 Unary mathematical operations } *) - -val trace : mat -> elt - -val sum : mat -> elt - -val mean : mat -> elt - -val sum_rows : mat -> mat - -val sum_cols : mat -> mat - -val mean_rows : mat -> mat - -val mean_cols : mat -> mat - -val neg : mat -> mat - -(* val l1norm : mat -> elt *) - -(* val l2norm : mat -> elt *) - -(** {5 Binary mathematical operations } *) - -val add : mat -> mat -> mat - -val sub : mat -> mat -> mat - -val mul : mat -> mat -> mat - -val div : mat -> mat -> mat - -val dot : mat -> mat -> mat - -val add_scalar : mat -> elt -> mat - -val sub_scalar : mat -> elt -> mat - -val mul_scalar : mat -> elt -> mat - -val div_scalar : mat -> elt -> mat - -val scalar_add : elt -> mat -> mat - -val scalar_sub : elt -> mat -> mat - -val scalar_mul : elt -> mat -> mat - -val scalar_div : elt -> mat -> mat - -val power_scalar : mat -> elt -> mat - -val mpow : mat -> float -> mat diff --git a/src/owl/sparse/owl_sparse_matrix_d.ml b/src/owl/sparse/owl_sparse_matrix_d.ml deleted file mode 100644 index 6952c079c..000000000 --- a/src/owl/sparse/owl_sparse_matrix_d.ml +++ /dev/null @@ -1,56 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** [ Sparse matrix ] *) - -open Bigarray -module M = Owl_sparse_matrix_generic -include M - -type elt = float - -type mat = (float, Bigarray.float64_elt) Owl_sparse_matrix_generic.t - -(* overload functions in Owl_sparse_matrix_generic *) - -let zeros m n = M.zeros Float64 m n - -let ones m n = M.ones Float64 m n - -let eye m = M.eye Float64 m - -let binary m n = M.binary Float64 m n - -let uniform ?(scale = 1.) m n = M.uniform ~scale Float64 m n - -let sequential m n = M.sequential Float64 m n - -let permutation_matrix m = M.permutation_matrix Float64 m - -let of_array m n x = M.of_array Float64 m n x - -let load f = M.load Float64 f - -(* specific functions for float64 matrix *) - -let _random_basic f m n = - let c = int_of_float (float_of_int (m * n) *. 0.15) in - let x = M.zeros ~density:0.2 Float64 m n in - let l = Owl_stats.choose (Array.init (m * n) (fun i -> i)) c in - for k = 0 to c - 1 do - let i = l.(k) / n in - let j = l.(k) - (i * n) in - insert x i j (f ()) - done; - x - - -let uniform_int ?(a = 0) ?(b = 99) m n = - _random_basic (fun () -> float_of_int (Owl_stats.uniform_int_rvs ~a ~b)) m n - - -let linspace a b n = Owl_dense_matrix_generic.linspace Float64 a b n |> of_dense - -(** ends here *) diff --git a/src/owl/sparse/owl_sparse_matrix_d.mli b/src/owl/sparse/owl_sparse_matrix_d.mli deleted file mode 100644 index 831959fcb..000000000 --- a/src/owl/sparse/owl_sparse_matrix_d.mli +++ /dev/null @@ -1,276 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type elt = float - -type mat = (float, Bigarray.float64_elt) Owl_sparse_matrix_generic.t - -(** {5 Create sparse matrices} *) - -val zeros : int -> int -> mat - -val ones : int -> int -> mat - -val eye : int -> mat - -val binary : int -> int -> mat - -val uniform : ?scale:float -> int -> int -> mat - -val uniform_int : ?a:int -> ?b:int -> int -> int -> mat - -val sequential : int -> int -> mat - -val linspace : elt -> elt -> int -> mat - -(** {5 Obtain the basic properties of a matrix} *) - -val shape : mat -> int * int - -val row_num : mat -> int - -val col_num : mat -> int - -val row_num_nz : mat -> int - -val col_num_nz : mat -> int - -val numel : mat -> int - -val nnz : mat -> int - -val nnz_rows : mat -> int array - -val nnz_cols : mat -> int array - -val density : mat -> float - -(** {5 Manipulate a matrix} *) - -val insert : mat -> int -> int -> elt -> unit - -val get : mat -> int -> int -> elt - -val set : mat -> int -> int -> elt -> unit - -val reset : mat -> unit - -val fill : mat -> elt -> unit - -val copy : mat -> mat - -val transpose : mat -> mat - -val diag : mat -> mat - -val row : mat -> int -> mat - -val col : mat -> int -> mat - -val rows : mat -> int array -> mat - -val cols : mat -> int array -> mat - -val prune : mat -> elt -> float -> unit - -(** {5 Iterate elements, columns, and rows} *) - -val iteri : (int -> int -> elt -> unit) -> mat -> unit - -val iter : (elt -> unit) -> mat -> unit - -val mapi : (int -> int -> elt -> elt) -> mat -> mat - -val map : (elt -> elt) -> mat -> mat - -val foldi : (int -> int -> 'a -> elt -> 'a) -> 'a -> mat -> 'a - -val fold : ('a -> elt -> 'a) -> 'a -> mat -> 'a - -val filteri : (int -> int -> elt -> bool) -> mat -> (int * int) array - -val filter : (elt -> bool) -> mat -> (int * int) array - -val iteri_rows : (int -> mat -> unit) -> mat -> unit - -val iter_rows : (mat -> unit) -> mat -> unit - -val iteri_cols : (int -> mat -> unit) -> mat -> unit - -val iter_cols : (mat -> unit) -> mat -> unit - -val mapi_rows : (int -> mat -> 'a) -> mat -> 'a array - -val map_rows : (mat -> 'a) -> mat -> 'a array - -val mapi_cols : (int -> mat -> 'a) -> mat -> 'a array - -val map_cols : (mat -> 'a) -> mat -> 'a array - -val fold_rows : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val fold_cols : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val iteri_nz : (int -> int -> elt -> unit) -> mat -> unit - -val iter_nz : (elt -> unit) -> mat -> unit - -val mapi_nz : (int -> int -> elt -> elt) -> mat -> mat - -val map_nz : (elt -> elt) -> mat -> mat - -val foldi_nz : (int -> int -> 'a -> elt -> 'a) -> 'a -> mat -> 'a - -val fold_nz : ('a -> elt -> 'a) -> 'a -> mat -> 'a - -val filteri_nz : (int -> int -> elt -> bool) -> mat -> (int * int) array - -val filter_nz : (elt -> bool) -> mat -> (int * int) array - -val iteri_rows_nz : (int -> mat -> unit) -> mat -> unit - -val iter_rows_nz : (mat -> unit) -> mat -> unit - -val iteri_cols_nz : (int -> mat -> unit) -> mat -> unit - -val iter_cols_nz : (mat -> unit) -> mat -> unit - -val mapi_rows_nz : (int -> mat -> 'a) -> mat -> 'a array - -val map_rows_nz : (mat -> 'a) -> mat -> 'a array - -val mapi_cols_nz : (int -> mat -> 'a) -> mat -> 'a array - -val map_cols_nz : (mat -> 'a) -> mat -> 'a array - -val fold_rows_nz : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val fold_cols_nz : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -(** {5 Examine elements and compare two matrices} *) - -val exists : (elt -> bool) -> mat -> bool - -val not_exists : (elt -> bool) -> mat -> bool - -val for_all : (elt -> bool) -> mat -> bool - -val exists_nz : (elt -> bool) -> mat -> bool - -val not_exists_nz : (elt -> bool) -> mat -> bool - -val for_all_nz : (elt -> bool) -> mat -> bool - -val is_zero : mat -> bool - -val is_positive : mat -> bool - -val is_negative : mat -> bool - -val is_nonnegative : mat -> bool - -val equal : mat -> mat -> bool - -val not_equal : mat -> mat -> bool - -val greater : mat -> mat -> bool - -val less : mat -> mat -> bool - -val greater_equal : mat -> mat -> bool - -val less_equal : mat -> mat -> bool - -(** {5 Randomisation functions} *) - -val permutation_matrix : int -> mat - -val draw_rows : ?replacement:bool -> mat -> int -> mat * int array - -val draw_cols : ?replacement:bool -> mat -> int -> mat * int array - -val shuffle_rows : mat -> mat - -val shuffle_cols : mat -> mat - -val shuffle : mat -> mat - -(** {5 Input/Output and helper functions} *) - -val to_array : mat -> (int array * elt) array - -val of_array : int -> int -> (int array * elt) array -> mat - -val to_dense : mat -> Owl_dense.Matrix.D.mat - -val of_dense : Owl_dense.Matrix.D.mat -> mat - -val print : mat -> unit - -val save : mat -> string -> unit - -val load : string -> mat - -(** {5 Unary mathematical operations } *) - -val min : mat -> elt - -val max : mat -> elt - -val minmax : mat -> elt * elt - -val trace : mat -> elt - -val sum : mat -> elt - -val mean : mat -> elt - -val sum_rows : mat -> mat - -val sum_cols : mat -> mat - -val mean_rows : mat -> mat - -val mean_cols : mat -> mat - -val abs : mat -> mat - -val neg : mat -> mat - -val l1norm : mat -> elt - -val l2norm : mat -> elt - -(** {5 Binary mathematical operations } *) - -val add : mat -> mat -> mat - -val sub : mat -> mat -> mat - -val mul : mat -> mat -> mat - -val div : mat -> mat -> mat - -val dot : mat -> mat -> mat - -val add_scalar : mat -> elt -> mat - -val sub_scalar : mat -> elt -> mat - -val mul_scalar : mat -> elt -> mat - -val div_scalar : mat -> elt -> mat - -val scalar_add : elt -> mat -> mat - -val scalar_sub : elt -> mat -> mat - -val scalar_mul : elt -> mat -> mat - -val scalar_div : elt -> mat -> mat - -val power_scalar : mat -> elt -> mat - -val mpow : mat -> float -> mat diff --git a/src/owl/sparse/owl_sparse_matrix_generic.ml b/src/owl/sparse/owl_sparse_matrix_generic.ml deleted file mode 100644 index 3fc494735..000000000 --- a/src/owl/sparse/owl_sparse_matrix_generic.ml +++ /dev/null @@ -1,562 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -open Bigarray -open Owl_sparse_common -open Owl_base_dense_common - -type ('a, 'b) kind = ('a, 'b) Bigarray.kind - -type ('a, 'b) t = - { mutable m : int - ; (* number of rows *) - mutable n : int - ; (* number of columns *) - mutable k : ('a, 'b) kind - ; (* type of sparse matrices *) - mutable d : ('a, 'b) eigen_mat (* point to eigen struct *) - } - -let zeros ?(density = 0.01) k m n = { m; n; k; d = _eigen_create density k m n } - -let eye k m = { m; n = m; k; d = _eigen_eye k m } - -let shape x = x.m, x.n - -let row_num x = x.m - -let col_num x = x.n - -let numel x = x.m * x.n - -let nnz x = _eigen_nnz x.d - -let density x = float_of_int (nnz x) /. float_of_int (numel x) - -let kind x = x.k - -let insert x i j a = if a <> Owl_const.zero x.k then _eigen_insert x.d i j a - -let set x i j a = _eigen_set x.d i j a - -let get x i j = _eigen_get x.d i j - -let reset x = _eigen_reset x.d - -let prune x a eps = _eigen_prune x.d a eps - -let copy x = { m = x.m; n = x.n; k = x.k; d = _eigen_copy x.d } - -let transpose x = { m = x.n; n = x.m; k = x.k; d = _eigen_transpose x.d } - -let diag x = { m = min x.m x.n; n = 1; k = x.k; d = _eigen_diagonal x.d } - -let trace x = _eigen_trace x.d - -let row x i = { m = 1; n = x.n; k = x.k; d = _eigen_row x.d i } - -let col x j = { m = x.m; n = 1; k = x.k; d = _eigen_col x.d j } - -let iteri f x = - for i = 0 to row_num x - 1 do - for j = 0 to col_num x - 1 do - f i j (get x i j) - done - done - - -let iter f x = - for i = 0 to row_num x - 1 do - for j = 0 to col_num x - 1 do - f (get x i j) - done - done - - -let mapi f x = - let d = density x in - let y = zeros ~density:d (kind x) (row_num x) (col_num x) in - iteri (fun i j z -> insert y i j (f i j z)) x; - y - - -let map f x = - let d = density x in - let y = zeros ~density:d (kind x) (row_num x) (col_num x) in - iteri (fun i j z -> insert y i j (f z)) x; - y - - -let _fold_basic iter_fun f a x = - let r = ref a in - iter_fun (fun y -> r := f !r y) x; - !r - - -let fold f a x = _fold_basic iter f a x - -let foldi f a x = - let r = ref a in - iteri (fun i j y -> r := f i j !r y) x; - !r - - -let filteri f x = - let s = Owl_utils.Stack.make () in - iteri (fun i j y -> if f i j y then Owl_utils.Stack.push s (i, j)) x; - Owl_utils.Stack.to_array s - - -let filter f x = filteri (fun _ _ y -> f y) x - -let iteri_nz f x = - let _ = _eigen_compress x.d in - let d = _eigen_valueptr x.d in - let q = _eigen_innerindexptr x.d in - let p = _eigen_outerindexptr x.d in - for i = 0 to x.m - 1 do - for k = Int64.to_int p.{i} to Int64.to_int p.{i + 1} - 1 do - let j = Int64.to_int q.{k} in - f i j d.{k} - done - done - - -let iter_nz f x = - let _ = _eigen_compress x.d in - let d = _eigen_valueptr x.d in - for i = 0 to Array1.dim d - 1 do - f d.{i} - done - - -let mapi_nz f x = - let _ = _eigen_compress x.d in - let d = _eigen_valueptr x.d in - let q = _eigen_innerindexptr x.d in - let p = _eigen_outerindexptr x.d in - let y = copy x in - let e = _eigen_valueptr y.d in - for i = 0 to x.m - 1 do - for k = Int64.to_int p.{i} to Int64.to_int p.{i + 1} - 1 do - let j = Int64.to_int q.{k} in - e.{k} <- f i j d.{k} - done - done; - y - - -let map_nz f x = - let _ = _eigen_compress x.d in - let d = _eigen_valueptr x.d in - let y = copy x in - let e = _eigen_valueptr y.d in - for i = 0 to Array1.dim d - 1 do - e.{i} <- f d.{i} - done; - y - - -let foldi_nz f a x = - let r = ref a in - iteri_nz (fun i j y -> r := f i j !r y) x; - !r - - -let fold_nz f a x = _fold_basic iter_nz f a x - -let filteri_nz f x = - let s = Owl_utils.Stack.make () in - iteri_nz (fun i j y -> if f i j y then Owl_utils.Stack.push s (i, j)) x; - Owl_utils.Stack.to_array s - - -let filter_nz f x = filteri_nz (fun _ _ y -> f y) x - -let _disassemble_rows x = - _eigen_compress x.d; - Owl_log.debug "_disassemble_rows :allocate space"; - let d = Array.init x.m (fun _ -> zeros x.k 1 x.n) in - Owl_log.debug "_disassemble_rows: iteri_nz"; - let _ = iteri_nz (fun i j z -> insert d.(i) 0 j z) x in - Owl_log.debug "_disassemble_rows: ends"; - d - - -let _disassemble_cols x = - _eigen_compress x.d; - let d = Array.init x.n (fun _ -> zeros x.k x.m 1) in - let _ = iteri_nz (fun i j z -> insert d.(j) i 0 z) x in - d - - -let iteri_rows f x = Array.iteri (fun i y -> f i y) (_disassemble_rows x) - -let iter_rows f x = iteri_rows (fun _ y -> f y) x - -let iteri_cols f x = Array.iteri (fun j y -> f j y) (_disassemble_cols x) - -let iter_cols f x = iteri_cols (fun _ y -> f y) x - -let mapi_rows f x = - let a = _disassemble_rows x in - Array.init (row_num x) (fun i -> f i a.(i)) - - -let map_rows f x = mapi_rows (fun _ y -> f y) x - -let mapi_cols f x = - let a = _disassemble_cols x in - Array.init (col_num x) (fun i -> f i a.(i)) - - -let map_cols f x = mapi_cols (fun _ y -> f y) x - -let fold_rows f a x = _fold_basic iter_rows f a x - -let fold_cols f a x = _fold_basic iter_cols f a x - -let iteri_rows_nz f x = iteri_rows (fun i y -> if nnz y != 0 then f i y) x - -let iter_rows_nz f x = iteri_rows_nz (fun _ y -> f y) x - -let iteri_cols_nz f x = iteri_cols (fun i y -> if nnz y != 0 then f i y) x - -let iter_cols_nz f x = iteri_cols_nz (fun _ y -> f y) x - -let mapi_rows_nz f x = - let a = _disassemble_rows x in - let s = Owl_utils.Stack.make () in - Array.iteri (fun i y -> if nnz y != 0 then Owl_utils.Stack.push s (f i y)) a; - Owl_utils.Stack.to_array s - - -let map_rows_nz f x = mapi_rows_nz (fun _ y -> f y) x - -let mapi_cols_nz f x = - let a = _disassemble_cols x in - let s = Owl_utils.Stack.make () in - Array.iteri (fun i y -> if nnz y != 0 then Owl_utils.Stack.push s (f i y)) a; - Owl_utils.Stack.to_array s - - -let map_cols_nz f x = mapi_cols_nz (fun _ y -> f y) x - -let fold_rows_nz f a x = _fold_basic iter_rows_nz f a x - -let fold_cols_nz f a x = _fold_basic iter_cols_nz f a x - -let _exists_basic iter_fun f x = - try - iter_fun (fun y -> if f y = true then failwith "found") x; - false - with - | _exn -> true - - -let exists f x = _exists_basic iter f x - -let not_exists f x = not (exists f x) - -let for_all f x = - let g y = not (f y) in - not_exists g x - - -let exists_nz f x = _exists_basic iter_nz f x - -let not_exists_nz f x = not (exists_nz f x) - -let for_all_nz f x = - let g y = not (f y) in - not_exists_nz g x - - -let is_zero x = _eigen_is_zero x.d - -let is_positive x = _eigen_is_positive x.d - -let is_negative x = _eigen_is_negative x.d - -let is_nonpositive x = _eigen_is_nonpositive x.d - -let is_nonnegative x = _eigen_is_nonnegative x.d - -let equal x1 x2 = _eigen_equal x1.d x2.d - -let not_equal x1 x2 = _eigen_not_equal x1.d x2.d - -let greater x1 x2 = _eigen_greater x1.d x2.d - -let less x1 x2 = _eigen_less x1.d x2.d - -let greater_equal x1 x2 = _eigen_greater_equal x1.d x2.d - -let less_equal x1 x2 = _eigen_less_equal x1.d x2.d - -let add x y = { m = x.m; n = x.n; k = x.k; d = _eigen_add x.d y.d } - -let sub x y = { m = x.m; n = x.n; k = x.k; d = _eigen_sub x.d y.d } - -let mul x y = { m = x.m; n = x.n; k = x.k; d = _eigen_mul x.d y.d } - -let div x y = { m = x.m; n = x.n; k = x.k; d = _eigen_div x.d y.d } - -let dot x y = { m = x.m; n = y.n; k = x.k; d = _eigen_gemm x.d y.d } - -let add_scalar x a = { m = x.m; n = x.n; k = x.k; d = _eigen_add_scalar x.d a } - -let sub_scalar x a = { m = x.m; n = x.n; k = x.k; d = _eigen_sub_scalar x.d a } - -let mul_scalar x a = { m = x.m; n = x.n; k = x.k; d = _eigen_mul_scalar x.d a } - -let div_scalar x a = { m = x.m; n = x.n; k = x.k; d = _eigen_div_scalar x.d a } - -let min x = _eigen_min x.d - -let max x = _eigen_max x.d - -(* TODO: optimise *) -let minmax x = _eigen_min x.d, _eigen_max x.d - -let min2 x y = _eigen_min2 x.d y.d [@@warning "-32"] - -let max2 x y = _eigen_max2 x.d y.d [@@warning "-32"] - -let sum x = _eigen_sum x.d - -let mean x = (_mean_elt x.k) (sum x) (numel x) - -let abs x = { m = x.m; n = x.n; k = x.k; d = _eigen_abs x.d } - -let neg x = { m = x.m; n = x.n; k = x.k; d = _eigen_neg x.d } - -(* TODO: optimise *) -let reci x = - let _op = Owl_base_dense_common._inv_elt (kind x) in - map_nz (fun a -> _op a) x - - -let power_scalar x c = - let _op = Owl_base_dense_common._pow_elt (kind x) in - map (fun y -> _op y c) x - - -let l1norm x = x |> abs |> sum - -let l2norm x = mul x x |> sum |> sqrt - -let scalar_add a x = add_scalar x a - -let scalar_sub a x = sub_scalar x a |> neg - -let scalar_mul a x = mul_scalar x a - -let scalar_div a x = div_scalar x a |> reci - -(** permutation and draw functions *) - -let permutation_matrix k d = - let l = Array.init d (fun x -> x) |> Owl_stats.shuffle in - let y = zeros k d d in - let _a1 = Owl_const.one k in - Array.iteri (fun i j -> insert y i j _a1) l; - y - - -let draw_rows ?(replacement = true) x c = - let m, _n = shape x in - let a = Array.init m (fun i -> i) in - let l = - match replacement with - | true -> Owl_stats.sample a c - | false -> Owl_stats.choose a c - in - let y = zeros (kind x) c m in - let _a1 = Owl_const.one (kind x) in - let _ = Array.iteri (fun i j -> insert y i j _a1) l in - dot y x, l - - -let draw_cols ?(replacement = true) x c = - let _m, n = shape x in - let a = Array.init n (fun i -> i) in - let l = - match replacement with - | true -> Owl_stats.sample a c - | false -> Owl_stats.choose a c - in - let y = zeros (kind x) n c in - let _a1 = Owl_const.one (kind x) in - let _ = Array.iteri (fun j i -> insert y i j _a1) l in - dot x y, l - - -let shuffle_rows x = - let y = permutation_matrix (kind x) (row_num x) in - dot y x - - -let shuffle_cols x = - let y = permutation_matrix (kind x) (col_num x) in - dot x y - - -let shuffle x = x |> shuffle_rows |> shuffle_cols - -let to_dense x = - let y = Owl_dense_matrix_generic.zeros x.k x.m x.n in - iteri_nz (fun i j z -> Owl_dense_matrix_generic.set y i j z) x; - y - - -let of_dense x = - let m, n = Owl_dense_matrix_generic.shape x in - let y = zeros ~density:1. (Owl_dense_matrix_generic.kind x) m n in - for i = 0 to m - 1 do - for j = 0 to n - 1 do - let z = Owl_dense_matrix_generic.get x i j in - insert y i j z - done - done; - y - - -let sum_rows x = - let y = Owl_dense_matrix_generic.ones x.k 1 x.m |> of_dense in - dot y x - - -let sum_cols x = - let y = Owl_dense_matrix_generic.ones x.k x.n 1 |> of_dense in - dot x y - - -let mean_rows x = - let m, _n = shape x in - let k = kind x in - let a = (_mean_elt k) (Owl_const.one k) m in - let y = Owl_dense_matrix_generic.create k 1 m a |> of_dense in - dot y x - - -let mean_cols x = - let _m, n = shape x in - let k = kind x in - let a = (_mean_elt k) (Owl_const.one k) n in - let y = Owl_dense_matrix_generic.create k n 1 a |> of_dense in - dot x y - - -let nnz_rows x = - let s = Hashtbl.create 1000 in - let _ = iteri_nz (fun i _ _ -> if not (Hashtbl.mem s i) then Hashtbl.add s i 0) x in - Hashtbl.fold (fun k _v l -> l @ [ k ]) s [] |> Array.of_list - - -let nnz_cols x = - let s = Hashtbl.create 1000 in - let _ = iteri_nz (fun _ j _ -> if not (Hashtbl.mem s j) then Hashtbl.add s j 0) x in - Hashtbl.fold (fun k _v l -> l @ [ k ]) s [] |> Array.of_list - - -let row_num_nz x = nnz_rows x |> Array.length - -let col_num_nz x = nnz_cols x |> Array.length - -let to_array x = - let y = Array.make (nnz x) ([||], Owl_const.zero x.k) in - let k = ref 0 in - iteri_nz - (fun i j v -> - y.(!k) <- [| i; j |], v; - k := !k + 1) - x; - y - - -let of_array k m n x = - let y = zeros k m n in - Array.iter (fun (i, v) -> insert y i.(0) i.(1) v) x; - y - - -let ones k m n = Owl_dense_matrix_generic.ones k m n |> of_dense - -let sequential k m n = - let x = Owl_dense_matrix_generic.sequential k m n |> of_dense in - _eigen_prune x.d (Owl_const.zero x.k) 0.; - x - - -let fill x a = - let m, n = shape x in - for i = 0 to m - 1 do - for j = 0 to n - 1 do - insert x i j a - done - done - - -let _random_basic d k f m n = - let c = int_of_float (float_of_int (m * n) *. d) in - let x = zeros ~density:(d +. 0.01) k m n in - let l = Owl_stats.choose (Array.init (m * n) (fun i -> i)) c in - for k = 0 to c - 1 do - let i = l.(k) / n in - let j = l.(k) - (i * n) in - insert x i j (f ()) - done; - x - - -let binary ?(density = 0.15) k m n = - let _a1 = Owl_const.one k in - _random_basic density k (fun () -> _a1) m n - - -let uniform ?(density = 0.15) ?(scale = 1.) k m n = - let _op = Owl_ndarray._owl_uniform_fun k in - _random_basic density k (fun () -> _op scale) m n - - -let print x = _eigen_print x.d - -(* TODO: improve the performance *) -let pp_spmat x = - let m, n = shape x in - let c = nnz x in - let p = 100. *. density x in - let mz, nz = row_num_nz x, col_num_nz x in - if m < 100 && n < 100 then Owl_dense_matrix_generic.print (to_dense x); - Printf.printf "shape = (%i,%i) | (%i,%i); nnz = %i (%.1f%%)\n" m n mz nz c p - - -let save x f = Owl_io.marshal_to_file x f - -let load _k f = Owl_io.marshal_from_file f - -(* TODO: optimise *) -let rows x l = - let y = zeros x.k (Array.length l) x.n in - Array.iteri (fun i k -> iteri_nz (fun _ j v -> insert y i j v) (row x k)) l; - y - - -let cols x l = - let y = zeros x.k x.m (Array.length l) in - Array.iteri (fun j k -> iteri_nz (fun i _ v -> insert y i j v) (col x k)) l; - y - - -let concat_vertical _x _y = - failwith "owl_sparse_matrix_generic:concat_vertical:not implemented" - - -let concat_horizontal _x _y = - failwith "owl_sparse_matrix_generic:concat_horizontal:not implemented" - - -let mpow _x _a = failwith "owl_sparse_matrix_generic:mpow:not implemented" diff --git a/src/owl/sparse/owl_sparse_matrix_generic.mli b/src/owl/sparse/owl_sparse_matrix_generic.mli deleted file mode 100644 index c8ebf59e0..000000000 --- a/src/owl/sparse/owl_sparse_matrix_generic.mli +++ /dev/null @@ -1,711 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** Sparse matrix module *) - -(** {5 Type definition} *) - -type ('a, 'b) t -(** -Abstract type of sparse matrices - *) - -type ('a, 'b) kind = ('a, 'b) Bigarray.kind -(** -Type of sparse matrices. It is defined in [types.ml] as record type. - *) - -(** {5 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. - *) - -val ones : ('a, 'b) kind -> int -> int -> ('a, 'b) t -(** -[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 eye : ('a, 'b) kind -> int -> ('a, 'b) t -(** -[eye m] creates an [m] by [m] identity matrix. - *) - -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 -(** TODO *) - -(** {5 Obtain the basic properties} *) - -val shape : ('a, 'b) t -> int * int -(** -If [x] is an [m] by [n] matrix, [shape x] returns [(m,n)], i.e., the size -of two dimensions of [x]. - *) - -val row_num : ('a, 'b) t -> int -(** -[row_num x] returns the number of rows in matrix [x]. - *) - -val col_num : ('a, 'b) t -> int -(** -[col_num x] returns the number of columns in matrix [x]. - *) - -val row_num_nz : ('a, 'b) t -> int -(** -[row_num_nz x] returns the number of non-zero rows in matrix [x]. - *) - -val col_num_nz : ('a, 'b) t -> int -(** -[col_num_nz x] returns the number of non-zero columns in matrix [x]. - *) - -val numel : ('a, 'b) t -> int -(** -[numel x] returns the number of elements in matrix [x]. It is equivalent -to [(row_num x) * (col_num x)]. - *) - -val nnz : ('a, 'b) t -> int -(** -[nnz x] returns the number of non-zero elements in matrix [x]. - *) - -val nnz_rows : ('a, 'b) t -> int array -(** -[nnz_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 : ('a, 'b) t -> int array -(** -[nnz_cols x] returns the number of non-zero cols in matrix [x]. - *) - -val density : ('a, 'b) t -> float -(** -[density x] returns the density of non-zero element. This operation is -equivalent to [nnz x] divided by [numel x]. - *) - -val kind : ('a, 'b) t -> ('a, 'b) kind - -(** {5 Manipulate a matrix} *) - -val get : ('a, 'b) t -> int -> int -> 'a -(** -[set x i j a] sets the element [(i,j)] of [x] to value [a]. - *) - -val set : ('a, 'b) t -> int -> int -> 'a -> unit -(** -[get x i j] returns the value of element [(i,j)] of [x]. - *) - -val insert : ('a, 'b) t -> int -> int -> 'a -> unit - -val reset : ('a, 'b) t -> unit -(** -[reset x] resets all the elements in [x] to [0]. - *) - -val fill : ('a, 'b) t -> 'a -> unit -(** TODO *) - -val copy : ('a, 'b) t -> ('a, 'b) t -(** -[copy x] makes an exact copy of matrix [x]. Note that the copy becomes -mutable no matter [w] is mutable or not. This is especially useful if you -want to modify certain elements in an immutable matrix from math operations. - *) - -val transpose : ('a, 'b) t -> ('a, 'b) t -(** -[transpose x] transposes an [m] by [n] matrix to [n] by [m] one. - *) - -val diag : ('a, 'b) t -> ('a, 'b) t -(** -[diag x] returns the diagonal elements of [x]. - *) - -val row : ('a, 'b) t -> int -> ('a, 'b) t -(** -[row x i] returns the row [i] of [x]. - *) - -val col : ('a, 'b) t -> int -> ('a, 'b) t -(** -[col x j] returns the column [j] of [x]. - *) - -val rows : ('a, 'b) t -> int array -> ('a, 'b) t -(** -[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 cols : ('a, 'b) t -> int array -> ('a, 'b) t -(** -Similar to [rows], [cols x a] returns the columns (specified in array [a]) -of x in a new sparse matrix. - *) - -val prune : ('a, 'b) t -> 'a -> float -> unit -(** -[prune x ...] - *) - -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 - *) - -(** {5 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. - *) - -val iter : ('a -> unit) -> ('a, 'b) t -> unit -(** -[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. - *) - -val map : ('a -> 'a) -> ('a, 'b) t -> ('a, 'b) t -(** -[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 -> 'c - -val fold : ('c -> 'a -> 'c) -> 'c -> ('a, 'b) t -> 'c -(** -[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. - *) - -val filteri : (int -> int -> 'a -> bool) -> ('a, 'b) t -> (int * int) array -(** -[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]. - *) - -val iter_nz : ('a -> unit) -> ('a, 'b) t -> unit -(** -Similar to [iter_nz] except the coordinates of elements are not passed to [f]. - *) - -val mapi_nz : (int -> int -> 'a -> 'a) -> ('a, 'b) t -> ('a, 'b) t -(** -[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 map_nz : ('a -> 'a) -> ('a, 'b) t -> ('a, 'b) t -(** -Similar to [mapi_nz] except the coordinates of elements are not passed to [f]. - *) - -val foldi_nz : (int -> int -> 'c -> 'a -> 'c) -> 'c -> ('a, 'b) t -> 'c -(** TODO *) - -val fold_nz : ('c -> 'a -> 'c) -> 'c -> ('a, 'b) t -> 'c -(** -[fold_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) array -(** -[filteri_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) array -(** -[filter_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 -> ('a, 'b) t -> unit) -> ('a, 'b) t -> unit -(** -[iteri_rows_nz f x] is similar to [iteri_rows] but only applies [f] to -non-zero rows in [x]. - *) - -val iter_rows_nz : (('a, 'b) t -> unit) -> ('a, 'b) t -> unit -(** -Similar to [iteri_rows_nz] except that row numbers are not passed to [f]. - *) - -val iteri_cols_nz : (int -> ('a, 'b) t -> unit) -> ('a, 'b) t -> unit -(** -[iteri_cols_nz f x] is similar to [iteri_cols] but only applies [f] to -non-zero columns in [x]. - *) - -val iter_cols_nz : (('a, 'b) t -> unit) -> ('a, 'b) t -> unit -(** -Similar to [iteri_cols_nz] except that column numbers are not passed to [f]. - *) - -val mapi_rows_nz : (int -> ('a, 'b) t -> 'c) -> ('a, 'b) t -> 'c array -(** -[mapi_rows_nz f x] applies [f] only to the non-zero rows in [x]. - *) - -val map_rows_nz : (('a, 'b) t -> 'c) -> ('a, 'b) t -> 'c array -(** -Similar to [mapi_rows_nz], but row numbers are not passed to [f]. - *) - -val mapi_cols_nz : (int -> ('a, 'b) t -> 'c) -> ('a, 'b) t -> 'c array -(** -[mapi_cols_nz f x] applies [f] only to the non-zero columns in [x]. - *) - -val map_cols_nz : (('a, 'b) t -> 'c) -> ('a, 'b) t -> 'c array -(** -Similar to [mapi_cols_nz], but columns numbers are not passed to [f]. - *) - -val fold_rows_nz : ('c -> ('a, 'b) t -> 'c) -> 'c -> ('a, 'b) t -> 'c -(** -[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]. - *) - -val fold_cols_nz : ('c -> ('a, 'b) t -> 'c) -> 'c -> ('a, 'b) t -> 'c -(** -[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]. - *) - -(** {5 Examine elements and compare two matrices} *) - -val exists : ('a -> bool) -> ('a, 'b) t -> bool -(** -[exists 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 -> bool -(** -[not_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 -> bool -(** -[for_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 -> bool -(** -[exists_nz f x] is similar to [exists] but only checks non-zero elements. - *) - -val not_exists_nz : ('a -> bool) -> ('a, 'b) t -> bool -(** -[not_exists_nz f x] is similar to [not_exists] but only checks non-zero elements. - *) - -val for_all_nz : ('a -> bool) -> ('a, 'b) t -> bool -(** -[for_all_nz f x] is similar to [for_all_nz] but only checks non-zero elements. - *) - -val is_zero : ('a, 'b) t -> bool -(** -[is_zero x] returns [true] if all the elements in [x] are zeros. - *) - -val is_positive : ('a, 'b) t -> bool -(** -[is_positive x] returns [true] if all the elements in [x] are positive. - *) - -val is_negative : ('a, 'b) t -> bool -(** -[is_negative x] returns [true] if all the elements in [x] are negative. - *) - -val is_nonpositive : ('a, 'b) t -> bool -(** -[is_nonpositive] returns [true] if all the elements in [x] are non-positive. - *) - -val is_nonnegative : ('a, 'b) t -> bool -(** -[is_nonnegative] returns [true] if all the elements in [x] are non-negative. - *) - -val equal : ('a, 'b) t -> ('a, 'b) t -> bool -(** -[equal x y] returns [true] if two matrices [x] and [y] are equal. - *) - -val not_equal : ('a, 'b) t -> ('a, 'b) t -> bool -(** -[not_equal x y] returns [true] if there is at least one element in [x] is -not equal to that in [y]. - *) - -val greater : ('a, 'b) t -> ('a, 'b) t -> bool -(** -[greater x y] returns [true] if all the elements in [x] are greater than -the corresponding elements in [y]. - *) - -val less : ('a, 'b) t -> ('a, 'b) t -> bool -(** -[less x y] returns [true] if all the elements in [x] are smaller than -the corresponding elements in [y]. - *) - -val greater_equal : ('a, 'b) t -> ('a, 'b) t -> bool -(** -[greater_equal x y] returns [true] if all the elements in [x] are not -smaller than the corresponding elements in [y]. - *) - -val less_equal : ('a, 'b) t -> ('a, 'b) t -> bool -(** -[less_equal x y] returns [true] if all the elements in [x] are not -greater than the corresponding elements in [y]. - *) - -(** {5 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. - *) - -val shuffle_rows : ('a, 'b) t -> ('a, 'b) t -(** -[shuffle_rows x] shuffles all the rows in matrix [x]. - *) - -val shuffle_cols : ('a, 'b) t -> ('a, 'b) t -(** -[shuffle_cols x] shuffles all the columns in matrix [x]. - *) - -val shuffle : ('a, 'b) t -> ('a, 'b) t -(** -[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)]. - *) - -(** {5 Input/Output and helper functions} *) - -val to_array : ('a, 'b) t -> (int array * 'a) array -(** TODO *) - -val of_array : ('a, 'b) kind -> int -> int -> (int array * 'a) array -> ('a, 'b) t -(** TODO *) - -val to_dense : ('a, 'b) t -> ('a, 'b) Owl_dense_matrix_generic.t -(** -[to_dense x] converts [x] into a dense matrix. - *) - -val of_dense : ('a, 'b) Owl_dense_matrix_generic.t -> ('a, 'b) t -(** -[of_dense x] returns a sparse matrix from the dense matrix [x]. - *) - -val print : ('a, 'b) t -> unit -(** -[print x] pretty prints matrix [x] without headings. - *) - -val pp_spmat : ('a, 'b) t -> unit - [@@ocaml.toplevel_printer] -(** -[pp_spmat x] pretty prints matrix [x] with headings. Toplevel uses this -function to print out the matrices. - *) - -val save : ('a, 'b) t -> string -> unit -(** -[save 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 : ('a, 'b) kind -> string -> ('a, 'b) t -(** -[load k f] loads a sparse matrix from file [f]. The file must be previously -saved by using [save] function. - *) - -(** {5 Unary mathematical operations } *) - -val min : (float, 'a) t -> float -(** -[min x] returns the minimum value of all elements in [x]. - *) - -val max : (float, 'a) t -> float -(** -[max x] returns the maximum value of all elements in [x]. - *) - -val minmax : (float, 'a) t -> float * float -(** -[minmax x] returns both the minimum and minimum values in [x]. - *) - -val trace : ('a, 'b) t -> 'a -(** -[trace x] returns the sum of diagonal elements in [x]. - *) - -val sum : ('a, 'b) t -> 'a -(** -[sum x] returns the summation of all the elements in [x]. - *) - -val mean : ('a, 'b) t -> 'a -(** -[mean x] returns the mean value of all the elements in [x]. It is -equivalent to calculate [sum x] divided by [numel x] - *) - -val sum_rows : ('a, 'b) t -> ('a, 'b) t -(** -[sum_rows x] returns the summation of all the row vectors in [x]. - *) - -val sum_cols : ('a, 'b) t -> ('a, 'b) t -(** -[sum_cols] returns the summation of all the column vectors in [x]. - *) - -val mean_rows : ('a, 'b) t -> ('a, 'b) t -(** -[mean_rows x] returns the mean value of all row vectors in [x]. It is -equivalent to [div_scalar (sum_rows x) (float_of_int (row_num x))]. - *) - -val mean_cols : ('a, 'b) t -> ('a, 'b) t -(** -[mean_cols x] returns the mean value of all column vectors in [x]. -It is equivalent to [div_scalar (sum_cols x) (float_of_int (col_num x))]. - *) - -val abs : (float, 'a) t -> (float, 'a) t -(** -[abs x] returns a new matrix where each element has the absolute value of -that in the original matrix [x]. - *) - -val neg : ('a, 'b) t -> ('a, 'b) t -(** -[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 -> float -(** TODO *) - -val l2norm : (float, 'b) t -> float -(** TODO *) - -(** {5 Binary mathematical operations } *) - -val add : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** -[add x y] adds two matrices [x] and [y]. Both must have the same dimensions. - *) - -val sub : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** -[sub x y] subtracts the matrix [x] from [y]. Both must have the same dimensions. - *) - -val mul : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** -[mul x y] performs an element-wise multiplication, so both [x] and [y] -must have the same dimensions. - *) - -val div : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** -[div x y] performs an element-wise division, so both [x] and [y] -must have the same dimensions. - *) - -val dot : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** -[dot x y] calculates the dot product of an [m] by [n] matrix [x] and -another [n] by [p] matrix [y]. - *) - -val add_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t - -(* [add_scalar] only applies to non-zero elements .*) - -val sub_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t - -(* [sub_scalar] only applies to non-zero elements .*) - -val mul_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t -(** -[mul_scalar x a] multiplies every element in [x] by a constant factor [a]. - *) - -val div_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t -(** -[div_scalar x a] divides every element in [x] by a constant factor [a]. - *) - -val scalar_add : 'a -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val scalar_sub : 'a -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val scalar_mul : 'a -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val scalar_div : 'a -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val power_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t -(** -[power x a] calculates the power of [a] of each element in [x]. - *) - -val mpow : ('a, 'b) t -> float -> ('a, 'b) t -(** TODO: not implemented, just a place holder. *) - -(** ends here *) diff --git a/src/owl/sparse/owl_sparse_matrix_s.ml b/src/owl/sparse/owl_sparse_matrix_s.ml deleted file mode 100644 index 58d057b3e..000000000 --- a/src/owl/sparse/owl_sparse_matrix_s.ml +++ /dev/null @@ -1,56 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** [ Sparse matrix ] *) - -open Bigarray -module M = Owl_sparse_matrix_generic -include M - -type elt = float - -type mat = (float, Bigarray.float32_elt) Owl_sparse_matrix_generic.t - -(* overload functions in Owl_sparse_matrix_generic *) - -let zeros m n = M.zeros Float32 m n - -let ones m n = M.ones Float32 m n - -let eye m = M.eye Float32 m - -let binary m n = M.binary Float32 m n - -let uniform ?(scale = 1.) m n = M.uniform ~scale Float32 m n - -let sequential m n = M.sequential Float32 m n - -let permutation_matrix m = M.permutation_matrix Float32 m - -let of_array m n x = M.of_array Float32 m n x - -let load f = M.load Float32 f - -(* specific functions for float32 matrix *) - -let _random_basic f m n = - let c = int_of_float (float_of_int (m * n) *. 0.15) in - let x = M.zeros ~density:0.2 Float32 m n in - let l = Owl_stats.choose (Array.init (m * n) (fun i -> i)) c in - for k = 0 to c - 1 do - let i = l.(k) / n in - let j = l.(k) - (i * n) in - insert x i j (f ()) - done; - x - - -let uniform_int ?(a = 0) ?(b = 99) m n = - _random_basic (fun () -> float_of_int (Owl_stats.uniform_int_rvs ~a ~b)) m n - - -let linspace a b n = Owl_dense_matrix_generic.linspace Float32 a b n |> of_dense - -(** ends here *) diff --git a/src/owl/sparse/owl_sparse_matrix_s.mli b/src/owl/sparse/owl_sparse_matrix_s.mli deleted file mode 100644 index 1430b394e..000000000 --- a/src/owl/sparse/owl_sparse_matrix_s.mli +++ /dev/null @@ -1,276 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type elt = float - -type mat = (float, Bigarray.float32_elt) Owl_sparse_matrix_generic.t - -(** {5 Create sparse matrices} *) - -val zeros : int -> int -> mat - -val ones : int -> int -> mat - -val eye : int -> mat - -val binary : int -> int -> mat - -val uniform : ?scale:float -> int -> int -> mat - -val uniform_int : ?a:int -> ?b:int -> int -> int -> mat - -val sequential : int -> int -> mat - -val linspace : elt -> elt -> int -> mat - -(** {5 Obtain the basic properties of a matrix} *) - -val shape : mat -> int * int - -val row_num : mat -> int - -val col_num : mat -> int - -val row_num_nz : mat -> int - -val col_num_nz : mat -> int - -val numel : mat -> int - -val nnz : mat -> int - -val nnz_rows : mat -> int array - -val nnz_cols : mat -> int array - -val density : mat -> float - -(** {5 Manipulate a matrix} *) - -val insert : mat -> int -> int -> elt -> unit - -val get : mat -> int -> int -> elt - -val set : mat -> int -> int -> elt -> unit - -val reset : mat -> unit - -val fill : mat -> elt -> unit - -val copy : mat -> mat - -val transpose : mat -> mat - -val diag : mat -> mat - -val row : mat -> int -> mat - -val col : mat -> int -> mat - -val rows : mat -> int array -> mat - -val cols : mat -> int array -> mat - -val prune : mat -> elt -> float -> unit - -(** {5 Iterate elements, columns, and rows} *) - -val iteri : (int -> int -> elt -> unit) -> mat -> unit - -val iter : (elt -> unit) -> mat -> unit - -val mapi : (int -> int -> elt -> elt) -> mat -> mat - -val map : (elt -> elt) -> mat -> mat - -val foldi : (int -> int -> 'a -> elt -> 'a) -> 'a -> mat -> 'a - -val fold : ('a -> elt -> 'a) -> 'a -> mat -> 'a - -val filteri : (int -> int -> elt -> bool) -> mat -> (int * int) array - -val filter : (elt -> bool) -> mat -> (int * int) array - -val iteri_rows : (int -> mat -> unit) -> mat -> unit - -val iter_rows : (mat -> unit) -> mat -> unit - -val iteri_cols : (int -> mat -> unit) -> mat -> unit - -val iter_cols : (mat -> unit) -> mat -> unit - -val mapi_rows : (int -> mat -> 'a) -> mat -> 'a array - -val map_rows : (mat -> 'a) -> mat -> 'a array - -val mapi_cols : (int -> mat -> 'a) -> mat -> 'a array - -val map_cols : (mat -> 'a) -> mat -> 'a array - -val fold_rows : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val fold_cols : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val iteri_nz : (int -> int -> elt -> unit) -> mat -> unit - -val iter_nz : (elt -> unit) -> mat -> unit - -val mapi_nz : (int -> int -> elt -> elt) -> mat -> mat - -val map_nz : (elt -> elt) -> mat -> mat - -val foldi_nz : (int -> int -> 'a -> elt -> 'a) -> 'a -> mat -> 'a - -val fold_nz : ('a -> elt -> 'a) -> 'a -> mat -> 'a - -val filteri_nz : (int -> int -> elt -> bool) -> mat -> (int * int) array - -val filter_nz : (elt -> bool) -> mat -> (int * int) array - -val iteri_rows_nz : (int -> mat -> unit) -> mat -> unit - -val iter_rows_nz : (mat -> unit) -> mat -> unit - -val iteri_cols_nz : (int -> mat -> unit) -> mat -> unit - -val iter_cols_nz : (mat -> unit) -> mat -> unit - -val mapi_rows_nz : (int -> mat -> 'a) -> mat -> 'a array - -val map_rows_nz : (mat -> 'a) -> mat -> 'a array - -val mapi_cols_nz : (int -> mat -> 'a) -> mat -> 'a array - -val map_cols_nz : (mat -> 'a) -> mat -> 'a array - -val fold_rows_nz : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val fold_cols_nz : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -(** {5 Examine elements and compare two matrices} *) - -val exists : (elt -> bool) -> mat -> bool - -val not_exists : (elt -> bool) -> mat -> bool - -val for_all : (elt -> bool) -> mat -> bool - -val exists_nz : (elt -> bool) -> mat -> bool - -val not_exists_nz : (elt -> bool) -> mat -> bool - -val for_all_nz : (elt -> bool) -> mat -> bool - -val is_zero : mat -> bool - -val is_positive : mat -> bool - -val is_negative : mat -> bool - -val is_nonnegative : mat -> bool - -val equal : mat -> mat -> bool - -val not_equal : mat -> mat -> bool - -val greater : mat -> mat -> bool - -val less : mat -> mat -> bool - -val greater_equal : mat -> mat -> bool - -val less_equal : mat -> mat -> bool - -(** {5 Randomisation functions} *) - -val permutation_matrix : int -> mat - -val draw_rows : ?replacement:bool -> mat -> int -> mat * int array - -val draw_cols : ?replacement:bool -> mat -> int -> mat * int array - -val shuffle_rows : mat -> mat - -val shuffle_cols : mat -> mat - -val shuffle : mat -> mat - -(** {5 Input/Output and helper functions} *) - -val to_array : mat -> (int array * elt) array - -val of_array : int -> int -> (int array * elt) array -> mat - -val to_dense : mat -> Owl_dense_matrix_s.mat - -val of_dense : Owl_dense_matrix_s.mat -> mat - -val print : mat -> unit - -val save : mat -> string -> unit - -val load : string -> mat - -(** {5 Unary mathematical operations } *) - -val min : mat -> elt - -val max : mat -> elt - -val minmax : mat -> elt * elt - -val trace : mat -> elt - -val sum : mat -> elt - -val mean : mat -> elt - -val sum_rows : mat -> mat - -val sum_cols : mat -> mat - -val mean_rows : mat -> mat - -val mean_cols : mat -> mat - -val abs : mat -> mat - -val neg : mat -> mat - -val l1norm : mat -> elt - -val l2norm : mat -> elt - -(** {5 Binary mathematical operations } *) - -val add : mat -> mat -> mat - -val sub : mat -> mat -> mat - -val mul : mat -> mat -> mat - -val div : mat -> mat -> mat - -val dot : mat -> mat -> mat - -val add_scalar : mat -> elt -> mat - -val sub_scalar : mat -> elt -> mat - -val mul_scalar : mat -> elt -> mat - -val div_scalar : mat -> elt -> mat - -val scalar_add : elt -> mat -> mat - -val scalar_sub : elt -> mat -> mat - -val scalar_mul : elt -> mat -> mat - -val scalar_div : elt -> mat -> mat - -val power_scalar : mat -> elt -> mat - -val mpow : mat -> float -> mat diff --git a/src/owl/sparse/owl_sparse_matrix_z.ml b/src/owl/sparse/owl_sparse_matrix_z.ml deleted file mode 100644 index 36fd49938..000000000 --- a/src/owl/sparse/owl_sparse_matrix_z.ml +++ /dev/null @@ -1,62 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** [ Complex sparse matrix ] - The default format is compressed row storage (CRS). - *) - -open Bigarray -module M = Owl_sparse_matrix_generic -include M - -type elt = Complex.t - -type mat = (Complex.t, Bigarray.complex64_elt) Owl_sparse_matrix_generic.t - -(* overload functions in Owl_sparse_matrix *) - -let zeros m n = M.zeros Complex64 m n - -let ones m n = M.ones Complex64 m n - -let eye m = M.eye Complex64 m - -let binary m n = M.binary Complex64 m n - -let uniform ?(scale = 1.) m n = M.uniform ~scale Complex64 m n - -let sequential m n = M.sequential Complex64 m n - -let permutation_matrix m = M.permutation_matrix Complex64 m - -let of_array m n x = M.of_array Complex64 m n x - -let load f = M.load Complex64 f - -(* specific functions for complex64 matrix *) - -let _random_basic f m n = - let c = int_of_float (float_of_int (m * n) *. 0.15) in - let x = M.zeros ~density:0.2 Complex64 m n in - let l = Owl_stats.choose (Array.init (m * n) (fun i -> i)) c in - for k = 0 to c - 1 do - let i = l.(k) / n in - let j = l.(k) - (i * n) in - insert x i j (f ()) - done; - x - - -let uniform_int ?(a = 1) ?(b = 99) m n = - _random_basic - (fun () -> - let re = Owl_stats.uniform_int_rvs ~a ~b |> float_of_int in - let im = Owl_stats.uniform_int_rvs ~a ~b |> float_of_int in - Complex.{ re; im }) - m - n - - -(** ends here *) diff --git a/src/owl/sparse/owl_sparse_matrix_z.mli b/src/owl/sparse/owl_sparse_matrix_z.mli deleted file mode 100644 index 523000e85..000000000 --- a/src/owl/sparse/owl_sparse_matrix_z.mli +++ /dev/null @@ -1,266 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type elt = Complex.t - -type mat = (Complex.t, Bigarray.complex64_elt) Owl_sparse_matrix_generic.t - -(** {5 Create sparse matrices} *) - -val zeros : int -> int -> mat - -val ones : int -> int -> mat - -val eye : int -> mat - -val binary : int -> int -> mat - -val uniform : ?scale:float -> int -> int -> mat - -val uniform_int : ?a:int -> ?b:int -> int -> int -> mat - -val sequential : int -> int -> mat - -(** {5 Obtain the basic properties of a matrix} *) - -val shape : mat -> int * int - -val row_num : mat -> int - -val col_num : mat -> int - -val row_num_nz : mat -> int - -val col_num_nz : mat -> int - -val numel : mat -> int - -val nnz : mat -> int - -val nnz_rows : mat -> int array - -val nnz_cols : mat -> int array - -val density : mat -> float - -(** {5 Manipulate a matrix} *) - -val insert : mat -> int -> int -> elt -> unit - -val set : mat -> int -> int -> elt -> unit - -val get : mat -> int -> int -> elt - -val reset : mat -> unit - -val fill : mat -> elt -> unit - -val copy : mat -> mat - -val transpose : mat -> mat - -val diag : mat -> mat - -val row : mat -> int -> mat - -val col : mat -> int -> mat - -val rows : mat -> int array -> mat - -val cols : mat -> int array -> mat - -val prune : mat -> elt -> float -> unit - -(** {5 Iterate elements, columns, and rows} *) - -val iteri : (int -> int -> elt -> unit) -> mat -> unit - -val iter : (elt -> unit) -> mat -> unit - -val mapi : (int -> int -> elt -> elt) -> mat -> mat - -val map : (elt -> elt) -> mat -> mat - -val foldi : (int -> int -> 'a -> elt -> 'a) -> 'a -> mat -> 'a - -val fold : ('a -> elt -> 'a) -> 'a -> mat -> 'a - -val filteri : (int -> int -> elt -> bool) -> mat -> (int * int) array - -val filter : (elt -> bool) -> mat -> (int * int) array - -val iteri_rows : (int -> mat -> unit) -> mat -> unit - -val iter_rows : (mat -> unit) -> mat -> unit - -val iteri_cols : (int -> mat -> unit) -> mat -> unit - -val iter_cols : (mat -> unit) -> mat -> unit - -val mapi_rows : (int -> mat -> 'a) -> mat -> 'a array - -val map_rows : (mat -> 'a) -> mat -> 'a array - -val mapi_cols : (int -> mat -> 'a) -> mat -> 'a array - -val map_cols : (mat -> 'a) -> mat -> 'a array - -val fold_rows : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val fold_cols : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val iteri_nz : (int -> int -> elt -> unit) -> mat -> unit - -val iter_nz : (elt -> unit) -> mat -> unit - -val mapi_nz : (int -> int -> elt -> elt) -> mat -> mat - -val map_nz : (elt -> elt) -> mat -> mat - -val foldi_nz : (int -> int -> 'a -> elt -> 'a) -> 'a -> mat -> 'a - -val fold_nz : ('a -> elt -> 'a) -> 'a -> mat -> 'a - -val filteri_nz : (int -> int -> elt -> bool) -> mat -> (int * int) array - -val filter_nz : (elt -> bool) -> mat -> (int * int) array - -val iteri_rows_nz : (int -> mat -> unit) -> mat -> unit - -val iter_rows_nz : (mat -> unit) -> mat -> unit - -val iteri_cols_nz : (int -> mat -> unit) -> mat -> unit - -val iter_cols_nz : (mat -> unit) -> mat -> unit - -val mapi_rows_nz : (int -> mat -> 'a) -> mat -> 'a array - -val map_rows_nz : (mat -> 'a) -> mat -> 'a array - -val mapi_cols_nz : (int -> mat -> 'a) -> mat -> 'a array - -val map_cols_nz : (mat -> 'a) -> mat -> 'a array - -val fold_rows_nz : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -val fold_cols_nz : ('a -> mat -> 'a) -> 'a -> mat -> 'a - -(** {5 Examine elements and compare two matrices} *) - -val exists : (elt -> bool) -> mat -> bool - -val not_exists : (elt -> bool) -> mat -> bool - -val for_all : (elt -> bool) -> mat -> bool - -val exists_nz : (elt -> bool) -> mat -> bool - -val not_exists_nz : (elt -> bool) -> mat -> bool - -val for_all_nz : (elt -> bool) -> mat -> bool - -val is_zero : mat -> bool - -val is_positive : mat -> bool - -val is_negative : mat -> bool - -val is_nonnegative : mat -> bool - -val equal : mat -> mat -> bool - -val not_equal : mat -> mat -> bool - -val greater : mat -> mat -> bool - -val less : mat -> mat -> bool - -val greater_equal : mat -> mat -> bool - -val less_equal : mat -> mat -> bool - -(** {5 Randomisation functions} *) - -val permutation_matrix : int -> mat - -val draw_rows : ?replacement:bool -> mat -> int -> mat * int array - -val draw_cols : ?replacement:bool -> mat -> int -> mat * int array - -val shuffle_rows : mat -> mat - -val shuffle_cols : mat -> mat - -val shuffle : mat -> mat - -(** {5 Input/Output and helper functions} *) - -val to_array : mat -> (int array * elt) array - -val of_array : int -> int -> (int array * elt) array -> mat - -val to_dense : mat -> Owl_dense_matrix_z.mat - -val of_dense : Owl_dense_matrix_z.mat -> mat - -val print : mat -> unit - -val save : mat -> string -> unit - -val load : string -> mat - -(** {5 Unary mathematical operations } *) - -val trace : mat -> elt - -val sum : mat -> elt - -val mean : mat -> elt - -val sum_rows : mat -> mat - -val sum_cols : mat -> mat - -val mean_rows : mat -> mat - -val mean_cols : mat -> mat - -val neg : mat -> mat - -(* val l1norm : mat -> elt *) - -(* val l2norm : mat -> elt *) - -(** {5 Binary mathematical operations } *) - -val add : mat -> mat -> mat - -val sub : mat -> mat -> mat - -val mul : mat -> mat -> mat - -val div : mat -> mat -> mat - -val dot : mat -> mat -> mat - -val add_scalar : mat -> elt -> mat - -val sub_scalar : mat -> elt -> mat - -val mul_scalar : mat -> elt -> mat - -val div_scalar : mat -> elt -> mat - -val scalar_add : elt -> mat -> mat - -val scalar_sub : elt -> mat -> mat - -val scalar_mul : elt -> mat -> mat - -val scalar_div : elt -> mat -> mat - -val power_scalar : mat -> elt -> mat - -val mpow : mat -> float -> mat diff --git a/src/owl/sparse/owl_sparse_ndarray.ml b/src/owl/sparse/owl_sparse_ndarray.ml deleted file mode 100644 index c9ebe24f3..000000000 --- a/src/owl/sparse/owl_sparse_ndarray.ml +++ /dev/null @@ -1,33 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** Sparse ndarray: module aliases *) - -module Operator = Owl_operator.Make_Basic (Owl_sparse_ndarray_generic) - -module Generic = struct - include Owl_sparse_ndarray_generic - include Operator -end - -module S = struct - include Owl_sparse_ndarray_s - include Operator -end - -module D = struct - include Owl_sparse_ndarray_d - include Operator -end - -module C = struct - include Owl_sparse_ndarray_c - include Operator -end - -module Z = struct - include Owl_sparse_ndarray_z - include Operator -end diff --git a/src/owl/sparse/owl_sparse_ndarray_c.ml b/src/owl/sparse/owl_sparse_ndarray_c.ml deleted file mode 100644 index 61578cbcf..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_c.ml +++ /dev/null @@ -1,24 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -open Bigarray -module M = Owl_sparse_ndarray_generic -include M - -type elt = Complex.t - -type arr = (Complex.t, Bigarray.complex32_elt) Owl_sparse_ndarray_generic.t - -(* overload functions in Owl_dense_ndarray_generic *) - -let zeros s = M.zeros Complex32 s - -let binary ?density s = M.binary ?density Complex32 s - -let uniform ?scale ?density s = M.uniform ?scale ?density Complex32 s - -let of_array s x = M.of_array Complex32 s x - -let load f = M.load Complex32 f diff --git a/src/owl/sparse/owl_sparse_ndarray_c.mli b/src/owl/sparse/owl_sparse_ndarray_c.mli deleted file mode 100644 index 45a3ca75c..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_c.mli +++ /dev/null @@ -1,172 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type elt = Complex.t - -type arr = (Complex.t, Bigarray.complex32_elt) Owl_sparse_ndarray_generic.t - -(** {5 Create sparse ndarray} *) - -val zeros : int array -> arr - -val binary : ?density:float -> int array -> arr - -val uniform : ?scale:float -> ?density:float -> int array -> arr - -(** {5 Obtain basic properties} *) - -val shape : arr -> int array - -val num_dims : arr -> int - -val nth_dim : arr -> int -> int - -val numel : arr -> int - -val nnz : arr -> int - -val density : arr -> float - -val same_shape : arr -> arr -> bool - -val kind : arr -> (elt, Bigarray.complex32_elt) Bigarray.kind - -(** {5 Manipulate a N-dimensional array} *) - -val get : arr -> int array -> elt - -val set : arr -> int array -> elt -> unit - -val slice : int option array -> arr -> arr - -val copy : arr -> arr - -val flatten : arr -> arr - -val reshape : arr -> int array -> arr - -val transpose : ?axis:int array -> arr -> arr - -val swap : int -> int -> arr -> arr - -(** {5 Iterate array elements} *) - -val iteri : ?axis:int option array -> (int array -> elt -> unit) -> arr -> unit - -val iter : ?axis:int option array -> (elt -> unit) -> arr -> unit - -val mapi : ?axis:int option array -> (int array -> elt -> elt) -> arr -> arr - -val map : ?axis:int option array -> (elt -> elt) -> arr -> arr - -val filteri - : ?axis:int option array - -> (int array -> elt -> bool) - -> arr - -> int array array - -val filter : ?axis:int option array -> (elt -> bool) -> arr -> int array array - -val foldi : ?axis:int option array -> (int array -> 'c -> elt -> 'c) -> 'c -> arr -> 'c - -val fold : ?axis:int option array -> ('c -> elt -> 'c) -> 'c -> arr -> 'c - -val iteri_nz : ?axis:int option array -> (int array -> elt -> unit) -> arr -> unit - -val iter_nz : ?axis:int option array -> (elt -> unit) -> arr -> unit - -val mapi_nz : ?axis:int option array -> (int array -> elt -> elt) -> arr -> arr - -val map_nz : ?axis:int option array -> (elt -> elt) -> arr -> arr - -val filteri_nz - : ?axis:int option array - -> (int array -> elt -> bool) - -> arr - -> int array array - -val filter_nz : ?axis:int option array -> (elt -> bool) -> arr -> int array array - -val foldi_nz : ?axis:int option array -> (int array -> 'c -> elt -> 'c) -> 'c -> arr -> 'c - -val fold_nz : ?axis:int option array -> ('c -> elt -> 'c) -> 'c -> arr -> 'c - -(** {5 Examine array elements or compare two arrays } *) - -val exists : (elt -> bool) -> arr -> bool - -val not_exists : (elt -> bool) -> arr -> bool - -val for_all : (elt -> bool) -> arr -> bool - -val is_zero : arr -> bool - -val is_positive : arr -> bool - -val is_negative : arr -> bool - -val is_nonpositive : arr -> bool - -val is_nonnegative : arr -> bool - -val equal : arr -> arr -> bool - -val not_equal : arr -> arr -> bool - -val greater : arr -> arr -> bool - -val less : arr -> arr -> bool - -val greater_equal : arr -> arr -> bool - -val less_equal : arr -> arr -> bool - -(** {5 Input/Output and helper functions} *) - -val to_array : arr -> (int array * elt) array - -val of_array : int array -> (int array * elt) array -> arr - -val print : arr -> unit - -val save : arr -> string -> unit - -val load : string -> arr - -(** {5 Unary mathematical operations } *) - -val neg : arr -> arr - -val sum : arr -> elt - -val mean : arr -> elt - -(** {5 Binary mathematical operations } *) - -val add : arr -> arr -> arr - -val sub : arr -> arr -> arr - -val mul : arr -> arr -> arr - -val div : arr -> arr -> arr - -val add_scalar : arr -> elt -> arr - -val sub_scalar : arr -> elt -> arr - -val mul_scalar : arr -> elt -> arr - -val div_scalar : arr -> elt -> arr - -val scalar_add : elt -> arr -> arr - -val scalar_sub : elt -> arr -> arr - -val scalar_mul : elt -> arr -> arr - -val scalar_div : elt -> arr -> arr - -(* ends here *) diff --git a/src/owl/sparse/owl_sparse_ndarray_d.ml b/src/owl/sparse/owl_sparse_ndarray_d.ml deleted file mode 100644 index d8af98826..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_d.ml +++ /dev/null @@ -1,24 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -open Bigarray -module M = Owl_sparse_ndarray_generic -include M - -type elt = float - -type arr = (float, float64_elt) Owl_sparse_ndarray_generic.t - -(* overload functions in Owl_dense_ndarray_generic *) - -let zeros s = M.zeros Float64 s - -let binary ?density s = M.binary ?density Float64 s - -let uniform ?scale ?density s = M.uniform ?scale ?density Float64 s - -let of_array s x = M.of_array Float64 s x - -let load f = M.load Float64 f diff --git a/src/owl/sparse/owl_sparse_ndarray_d.mli b/src/owl/sparse/owl_sparse_ndarray_d.mli deleted file mode 100644 index aba2c1dc6..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_d.mli +++ /dev/null @@ -1,180 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type elt = float - -type arr = (float, Bigarray.float64_elt) Owl_sparse_ndarray_generic.t - -(** {5 Create sparse ndarray} *) - -val zeros : int array -> arr - -val binary : ?density:float -> int array -> arr - -val uniform : ?scale:float -> ?density:float -> int array -> arr - -(** {5 Obtain basic properties} *) - -val shape : arr -> int array - -val num_dims : arr -> int - -val nth_dim : arr -> int -> int - -val numel : arr -> int - -val nnz : arr -> int - -val density : arr -> float - -val same_shape : arr -> arr -> bool - -val kind : arr -> (elt, Bigarray.float64_elt) Bigarray.kind - -(** {5 Manipulate a N-dimensional array} *) - -val get : arr -> int array -> elt - -val set : arr -> int array -> elt -> unit - -val slice : int option array -> arr -> arr - -val copy : arr -> arr - -val flatten : arr -> arr - -val reshape : arr -> int array -> arr - -val transpose : ?axis:int array -> arr -> arr - -val swap : int -> int -> arr -> arr - -(** {5 Iterate array elements} *) - -val iteri : ?axis:int option array -> (int array -> elt -> unit) -> arr -> unit - -val iter : ?axis:int option array -> (elt -> unit) -> arr -> unit - -val mapi : ?axis:int option array -> (int array -> elt -> elt) -> arr -> arr - -val map : ?axis:int option array -> (elt -> elt) -> arr -> arr - -val filteri - : ?axis:int option array - -> (int array -> elt -> bool) - -> arr - -> int array array - -val filter : ?axis:int option array -> (elt -> bool) -> arr -> int array array - -val foldi : ?axis:int option array -> (int array -> 'c -> elt -> 'c) -> 'c -> arr -> 'c - -val fold : ?axis:int option array -> ('c -> elt -> 'c) -> 'c -> arr -> 'c - -val iteri_nz : ?axis:int option array -> (int array -> elt -> unit) -> arr -> unit - -val iter_nz : ?axis:int option array -> (elt -> unit) -> arr -> unit - -val mapi_nz : ?axis:int option array -> (int array -> elt -> elt) -> arr -> arr - -val map_nz : ?axis:int option array -> (elt -> elt) -> arr -> arr - -val filteri_nz - : ?axis:int option array - -> (int array -> elt -> bool) - -> arr - -> int array array - -val filter_nz : ?axis:int option array -> (elt -> bool) -> arr -> int array array - -val foldi_nz : ?axis:int option array -> (int array -> 'c -> elt -> 'c) -> 'c -> arr -> 'c - -val fold_nz : ?axis:int option array -> ('c -> elt -> 'c) -> 'c -> arr -> 'c - -(** {5 Examine array elements or compare two arrays } *) - -val exists : (elt -> bool) -> arr -> bool - -val not_exists : (elt -> bool) -> arr -> bool - -val for_all : (elt -> bool) -> arr -> bool - -val is_zero : arr -> bool - -val is_positive : arr -> bool - -val is_negative : arr -> bool - -val is_nonpositive : arr -> bool - -val is_nonnegative : arr -> bool - -val equal : arr -> arr -> bool - -val not_equal : arr -> arr -> bool - -val greater : arr -> arr -> bool - -val less : arr -> arr -> bool - -val greater_equal : arr -> arr -> bool - -val less_equal : arr -> arr -> bool - -(** {5 Input/Output and helper functions} *) - -val to_array : arr -> (int array * elt) array - -val of_array : int array -> (int array * elt) array -> arr - -val print : arr -> unit - -val save : arr -> string -> unit - -val load : string -> arr - -(** {5 Unary mathematical operations } *) - -val min : arr -> elt - -val max : arr -> elt - -val minmax : arr -> elt * elt - -val abs : arr -> arr - -val neg : arr -> arr - -val sum : arr -> elt - -val mean : arr -> elt - -(** {5 Binary mathematical operations } *) - -val add : arr -> arr -> arr - -val sub : arr -> arr -> arr - -val mul : arr -> arr -> arr - -val div : arr -> arr -> arr - -val add_scalar : arr -> elt -> arr - -val sub_scalar : arr -> elt -> arr - -val mul_scalar : arr -> elt -> arr - -val div_scalar : arr -> elt -> arr - -val scalar_add : elt -> arr -> arr - -val scalar_sub : elt -> arr -> arr - -val scalar_mul : elt -> arr -> arr - -val scalar_div : elt -> arr -> arr - -(* ends here *) diff --git a/src/owl/sparse/owl_sparse_ndarray_generic.ml b/src/owl/sparse/owl_sparse_ndarray_generic.ml deleted file mode 100644 index 82639b70d..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_generic.ml +++ /dev/null @@ -1,656 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -open Bigarray -open Owl_ndarray -open Owl_base_dense_common - -type ('a, 'b) kind = ('a, 'b) Bigarray.kind - -type ('a, 'b) t = - { mutable s : int array - ; mutable h : (int array, int) Hashtbl.t - ; mutable d : ('a, 'b, c_layout) Array1.t - } - -let nnz x = - let _stats = Hashtbl.stats x.h in - Hashtbl.(_stats.num_bindings) - - -let _make_elt_array k n = - let x = Array1.create k c_layout n in - Array1.fill x (Owl_const.zero k); - x - - -let _allocate_more_space x = - let c = nnz x in - if c < Array1.dim x.d - then () - else ( - Owl_log.debug "allocate space %i" c; - x.d <- Owl_utils.array1_extend x.d c) - - -let _remove_ith_item x i = - Owl_log.debug "_remove_ith_item"; - for j = i to nnz x - 2 do - x.d.{j} <- x.d.{j + 1} - done; - Hashtbl.filter_map_inplace - (fun _k v -> if v = i then None else if v > i then Some (v - 1) else Some v) - x.h - - -(* check whether index x is in slice s *) -let _in_slice s x = - let r = ref true in - (try - Array.iteri - (fun i v -> - match v with - | Some v -> - if v <> x.(i) - then ( - r := false; - failwith "not in the slice") - | None -> ()) - s - with - | _exn -> ()); - !r - - -let zeros k s = - let n = Array.fold_right (fun c a -> c * a) s 1 in - let c = max (n / 1000) 1024 in - { s = Array.copy s; h = Hashtbl.create c; d = _make_elt_array k c } - - -let shape x = Array.copy x.s - -let num_dims x = Array.length x.s - -let nth_dim x i = x.s.(i) - -let numel x = Array.fold_right (fun c a -> c * a) x.s 1 - -let density x = - let a = float_of_int (nnz x) in - let b = float_of_int (numel x) in - a /. b - - -let kind x = Array1.kind x.d - -let same_shape x y = - if num_dims x <> num_dims y - then false - else ( - let s0 = shape x in - let s1 = shape y in - let b = ref true in - Array.iteri (fun i _d -> if s0.(i) <> s1.(i) then b := false) s0; - !b) - - -let _check_same_shape x y = - if same_shape x y = false then failwith "Owl_sparse_ndarray: _check_same_shape fails." - - -let copy x = { s = Array.copy x.s; h = Hashtbl.copy x.h; d = Owl_utils.array1_copy x.d } - -let get x i = - try - let j = Hashtbl.find x.h i in - Array1.unsafe_get x.d j - with - | _exn -> Owl_const.zero (kind x) - - -let set x i a = - _allocate_more_space x; - let _a0 = Owl_const.zero (kind x) in - if a = _a0 - then ( - try - let j = Hashtbl.find x.h i in - Array1.unsafe_set x.d j _a0; - _remove_ith_item x j - with - | _exn -> ()) - else ( - try - let j = Hashtbl.find x.h i in - Array1.unsafe_set x.d j a - with - | _exn -> - let j = nnz x in - Hashtbl.add x.h (Array.copy i) j; - Array1.unsafe_set x.d j a) - - -let flatten x = - let s = Owl_utils.calc_stride (shape x) in - let y = copy x in - Hashtbl.iter - (fun i j -> - let i' = Owl_utils.index_nd_1d i s in - Hashtbl.remove y.h i; - Hashtbl.add y.h [| i' |] j) - x.h; - y.s <- [| numel x |]; - y - - -let reshape x s = - let y = copy x in - let s0 = Owl_utils.calc_stride (shape x) in - let s1 = Owl_utils.calc_stride s in - let i1 = Array.copy s in - Hashtbl.iter - (fun i j -> - let k = Owl_utils.index_nd_1d i s0 in - Owl_utils.index_1d_nd k i1 s1; - Hashtbl.remove y.h i; - Hashtbl.add y.h (Array.copy i1) j) - x.h; - y.s <- s; - y - - -let rec __iteri_fix_axis d j i l h f x = - if j = d - 1 - then - for k = l.(j) to h.(j) do - i.(j) <- k; - f i (get x i) - done - else - for k = l.(j) to h.(j) do - i.(j) <- k; - __iteri_fix_axis d (j + 1) i l h f x - done - - -let _iteri_fix_axis axis f x = - let d = num_dims x in - let i = Array.make d 0 in - let l = Array.make d 0 in - let h = shape x in - Array.iteri - (fun j a -> - match a with - | Some b -> - l.(j) <- b; - h.(j) <- b - | None -> h.(j) <- h.(j) - 1) - axis; - __iteri_fix_axis d 0 i l h f x - - -let iteri ?axis f x = - match axis with - | Some a -> _iteri_fix_axis a f x - | None -> _iteri_fix_axis (Array.make (num_dims x) None) f x - - -let iter ?axis f x = iteri ?axis (fun _ y -> f y) x - -let mapi ?axis f x = - let y = copy x in - iteri ?axis (fun i z -> set y i (f i z)) y; - y - - -let map ?axis f x = - let y = copy x in - iteri ?axis (fun i z -> set y i (f z)) y; - y - - -let _iteri_all_axis_nz f x = Hashtbl.iter (fun i j -> f i x.d.{j}) x.h - -let _iteri_fix_axis_nz axis f x = - Hashtbl.iter (fun i j -> if _in_slice axis i = true then f i x.d.{j}) x.h - - -let _iter_all_axis_nz f x = - for i = 0 to nnz x - 1 do - f (Array1.unsafe_get x.d i) - done - - -let iteri_nz ?axis f x = - match axis with - | Some a -> _iteri_fix_axis_nz a f x - | None -> _iteri_all_axis_nz f x - - -let iter_nz ?axis f x = - match axis with - | Some a -> _iteri_fix_axis_nz a (fun _ y -> f y) x - | None -> _iter_all_axis_nz f x - - -let mapi_nz ?axis f x = - let y = copy x in - (match axis with - | Some a -> - Hashtbl.iter (fun i j -> if _in_slice a i = true then y.d.{j} <- f i x.d.{j}) y.h - | None -> Hashtbl.iter (fun i j -> y.d.{j} <- f i x.d.{j}) y.h); - y - - -let map_nz ?axis f x = - match axis with - | Some a -> mapi_nz ~axis:a (fun _ z -> f z) x - | None -> - let y = copy x in - for i = 0 to nnz y do - let a = f (Array1.unsafe_get y.d i) in - Array1.unsafe_set y.d i a - done; - y - - -let _check_transpose_axis axis d = - let info = "check_transpose_axiss fails" in - if Array.length axis <> d then failwith info; - let h = Hashtbl.create 16 in - Array.iter - (fun x -> - if x < 0 || x >= d then failwith info; - if Hashtbl.mem h x = true then failwith info; - Hashtbl.add h x 0) - axis - - -let transpose ?axis x = - let d = num_dims x in - let a = - match axis with - | Some a -> a - | None -> Array.init d (fun i -> d - i - 1) - in - (* check if axis is a correct permutation *) - _check_transpose_axis a d; - let s0 = shape x in - let s1 = Array.map (fun j -> s0.(j)) a in - let i' = Array.make d 0 in - let y = zeros (kind x) s1 in - iteri - (fun i z -> - Array.iteri (fun k j -> i'.(k) <- i.(j)) a; - set y i' z) - x; - y - - -let swap a0 a1 x = - let d = num_dims x in - let a = Array.init d (fun i -> i) in - let t = a.(a0) in - a.(a0) <- a.(a1); - a.(a1) <- t; - transpose ~axis:a x - - -let filteri ?axis f x = - let s = Owl_utils.Stack.make () in - iteri - ?axis - (fun i y -> - if f i y = true - then ( - let j = Array.copy i in - Owl_utils.Stack.push s j)) - x; - Owl_utils.Stack.to_array s - - -let filter ?axis f x = filteri ?axis (fun _ y -> f y) x - -let filteri_nz ?axis f x = - let s = Owl_utils.Stack.make () in - iteri_nz - ?axis - (fun i y -> - if f i y = true - then ( - let j = Array.copy i in - Owl_utils.Stack.push s j)) - x; - Owl_utils.Stack.to_array s - - -let filter_nz ?axis f x = filteri_nz ?axis (fun _ y -> f y) x - -let _fold_basic ?axis iter_fun f a x = - let r = ref a in - iter_fun ?axis (fun y -> r := f !r y) x; - !r - - -let fold ?axis f a x = _fold_basic ?axis iter f a x - -let fold_nz ?axis f a x = _fold_basic ?axis iter_nz f a x - -let foldi ?axis f a x = - let c = ref a in - iteri ?axis (fun i y -> c := f i !c y) x; - !c - - -let foldi_nz ?axis f a x = - let c = ref a in - iteri_nz ?axis (fun i y -> c := f i !c y) x; - !c - - -let slice axis x = - (* make the index mapping *) - let s = Owl_utils.Stack.make () in - for i = 0 to Array.length axis - 1 do - match axis.(i) with - | Some _ -> () - | None -> Owl_utils.Stack.push s i - done; - let m = Owl_utils.Stack.to_array s in - (* create a new sparse ndarray for the slice *) - let s0 = shape x in - let s1 = Array.map (fun i -> s0.(i)) m in - let y = zeros (kind x) s1 in - (* only iterate non-zero elements *) - iteri_nz - (fun i v -> - if _in_slice axis i = true - then ( - let i' = Array.map (fun j -> i.(j)) m in - set y i' v)) - x; - y - - -let _exists_basic iter_fun f x = - try - iter_fun (fun y -> if f y = true then failwith "found") x; - false - with - | _exn -> true - - -let exists f x = _exists_basic iter f x - -let not_exists f x = not (exists f x) - -let for_all f x = - let g y = not (f y) in - not_exists g x - - -let exists_nz f x = _exists_basic iter_nz f x - -let not_exists_nz f x = not (exists_nz f x) - -let for_all_nz f x = - let g y = not (f y) in - not_exists_nz g x - - -let is_zero x = nnz x = 0 - -let is_positive x = - let _a0 = Owl_const.zero (kind x) in - if nnz x < numel x then false else for_all (( < ) _a0) x - - -let is_negative x = - let _a0 = Owl_const.zero (kind x) in - if nnz x < numel x then false else for_all (( > ) _a0) x - - -let is_nonpositive x = - let _a0 = Owl_const.zero (kind x) in - for_all_nz (( >= ) _a0) x - - -let is_nonnegative x = - let _a0 = Owl_const.zero (kind x) in - for_all_nz (( <= ) _a0) x - - -let add_scalar x a = - let _op = _add_elt (kind x) in - map_nz (fun z -> _op z a) x - - -let sub_scalar x a = add_scalar x (_neg_elt (kind x) a) - -let mul_scalar x a = - let _op = _mul_elt (kind x) in - map_nz (fun z -> _op z a) x - - -let div_scalar x a = mul_scalar x ((_inv_elt (kind x)) a) - -let scalar_add a x = - let _op = _add_elt (kind x) in - map_nz (fun z -> _op a z) x - - -let scalar_sub a x = - let _op = _sub_elt (kind x) in - map_nz (fun z -> _op a z) x - - -let scalar_mul a x = - let _op = _mul_elt (kind x) in - map_nz (fun z -> _op a z) x - - -let scalar_div a x = - let _op = _div_elt (kind x) in - map_nz (fun z -> _op a z) x - - -let add x1 x2 = - let k = kind x1 in - let _a0 = Owl_const.zero k in - let __add_elt = _add_elt k in - let y = zeros k (shape x1) in - let _ = - iteri_nz - (fun i a -> - let b = get x2 i in - if b = _a0 then set y i a) - x1 - in - let _ = - iteri_nz - (fun i a -> - let b = get x1 i in - set y i (__add_elt a b)) - x2 - in - y - - -let neg x = map_nz (_neg_elt (kind x)) x - -let sub x1 x2 = add x1 (neg x2) - -let mul x1 x2 = - let k = kind x1 in - let _a0 = Owl_const.zero k in - let __mul_elt = _mul_elt k in - let y = zeros (kind x1) (shape x1) in - let _ = - iteri_nz - (fun i a -> - let b = get x2 i in - if b <> _a0 then set y i (__mul_elt a b)) - x1 - in - y - - -let div x1 x2 = - let k = kind x1 in - let _a0 = Owl_const.zero k in - let __div_elt = _div_elt k in - let __inv_elt = _inv_elt k in - let y = zeros (kind x1) (shape x1) in - let _ = - iteri_nz - (fun i a -> - let b = get x2 i in - if b <> _a0 then set y i (__div_elt a (__inv_elt b))) - x1 - in - y - - -let abs x = - let _op = _abs_elt (kind x) in - map_nz _op x - - -let sum x = - let k = kind x in - fold_nz (_add_elt k) (Owl_const.zero k) x - - -let mean x = (_mean_elt (kind x)) (sum x) (numel x) - -let equal x1 x2 = - _check_same_shape x1 x2; - if nnz x1 <> nnz x2 then false else sub x1 x2 |> is_zero - - -let not_equal x1 x2 = not (equal x1 x2) - -let greater x1 x2 = - _check_same_shape x1 x2; - is_positive (sub x1 x2) - - -let less x1 x2 = greater x2 x1 - -let greater_equal x1 x2 = - _check_same_shape x1 x2; - is_nonnegative (sub x1 x2) - - -let less_equal x1 x2 = greater_equal x2 x1 - -let minmax x = - let k = kind x in - let _a0 = Owl_const.zero k in - let xmin = ref (Owl_const.pos_inf k) in - let xmax = ref (Owl_const.neg_inf k) in - iter_nz - (fun y -> - if y < !xmin then xmin := y; - if y > !xmax then xmax := y) - x; - match nnz x < numel x with - | true -> min !xmin _a0, max !xmax _a0 - | false -> !xmin, !xmax - - -let min x = fst (minmax x) - -let max x = snd (minmax x) - -(* input/output functions *) - -let print_index i = - Printf.printf "[ "; - Array.iter (fun x -> Printf.printf "%i " x) i; - Printf.printf "] " - - -let print_element k v = - let s = (Owl_utils.elt_to_str k) v in - Printf.printf "%s" s - [@@warning "-32"] - - -let print x = - let _op = Owl_utils.elt_to_str (kind x) in - iteri - (fun i y -> - print_index i; - Printf.printf "%s\n" (_op y)) - x - - -let pp_spnda x = - let _op = Owl_utils.elt_to_str (kind x) in - let k = shape x in - let s = Owl_utils.calc_stride k in - let _pp i j = - for i' = i to j do - Owl_utils.index_1d_nd i' k s; - print_index k; - Printf.printf "%s\n" (_op (get x k)) - done - in - let n = numel x in - if n <= 40 - then _pp 0 (n - 1) - else ( - _pp 0 19; - print_endline "......"; - _pp (n - 20) (n - 1)) - - -let save x f = Owl_io.marshal_to_file x f - -let load _k f = Owl_io.marshal_from_file f - -let _random_basic a k f d = - let x = zeros k d in - let n = numel x in - let c = int_of_float (float_of_int n *. a) in - let i = Array.copy d in - let s = Owl_utils.calc_stride d in - for _k = 0 to c - 1 do - let j = Owl_stats.uniform_int_rvs ~a:0 ~b:(n - 1) in - Owl_utils.index_1d_nd j i s; - set x i (f ()) - done; - x - - -let binary ?(density = 0.1) k s = - let _a1 = Owl_const.one k in - _random_basic density k (fun () -> _a1) s - - -let uniform ?(scale = 1.) ?(density = 0.1) k s = - let _op = _owl_uniform_fun k in - _random_basic density k (fun () -> _op scale) s - - -let to_array x = - let y = Array.make (nnz x) ([||], Owl_const.zero (kind x)) in - let j = ref 0 in - iteri_nz - (fun i v -> - y.(!j) <- Array.copy i, v; - j := !j + 1) - x; - y - - -let of_array k s x = - let y = zeros k s in - Array.iter (fun (i, v) -> set y i v) x; - y - -(* ends here *) diff --git a/src/owl/sparse/owl_sparse_ndarray_generic.mli b/src/owl/sparse/owl_sparse_ndarray_generic.mli deleted file mode 100644 index 2f7104f42..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_generic.mli +++ /dev/null @@ -1,276 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -(** Sparse N-dimensional array module *) - -(** {5 Type definition} *) - -type ('a, 'b) kind = ('a, 'b) Bigarray.kind -(** Type of [kind]. *) - -type ('a, 'b) t -(** Abstract type of sparse ndarray. *) - -(** {5 Create sparse ndarray} *) - -val zeros : ('a, 'b) kind -> int array -> ('a, 'b) t -(** TODO *) - -val binary : ?density:float -> ('a, 'b) kind -> int array -> ('a, 'b) t -(** TODO *) - -val uniform : ?scale:float -> ?density:float -> ('a, 'b) kind -> int array -> ('a, 'b) t -(** TODO *) - -(** {5 Obtain basic properties} *) - -val shape : ('a, 'b) t -> int array -(** TODO *) - -val num_dims : ('a, 'b) t -> int -(** TODO *) - -val nth_dim : ('a, 'b) t -> int -> int -(** TODO *) - -val numel : ('a, 'b) t -> int -(** TODO *) - -val nnz : ('a, 'b) t -> int -(** TODO *) - -val density : ('a, 'b) t -> float -(** TODO *) - -val same_shape : ('a, 'b) t -> ('a, 'b) t -> bool -(** TODO *) - -val kind : ('a, 'b) t -> ('a, 'b) kind -(** TODO *) - -(** {5 Manipulate a N-dimensional array} *) - -val get : ('a, 'b) t -> int array -> 'a -(** TODO *) - -val set : ('a, 'b) t -> int array -> 'a -> unit -(** TODO *) - -val slice : int option array -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val copy : ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val flatten : ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val reshape : ('a, 'b) t -> int array -> ('a, 'b) t -(** TODO *) - -val transpose : ?axis:int array -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val swap : int -> int -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -(** {5 Iterate array elements} *) - -val iteri : ?axis:int option array -> (int array -> 'a -> unit) -> ('a, 'b) t -> unit -(** TODO *) - -val iter : ?axis:int option array -> ('a -> unit) -> ('a, 'b) t -> unit -(** TODO *) - -val mapi : ?axis:int option array -> (int array -> 'a -> 'a) -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val map : ?axis:int option array -> ('a -> 'a) -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val filteri - : ?axis:int option array - -> (int array -> 'a -> bool) - -> ('a, 'b) t - -> int array array -(** TODO *) - -val filter : ?axis:int option array -> ('a -> bool) -> ('a, 'b) t -> int array array -(** TODO *) - -val foldi - : ?axis:int option array - -> (int array -> 'c -> 'a -> 'c) - -> 'c - -> ('a, 'b) t - -> 'c -(** TODO *) - -val fold : ?axis:int option array -> ('c -> 'a -> 'c) -> 'c -> ('a, 'b) t -> 'c -(** TODO *) - -val iteri_nz : ?axis:int option array -> (int array -> 'a -> unit) -> ('a, 'b) t -> unit -(** TODO *) - -val iter_nz : ?axis:int option array -> ('a -> unit) -> ('a, 'b) t -> unit -(** TODO *) - -val mapi_nz - : ?axis:int option array - -> (int array -> 'a -> 'a) - -> ('a, 'b) t - -> ('a, 'b) t -(** TODO *) - -val map_nz : ?axis:int option array -> ('a -> 'a) -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val filteri_nz - : ?axis:int option array - -> (int array -> 'a -> bool) - -> ('a, 'b) t - -> int array array -(** TODO *) - -val filter_nz : ?axis:int option array -> ('a -> bool) -> ('a, 'b) t -> int array array -(** TODO *) - -val foldi_nz - : ?axis:int option array - -> (int array -> 'c -> 'a -> 'c) - -> 'c - -> ('a, 'b) t - -> 'c -(** TODO *) - -val fold_nz : ?axis:int option array -> ('c -> 'a -> 'c) -> 'c -> ('a, 'b) t -> 'c -(** TODO *) - -(** {5 Examine array elements or compare two arrays } *) - -val exists : ('a -> bool) -> ('a, 'b) t -> bool -(** TODO *) - -val not_exists : ('a -> bool) -> ('a, 'b) t -> bool -(** TODO *) - -val for_all : ('a -> bool) -> ('a, 'b) t -> bool -(** TODO *) - -val is_zero : ('a, 'b) t -> bool -(** TODO *) - -val is_positive : ('a, 'b) t -> bool -(** TODO *) - -val is_negative : ('a, 'b) t -> bool -(** TODO *) - -val is_nonpositive : ('a, 'b) t -> bool -(** TODO *) - -val is_nonnegative : ('a, 'b) t -> bool -(** TODO *) - -val equal : ('a, 'b) t -> ('a, 'b) t -> bool -(** TODO *) - -val not_equal : ('a, 'b) t -> ('a, 'b) t -> bool -(** TODO *) - -val greater : ('a, 'b) t -> ('a, 'b) t -> bool -(** TODO *) - -val less : ('a, 'b) t -> ('a, 'b) t -> bool -(** TODO *) - -val greater_equal : ('a, 'b) t -> ('a, 'b) t -> bool -(** TODO *) - -val less_equal : ('a, 'b) t -> ('a, 'b) t -> bool -(** TODO *) - -(** {5 Input/Output and helper functions} *) - -val to_array : ('a, 'b) t -> (int array * 'a) array -(** TODO *) - -val of_array : ('a, 'b) kind -> int array -> (int array * 'a) array -> ('a, 'b) t -(** TODO *) - -val print : ('a, 'b) t -> unit -(** TODO *) - -val pp_spnda : ('a, 'b) t -> unit [@@ocaml.toplevel_printer] -(** TODO *) - -val save : ('a, 'b) t -> string -> unit -(** TODO *) - -val load : ('a, 'b) kind -> string -> ('a, 'b) t -(** TODO *) - -(** {5 Unary mathematical operations } *) - -val min : ('a, 'b) t -> 'a -(** TODO *) - -val max : ('a, 'b) t -> 'a -(** TODO *) - -val minmax : ('a, 'b) t -> 'a * 'a -(** TODO *) - -val abs : ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val neg : ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val sum : ('a, 'b) t -> 'a -(** TODO *) - -val mean : ('a, 'b) t -> 'a -(** TODO *) - -(** {5 Binary mathematical operations } *) - -val add : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val sub : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val mul : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val div : ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val add_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t -(** TODO *) - -val sub_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t -(** TODO *) - -val mul_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t -(** TODO *) - -val div_scalar : ('a, 'b) t -> 'a -> ('a, 'b) t -(** TODO *) - -val scalar_add : 'a -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val scalar_sub : 'a -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val scalar_mul : 'a -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -val scalar_div : 'a -> ('a, 'b) t -> ('a, 'b) t -(** TODO *) - -(* ends here *) diff --git a/src/owl/sparse/owl_sparse_ndarray_s.ml b/src/owl/sparse/owl_sparse_ndarray_s.ml deleted file mode 100644 index ac5962ca2..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_s.ml +++ /dev/null @@ -1,24 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -open Bigarray -module M = Owl_sparse_ndarray_generic -include M - -type elt = float - -type arr = (float, float32_elt) Owl_sparse_ndarray_generic.t - -(* overload functions in Owl_dense_ndarray_generic *) - -let zeros s = M.zeros Float32 s - -let binary ?density s = M.binary ?density Float32 s - -let uniform ?scale ?density s = M.uniform ?scale ?density Float32 s - -let of_array s x = M.of_array Float32 s x - -let load f = M.load Float32 f diff --git a/src/owl/sparse/owl_sparse_ndarray_s.mli b/src/owl/sparse/owl_sparse_ndarray_s.mli deleted file mode 100644 index 59792acdb..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_s.mli +++ /dev/null @@ -1,180 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type elt = float - -type arr = (float, Bigarray.float32_elt) Owl_sparse_ndarray_generic.t - -(** {5 Create sparse ndarray} *) - -val zeros : int array -> arr - -val binary : ?density:float -> int array -> arr - -val uniform : ?scale:float -> ?density:float -> int array -> arr - -(** {5 Obtain basic properties} *) - -val shape : arr -> int array - -val num_dims : arr -> int - -val nth_dim : arr -> int -> int - -val numel : arr -> int - -val nnz : arr -> int - -val density : arr -> float - -val same_shape : arr -> arr -> bool - -val kind : arr -> (elt, Bigarray.float32_elt) Bigarray.kind - -(** {5 Manipulate a N-dimensional array} *) - -val get : arr -> int array -> elt - -val set : arr -> int array -> elt -> unit - -val slice : int option array -> arr -> arr - -val copy : arr -> arr - -val flatten : arr -> arr - -val reshape : arr -> int array -> arr - -val transpose : ?axis:int array -> arr -> arr - -val swap : int -> int -> arr -> arr - -(** {5 Iterate array elements} *) - -val iteri : ?axis:int option array -> (int array -> elt -> unit) -> arr -> unit - -val iter : ?axis:int option array -> (elt -> unit) -> arr -> unit - -val mapi : ?axis:int option array -> (int array -> elt -> elt) -> arr -> arr - -val map : ?axis:int option array -> (elt -> elt) -> arr -> arr - -val filteri - : ?axis:int option array - -> (int array -> elt -> bool) - -> arr - -> int array array - -val filter : ?axis:int option array -> (elt -> bool) -> arr -> int array array - -val foldi : ?axis:int option array -> (int array -> 'c -> elt -> 'c) -> 'c -> arr -> 'c - -val fold : ?axis:int option array -> ('c -> elt -> 'c) -> 'c -> arr -> 'c - -val iteri_nz : ?axis:int option array -> (int array -> elt -> unit) -> arr -> unit - -val iter_nz : ?axis:int option array -> (elt -> unit) -> arr -> unit - -val mapi_nz : ?axis:int option array -> (int array -> elt -> elt) -> arr -> arr - -val map_nz : ?axis:int option array -> (elt -> elt) -> arr -> arr - -val filteri_nz - : ?axis:int option array - -> (int array -> elt -> bool) - -> arr - -> int array array - -val filter_nz : ?axis:int option array -> (elt -> bool) -> arr -> int array array - -val foldi_nz : ?axis:int option array -> (int array -> 'c -> elt -> 'c) -> 'c -> arr -> 'c - -val fold_nz : ?axis:int option array -> ('c -> elt -> 'c) -> 'c -> arr -> 'c - -(** {5 Examine array elements or compare two arrays } *) - -val exists : (elt -> bool) -> arr -> bool - -val not_exists : (elt -> bool) -> arr -> bool - -val for_all : (elt -> bool) -> arr -> bool - -val is_zero : arr -> bool - -val is_positive : arr -> bool - -val is_negative : arr -> bool - -val is_nonpositive : arr -> bool - -val is_nonnegative : arr -> bool - -val equal : arr -> arr -> bool - -val not_equal : arr -> arr -> bool - -val greater : arr -> arr -> bool - -val less : arr -> arr -> bool - -val greater_equal : arr -> arr -> bool - -val less_equal : arr -> arr -> bool - -(** {5 Input/Output and helper functions} *) - -val to_array : arr -> (int array * elt) array - -val of_array : int array -> (int array * elt) array -> arr - -val print : arr -> unit - -val save : arr -> string -> unit - -val load : string -> arr - -(** {5 Unary mathematical operations } *) - -val min : arr -> elt - -val max : arr -> elt - -val minmax : arr -> elt * elt - -val abs : arr -> arr - -val neg : arr -> arr - -val sum : arr -> elt - -val mean : arr -> elt - -(** {5 Binary mathematical operations } *) - -val add : arr -> arr -> arr - -val sub : arr -> arr -> arr - -val mul : arr -> arr -> arr - -val div : arr -> arr -> arr - -val add_scalar : arr -> elt -> arr - -val sub_scalar : arr -> elt -> arr - -val mul_scalar : arr -> elt -> arr - -val div_scalar : arr -> elt -> arr - -val scalar_add : elt -> arr -> arr - -val scalar_sub : elt -> arr -> arr - -val scalar_mul : elt -> arr -> arr - -val scalar_div : elt -> arr -> arr - -(* ends here *) diff --git a/src/owl/sparse/owl_sparse_ndarray_z.ml b/src/owl/sparse/owl_sparse_ndarray_z.ml deleted file mode 100644 index 566427130..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_z.ml +++ /dev/null @@ -1,24 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -open Bigarray -module M = Owl_sparse_ndarray_generic -include M - -type elt = Complex.t - -type arr = (Complex.t, Bigarray.complex64_elt) Owl_sparse_ndarray_generic.t - -(* overload functions in Owl_dense_ndarray_generic *) - -let zeros s = M.zeros Complex64 s - -let binary ?density s = M.binary ?density Complex64 s - -let uniform ?scale ?density s = M.uniform ?scale ?density Complex64 s - -let of_array s x = M.of_array Complex64 s x - -let load f = M.load Complex64 f diff --git a/src/owl/sparse/owl_sparse_ndarray_z.mli b/src/owl/sparse/owl_sparse_ndarray_z.mli deleted file mode 100644 index 1a275e2d7..000000000 --- a/src/owl/sparse/owl_sparse_ndarray_z.mli +++ /dev/null @@ -1,172 +0,0 @@ -(* - * OWL - OCaml Scientific Computing - * Copyright (c) 2016-2022 Liang Wang - *) - -type elt = Complex.t - -type arr = (Complex.t, Bigarray.complex64_elt) Owl_sparse_ndarray_generic.t - -(** {5 Create sparse ndarray} *) - -val zeros : int array -> arr - -val binary : ?density:float -> int array -> arr - -val uniform : ?scale:float -> ?density:float -> int array -> arr - -(** {5 Obtain basic properties} *) - -val shape : arr -> int array - -val num_dims : arr -> int - -val nth_dim : arr -> int -> int - -val numel : arr -> int - -val nnz : arr -> int - -val density : arr -> float - -val same_shape : arr -> arr -> bool - -val kind : arr -> (elt, Bigarray.complex64_elt) Bigarray.kind - -(** {5 Manipulate a N-dimensional array} *) - -val get : arr -> int array -> elt - -val set : arr -> int array -> elt -> unit - -val slice : int option array -> arr -> arr - -val copy : arr -> arr - -val flatten : arr -> arr - -val reshape : arr -> int array -> arr - -val transpose : ?axis:int array -> arr -> arr - -val swap : int -> int -> arr -> arr - -(** {5 Iterate array elements} *) - -val iteri : ?axis:int option array -> (int array -> elt -> unit) -> arr -> unit - -val iter : ?axis:int option array -> (elt -> unit) -> arr -> unit - -val mapi : ?axis:int option array -> (int array -> elt -> elt) -> arr -> arr - -val map : ?axis:int option array -> (elt -> elt) -> arr -> arr - -val filteri - : ?axis:int option array - -> (int array -> elt -> bool) - -> arr - -> int array array - -val filter : ?axis:int option array -> (elt -> bool) -> arr -> int array array - -val foldi : ?axis:int option array -> (int array -> 'c -> elt -> 'c) -> 'c -> arr -> 'c - -val fold : ?axis:int option array -> ('c -> elt -> 'c) -> 'c -> arr -> 'c - -val iteri_nz : ?axis:int option array -> (int array -> elt -> unit) -> arr -> unit - -val iter_nz : ?axis:int option array -> (elt -> unit) -> arr -> unit - -val mapi_nz : ?axis:int option array -> (int array -> elt -> elt) -> arr -> arr - -val map_nz : ?axis:int option array -> (elt -> elt) -> arr -> arr - -val filteri_nz - : ?axis:int option array - -> (int array -> elt -> bool) - -> arr - -> int array array - -val filter_nz : ?axis:int option array -> (elt -> bool) -> arr -> int array array - -val foldi_nz : ?axis:int option array -> (int array -> 'c -> elt -> 'c) -> 'c -> arr -> 'c - -val fold_nz : ?axis:int option array -> ('c -> elt -> 'c) -> 'c -> arr -> 'c - -(** {5 Examine array elements or compare two arrays } *) - -val exists : (elt -> bool) -> arr -> bool - -val not_exists : (elt -> bool) -> arr -> bool - -val for_all : (elt -> bool) -> arr -> bool - -val is_zero : arr -> bool - -val is_positive : arr -> bool - -val is_negative : arr -> bool - -val is_nonpositive : arr -> bool - -val is_nonnegative : arr -> bool - -val equal : arr -> arr -> bool - -val not_equal : arr -> arr -> bool - -val greater : arr -> arr -> bool - -val less : arr -> arr -> bool - -val greater_equal : arr -> arr -> bool - -val less_equal : arr -> arr -> bool - -(** {5 Input/Output and helper functions} *) - -val to_array : arr -> (int array * elt) array - -val of_array : int array -> (int array * elt) array -> arr - -val print : arr -> unit - -val save : arr -> string -> unit - -val load : string -> arr - -(** {5 Unary mathematical operations } *) - -val neg : arr -> arr - -val sum : arr -> elt - -val mean : arr -> elt - -(** {5 Binary mathematical operations } *) - -val add : arr -> arr -> arr - -val sub : arr -> arr -> arr - -val mul : arr -> arr -> arr - -val div : arr -> arr -> arr - -val add_scalar : arr -> elt -> arr - -val sub_scalar : arr -> elt -> arr - -val mul_scalar : arr -> elt -> arr - -val div_scalar : arr -> elt -> arr - -val scalar_add : elt -> arr -> arr - -val scalar_sub : elt -> arr -> arr - -val scalar_mul : elt -> arr -> arr - -val scalar_div : elt -> arr -> arr - -(* ends here *) diff --git a/test/test_runner.ml b/test/test_runner.ml index 4b6702b75..cce4db693 100644 --- a/test/test_runner.ml +++ b/test/test_runner.ml @@ -9,8 +9,6 @@ let () = ; "algodiff grad", Unit_algodiff_grad.test_set ; "dense matrix", Unit_dense_matrix.test_set ; "dense ndarray", Unit_dense_ndarray.test_set - ; "sparse matrix", Unit_sparse_matrix.test_set - ; "sparse ndarray", Unit_sparse_ndarray.test_set ; "ndarray core", Unit_ndarray_core.test_set ; "ndarray primitive", Unit_ndarray_primitive.test_set ; "ndarray operation", Unit_ndarray_operation.test_set diff --git a/test/unit_sparse_matrix.ml b/test/unit_sparse_matrix.ml deleted file mode 100644 index a33d06644..000000000 --- a/test/unit_sparse_matrix.ml +++ /dev/null @@ -1,323 +0,0 @@ -(** Unit test for Owl_sparse_matrix_generic module *) - -open Bigarray -module M = Owl_sparse_matrix_generic - -(* define the test error *) -let eps = 1e-16 - -(* make testable *) -let matrix = Alcotest.testable (fun _p _x -> ()) M.equal - -(* some test input *) -let x0 = M.zeros Float64 3 4 - -let x1 = M.ones Float64 3 4 - -let x2 = M.sequential Float64 3 4 - -(* a module with functions to test *) -module To_test = struct - let sequential () = M.sequential Float64 3 4 - - let row_num x = M.row_num x - - let col_num x = M.col_num x - - let numel x = M.numel x - - let transpose () = - let y = M.zeros Float64 4 3 in - let y = M.mapi (fun i j _ -> i + (M.row_num y * j) |> float_of_int) y in - M.equal y (M.transpose x2) - - - let fill () = - let x = M.zeros Float64 3 4 in - M.fill x 1.; - x - - - let get x = M.get x 1 2 - - let set () = - let x = M.zeros Float64 3 4 in - M.set x 2 1 5.; - M.get x 2 1 - - - let row () = - Owl_dense_matrix_generic.of_arrays Float64 [| [| 4.; 5.; 6.; 7. |] |] |> M.of_dense - - - let col () = - Owl_dense_matrix_generic.of_arrays Float64 [| [| 1. |]; [| 5. |]; [| 9. |] |] - |> M.of_dense - - - let trace x = M.trace x - - let sum x = M.sum x - - let exists x = M.exists (fun a -> a = 6.) x - - let not_exists x = M.not_exists (fun a -> a > 13.) x - - let for_all x = M.for_all (fun a -> a < 11.) x - - let equal x y = M.equal x y - - let not_equal x y = M.not_equal x y - - let less () = - let x = M.ones Float64 3 4 in - let y = M.ones Float64 3 4 in - let y = M.mul_scalar y 2. in - M.less x y - - - let greater () = - let x = M.ones Float64 3 4 in - let y = M.ones Float64 3 4 in - M.set y 0 0 0.; - M.set y 0 1 2.; - M.greater x y - - - let greater_equal x = M.greater_equal x x - - let less_equal x = M.less_equal x x - - let is_zero x = M.is_zero x - - let is_positive x = M.is_positive x - - let is_negative x = M.is_negative x - - let is_nonnegative x = M.is_nonnegative x - - let is_nonpositive x = M.is_nonpositive x - - let add x = - let y0 = M.mul_scalar x 2. in - let y1 = M.add x x in - M.equal y0 y1 - - - let mul x = - let y0 = M.mul_scalar x 2. in - let m, n = M.shape x in - let y1 = M.zeros Float64 m n in - M.fill y1 2.; - let y2 = M.mul x y1 in - M.equal y0 y2 - - - let dot () = - let x = M.sequential Float64 2 3 in - let x = M.map (( +. ) 1.) x in - let y = M.sequential Float64 3 2 in - let y = M.map (( +. ) 1.) y in - let a = M.dot x y in - let b = - Owl_dense_matrix_generic.of_arrays Float64 [| [| 22.; 28. |]; [| 49.; 64. |] |] - |> M.of_dense - in - M.equal a b - - - let add_scalar () = M.add_scalar x1 2. |> M.sum = 36. - - let mul_scalar () = M.mul_scalar x1 2. |> M.sum = 24. - - let min () = - let x = M.add_scalar x2 1. in - M.min x = 0. - - - let max () = - let x = M.add_scalar x2 1. in - M.max x = 12. - - - let map () = - let x = M.ones Float64 3 4 in - let y = M.sequential Float64 3 4 in - let z0 = M.add x y in - let z1 = M.map (fun a -> a +. 1.) y in - M.equal z0 z1 - - - let fold x = M.fold ( +. ) 0. x - - let foldi () = - let a = M.foldi (fun i _ c a -> if i <> 0 then c +. a else c) 0. x2 in - a = 60. - - - let foldi_nz () = - let a = M.foldi_nz (fun i _ c a -> if i <> 2 then c +. a else c) 0. x2 in - a = 28. - - - let filter () = M.filter (( = ) 3.) x2 = [| 0, 3 |] - - let fold_rows () = - let x = - M.fold_rows (fun c a -> M.add c a) (M.zeros Float64 1 4) x2 - |> M.to_dense - |> Owl_dense_matrix_generic.to_arrays - in - x = [| [| 12.; 15.; 18.; 21. |] |] - - - let fold_cols () = - let x = - M.fold_cols (fun c a -> M.add c a) (M.zeros Float64 3 1) x2 - |> M.to_dense - |> Owl_dense_matrix_generic.to_arrays - in - x = [| [| 6. |]; [| 22. |]; [| 38. |] |] - - - let sum_rows () = - let x = M.sum_rows x2 |> M.to_dense |> Owl_dense_matrix_generic.to_arrays in - x = [| [| 12.; 15.; 18.; 21. |] |] - - - let sum_cols () = - let x = M.sum_cols x2 |> M.to_dense |> Owl_dense_matrix_generic.to_arrays in - x = [| [| 6. |]; [| 22. |]; [| 38. |] |] - - - let of_array () = - let a = - [| [| 0; 1 |], 1.; [| 0; 2 |], 2.; [| 0; 3 |], 3.; [| 1; 0 |], 4.; [| 1; 1 |], 5. - ; [| 1; 2 |], 6.; [| 1; 3 |], 7.; [| 2; 0 |], 8.; [| 2; 1 |], 9.; [| 2; 2 |], 10. - ; [| 2; 3 |], 11. |] - in - let y = M.of_array Float64 3 4 a in - M.equal y x2 - - - let save_load () = - M.save x2 "sp_mat.tmp"; - let y = M.load Float64 "sp_mat.tmp" in - M.equal x2 y -end - -(* the tests *) - -let sequential () = Alcotest.(check matrix) "sequential" x2 (To_test.sequential ()) - -let row_num () = Alcotest.(check int) "row_num" 3 (To_test.row_num x2) - -let col_num () = Alcotest.(check int) "col_num" 4 (To_test.col_num x0) - -let numel () = Alcotest.(check int) "numel" 12 (To_test.numel x0) - -let transpose () = Alcotest.(check bool) "transpose" true (To_test.transpose ()) - -let get () = Alcotest.(check (float eps)) "get" 6. (To_test.get x2) - -let set () = Alcotest.(check (float eps)) "set" 5. (To_test.set ()) - -let fill () = Alcotest.(check matrix) "fill" x1 (To_test.fill ()) - -let row () = Alcotest.(check matrix) "row" (M.row x2 1) (To_test.row ()) - -let col () = Alcotest.(check matrix) "col" (M.col x2 1) (To_test.col ()) - -let trace () = Alcotest.(check (float eps)) "trace" 15. (To_test.trace x2) - -let sum () = Alcotest.(check (float eps)) "sum" 66. (To_test.sum x2) - -let exists () = Alcotest.(check bool) "exits" true (To_test.exists x2) - -let not_exists () = Alcotest.(check bool) "not_exists" true (To_test.not_exists x2) - -let for_all () = Alcotest.(check bool) "for_all" false (To_test.for_all x2) - -let equal () = - Alcotest.(check bool) "equal" true (To_test.equal x1 (M.map (( +. ) 1.) x0)) - - -let not_equal () = Alcotest.(check bool) "not_equal" true (To_test.not_equal x0 x1) - -let less () = Alcotest.(check bool) "less" true (To_test.less ()) - -let greater () = Alcotest.(check bool) "greater" false (To_test.greater ()) - -let greater_equal () = - Alcotest.(check bool) "greater_equal" true (To_test.greater_equal x2) - - -let less_equal () = Alcotest.(check bool) "less_equal" true (To_test.less_equal x2) - -let is_zero () = Alcotest.(check bool) "is_zero" true (To_test.is_zero x0) - -let is_positive () = Alcotest.(check bool) "is_positive" true (To_test.is_positive x1) - -let is_negative () = Alcotest.(check bool) "is_negative" false (To_test.is_negative x1) - -let is_nonnegative () = - Alcotest.(check bool) "is_nonnegative" true (To_test.is_nonnegative x0) - - -let is_nonpositive () = - Alcotest.(check bool) "is_nonpositive" true (To_test.is_nonpositive x0) - - -let add () = Alcotest.(check bool) "add" true (To_test.add x2) - -let mul () = Alcotest.(check bool) "mul" true (To_test.mul x2) - -let dot () = Alcotest.(check bool) "dot" true (To_test.dot ()) - -let add_scalar () = Alcotest.(check bool) "add_scalar" true (To_test.add_scalar ()) - -let mul_scalar () = Alcotest.(check bool) "mul_scalar" true (To_test.mul_scalar ()) - -let min () = Alcotest.(check bool) "min" true (To_test.min ()) - -let max () = Alcotest.(check bool) "max" true (To_test.max ()) - -let map () = Alcotest.(check bool) "map" true (To_test.map ()) - -let fold () = Alcotest.(check (float eps)) "fold" (M.sum x2) (To_test.fold x2) - -let foldi () = Alcotest.(check bool) "foldi" true (To_test.foldi ()) - -let foldi_nz () = Alcotest.(check bool) "foldi_nz" true (To_test.foldi_nz ()) - -let filter () = Alcotest.(check bool) "filter" true (To_test.filter ()) - -let fold_rows () = Alcotest.(check bool) "fold_rows" true (To_test.fold_rows ()) - -let fold_cols () = Alcotest.(check bool) "fold_cols" true (To_test.fold_cols ()) - -let sum_rows () = Alcotest.(check bool) "sum_rows" true (To_test.sum_rows ()) - -let sum_cols () = Alcotest.(check bool) "sum_cols" true (To_test.sum_cols ()) - -let of_array () = Alcotest.(check bool) "of_array" true (To_test.of_array ()) - -let save_load () = Alcotest.(check bool) "save_load" true (To_test.save_load ()) - -let test_set = - [ "sequential", `Slow, sequential; "row_num", `Slow, row_num; "col_num", `Slow, col_num - ; "numel", `Slow, numel; "transpose", `Slow, transpose; "get", `Slow, get - ; "set", `Slow, set; "row", `Slow, row; "col", `Slow, col; "fill", `Slow, fill - ; "trace", `Slow, trace; "sum", `Slow, sum; "exists", `Slow, exists - ; "not_exists", `Slow, not_exists; "for_all", `Slow, for_all; "equal", `Slow, equal - ; "not_equal", `Slow, not_equal; "less", `Slow, less; "greater", `Slow, greater - ; "greater_equal", `Slow, greater_equal; "less_equal", `Slow, less_equal - ; "is_zero", `Slow, is_zero; "is_positive", `Slow, is_positive - ; "is_negative", `Slow, is_negative; "is_nonnegative", `Slow, is_nonnegative - ; "is_nonpositive", `Slow, is_nonpositive; "add", `Slow, add; "mul", `Slow, mul - ; "dot", `Slow, dot; "add_scalar", `Slow, add_scalar; "mul_scalar", `Slow, mul_scalar - ; "min", `Slow, min; "max", `Slow, max; "map", `Slow, map; "fold", `Slow, fold - ; "foldi", `Slow, foldi; "foldi_nz", `Slow, foldi_nz; "filter", `Slow, filter - ; "fold_rows", `Slow, fold_rows; "fold_cols", `Slow, fold_cols - ; "sum_rows", `Slow, sum_rows; "sum_cols", `Slow, sum_cols; "of_array", `Slow, of_array - ; "save_load", `Slow, save_load ] diff --git a/test/unit_sparse_ndarray.ml b/test/unit_sparse_ndarray.ml deleted file mode 100644 index 429ad5d41..000000000 --- a/test/unit_sparse_ndarray.ml +++ /dev/null @@ -1,246 +0,0 @@ -(** Unit test for Owl_sparse_ndarray_generic module *) - -open Bigarray -module M = Owl_sparse_ndarray_generic - -(* make testable *) -let ndarray = Alcotest.testable (fun _p (_x : (float, float64_elt) M.t) -> ()) M.equal - -(* some test input *) -let x0 = M.zeros Float64 [| 2; 2; 3 |] - -let _ = - M.set x0 [| 0; 0; 1 |] 1.; - M.set x0 [| 0; 1; 0 |] 2.; - M.set x0 [| 1; 0; 0 |] 3. - - -let x1 = M.zeros Float64 [| 2; 2; 3 |] - -let _ = - M.set x1 [| 0; 0; 1 |] 1.; - M.set x1 [| 0; 0; 2 |] 2.; - M.set x1 [| 0; 1; 1 |] 3.; - M.set x1 [| 1; 0; 0 |] 4. - - -let x2 = M.zeros Float64 [| 2; 2; 3 |] - -let _ = - M.set x2 [| 0; 0; 1 |] 2.; - M.set x2 [| 0; 0; 2 |] 2.; - M.set x2 [| 0; 1; 0 |] 2.; - M.set x2 [| 0; 1; 1 |] 3.; - M.set x2 [| 1; 0; 0 |] 7. - - -(* a module with functions to test *) -module To_test = struct - let shape () = M.shape x0 = [| 2; 2; 3 |] - - let num_dims () = M.num_dims x0 = 3 - - let nth_dim () = M.nth_dim x0 2 = 3 - - let numel () = M.numel x0 = 12 - - let nnz () = M.nnz x0 = 3 - - let density () = M.density x0 = 3. /. 12. - - let get () = M.get x0 [| 0; 1; 0 |] = 2. - - let set () = - let x = M.zeros Float64 [| 2; 2; 3 |] in - M.set x [| 1; 0; 1 |] 5.; - M.get x [| 1; 0; 1 |] = 5. - - - let slice () = - let y = M.slice [| None; Some 0; Some 0 |] x0 in - let z = M.zeros Float64 [| 2 |] in - M.set z [| 1 |] 3.; - M.equal y z - - - let copy () = M.copy x0 = x0 - - let map () = M.map (fun a -> a +. 1.) x0 |> M.sum = 18. - - let map_nz () = M.map_nz (fun a -> a +. 1.) x0 |> M.sum = 9. - - let fold () = M.fold (fun c a -> c +. a) 0. x0 = 6. - - let foldi () = - let a = M.foldi (fun i c a -> if i.(2) = 0 then c +. a else c) 0. x0 in - a = 5. - - - let fold_nz () = M.fold ~axis:[| None; None; Some 0 |] (fun c a -> c +. a) 1. x0 = 6. - - let foldi_nz () = - M.fold ~axis:[| None; None; Some 0 |] (fun c a -> if a > 2. then c +. a else c) 1. x0 - = 4. - - - let add () = M.equal (M.add x0 x1) x2 - - let mul () = M.mul x0 x1 |> M.sum = 13. - - let add_scalar () = M.add_scalar x0 2. |> M.sum = 12. - - let mul_scalar () = M.mul_scalar x0 2. |> M.sum = 12. - - let abs () = M.equal (M.abs x0) x0 - - let neg () = M.equal (M.map (fun a -> -1. *. a) x0) (M.neg x0) - - let sum () = M.sum x0 = 6. - - let min () = M.min x0 = 0. - - let max () = M.max x0 = 3. - - let is_zero () = M.is_zero x0 - - let is_positive () = M.is_positive x0 - - let is_negative () = M.is_negative x0 - - let is_nonnegative () = M.is_nonnegative x0 - - let equal () = M.equal x0 x1 - - let greater () = M.greater x2 x0 - - let greater_equal () = M.greater_equal x2 x0 - - let filter () = M.filter (( = ) 3.) x0 = [| [| 1; 0; 0 |] |] - - let filteri () = M.filteri (fun i a -> i.(2) = 1 && a = 3.) x1 = [| [| 0; 1; 1 |] |] - - let filteri_nz () = M.filteri (fun i a -> i.(0) = 1 && a = 4.) x1 = [| [| 1; 0; 0 |] |] - - let transpose () = - let y = M.copy x0 in - let y = M.transpose y in - M.get y [| 1; 0; 0 |] = 1. && M.get y [| 0; 1; 0 |] = 2. && M.get y [| 0; 0; 1 |] = 3. - - - let flatten () = M.get (M.flatten x0) [| 3 |] = 2. - - let reshape () = M.get (M.reshape x0 [| 2; 3; 2 |]) [| 0; 1; 1 |] = 2. - - let of_array () = - let a = [| [| 0; 0; 1 |], 1.; [| 0; 1; 0 |], 2.; [| 1; 0; 0 |], 3. |] in - let y = M.of_array Float64 [| 2; 2; 3 |] a in - M.equal y x0 - - - let save_load () = - M.save x0 "ds_nda.tmp"; - let y = M.load Float64 "ds_nda.tmp" in - M.equal x0 y -end - -(* the tests *) - -let shape () = Alcotest.(check bool) "shape" true (To_test.shape ()) - -let num_dims () = Alcotest.(check bool) "num_dims" true (To_test.num_dims ()) - -let nth_dim () = Alcotest.(check bool) "nth_dim" true (To_test.nth_dim ()) - -let numel () = Alcotest.(check bool) "numel" true (To_test.numel ()) - -let nnz () = Alcotest.(check bool) "nnz" true (To_test.nnz ()) - -let density () = Alcotest.(check bool) "density" true (To_test.density ()) - -let get () = Alcotest.(check bool) "get" true (To_test.get ()) - -let set () = Alcotest.(check bool) "set" true (To_test.set ()) - -let slice () = Alcotest.(check bool) "slice" true (To_test.slice ()) - -let copy () = Alcotest.(check bool) "copy" true (To_test.copy ()) - -let map () = Alcotest.(check bool) "map" true (To_test.map ()) - -let map_nz () = Alcotest.(check bool) "map_nz" true (To_test.map_nz ()) - -let fold () = Alcotest.(check bool) "fold" true (To_test.fold ()) - -let foldi () = Alcotest.(check bool) "foldi" true (To_test.foldi ()) - -let fold_nz () = Alcotest.(check bool) "fold_nz" true (To_test.fold_nz ()) - -let foldi_nz () = Alcotest.(check bool) "foldi_nz" true (To_test.foldi_nz ()) - -let add () = Alcotest.(check bool) "add" true (To_test.add ()) - -let mul () = Alcotest.(check bool) "mul" true (To_test.mul ()) - -let add_scalar () = Alcotest.(check bool) "add_scalar" true (To_test.add_scalar ()) - -let mul_scalar () = Alcotest.(check bool) "mul_scalar" true (To_test.mul_scalar ()) - -let abs () = Alcotest.(check bool) "abs" true (To_test.abs ()) - -let neg () = Alcotest.(check bool) "neg" true (To_test.neg ()) - -let sum () = Alcotest.(check bool) "sum" true (To_test.sum ()) - -let min () = Alcotest.(check bool) "min" true (To_test.min ()) - -let max () = Alcotest.(check bool) "max" true (To_test.max ()) - -let is_zero () = Alcotest.(check bool) "is_zero" false (To_test.is_zero ()) - -let is_positive () = Alcotest.(check bool) "is_positive" false (To_test.is_positive ()) - -let is_negative () = Alcotest.(check bool) "is_negative" false (To_test.is_negative ()) - -let is_nonnegative () = - Alcotest.(check bool) "is_nonnegative" true (To_test.is_nonnegative ()) - - -let equal () = Alcotest.(check bool) "equal" false (To_test.equal ()) - -let greater () = Alcotest.(check bool) "greater" false (To_test.greater ()) - -let greater_equal () = - Alcotest.(check bool) "greater_equal" true (To_test.greater_equal ()) - - -let filter () = Alcotest.(check bool) "filter" true (To_test.filter ()) - -let filteri () = Alcotest.(check bool) "filteri" true (To_test.filteri ()) - -let filteri_nz () = Alcotest.(check bool) "filteri_nz" true (To_test.filteri_nz ()) - -let transpose () = Alcotest.(check bool) "transpose" true (To_test.transpose ()) - -let flatten () = Alcotest.(check bool) "flatten" true (To_test.flatten ()) - -let reshape () = Alcotest.(check bool) "reshape" true (To_test.reshape ()) - -let of_array () = Alcotest.(check bool) "of_array" true (To_test.of_array ()) - -let save_load () = Alcotest.(check bool) "save_load" true (To_test.save_load ()) - -let test_set = - [ "shape", `Slow, shape; "num_dims", `Slow, num_dims; "nth_dim", `Slow, nth_dim - ; "numel", `Slow, numel; "nnz", `Slow, nnz; "density", `Slow, density; "get", `Slow, get - ; "set", `Slow, set; "slice", `Slow, slice; "copy", `Slow, copy; "map", `Slow, map - ; "map_nz", `Slow, map_nz; "fold", `Slow, fold; "foldi", `Slow, foldi - ; "fold_nz", `Slow, fold_nz; "foldi_nz", `Slow, foldi_nz; "add", `Slow, add - ; "mul", `Slow, mul; "add_scalar", `Slow, add_scalar; "mul_scalar", `Slow, mul_scalar - ; "abs", `Slow, abs; "neg", `Slow, neg; "sum", `Slow, sum; "min", `Slow, min - ; "max", `Slow, max; "is_zero", `Slow, is_zero; "is_positive", `Slow, is_positive - ; "is_negative", `Slow, is_negative; "is_nonnegative", `Slow, is_nonnegative - ; "equal", `Slow, equal; "greater", `Slow, greater - ; "greater_equal", `Slow, greater_equal; "filter", `Slow, filter - ; "filteri", `Slow, filteri; "filteri_nz", `Slow, filteri_nz - ; "transpose", `Slow, transpose; "flatten", `Slow, flatten; "reshape", `Slow, reshape - ; "of_array", `Slow, of_array; "save_load", `Slow, save_load ]