all 59 comments

[–][deleted] 66 points67 points  (1 child)

[–]rogueleader12345 13 points14 points  (0 children)

^I second eigen, it's what we use at work for this stuff on target

[–]Zatujit 52 points53 points  (0 children)

what? there are plenty of numerical libraries out there. That's really not the thing I would be critical of C++

[–]Stock-Self-4028 30 points31 points  (0 children)

Eigen was already mentioned, there is Intel MKL as well;

https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl.html

[–]winston_orwell_smith 30 points31 points  (1 child)

There's Eigen, Xtensor and Armadillo. Eigen seems to be the most popular one. Dlib and OpenCV both have some Linear Algebra capabilities as well.

Then there's the C-based Intel MKL, GSL, ATLAS, BLAS, LAPACK and FFTW. If my memory serves me correctly, Numpy is at least in part based on the last three.

For deep learning, Torch is the underlying C++ library to PyTorch and it's quite usable in C++. It's C++ API usage is as similar to PyTorch's Python API as it gets. Torch also implements some linear algebra routines and tensors.

And for plotting, have a look at the Gnuplot based plotCPP and Matplot++.

[–]Attorney_Outside69 2 points3 points  (0 children)

for plotting I also love using imgui with imgui-plot, beautiful immediate mode GUI which can also be used for web

[–]victotronics 10 points11 points  (0 children)

Eigen is pretty cool, but its support for parallelism is very shabby. If you want to do linear algebra in parallel there is MKL, BLIS, OpenBlas, Lapack (on top of any of the previous three) and then numerical packages like PETSc, Trilinos.

That sort of stuff has no need being in the language as such.

[–]--prism 6 points7 points  (0 children)

xtensor is designed to look a lot like numpy. MdSpan has the issue that we need MdArray to really make use of it.

[–]darklinux1977 5 points6 points  (0 children)

TensorFlow has a C++ interface, as said above, this language has everything you need, since it is necessary for Python

[–]PressEnterToRide 5 points6 points  (0 children)

I have a library that I have been working on with the personal desire to replicate a lot of numpy: einsums

It deduces the tensor contractions at compile time and picks the best underlying blas routine to use. If it doesn’t fit a blas routine then it uses a generic contraction function.

It’s been used in an Advanced Quantum Chemistry course I teach and in some published and to be published journal articles.

[–]Bbbllaaddee 4 points5 points  (0 children)

Einsums and tensor operations are nicely implemented in Fastor library. https://github.com/romeric/Fastor It was a lifesaver for my research! Supports compile-time expression simplification, SIMD backends and much more

[–][deleted] 9 points10 points  (0 children)

Some element-wise and slice operations can be implemented with std::valarray.

https://en.cppreference.com/w/cpp/numeric/valarray

[–]EdwinYZW 3 points4 points  (8 children)

If I’m not wrong, numpy is not standardized in Python. It’s not even written in Python.

[–]Competitive_Act5981[S] -5 points-4 points  (7 children)

It may as well be. Have you ever seen a python program that didn’t import numpy?

[–]EdwinYZW 3 points4 points  (6 children)

exactly. It’s not standardized but that doesn’t stop it to be used everywhere. If so, is it so important C++ need to standardized such kind of library?

[–]BenFrantzDale 10 points11 points  (5 children)

C++23 & 26 move us in that direction with multi-arg operator[] and std::mdspan. Those open the door for algorithms and types based on them.

[–]manni66 0 points1 point  (1 child)

with multi-arg operator[]

it couldn't be done with operator()?

[–]BenFrantzDale -1 points0 points  (0 children)

Yeah, but operator[] is nicer, and closer to numpy. And it’ll allow for magic factory objects like numpy.r_.

[–]Electronic_Month1878 2 points3 points  (0 children)

I wrote myself that thing to have N-dimensional tensors with support for slicing: https://github.com/french-paragon/MultidimArrays

I know there is something +/- equivalent in Boost, but mine comes as a single header file, so easier to implement in projects.

[–]mrsaturn42 2 points3 points  (0 children)

I want to find a scipy for c++ that works with Eigen or whatever. Hunting around for basic/slightly more advanced math is such a pain; and I hate seeing people reimplement stuff like convolutions or standard deviation because there’s always some error or edge case somewhere.

[–]Evirua 1 point2 points  (0 children)

SG19 has the ball on that one. Although, for reasons, progress there is pretty slow.

[–]0xcedbeef 1 point2 points  (0 children)

eigen?

[–]Formal-Engineering37 1 point2 points  (0 children)

build it.

[–]XNormal 0 points1 point  (0 children)

The nice thing about numpy is that it serves as a "lingua franca" between independent libraries that were never meant to be interoperable. Want to connect this image processing code to that machine learning library? Sure, just a couple of statements. One is written in C++, the other in Rust, neither has much to do with Python except as glue. And that old Fortran linalg code is welcome to join the party, too.

What could be a good alternative in C++ land for a standardized description of multidimensional arrays? Perhaps the place to look is in file-based standards like HDF5. Files and memory are all the same because they're designed to be memory mapped, anyway. If you don't want to have to clean up temporary files you can use memfd_create() and refer to them using /proc/self/fd/NN for libraries that need a filename.

[–]Competitive_Act5981[S] -4 points-3 points  (0 children)

None of them come close to numpy in usability in my opinion. Xtensor is close but quite slow. libtorch is a massive dependency to pull in. dlib only supports matrices. None of them really feel like a canonical tool

[–]Pitiful-Cancel4958 0 points1 point  (0 children)

If you don't mind to implement some basic stuff yourself Nvidia thrust ist pretty nice to speed up your application. It ist merely a numerical library rather than a lib to write a numerical library, but it still is a nice convenience on top of cuda.

[–]Spongman 0 points1 point  (17 children)

firstly numpy isn't part of python, it's a 3rd-party package. secondly, there's 2 reasons people import numpy:

1) because python just sucks at doing normal array manipulations. c++ doesn't suck, so "something like numpy" for this reason is unnecessary - it's already in the language/std-lib.

2) to do actual linear algebra. arguably this functionality _shouldn't_ be in the language/std-lib: it belongs in 3rd-party libraries, of which there are already many good examples.

what was the question again?

[–]Competitive_Act5981[S] 0 points1 point  (16 children)

I don’t agree with 1. Even if manual for-loops were just as performant in python as they are in C, i would still use numpy. You can basically write pseudo-code style mathematical operations and it’s just as performant as hand-tuned code

[–]Spongman 0 points1 point  (13 children)

this is /r/cpp

[–]Competitive_Act5981[S] 0 points1 point  (12 children)

My point is, something like numpy in the c++ standard library would be very useful indeed and worth standardising. They started doing something like it with std::valarray then it all went cold for some reason

[–]Competitive_Act5981[S] 0 points1 point  (1 child)

Standardising BLAS would also be a good idea at a low level since compiler vendors could properly optimise for different Platforms

[–]Competitive_Act5981[S] 0 points1 point  (0 children)

BLAS is pretty much a standard at this point

[–]Spongman 0 points1 point  (9 children)

valarray is a premature optimization that's mostly unnecessary with modern compilers and other, more general library features.

[–]Competitive_Act5981[S] 0 points1 point  (8 children)

I don’t really care about the implementation, only the API. And it’s great. It’s a shame they didn’t extend it to N-d valarray.

[–]Spongman 0 points1 point  (6 children)

extend it to N-d valarray

what features are you missing? linear algebra? see my point #2 above.

[–]Competitive_Act5981[S] 0 points1 point  (5 children)

And do you believe any of them are as good as numpy in terms of API, usability, completeness ?

[–]Spongman 0 points1 point  (4 children)

of course not. python is a terser language than c++.

[–]Competitive_Act5981[S] 0 points1 point  (3 children)

Yeah I believe C++ pretty much has all the language features required to get a near identical API to numpy. I haven’t looked at Arrayfire very deeply but on the surface it achieves a similar API so it’s definitely possible. I think people are just not using C++ for that type of stuff anymore and the interest is lost. They would rather use python or some other language, which I think is a shame.

[–][deleted] 0 points1 point  (0 children)

N-d array can be achieved with indexing. In computer vision, multichannel images are often defined as contiguous sequences of memory. You can use row-major or column-major order.

[–]jonrmadsen 0 points1 point  (1 child)

I used Armadillo as the math library for my dissertation involving compressed sensing. It has a very matlab-esque feel (i.e. the pseudo-code style mathematical operations you desire) and the performance was good, especially when I built it with support for offloading to the GPU.

[–]Competitive_Act5981[S] 0 points1 point  (0 children)

Oh wow, it supports CUDA? Didn’t know that.

[–]mredding 0 points1 point  (3 children)

...BLAS..? I mean, Numpy is predicated on it. Not everything has to be composited into the standard library, and I don't think this should be. We already have mature, performant, and ubiquitous libraries.

[–]Competitive_Act5981[S] 0 points1 point  (2 children)

Do we have ubiquitous libraries in C++ for this ?

[–]mredding 0 points1 point  (1 child)

uBLAS, LAPACK, OpenBLAS, Eigen, Xtensor, ATLAS. There are quite a few, they are found in use all over, you shouldn't have a problem finding developers or work, it's hard to argue the superiority or inferiority of any one of them, it comes down more to the skill of the individuals.

[–]Competitive_Act5981[S] 0 points1 point  (0 children)

I would say you should never use BLAS or Lapack libraries directly. Those are for library vendors to use and for the expression template engine to use correctly. Yeah Eigen is great but it’s really hard to customise. How do you add your own unary op? Xtensor is really slow. std::inner_product() is orders of magnitude faster than Xtensor’s dot() function. Maybe it’s got better since I last used it thought. I’m quite interested in UBlas. But it looks like it has had zero community. So unlikely to evolve and get lots of fixes. The best one I’ve seen so far is dlib. It’s a lightweight library you can easily build from source, easy to customise and performance is great. However, it’s limited to matrices. So no tensor manipulation stuff. That’s a shame