moonpool/dev/ocaml/Stdlib/Bigarray/Genarray/index.html
2023-08-28 17:11:38 +00:00

15 lines
No EOL
24 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Genarray (ocaml.Stdlib.Bigarray.Genarray)</title><link rel="stylesheet" href="../../../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.1"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../../../index.html">ocaml</a> &#x00BB; <a href="../../index.html">Stdlib</a> &#x00BB; <a href="../index.html">Bigarray</a> &#x00BB; Genarray</nav><header class="odoc-preamble"><h1>Module <code><span>Bigarray.Genarray</span></code></h1></header><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> <span>(!'a, !'b, !'c) t</span></span></code></div><div class="spec-doc"><p>The type <code>Genarray.t</code> is the type of Bigarrays with variable numbers of dimensions. Any number of dimensions between 0 and 16 is supported.</p><p>The three type parameters to <code>Genarray.t</code> identify the array element kind and layout, as follows:</p><ul><li>the first parameter, <code>'a</code>, is the OCaml type for accessing array elements (<code>float</code>, <code>int</code>, <code>int32</code>, <code>int64</code>, <code>nativeint</code>);</li><li>the second parameter, <code>'b</code>, is the actual kind of array elements (<code>float32_elt</code>, <code>float64_elt</code>, <code>int8_signed_elt</code>, <code>int8_unsigned_elt</code>, etc);</li><li>the third parameter, <code>'c</code>, identifies the array layout (<code>c_layout</code> or <code>fortran_layout</code>).</li></ul><p>For instance, <code>(float, float32_elt, fortran_layout) Genarray.t</code> is the type of generic Bigarrays containing 32-bit floats in Fortran layout; reads and writes in this array use the OCaml type <code>float</code>.</p></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-create"><a href="#val-create" class="anchor"></a><code><span><span class="keyword">val</span> create : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="../index.html#type-kind">kind</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span><span class="type-var">'c</span> <a href="../index.html#type-layout">layout</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>int array</span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p><code>Genarray.create kind layout dimensions</code> returns a new Bigarray whose element kind is determined by the parameter <code>kind</code> (one of <code>float32</code>, <code>float64</code>, <code>int8_signed</code>, etc) and whose layout is determined by the parameter <code>layout</code> (one of <code>c_layout</code> or <code>fortran_layout</code>). The <code>dimensions</code> parameter is an array of integers that indicate the size of the Bigarray in each dimension. The length of <code>dimensions</code> determines the number of dimensions of the Bigarray.</p><p>For instance, <code>Genarray.create int32 c_layout [|4;6;8|]</code> returns a fresh Bigarray of 32-bit integers, in C layout, having three dimensions, the three dimensions being 4, 6 and 8 respectively.</p><p>Bigarrays returned by <code>Genarray.create</code> are not initialized: the initial values of array elements is unspecified.</p><p><code>Genarray.create</code> raises <code>Invalid_argument</code> if the number of dimensions is not in the range 0 to 16 inclusive, or if one of the dimensions is negative.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-init"><a href="#val-init" class="anchor"></a><code><span><span class="keyword">val</span> init :
<span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="../index.html#type-kind">kind</a></span> <span class="arrow">&#45;&gt;</span></span>
<span><span><span class="type-var">'c</span> <a href="../index.html#type-layout">layout</a></span> <span class="arrow">&#45;&gt;</span></span>
<span><span>int array</span> <span class="arrow">&#45;&gt;</span></span>
<span><span>(<span><span>int array</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span>)</span> <span class="arrow">&#45;&gt;</span></span>
<span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p><code>Genarray.init kind layout dimensions f</code> returns a new Bigarray <code>b</code> whose element kind is determined by the parameter <code>kind</code> (one of <code>float32</code>, <code>float64</code>, <code>int8_signed</code>, etc) and whose layout is determined by the parameter <code>layout</code> (one of <code>c_layout</code> or <code>fortran_layout</code>). The <code>dimensions</code> parameter is an array of integers that indicate the size of the Bigarray in each dimension. The length of <code>dimensions</code> determines the number of dimensions of the Bigarray.</p><p>Each element <code>Genarray.get b i</code> is initialized to the result of <code>f i</code>. In other words, <code>Genarray.init kind layout dimensions f</code> tabulates the results of <code>f</code> applied to the indices of a new Bigarray whose layout is described by <code>kind</code>, <code>layout</code> and <code>dimensions</code>. The index array <code>i</code> may be shared and mutated between calls to f.</p><p>For instance, <code>Genarray.init int c_layout [|2; 1; 3|]
(Array.fold_left (+) 0)</code> 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.</p><p><code>Genarray.init</code> raises <code>Invalid_argument</code> if the number of dimensions is not in the range 0 to 16 inclusive, or if one of the dimensions is negative.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.12.0</li></ul></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-num_dims"><a href="#val-num_dims" class="anchor"></a><code><span><span class="keyword">val</span> num_dims : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>Return the number of dimensions of the given Bigarray.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-dims"><a href="#val-dims" class="anchor"></a><code><span><span class="keyword">val</span> dims : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span>int array</span></span></code></div><div class="spec-doc"><p><code>Genarray.dims a</code> returns all dimensions of the Bigarray <code>a</code>, as an array of integers of length <code>Genarray.num_dims a</code>.</p></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-nth_dim"><a href="#val-nth_dim" class="anchor"></a><code><span><span class="keyword">val</span> nth_dim : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p><code>Genarray.nth_dim a n</code> returns the <code>n</code>-th dimension of the Bigarray <code>a</code>. The first dimension corresponds to <code>n = 0</code>; the second dimension corresponds to <code>n = 1</code>; the last dimension, to <code>n = Genarray.num_dims a - 1</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>n</code> is less than 0 or greater or equal than <code>Genarray.num_dims a</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-kind"><a href="#val-kind" class="anchor"></a><code><span><span class="keyword">val</span> kind : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="../index.html#type-kind">kind</a></span></span></code></div><div class="spec-doc"><p>Return the kind of the given Bigarray.</p></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-layout"><a href="#val-layout" class="anchor"></a><code><span><span class="keyword">val</span> layout : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'c</span> <a href="../index.html#type-layout">layout</a></span></span></code></div><div class="spec-doc"><p>Return the layout of the given Bigarray.</p></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-change_layout"><a href="#val-change_layout" class="anchor"></a><code><span><span class="keyword">val</span> change_layout : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span><span class="type-var">'d</span> <a href="../index.html#type-layout">layout</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'d</span>)</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p><code>Genarray.change_layout a layout</code> returns a Bigarray with the specified <code>layout</code>, sharing the data with <code>a</code> (and hence having the same dimensions as <code>a</code>). No copying of elements is involved: the new array and the original array share the same storage space. The dimensions are reversed, such that <code>get v [| a; b |]</code> in C layout becomes <code>get v [| b+1; a+1 |]</code> in Fortran layout.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.04.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-size_in_bytes"><a href="#val-size_in_bytes" class="anchor"></a><code><span><span class="keyword">val</span> size_in_bytes : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p><code>size_in_bytes a</code> is the number of elements in <code>a</code> multiplied by <code>a</code>'s <a href="../index.html#val-kind_size_in_bytes"><code>kind_size_in_bytes</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.03.0</li></ul></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-get"><a href="#val-get" class="anchor"></a><code><span><span class="keyword">val</span> get : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>int array</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Read an element of a generic Bigarray. <code>Genarray.get a [|i1; ...; iN|]</code> returns the element of <code>a</code> whose coordinates are <code>i1</code> in the first dimension, <code>i2</code> in the second dimension, ..., <code>iN</code> in the <code>N</code>-th dimension.</p><p>If <code>a</code> has C layout, the coordinates must be greater or equal than 0 and strictly less than the corresponding dimensions of <code>a</code>. If <code>a</code> has Fortran layout, the coordinates must be greater or equal than 1 and less or equal than the corresponding dimensions of <code>a</code>.</p><p>If <code>N &gt; 3</code>, alternate syntax is provided: you can write <code>a.{i1, i2, ..., iN}</code> instead of <code>Genarray.get a [|i1; ...; iN|]</code>. (The syntax <code>a.{...}</code> with one, two or three coordinates is reserved for accessing one-, two- and three-dimensional arrays as described below.)</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if the array <code>a</code> does not have exactly <code>N</code> dimensions, or if the coordinates are outside the array bounds.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-set"><a href="#val-set" class="anchor"></a><code><span><span class="keyword">val</span> set : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>int array</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Assign an element of a generic Bigarray. <code>Genarray.set a [|i1; ...; iN|] v</code> stores the value <code>v</code> in the element of <code>a</code> whose coordinates are <code>i1</code> in the first dimension, <code>i2</code> in the second dimension, ..., <code>iN</code> in the <code>N</code>-th dimension.</p><p>The array <code>a</code> must have exactly <code>N</code> dimensions, and all coordinates must lie inside the array bounds, as described for <code>Genarray.get</code>; otherwise, <code>Invalid_argument</code> is raised.</p><p>If <code>N &gt; 3</code>, alternate syntax is provided: you can write <code>a.{i1, i2, ..., iN} &lt;- v</code> instead of <code>Genarray.set a [|i1; ...; iN|] v</code>. (The syntax <code>a.{...} &lt;- v</code> with one, two or three coordinates is reserved for updating one-, two- and three-dimensional arrays as described below.)</p></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-sub_left"><a href="#val-sub_left" class="anchor"></a><code><span><span class="keyword">val</span> sub_left : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <a href="../index.html#type-c_layout">c_layout</a>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <a href="../index.html#type-c_layout">c_layout</a>)</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p>Extract a sub-array of the given Bigarray by restricting the first (left-most) dimension. <code>Genarray.sub_left a ofs len</code> returns a Bigarray with the same number of dimensions as <code>a</code>, and the same dimensions as <code>a</code>, except the first dimension, which corresponds to the interval <code>[ofs ... ofs + len - 1]</code> of the first dimension of <code>a</code>. 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 <code>[|i1; ...; iN|]</code> of the sub-array is identical to the element at coordinates <code>[|i1+ofs; ...; iN|]</code> of the original array <code>a</code>.</p><p><code>Genarray.sub_left</code> applies only to Bigarrays in C layout.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>ofs</code> and <code>len</code> do not designate a valid sub-array of <code>a</code>, that is, if <code>ofs &lt; 0</code>, or <code>len &lt; 0</code>, or <code>ofs + len &gt; Genarray.nth_dim a 0</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-sub_right"><a href="#val-sub_right" class="anchor"></a><code><span><span class="keyword">val</span> sub_right :
<span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <a href="../index.html#type-fortran_layout">fortran_layout</a>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span>
<span>int <span class="arrow">&#45;&gt;</span></span>
<span>int <span class="arrow">&#45;&gt;</span></span>
<span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <a href="../index.html#type-fortran_layout">fortran_layout</a>)</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p>Extract a sub-array of the given Bigarray by restricting the last (right-most) dimension. <code>Genarray.sub_right a ofs len</code> returns a Bigarray with the same number of dimensions as <code>a</code>, and the same dimensions as <code>a</code>, except the last dimension, which corresponds to the interval <code>[ofs ... ofs + len - 1]</code> of the last dimension of <code>a</code>. 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 <code>[|i1; ...; iN|]</code> of the sub-array is identical to the element at coordinates <code>[|i1; ...; iN+ofs|]</code> of the original array <code>a</code>.</p><p><code>Genarray.sub_right</code> applies only to Bigarrays in Fortran layout.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>ofs</code> and <code>len</code> do not designate a valid sub-array of <code>a</code>, that is, if <code>ofs &lt; 1</code>, or <code>len &lt; 0</code>, or <code>ofs + len &gt; Genarray.nth_dim a (Genarray.num_dims a - 1)</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-slice_left"><a href="#val-slice_left" class="anchor"></a><code><span><span class="keyword">val</span> slice_left : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <a href="../index.html#type-c_layout">c_layout</a>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>int array</span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <a href="../index.html#type-c_layout">c_layout</a>)</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p>Extract a sub-array of lower dimension from the given Bigarray by fixing one or several of the first (left-most) coordinates. <code>Genarray.slice_left a [|i1; ... ; iM|]</code> returns the 'slice' of <code>a</code> obtained by setting the first <code>M</code> coordinates to <code>i1</code>, ..., <code>iM</code>. If <code>a</code> has <code>N</code> dimensions, the slice has dimension <code>N - M</code>, and the element at coordinates <code>[|j1; ...; j(N-M)|]</code> in the slice is identical to the element at coordinates <code>[|i1; ...; iM; j1; ...; j(N-M)|]</code> in the original array <code>a</code>. No copying of elements is involved: the slice and the original array share the same storage space.</p><p><code>Genarray.slice_left</code> applies only to Bigarrays in C layout.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>M &gt;= N</code>, or if <code>[|i1; ... ; iM|]</code> is outside the bounds of <code>a</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-slice_right"><a href="#val-slice_right" class="anchor"></a><code><span><span class="keyword">val</span> slice_right :
<span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <a href="../index.html#type-fortran_layout">fortran_layout</a>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span>
<span><span>int array</span> <span class="arrow">&#45;&gt;</span></span>
<span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <a href="../index.html#type-fortran_layout">fortran_layout</a>)</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p>Extract a sub-array of lower dimension from the given Bigarray by fixing one or several of the last (right-most) coordinates. <code>Genarray.slice_right a [|i1; ... ; iM|]</code> returns the 'slice' of <code>a</code> obtained by setting the last <code>M</code> coordinates to <code>i1</code>, ..., <code>iM</code>. If <code>a</code> has <code>N</code> dimensions, the slice has dimension <code>N - M</code>, and the element at coordinates <code>[|j1; ...; j(N-M)|]</code> in the slice is identical to the element at coordinates <code>[|j1; ...; j(N-M); i1; ...; iM|]</code> in the original array <code>a</code>. No copying of elements is involved: the slice and the original array share the same storage space.</p><p><code>Genarray.slice_right</code> applies only to Bigarrays in Fortran layout.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>M &gt;= N</code>, or if <code>[|i1; ... ; iM|]</code> is outside the bounds of <code>a</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-blit"><a href="#val-blit" class="anchor"></a><code><span><span class="keyword">val</span> blit : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Copy all elements of a Bigarray in another Bigarray. <code>Genarray.blit src dst</code> copies all elements of <code>src</code> into <code>dst</code>. Both arrays <code>src</code> and <code>dst</code> must have the same number of dimensions and equal dimensions. Copying a sub-array of <code>src</code> to a sub-array of <code>dst</code> can be achieved by applying <code>Genarray.blit</code> to sub-array or slices of <code>src</code> and <code>dst</code>.</p></div></div><div class="odoc-spec"><div class="spec value external anchored" id="val-fill"><a href="#val-fill" class="anchor"></a><code><span><span class="keyword">val</span> fill : <span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>, <span class="type-var">'c</span>)</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Set all elements of a Bigarray to a given value. <code>Genarray.fill a v</code> stores the value <code>v</code> in all elements of the Bigarray <code>a</code>. Setting only some elements of <code>a</code> to <code>v</code> can be achieved by applying <code>Genarray.fill</code> to a sub-array or a slice of <code>a</code>.</p></div></div></div></body></html>