gpt4 book ai didi

python - 在类内部创建类的副本

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:32 25 4
gpt4 key购买 nike

我想在 python 中对代码进行 cythonize 以加快代码速度。在下面,您可以看到我试图让 cython 可以理解我的 python 类:

import numpy as np
cimport numpy as np
ctypedef np.double_t DTYPE_T
cpdef double std_G,v_c
std_G=4.3e-9 # Newton's const in Mpc (km/s)^2 M_sol^{-1}
v_c = 299792.458 #km/s

cdef extern from "math.h":
double log(double) nogil
double sqrt(double) nogil


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 do_callback(double x, void* params):
return (<ComovingDistMemoization>params).eval(x)


cdef class ComovingDistMemoization(object):
cdef list _memotable
cdef cosmolgy
def __cinit__(self, cosmology, memotable = None):

if memotable is None:
self._memotable = []

self._memotable = memotable
self.cosmology = cosmology
def __call__(self, double z):


if z in self._memotable:
return self._memotable[z]

def eval(z):

return 1./sqrt(self.cosmology.hubble2(z))
cdef gsl_integration_workspace* w =gsl_integration_workspace_alloc(1000)
cdef gsl_function F

F.function = &do_callback
F.params = <void*>self
cdef double result = 3, error = 5
cdef double y, err, dist
gsl_integration_qags (&F, 0, z, 0, 1e-7, 1000, w, &result, &error)
y, err = result, error

gsl_integration_workspace_free(w)

dist = self.cosmology.v_c * y #to get proper units, ie to put in the hubble length

self._memotable[z] = dist

return dist


cdef class cosmology(object):
cdef comovingdist
cdef public double omega_m, omega_l, h, w, omega_r, G, v_c
cdef double H0, hubble_length
def __init__(self, omega_m = 0.3, omega_l = 0.7, h = 0.7, w = -1, omega_r = 0., G = std_G):

self.omega_m = omega_m
self.omega_l = omega_l
self.omega_r = omega_r
self.h = h
self.w = w
self.G = G
self.v_c = v_c

self.comovingdist = ComovingDistMemoization(self)
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

def hubble2(self, double z):
cdef double inv_a
inv_a = 1.+z
return (self.omega_r*inv_a**4 + self.omega_m*inv_a**3 + \
self.omega_l*(inv_a**(3*(1+self.w))) + (1 - self.omega_m - self.omega_l - self.omega_r)*inv_a**2)*self.H0**2

property hubble_length:
def __get__(self):
return self.v_c / self.H0

def rho_crit(self, double z):
return 3.*self.hubble2(z)/(8*np.pi*self.G)


def angulardist(self, double z, double z2 = None):

if z2 is None:
return self.comovingdist(z) / (1+z)

return (self.comovingdist(z2) - self.comovingdist(z)) / (1+z2)

然而,其中一个引发错误的地方,到目前为止我找不到任何替代品,我的调查刚刚达到这一点,即 cython 不支持 __copy__(self) 函数。第一个问题:如何在 cython 中复制一个 class 的方法?我用 pickle.Pickler 读到,可以为类的实例提供替代,但我不知道它应该如何生成类的 copy类(class)里面??更新:将 __copy__ 替换为以下代码是否正确:

def __reduce__(self):
return (self.__class__, (), self.__getstate__())
def __getstate__(self):
return (self.omega_m, self.omega_l, self.omega_r, self.h, self.w, self.G, self.v_c)
def __setstate__(self, data):
(self.omega_m, self.omega_l, self.omega_r, self.h, self.w, self.G, self.v_c) = data

我的第二个问题是关于 property,我是否在 cython 中定义了属性,或者我还需要有一个 __set__ 函数?

我的最后一个问题:当我使用 ComovingDistMemoization 调用 cosmology 类的所有实例时,它们返回零。我想知道我在 ComovingDistMemoization 类中调用例如 cosmology 类的方式是否导致了问题,并且无法在它们之间传递信息?

最佳答案

回答你的第二个问题,在你的情况下你不需要 __set__ 函数,而且你使用 __get__ 概念的方式似乎有错误.要获取的属性名称必须设置如下:

def get_H0(self):
return 100*self.h #km/s/MPC
H0 = property(fget=get_H0)

def get_hubble_length(self):
return self.v_c / self.H0
hubble_length = property(fget=get_hubble_length)

编辑:我不知道 @property 不能与 Cython 一起使用

关于python - 在类内部创建类的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24615342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com