# Fast, lightweight C++ tensor library for dimension-agnostic code

I am looking for a C++ tensor library that supports dimension-agnostic code. Specifically, I need to perform operations along each dimension (up to 3), e.g. calculating a weighted sum. The dimensions is a template parameter (and thus a compile-time constant). Another constraint is that the library should be relatively lightweight, so rather Eigen/Boost-style than Trilinos/PETSc.

Any suggestions?

Note: I have had a look at Eigen and think it almost fits the profile exactly, if it weren't limited to 2D tensors. If I am mistaken by this, please correct me.

FTensor is a lightweight, header only, fully templated library that includes ergonomic summation notation. It has been tested extensively in 2, 3, and 4 dimensions, but should work fine for any number of dimensions.

For what its worth, Eigen does have a Tensor class as an unsupported module.

http://eigen.tuxfamily.org/dox-devel/unsupported/group_CXX11_Tensor__Module.html

I haven't used it myself so can't say more about it.

The Armadillo class library has a 3rd-order tensor class.

http://arma.sourceforge.net/

I haven't used the tensor capabilities of Armadillo either but have experimented with the standard matrix classes and they seem relatively easy to use and have good performance.

• More specifically, Armadillo has the Cube class, which implements 3rd order tensors – mtall May 22 '14 at 16:59

I think this new taco lib is really good too.

The Tensor Algebra Compiler (taco) is a C++ library that computes tensor algebra expressions on sparse and dense tensors. It uses novel compiler techniques to get performance competitive with hand-optimized kernels in widely used libraries for both sparse tensor algebra and sparse linear algebra.

You can use taco as a C++ library that lets you load tensors, read tensors from files, and compute tensor expressions. You can also use taco as a code generator that generates C functions that compute tensor expressions.

• Welcome to SciComp.SE! Could you expand your answer to describe the library (links can go dead) and especially how it meets the requirements of the OP? Otherwise it's more of a comment. – Christian Clason Feb 4 '18 at 9:19
• Seems like a very interesting library! It looks like this generates an extra compilation step, though. I'm wondering whether it would be possible to implement this using templates instead? – HelloGoodbye Mar 27 '19 at 9:42
• @HelloGoodbye I was looking for the same thing it sounds like you are, and ended up building github.com/dsharlet/array. It implements similar functionality but with templates. It doesn't do sparse arrays though. – dsharlet Aug 1 at 22:05

XTensor is a modern approach and is getting more and more popular. https://github.com/QuantStack/xtensor

The deal.II library (http://www.dealii.org), while written for much larger purposes, also has a sub-library of tensor classes that likely does a lot of what you want to do. In particular, it uses templates for the dimension.

(Disclaimer: I am one of the principal authors of this library.)

The library Boost.Numeric.uBlas recently added a tensor extension which is shipped with Boost version 1.70. Please have a look at https://github.com/boostorg/ublas. It provides standard matrix and tensor operations with runtime-variable order (number of dimensions), dimensions for the first- and last-order storage formats (column- and row-major). You can also easily use the Einstein summation convention to express aribtrary tensor multiplications. Boost.Numeric.uBlas is header only and easy to integrate into existing projects.

I have not used it myself but libtensor seems to fulfill your requirements.

this is a multidimensional array C++ library https://github.com/ContinuumIO/libdynd

LTensor (https://code.google.com/p/ltensor/) is a VERY easy to use C++ template library for tensors up to rank 4 (based on indical notation), fast and lightweight too. You don't need to compile anything only need to include the main header file. I have used it on several projects and worked ok.

It has some built-in features for rank-2 tensors like linear solvers, svd, LU and Cholesky decompositions, etc. I didn't use any of them (I use other libraries for that).

There is also ITensor (requires C++17).