简体   繁体   English

使用 SWIG 将 numpy 数组元素(int)传递给 c++ int

[英]Passing numpy array element (int) to c++ int using SWIG

I'd like to pass an integer element from a numpy array in python to a c++ function that catches it as a c++ integer using SWIG. I'd like to pass an integer element from a numpy array in python to a c++ function that catches it as a c++ integer using SWIG.

What am I missing here?我在这里想念什么?

add_vector.i add_vector.i

%module add_vector
%{
    #define SWIG_FILE_WITH_INIT
    #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION  // gets rid of warning
    #include "add_vector.h"
%}

%include "numpy.i"
%init %{
import_array();
%}

%include "add_vector.h"

add_vector.h add_vector.h

#include <iostream>

void print_int(int x);

add_vector.cpp add_vector.cpp

#include "add_vector.h"

void print_int(int x) {
    std::cout << x << std::endl;
}

tester.py测试器.py

import add_vector as vec
import numpy as np

a = np.array([1,2,3])
print(a[1])
vec.print_int(a[1])

OUTPUT OUTPUT

2
Traceback (most recent call last):
  File "tester.py", line 6, in <module>
    vec.print_int(a[1])
TypeError: in method 'print_int', argument 1 of type 'int'

Reading from the numpy.i manual ( https://docs.scipy.org/doc/numpy-1.13.0/reference/swig.interface-file.html#numpy-array-scalars-and-swig ), I copied the pyfragments.swg file into my working directory, but nothing changed. Reading from the numpy.i manual ( https://docs.scipy.org/doc/numpy-1.13.0/reference/swig.interface-file.html#numpy-array-scalars-and-swig ), I copied the pyfragments.swg 文件进入我的工作目录,但没有任何改变。

I've also tried a number of %apply directives both for passing an int and an int *, but that hasn't yet changed anything.我还尝试了一些 %apply 指令来传递一个 int 和一个 int *,但这还没有改变任何东西。 I keep getting the same TypeError I listed above.我不断收到上面列出的相同类型错误。

Versions: numpy 1.17.3;版本: numpy 1.17.3; swig 2.0.12;痛饮2.0.12; python 3.7.3; python 3.7.3; numpy.i is being copied into my working directory from: /usr/lib/python2.7/dist-packages/instant/swig/numpy.i numpy.i被复制到我的工作目录中:/usr/lib/python2.7/dist-packages/instant/swig/numpy.i

Solved: There were 3 issues:已解决:有3个问题:

  1. The numpy.i file I copied over isn't compatible , and the compatible version isn't included in the installation package when you go through anaconda (still not sure why they'd do that). The numpy.i file I copied over isn't compatible , and the compatible version isn't included in the installation package when you go through anaconda (still not sure why they'd do that).

Answer: Find which version of numpy you're running, then go here ( https://github.com/numpy/numpy/releases ) and download the numpy-[your_version].zip file, then specifically copy the numpy.i file, found in numpy-[your_version]/tools/swig/. Answer: Find which version of numpy you're running, then go here ( https://github.com/numpy/numpy/releases ) and download the numpy-[your_version].zip file, then specifically copy the numpy.i file ,在 numpy-[your_version]/tools/swig/ 中找到。 Now paste that numpy.i into your project working directory.现在将 numpy.i 粘贴到您的项目工作目录中。

  1. As a default, numpy makes integers of type long.默认情况下,numpy 生成 long 类型的整数。 So in tester.py file, I needed to write: a = np.array([1,2,3], dtype=np.intc)所以在 tester.py文件中,我需要写: a = np.array([1,2,3], dtype=np.intc)

  2. Need to convert numpy int to c++ int in add_vector.i .需要add_vector.i 中将 numpy int 转换为 c++ int 。 This can be done by using the %apply directive right above the %include "add_vector.h" line: %apply (int DIM1) {(int x)};这可以通过使用 %include "add_vector.h" 行上方的 %apply 指令来完成: %apply (int DIM1) {(int x)};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM