Multi-dimensional arrays with broadcasting and lazy computing.

In [1]:
#include <iostream>

#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.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};

xt::view(arr1, 1) + arr2


Out[2]:
  7.
 11.
 14.

In [3]:
arr1


Out[3]:
 1.
 2.
 3.
 2.
 5.
 7.
 2.
 5.
 7.

Initialize a 1-D array and reshape it inplace


In [4]:
#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"

In [5]:
xt::xarray<int> arr
  {1, 2, 3, 4, 5, 6, 7, 8, 9};

arr.reshape({3, 3});

arr


Out[5]:
1
2
3
4
5
6
7
8
9

Broadcasting the xt::pow universal functions


In [6]:
#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xmath.hpp"
#include "xtensor/xio.hpp"

In [7]:
xt::xarray<double> arr3
  {1.0, 2.0, 3.0};

xt::xarray<unsigned int> arr4
  {4, 5, 6, 7};

arr4.reshape({4, 1});

xt::pow(arr3, arr4)


Out[7]:
    1.
   16.
   81.
    1.
   32.
  243.
    1.
   64.
  729.
    1.
  128.
 2187.

Random arrays with the random module


In [8]:
#include <iostream>
#include "xtensor/xrandom.hpp"

In [9]:
xt::xarray<double> arr5 = xt::random::randn<double>({4, 3});
arr5


Out[9]:
 0.13453 
-0.146382
 0.46065 
-1.871384
 0.163712
-0.214253
 0.298595
-0.827944
 0.010215
 1.055466
-0.546841
 1.174568

In [10]:
xt::random::randn<double>({5, 3, 8, 10}) > 0.2


Out[10]:
 true
false
 true
false
false
false
false
false
false
 true
 true
false
 true
false
false
false
false
false
 true
false
 true
 true
 true
false
 true
false
 true
 true
false
 true
 true
false
 true
false
 true
false
false
false
 true
 true
 true
 true
 true
false
false
false
false
false
false
 true
false
false
false
 true
false
 true
 true
 true
false
false
 true
false
 true
 true
 true
 true
false
 true
false
 true
 true
 true
 true
false
 true
false
 true
false
false
false
false
 true
 true
false
 true
false
false
 true
false
false
 true
false
 true
false
false
false
 true
false
false
 true
false
 true
false
false
false
false
false
 true
false
 true
false
false
false
false
false
false
false
false
false
 true
 true
 true
false
false
 true
false
false
 true
false
false
false
 true
false
 true
 true
false
 true
false
false
false
false
false
false
 true
false
false
false
 true
false
false
false
 true
 true
false
 true
 true
false
 true
 true
 true
false
false
false
false
false
 true
false
 true
false
 true
false
 true
false
 true
false
false
false
false
false
 true
false
false
false
false
 true
false
 true
false
false
false
false
 true
false
false
false
 true
false
false
false
false
 true
false
false
false
 true
false
false
false
false
false
 true
false
 true
false
false
false
 true
false
 true
false
false
false
false
 true
false
 true
false
 true
 true
false
 true
false
false
false
false
false
 true
 true
false
false
 true
false
 true
 true
false
false
false
 true
false
 true
false
false
 true
false
 true
false
false
 true
false
 true
false
 true
 true
 true
false
false
false
 true
false
 true
 true
false
 true
 true
 true
false
false
 true
false
false
false
false
 true
 true
 true
 true
false
false
false
 true
false
false
 true
false
false
 true
false
 true
false
 true
 true
false
false
false
false
 true
false
false
 true
false
false
false
 true
 true
false
false
 true
false
false
false
 true
 true
false
 true
 true
false
false
false
false
false
 true
false
false
false
false
false
 true
false
 true
false
false
false
false
false
false
 true
false
false
 true
false
false
false
 true
false
false
false
 true
false
false
 true
 true
 true
 true
false
 true
 true
 true
 true
false
false
 true
 true
false
false
false
false
false
false
false
 true
 true
 true
false
 true
false
false
 true
 true
false
false
false
 true
false
false
false
false
false
 true
 true
 true
false
false
false
false
 true
 true
 true
false
 true
false
false
false
false
 true
 true
 true
false
 true
false
 true
false
 true
 true
false
 true
false
 true
 true
false
false
 true
false
false
false
 true
 true
 true
false
false
false
false
false
 true
 true
false
false
false
false
false
 true
false
false
false
false
false
false
false
false
false
 true
false
false
 true
false
 true
 true
 true
false
false
 true
 true
false
 true
 true
false
false
false
 true
 true
false
false
false
false
false
 true
false
false
 true
false
false
false
 true
 true
 true
 true
false
false
false
 true
false
false
false
false
false
false
false
false
false
false
false
false
 true
 true
 true
 true
 true
false
false
false
 true
false
 true
false
false
false
false
 true
false
false
false
false
 true
false
 true
false
false
false
 true
false
false

Using linspace, arange, ones, zeros


In [11]:
#include "xtensor/xbuilder.hpp"

In [12]:
xt::xarray<double> ar = xt::linspace<double>(0.0, 10.0, 12);
ar.reshape({4, 3});
ar


Out[12]:
  0.      
  0.909091
  1.818182
  2.727273
  3.636364
  4.545455
  5.454545
  6.363636
  7.272727
  8.181818
  9.090909
 10.      

In [13]:
xt::xarray<double> fones = xt::ones<float>({2, 2});
fones


Out[13]:
 1.
 1.
 1.
 1.

In [14]:
xt::arange<int>(1569325055)


Out[14]:
         0
         1
         2
1569325052
1569325053
1569325054

Using xt::broadcast


In [15]:
#include <vector>
#include "xtensor/xbroadcast.hpp"

In [16]:
xt::broadcast(xt::linspace<double>(0.0, 10.0, 4),
              std::vector<std::size_t>({3, 4}))


Out[16]:
 0.      
 0.      
 0.      
 0.      
 3.333333
 3.333333
 3.333333
 3.333333
 6.666667
 6.666667
 6.666667
 6.666667

Using standard algorithms with xexpressions


In [17]:
#include <algorithm>

In [18]:
xt::xarray<double> frand = xt::random::randn<double>({2, 2});

// 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);

frand


-2.01197
Out[18]:
-2.339393
 0.747579
-0.307341
-0.112812

Iterating over a prescribed broadcasted shape


In [19]:
// 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);


-6.0359

Blas bindings for xtensor.

In [20]:
#include "xtensor-blas/xlinalg.hpp"

In [21]:
xt::xtensor<double, 2> m = {{1.5, 0.5}, {0.7, 1.0}};

In [22]:
std::cout << "Matrix rank: " << std::endl << xt::linalg::matrix_rank(m);


Matrix rank: 
2

In [23]:
std::cout << "Matrix inverse: " << std::endl << xt::linalg::inv(m);


Matrix inverse: 
{{ 0.869565, -0.434783},
 {-0.608696,  1.304348}}

In [24]:
std::cout << "Eigen values: " << std::endl << xt::linalg::eigvals(m);


Eigen values: 
{ 1.892262+0.i,  0.607738+0.i}

In [25]:
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;


{{{  15.,   18.,   21.},
  {  42.,   45.,   48.}},
 {{  42.,   54.,   66.},
  { 150.,  162.,  174.}},
 {{  69.,   90.,  111.},
  { 258.,  279.,  300.}}}

In [ ]: