package ocaml-base-compiler
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
orfortran_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
.
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 num_dims : ('a, 'b, 'c) t -> int
Return the number of dimensions of the given Bigarray.
val dims : ('a, 'b, 'c) t -> 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 -> int -> 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
.
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.
val size_in_bytes : ('a, 'b, 'c) t -> int
size_in_bytes a
is the number of elements in a
multiplied by a
's kind_size_in_bytes
.
val get : ('a, 'b, 'c) t -> int array -> '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 > 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.)
val set : ('a, 'b, 'c) t -> int array -> 'a -> 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 > 3
, alternate syntax is provided: you can write a.{i1, i2, ..., iN} <- v
instead of Genarray.set a [|i1; ...; iN|] v
. (The syntax a.{...} <- v
with one, two or three coordinates is reserved for updating one-, two- and three-dimensional arrays as described below.)
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.
val sub_right :
('a, 'b, fortran_layout) t ->
int ->
int ->
('a, 'b, 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.
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.
val slice_right :
('a, 'b, fortran_layout) t ->
int array ->
('a, 'b, 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.
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 -> 'a -> 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
.