package batteries

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module BatBigarraySource

Additional and modified functions for big arrays.

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:

  • comparisons (=, <>, <=, etc, as well as Pervasives.compare);
  • hashing (module Hash);
  • and structured input-output (Pervasives.output_value and Pervasives.input_value, as well as the functions from the Marshal module).

This module replaces Stdlib's Bigarray module.

  • author Michel Serrano (Base library)
  • author Xavier Leroy (Base library)
  • author David Teller

Element kinds

Big arrays can contain elements of the following kinds:

Each element kind is represented at the type level by one of the abstract types defined below.

Sourcetype float16_elt = Bigarray.float16_elt =
  1. | Float16_elt
Sourcetype float32_elt = Bigarray.float32_elt =
  1. | Float32_elt
Sourcetype float64_elt = Bigarray.float64_elt =
  1. | Float64_elt
Sourcetype complex32_elt = Bigarray.complex32_elt =
  1. | Complex32_elt
Sourcetype complex64_elt = Bigarray.complex64_elt =
  1. | Complex64_elt
Sourcetype int8_signed_elt = Bigarray.int8_signed_elt =
  1. | Int8_signed_elt
Sourcetype int8_unsigned_elt = Bigarray.int8_unsigned_elt =
  1. | Int8_unsigned_elt
Sourcetype int16_signed_elt = Bigarray.int16_signed_elt =
  1. | Int16_signed_elt
Sourcetype int16_unsigned_elt = Bigarray.int16_unsigned_elt =
  1. | Int16_unsigned_elt
Sourcetype int_elt = Bigarray.int_elt =
  1. | Int_elt
Sourcetype int32_elt = Bigarray.int32_elt =
  1. | Int32_elt
Sourcetype int64_elt = Bigarray.int64_elt =
  1. | Int64_elt
Sourcetype nativeint_elt = Bigarray.nativeint_elt =
  1. | Nativeint_elt
Sourcetype ('a, 'b) kind = ('a, 'b) Bigarray.kind =
  1. | Float32 : (float, float32_elt) kind
  2. | Float64 : (float, float64_elt) kind
  3. | Int8_signed : (int, int8_signed_elt) kind
  4. | Int8_unsigned : (int, int8_unsigned_elt) kind
  5. | Int16_signed : (int, int16_signed_elt) kind
  6. | Int16_unsigned : (int, int16_unsigned_elt) kind
  7. | Int32 : (int32, int32_elt) kind
  8. | Int64 : (int64, int64_elt) kind
  9. | Int : (int, int_elt) kind
  10. | Nativeint : (nativeint, nativeint_elt) kind
  11. | Complex32 : (Complex.t, complex32_elt) kind
  12. | Complex64 : (Complex.t, complex64_elt) kind
  13. | Char : (char, int8_unsigned_elt) kind
  14. | Float16 : (float, float16_elt) kind
    (*

    To each element kind is associated an OCaml type, which is the type of OCaml values that can be stored in the big array or read back from it. This type is not necessarily the same as the type of the array elements proper: for instance, a big array 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 type float, which is 64-bit double precision floats.

    # 137 "src/batBigarray.mli" # 138 "src/batBigarray.mli" # 139 "src/batBigarray.mli" # 140 "src/batBigarray.mli" # 141 "src/batBigarray.mli" # 142 "src/batBigarray.mli" The GADT type ('a, 'b) kind captures this association of an OCaml type 'a for values read or written in the big array, and of an element kind 'b which represents the actual contents of the big array. 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 to write 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'
    *)
Sourceval float32 : (float, float32_elt) kind
Sourceval float64 : (float, float64_elt) kind
Sourceval int8_signed : (int, int8_signed_elt) kind
Sourceval int8_unsigned : (int, int8_unsigned_elt) kind
Sourceval int16_signed : (int, int16_signed_elt) kind
Sourceval int16_unsigned : (int, int16_unsigned_elt) kind
Sourceval int : (int, int_elt) kind
Sourceval int32 : (int32, int32_elt) kind
Sourceval int64 : (int64, int64_elt) kind
Sourceval nativeint : (nativeint, nativeint_elt) kind
Sourceval char : (char, int8_unsigned_elt) kind

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.

Sourceval kind_size_in_bytes : ('a, 'b) kind -> int

kind_size_in_bytes k is the number of bytes used to store an element of type k.

  • since 2.5.0

Array layouts

Sourcetype c_layout = Bigarray.c_layout =
  1. | C_layout_typ
Sourcetype fortran_layout = Bigarray.fortran_layout =
  1. | Fortran_layout_typ

To facilitate interoperability with existing C and Fortran code, this library supports two different memory layouts for big arrays, 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 abstract types Bigarray.c_layout and fortran_layout respectively.

Sourcetype 'a layout = 'a Bigarray.layout =
  1. | C_layout : c_layout layout
  2. | Fortran_layout : fortran_layout layout

The type 'a layout represents one of the two supported memory layouts: C-style if 'a is Bigarray.c_layout, Fortran-style if 'a is Bigarray.fortran_layout.

Supported layouts

The abstract values c_layout and fortran_layout represent the two supported layouts at the level of values.

Sourceval c_layout : c_layout layout
Sourceval fortran_layout : fortran_layout layout
Sourcemodule Genarray : sig ... end

Generic arrays (of arbitrarily many dimensions)

Zero-dimensional arrays

Sourcemodule Array0 : sig ... end

Zero-dimensional arrays. The Array0 structure provides operations similar to those of Bigarray.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

Sourcemodule Array1 : sig ... end

One-dimensional arrays. The Array1 structure provides operations similar to those of Bigarray.Genarray, but specialized to the case of one-dimensional arrays. (The Array2 and Array3 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

Sourcemodule Array2 : sig ... end

Two-dimensional arrays. The Array2 structure provides operations similar to those of Bigarray.Genarray, but specialized to the case of two-dimensional arrays.

Three-dimensional arrays

Sourcemodule Array3 : sig ... end

Three-dimensional arrays. The Array3 structure provides operations similar to those of Bigarray.Genarray, but specialized to the case of three-dimensional arrays.

Coercions between generic big arrays and fixed-dimension big arrays

Sourceval genarray_of_array0 : ('a, 'b, 'c) Array0.t -> ('a, 'b, 'c) Genarray.t

Return the generic big array corresponding to the given zero-dimensional big array.

  • since 2.7.0 and OCaml 4.05.0
Sourceval genarray_of_array1 : ('a, 'b, 'c) Array1.t -> ('a, 'b, 'c) Genarray.t

Return the generic big array corresponding to the given one-dimensional big array.

Sourceval genarray_of_array2 : ('a, 'b, 'c) Array2.t -> ('a, 'b, 'c) Genarray.t

Return the generic big array corresponding to the given two-dimensional big array.

Sourceval genarray_of_array3 : ('a, 'b, 'c) Array3.t -> ('a, 'b, 'c) Genarray.t

Return the generic big array corresponding to the given three-dimensional big array.

Sourceval array0_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array0.t

Return the zero-dimensional big array corresponding to the given generic big array. Raise Invalid_argument if the generic big array does not have exactly zero dimension.

  • since 2.7.0 and OCaml 4.05.0
Sourceval array1_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array1.t

Return the one-dimensional big array corresponding to the given generic big array.

  • raises Invalid_argument

    if the generic big array does not have exactly one dimension.

Sourceval array2_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array2.t

Return the two-dimensional big array corresponding to the given generic big array.

  • raises Invalid_argument

    if the generic big array does not have exactly two dimensions.

Sourceval array3_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array3.t

Return the three-dimensional big array corresponding to the given generic big array.

  • raises Invalid_argument

    if the generic big array does not have exactly three dimensions.

Re-shaping big arrays

Sourceval reshape : ('a, 'b, 'c) Genarray.t -> int array -> ('a, 'b, 'c) Genarray.t

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.

Sourceval reshape_0 : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array0.t

Specialized version of Bigarray.reshape for reshaping to zero-dimensional arrays.

  • since 2.7.0 and OCaml 4.05.0
Sourceval reshape_1 : ('a, 'b, 'c) Genarray.t -> int -> ('a, 'b, 'c) Array1.t

Specialized version of Bigarray.reshape for reshaping to one-dimensional arrays.

Sourceval 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.

Sourceval 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.

OCaml

Innovation. Community. Security.