2

カスタム クラス オブジェクトを保持する C++ ベクトル メンバーを持つクラスがあります。このベクトルを python からリストとして読み取れるようにしたいのですが、どのような方法でもそれを行うことができませんでした。

私のヘッダー:

from __future__ import division

import cython               
from libcpp.vector cimport vector   #this import the C++ vector, which
                                    #is compatible with python lists
import numpy as np
cimport numpy as np

#@cython.boundscheck(False) #this line remove some correct checking for
                            #for bounds, which makes it hard to debug
                            #but also faster

これが私のカスタムクラスです:

ctypedef class boid:
    """Placeholder for the boids"""
    cdef public vector[double] pos  #position of the boid
    cdef public vector[double] vel  #velocity of the boid

    def __init__(self, vector[double] pos_):
        self.pos = pos_

これはベクトルを持つ他のクラスです。

cdef class csopsim:
    """This is the simulation"""
    #declaring c variable types
    cdef vector[boid] boids  #list of boids

    def __init__(self,int scenario):
        #setting default values
        self.BOX_SIZE = 640
        self.BOX = float(self.BOX_SIZE)

        self.NUM_MALES = 10
        for x in xrange(self.NUM_MALES):
            self.boids.push_back(boid(0,np.random.uniform(350,450,2)))

これは問題なくコンパイルされますが、明らかに csopsim.boids を取得しようとすると、属性なしのエラーがスローされます。次のように変更すると

cdef public vector[boid] boids

コンパイルされません。メソッドを作成する場合

def getboids(self):
    return self.boids

コンパイルされません。メソッドを作成する場合

cdef vector[boid] getboids(self):
    return self.boids

コンパイルされますが、Python からメソッドを呼び出そうとすると、AttributeError: 'csopsim.csopsim' object has no attribute 'getboids' がスローされます。この問題に対するシンプルで簡単な解決策があることを願っています:)

4

1 に答える 1