File: indices.rst

package info (click to toggle)
xtensor 0.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,476 kB
  • sloc: cpp: 65,302; makefile: 202; python: 171; javascript: 8
file content (289 lines) | stat: -rw-r--r-- 8,155 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
.. Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht

   Distributed under the terms of the BSD 3-Clause License.

   The full license is in the file LICENSE, distributed with this software.

Indices
=======

Definition
----------

There are two types of indices: *array indices* and *flat indices*. Consider this example (stored in row-major):

.. code-block:: cpp

    #include <xtensor/xtensor.hpp>
    #include <xtensor/xarray.hpp>
    #include <xtensor/xio.hpp>

    int main()
    {
        xt::xarray<size_t> a = xt::arange<size_t>(3 * 4);

        a.reshape({3,4});

        std::cout << a << std::endl;
    }

Which prints

.. code-block:: none

    {{ 0,  1,  2,  3},
     { 4,  5,  6,  7},
     { 8,  9, 10, 11}}

The *array index* ``{1, 2}`` corresponds to the *flat index* ``6``.

Operators: array index
------------------------

An *array index* can be specified to an operators by a sequence of numbers.
To this end the following operators are at your disposal:

:cpp:func:`operator()(args...) <xt::xcontainer::operator>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*   Example: ``a(1, 2) == 6``.
*   See also: :cpp:func:`xt::xcontainer::operator()`.

Returns a (constant) reference to the element,
specified by an *array index* given by a number of unsigned integers.

*   If the number of indices is less that the dimension of the array,
    the indices are pre-padded with zeros until the dimension is matched
    (example: ``a(2) == a(0, 2) == 2``).

*   If the number of indices is greater than the dimension of the array,
    the first ``#indices - dimension`` indices are ignored.

*   To post-pad an arbitrary number of zeros use ``xt::missing``
    (example ``a(2, xt::missing) == a(2, 0) == 8``.

:cpp:func:`at(args...) <xt::xcontainer::at>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*   Example: ``a.at(1, 2) == 6``.
*   See also: :cpp:func:`xt::xcontainer::at`.

Same as :cpp:func:`~xt::xcontainer::operator()`:
Returns a (constant) reference to the element,
specified by an *array index* given by a number of unsigned integers.

:cpp:func:`unchecked(args...) <xt::xcontainer::unchecked>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*   Example: ``a.unchecked(1, 2) == 6``.
*   See also: :cpp:func:`xt::xcontainer::unchecked`.

Returns a (constant) reference to the element,
specified by an *array index* given by a number of unsigned integers.
Different than :cpp:func:`~xt::xcontainer::operator()` there are no bounds checks (even when assertions)
are turned on, and the number of indices is assumed to match the dimension of the array.
:cpp:func:`~xt::xcontainer::unchecked` is thus aimed at performance.

.. note::

    If you assume responsibility for bounds-checking, this operator can be used to virtually
    post-pad zeros if you specify less indices than the rank of the array.
    Example: ``a.unchecked(1) == a(1, 0)``.

:cpp:func:`periodic(args...) <xt::xcontainer::periodic>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*   Example: ``a.periodic(-1, -2) == 7``.
*   See also: :cpp:func:`xt::xcontainer::periodic`.

Returns a (constant) reference to the element,
specified by an *array index* given by a number of signed integers.
Negative and 'overflowing' indices are changed by assuming periodicity along that axis.
For example, for the first axis: ``-1 -> a.shape(0) - 1 = 2``,
likewise for example ``3 -> 3 - a.shape(0) = 0``.
Of course this comes as the cost of some extra complexity.

:cpp:func:`in_bounds(args...) <xt::xcontainer::in_bounds>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*   Example: ``a.in_bounds(1, 2) == true``.
*   See also: :cpp:func:`xt::xcontainer::in_bounds`.

Check if the *array index* is 'in bounds', return ``false`` otherwise.

:cpp:func:`operator[]({...}) <xt::xcontainer::operator[]>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*   Example: ``a[{1, 2}] == 6``.
*   See also: :cpp:func:`xt::xcontainer::operator[]`.

Returns a (constant) reference to the element,
specified by an *array index* given by a list of unsigned integers.

Operators: flat index
---------------------

:cpp:func:`flat(i) <xt::xcontainer::flat>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*   Example: ``a.flat(6) == 6``.
*   See also: :cpp:func:`xt::xcontainer::flat`.

Returns a (constant) reference to the element specified by a *flat index*,
given an unsigned integer.

.. note::

    If the layout would not have been the default *row major*,
    but *column major*, then ``a.flat(6) == 2``.

.. note::

    In many cases ``a.flat(i) == a.data()[i]``.

Array indices
-------------

Functions like :cpp:func:`xt::argwhere(a \< 5) <xt::argwhere>` return a ``std::vector`` of *array indices*.
Using the same matrix as above, we can do

.. code-block:: cpp

    int main()
    {
        xt::xarray<size_t> a = xt::arange<size_t>(3 * 4);

        a.reshape({3,4});

        auto idx = xt::from_indices(xt::argwhere(a >= 6));

        std::cout << idx << std::endl;
    }

which prints

.. code-block:: none

    {{1, 2},
     {1, 3},
     {2, 0},
     {2, 1},
     {2, 2},
     {2, 3}}

To print the ``std::vector``, it is converted to a :cpp:type:`xt::xtensor\<size_t, 2\> <xt::xtensor>`
array, which is done using :cpp:func:`xt::from_indices`.

From array indices to flat indices
----------------------------------

To convert the array indices to a :cpp:type:`xt::xtensor\<size_t, 1\> <xt::xtensor>` of flat indices,
:cpp:func:`xt::ravel_indices` can be used.
For the same example:

.. code-block:: cpp

    #include <xtensor/xtensor.hpp>
    #include <xtensor/xarray.hpp>
    #include <xtensor/xio.hpp>

    int main()
    {
        xt::xarray<size_t> a = xt::arange<size_t>(3 * 4);

        a.reshape({3,4});

        auto idx = xt::ravel_indices(xt::argwhere(a >= 6), a.shape());

        std::cout << idx << std::endl;
    }

which prints

.. code-block:: none

    { 6,  7,  8,  9, 10, 11}

.. note::

    To convert to a ``std::vector`` use

    .. code-block:: cpp

        auto idx = xt::ravel_indices<xt::ravel_vector_tag>(xt::argwhere(a >= 6), a.shape());

1-D arrays: array indices == flat indices
-----------------------------------------

For 1-D arrays the array indices and flat indices coincide.
One can use the generic functions :cpp:func:`xt::flatten_indices` to get a
:cpp:type:`xt::xtensor\<size_t, 1\> <xt::xtensor>` of (array/flat) indices.
For example:

.. code-block:: cpp

    #include <xtensor/xtensor.hpp>
    #include <xtensor/xview.hpp>
    #include <xtensor/xio.hpp>

    int main()
    {
        xt::xtensor<size_t, 1> a = xt::arange<size_t>(16);

        auto idx = xt::flatten_indices(xt::argwhere(a >= 6));

        std::cout << idx << std::endl;

        std::cout << xt::view(a, xt::keep(idx)) << std::endl;
    }

which prints the indices and the selection (which are in this case identical):

.. code-block:: none

    { 6,  7,  8,  9, 10, 11, 12, 13, 14, 15}
    { 6,  7,  8,  9, 10, 11, 12, 13, 14, 15}

From flat indices to array indices
----------------------------------

To convert *flat indices* to *array_indices* the function :cpp:func:`xt::unravel_indices` can be used.
For example

.. code-block:: cpp

    #include <xtensor/xarray.hpp>
    #include <xtensor/xtensor.hpp>
    #include <xtensor/xstrides.hpp>
    #include <xtensor/xio.hpp>

    int main()
    {
        xt::xarray<size_t> a = xt::arange<size_t>(3 * 4);

        a.reshape({3,4});

        auto flat_indices = xt::ravel_indices(xt::argwhere(a >= 6), a.shape());

        auto array_indices = xt::from_indices(xt::unravel_indices(flat_indices, a.shape()));

        std::cout << "flat_indices = " << std::endl << flat_indices << std::endl;
        std::cout << "array_indices = " << std::endl << array_indices << std::endl;
    }

which prints

.. code-block:: none

    flat_indices =
    { 6,  7,  8,  9, 10, 11}
    array_indices =
    {{1, 2},
     {1, 3},
     {2, 0},
     {2, 1},
     {2, 2},
     {2, 3}}

Notice that once again the function :cpp:func:`xt::from_indices` has been used to convert a
``std::vector`` of indices to a :cpp:type:`xt::xtensor` array for printing.