C/C++ で void 関数を記述し、SWIG を使用して Python/Numpy にラップすることができました。これは、パラメーターとして(int* INPLACE_ARRAY1, int DIM1)
受け取り、int* vector
このベクトルでいくつかの計算を行い、同じベクトルで結果を上書きし、この結果は内部で利用可能でしたPython のオブジェクト。次のように:
extern "C" void soma_aloc(int* vetor, int tamanho)
{
int m = 0;
int* ponteiro = new int[tamanho];
for(m = 0; m < tamanho; m++)
{
ponteiro[m] = vetor[m];
};
for(m = 0; m < tamanho; m++)
{
ponteiro[m] = ponteiro[m] * 10;
};
for(m = 0; m < tamanho; m++)
{
vetor[m] = ponteiro[m];
};
delete [] ponteiro;
};
これは、 int および double 配列へのポインターを SWIG で and を使用してラップする方法を学習するためのテストでtypemaps (DATA_TYPE* INPLACE_ARRAY1, int DIM1)
あり(DATA_TYPE* INPLACE_ARRAY2, int DIM1, int DIM2)
、うまく機能しました。
vec1 = numpy.array(['a','a','a'])
しかし問題は、char/string Numpy ベクトル (ベクターやのようなもの) で同じアイデアを試してみたことです。numpy.array(['a','a','a'],dtype=str)
各位置を のように変更しますが(['b','b','b'])
、Python は を示しin method 'vector_char2D', argument 1 of type 'char *'
ています。char/string で同じことを行うことは可能ですか?
.cpp:
extern "C" void vetor_char2D(char* vetorchar, int tamanho_vetor)
{
for(int i = 0; i < tamanho_vetor; i++)
{
vetorchar[i] = 'b';
};
};
.i:
%module testestring
%include stl.i
%include std_string.i
%{
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
#include <string>
#include <iostream>
#define SWIG_FILE_WITH_INIT
#include "testestring.hpp"
%}
%include "numpy.i"
%init %{
import_array();
%}
%apply (char* INPLACE_ARRAY1, int DIM1) {(char* vetorchar, int tamanho_vetor)}
%include "testestring.hpp" (just the header of the above function vetor_char2D)
%clear (char* vetorchar, int tamanho_vetor);
私はSWIGの経験が非常に少ないです。char*
、char**
および/またはstd::string*/std::string**
?を使用してこれを行うことができます。前もって感謝します!