Linear Algebra In C++ With XTensor Is Just Like Numpy
By: Emmett Boudreau
Tags: Cplusplus, Python, Programming, Linear Algebra, Data Science

Basic Matrices

If you are a Python and Numpy user, then you are in luck with XTensor. Here is a link to an absolutely invaluable Numpy to XTensor cheat-sheet which you can use to apply your Numpy familiarity:

This is going to make the transition from Numpy to XTensor incredibly simple and stream-lined for Python-based Data Scientists and end-users. After installing XTensor, you can compile and execute the following code to ensure that the headers are now on your machine:

#include <iostream>

// Base Arrays:

#include "xtensor/xarray.hpp"

// XTensor IO:

#include "xtensor/xio.hpp"

// XTensor View:

#include "xtensor/xview.hpp"

int main()

{

return 0;

}

If nothing happens whenever you compile and run that code, then

congratulations!

Your installation was successful and XTensor is now available on your machine. If the output was an error, you will most likely need to try your luck with a different installation method, or build from source. Another important thing to ensure is that you are in the Conda environment that you installed XTensor in, of course.

Moving on, we will of course start our new code by adding the same inclusions we did in that little test.

#include <iostream>

// Base Arrays:

#include "xtensor/xarray.hpp"

// XTensor IO:

#include "xtensor/xio.hpp"

// XTensor View:

#include "xtensor/xview.hpp"

Including these headers will give us a new name-space, xt. For starters, in order to create a matrix, we are going to use the xarray object template from the xt name-space. For this example, we will have a matrix full of integers, so we will provide the “ int” argument whenever we create our new object. I am just going to name it array, and we will put the some integers into it.

xt::xarray<int>array

({{500, 500, 300}, {500, 600, 800}})

There are two different types of XTensor matrices:

xarray — Matrices with a dynamic dimension set.

dimension set. xtensor — Matrices with a static dimension set.

Next, if we are in a notebook, then we can simply type “ array” to show our new matrix.

(image by author)

Alternatively, if you are compiling and executing code, then you will need to use cout and put this code into a main() function of course. I am not going to be doing that in this example, as I am working in a notebook, but here is how you would structure this code, for example:

#include <iostream>

// Base Arrays:

#include "xtensor/xarray.hpp"

// XTensor IO:

#include "xtensor/xio.hpp"

// XTensor View:

#include "xtensor/xview.hpp"

int main()

{

xt::xarray<int>array

({{500, 500, 300}, {500, 600, 800}});

std::cout << array;

}

Within some IDEs, you might be able to just cout it, as well:

std::cout << array;

This is where including iostream off the bat will come in handy, even if you are working in a notebook.

We can reshape our new array using the reshape() function, which is a child of our array object.

array.reshape({3, 2})

(image by author)

We can also cast a new type into all of the dims of our array. This is useful, for example if we wanted the ability to count fractional parts but originally created an matrix full of integers. We can do this using the cast function, which is back in the xt namespace.

xt::cast<double>(array)

(image by author)

We also have access to all of the typical Numpy initializers/generators that we can use to create new matrices. A very useful version of this commonly used in Machine-learning is np.zeroes(). The equivalent to this in XTensor is zeros from the xt name-space, which is of course an object that inherits from our regular xarray. Note that it does inherit from xarray and not xtensor, so the dimensions of our new found matrix will be mutable.

xt::zeros<double>({3, 4})

(image by author)

This also applies to the other generators available in Numpy that you might expect to carry over, such as eye, ones, and many others.

(image by author)

We can of course also index our arrays in the same way we would with Numpy. One minor difference is that parenthesis are used to call indexes, rather than brackets.