File: operator.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 (86 lines) | stat: -rw-r--r-- 2,204 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
.. 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.

Operators
=========

Operations and functions of *xtensor* are not evaluated until they are assigned.
In the following, ``e1``, ``e2`` and ``e3`` can be arbitrary tensor expressions.
The results of operations and functions are assigned to :cpp:type:`xt::xarray` in the examples,
but that could be any other container (or even views). To keep an unevaluated
operator / function, assign to an ``auto`` variable:

.. code::

    auto res = e1 + e2;

See :ref:`lazy-evaluation` for more details on unevaluated expressions.

Arithmetic operators
--------------------

.. code::

    xt::xarray<double> res0 = -e1;
    xt::xarray<double> res1 = e1 + e2;
    xt::xarray<double> res2 = e1 - e2;
    xt::xarray<double> res3 = e1 * e2;
    xt::xarray<double> res4 = e1 / e2;
    xt::xarray<double> res5 = e1 % e2;

    res1 += e2;
    res2 -= e2;
    res3 *= e2;
    res4 /= e2;
    res5 %= e2;

Bitwise operators
-----------------

.. code::

    xt::xarray<double> res0 = e1 & e2;
    xt::xarray<double> res1 = e1 | e2;
    xt::xarray<double> res2 = e1 ^ e2;
    xt::xarray<double> res3 = ~e1;

    res0 &= e2;
    res1 |= e2;

Logical operators
-----------------

.. code::

    xt::xarray<double> res0 = e1 && e2;
    xt::xarray<double> res1 = e1 || e2;
    xt::xarray<double> res2 = !e1;
    bool res3 = any(e1);
    bool res4 = all(e1);
    xt::xarray<double> res5 = where(e1, e2, e3);

Comparison operators
--------------------

Comparison operators return expressions performing element-wise
comparison:

.. code::

    xt::xarray<double> res0 = e1 < e2;
    xt::xarray<double> res1 = e1 > e2;
    xt::xarray<double> res2 = e1 <= e2;
    xt::xarray<double> res3 = e1 >= e2;
    xt::xarray<double> res4 = xt::equal(e1, e2);
    xt::xarray<double> res5 = xt::not_equal(e1, e2);

Except for equality and inequality operators which performs traditional
comparison and return a boolean:

.. code::

    bool res0 = e1 == e2; // true if all elements in e1 equal those in e2
    bool res1 = e1 != e2;