From NumPy to xtensor
Containers
Two container types are provided. xt::xarray
(dynamic number of dimensions)
and xt::xtensor
(static number of dimensions).
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Initializers
Lazy helper functions return tensor expressions. Return types don’t hold any value and are evaluated upon access or assignment. They can be assigned to a container or directly used in expressions.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
xtensor’s meshgrid
implementation corresponds to numpy’s 'ij'
indexing order.
Slicing and indexing
See numpy indexing
page.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Broadcasting
xtensor offers lazy numpy-style broadcasting, and universal functions. Unlike numpy, no copy or temporary variables are created.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
|
|
|
Random
The random module provides simple ways to create random tensor expressions, lazily.
See numpy.random
and xtensor random page.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Concatenation, splitting, squeezing
Concatenating expressions does not allocate memory, it returns a tensor or view expression holding closures on the specified arguments.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Rearrange elements
In the same spirit as concatenation, the following operations do not allocate any memory and do not modify the underlying xexpression.
Python3 - NumPy |
C++14 - xtensor |
---|---|
Iteration
xtensor follows the idioms of the C++ STL providing iterator pairs to iterate on arrays in different fashions.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
Iterating over |
a.begin({3, 4}) a.end({3, 4}) |
Iterating over |
a.begin<xt::layout_type::row_major>() a.begin<xt::layout_type::row_major>() |
Iterating over |
a.begin<xt::layout_type::column_major>() a.end<xt::layout_type::column_major>() |
Logical
Logical universal functions are truly lazy.
xt::where(condition, a, b)
does not evaluate a
where condition
is falsy, and it does not evaluate b
where condition
is truthy.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
|
|
|
|
|
|
|
|
|
Indices
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Comparisons
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
a < b |
|
a <= b |
|
a > b |
|
a >= b |
|
|
Minimum, Maximum, Sorting
Python3 - NumPy |
C++14 - xtensor |
---|---|
|
|
Complex numbers
Functions xt::real()
and xt::imag()
respectively return views on the real and imaginary part
of a complex expression.
The returned value is an expression holding a closure on the passed argument.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
The constness and value category (rvalue / lvalue) of
xt::real(a)
is the same as that ofa
. Hence, ifa
is a non-const lvalue,real(a)
is an non-const lvalue reference, to which one can assign a real expression.If
a
has complex values, the same holds forxt::imag(a)
. The constness and value category ofxt::imag(a)
is the same as that ofa
.If
a
has real values,xt::imag(a)
returnsxt::zeros(a.shape())
.
Reducers
Reducers accumulate values of tensor expressions along specified axes. When no axis is specified, values are accumulated along all axes. Reducers are lazy, meaning that returned expressions don’t hold any values and are computed upon access or assignment.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
|
|
|
|
|
More generally, one can use the xt::reduce(function, input, axes)
which allows the specification
of an arbitrary binary function for the reduction.
The binary function must be commutative and associative up to rounding errors.
NaN functions
NaN functions allow disregarding NaNs during computation, changing the effective number of elements considered in reductions.
Python3 - NumPy |
C++14 - xtensor |
---|---|
I/O
Print options
These options determine the way floating point numbers, tensors and other xtensor expressions are displayed.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
Reading npy, csv file formats
Functions xt::load_csv()
and xt::dump_csv()
respectively take input and output streams as arguments.
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Mathematical functions
xtensor universal functions are provided for a large set number of mathematical functions.
Basic functions:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
|
|
|
|
|
|
|
Exponential functions:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Power functions:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Trigonometric functions:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Hyperbolic functions:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Error and gamma functions:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Classification functions:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
Histogram:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
|
See Histogram.
Numerical constants:
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
Linear algebra
Many functions found in the numpy.linalg
module are implemented in xtensor-blas, a separate package offering BLAS and LAPACK bindings,
as well as a convenient interface replicating the linalg
module.
Please note, however, that while we’re trying to be as close to NumPy as possible, some features are not
implemented yet. Most prominently that is broadcasting for all functions except for xt::linalg::dot()
.
Matrix, vector and tensor products
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Decompositions
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Matrix eigenvalues
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
Norms and other numbers
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|
|
|
Solving equations and inverting matrices
Python 3 - NumPy |
C++ 14 - xtensor |
---|---|