Stdlib.Bigarray(3) OCaml library Stdlib.Bigarray(3)

# [NAME](#NAME)

Stdlib.Bigarray - no description

# [Module](#Module)

Module Stdlib.Bigarray

# [Documentation](#Documentation)

Module **Bigarray**  
: **(module Stdlib\_\_Bigarray)**

## [Element kinds](#Element_kinds)

Bigarrays 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 integers ( **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* =  
| 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 **:** **(Complex.t, complex32\_elt) kind**  
| Complex64 **:** **(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 type **float** , 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* : **(Complex.t, complex32\_elt) kind**

See **Bigarray.char** .

*val complex64* : **(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** and **float64\_elt** are accessed using the OCaml type **float** . Bigarrays of complex kinds **complex32\_elt** , **complex64\_elt** are accessed with the OCaml type **Complex.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; and **nativeint** for platform-native integer Bigarrays. Finally, Bigarrays 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** .

*val kind\_size\_in\_bytes* : **('a, 'b) kind -&gt; int**

**kind\_size\_in\_bytes k** is the number of bytes used to store an element of type **k** .

**Since** 4.03.0

## [Array layouts](#Array_layouts)

*type c\_layout* =  
| C\_layout\_typ

See **Bigarray.fortran\_layout** .

*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** and **Bigarray.fortran\_layout** respectively.

## [Supported layouts](#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**

*val c\_layout* : **c\_layout layout**

*val fortran\_layout* : **fortran\_layout layout**

## [Generic arrays (of arbitrarily many dimensions)](#Generic_arrays_%28of_arbitrarily_many_dimensions%29)

*module Genarray :* **sig end**

## [Zero-dimensional arrays](#Zero-dimensional_arrays)

*module 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.

**Since** 4.05.0

## [One-dimensional arrays](#One-dimensional_arrays)

*module 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 **Bigarray.Array2** and **Bigarray.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](#Two-dimensional_arrays)

*module 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](#Three-dimensional_arrays)

*module 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 Bigarrays and fixed-dimension Bigarrays](#Coercions_between_generic_Bigarrays_and_fixed-dimension_Bigarrays)

*val genarray\_of\_array0* : **('a, 'b, 'c) Array0.t -&gt; ('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 -&gt; ('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 -&gt; ('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 -&gt; ('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 -&gt; ('a, 'b, 'c) Array0.t**

Return the zero-dimensional Bigarray corresponding to the given generic Bigarray.

**Since** 4.05.0

**Raises Invalid\_argument** if the generic Bigarray does not have exactly zero dimension.

*val array1\_of\_genarray* : **('a, 'b, 'c) Genarray.t -&gt; ('a, 'b, 'c) Array1.t**

Return the one-dimensional Bigarray corresponding to the given generic Bigarray.

**Raises Invalid\_argument** if the generic Bigarray does not have exactly one dimension.

*val array2\_of\_genarray* : **('a, 'b, 'c) Genarray.t -&gt; ('a, 'b, 'c) Array2.t**

Return the two-dimensional Bigarray corresponding to the given generic Bigarray.

**Raises Invalid\_argument** if the generic Bigarray does not have exactly two dimensions.

*val array3\_of\_genarray* : **('a, 'b, 'c) Genarray.t -&gt; ('a, 'b, 'c) Array3.t**

Return the three-dimensional Bigarray corresponding to the given generic Bigarray.

**Raises Invalid\_argument** if the generic Bigarray does not have exactly three dimensions.

## [Re-shaping Bigarrays](#Re-shaping_Bigarrays)

*val reshape* : **('a, 'b, 'c) Genarray.t -&gt;** **int array -&gt; ('a, 'b, 'c) Genarray.t**

**reshape b \[|d1;...;dN|]** converts the Bigarray **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 Bigarray must have exactly the same number of elements as the original Bigarray **b** . That is, the product of the dimensions of **b** must be equal to **i1 * ... * iN** . Otherwise, **Invalid\_argument** is raised.

*val reshape\_0* : **('a, 'b, 'c) Genarray.t -&gt; ('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 -&gt; int -&gt; ('a, 'b, 'c) Array1.t**

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

*val reshape\_2* : **('a, 'b, 'c) Genarray.t -&gt;** **int -&gt; int -&gt; ('a, 'b, 'c) Array2.t**

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

*val reshape\_3* : **('a, 'b, 'c) Genarray.t -&gt;** **int -&gt; int -&gt; int -&gt; ('a, 'b, 'c) Array3.t**

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

2026-10-27 OCamldoc

---

Document: Stdlib.Bigarray(3o) from ocaml-4 4.14.4-r4 (Chainguard OS)
Source: https://github.com/ocaml/ocaml
