Hello,
I am trying to use a class namely "Matrix" which is written in a cython script "matrix.pyx", in another cython script "Sampling_debugger.pyx". Here you can find part of my codes
matrix.pyx
import ctypes
cimport numpy as np
cimport cython
from libc.math cimport log, exp, pow
from libc.stdint cimport *
from cython.parallel import parallel, prange
cdef class Matrix:
cdef vector[double] matrix
cdef int _rows
cdef int _columns
def __cinit__(self, int rows=0, int columns=0, bint Identity=False):
self._rows=rows
self._columns=columns
self.matrix.resize(rows*columns)
if Identity:
self._IdentityMatrix()
property rows:
def __get__(self):
return self._rows
def __set__(self, int x):
self._rows = x
property columns:
def __get__(self):
return self._columns
def __set__(self, int y):
self._columns = y
cpdef double getVal(self, int r, int c):
return self.matrix[r*self._columns+c]
cpdef void setVal(self, int r, int c, double v):
self.matrix[r*self._columns+c] = v
matrix.pxd
from libcpp.vector cimport vector
cdef class Matrix:
cdef public:
int _rows
int _columns
bint Identity
cpdef double getVal(self, int r, int c)
cpdef void setVal(self, int r, int c, double v)
Sampling_debugger.pyx
import numpy as np
cimport numpy as np
cimport cython
from cython_gsl cimport *
from matrix cimport *
from matrix import *
cdef extern from "stdlib.h":
long rand()
void srand(int seed)
cdef extern from "time.h":
ctypedef long time_t
time_t time(time_t*)
cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)
srand(time(NULL))
cdef Matrix initialWeight(double alpha, int K ):
cdef:
Matrix mu = Matrix(K,1)
int i
int seed = rand()
#generate random seed
gsl_rng_set(r, seed)
cdef double prev_mu
for i from 0 <= i < K:
if i >= 1:
prev_mu *= gsl_ran_beta(r, alpha, 1)
else:
prev_mu = gsl_ran_beta(r, alpha, 1)
mu.setVal( i, 0, prev_mu)
return mu
cpdef void run( int k, double alpha):
cdef:
int i
Matrix mu = initialWeight(alpha, k )
for i from 0 <= i < k:
print mu.getVal(i,0)
return
and eventually, my "setup.py" script is:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
from numpy import get_include
import numpy
import cython_gsl
modules = [
Extension("matrix", sources=["matrix.pyx"],language="c++",libraries=cython_gsl.get_libraries(),include_dirs=[numpy.get_include(), cython_gsl.get_include()]),
Extension("Sampling_debugger", sources=["Sampling_debugger.pyx"],language="c++",libraries=cython_gsl.get_libraries(),include_dirs=[numpy.get_include(), cython_gsl.get_include()])
]
setup(
cmdclass={'build_ext': build_ext},
ext_modules=cythonize(modules)
)
when I compile my scripts, I get these error messages
cdef class Matrix:
cdef vector[double] matrix
^
------------------------------------------------------------
matrix.pyx:55:25: C attributes cannot be added in implementation part of extension type defined in a pxd
Error compiling Cython file:
------------------------------------------------------------
...
cdef class Matrix:
cdef vector[double] matrix
cdef int _rows
^
------------------------------------------------------------
matrix.pyx:56:14: C attributes cannot be added in implementation part of extension type defined in a pxd
Error compiling Cython file:
------------------------------------------------------------
...
cdef class Matrix:
cdef vector[double] matrix
cdef int _rows
^
------------------------------------------------------------
matrix.pyx:56:14: '_rows' redeclared
If I individually compile "matrix.pyx" without the header file "matrix.pxd", it gets compiled without any error. I will appreciate if one of the experts here explains what I am doing wrong?!!
Thanks in advance.