In [2]:
#include <iostream>
std::cout << "Hello EuroScipy!" << std::endl;
In [ ]:
#include <xtensor/xio.hpp>
#include <xtensor/xarray.hpp>
xt::xarray<double> a = {{1,2,3}, {4,5,6}};
std::cout << a << std::endl;
In [ ]:
for (auto& el : a)
{
std::cout << el << std::endl;
}
In [ ]:
a(0, 1) = 10;
std::cout << a << std::endl;
In [ ]:
std::sort(a.begin(), a.end());
std::cout << a << std::endl;
In [ ]:
std::cout << xt::transpose(a) << std::endl;
In [ ]:
a.reshape({1, 1, 6, 1});
std::cout << a << std::endl;
In [ ]:
a.reshape({2, 3});
In [ ]:
auto xfunc = sin(a) * 2;
std::cout << xfunc << std::endl;
In [ ]:
template <class T>
void print_type(T) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
In [ ]:
print_type(xfunc);
In [ ]:
auto xfunc_el = xfunc(0, 1);
std::cout << xfunc_el << std::endl;
In [ ]:
xt::xarray<double> xfunc_result = xfunc;
std::cout << xfunc_result << std::endl;
In [ ]:
std::cout << a << std::endl;
In [ ]:
xt::xarray<double> brcast = {-10, +1, -10};
std::cout << brcast << std::endl;
In [ ]:
std::cout << a * brcast << std::endl;
In [ ]:
xt::xarray<double, xt::layout_type::column_major> a_cm = {{1,2,3}, {4,5,6}};
std::cout << a_cm << std::endl;
In [ ]:
// uses a std::array for shape & strides
#include <xtensor/xtensor.hpp>
xt::xtensor<double, 2> b = {{1, 2, 3}, {4, 5, 6}};
std::cout << b << std::endl;
In [ ]:
// uses a fixed size container for data (std::array)
// strides computed constexpr at compile time (no storage)
#include <xtensor/xfixed.hpp>
xt::xtensor_fixed<double, xt::xshape<2, 3>> c = {{1,2,3}, {4,5,6}};
std::cout << c << std::endl;
In [ ]:
#include <xtensor/xview.hpp>
#include <xtensor/xio.hpp>
using namespace xt::placeholders;
std::cout << a << std::endl;
In [ ]:
std::cout << xt::view(a, 1, xt::all()) << std::endl;
In [ ]:
// numpy equivalent: a[:, ::2]
std::cout << xt::view(a, xt::all(), xt::range(_, _, 2)) << std::endl;
In [ ]:
xt::view(a, xt::all(), xt::range(_, _, 2)) *= 100;
std::cout << a << std::endl;
In [ ]:
std::cout << a << std::endl;
In [ ]:
std::cout << xt::view(a, xt::keep(0, 0, 0) , xt::all()) << std::endl;
In [ ]:
#include <vector>
#include <xtensor/xadapt.hpp>
std::vector<double> vdata = {1, 2, 3, 4, 5, 6, 7, 8, 9};
auto adapted_vdata = xt::adapt(vdata, {3, 3});
In [ ]:
std::cout << adapted_vdata << std::endl;
In [ ]:
adapted_vdata(1, 2)
In [ ]:
#include <xtensor/xmath.hpp>
std::cout << sum(adapted_vdata) << std::endl;
In [ ]:
std::cout << sum(adapted_vdata, {1}) << std::endl;
double sum_of_sines(xt::pyarray<double>& m)
{
auto sines = xt::sin(m); // sines does not actually hold values.
return std::accumulate(sines.begin(), sines.end(), 0.0);
}
PYBIND11_MODULE(xtensor_python_test, m)
{
xt::import_numpy();
m.doc() = "Test module for xtensor python bindings";
m.def("sum_of_sines", sum_of_sines, "Sum the sines of the input values");
}
Check out https://github.com/QuantStack/xtensor
Follow on Twitter: