Builders

Most of xtensor builders return unevaluated expressions (see Expressions and lazy evaluation for more details) that can be assigned to any kind of xtensor container.

Ones

// Lazy version
auto e = xt::ones<double>({2, 3});
std::cout << e < std::endl;
// Outputs {{1., 1., 1.}, {1., 1., 1.}}

// Evaluated versions
using fixed_tensor = xt::xtensor_fixed<double, xt::xshape<2, 3>>;
xt::xarray<double>     a0 = xt::ones<double>({2, 3});
xt::xtensor<double, 2> a1 = xt::ones<double>({2, 3});
fixed_tensor           a2 = xt::ones<double>({2, 3});

Zeros

// Lazy version
auto e = xt::zeros<double>({2, 3});
std::cout << e << std::endl;
// Outputs {{0., 0., 0.}, {0., 0., 0.}}

// Evaluated versions
using fixed_tensor = xt::xtensor_fixed<double, xt::xshape<2, 3>>;
xt::xarray<double>     a0 = xt::zeros<double>({2, 3});
xt::xtensor<double, 2> a1 = xt::zeros<double>({2, 3});
fixed_tensor           a2 = xt::zeros<double>({2, 3});

Empty

xt::empty creates a container of uninitialized values. It selects the best container match from the supplied shape:

xt::xarray<double>::shape_type sh0 = {2, 3};
auto a0 = xt::empty<double>(sh0);
// a0 is xt::xarray<double>

xt::xtensor<double, 2>::shape_type sh1 = {2, 3};
auto a1 = xt::empty<double>(sh1);
// a1 is xt::xtensor<double, 2>

xt::xshape<2, 3> sh2;
auto a2 = xt::empty<double>(sh2);
// a2 is xt::xtensor_fixed<double, xt::xshape<2, 3>>

Full like

xt::full_like returns a container with the same shape as the input expression, and filled with the specified value:

xt::xarray<double> a0 = {{1., 2., 3.}, {4., 5., 6.}};
auto b0 = xt::full_like(a0, 3.);
std::cout << b0 << std::endl;
// Outputs {{3., 3., 3.}, {3., 3., 3.}}
// b0 is an xt::xarray<double>

xt::xtensor<double, 2> a1 = {{1., 2., 3.}, {4., 5., 6.}};
auto b1 = xt::full_like(a1, 3.);
std::cout << b1 << std::endl;
// Outputs {{3., 3., 3.}, {3., 3., 3.}}
// b1 is an xt::xtensor<double, 2>

xt::xtensor_fixed<double, xt::xshape<2, 3>> a2 = {{1., 2., 3.}, {4., 5., 6.}};
auto b2 = xt::full_like(a2, 3.);
std::cout << b2 << std::endl;
// Outputs {{3., 3., 3.}, {3., 3., 3.}}
// b2 is an xt::xtensor_fixed<double, xt::xshape<2, 3>>

Ones like

ones_like(e) isquivalent to full_like(e, 1.).

Zeros like

zeros_like(e) isquivalent to full_like(e, 0.).

Eye

Generates an array with ones on the specified diagonal:

auto a = xt::eye<double>({2, 3}, 1);
std::cout << a << std::endl;
// Outputs {{O, 1, 0}, {0, 0, 1}}

auto b = xt::eye<double>({3, 2}, -1);
std::cout << b << std::endl;
// Outputs {{0, 0}, {1, 0}, {0, 1}}

aut c = xt::eye<double>(3, 1);
std::cout << c << std::endl;
// Outputs {{O, 1, 0}, {0, 0, 1}, {0, 0, 0}}

Arange

Generates numbers evenly spaced:

auto e = xt::arange<double>(0., 10., 2);
std::cout << e << std::endl;
// Outputs {0., 2., 4., 6., 8.}

A common pattern is to used arange followed by reshape to initialize tensor with arbitrary number of dimensions:

xt::xarray<double> a = xt::arange<double>(0., 6.).reshape({2, 3});
std::cout << a << std::endl;
// Outputs {{0., 1., 2.}, {3., 4., 5.}}

Linspace

auto a = xt::linspace<double>(0., 10., 5);
std::cout << a << std::endl;
// Ouputs {0., 2.5, 5., 7.5, 10.}

Logspace

Similar to linspace but numbers are evenly space on a log scale.

Concatenate

xt::xarray<double> a = {{1, 2, 3}};
xt::xarray<double> b = {{2, 3, 4}};

auto c0 = xt::concatenate(xt::xtuple(a, b));
std::cout << c0 << std::endl;
// Outputs {{1, 2, 3}, {2, 3, 4}}

auto c1 = xt::concatenate(xt::xtuple(a, b), 1);
std::cout << c1 << std::endl;
// Outputs {1, 2, 3, 2, 3, 4}

Stack

stack always create a new dimension along which elements are stacked:

xt::xarray<double> a = {1, 2, 3};
xt::xarray<double> b = {5, 6, 7};

auto s0 = xt::stack(xt::xtuple(a, b));
std::cout << s0 << std::endl;
// Outputs {{1, 2, 3}, {5, 6, 7}}

auto s1 = xt::stack(xt::xtuple(a, b), 1);
std::cout << s1 << std::endl;
// Outputs {{1, 5}, {2, 6}, {3, 7}}

Diag

Returns a 2D-expression using the input value as its diagonal:

xt::xarray<double> a = {1, 5, 7};
auto b = xt::diag(a);
std::cout << b << std::endl;
// Outputs {{1, 0, 0} {0, 5, 0}, {5, 0, 7}}

Diagonal

Returns the elements on the diagonal of the expression

xt::xarray<double> a = {{1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9}};
auto d = xt::diagonal(a);
std::cout << d << std::endl;
// Outputs {1, 5, 9}