Bigarray.Array3(3)
π NAME
Bigarray.Array3 β three-dimensional arrays, bestie. that's it, that's the vibe π
oh and hey, this page lives in the OCaml library manual, so you already know it's gonna be cute and typed β¨
π§ Module
okay so first things first, the whole thing ships as:
Module Array3
: sig end
π Documentation
three-dimensional arrays, periodt. the Array3 structure gives you the same kinds of operations as Bigarray.Genarray, except it's hyper-specialized to the three-dimensional case β like, it only knows how to do 3D and it does it iconically π
type (!'a, !'b, !'c) t
this is the type of three-dimensional Bigarrays, and the three params are doing completely different jobs, so let's break it down π«Ά: elements have OCaml type 'a, representation kind 'b, and memory layout 'c. three letters, three personalities, no cap.
πΈ the constructors
val create : ('a, 'b) Bigarray.kind -> 'c Bigarray.layout -> int -> int
-> int -> ('a, 'b, 'c) t
Array3.create kind layout dim1 dim2 dim3 hands you a brand-new three-dimensional Bigarray, sized dim1 in the first dimension, dim2 in the second, and dim3 in the third. kind and layout do exactly what they do for Bigarray.Genarray.create β element kind and array layout, respectively. lowkey the most normal function here π
val init : ('a, 'b) Bigarray.kind -> 'c Bigarray.layout -> int -> int ->
int -> (int -> int -> int -> 'a) -> ('a, 'b, 'c) t
Array3.init kind layout dim1 dim2 dim3 f gives you a new Bigarray b, three dimensions, size dim1 in the first, dim2 in the second, dim3 in the third. kind and layout set the element kind and layout, same deal as Bigarray.Genarray.create describes.
every single element Array3.get b i j k gets initialized to whatever f i j k returns β¨
put another way: Array3.init kind layout dim1 dim2 dim3 f tabulates the results of f applied to the indices of a fresh Bigarray whose layout is described by kind, layout, dim1, dim2 and dim3. it's giving "fill in the blanks but make it functional" π
Since 4.12 β so if you're on something ancient, this one's not invited to the party π
π¦ the little getters (dimensions & friends)
val dim1 : ('a, 'b, 'c) t -> int
returns the first dimension of the three-dimensional Bigarray you handed it. easy.
val dim2 : ('a, 'b, 'c) t -> int
returns the second dimension. we love a consistent queen.
val dim3 : ('a, 'b, 'c) t -> int
and yep β returns the third dimension. that's the whole trio, collect all three π―
val kind : ('a, 'b, 'c) t -> ('a, 'b) Bigarray.kind
returns the kind of the given Bigarray. iykyk.
val layout : ('a, 'b, 'c) t -> 'c Bigarray.layout
returns the layout of the given Bigarray. very demure, very mindful π
π₯ change_layout
val change_layout : ('a, 'b, 'c) t -> 'd Bigarray.layout -> ('a, 'b, 'd)
t
Array3.change_layout a layout returns a Bigarray with the layout you asked for, and it shares the data with a (so obviously it has the same dimensions as a). no copying of elements happens β the new array and the original are literally sharing the same storage space, besties. same brain cell, different outfit π
and here's the part you cannot skip: the dimensions are reversed, such that get v [| a; b; c |] in C layout becomes get v [| c+1; b+1; a+1 |] in Fortran layout. not me warning you about off-by-one in two languages at once π
Since 4.06
val size_in_bytes : ('a, 'b, 'c) t -> int
size_in_bytes a is the number of elements in a multiplied by a's Bigarray.kind_size_in_bytes. that's the math, that's the whole function, go off π
Since 4.03
π get & set (the main characters)
val get : ('a, 'b, 'c) t -> int -> int -> int -> 'a
Array3.get a x y z, also written a.{x,y,z}, returns the element of a at coordinates (x, y, z). and listen β x, y and z must be within the bounds of a, as described for Bigarray.Genarray.get. otherwise Invalid_argument gets raised, and that's not the vibe you want π¨
val set : ('a, 'b, 'c) t -> int -> int -> int -> 'a -> unit
Array3.set a x y v, or alternatively a.{x,y,z} <- v, stores the value v at coordinates (x, y, z) in a. same rules, same drama: x, y and z must be within the bounds of a as described for Bigarray.Genarray.set β step out of bounds and Invalid_argument is raised, no mercy π
π§ sub-array era: sub_left & sub_right
val sub_left : ('a, 'b, Bigarray.c_layout) t -> int -> int -> ('a, 'b,
Bigarray.c_layout) t
extracts a three-dimensional sub-array of the given three-dimensional Bigarray by restricting the first dimension. peep Bigarray.Genarray.sub_left for the full deets. and important: Array3.sub_left applies only to arrays with C layout. C-layout girlies only π
val sub_right : ('a, 'b, Bigarray.fortran_layout) t -> int -> int ->
('a, 'b, Bigarray.fortran_layout) t
extracts a three-dimensional sub-array of the given three-dimensional Bigarray by restricting the second dimension. Bigarray.Genarray.sub_right has the details. and Array3.sub_right applies only to arrays with Fortran layout. Fortran girlies, this one's yours π«Ά
β slice era, part one (the 1D ones)
val slice_left_1 : ('a, 'b, Bigarray.c_layout) t -> int -> int -> ('a,
'b, Bigarray.c_layout) Bigarray.Array1.t
extracts a one-dimensional slice of the given three-dimensional Bigarray by fixing the first two coordinates. the integer parameters are the coordinates of the slice you want. see Bigarray.Genarray.slice_left for more. Array3.slice_left_1 applies only to arrays with C layout β¨
val slice_right_1 : ('a, 'b, Bigarray.fortran_layout) t -> int -> int ->
('a, 'b, Bigarray.fortran_layout) Bigarray.Array1.t
extracts a one-dimensional slice of the given three-dimensional Bigarray by fixing the last two coordinates. the integer parameters are the coordinates of the slice to extract. Bigarray.Genarray.slice_right has the receipts. Array3.slice_right_1 applies only to arrays with Fortran layout π
π slice era, part two (the 2D ones)
val slice_left_2 : ('a, 'b, Bigarray.c_layout) t -> int -> ('a, 'b,
Bigarray.c_layout) Bigarray.Array2.t
extracts a two-dimensional slice of the given three-dimensional Bigarray by fixing the first coordinate. the integer parameter is the first coordinate of the slice to extract. see Bigarray.Genarray.slice_left for the details. Array3.slice_left_2 applies only to arrays with C layout, no exceptions π¦
val slice_right_2 : ('a, 'b, Bigarray.fortran_layout) t -> int -> ('a,
'b, Bigarray.fortran_layout) Bigarray.Array2.t
extracts a two-dimensional slice of the given three-dimensional Bigarray by fixing the last coordinate. the integer parameter is the coordinate of the slice to extract. Bigarray.Genarray.slice_right explains more. Array3.slice_right_2 applies only to arrays with Fortran layout π
πΈ blit & fill
val blit : ('a, 'b, 'c) t -> ('a, 'b, 'c) t -> unit
copies the first Bigarray to the second Bigarray. go read Bigarray.Genarray.blit for the fine print π
val fill : ('a, 'b, 'c) t -> 'a -> unit
fills the given Bigarray with the given value. Bigarray.Genarray.fill has more, as always π§
π of_array
val of_array : ('a, 'b) Bigarray.kind -> 'c Bigarray.layout -> 'a array
array array -> ('a, 'b, 'c) t
builds a three-dimensional Bigarray initialized from the given array of arrays of arrays. yes, 'a array array array β array-ception, and honestly? iconic π
β οΈ the unsafe girlies
val unsafe_get : ('a, 'b, 'c) t -> int -> int -> int -> 'a
like Bigarray.Array3.get, but bounds checking is not always performed. "not always" is doing a lot of heavy lifting in that sentence, so pls be careful π¨
val unsafe_set : ('a, 'b, 'c) t -> int -> int -> int -> 'a -> unit
like Bigarray.Array3.set, but bounds checking is not always performed. same energy, same warning: you are on your own here π
seriously bestie, unsafe_ is right there in the name for a reason β this is the "undefined-ish behaviour if you get it wrong" corner of the library, so treat it with respect, not group-chat recklessness π
okay that's the whole page, you ate that π
β¨ this lovely little doc dropped as part of the OCaml library documentation, generated by OCamldoc on 2026-06-22, and it's filed under Bigarray.Array3(3) β mwah, go forth and index three-dimensionally π«Άπ
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms