program story

C ++ 용 NumPy 스타일 배열?

inputbox 2020. 12. 5. 09:44
반응형

C ++ 용 NumPy 스타일 배열?


슬라이싱, 벡터화 된 연산, 요소 별 콘텐츠 추가 및 빼기 등을 지원하는 NumPy와 유사한 배열이있는 C ++ (또는 C) 라이브러리가 있습니까?


다음은 귀하의 요구에 맞는 몇 가지 무료 소프트웨어입니다.

  1. GNU 과학 라이브러리 따라서 C. 작성된 GPL 소프트웨어, 그것은 C 같은 프로그래밍 (포인터 등)의 할당 및 방법이있다. 으로 GSLwrap 여전히 GSL을 사용하는 동안, 당신은, 프로그램의 C ++ 방법이 있습니다. GSL에는 BLAS 구현이 있지만 더 많은 성능을 원한다면 기본 CBLAS 대신 ATLAS사용할 수 있습니다 .

  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참조 하십시오 .

이러한 대안은 데이터 구조와 기본 선형 대수를 얻고 자 할 때 정말 좋습니다. 스타일, 라이선스 또는 시스템 관리자 문제에 대한 취향에 따라 (LAPACK과 같은 큰 라이브러리를 설치하는 것이 어려울 수 있음) 필요에 가장 적합한 것을 선택할 수 있습니다.


xtensor를 사용해보십시오 . ( NumPy to Xtensor 치트 시트 참조 ).

xtensor는 다차원 배열 표현식을 사용한 수치 분석을위한 C ++ 라이브러리입니다.

xtensor는 제공합니다

  • numpy 스타일의 방송을 가능하게하는 확장 가능한 표현 시스템.
  • C ++ 표준 라이브러리의 관용구를 따르는 API.
  • xtensor를 기반으로 배열 표현식을 조작하고 빌드하는 도구.

2 차원 배열을 초기화하고 행 중 하나와 1 차원 배열의 합을 계산합니다.

#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;

출력

{7, 11, 14}

1 차원 배열을 초기화하고 제자리에서 모양을 변경합니다.

#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;

출력

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

DyND 는 무엇보다도 C ++ 용 NumPy와 유사한 라이브러리로 설계되었습니다. 방송, 산술 연산자, 슬라이싱과 같은 것들은 모두 잘 작동합니다. 다른 한편으로는 여전히 매우 실험적이고 다양한 기능은 아직 구현되지 않았습니다.

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

#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 post a little while back with more examples and side-by-side comparisons of the syntax for Fortran 90, DyND in C++, and NumPy in Python.

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


Eigen is a good linear algebra library.

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?


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.


VIGRA contains a good N-dimensional array implementation:

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.

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


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


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


While GLM is 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.

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

벡터 또는 행렬에 포함 된 값에 액세스하는 것도 똑같이 간단합니다. 예를 들어 vec3 벡터를 선언 한 후 vector.x를 사용하여 첫 번째 요소에 액세스 할 수 있습니다. 확인 해봐.


LibTorch (C ++ 용 PyTorch 프런트 엔드)를 사용하고 행복하세요.

참고 URL : https://stackoverflow.com/questions/11169418/numpy-style-arrays-for-c

반응형