package pyml

  1. Overview
  2. Docs

OCaml Interface for Numpy.

Arrays are passed in place (without copy): Python and OCaml programs can change the contents of the array and the changes are visible in the other language.

The following table gives the correspondence between bigarray kinds and Numpy element types.

  • float32 / NPY_FLOAT
  • float64 / NPY_DOUBLE
  • int8_signed / NPY_BYTE
  • int8_unsigned / NPY_UBYTE
  • int16_signed / NPY_SHORT
  • int16_unsigned / NPY_USHORT
  • int32 / NPY_INT
  • int64 / NPY_LONGLONG
  • nativeint / NPY_LONG
  • complex32 / NPY_CFLOAT
  • complex64 / NPY_CDOUBLE
  • char / NPY_CHAR

Other kinds/element types are not supported. In particular, OCaml integer kind, int, has no equivalent type in Numpy.

val of_bigarray : ('a, 'b, 'c) Stdlib.Bigarray.Genarray.t -> Py.Object.t

of_bigarray a returns a Numpy array that shares the same contents than the OCaml array a.

val to_bigarray : ('a, 'b) Stdlib.Bigarray.kind -> 'c Stdlib.Bigarray.layout -> Py.Object.t -> ('a, 'b, 'c) Stdlib.Bigarray.Genarray.t

to_bigarray kind layout a returns a bigarray that shares the same contents than the Numpy array a. If `kind` and/or `layout` are unknown, you may use to_bigarray_k.

type ('a, 'b, 'c) to_bigarray = {
  1. kind : ('a, 'b) Stdlib.Bigarray.kind;
  2. layout : 'c Stdlib.Bigarray.layout;
  3. array : ('a, 'b, 'c) Stdlib.Bigarray.Genarray.t;
}
type 'r to_bigarray_k = {
  1. f : 'a 'b 'c. ('a, 'b, 'c) to_bigarray -> 'r;
}
val to_bigarray_k : 'r to_bigarray_k -> Py.Object.t -> 'r

to_bigarray_k k a calls k.f with the contents of the Numpy array a. k.f has to be polymorphic in the kind and the layout of the bigarray: functions compare_kind, compare_layout and check_kind_and_layout can be used to introspect the bigarray polymorphically.

val compare_kind : ('a, 'b) Stdlib.Bigarray.kind -> ('c, 'd) Stdlib.Bigarray.kind -> int

compare_kind provides a total order on Bigarray.kind. As opposed to generic compare of OCaml standard libary, compare_kind is polymorphic in the kind of the bigarray.

val compare_layout : 'a Stdlib.Bigarray.layout -> 'b Stdlib.Bigarray.layout -> int

compare_layout provides a total order on Bigarray.layout. As opposed to generic compare of OCaml standard libary, compare_kind is polymorphic in the layout of the bigarray.

val check_kind_and_layout : ('a, 'b) Stdlib.Bigarray.kind -> 'c Stdlib.Bigarray.layout -> ('d, 'e, 'f) Stdlib.Bigarray.Genarray.t -> ('a, 'b, 'c) Stdlib.Bigarray.Genarray.t option

check_kind_and_layout kind layout a returns Some a if a has the given kind and layout (that is to say, if we have the following type equalities, 'a = 'd, 'b = 'e and 'c = 'f). This function allows the callback of to_bigarray_k to be polymorphic in the kind of the array.