module Bigarray: sig .. end
Large, multi-dimensional, numerical arrays.
This module implements multi-dimensional arrays of integers and
floating-point numbers, thereafter referred to as 'big arrays'.
The implementation allows efficient sharing of large numerical
arrays between OCaml code and C or Fortran numerical libraries.
Concerning the naming conventions, users of this module are encouraged
to do open Bigarray;
in their source, then refer to array types and
operations via short dot notation, e.g. Array1.t
or Array2.sub
.
Big arrays support all the OCaml ad-hoc polymorphic operations:
Element kinds
Element kinds
Big arrays can contain elements of the following kinds:
- IEEE single precision (32 bits) floating-point numbers
(
Bigarray.float32_elt
),
- IEEE double precision (64 bits) floating-point numbers
(
Bigarray.float64_elt
),
- IEEE single precision (2 * 32 bits) floating-point complex numbers
(
Bigarray.complex32_elt
),
- IEEE double precision (2 * 64 bits) floating-point complex numbers
(
Bigarray.complex64_elt
),
- 8-bit integers (signed or unsigned)
(
Bigarray.int8_signed_elt
or Bigarray.int8_unsigned_elt
),
- 16-bit integers (signed or unsigned)
(
Bigarray.int16_signed_elt
or Bigarray.int16_unsigned_elt
),
- OCaml integers (signed, 31 bits on 32-bit architectures,
63 bits on 64-bit architectures) (
Bigarray.int_elt
),
- 32-bit signed integer (
Bigarray.int32_elt
),
- 64-bit signed integers (
Bigarray.int64_elt
),
- platform-native signed integers (32 bits on 32-bit architectures,
64 bits on 64-bit architectures) (
Bigarray.nativeint_elt
).
Each element kind is represented at the type level by one of the
*_elt
types defined below (defined with a single constructor instead
of abstract types for technical injectivity reasons).
type float32_elt =
type float64_elt =
type int8_signed_elt =
type int8_unsigned_elt =
type int16_signed_elt =
type int16_unsigned_elt =
type int32_elt =
type int64_elt =
type int_elt =
type nativeint_elt =
type complex32_elt =
type 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 : (Complex.t, complex32_elt) kind |
| |
Complex64 : (Complex.t, complex64_elt) kind |
| |
Char : (char, int8_unsigned_elt) kind |
let float32: kind(float, float32_elt);
let float64: kind(float, float64_elt);
let complex32: kind(Complex.t, complex32_elt);
let complex64: kind(Complex.t, complex64_elt);
let int8_signed: kind(int, int8_signed_elt);
let int8_unsigned: kind(int, int8_unsigned_elt);
let int16_signed: kind(int, int16_signed_elt);
let int16_unsigned: kind(int, int16_unsigned_elt);
let int: kind(int, int_elt);
let int32: kind(int32, int32_elt);
let int64: kind(int64, int64_elt);
let nativeint: kind(nativeint, nativeint_elt);
let char: kind(char, int8_unsigned_elt);
As shown by the types of the values above,
big arrays of kind
float32_elt
and
float64_elt
are
accessed using the OCaml type
float
. Big arrays of complex kinds
complex32_elt
,
complex64_elt
are accessed with the OCaml type
Complex.t
. Big arrays 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; and
nativeint
for
platform-native integer bigarrays. Finally, big arrays of
kind
int8_unsigned_elt
can also be accessed as arrays of
characters instead of arrays of small integers, by using
the kind value
char
instead of
int8_unsigned
.
Array layouts
type c_layout =
type fortran_layout =
Supported layouts
The GADT type 'a layout
represents one of the two supported
memory layouts: C-style or Fortran-style. Its constructors are
re-exported as values below for backward-compatibility reasons.
type 'a layout =
| |
C_layout : c_layout layout |
| |
Fortran_layout : fortran_layout layout |
let c_layout: layout(c_layout);
let fortran_layout: layout(fortran_layout);
Generic arrays (of arbitrarily many dimensions)
module Genarray: sig .. end
One-dimensional arrays
module Array1: sig .. end
One-dimensional arrays.
Two-dimensional arrays
module Array2: sig .. end
Two-dimensional arrays.
Three-dimensional arrays
module Array3: sig .. end
Three-dimensional arrays.
Coercions between generic big arrays and fixed-dimension big arrays
let genarray_of_array1: Array1.t('a, 'b, 'c) => Genarray.t('a, 'b, 'c);
Return the generic big array corresponding to the given one-dimensional
big array.
let genarray_of_array2: Array2.t('a, 'b, 'c) => Genarray.t('a, 'b, 'c);
Return the generic big array corresponding to the given two-dimensional
big array.
let genarray_of_array3: Array3.t('a, 'b, 'c) => Genarray.t('a, 'b, 'c);
Return the generic big array corresponding to the given three-dimensional
big array.
let array1_of_genarray: Genarray.t('a, 'b, 'c) => Array1.t('a, 'b, 'c);
Return the one-dimensional big array corresponding to the given
generic big array. Raise Invalid_argument
if the generic big array
does not have exactly one dimension.
let array2_of_genarray: Genarray.t('a, 'b, 'c) => Array2.t('a, 'b, 'c);
Return the two-dimensional big array corresponding to the given
generic big array. Raise Invalid_argument
if the generic big array
does not have exactly two dimensions.
let array3_of_genarray: Genarray.t('a, 'b, 'c) => Array3.t('a, 'b, 'c);
Return the three-dimensional big array corresponding to the given
generic big array. Raise Invalid_argument
if the generic big array
does not have exactly three dimensions.
Re-shaping big arrays
let reshape: (Genarray.t('a, 'b, 'c), array(int)) => Genarray.t('a, 'b, 'c);
reshape b [|d1;...;dN|]
converts the big array b
to a
N
-dimensional array of dimensions d1
...dN
. The returned
array and the original array b
share their data
and have the same layout. For instance, assuming that b
is a one-dimensional array of dimension 12, reshape b [|3;4|]
returns a two-dimensional array b'
of dimensions 3 and 4.
If b
has C layout, the element (x,y)
of b'
corresponds
to the element x * 3 + y
of b
. If b
has Fortran layout,
the element (x,y)
of b'
corresponds to the element
x + (y - 1) * 4
of b
.
The returned big array must have exactly the same number of
elements as the original big array b
. That is, the product
of the dimensions of b
must be equal to i1 * ... * iN
.
Otherwise, Invalid_argument
is raised.
let reshape_1: (Genarray.t('a, 'b, 'c), int) => Array1.t('a, 'b, 'c);
Specialized version of
Bigarray.reshape
for reshaping to
one-dimensional arrays.
let reshape_2: (Genarray.t('a, 'b, 'c), int, int) => Array2.t('a, 'b, 'c);
Specialized version of
Bigarray.reshape
for reshaping to
two-dimensional arrays.
let reshape_3: (Genarray.t('a, 'b, 'c), int, int, int) => Array3.t('a, 'b, 'c);
Specialized version of
Bigarray.reshape
for reshaping to
three-dimensional arrays.