include module type of sig ... end
Element kinds
type float32_elt =
| Float32_elt
type float64_elt =
| Float64_elt
type int8_signed_elt =
| Int8_signed_elt
type int8_unsigned_elt =
| Int8_unsigned_elt
type int16_signed_elt =
| Int16_signed_elt
type int16_unsigned_elt =
| Int16_unsigned_elt
type int32_elt =
| Int32_elt
type int64_elt =
| Int64_elt
type int_elt =
| Int_elt
type nativeint_elt =
| Nativeint_elt
type complex32_elt =
| Complex32_elt
type complex64_elt =
| Complex64_elt
type ('a, 'b) kind =
| Float32 : (float, float32_elt) kind
| Float64 : (float, float64_elt) kind
| Int8_signed : (int, int8_signed_elt) kind
| Int8_unsigned : (int, int8_unsigned_elt) kind
| Int16_signed : (int, int16_signed_elt) kind
| Int16_unsigned : (int, int16_unsigned_elt) kind
| Int32 : (int32, int32_elt) kind
| Int64 : (int64, int64_elt) kind
| Int : (int, int_elt) kind
| Nativeint : (nativeint, nativeint_elt) kind
| Complex32 : (Stdlib.Complex.t, complex32_elt) kind
| Complex64 : (Stdlib.Complex.t, complex64_elt) kind
| Char : (char, int8_unsigned_elt) kind
To each element kind is associated an OCaml type, which is the type of OCaml values that can be stored in the Bigarray or read back from it. This type is not necessarily the same as the type of the array elements proper: for instance, a Bigarray whose elements are of kind
float32_elt
contains 32-bit single precision floats, but reading or writing one of its elements from OCaml uses the OCaml typefloat
, which is 64-bit double precision floats.The GADT type
('a, 'b) kind
captures this association of an OCaml type'a
for values read or written in the Bigarray, and of an element kind'b
which represents the actual contents of the Bigarray. Its constructors list all possible associations of OCaml types with element kinds, and are re-exported below for backward-compatibility reasons.Using a generalized algebraic datatype (GADT) here allows writing well-typed polymorphic functions whose return type depend on the argument type, such as:
let zero : type a b. (a, b) kind -> a = function | Float32 -> 0.0 | Complex32 -> Complex.zero | Float64 -> 0.0 | Complex64 -> Complex.zero | Int8_signed -> 0 | Int8_unsigned -> 0 | Int16_signed -> 0 | Int16_unsigned -> 0 | Int32 -> 0l | Int64 -> 0L | Int -> 0 | Nativeint -> 0n | Char -> '\000'
val float32 : (float, float32_elt) kind
See
Bigarray.char
.
val float64 : (float, float64_elt) kind
See
Bigarray.char
.
val complex32 : (Stdlib.Complex.t, complex32_elt) kind
See
Bigarray.char
.
val complex64 : (Stdlib.Complex.t, complex64_elt) kind
See
Bigarray.char
.
val int8_signed : (int, int8_signed_elt) kind
See
Bigarray.char
.
val int8_unsigned : (int, int8_unsigned_elt) kind
See
Bigarray.char
.
val int16_signed : (int, int16_signed_elt) kind
See
Bigarray.char
.
val int16_unsigned : (int, int16_unsigned_elt) kind
See
Bigarray.char
.
val int : (int, int_elt) kind
See
Bigarray.char
.
val int32 : (int32, int32_elt) kind
See
Bigarray.char
.
val int64 : (int64, int64_elt) kind
See
Bigarray.char
.
val nativeint : (nativeint, nativeint_elt) kind
See
Bigarray.char
.
val char : (char, int8_unsigned_elt) kind
As shown by the types of the values above, Bigarrays of kind
float32_elt
andfloat64_elt
are accessed using the OCaml typefloat
. Bigarrays of complex kindscomplex32_elt
,complex64_elt
are accessed with the OCaml typeComplex
.t. Bigarrays of integer kinds are accessed using the smallest OCaml integer type large enough to represent the array elements:int
for 8- and 16-bit integer Bigarrays, as well as OCaml-integer Bigarrays;int32
for 32-bit integer Bigarrays;int64
for 64-bit integer Bigarrays; andnativeint
for platform-native integer Bigarrays. Finally, Bigarrays of kindint8_unsigned_elt
can also be accessed as arrays of characters instead of arrays of small integers, by using the kind valuechar
instead ofint8_unsigned
.
val kind_size_in_bytes : ('a, 'b) kind -> int
kind_size_in_bytes k
is the number of bytes used to store an element of typek
.- since
- 4.03.0
Array layouts
type fortran_layout =
| Fortran_layout_typ
To facilitate interoperability with existing C and Fortran code, this library supports two different memory layouts for Bigarrays, one compatible with the C conventions, the other compatible with the Fortran conventions.
In the C-style layout, array indices start at 0, and multi-dimensional arrays are laid out in row-major format. That is, for a two-dimensional array, all elements of row 0 are contiguous in memory, followed by all elements of row 1, etc. In other terms, the array elements at
(x,y)
and(x, y+1)
are adjacent in memory.In the Fortran-style layout, array indices start at 1, and multi-dimensional arrays are laid out in column-major format. That is, for a two-dimensional array, all elements of column 0 are contiguous in memory, followed by all elements of column 1, etc. In other terms, the array elements at
(x,y)
and(x+1, y)
are adjacent in memory.Each layout style is identified at the type level by the phantom types
Bigarray.c_layout
andBigarray.fortran_layout
respectively.
Supported layouts
type 'a layout =
| C_layout : c_layout layout
| Fortran_layout : fortran_layout layout
val c_layout : c_layout layout
val fortran_layout : fortran_layout layout
Generic arrays (of arbitrarily many dimensions)
module Genarray : sig ... end
Zero-dimensional arrays
module Array0 : sig ... end
Zero-dimensional arrays. The
Array0
structure provides operations similar to those ofBigarray.Genarray
, but specialized to the case of zero-dimensional arrays that only contain a single scalar value. Statically knowing the number of dimensions of the array allows faster operations, and more precise static type-checking.
One-dimensional arrays
module Array1 : sig ... end
One-dimensional arrays. The
Array1
structure provides operations similar to those ofBigarray.Genarray
, but specialized to the case of one-dimensional arrays. (TheArray2
andArray3
structures below provide operations specialized for two- and three-dimensional arrays.) Statically knowing the number of dimensions of the array allows faster operations, and more precise static type-checking.
Two-dimensional arrays
module Array2 : sig ... end
Two-dimensional arrays. The
Array2
structure provides operations similar to those ofBigarray.Genarray
, but specialized to the case of two-dimensional arrays.
Three-dimensional arrays
module Array3 : sig ... end
Three-dimensional arrays. The
Array3
structure provides operations similar to those ofBigarray.Genarray
, but specialized to the case of three-dimensional arrays.
Coercions between generic Bigarrays and fixed-dimension Bigarrays
val genarray_of_array0 : ('a, 'b, 'c) Array0.t -> ('a, 'b, 'c) Genarray.t
Return the generic Bigarray corresponding to the given zero-dimensional Bigarray.
- since
- 4.05.0
val genarray_of_array1 : ('a, 'b, 'c) Array1.t -> ('a, 'b, 'c) Genarray.t
Return the generic Bigarray corresponding to the given one-dimensional Bigarray.
val genarray_of_array2 : ('a, 'b, 'c) Array2.t -> ('a, 'b, 'c) Genarray.t
Return the generic Bigarray corresponding to the given two-dimensional Bigarray.
val genarray_of_array3 : ('a, 'b, 'c) Array3.t -> ('a, 'b, 'c) Genarray.t
Return the generic Bigarray corresponding to the given three-dimensional Bigarray.
val array0_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array0.t
Return the zero-dimensional Bigarray corresponding to the given generic Bigarray. Raise
Invalid_argument
if the generic Bigarray does not have exactly zero dimension.- since
- 4.05.0
val array1_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array1.t
Return the one-dimensional Bigarray corresponding to the given generic Bigarray. Raise
Invalid_argument
if the generic Bigarray does not have exactly one dimension.
val array2_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array2.t
Return the two-dimensional Bigarray corresponding to the given generic Bigarray. Raise
Invalid_argument
if the generic Bigarray does not have exactly two dimensions.
val array3_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array3.t
Return the three-dimensional Bigarray corresponding to the given generic Bigarray. Raise
Invalid_argument
if the generic Bigarray does not have exactly three dimensions.
Re-shaping Bigarrays
val reshape : ('a, 'b, 'c) Genarray.t -> int array -> ('a, 'b, 'c) Genarray.t
reshape b [|d1;...;dN|]
converts the Bigarrayb
to aN
-dimensional array of dimensionsd1
...dN
. The returned array and the original arrayb
share their data and have the same layout. For instance, assuming thatb
is a one-dimensional array of dimension 12,reshape b [|3;4|]
returns a two-dimensional arrayb'
of dimensions 3 and 4. Ifb
has C layout, the element(x,y)
ofb'
corresponds to the elementx * 3 + y
ofb
. Ifb
has Fortran layout, the element(x,y)
ofb'
corresponds to the elementx + (y - 1) * 4
ofb
. The returned Bigarray must have exactly the same number of elements as the original Bigarrayb
. That is, the product of the dimensions ofb
must be equal toi1 * ... * iN
. Otherwise,Invalid_argument
is raised.
val reshape_0 : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array0.t
Specialized version of
Bigarray.reshape
for reshaping to zero-dimensional arrays.- since
- 4.05.0
val reshape_1 : ('a, 'b, 'c) Genarray.t -> int -> ('a, 'b, 'c) Array1.t
Specialized version of
Bigarray.reshape
for reshaping to one-dimensional arrays.
val reshape_2 : ('a, 'b, 'c) Genarray.t -> int -> int -> ('a, 'b, 'c) Array2.t
Specialized version of
Bigarray.reshape
for reshaping to two-dimensional arrays.
val reshape_3 : ('a, 'b, 'c) Genarray.t -> int -> int -> int -> ('a, 'b, 'c) Array3.t
Specialized version of
Bigarray.reshape
for reshaping to three-dimensional arrays.