We can operate on arrays of different shapes of dimensions in an 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 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:
(2, 3) # A
(4, 2, 1) # B
(4, 2, 3) # Result
Universal functions, Laziness and Vectorization
With xtensor, if x, y and z are arrays of broadcastable shapes, the return type of an expression such as x + y * sin(z) is not an array. It is an xexpression object offering the same interface as an N-dimensional array, which does not hold the result. Values are only computed upon access or when the expression is assigned to an xarray object. This allows to operate symbolically on very large arrays and only compute the result for the indices of interest.
We provide utilities to vectorize any scalar function (taking multiple scalar arguments) into a function that will perform on xexpressions, applying the lazy broadcasting rules which we just described. These functions are called xfunctions. They are xtensor's counterpart to numpy's universal functions.
In xtensor, arithmetic operations (+, -, *, /) and all special functions are xfunctions.
Iterating over xexpressions and Broadcasting Iterators
All xexpressions offer two sets of functions to retrieve iterator pairs (and their const counterpart).
begin() and end() provide instances of xiterators which can be used to iterate over all the elements of the expression. The order in which elements are listed is row-major in that the index of last dimension is incremented first.
xbegin(shape) and xend(shape) are similar but take a broadcasting shape as an argument. Elements are iterated upon in a row-major way, but 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.
Fixed-dimension and Dynamic dimension
Two container classes implementing multi-dimensional arrays are provided: xarray and xtensor.
xarray can be reshaped dynamically to any number of dimensions. It is the container that is the most similar to numpy arrays.
xtensor has a dimension set at compilation time, which enables many optimizations. For example, shapes and strides of xtensor instances are allocated on the stack instead of the heap.
xarray and xtensor container are both xexpressions and can be involved and mixed in universal functions, assigned to each other etc...
Besides, two access operators are provided:
The variadic template operator() which can take multiple integral arguments or none.
And the operator which takes a single multi-index argument, which can be of size determined at runtime. operator also supports access with braced initializers.
The python bindings are built upon the pybind11 library, a lightweight header-only for creating bindings between the Python and C++ programming languages.
Example 1: Use an algorithm of the C++ library on a numpy array inplace.
#include"xtensor/xmath.hpp"// C++ universal functions
#include"xtensor-python/pyarray.hpp"// numpy bindingsdoublesum_of_sines(xt::pyarray<double> &m)
auto sines = xt::sin(m); // sines does not hold any valuereturnstd::accumulate(sines.begin(), sines.end(), 0.0);
"Test module for xtensor python bindings");
"Return the sum of the sines");
import numpy as np
import xtensor_python_test as xt
a = np.arange(15).reshape(3, 5)
s = xt.sum_of_sines(v)
Example 2: Create a universal function from a C++ scalar function