I have a question regarding to declaration of a method of a class. I have written the cosmology class and now I would like to declare a method of this class (Da) since I would like to use it in another class but since my C knowledge is not enough and by googling my problem I couldn't find any answer I will appreciate if I will get a tip:

ctypedef double* (* Da)(double* z, double z_ref, int length)

cdef extern from "gsl/gsl_math.h":

ctypedef struct gsl_function:

double (* function) (double x, void * params)

void * params

cdef extern from "gsl/gsl_integration.h":

ctypedef struct gsl_integration_workspace

gsl_integration_workspace * gsl_integration_workspace_alloc(size_t n)

void gsl_integration_workspace_free(gsl_integration_workspace * w)

int gsl_integration_qags(const gsl_function * f, double a, double b, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, double *result, double *abserr)

cdef double func_callback(double x, void* params):

return (<cosmology>params).__angKernel(x)

cdef class cosmology(object):

cdef public double omega_m, omega_l, h, w, omega_r, G, v_c

cdef object omega_c

def __init__(self,double omega_m = 0.3, double omega_l = 0.7, double h = 0.7, double w = -1, double omega_r = 0., double G = std_G):

self.omega_m = omega_m

self.omega_l = omega_l

self.omega_r = omega_r

self.omega_c = (1. - omega_m - omega_l)

self.h = h

self.w = w

self.G = G

self.v_c = v_c

def __copy__(self):

return cosmology(omega_m = self.omega_m, omega_l = self.omega_l, h = self.h, w = self.w, omega_r = self.omega_r, G = self.G)

property H0:

def __get__(self):

return 100*self.h #km/s/MPC

cpdef double a(self, double z):

return 1./(1.+z)

cpdef double E(self, double a):

return (self.omega_r*a**(-4) + self.omega_m*a**(-3) + self.omega_c*a**(-2) + self.omega_l)**0.5

<at> cython.boundscheck(False)

<at> cython.wraparound(False)

<at> cython.cdivision(True)

cdef double __angKernel(self, double x):

return self.E(x**-1)**-1

<at> cython.cdivision(True)

<at> cython.boundscheck(False)

<at> cython.wraparound(False)

cdef double* Da(self, double* z, double z_ref=0, int N):

cdef gsl_integration_workspace* w =gsl_integration_workspace_alloc(1000)

cdef gsl_function F

F.function = &func_callback

F.params = <void*>self

cdef double result = 3, error = 5

cdef double err, rk, zs, omc, tmp

omc=self.omega_c

cdef double[::1] d = np.empty((N))

cdef double* Dptr = &d[0]

cdef int i

for i in range(N):

if z[i] < 0:

raise ValueError("Redshift z must not be negative")

if z[i] < z_ref:

raise ValueError("Redshift z must not be smaller than the reference redshift")

gsl_integration_qags(&F, z_ref+1, z[i]+1, 0, 1e-7, 1000, w, &result, &error)

tmp, err = result, error

# check for curvature

rk = (fabs(omc))**0.5

if (rk*tmp > 0.01):

if omc > 0:

tmp = sinh(rk*tmp)/rk

if omc < 0:

tmp = sin(rk*tmp)/rk

Dptr[i]=tmp/(z[i]+1.)

gsl_integration_workspace_free(w)

return d

Thanks in advance.

You received this message because you are subscribed to the Google Groups "cython-users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to

.

.

File "/usr/local/lib/python2.7/site-packages/Cython-0.19.1-py2.7-linux-x86_64.egg/Cython/Compiler/Parsing.py", line 1916, in p_statement_list

pos = s.position()

AttributeError: 'Cython.Compiler.Scanning.PyrexScanner' object has no attribute 'position'

I googled and there is similar topic before, but I still don't know how to solve my problem. Please help. Thanks!

That's because position() is compiled as a cdef method here:

https://github.com/cython/cython/blob/master/Cython/Plex/Scanners.pxd