2017-12-23 16 views
1

ベクトルを初期化するためにPythonから複素数のリストを渡す必要があります。いくつかの演算の後に、複素数のベクトルを渡す必要がありますPython。ここでSWIG:複素数のベクトルをC++からPythonに渡す方法

私の試みです:

example.i

%module example 

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

%include "std_vector.i" 
%include <complex.i> 
%include <std_complex.i> 
namespace std { 
    %template(DoubleVector) vector<double>; 
    %template(ComplexVector) vector<std::complex<double> >; 
} 
%include "example.h" 

example.cpp

#include "example.h" 


void my_class::set_value(Complex a, std::vector<Complex> v) 
{ 
    com1 = {1,2}; 
    comV = v; 
} 

void my_class::print_value() 
{ 
    std::cout << com1<< std::endl; 
    for (int i=0; i<comV.size(); i++) 
     std::cout<<comV[i]<<std::endl; 
} 

example.h

#include <iostream> 
#include <cstdio> 
#include <vector> 
#include <omp.h> 
#include <complex> 

typedef std::complex<double> Complex; 

class my_class 
{ 
    private:  
     Complex com1; 
     std::vector<Complex> comV; 
    public: 
     my_class(){ } 
     void set_value(Complex a, std::vector<Complex> v); 
     void print_value(); 
}; 

私がコンパイルされ、私に次を与えますテストしようとするとエラーが発生する:

import example 
c = example.my_class() 
c.set_value(complex(1.0,3.5), [complex(1,1.2), complex(2.0,4.2)]) 
c.print_value() 

return _example.my_class_set_value(self, a, v) 
TypeError: in method 'my_class_set_value', argument 3 of type 'std::vector< Complex,std::allocator<Complex> >' 

私は間違いをどこで犯したのですか?

答えて

1

SWIGファイルに明示的なインスタンス化ComplexVectorを入れなければなりません。それ以外の場合は動作しません。あなたが間違っているのは、std::complex<double>(あなたは名前空間を忘れてしまった)の資格がないということでした。

%module example 

%{ 
#include "example.h" 
%} 

%include "std_vector.i" 
%include "std_complex.i" 
namespace std { 
    %template(DoubleVector) vector<double>; 
    %template(ComplexVector) vector<std::complex<double>>; 
} 
%include "example.h" 

Futhermore my_class::print_value()は不当命令につながるreturn文が欠落しています。

+0

それは働いた。ありがとう@Henri – Abolfazl

関連する問題