gpt4 book ai didi

Python(sympy)类型错误: cannot concatenate 'str' and 'Add' objects

转载 作者:太空宇宙 更新时间:2023-11-03 19:34:01 27 4
gpt4 key购买 nike

我正在尝试使用我在 lambda 中使用 sympy.Eq(func, var) 生成的方程。它似乎正在返回“添加”对象的列表,我不知道如何使用它。在使用eval之前,我尝试将类型转换为str,并且尝试了from sympy import I,我认为这与该问题有关。

这是代码和错误消息:

py solid.py -v y = (x-1.0)(x-3.0)*2 1.0 3.0 -axis y --method disk

<小时/>

这是一个运行示例:

import sys
import argparse
import math
from scipy import integrate
from sympy import Symbol, Eq, solve, I
import numpy
import pylab

parser = argparse.ArgumentParser(description='Find the volume of the solid of rotation defined. Use * for multiplication and ** to raise a power. Bounds are set equal to the implicit variable. Trailing .0\'s must be used for constants, and parentheses must be escaped ( e.g. \(x-2\) ). Answers are accurate to 6 decimals')
parser.add_argument('var', choices='xy', help='x|y')
parser.add_argument('=', metavar='=', choices='=')
parser.add_argument('equation', help='continous function')
parser.add_argument('a', type=float, help='bound \'a\'')
parser.add_argument('b', type=float, help='bound \'b\'')
parser.add_argument('-axis', choices='xy', help='axis of revolution (default == x)')
parser.add_argument('-m', choices='ds', help='method (disk/shell)')
parser.add_argument('-g', action='store_true', help='show graph')
parser.add_argument('-v', action='store_true', help='verbose (for debugging)')
args = parser.parse_args()


y = Symbol('y')
x = Symbol('x')

def solve_for_implicit(func, var):
if var == 'x':
equation = Eq(eval(func), x)
func = solve(equation, y)
else:
equation = Eq(eval(func), y)
func = solve(equation, x)

return func

def volume(var, func, a, b, axis=None, method=None):
if axis == None: axis = 'x'
if method == 's': method = 'shell'
if method == 'd': method = 'disk'

if var == axis and axis == 'x':
if args.v: print 'x = y about x'

if not method == 'disk':
pass
else:
pass

elif var == axis and axis == 'y':
# SHELL METHOD
if args.v: print 'y = x about y'
if not method == 'disk':
# this is what should be used for y = x about y
if args.v: print 'using SHELL method'
func = eval('lambda x: ' + 'x*(' + func + ')')
integral = integrate.quad(func, a, b)
if args.v: print 'integral from', a, 'to', b, '=', integral[0]
answer = 2 * math.pi * integral[0]
if args.v: print '2*pi*', integral[0], '=', answer
else:
func = solve_for_implicit(func, var)
print func[0]
func = eval('lambda y: ' + '(' + str(func[0]) + ')**2')
integral = integrate.quad(func, a, b)
if args.v: print 'integral=', integral[0]
answer = math.pi * integral[0]

elif not var == axis and axis == 'y':
# DISK METHOD
if args.v: print 'x = y about y -- '
if not method == 'shell':
pass
else:
pass

elif not var == axis and axis == 'x':
# DISK
if args.v: print 'y = x about x --',
if not method == 'shell':
pass
else:
pass

return answer


print volume(args.var, args.equation, args.a, args.b, args.axis, args.m)
if args.g: graph(args.equation, args.a, args.b)

Traceback (most recent call last):
File "solid.py", line 136, in print volume(args.var, args.equation, args.a, args.b, args.axis, args.m) File "solid.py", line 75, in volume func = eval('lambda y: ' + '(' + func[0] + ')**2') TypeError: cannot concatenate 'str' and 'Add' objects

当我首先尝试将其类型转换为 str() 时,我得到:

File "solid.py", line 136, in print volume(args.var, args.equation, args.a, args.b, args.axis, args.m) File "solid.py", line 76, in volume integral = integrate.quad(func, a, b) File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadpack.py", line 185, in quad retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points) File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadpack.py", line 249, in _quad return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) quadpack.error: Supplied function does not return a valid float.

此外,print func[0] 给出:

2.33333333333333 + (0.296296296296298 - y/2 + (-0.0877914951989024 + (0.592592592592595 - y)2/4)(1/2))(1/3)*(1/2 - I*3(1/2)/2) + 0.444444444444444/((1/2 - I*3*(1/2)/2)(0.296296296296298 - y/2 + (-0.0877914951989024 + (0.592592592592595 - y)2/4)(1/2))**(1/3))

感谢大家的帮助,很抱歉之前没有提供完整的脚本。

最佳答案

“在使用 eval 之前,我尝试将类型转换为 astr,并且尝试使用 sympy import I,我认为这与问题有关”

是的,毫无疑问这正是问题所在。我确信 Numpy 有关于如何使用添加对象的文档。将它们转换为字符串然后评估它们绝对不是正确的解决方案。

除了阅读文档之外,您还可以使用 dir(func) 检查对象以查看其具有哪些功能和属性。在解释器中还有一个漂亮的帮助功能,因此您可以键入 help(func) 并获取信息。

关于Python(sympy)类型错误: cannot concatenate 'str' and 'Add' objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4447426/

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