gpt4 book ai didi

python - 从 Julia 调用 Python 时出现 PyCall 错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:00:43 25 4
gpt4 key购买 nike

我正在玩弄 Julia,我正在使用 Sympy,我认为它使用 PyCall 来调用 Python。

当我运行下面的脚本时,出现了一个很长的错误。在这里发布所有内容太长了,但这里是它的开始:

LoadError: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, 

PyPtr), o, arg, C_NULL)) <type 'exceptions.RuntimeError'>
RuntimeError('maximum recursion depth exceeded while calling a Python object',)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\cache.py", line 93, in wrapper
retval = cfunc(*args, **kwargs)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\compatibility.py", line 809, in wrapper
result = user_function(*args, **kwds)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\function.py", line 427, in __new__
result = super(Function, cls).__new__(cls, *args, **options)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\cache.py", line 93, in wrapper
retval = cfunc(*args, **kwargs)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\compatibility.py", line 809, in wrapper
result = user_function(*args, **kwds)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\function.py", line 250, in __new__
evaluated = cls.eval(*args)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\functions\elementary\integers.py", line 25, in eval
if arg.is_imaginary or (S.ImaginaryUnit*arg).is_real:
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\decorators.py", line 91, in __sympifyit_wrapper
return func(a, b)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\decorators.py", line 132, in binary_op_wrapper
return func(self, other)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\expr.py", line 140, in __mul__
return Mul(self, other)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\cache.py", line 93, in wrapper
retval = cfunc(*args, **kwargs)
File "d:\Users\OEM\AppData\Local\JuliaPro-0.6.0.1\pkgs-0.6.0.1\v0.6\Conda\deps\usr\lib\site-packages\sympy\core\compatibility.py", line 809, in wrapper
result = user_function(*args, **kwds)

正如您可能看到的那样,到最后它会重复:看到最后的第 93 行,然后是第 140 行,然后是第 93 行...

这是我的代码:

function oddPeriodSquareRoots()
#=
Get the length of the continued fraction for square root of for the number i.
E.g. √7=[2;(1,1,1,4)]
=#


irrationalNumber, intPart, fractionalPart = symbols(string("irrationalNumber intPart fractionalPart"))

for i in [6451]

# For perfect squares, the period is 0
irrationalNumber = BigFloat(sqrt(BigFloat(i)))
if irrationalNumber == floor(irrationalNumber)
continue
end

# Get the continued fraction using symbolic programming
irrationalNumber = sqrt(Sym(i))

continuedFractionLength = 0
while true

intPart = Sym(BigInt(floor(irrationalNumber)))
if continuedFractionLength == 0
firstContinuedFractionTimes2 = intPart*2
end

continuedFractionLength += 1
if intPart == firstContinuedFractionTimes2
break
end

fractionalPart = irrationalNumber - intPart
irrationalNumber = 1 / fractionalPart

end

continuedFractionLength -= 1 # We ignore the first term.


end


return continuedFractionLength
end

此例程计算某个数的平方根的连分数的长度。对于数字 6451,它给出了错误。

所以我的问题是这可以解决吗?

最佳答案

很高兴找到了递归限制解决方案。这是以前从未见过的。这条评论是关于如何简化您的 SymPy 代码的,因为您似乎对此感到困惑。基本上,你只需要让你的初始值成为符号,然后 Julia 的方法应该(在大多数情况下)处理剩下的事情。这是一个轻微的重写:

using SymPy

使用 PyCall@pyimport 系统sys.setrecursionlimit(10000)

"""获取数字 i 的平方根的连分数的长度。例如。 √7=[2;(1,1,1,4)]"""函数 oddPeriodSquareRoots(n)

i = Sym(n)
# For perfect squares, the period is 0
continuedFractionLength = 0

irrationalNumber = sqrt(i)
if is_integer(irrationalNumber)
return continuedFractionLength
end

# Get the continued fraction using symbolic programming

while true

intPart = floor(irrationalNumber)
if continuedFractionLength == 0
firstContinuedFractionTimes2 = intPart*2
end

continuedFractionLength += 1
if intPart == firstContinuedFractionTimes2
break
end

fractionalPart = irrationalNumber - intPart
irrationalNumber = 1 / fractionalPart

end

continuedFractionLength -= 1 # We ignore the first term.


return continuedFractionLength

结束

关于python - 从 Julia 调用 Python 时出现 PyCall 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45808738/

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