Bigarray.Genarray(3) OCaml library Bigarray.Genarray(3)

# [NAME](#NAME)

Bigarray.Genarray - no description

# [Module](#Module)

Module Bigarray.Genarray

# [Documentation](#Documentation)

Module **Genarray**  
: **sig end**

*type* **(!'a, !'b, !'c)** *t*

The type **Genarray.t** is the type of Bigarrays with variable numbers of dimensions. Any number of dimensions between 0 and 16 is supported.

The three type parameters to **Genarray.t** identify the array element kind and layout, as follows:

-the first parameter, **'a** , is the OCaml type for accessing array elements ( **float** , **int** , **int32** , **int64** , **nativeint** );

-the second parameter, **'b** , is the actual kind of array elements ( **float32\_elt** , **float64\_elt** , **int8\_signed\_elt** , **int8\_unsigned\_elt** , etc);

-the third parameter, **'c** , identifies the array layout ( **c\_layout** or **fortran\_layout** ).

For instance, **(float, float32\_elt, fortran\_layout) Genarray.t** is the type of generic Bigarrays containing 32-bit floats in Fortran layout; reads and writes in this array use the OCaml type **float** .

*val create* : **('a, 'b) Bigarray.kind -&gt;** **'c Bigarray.layout -&gt; int array -&gt; ('a, 'b, 'c) t**

**Genarray.create kind layout dimensions** returns a new Bigarray whose element kind is determined by the parameter **kind** (one of **float32** , **float64** , **int8\_signed** , etc) and whose layout is determined by the parameter **layout** (one of **c\_layout** or **fortran\_layout** ). The **dimensions** parameter is an array of integers that indicate the size of the Bigarray in each dimension. The length of **dimensions** determines the number of dimensions of the Bigarray.

For instance, **Genarray.create int32 c\_layout \[|4;6;8|]** returns a fresh Bigarray of 32-bit integers, in C layout, having three dimensions, the three dimensions being 4, 6 and 8 respectively.

Bigarrays returned by **Genarray.create** are not initialized: the initial values of array elements is unspecified.

**Genarray.create** raises **Invalid\_argument** if the number of dimensions is not in the range 0 to 16 inclusive, or if one of the dimensions is negative.

*val init* : **('a, 'b) Bigarray.kind -&gt;** **'c Bigarray.layout -&gt;** **int array -&gt; (int array -&gt; 'a) -&gt; ('a, 'b, 'c) t**

**Genarray.init kind layout dimensions f** returns a new Bigarray **b** whose element kind is determined by the parameter **kind** (one of **float32** , **float64** , **int8\_signed** , etc) and whose layout is determined by the parameter **layout** (one of **c\_layout** or **fortran\_layout** ). The **dimensions** parameter is an array of integers that indicate the size of the Bigarray in each dimension. The length of **dimensions** determines the number of dimensions of the Bigarray.

Each element **Genarray.get b i** is initialized to the result of **f i** . In other words, **Genarray.init kind layout dimensions f** tabulates the results of **f** applied to the indices of a new Bigarray whose layout is described by **kind** , **layout** and **dimensions** . The index array **i** may be shared and mutated between calls to f.

For instance, **Genarray.init int c\_layout \[|2; 1; 3|]**  
(Array.fold\_left (+) 0) returns a fresh Bigarray of integers, in C layout, having three dimensions (2, 1, 3, respectively), with the element values 0, 1, 2, 1, 2, 3.

**Genarray.init** raises **Invalid\_argument** if the number of dimensions is not in the range 0 to 16 inclusive, or if one of the dimensions is negative.

**Since** 4.12

*val num\_dims* : **('a, 'b, 'c) t -&gt; int**

Return the number of dimensions of the given Bigarray.

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

**Genarray.dims a** returns all dimensions of the Bigarray **a** , as an array of integers of length **Genarray.num\_dims a** .

*val nth\_dim* : **('a, 'b, 'c) t -&gt; int -&gt; int**

**Genarray.nth\_dim a n** returns the **n** -th dimension of the Bigarray **a** . The first dimension corresponds to **n = 0** ; the second dimension corresponds to **n = 1** ; the last dimension, to **n = Genarray.num\_dims a - 1** .

**Raises Invalid\_argument** if **n** is less than 0 or greater or equal than **Genarray.num\_dims a** .

*val kind* : **('a, 'b, 'c) t -&gt; ('a, 'b) Bigarray.kind**

Return the kind of the given Bigarray.

*val layout* : **('a, 'b, 'c) t -&gt; 'c Bigarray.layout**

Return the layout of the given Bigarray.

*val change\_layout* : **('a, 'b, 'c) t -&gt;** **'d Bigarray.layout -&gt; ('a, 'b, 'd) t**

**Genarray.change\_layout a layout** returns a Bigarray with the specified **layout** , sharing the data with **a** (and hence having the same dimensions as **a** ). No copying of elements is involved: the new array and the original array share the same storage space. The dimensions are reversed, such that **get v \[| a; b |]** in C layout becomes **get v \[| b+1; a+1 |]** in Fortran layout.

**Since** 4.04

*val size\_in\_bytes* : **('a, 'b, 'c) t -&gt; int**

**size\_in\_bytes a** is the number of elements in **a** multiplied by **a** 's **Bigarray.kind\_size\_in\_bytes** .

**Since** 4.03

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

Read an element of a generic Bigarray. **Genarray.get a \[|i1; ...; iN|]** returns the element of **a** whose coordinates are **i1** in the first dimension, **i2** in the second dimension, ..., **iN** in the **N** -th dimension.

If **a** has C layout, the coordinates must be greater or equal than 0 and strictly less than the corresponding dimensions of **a** . If **a** has Fortran layout, the coordinates must be greater or equal than 1 and less or equal than the corresponding dimensions of **a** .

If **N &gt; 3** , alternate syntax is provided: you can write **a.{i1, i2, ..., iN}** instead of **Genarray.get a \[|i1; ...; iN|]** . (The syntax **a.{...}** with one, two or three coordinates is reserved for accessing one-, two- and three-dimensional arrays as described below.)

**Raises Invalid\_argument** if the array **a** does not have exactly **N** dimensions, or if the coordinates are outside the array bounds.

*val set* : **('a, 'b, 'c) t -&gt; int array -&gt; 'a -&gt; unit**

Assign an element of a generic Bigarray. **Genarray.set a \[|i1; ...; iN|] v** stores the value **v** in the element of **a** whose coordinates are **i1** in the first dimension, **i2** in the second dimension, ..., **iN** in the **N** -th dimension.

The array **a** must have exactly **N** dimensions, and all coordinates must lie inside the array bounds, as described for **Genarray.get** ; otherwise, **Invalid\_argument** is raised.

If **N &gt; 3** , alternate syntax is provided: you can write **a.{i1, i2, ..., iN} &lt;- v** instead of **Genarray.set a \[|i1; ...; iN|] v** . (The syntax **a.{...} &lt;- v** with one, two or three coordinates is reserved for updating one-, two- and three-dimensional arrays as described below.)

*val sub\_left* : **('a, 'b, Bigarray.c\_layout) t -&gt;** **int -&gt; int -&gt; ('a, 'b, Bigarray.c\_layout) t**

Extract a sub-array of the given Bigarray by restricting the first (left-most) dimension. **Genarray.sub\_left a ofs len** returns a Bigarray with the same number of dimensions as **a** , and the same dimensions as **a** , except the first dimension, which corresponds to the interval **\[ofs ... ofs + len - 1]** of the first dimension of **a** . No copying of elements is involved: the sub-array and the original array share the same storage space. In other terms, the element at coordinates **\[|i1; ...; iN|]** of the sub-array is identical to the element at coordinates **\[|i1+ofs; ...; iN|]** of the original array **a** .

**Genarray.sub\_left** applies only to Bigarrays in C layout.

**Raises Invalid\_argument** if **ofs** and **len** do not designate a valid sub-array of **a** , that is, if **ofs &lt; 0** , or **len &lt; 0** , or **ofs + len &gt; Genarray.nth\_dim a 0** .

*val sub\_right* : **('a, 'b, Bigarray.fortran\_layout) t -&gt;** **int -&gt; int -&gt; ('a, 'b, Bigarray.fortran\_layout) t**

Extract a sub-array of the given Bigarray by restricting the last (right-most) dimension. **Genarray.sub\_right a ofs len** returns a Bigarray with the same number of dimensions as **a** , and the same dimensions as **a** , except the last dimension, which corresponds to the interval **\[ofs ... ofs + len - 1]** of the last dimension of **a** . No copying of elements is involved: the sub-array and the original array share the same storage space. In other terms, the element at coordinates **\[|i1; ...; iN|]** of the sub-array is identical to the element at coordinates **\[|i1; ...; iN+ofs|]** of the original array **a** .

**Genarray.sub\_right** applies only to Bigarrays in Fortran layout.

**Raises Invalid\_argument** if **ofs** and **len** do not designate a valid sub-array of **a** , that is, if **ofs &lt; 1** , or **len &lt; 0** , or **ofs + len &gt; Genarray.nth\_dim a (Genarray.num\_dims a - 1)** .

*val slice\_left* : **('a, 'b, Bigarray.c\_layout) t -&gt;** **int array -&gt; ('a, 'b, Bigarray.c\_layout) t**

Extract a sub-array of lower dimension from the given Bigarray by fixing one or several of the first (left-most) coordinates. **Genarray.slice\_left a \[|i1; ... ; iM|]** returns the 'slice' of **a** obtained by setting the first **M** coordinates to **i1** , ..., **iM** . If **a** has **N** dimensions, the slice has dimension **N - M** , and the element at coordinates **\[|j1; ...; j(N-M)|]** in the slice is identical to the element at coordinates **\[|i1; ...; iM; j1; ...; j(N-M)|]** in the original array **a** . No copying of elements is involved: the slice and the original array share the same storage space.

**Genarray.slice\_left** applies only to Bigarrays in C layout.

**Raises Invalid\_argument** if **M &gt;= N** , or if **\[|i1; ... ; iM|]** is outside the bounds of **a** .

*val slice\_right* : **('a, 'b, Bigarray.fortran\_layout) t -&gt;** **int array -&gt; ('a, 'b, Bigarray.fortran\_layout) t**

Extract a sub-array of lower dimension from the given Bigarray by fixing one or several of the last (right-most) coordinates. **Genarray.slice\_right a \[|i1; ... ; iM|]** returns the 'slice' of **a** obtained by setting the last **M** coordinates to **i1** , ..., **iM** . If **a** has **N** dimensions, the slice has dimension **N - M** , and the element at coordinates **\[|j1; ...; j(N-M)|]** in the slice is identical to the element at coordinates **\[|j1; ...; j(N-M); i1; ...; iM|]** in the original array **a** . No copying of elements is involved: the slice and the original array share the same storage space.

**Genarray.slice\_right** applies only to Bigarrays in Fortran layout.

**Raises Invalid\_argument** if **M &gt;= N** , or if **\[|i1; ... ; iM|]** is outside the bounds of **a** .

*val blit* : **('a, 'b, 'c) t -&gt; ('a, 'b, 'c) t -&gt; unit**

Copy all elements of a Bigarray in another Bigarray. **Genarray.blit src dst** copies all elements of **src** into **dst** . Both arrays **src** and **dst** must have the same number of dimensions and equal dimensions. Copying a sub-array of **src** to a sub-array of **dst** can be achieved by applying **Genarray.blit** to sub-array or slices of **src** and **dst** .

*val fill* : **('a, 'b, 'c) t -&gt; 'a -&gt; unit**

Set all elements of a Bigarray to a given value. **Genarray.fill a v** stores the value **v** in all elements of the Bigarray **a** . Setting only some elements of **a** to **v** can be achieved by applying **Genarray.fill** to a sub-array or a slice of **a** .

2026-09-04 OCamldoc

---

Document: Bigarray.Genarray(3o) from ocaml 5.5.1-r0 (Chainguard OS)
Source: https://github.com/ocaml/ocaml
