- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用我在 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/
你信吗?我有一个这样的循环(请原谅任何错误,我不得不大量编辑大量信息和变量名称,相信我它有效)。 ...旧示例已删除,请参见下面的代码... 如果我将那些中间的 str = "Blah\(odat.c
我正在做一个本地测试来比较 C# 中 String 和 StringBuilder 的 Replace 操作性能,但是对于 String 我使用了以下代码: String str = "String
我想知道为什么str += "A"和 str = str + "A"有不同的表现。 在实践中, string str = "cool" for(int i = 0; i approximately
我有一个类型列表 [("['106.52.116.101']", 1), ("['45.136.108.85']", 1)] 并想将其转换为 [('106.52.116.101', 1), ('45.
我有一个类型列表 [("['106.52.116.101']", 1), ("['45.136.108.85']", 1)] 并想将其转换为 [('106.52.116.101', 1), ('45.
我正在遍历 HashMap并通过一些本地变量中的模式匹配将值放入其中。 委托(delegate)者 fn lyrics_no_bottles(song_template:&mut String){
如果字符串(短语)中只有元音,它(对我而言)说True;否则说 False。我不明白为什么它总是返回 False,因为 (x >= x) 总是返回 True。我感谢任何人检查此查询的解决方案。 (st
我有代码以某种方式转换字符串引用,例如取第一个字母 trait Tr { fn trim_indent(self) -> Self; } impl Tr for &'a str { f
我正在学习指针,这是我的代码。我定义了一个指向 char(实际上是字符串)的指针 *str 和一个指向 int *a 的指针,它们的定义方式相同。我认为 str 和 a 都应该是一个地址,但是当我试图
为什么我会收到错误消息?我已经正确添加了类型,对吗? Invalid index type "str" for "Union[str, Dict[str, str]]"; expected type
你知道下面两个函数是否等价吗? function validate(str) { return ( ['null','','undefined'].indexOf(str) [v, valida
我正在解决这里的 Dataquest 问题:https://app.dataquest.io/m/293/data-cleaning-basics/5/removing-non-digit-chara
我有一个字符串列表,如下所示: ["A TB", "A-R TB", "B TB", "B-R TB", "C TB", "C-R TB"...] 但字符串的顺序是随机的。我如何编写一个将元素配对的函
我正在尝试将此函数从使用 split 改为使用 str.extract (正则表达式)。 def bull_lev(x): spl = x.rsplit(None, 2)[-2].strip(
给定这样的数据结构: [{'a':1, 'b': 2}, {'c':3 }, {'a':4, 'c':9}, {'d':0}, {'d': 0, 'b':6}] 目标是解析数据以产生: {'a': 2
给定这样的数据结构: [{'a':1, 'b': 2}, {'c':3 }, {'a':4, 'c':9}, {'d':0}, {'d': 0, 'b':6}] 目标是解析数据以产生: {'a': 2
s = 'someString' s = QTreeWidgetItem(s) print(s.text(0)) # 0 being 'column' 输出: 's' 如果我对另一
黑白有什么区别: function(char* str ) function(char* str[] ) function(char str[] ) 它们是如何被调用的(通过什么类型的string/c
我试过谷歌搜索但找不到准确的答案,所以请允许我尝试在这里提问。如果问题看起来不合适,请告诉我,我会删除它。 在 JS 中,您可以通过三种不同的方式编写特定的内置功能: 字符串长度 str.toStri
我有这段代码(我的 strlen 函数) size_t slen(const char *str) { size_t len = 0; while (*str) {
我是一名优秀的程序员,十分优秀!