A C++ array expression library.
xtensor is a C++ library meant for numerical analysis with multi-dimensional array expressions.
xtensor provides
xtensor.The implementation of the containers of xtensor is inspired by NumPy, the Python array programming library. Adaptors for existing data structures to be plugged into our expression system can easily be written. In fact, xtensor can be used to process numpy data structures inplace using Python's buffer protocol.
xtensor requires a modern C++ compiler supporting C++14. The following C+ compilers are supported:
In [1]:
#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
In [2]:
xt::xarray<double> arr1
{{1.0, 2.0, 3.0},
{2.0, 5.0, 7.0},
{2.0, 5.0, 7.0}};
xt::xarray<double> arr2
{5.0, 6.0, 7.0};
std::cout << xt::view(arr1, 1) + arr2;;
In [3]:
#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
In [4]:
xt::xarray<int> arr
{1, 2, 3, 4, 5, 6, 7, 8, 9};
arr.reshape({3, 3});
std::cout << arr;
In [5]:
#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xmath.hpp"
#include "xtensor/xio.hpp"
In [6]:
xt::xarray<double> arr3
{1.0, 2.0, 3.0};
xt::xarray<unsigned int> arr4
{4, 5, 6, 7};
arr4.reshape({4, 1});
std::cout << xt::pow(arr3, arr4);;
In [7]:
#include <iostream>
#include "xtensor/xrandom.hpp"
In [8]:
xt::xarray<double> arr5 = xt::random::randn<double>({4, 3});
std::cout << arr5;
In [9]:
std::cout << xt::random::randn<double>({4, 3});
In [10]:
#include "xtensor/xbuilder.hpp"
In [11]:
xt::xarray<double> ar = xt::linspace<double>(0.0, 10.0, 12);
ar.reshape({4, 3});
std::cout << ar;
In [12]:
xt::xarray<double> fones = xt::ones<float>({2, 2});
std::cout << fones;
In [13]:
#include <vector>
#include "xtensor/xbroadcast.hpp"
In [14]:
std::cout << xt::broadcast(xt::linspace<double>(0.0, 10.0, 4),
std::vector<std::size_t>({3, 4}));
In [15]:
#include <algorithm>
In [16]:
xt::xarray<double> frand = xt::random::randn<double>({2, 2});
std::cout << frand << std::endl << std::endl;
// begin() and end() provide and iterator pair iterating over the xexpression in a row-major fashion
std::cout << std::accumulate(frand.begin(), frand.end(), 0.0);
In [17]:
// begin(shape) and end(shape) provide and iterator pair iterating
// over the xexpression broadcasted to the prescrived shape in a row-major fashion
std::vector<std::size_t> shape = {3, 2, 2};
std::cout << std::accumulate(frand.begin(shape), frand.end(shape), 0.0);
Blas bindings for xtensor
xtensor-blas is an extension to the xtensor library, offering bindings to BLAS and LAPACK libraries
through cxxblas and cxxlapack from the FLENS project.
xtensor-blas currently provides non-broadcasting dot, norm (1- and 2-norm for vectors), inverse, solve,
eig, cross, det, slogdet, matrix_rank, inv, cholesky, qr, svd in the xt::linalg namespace (check the corresponding xlinalg.hpp header for the function signatures). The functions, and signatures, are trying to be 1-to-1 equivalent to NumPy.
Low-level functions to interface with BLAS or LAPACK with xtensor containers are also offered
in the blas and lapack namespace.
xtensor and xtensor-blas require a modern C++ compiler supporting C++14. The following C++ compilers are supported:
In [18]:
#include "xtensor-blas/xlinalg.hpp"
In [19]:
xt::xtensor<double, 2> m = {{1.5, 0.5}, {0.7, 1.0}};
In [20]:
std::cout << "Matrix rank: " << std::endl << xt::linalg::matrix_rank(m);
In [21]:
std::cout << "Matrix inverse: " << std::endl << xt::linalg::inv(m);
In [22]:
std::cout << "Eigen values: " << std::endl << xt::linalg::eigvals(m);
In [23]:
xt::xarray<double> arg1 = xt::arange<double>(9);
xt::xarray<double> arg2 = xt::arange<double>(18);
arg1.reshape({3, 3});
arg2.reshape({2, 3, 3});
std::cout << xt::linalg::dot(arg1, arg2) << std::endl;
In [ ]: