File: basic.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 (247 lines) | stat: -rw-r--r-- 6,173 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
.. 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.

Basics
======

Tensor types
------------

- ``xarray<T>``: tensor that can be reshaped to any number of dimensions.
- ``xtensor<T, N>``: tensor with a number of dimensions set to ``N`` at compile time.
- ``xtensor_fixed<T, xshape<I, J, K>``: tensor whose shape is fixed at compile time.
- ``xchunked_array<CS>``: chunked array using the ``CS`` chunk storage.

.. note::

   Except if mentioned otherwise, the methods described below are available for the
   three kinds of containers, even if the examples show :cpp:type:`xt::xarray` usage only.

Initialization
--------------

Tensor with dynamic shape:

.. code::

    #include <xtensor/xarray.hpp>

    xt::xarray<double>::shape_type shape = {2, 3};
    xt::xarray<double> a0(shape);
    xt::xarray<double> a1(shape, 2.5);
    xt::xarray<double> a2 = {{1., 2., 3.}, {4., 5., 6.}};
    auto a3 = xt::xarray<double>::from_shape(shape);

Tensor with static number of dimensions:

.. code::

    #include <xtensor/xtensor.hpp>

    xt::xtensor<double, 2>::shape_type shape = {2, 3};
    xt::xtensor<double, 2> a0(shape);
    xt::xtensor<double, 2> a1(shape, 2.5);
    xt::xtensor<double, 2> a2 = {{1., 2., 3.}, {4., 5., 6.}};
    auto a3 = xt::xtensor<double, 2>::from_shape(shape);

Tensor with fixed shape:

.. code::

    #include <xtensor/xfixed.hpp>

    xt::xtensor_fixed<double, xt::xshape<2, 3>> = {{1., 2., 3.}, {4., 5., 6.}};

In-memory chunked tensor with dynamic shape:

.. code::

    #include <xtensor/xchunked_array.hpp>

    std::vector<std::size_t> shape = {10, 10, 10};
    std::vector<std::size_t> chunk_shape = {2, 3, 4};
    auto a = xt::chunked_array<double>(shape, chunk_shape);

Output
------

.. code::

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

    xt::xarray<double> a = {{1., 2.}, {3., 4.}};
    std::cout << a << std::endl;

    xt::xtensor<double, 2> b = {{1., 2.}, {3., 4.}};
    std::cout << b << std::endl;

    xt::xtensor_fixed<double, xt::xshape<2, 2>> c = {{1., 2.}, {3., 4.}};
    std::cout << c << std::endl;

Shape - dimension - size
------------------------

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    auto size = a.size();     // size = 6
    auto dim = a.dimension(); // dim = 2
    auto shape = a.shape();   // shape = {2, 3}
    auto sh1 = a.shape(1);    // sh1 = 3

Print the shape
---------------

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    auto shape = a.shape();
    std::cout << xt::adapt(shape) << std::endl;

Reshape
-------

The number of elements of an :cpp:type:`xt::xarray` must remain the same:

.. code::

    xt::xarray<double> a0 = {1., 2., 3., 4., 5., 6.};
    a0.reshape({2, 3});
    std::cout << a0 << std::endl;
    // outputs {{1., 2., 3.}, {4., 5., 6. }}

For :cpp:type:`xt::xtensor` the number of elements and the number of dimensions
must remain the same:

.. code::

    xt::xtensor<double, 2> a1 = {{1., 2.}, {3., 4.}, {5., 6.}};
    a1.reshape({2, 3});
    std::cout << a1 << std::endl;
    // outputs {{1., 2., 3.}, {4., 5., 6. }}

One value in the shape can be -1. In this case, the value is inferred from the
length of the underlying buffer and remaining dimensions:

.. code::

    xt::xarray<double> a0 = {1., 2., 3., 4., 5., 6.};
    a0.reshape({2, -1});
    std::cout << a0 << std::endl;
    // outputs {{1., 2., 3.}, {4., 5., 6. }}

    xt::xtensor<double, 2> a1 = {{1., 2.}, {3., 4.}, {5., 6.}};
    a1.reshape({-1, 3});
    std::cout << a1 << std::endl;
    // outputs {{1., 2., 3.}, {4., 5., 6. }}

``reshape`` is not defined for ``xtensor_fixed``.

Resize
------

.. code::

    xt::xarray<double> a0 = {1., 2., 3, 4.};
    a0.resize({2, 3});

When resizing an :cpp:type:`xt::xtensor` object, the number of dimensions must remain
the same:

.. code::

    xt::xtensor<double, 2> a1 = {{1., 2.}, {3., 4.}};
    a1.resize({2, 3});

``resize`` is not defined for ``xtensor_fixed``.

.. warning::

    Contrary to STL containers like std::vector, resize do NOT
    preserve elements.

Element access
--------------

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    double d0 = a(0, 2);   // d0 is 3.
    double d1 = a(2);      // d1 is a(0, 2)
    double d2 = a[{0, 2}]; // d2 is a(0, 2)

The same operators are used for writing values:

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    a(0, 2)   = 8.;
    a(2)      = 8.;
    a[{0, 2}] = 8.;

The ``at`` method is an access operator with bound checking:

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    double d0 = a.at(0, 3);   // throws
    double d1 = a.at(3);      // throws

The ``periodic`` method is an access operator that applies periodicity
to its arguments:

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    double d0 = a.periodic(2, -1); // d0 is 3

Fill
----

.. code::

    auto a = xt::xarray<double>::from_shape({2, 3});
    a.fill(2.);
    std::cout << a << std::endl;
    // Outputs {{2., 2., 2.}, {2., 2., 2.}}

Iterators
---------

*xtensor* containers provide iterators compatible with algorithms from the STL:

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    xt::xarray<double> b(a.shape());
    std::transform(a.cbegin(), a.cend(), b.begin(), [](auto&& v) { return v + 1; });
    std::cout << b << std::endl;
    // Outputs {{2., 3., 4.}, {5., 6., 7.}}

Reverse iterators are also available:

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    xt::xarray<double> b(a.shape());
    std::copy(a.crbegin(), a.crend(), b.begin());
    std::cout << b << std::endl;
    // Outputs {{6., 5., 4.}, {3., 2., 1.}}

Data buffer
-----------

The underlying 1D data buffer can be accessed with the ``data`` method:

.. code::

    xt::xarray<double> a = {{1., 2., 3.}, {4., 5., 6.}};
    a.data()[4] = 8.;
    std::cout << a << std::endl;
    // Outputs {{1., 2., 3.}, {8., 5., 6.}}