File: builder.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 (246 lines) | stat: -rw-r--r-- 6,111 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
.. 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.

Builders
========

Most of *xtensor* builders return unevaluated expressions (see :ref:`lazy-evaluation`
for more details) that can be assigned to any kind of *xtensor* container.

Ones
----

.. code::

    // Lazy version
    auto e = xt::ones<double>({2, 3});
    std::cout << e < std::endl;
    // Outputs {{1., 1., 1.}, {1., 1., 1.}}

    // Evaluated versions
    using fixed_tensor = xt::xtensor_fixed<double, xt::xshape<2, 3>>;
    xt::xarray<double>     a0 = xt::ones<double>({2, 3});
    xt::xtensor<double, 2> a1 = xt::ones<double>({2, 3});
    fixed_tensor           a2 = xt::ones<double>({2, 3});

Zeros
-----

.. code::

    // Lazy version
    auto e = xt::zeros<double>({2, 3});
    std::cout << e << std::endl;
    // Outputs {{0., 0., 0.}, {0., 0., 0.}}

    // Evaluated versions
    using fixed_tensor = xt::xtensor_fixed<double, xt::xshape<2, 3>>;
    xt::xarray<double>     a0 = xt::zeros<double>({2, 3});
    xt::xtensor<double, 2> a1 = xt::zeros<double>({2, 3});
    fixed_tensor           a2 = xt::zeros<double>({2, 3});

Empty
-----

``xt::empty`` creates a container of uninitialized values. It selects the best container
match from the supplied shape:

.. code::

    xt::xarray<double>::shape_type sh0 = {2, 3};
    auto a0 = xt::empty<double>(sh0);
    // a0 is xt::xarray<double>

    xt::xtensor<double, 2>::shape_type sh1 = {2, 3};
    auto a1 = xt::empty<double>(sh1);
    // a1 is xt::xtensor<double, 2>

    xt::xshape<2, 3> sh2;
    auto a2 = xt::empty<double>(sh2);
    // a2 is xt::xtensor_fixed<double, xt::xshape<2, 3>>

Full like
---------

``xt::full_like`` returns a container with the same shape as the input expression, and
filled with the specified value:

.. code::

    xt::xarray<double> a0 = {{1., 2., 3.}, {4., 5., 6.}};
    auto b0 = xt::full_like(a0, 3.);
    std::cout << b0 << std::endl;
    // Outputs {{3., 3., 3.}, {3., 3., 3.}}
    // b0 is an xt::xarray<double>

    xt::xtensor<double, 2> a1 = {{1., 2., 3.}, {4., 5., 6.}};
    auto b1 = xt::full_like(a1, 3.);
    std::cout << b1 << std::endl;
    // Outputs {{3., 3., 3.}, {3., 3., 3.}}
    // b1 is an xt::xtensor<double, 2>

    xt::xtensor_fixed<double, xt::xshape<2, 3>> a2 = {{1., 2., 3.}, {4., 5., 6.}};
    auto b2 = xt::full_like(a2, 3.);
    std::cout << b2 << std::endl;
    // Outputs {{3., 3., 3.}, {3., 3., 3.}}
    // b2 is an xt::xtensor_fixed<double, xt::xshape<2, 3>>

Ones like
---------

``ones_like(e)`` is equivalent to ``full_like(e, 1.)``.

Zeros like
----------

``zeros_like(e)`` is equivalent to ``full_like(e, 0.)``.

Eye
---

Generates an array with ones on the specified diagonal:

.. code::

    auto a = xt::eye<double>({2, 3}, 1);
    std::cout << a << std::endl;
    // Outputs {{O, 1, 0}, {0, 0, 1}}

    auto b = xt::eye<double>({3, 2}, -1);
    std::cout << b << std::endl;
    // Outputs {{0, 0}, {1, 0}, {0, 1}}

    aut c = xt::eye<double>(3, 1);
    std::cout << c << std::endl;
    // Outputs {{O, 1, 0}, {0, 0, 1}, {0, 0, 0}}

Arange
------

Generates evenly spaced numbers:

.. code::

    auto e = xt::arange<double>(0., 10., 2);
    std::cout << e << std::endl;
    // Outputs {0., 2., 4., 6., 8.}

A common pattern is to use ``arange`` followed by reshape to initialize
a tensor with an arbitrary number of dimensions:

.. code::

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

Linspace
--------

.. code::

    auto a = xt::linspace<double>(0., 10., 5);
    std::cout << a << std::endl;
    // Outputs {0., 2.5, 5., 7.5, 10.}

Logspace
--------

Similar to ``linspace`` but numbers are evenly space on a log scale.

Concatenate
-----------

.. code::

    xt::xarray<double> a = {{1, 2, 3}};
    xt::xarray<double> b = {{2, 3, 4}};

    auto c0 = xt::concatenate(xt::xtuple(a, b));
    std::cout << c0 << std::endl;
    // Outputs {{1, 2, 3}, {2, 3, 4}}

    auto c1 = xt::concatenate(xt::xtuple(a, b), 1);
    std::cout << c1 << std::endl;
    // Outputs {1, 2, 3, 2, 3, 4}

Stack
-----

``stack`` always creates a new dimension along which elements are stacked:

.. code::

    xt::xarray<double> a = {1, 2, 3};
    xt::xarray<double> b = {5, 6, 7};

    auto s0 = xt::stack(xt::xtuple(a, b));
    std::cout << s0 << std::endl;
    // Outputs {{1, 2, 3}, {5, 6, 7}}

    auto s1 = xt::stack(xt::xtuple(a, b), 1);
    std::cout << s1 << std::endl;
    // Outputs {{1, 5}, {2, 6}, {3, 7}}

HStack
------

.. code::

    xt::xarray<double> a0 = {{1, 2, 3}, {4, 5, 6}};
    xt::xarray<double> b0 = {{7, 8}, {9, 10}};
    auto c0 = xt::hstack(xt::xtuple(a0, b0));
    std::cout << c0 << std:endl;
    // Outputs {{1, 2, 3, 7, 8}, {4, 5, 6, 0, 10}}

    xt::xarray<double> a1 = {1, 2, 3};
    xt::xarray<double> b1 = {2, 3 ,4};
    auto c1 = xt::hstack(xt::xtuple(a1, b1));
    std::cout << c1 << std::endl;
    // Outputs {1, 2, 3, 2, 3, 4}

VStack
------

.. code::

    xt::xarray<double> a0 = {1, 2, 3};
    xt::xarray<double> b0 = {2, 3, 4};
    auto c0 = xt::vstack(xt::xtuple(a0, b0));
    std::cout << c0 << std::endl;
    // Outputs {{1, 2, 3}, {2, 3 ,4}}

    xt::xarray<double> a1 = {{1, 2, 3}, {4, 5 ,6}, {7, 8, 9}};
    xt::xarray<double> b1 = {{10, 11, 12}};
    auto c1 = xt::vstack(xt::xtuple(a1, b1));
    std::cout << c1 << std::endl;
    // Outputs {{1, 2, 3}, {4, 5 ,6}, {7, 8, 9}, {10, 11, 12}}

Diag
----

Returns a 2D-expression using the input value as its diagonal:

.. code::

    xt::xarray<double> a = {1, 5, 7};
    auto b = xt::diag(a);
    std::cout << b << std::endl;
    // Outputs {{1, 0, 0} {0, 5, 0}, {0, 0, 7}}

Diagonal
--------

Returns the elements on the diagonal of the expression

.. code::

    xt::xarray<double> a = {{1, 2, 3},
                            {4, 5, 6},
                            {7, 8, 9}};
    auto d = xt::diagonal(a);
    std::cout << d << std::endl;
    // Outputs {1, 5, 9}