使用来自numpy.i的部分类型映射
using partial typemaps from numpy.i
我有一个函数只需要输入数组的一个维度,所以我忽略了2D numpy数组中的一个维度。有办法做到这一点吗?
头:#ifndef __nparrtest_h__
#define __nparrtest_h__
class NPArrTest {
public:
static void Print2D(int n, int m, const int* graph);
};
#endif
Cpp:
#include "NPArrTest.h"
#include <stdio.h>
void NPArrTest::Print2D(int n, int m, const int* graph) {
printf("n: %d m: %dn",n,m);
}
swig接口文件:
%module NPArrTest
%{
#define SWIG_FILE_WITH_INIT
#include "NPArrTest.h"
%}
%include "numpy.i"
%init %{
import_array1();
%}
%numpy_typemaps(int, NPY_INT , int)
//Something like the below:
%apply int DIM2, int* IN_ARRAY2 {int m, const int* graph};
%include "NPArrTest.h"
我不能让%apply (int DIM1, int* IN_ARRAY2) {(int m, const int* graph)};
或%apply int* IN_ARRAY2 {const int* graph};
工作(梯子只是通过2D数组)。
如何传递二维数组的单一维度长度值到c++函数?
>
注:是否有一种简单的方法来允许这种转换?
TypeError:不能根据'safe'规则将数组数据从dtype('int64')强制转换为dtype('int32')
我对一个固定维度的输出数组做了类似的处理。下面的代码是我为numpy.i做的扩展的一部分。
#ifdef SWIGPYTHON
%{
#ifndef SWIG_FILE_WITH_INIT
# define NO_IMPORT_ARRAY
#endif
#include "stdio.h"
#include <numpy/arrayobject.h>
%}
%include "numpy.i"
%define %numpy_ex_typemaps(DATA_TYPE, DATA_TYPECODE, DIM_TYPE)
/* Typemap suite for (DATA_TYPE** DYNARGOUTVIEW_ARRAY2_FIXED[ANY], DIM_TYPE* DIM1)
*/
%typemap(in,numinputs=0,noblock=1)
(DATA_TYPE* DYNARGOUTVIEW_ARRAY2_FIXED[ANY], DIM_TYPE* DIM1)
{
size_t secondDim = $1_dim0;
size_t firstDim;
DATA_TYPE* tempData;
$1 = &tempData;
$2 = &firstDim;
}
%typemap(out)
void DYNARGOUTVIEW_ARRAY2_FIXED_##DATA_TYPECODE
{
npy_intp dims[2] = {(npy_intp) firstDim, (npy_intp) secondDim};
%#ifdef __GNUC__
__extension__
%#endif
PyObject * array = PyArray_SimpleNewFromData(2, dims, DATA_TYPECODE,(void*)(tempData));
if (!array) SWIG_fail;
$result = SWIG_Python_AppendOutput($result,array);
}
%enddef /* %numpy_ex_typemaps() macro */
%numpy_ex_typemaps(double , NPY_DOUBLE , size_t)
%numpy_ex_typemaps(float , NPY_FLOAT , size_t)
%numpy_ex_typemaps(signed char , NPY_BYTE , size_t)
当你应用这个类型映射时,你必须匹配两个类型映射,例如
%apply (double* DYNARGOUTVIEW_ARRAY2_FIXED[ANY], size_t* DIM1) {(double* coefs[3], size_t* length)};
%apply void DYNARGOUTVIEW_ARRAY2_FIXED_NPY_DOUBLE {void getFilter}
你的函数是这样的。
void getFilter(double* coefs[3], size_t* length);
从原始numpy中应该可以清楚地看到。
相关文章:
- 将类型映射到整数值后,如何在给定整数值的情况下恢复类型?
- C++17:在编译时将类型映射到整数值
- 为C++重载函数创建SWIG类型映射
- 返回各种类型的函数的SWIG类型映射
- 类型映射资源并将列表转换为矢量(并返回)
- 有没有办法在 C++17 中创建编译时类型映射以进行类型检查?
- 如何在OpenCV中使用双类型映射进行重新映射
- SWIG 多参数类型映射适用于函数,但如果有多个构造函数,则不适用于构造函数
- Swig:如何类型映射c ++字符串常量和python字符串?
- SWIG 类型映射的 python 列表到双倍 *
- SWIG -- 在扩展中使用类型映射
- 嵌入的多个变体类型映射
- SWIG类型映射中的内存释放
- 您将如何为 std::list<std::string> 创建一个类型映射,以在<String> List in C++ 中为 Java 在 SWIG 中创建?
- SWIG-为具有四个参数的函数创建一个类型映射,该参数传递一个字节数组
- 在C++中将类类型映射到其他类类型
- C func 到C++具有非双射参数类型映射的实例成员蹦床
- Swig 界面中的类型映射错误
- 如何在 swig 中引用类型映射类
- 将实例变量类型映射到模板成员函数