Module Bigarray.Genarray

Contents

Instructions: Use this module in your project

In the IDE (CLion, Visual Studio Code, Xcode, etc.) you use for your DkSDK project:

  1. Add the following to your project's dependencies/CMakeLists.txt:

    Copy
    DkSDKProject_DeclareAvailable(ocaml
        CONSTRAINT "= 4.14.0"
        FINDLIBS str unix runtime_events threads dynlink)
    DkSDKProject_MakeAvailable(ocaml)
  2. Add the Findlib::ocaml library to any desired targets in src/*/CMakeLists.txt:

    Copy
    target_link_libraries(YourPackage_YourLibraryName
         # ... existing libraries, if any ...
         Findlib::ocaml)
  3. Click your IDE's Build button

Not using DkSDK?

FIRST, do one or all of the following:

  1. Run:

    Copy
    opam install ocaml.4.14.0
  2. Edit your dune-project and add:

    Copy
    (package
      (name YourExistingPackage)
      (depends
      ; ... existing dependenices ...
      (ocaml (>= 4.14.0))))

    Then run:

    Copy
    dune build *.opam # if this fails, run: dune build
  3. Edit your <package>.opam file and add:

    Copy
    depends: [
      # ... existing dependencies ...
      "ocaml" {>= "4.14.0"}
    ]

    Then run:

    Copy
    opam install . --deps-only

FINALLY, add the library to any desired (library)and/or (executable) targets in your **/dune files:

Copy
(library
  (name YourLibrary)
  ; ... existing library options ...
  (libraries
    ; ... existing libraries ...
    ))

(executable
  (name YourExecutable)
  ; ... existing executable options ...
  (libraries
    ; ... existing libraries ...
    ))
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)`` 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 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)`` kind -> 'c layout ->``int array``->``(``int array``-> 'a)`` -> ``('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.0
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.

  • raises Invalid_argument

    if n is less than 0 or greater or equal than Genarray.num_dims a.

val kind : ``('a, 'b, 'c)`` t -> ``('a, 'b)`` kind

Return the kind of the given Bigarray.

val layout : ``('a, 'b, 'c)`` t -> 'c layout

Return the layout of the given Bigarray.

val change_layout : ``('a, 'b, 'c)`` t -> 'd layout -> ``('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.0
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.

  • 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 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.)

  • 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 ->``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.)

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 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 < 0, or len < 0, or ofs + 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 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 < 1, or len < 0, or ofs + 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' 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 >= N, or if [|i1; ... ; iM|] is outside the bounds of a.

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.

  • raises Invalid_argument

    if M >= N, or if [|i1; ... ; iM|] is outside the bounds of a.

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

More from the DkSDK Book

    1. DkSDK
      1. Package capnp
        1. Module Capnp
          1. Module Capnp.Array
          1. Module Capnp.BytesStorage
          1. Module Capnp.Codecs
          1. Module Capnp.Message
          1. Module Capnp.MessageSig
            1. Module type MessageSig.MESSAGE
            1. Module type MessageSig.S
              1. Module S.ListStorage
              1. Module S.Message
              1. Module S.Object
              1. Module S.Segment
              1. Module S.Slice
              1. Module S.StructStorage
            1. Module type MessageSig.SEGMENT
            1. Module type MessageSig.SLICE
          1. Module Capnp.RPC
              1. Module type RPC.S
                1. Module S.Capability
                1. Module S.ListStorage
                1. Module S.Message
                1. Module S.Object
                1. Module S.Segment
                1. Module S.Service
                1. Module S.Slice
                1. Module S.StructRef
                1. Module S.StructStorage
                1. Module S.Untyped
              1. Module M.ListStorage
              1. Module M.Message
              1. Module M.Object
              1. Module M.Segment
              1. Module M.Slice
              1. Module M.StructStorage
          1. Module Capnp.Runtime
        1. Module Capnp_unix
          1. Module Capnp_unix.IO
      1. Package cmdliner
        1. Module Cmdliner
          1. Module Cmdliner.Arg
          1. Module Cmdliner.Cmd
          1. Module Cmdliner.Manpage
          1. Module Cmdliner.Term
        1. Module Cmdliner_arg
        1. Module Cmdliner_base
        1. Module Cmdliner_cline
        1. Module Cmdliner_cmd
        1. Module Cmdliner_docgen
        1. Module Cmdliner_eval
        1. Module Cmdliner_info
          1. Module Cmdliner_info.Arg
          1. Module Cmdliner_info.Cmd
          1. Module Cmdliner_info.Env
          1. Module Cmdliner_info.Eval
          1. Module Cmdliner_info.Exit
        1. Module Cmdliner_manpage
        1. Module Cmdliner_msg
        1. Module Cmdliner_term
        1. Module Cmdliner_term_deprecated
        1. Module Cmdliner_trie
      1. Package fmt
        1. Module Fmt
          1. Module Fmt.Dump
        1. Module Fmt_cli
        1. Module Fmt_tty
      1. Package logs
        1. Module Logs
        1. Module Logs_cli
        1. Module Logs_fmt
        1. Module Logs_lwt
        1. Module Logs_threaded
      1. Package lwt
        1. Module Lwt
          1. Module Lwt.Infix
          1. Module Lwt.Let_syntax
          1. Module Lwt.Syntax
        1. Module Lwt_bytes
        1. Module Lwt_condition
        1. Module Lwt_config
        1. Module Lwt_engine
          1. Module Lwt_engine.Ev_backend
          1. Module Lwt_engine.Versioned
        1. Module Lwt_features
        1. Module Lwt_fmt
        1. Module Lwt_gc
        1. Module Lwt_io
          1. Module Lwt_io.BE
          1. Module Lwt_io.LE
          1. Module Lwt_io.Versioned
        1. Module Lwt_list
        1. Module Lwt_main
          1. Module Lwt_main.Enter_iter_hooks
          1. Module Lwt_main.Exit_hooks
          1. Module Lwt_main.Leave_iter_hooks
        1. Module Lwt_mutex
        1. Module Lwt_mvar
        1. Module Lwt_pool
        1. Module Lwt_pqueue
          1. Module Lwt_pqueue.Make
        1. Module Lwt_preemptive
        1. Module Lwt_process
        1. Module Lwt_result
          1. Module Lwt_result.Infix
          1. Module Lwt_result.Let_syntax
          1. Module Lwt_result.Syntax
        1. Module Lwt_seq
        1. Module Lwt_sequence
        1. Module Lwt_stream
        1. Module Lwt_switch
        1. Module Lwt_sys
        1. Module Lwt_throttle
          1. Module Lwt_throttle.Make
        1. Module Lwt_timeout
        1. Module Lwt_unix
          1. Module Lwt_unix.IO_vectors
          1. Module Lwt_unix.LargeFile
          1. Module Lwt_unix.Versioned
      1. Package mtime
        1. Module Mtime
          1. Module Mtime.Span
        1. Module Mtime_clock
      1. Package ocaml
        1. Module Bigarray
        1. Module Condition
        1. Module Dynlink
        1. Module Event
        1. Module Mutex
        1. Module Profiling
        1. Module Semaphore
          1. Module Semaphore.Binary
          1. Module Semaphore.Counting
        1. Module Stdlib
          1. Module Stdlib.Arg
          1. Module Stdlib.Array
          1. Module Stdlib.ArrayLabels
          1. Module Stdlib.Atomic
          1. Module Stdlib.Bigarray
            1. ...
            1. Module Bigarray.Genarray
            1. ...
          1. Module Stdlib.Bool
          1. Module Stdlib.Buffer
          1. Module Stdlib.Bytes
          1. Module Stdlib.BytesLabels
          1. Module Stdlib.Callback
          1. Module Stdlib.Char
          1. Module Stdlib.Complex
          1. Module Stdlib.Digest
          1. Module Stdlib.Either
          1. Module Stdlib.Ephemeron
          1. Module Stdlib.Filename
          1. Module Stdlib.Float
          1. Module Stdlib.Format
          1. Module Stdlib.Fun
          1. Module Stdlib.Gc
          1. Module Stdlib.Genlex
          1. Module Stdlib.Hashtbl
              1. Module type Hashtbl.HashedType
              1. Module type Hashtbl.S
              1. Module type Hashtbl.SeededHashedType
              1. Module type Hashtbl.SeededS
          1. Module Stdlib.In_channel
          1. Module Stdlib.Int
          1. Module Stdlib.Int32
          1. Module Stdlib.Int64
          1. Module Stdlib.LargeFile
          1. Module Stdlib.Lazy
          1. Module Stdlib.Lexing
          1. Module Stdlib.List
          1. Module Stdlib.ListLabels
          1. Module Stdlib.Map
              1. Module type Map.OrderedType
              1. Module type Map.S
          1. Module Stdlib.Marshal
          1. Module Stdlib.MoreLabels
          1. Module Stdlib.Nativeint
          1. Module Stdlib.Obj
          1. Module Stdlib.Oo
          1. Module Stdlib.Option
          1. Module Stdlib.Out_channel
          1. Module Stdlib.Parsing
          1. Module Stdlib.Pervasives
          1. Module Stdlib.Printexc
          1. Module Stdlib.Printf
          1. Module Stdlib.Queue
          1. Module Stdlib.Random
          1. Module Stdlib.Result
          1. Module Stdlib.Scanf
          1. Module Stdlib.Seq
          1. Module Stdlib.Set
              1. Module type Set.OrderedType
              1. Module type Set.S
          1. Module Stdlib.Stack
          1. Module Stdlib.StdLabels
          1. Module Stdlib.Stream
          1. Module Stdlib.String
          1. Module Stdlib.StringLabels
          1. Module Stdlib.Sys
          1. Module Stdlib.Uchar
          1. Module Stdlib.Unit
          1. Module Stdlib.Weak
              1. Module type Weak.S
        1. Module Str
        1. Module Thread
        1. Module ThreadUnix
        1. Module Topdirs
        1. Module Unix
          1. Module Unix.LargeFile
        1. Module UnixLabels
          1. Module UnixLabels.LargeFile
          1. Module EndianBigstring
              1. Module type EndianBigstring.EndianBigstringSig
          1. Module EndianBytes
              1. Module type EndianBytes.EndianBytesSig
          1. Module EndianString
              1. Module type EndianString.EndianStringSig
      1. Package
      1. Package result
        1. Module Result
      1. Package stdint
        1. Module Stdint
          1. Module Stdint.Int128
          1. Module Stdint.Int16
          1. Module Stdint.Int24
          1. Module Stdint.Int32
          1. Module Stdint.Int40
          1. Module Stdint.Int48
          1. Module Stdint.Int56
          1. Module Stdint.Int64
          1. Module Stdint.Int8
          1. Module Stdint.Uint128
          1. Module Stdint.Uint16
          1. Module Stdint.Uint24
          1. Module Stdint.Uint32
          1. Module Stdint.Uint40
          1. Module Stdint.Uint48
          1. Module Stdint.Uint56
          1. Module Stdint.Uint64
          1. Module Stdint.Uint8