簡體   English   中英

Numpy.i 用於不同類型的多個 arrays 的類型映射

[英]Numpy.i typemap for multiple arrays of different types

I have a C function which takes, among other arguments, 3 arrays of differents types: void create_signal(double ztab[], ..., int *pdata, ..., char ranging[], ...); 我想在使用 SWIG 的 Python 模塊中使用這個 function。

我已經想出了如何使用 C 函數,它接收一個帶有numpy.i類型映射的數組: %apply (double* INPLACE_ARRAY1, int DIM1) {(double xtab[], int length)}例如,

甚至 2 個相同類型的 arrays: apply (double* INPLACE_ARRAY1, int DIM1) {(double xtab[], int length_x), (double ytab[], int length_y)}

但我不知道如果 arrays 是不同的類型(在我的例子中是intdoublechar ),或者我是否應該重寫自己的類型映射,是否可以這樣做。

謝謝你的幫助 !

無論如何,這里有一個例子

測試.h

#pragma once

void multipleInputs(const float* data, const int nData,
                    const float* pos, const int nPositions, const int nDim);

測試.cpp

#include "test.h"

#include <cstdio>

void multipleInputs(const float* data, const int nData,
                    const float* pos, const int nPositions, const int nDim)
{
  if (nData > 0) {
    printf("%d'th element is: %f\n", (nData-1), data[nData-1]);
  }
  if ((nDim > 0) && (nPositions > 0)){
    printf("%d'th dimension of the %d'th element is: %f\n",
           (nDim-1), (nPositions-1), data[nData-1]);
  }
}

測試.i

%module example
%{
  #define SWIG_FILE_WITH_INIT
  #include "test.h"
%}

%include "numpy.i"

%init {
  import_array();
}

%apply (float* IN_ARRAY1, int DIM1) \
{(const float* data, const int nData)}

%apply (float* IN_ARRAY2, int DIM1, int DIM2) \
{(const float* pos, const int nPositions, const int nDim)};

%include "test.h"

安裝程序.py

import numpy

from setuptools import setup, Extension

setup(name="example",
      py_modules=["example"],
      ext_modules=[Extension("_example",
                             ["test.i","test.cpp"],
                             include_dirs = [numpy.get_include(), '.'],
                             swig_opts=['-c++', '-I.'],
      )]
)

測試.py

import example
example.multipleInputs(np.ones((10),dtype=np.float32), 
                       np.ones((10,10),dtype=np.float32))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM