package slap

  1. Overview
  2. Docs

Slap.Vec provides operations on sized vectors.

type (+'n, 'num, 'prec, +'cnt_or_dsc) t

('n, 'num, 'prec, 'cnt_or_dsc) vec is the type of 'n-dimensional vector whose elements have OCaml type 'num, representation kind 'prec and memory contiguity flag 'cnt_or_dsc. The internal implementation is fortran-style one-dimensional big array.

val cnt : ('n, 'num, 'prec, Slap_misc.cnt) t -> ('n, 'num, 'prec, 'cnt) t

Recover polymorphism of the fourth type parameter.

Creation of vectors

val create : ('num, 'prec) Bigarray.kind -> 'n Slap_size.t -> ('n, 'num, 'prec, 'cnt) t

create kind n

  • returns

    a fresh n-dimensional vector (not initialized).

val make : ('num, 'prec) Bigarray.kind -> 'n Slap_size.t -> 'num -> ('n, 'num, 'prec, 'cnt) t

make kind n a

  • returns

    a fresh n-dimensional vector initialized with a.

val init : ('a, 'b) Bigarray.kind -> 'n Slap_size.t -> (int -> 'a) -> ('n, 'a, 'b, 'cnt) t

init kind n f

  • returns

    a fresh vector (f 1, ..., f n) with n elements.

Accessors

val kind : ('n, 'num, 'prec, 'cd) t -> ('num, 'prec) Bigarray.kind
  • returns

    the kind of the given big array.

val dim : ('n, 'num, 'prec, 'cd) t -> 'n Slap_size.t

dim x

  • returns

    the dimension of the vector x.

val get_dyn : ('n, 'num, 'prec, 'cd) t -> int -> 'num

get_dyn x i

  • returns

    the i-th element of the vector x.

val set_dyn : ('n, 'num, 'prec, 'cd) t -> int -> 'num -> unit

set_dyn x i a assigns a to the i-th element of the vector x.

val unsafe_get : ('n, 'num, 'prec, 'cd) t -> int -> 'num

Like Slap_vec.get_dyn, but size checking is not always performed.

val unsafe_set : ('n, 'num, 'prec, 'cd) t -> int -> 'num -> unit

Like Slap_vec.set_dyn, but size checking is not always performed.

val replace_dyn : ('n, 'num, 'prec, 'cd) t -> int -> ('num -> 'num) -> unit

replace_dyn v i f is set_dyn v i (f (get_dyn v i)).

Iterators

val map : ('y_num, 'y_prec) Bigarray.kind -> ('x_num -> 'y_num) -> ?y:('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t

map kind f ?y (x1, ..., xn) is (f x1, ..., f xn).

  • returns

    the vector y, which is overwritten.

  • parameter y

    default = a fresh vector.

val mapi : ('y_num, 'y_prec) Bigarray.kind -> (int -> 'x_num -> 'y_num) -> ?y:('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t

mapi kind f ?y (x1, ..., xn) is (f 1 x1, ..., f n xn) with the vector's dimension n.

  • returns

    the vector y, which is overwritten.

  • parameter y

    default = a fresh vector.

val fold_left : ('accum -> 'x_num -> 'accum) -> 'accum -> ('n, 'x_num, 'prec, 'cd) t -> 'accum

fold_left f init (x1, x2, ..., xn) is f (... (f (f init x1) x2) ...) xn.

val fold_lefti : (int -> 'accum -> 'num -> 'accum) -> 'accum -> ('n, 'num, 'prec, 'cd) t -> 'accum

fold_lefti f init (x1, x2, ..., xn) is f n (... (f 2 (f 1 init x1) x2) ...) xn with the vector's dimension n.

val fold_right : ('num -> 'accum -> 'accum) -> ('n, 'num, 'prec, 'cd) t -> 'accum -> 'accum

fold_right f (x1, x2, ..., xn) init is f x1 (f x2 (... (f xn init) ...)).

val fold_righti : (int -> 'num -> 'accum -> 'accum) -> ('n, 'num, 'prec, 'cd) t -> 'accum -> 'accum

fold_righti f (x1, x2, ..., xn) init is f 1 x1 (f 2 x2 (... (f n xn init) ...)) with the vector's dimension n.

val replace_all : ('n, 'num, 'prec, 'cd) t -> ('num -> 'num) -> unit

replace_all x f modifies the vector x in place -- the i-th element xi of x will be set to f xi.

val replace_alli : ('n, 'num, 'prec, 'cd) t -> (int -> 'num -> 'num) -> unit

replace_alli x f modifies the vector x in place -- the i-th element xi of x will be set to f i xi.

val iter : ('num -> unit) -> ('n, 'num, 'prec, 'cd) t -> unit

iter f (x1, x2, ..., xn) is f x1; f x2; ...; f xn.

val iteri : (int -> 'num -> unit) -> ('n, 'num, 'prec, 'cd) t -> unit

iteri f (x1, x2, ..., xn) is f 1 x1; f 2 x2; ...; f n xn.

Iterators on two vectors

val map2 : ('z_num, 'z_prec) Bigarray.kind -> ('x_num -> 'y_num -> 'z_num) -> ?z:('n, 'z_num, 'z_prec, 'z_cd) t -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t

map2 kind f ?z (x1, x2, ..., xn) (y1, y2, ..., yn) is (f x1 y1, f x2 y2, ..., f xn yn).

  • returns

    the vector z, which is overwritten.

  • parameter z

    default = a fresh vector.

val mapi2 : ('z_num, 'z_prec) Bigarray.kind -> (int -> 'x_num -> 'y_num -> 'z_num) -> ?z:('n, 'z_num, 'z_prec, 'z_cd) t -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t

mapi2 kind f ?z (x1, x2, ..., xn) (y1, y2, ..., yn) is (f 1 x1 y1, f 2 x2 y2, ..., f n xn yn) with the vectors' dimension n.

  • returns

    the vector z, which is overwritten.

  • parameter z

    default = a fresh vector.

val fold_left2 : ('accum -> 'x_num -> 'y_num -> 'accum) -> 'accum -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> 'accum

fold_left2 f init (x1, x2, ..., xn) (y1, y2, ..., yn) is f (... (f (f init x1 y1) x2 y2) ...) xn yn.

val fold_lefti2 : (int -> 'accum -> 'x_num -> 'y_num -> 'accum) -> 'accum -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> 'accum

fold_lefti2 f init (x1, x2, ..., xn) (y1, y2, ..., yn) is f n (... (f 2 (f 1 init x1 y1) x2 y2) ...) xn yn with the vectors' dimension n.

val fold_right2 : ('x_num -> 'y_num -> 'accum -> 'accum) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> 'accum -> 'accum

fold_righti2 f (x1, x2, ..., xn) (y1, y2, ..., yn) init is f x1 y1 (f x2 y2 (... (f xn yn init) ...)).

val fold_righti2 : (int -> 'x_num -> 'y_num -> 'accum -> 'accum) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> 'accum -> 'accum

fold_righti2 f (x1, x2, ..., xn) (y1, y2, ..., yn) init is f 1 x1 y1 (f 2 x2 y2 (... (f n xn yn init) ...)) with the vectors' dimension n.

val iter2 : ('x_num -> 'y_num -> unit) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> unit

iter2 f (x1, x2, ..., xn) (y1, y2, ..., yn) is f x1 y1; f x2 y2; ...; f xn yn.

val iteri2 : (int -> 'x_num -> 'y_num -> unit) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> unit

iteri2 f (x1, x2, ..., xn) (y1, y2, ..., yn) is f 1 x1 y1; f 2 x2 y2; ...; f n xn yn.

Iterators on three vectors

val map3 : ('w_num, 'w_prec) Bigarray.kind -> ('x_num -> 'y_num -> 'z_num -> 'w_num) -> ?w:('n, 'w_num, 'w_prec, 'w_cd) t -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t -> ('n, 'w_num, 'w_prec, 'w_cd) t

map3 kind f ?w (x1, x2, ..., xn) (y1, y2, ..., yn) (z1, z2, ..., zn) is (f x1 y1 z1, f x2 y2 z2, ..., f xn yn zn).

  • returns

    the vector w, which is overwritten.

  • parameter w

    default = a fresh vector.

val mapi3 : ('w_num, 'w_prec) Bigarray.kind -> (int -> 'x_num -> 'y_num -> 'z_num -> 'w_num) -> ?w:('n, 'w_num, 'w_prec, 'w_cd) t -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t -> ('n, 'w_num, 'w_prec, 'w_cd) t

mapi3 kind f ?w (x1, x2, ..., xn) (y1, y2, ..., yn) (z1, z2, ..., zn) is (f 1 x1 y1 z1, f 2 x2 y2 z2, ..., f n xn yn zn) with the vectors' dimension n.

  • returns

    the vector w, which is overwritten.

  • parameter w

    default = a fresh vector.

val fold_left3 : ('accum -> 'x_num -> 'y_num -> 'z_num -> 'accum) -> 'accum -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t -> 'accum

fold_left3 f init (x1, x2, ..., xn) (y1, y2, ..., yn) (z1, z2, ..., zn) is f (... (f (f init x1 y1 z1) x2 y2 z2) ...) xn yn zn.

val fold_lefti3 : (int -> 'accum -> 'x_num -> 'y_num -> 'z_num -> 'accum) -> 'accum -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t -> 'accum

fold_lefti3 f init (x1, x2, ..., xn) (y1, y2, ..., yn) (z1, z2, ..., zn) is f n (... (f 2 (f 1 init x1 y1 z1) x2 y2 z2) ...) xn yn zn with the vectors' dimension n.

val fold_right3 : ('x_num -> 'y_num -> 'z_num -> 'accum -> 'accum) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t -> 'accum -> 'accum

fold_right3 f (x1, x2, ..., xn) (y1, y2, ..., yn) (z1, z2, ..., zn) init is f x1 y1 z1 (f x2 y2 z2 (... (f xn yn zn init) ...)).

val fold_righti3 : (int -> 'x_num -> 'y_num -> 'z_num -> 'accum -> 'accum) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t -> 'accum -> 'accum

fold_righti3 f (x1, x2, ..., xn) (y1, y2, ..., yn) (z1, z2, ..., zn) init is f 1 x1 y1 z1 (f 2 x2 y2 z2 (... (f n xn yn zn init) ...)) with the vectors' dimension n.

val iter3 : ('x_num -> 'y_num -> 'z_num -> unit) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t -> unit

iter3 f (x1, x2, ..., xn) (y1, y2, ..., yn) (z1, z2, ..., zn) is f x1 y1 z1; f x2 y2 z2; ...; f xn yn zn.

val iteri3 : (int -> 'x_num -> 'y_num -> 'z_num -> unit) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> ('n, 'z_num, 'z_prec, 'z_cd) t -> unit

iteri3 f (x1, x2, ..., xn) (y1, y2, ..., yn) (z1, z2, ..., zn) is f 1 x1 y1 z1; f 2 x2 y2 z2; ...; f n xn yn zn.

Scanning

val for_all : ('num -> bool) -> ('n, 'num, 'prec, 'cd) t -> bool

for_all p (x1, x2, ..., xn) is (p x1) && (p x2) && ... && (p xn).

  • returns

    true if and only if all elements of the given vector satisfy the predicate p.

val exists : ('num -> bool) -> ('n, 'num, 'prec, 'cd) t -> bool

exists p (x1, x2, ..., xn) is (p x1) || (p x2) || ... || (p xn).

  • returns

    true if and only if at least one element of the given vector satisfies the predicate p.

val for_all2 : ('x_num -> 'y_num -> bool) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> bool

for_all2 p (x1, x2, ..., xn) (y1, y2, ..., yn) is (p x1 y1) && (p x2 y2) && ... && (p xn yn).

  • returns

    true if and only if all elements of the given two vectors satisfy the predicate p.

val exists2 : ('x_num -> 'y_num -> bool) -> ('n, 'x_num, 'x_prec, 'x_cd) t -> ('n, 'y_num, 'y_prec, 'y_cd) t -> bool

exists2 p (x1, x2, ..., xn) (y1, y2, ..., yn) is (p x1 y1) || (p x2 y2) || ... || (p xn yn).

  • returns

    true if and only if at least one pair of elements of the given two vectors satisfies the predicate p.

val mem : ?equal:('num -> 'num -> bool) -> 'num -> ('n, 'num, 'prec, 'cd) t -> bool

mem ?equal a v

  • returns

    true if and only if a is equal to an element of v.

  • parameter equal

    default = (=) (polymorphic compare)

Basic operations

val cons : ?y:('n Slap_size.s, 'num, 'prec, 'y_cd) t -> 'num -> ('n, 'num, 'prec, 'x_cd) t -> ('n Slap_size.s, 'num, 'prec, 'y_cd) t

cons ?y e x adds element e at the beginning of vector x, and copies it into y.

  • returns

    the vector y, which is overwritten.

  • parameter y

    default = a fresh vector.

  • since 4.0.0
val hd : ('n Slap_size.s, 'num, 'prec, 'x_cd) t -> 'num
  • returns

    the first element of a given vector. (typesafe)

  • since 4.0.0
val hd_dyn : ('n, 'num, 'prec, 'x_cd) t -> 'num
  • returns

    the first element of a given vector.

  • since 4.0.0
val last : ('n Slap_size.s, 'num, 'prec, 'x_cd) t -> 'num
  • returns

    the last element of a given vector. (typesafe)

  • since 4.0.0
val last_dyn : ('n, 'num, 'prec, 'x_cd) t -> 'num
  • returns

    the last element of a given vector.

  • since 4.0.0
val tl : ?share:bool -> ('n Slap_size.s, 'num, 'prec, 'x_cd) t -> ('n, 'num, 'prec, 'x_cd) t
  • returns

    a given vector without the first element. (typesafe)

  • parameter share

    true if a returned subvector refers a given vector (default = false)

  • since 4.0.0
val tl_dyn : ?share:bool -> ('n, 'num, 'prec, 'x_cd) t -> ('n Slap_size.p, 'num, 'prec, 'x_cd) t
  • returns

    a given vector without the first element.

  • parameter share

    true if a returned subvector refers a given vector (default = false)

  • since 4.0.0
val inits : ?share:bool -> ('n Slap_size.s, 'num, 'prec, 'x_cd) t -> ('n, 'num, 'prec, 'x_cd) t
  • returns

    a given vector without the first element. (typesafe) This is the same as init in Haskell.

  • parameter share

    true if a returned subvector refers a given vector (default = false)

  • since 4.0.0
val inits_dyn : ?share:bool -> ('n, 'num, 'prec, 'x_cd) t -> ('n Slap_size.p, 'num, 'prec, 'x_cd) t
  • returns

    a given vector without the first element. This is the same as init in Haskell.

  • parameter share

    true if a returned subvector refers a given vector (default = false)

  • since 4.0.0
val copy : ?y:('n, 'num, 'prec, 'y_cd) t -> ('n, 'num, 'prec, 'x_cd) t -> ('n, 'num, 'prec, 'y_cd) t

copy ?y x copies the vector x to the vector y.

  • returns

    the vector y, which is overwritten.

  • parameter y

    default = a fresh vector.

val fill : ('n, 'num, 'prec, 'cd) t -> 'num -> unit

Fill the given vector with the given value.

val append : ('m, 'num, 'prec, 'x_cd) t -> ('n, 'num, 'prec, 'y_cd) t -> (('m, 'n) Slap_size.add, 'num, 'prec, 'cnt) t

Concatenate two vectors.

val shared_rev : ('n, 'num, 'prec, 'cd) t -> ('n, 'num, 'prec, Slap_misc.dsc) t

shared_rev (x1, x2, ..., xn)

  • returns

    reversed vector (xn, ..., x2, x1). The data are shared.

val rev : ('n, 'num, 'prec, 'cd) t -> ('n, 'num, 'prec, 'cd) t

rev (x1, x2, ..., xn)

  • returns

    reversed vector (xn, ..., x2, x1). The data are NOT shared.

Conversion

val to_array : ('n, 'num, 'prec, 'cd) t -> 'num array

to_array x

  • returns

    the array of all the elements of the vector x.

val of_array_dyn : ('num, 'prec) Bigarray.kind -> 'n Slap_size.t -> 'num array -> ('n, 'num, 'prec, 'cnt) t

of_array_dyn kind n [|a1; ...; an|]

  • returns

    a fresh vector (a1, ..., an).

val unsafe_of_array : ('num, 'prec) Bigarray.kind -> 'n Slap_size.t -> 'num array -> ('n, 'num, 'prec, 'cnt) t

Like of_array_dyn, but size checking is not always performed.

  • since 1.0.0
val to_list : ('n, 'num, 'prec, 'cd) t -> 'num list

to_list x

  • returns

    the list of all the elements of the vector x.

val of_list_dyn : ('num, 'prec) Bigarray.kind -> 'n Slap_size.t -> 'num list -> ('n, 'num, 'prec, 'cnt) t

of_list_dyn kind n [a1; ...; an]

  • returns

    a fresh vector (a1, ..., an).

val unsafe_of_list : ('num, 'prec) Bigarray.kind -> 'n Slap_size.t -> 'num list -> ('n, 'num, 'prec, 'cnt) t

Like of_list_dyn, but size checking is not always performed.

  • since 1.0.0
val to_bigarray : ('n, 'num, 'prec, 'cd) t -> ('num, 'prec, Bigarray.fortran_layout) Bigarray.Array1.t

to_bigarray x

  • returns

    the big array of all the elements of the vector x.

val of_bigarray_dyn : ?share:bool -> 'n Slap_size.t -> ('num, 'prec, Bigarray.fortran_layout) Bigarray.Array1.t -> ('n, 'num, 'prec, 'cnt) t

of_bigarray_dyn ?share n ba

  • returns

    a fresh vector of all the elements of big array ba.

  • parameter share

    true if data are shared. (default = false)

val unsafe_of_bigarray : ?share:bool -> 'n Slap_size.t -> ('num, 'prec, Bigarray.fortran_layout) Bigarray.Array1.t -> ('n, 'num, 'prec, 'cnt) t

Like unsafe_of_bigarray, but size checking is not always performed.

  • since 1.0.0
type ('num, 'prec, 'cnt_or_dsc) dyn =
  1. | VEC : ('n, 'num, 'prec, 'cnt_or_dsc) t -> ('num, 'prec, 'cnt_or_dsc) dyn

The type of packages of existential quantified sized type: ('num, 'prec, 'cnt_or_dsc) dyn = exists n. (n, 'num, 'prec, 'cnt_or_dsc) Vec.t.

  • since 4.0.0
val of_array_c : ('num, 'prec) Bigarray.kind -> 'num array -> ('num, 'prec, 'cnt) dyn

let VEC n = of_array_c kind [|a1; ...; an|]

  • returns

    a constructor VEC that has a vector (a1, ..., an) that has the existential quantified sized type exists n. (n, 'num, 'prec, 'cnt_or_dsc) Vec.t. "c" of of_array_c means a "c"onstructor of GADT. This function is available in OCaml 4.00 or above.

  • since 4.0.0
val of_list_c : ('num, 'prec) Bigarray.kind -> 'num list -> ('num, 'prec, 'cnt) dyn

let VEC n = of_list_c kind [a1; ...; an]

  • returns

    a constructor VEC that has a vector (a1, ..., an) that has the existential quantified sized type exists n. (n, 'num, 'prec, 'cnt_or_dsc) Vec.t. "c" of of_list_c means a "c"onstructor of GADT. This function is available in OCaml 4.00 or above.

  • since 4.0.0
val of_bigarray_c : ?share:bool -> ('num, 'prec, Bigarray.fortran_layout) Bigarray.Array1.t -> ('num, 'prec, 'cnt) dyn

let VEC n = of_bigarray_c ?share ba

  • returns

    a constructor VEC that has a vector (of all the elements of big array ba) that has the existential quantified sized type exists n. (n, 'num, 'prec, 'cnt_or_dsc) Vec.t. "c" of of_bigarray_c means a "c"onstructor of GADT. This function is available in OCaml 4.00 or above.

  • parameter share

    true if data are shared. (default = false)

  • since 4.0.0

Subvectors

val subcntvec_dyn : 'm Slap_size.t -> ?ofsx:int -> ('n, 'num, 'prec, Slap_misc.cnt) t -> ('m, 'num, 'prec, 'cnt) t

subcntvec_dyn m ?ofsx x

  • returns

    a subvector of the contiguous vector x. The i-th element of it refers (ofsx+i-1)-th element of x. The data are shared.

  • parameter ofsx

    default = 1

val subdscvec_dyn : 'm Slap_size.t -> ?ofsx:int -> ?incx:int -> ('n, 'num, 'prec, 'cd) t -> ('m, 'num, 'prec, Slap_misc.dsc) t

subdscvec_dyn m ?ofsx ?incx x

  • returns

    a subvector of the vector x. The i-th element of it refers (ofsx+(i-1)*incx)-th element of x. The data are shared.

  • parameter ofs

    default = 1

  • parameter inc

    default = 1

  • raises Invalid_argument

    m, ofsx and incx do not designate a valid subvector of x.

val subvec_dyn : 'm Slap_size.t -> ?ofsx:int -> ?incx:int -> ('n, 'num, 'prec, 'cd) t -> ('m, 'num, 'prec, Slap_misc.dsc) t

An alias of subdscvec_dyn.