Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 定义某些函数运算符时出现PicklingError_Python_Python 2.7_Multiprocessing_Pickle - Fatal编程技术网

Python 定义某些函数运算符时出现PicklingError

Python 定义某些函数运算符时出现PicklingError,python,python-2.7,multiprocessing,pickle,Python,Python 2.7,Multiprocessing,Pickle,我正在使用一个基于Python 2.7的数学软件SageMath。下面的简化代码是从初始估计开始递归计算两个函数。当我尝试在代码中引入数值积分运算符,而不是使用内置函数时,它给出了一个错误: > PicklingError: Can't pickle <type 'function'>: attribute lookup __ builtin__.function failed 据我所知,通过搜索网页和阅读文档,这个pickle错误要么是由于整型运算符a函数不可pickle,

我正在使用一个基于Python 2.7的数学软件SageMath。下面的简化代码是从初始估计开始递归计算两个函数。当我尝试在代码中引入数值积分运算符,而不是使用内置函数时,它给出了一个错误:

> PicklingError: Can't pickle <type 'function'>: attribute lookup __ builtin__.function failed
据我所知,通过搜索网页和阅读文档,这个pickle错误要么是由于整型运算符a函数不可pickle,要么是由于它的参数不可pickle。我试图使算符本身可拾取,但没有成功,但我想问题应该是因为积分算符本身是其被积函数的一个函数,而由于它是一个算符,所以从一开始就不能确定其被积函数中的函数,那么,操作符的第一个参数,它本身是一个没有在顶层明确定义的函数,可能是操作符没有在整体pickable中的原因?你知道如何克服这个错误吗


注意。主代码比这里提供的最低限度的代码复杂得多,因此我需要定义一个整数运算符来防止代码的可读性。

我尝试先重现错误,然后进一步将错误最小化。但是您提供的代码没有运行,而且不清楚诸如重置、忘记、var和函数之类的东西是从哪里来的。那么,你能不能扩展它,让它运行并失败,或者给出一个运行的进一步最小化的示例?@Anthon,我猜代码对你来说是不可复制的,因为它不是纯Python,而是SageMath,一个基于Python的软件,这应该可以解释为什么像sin等函数不需要调用额外的库。在处理符号变量和假设时,还可以使用“reset”、“forget”和“var”。但是,当我使用符号内置积分运算符“integral”时,代码可以工作,只有当我使用自己的积分运算符numint时,代码才会失败。这有意义吗?谢谢你提供的细节。我编辑了你的OP,因此类型函数实际上可以被看到,而不是作为HTML标记被抑制-你是否尝试过将numint简化为类似于returna的东西,以查看它是否有效?你看了吗http://ask.sagemath.org/question/1229/how-to-save-a-function-in-sage,我只是在发现你的部分错误信息丢失后才发现,但在编辑帖子时,它是可见的。
reset()
forget()
from multiprocessing import Pool, cpu_count

#variables
var('x,y, x1,y1')
N=5   #number of iterations
var('q')

#functions' name definition (without specifying the rules)  # n is the level of estimation
U0=[]
U1=[]
for n in range(N+1):
    U0.append(function('U0_%s' %i, x,y))
    U1.append(function('U1_%s' %i, x,y, x1,y1))

#initial estimation of the functions
U0[0]=sin(x+y)
U1[0]=sin(x1+y1)*cos(x-y)


#numerical integrator
num=20.0  # at each call of the int function h is assigned (b-a)/num, so assigned to num here
def numint(f, x, a, b, h):
    #pickle_function(f)
    integ = 0.5*h*(f(x=a) + f(x=b))
    for i in range(1,num):
        integ = integ + h * f(x=a+i*h)
    return integ


#the integral operators
def Ix(f,x):
    return pool.apply( numint, (f,x,-1.0,+1.0,2.0/num) )
def Iy(f,y):
    return pool.apply_async( numint, (f,y,-1.0,+1.0,2.0/num) )
def IR(f,x,y):
    return Iy(Ix(f,x),y)
def N0(n,f0,f1):
    return f0[n] + IR(f1[n],x1,y1) + IR(IR(f1[n],x,y),x1,y1)
def N1(n,f0,f1):
    return f1[n] + IR(f0[n],x,y) - IR(IR(f1[n],x,y),x1,y1)


#the calculations
pool = Pool(processes=cpu_count())
for n in range(N):
    worker0 = N0(n,U0,U1)
    worker1 = N1(n,U0,U1)
    U0[n+1] = U0[n] - worker0.get()
    U1[n+1] = U1[n] - worker1.get()
    show(U0[n+1])
    show(U1[n+1])