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
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 typefloat
.- the first parameter,
val create : ('a, 'b) kind -> 'c layout -> int array -> ('a, 'b, 'c) t
Genarray.create kind layout dimensions
returns a new Bigarray whose element kind is determined by the parameterkind
(one offloat32
,float64
,int8_signed
, etc) and whose layout is determined by the parameterlayout
(one ofc_layout
orfortran_layout
). Thedimensions
parameter is an array of integers that indicate the size of the Bigarray in each dimension. The length ofdimensions
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
raisesInvalid_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 Bigarraya
, as an array of integers of lengthGenarray.num_dims a
.
val nth_dim : ('a, 'b, 'c) t -> int -> int
Genarray.nth_dim a n
returns then
-th dimension of the Bigarraya
. The first dimension corresponds ton = 0
; the second dimension corresponds ton = 1
; the last dimension, ton = Genarray.num_dims a - 1
. RaiseInvalid_argument
ifn
is less than 0 or greater or equal thanGenarray.num_dims a
.
val change_layout : ('a, 'b, 'c) t -> 'd layout -> ('a, 'b, 'd) t
Genarray.change_layout a layout
returns a Bigarray with the specifiedlayout
, sharing the data witha
(and hence having the same dimensions asa
). No copying of elements is involved: the new array and the original array share the same storage space. The dimensions are reversed, such thatget v [| a; b |]
in C layout becomesget v [| b+1; a+1 |]
in Fortran layout.- since
- 4.04.0
val size_in_bytes : ('a, 'b, 'c) t -> int
size_in_bytes a
is the number of elements ina
multiplied bya
'skind_size_in_bytes
.- since
- 4.03.0
val get : ('a, 'b, 'c) t -> int array -> 'a
Read an element of a generic Bigarray.
Genarray.get a [|i1; ...; iN|]
returns the element ofa
whose coordinates arei1
in the first dimension,i2
in the second dimension, ...,iN
in theN
-th dimension.If
a
has C layout, the coordinates must be greater or equal than 0 and strictly less than the corresponding dimensions ofa
. Ifa
has Fortran layout, the coordinates must be greater or equal than 1 and less or equal than the corresponding dimensions ofa
. RaiseInvalid_argument
if the arraya
does not have exactlyN
dimensions, or if the coordinates are outside the array bounds.If
N > 3
, alternate syntax is provided: you can writea.{i1, i2, ..., iN}
instead ofGenarray.get a [|i1; ...; iN|]
. (The syntaxa.{...}
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 valuev
in the element ofa
whose coordinates arei1
in the first dimension,i2
in the second dimension, ...,iN
in theN
-th dimension.The array
a
must have exactlyN
dimensions, and all coordinates must lie inside the array bounds, as described forGenarray.get
; otherwise,Invalid_argument
is raised.If
N > 3
, alternate syntax is provided: you can writea.{i1, i2, ..., iN} <- v
instead ofGenarray.set a [|i1; ...; iN|] v
. (The syntaxa.{...} <- 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, c_layout) t -> int -> int -> ('a, 'b, 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 asa
, and the same dimensions asa
, except the first dimension, which corresponds to the interval[ofs ... ofs + len - 1]
of the first dimension ofa
. 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 arraya
.Genarray.sub_left
applies only to Bigarrays in C layout. RaiseInvalid_argument
ifofs
andlen
do not designate a valid sub-array ofa
, that is, ifofs < 0
, orlen < 0
, orofs + len > Genarray.nth_dim a 0
.
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 asa
, and the same dimensions asa
, except the last dimension, which corresponds to the interval[ofs ... ofs + len - 1]
of the last dimension ofa
. 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 arraya
.Genarray.sub_right
applies only to Bigarrays in Fortran layout. RaiseInvalid_argument
ifofs
andlen
do not designate a valid sub-array ofa
, that is, ifofs < 1
, orlen < 0
, orofs + len > Genarray.nth_dim a (Genarray.num_dims a - 1)
.
val slice_left : ('a, 'b, c_layout) t -> int array -> ('a, 'b, 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' ofa
obtained by setting the firstM
coordinates toi1
, ...,iM
. Ifa
hasN
dimensions, the slice has dimensionN - 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 arraya
. 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. RaiseInvalid_argument
ifM >= N
, or if[|i1; ... ; iM|]
is outside the bounds ofa
.
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' ofa
obtained by setting the lastM
coordinates toi1
, ...,iM
. Ifa
hasN
dimensions, the slice has dimensionN - 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 arraya
. 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. RaiseInvalid_argument
ifM >= N
, or if[|i1; ... ; iM|]
is outside the bounds ofa
.
val blit : ('a, 'b, 'c) t -> ('a, 'b, 'c) t -> unit
Copy all elements of a Bigarray in another Bigarray.
Genarray.blit src dst
copies all elements ofsrc
intodst
. Both arrayssrc
anddst
must have the same number of dimensions and equal dimensions. Copying a sub-array ofsrc
to a sub-array ofdst
can be achieved by applyingGenarray.blit
to sub-array or slices ofsrc
anddst
.
val fill : ('a, 'b, 'c) t -> 'a -> unit
Set all elements of a Bigarray to a given value.
Genarray.fill a v
stores the valuev
in all elements of the Bigarraya
. Setting only some elements ofa
tov
can be achieved by applyingGenarray.fill
to a sub-array or a slice ofa
.