29

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.

10 Answers10

9

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.

Damascus Steel
  • 483
  • 1
  • 4
  • 10
6

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.

Bill Greene
  • 6,064
  • 1
  • 16
  • 24
6

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.

Talk: https://www.youtube.com/watch?v=Kffbzf9etLE Paper: http://tensor-compiler.org/kjolstad-oopsla17-tensor-compiler.pdf

Dhi Aurrahman
  • 61
  • 1
  • 2
  • 2
    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 04 '18 at 09: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 09:42
  • @HelloGoodbye I was looking for the same thing it sounds like you are, and ended up building https://github.com/dsharlet/array. It implements similar functionality but with templates. It doesn't do sparse arrays though. – dsharlet Aug 01 '20 at 22:05
6

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

Jingpeng Wu
  • 161
  • 1
  • 1
5

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.)

Wolfgang Bangerth
  • 55,373
  • 59
  • 119
2

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.

Cem Bassoy
  • 21
  • 1
1

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

Juan M. Bello-Rivas
  • 3,994
  • 16
  • 24
1

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

post-as-guest
  • 216
  • 2
  • 2
1

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).

0

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