C++ 的 NumPy 样式数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11169418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:54:23  来源:igfitidea点击:

NumPy style arrays for C++?

c++arraysdynamic-arrays

提问by Llamageddon

Are there any C++ (or C) libs that have NumPy-like arrays with support for slicing, vectorized operations, adding and subtracting contents element-by-element, etc.?

是否有任何 C++(或 C)库具有类似 NumPy 的数组,支持切片、矢量化操作、逐元素添加和减去内容等?

采纳答案by nojhan

Here are several free software that may suit your needs.

这里有几个可能适合您需求的免费软件。

  1. The GNU Scientific Libraryis a GPL software written in C. Thus, it has a C-like allocation and way of programming (pointers, etc.). With the GSLwrap, you can have a C++ way of programming, while still using the GSL. GSL has a BLASimplementation, but you can use ATLASinstead of the default CBLAS, if you want even more performances.

  2. The boost/uBLASlibrary is a BSL library, written in C++ and distributed as a boost package. It is a C++-way of implementing the BLAS standard. uBLAS comes with a few linear algebra functions, and there is an experimental binding to ATLAS.

  3. eigenis a linear algebra library written in C++, distributed under the LGPL3 (or GPL2). It's a C++ way of programming, but more integrated than the two others (more algorithms and data structures are available). Eigen claim to be fasterthan the BLAS implementations above, while not following the de-facto standard BLAS API. Eigen does not seem to put a lot of effort on parallel implementation.

  4. Armadillois LGPL3 library for C++. It has binding for LAPACK(the library used by numpy). It uses recursive templates and template meta-programming, which is a good point (I don't know if other libraries are doing it also?).

  5. xtensoris a C++ library that is BSD licensed. It offers A C++ API very similar to that of NumPy. See https://xtensor.readthedocs.io/en/latest/numpy.htmlfor a cheat sheet.

  1. GNU科学图书馆是用C编写因此,一个GPL软件,它具有类C(指针等)的分配和方式。使用GSLwrap,您可以使用 C++ 编程方式,同时仍然使用 GSL。GSL 有一个BLAS实现,但如果您想要更多性能,您可以使用ATLAS代替默认的 CBLAS。

  2. 升压/ uBLAS库库是一个BSL库,用C ++编写和分布式作为升压包。它是实现 BLAS 标准的 C++ 方式。uBLAS 带有一些线性代数函数,并且有一个与 ATLAS实验性绑定

  3. eigen是一个用 C++ 编写的线性代数库,在 LGPL3(或 GPL2)下分发。它是一种 C++ 编程方式,但比其他两种方式更加集成(可用的算法和数据结构更多)。Eigen声称比上述 BLAS 实现更快,但不遵循事实上的标准 BLAS API。Eigen 似乎并没有在并行实现上投入太多精力。

  4. Armadillo是 C++ 的 LGPL3 库。它绑定了LAPACK(numpy 使用的库)。它使用递归模板和模板元编程,这是一个很好的点(我不知道其他库是否也这样做?)。

  5. xtensor是 BSD 许可的 C++ 库。它提供了与 NumPy 非常相似的 C++ API。有关备忘单,请参阅https://xtensor.readthedocs.io/en/latest/numpy.html

These alternatives are really good if you just want to get data structures and basic linear algebra. Depending on your taste about style, license or sysadmin challenges (installing big libraries like LAPACK may be difficult), you may choose the one that best suits your needs.

如果您只想获得数据结构和基本的线性代数,这些替代方案非常好。根据您对风格、许可证或系统管理员挑战的品味(安装 LAPACK 之类的大型库可能很困难),您可以选择最适合您需求的一个。

回答by Quant

Try out xtensor. (See the NumPy to Xtensor Cheat Sheet).

试试xtensor。(请参阅NumPy 到 Xtensor 备忘单)。

xtensor is a C++ library meant for numerical analysis with multi-dimensional array expressions.

xtensor 是一个 C++ 库,用于多维数组表达式的数值分析。

xtensor provides

xtensor 提供

  • an extensible expression system enabling numpy-style broadcasting.
  • an API following the idioms of the C++ standard library.
  • tools to manipulate array expressions and build upon xtensor.
  • 一个可扩展的表达系统,支持 numpy 风格的广播。
  • 遵循 C++ 标准库习惯用法的 API。
  • 用于操作数组表达式并基于 xtensor 构建的工具。

Example

例子

Initialize a 2-D array and compute the sum of one of its rows and a 1-D array.

初始化一个二维数组并计算其中一行和一维数组的总和。

#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"

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::xarray<double> res = xt::view(arr1, 1) + arr2;

std::cout << res;

Outputs

输出

{7, 11, 14}

Initialize a 1-D array and reshape it inplace.

初始化一维数组并就地重塑它。

#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"

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

arr.reshape({3, 3});

std::cout << arr;

Outputs

输出

{{1, 2, 3},
 {4, 5, 6},
 {7, 8, 9}}

回答by IanH

DyNDis designed to be, among other things, a NumPy-like library for C++. Things like broadcasting, arithmetic operators, and slicing all work fine. On the other hand, it is still veryexperimental and many features haven't been implemented yet.

DyND被设计成一个类似 NumPy 的 C++ 库。诸如广播、算术运算符和切片之类的东西都可以正常工作。在另一方面,它仍然是非常实验和许多功能尚未实现。

Here's a simple implementation of the de Casteljau algorithm in C++ using DyND arrays:

下面是使用 DyND 数组在 C++ 中的 de Casteljau 算法的简单实现:

#include <iostream>
#include <dynd/array.hpp>

using namespace dynd;

nd::array decasteljau(nd::array a, double t){
    size_t e = a.get_dim_size();
    for(size_t i=0; i < e-1; i++){
        a = (1.-t) * a(irange()<(e-i-1)) + t * a(0<irange());
    }
    return a;
}

int main(){
    nd::array a = {1., 2., 2., -1.};
    std::cout << decasteljau(a, .25) << std::endl;
}

I wrote a blog posta little while back with more examples and side-by-side comparisons of the syntax for Fortran 90, DyND in C++, and NumPy in Python.

不久前,我写了一篇博客文章,其中包含更多示例和对 Fortran 90、C++ 中的 DyND 和 Python 中的 NumPy 的语法的并排比较。

Disclaimer: I'm one of the current DyND developers.

免责声明:我是当前的 DyND 开发人员之一。

回答by Frédéric Terrazzoni

Eigen is a good linear algebra library.

Eigen 是一个很好的线性代数库。

http://eigen.tuxfamily.org/index.php?title=Main_Page

http://eigen.tuxfamily.org/index.php?title=Main_Page

It is quite easy to install since it's a header-only library. It relies on template in order to to generate well optimized code. It vectorizes automatically the matrix operations.

它很容易安装,因为它是一个只有头文件的库。它依赖模板来生成优化好的代码。它自动矢量化矩阵运算。

It also fully support coefficient wise operations, such as the "per element multiplication" between two matrices for instance. It is what you need?

它还完全支持系数明智的操作,例如两个矩阵之间的“每元素乘法”。这是你需要的吗?

回答by Dan Stahlke

Blitz++supports arrays with an arbitrary number of axes, whereas Armadillo only supports up to three (vectors, matrices, and cubes). Eigen only supports vectors and matrices (not cubes). The downside is that Blitz++ doesn't have linear algebra functions beyond the basic entrywise operations and tensor contractions. Development seems to have slowed down quite some time ago, but perhaps that's just because the library does what it does and not many changes need to be made.

Blitz++支持具有任意数量轴的数组,而 Armadillo 最多仅支持三个(向量、矩阵和立方体)。Eigen 仅支持向量和矩阵(不支持立方体)。缺点是 Blitz++ 除了基本的入门操作和张量收缩之外没有线性代数函数。很久以前,开发似乎已经放慢了速度,但这也许只是因为库做了它该做的事情,不需要进行太多更改。

回答by Martin

VIGRA contains a good N-dimensional array implementation:

VIGRA 包含一个很好的 N 维数组实现:

http://ukoethe.github.io/vigra/doc/vigra/Tutorial.html

http://ukoethe.github.io/vigra/doc/vigra/Tutorial.html

I use it extensively, and find it very simple and effective. It's also header only, so very easy to integrate into your development environment. It's the closest thing I've come across to using NumPy in terms of it's API.

我广泛使用它,发现它非常简单有效。它也只是标题,因此很容易集成到您的开发环境中。就其 API 而言,这是我遇到的最接近使用 NumPy 的事情。

The main downside is that it isn't so widely used as the others, so you won't find much help online. That, and it's awkwardly named (try searching for it!)

主要的缺点是它不像其他人那样被广泛使用,所以你在网上找不到太多帮助。那个,它的名字很尴尬(尝试搜索它!)

回答by Claudio

Eigenis a template library for linear algebra (matrices, vectors…). It is header only and free to use (LGPL).

Eigen是线性代数(矩阵、向量……)的模板库。它仅包含标题且可免费使用 (LGPL)。

回答by Артем Ященко

Use LibTorch (PyTorch frontend for C++) and be happy.

使用 LibTorch(C++ 的 PyTorch 前端)并感到高兴。

回答by Matt Phillips

The GSLis great, it does all of what you're asking and much more. It is licensed under the GPL though.

GSL是伟大的,它所有你问等等什么。不过,它是根据 GPL 许可的。

回答by Matt Phillips

While GLMis designed to mesh easily with OpenGL and GLSL, it is a fully functional header only math library for C++ with a very intuitive set of interfaces.

虽然GLM旨在轻松地与 OpenGL 和 GLSL 进行网格划分,但它是一个功能齐全的仅标头的 C++ 数学库,具有一组非常直观的接口。

It declares vector & matrix types as well as various operations on them.

它声明了向量和矩阵类型以及对它们的各种操作。

Multiplying two matrices is a simple as (M1 * M2). Subtracting two vectors (V1- V2).

两个矩阵相乘很简单,如 (M1 * M2)。减去两个向量(V1-V2)。

Accessing values contained in vectors or matrices is equally simple. After declaring a vec3 vector for example, one can access its first element with vector.x. Check it out.

访问向量或矩阵中包含的值同样简单。例如,在声明一个 vec3 向量后,可以使用 vector.x 访问它的第一个元素。一探究竟。