xtensor is more than an N-dimensional array library: it is an expression engine that allows numerical computation on any object implementing the expression interface.
These objects can be in-memory containers such as xarray<T> and xtensor<T>, but can also be backed by a database or a representation on the file system. This
also enables creating adaptors as expressions for other data structures.

Assume x, y and z are arrays of compatible shapes (we’ll come back to that later), the return type of an expression such as x+y*sin(z) is not an array.
The result is an xexpression which offers the same interface as an N-dimensional array but does not hold any value. Such expressions can be plugged into others to build
more complex expressions:

autof=x+y*sin(z);autof2=w+2*cos(f);

The expression engine avoids the evaluation of intermediate results and their storage in temporary arrays, so you can achieve the same performance as if you had written
a simple loop. Assuming x, y and z are one-dimensional arrays of length n,

An expression such as x+y*sin(z) does not hold the result. Values are only computed upon access or when the expression is assigned to a container. This
allows to operate symbolically on very large arrays and only compute the result for the indices of interest:

// Assume x and y are xarrays each containing 1 000 000 objectsautof=cos(x)+sin(y);doublefirst_res=f(1200);doublesecond_res=f(2500);// Only two values have been computed

That means if you use the same expression in two assign statements, the computation of the expression will be done twice. Depending on the complexity of the computation
and the size of the data, it might be convenient to store the result of the expression in a temporary variable:

// Assume x and y are small arraysxt::xarray<double>tmp=cos(x)+sin(y);xt::xarray<double>res1=tmp+2*x;xt::xarray<double>res2=tmp-2*x;

If you have to force the evaluation of an xexpression for some reason (for example, you want to have all results in memory to perform a sort, or use external BLAS functions) then you can use xt::eval on an xexpression.
Evaluating will either return an rvalue to a newly allocated container in the case of a xexpression, or a reference to a container in case you are evaluating a xarray or xtensor. Note that, in order to avoid copies, you should use an universal reference on the lefthand side (auto&&). For example:

xt::xarray<double>a={1,2,3};xt::xarray<double>b={3,2,1};autocalc=a+b;// unevaluated xexpression!auto&&e=xt::eval(calc);// a rvalue container xarray!// this just returns a reference to the existing containerauto&&a_ref=xt::eval(a);

The number of dimensions of an xexpression and the sizes of these dimensions are provided by the shape() method, which returns a sequence of unsigned integers
specifying the size of each dimension. We can operate on expressions of different shapes of dimensions in a elementwise fashion. Broadcasting rules of xtensor are
similar to those of Numpy and libdynd.

In an operation involving two arrays of different dimensions, the array with the lesser dimensions is broadcast across the leading dimensions of the other.
For example, if A has shape (2,3), and B has shape (4,2,3), the result of a broadcasted operation with A and B has shape (4,2,3).

(2, 3) # A
(4, 2, 3) # B
---------
(4, 2, 3) # Result

The same rule holds for scalars, which are handled as 0-D expressions. If A is a scalar, the equation becomes:

() # A
(4, 2, 3) # B
---------
(4, 2, 3) # Result

If matched up dimensions of two input arrays are different, and one of them has size 1, it is broadcast to match the size of the other. Let’s say B has the shape (4,2,1)
in the previous example, so the broadcasting happens as follows:

operator() is an access operator which can take multiple integral arguments of none.

operator[] has two overloads: one that takes a single integral argument and is equivalent to the call of operator() with one argument, and one with a single multi-index argument, which can be of size determined at runtime. This operator also supports braced initializer arguments.

element() is an access operator which takes a pair of iterators on a container of indices.

begin() and end() return instances of xiterator which can be used to iterate over all the elements of the expression. The layout of the iteration can be specified
through the layout_type template parameter, accepted values are layout_type::row_major and layout_type::column_major. If not specified, DEFAULT_LAYOUT is used.
This iterator pair permits to use algorithms of the STL with xexpression as if they were simple containers.

begin(shape) and end(shape) are similar but take a broadcasting shape as an argument. Elements are iterated upon in DEFAULT_LAYOUT if no layout_type template
parameter is specified. Certain dimensions are repeated to match the provided shape as per the rules described above. For an expression e, e.xbegin(e.shape()) and e.begin()
are equivalent.

rbegin() and rend() return instances of xiterator which can be used to iterate over all the elements of the reversed expression. As begin() and end(), the
layout of the iteration can be specified through the layout_type parameter.

rbegin(shape) and rend(shape) are the reversed counterpart of begin(shape) and end(shape).