I have an annoying error message that I cannot figure it out. The following code (in Python 3) gives a correct results:
我有一个恼人的错误消息,我无法弄明白。以下代码(在Python 3中)给出了正确的结果:
from sympy import *
x, y, z, t = symbols('x y z t')
expr = cos(x) + 1
print(expr.subs(x, y))
The results are:
结果是:
cos(y) + 1
However, when expr is modified or different say,
但是,当expr被修改或者说不同时,
from sympy import *
x, y, z, t = symbols('x y z t')
expr = 1
print(expr.subs(x, y))
The output is the error message saying,
输出是错误消息,
AttributeError: 'int' object has no attribute 'subs'
How can I fix this? I need subs to handle symbolic expression and numerical when necessary (say symbolic expression was modified in such a way that result is numerical). Regards.
我怎样才能解决这个问题?我需要sub来处理符号表达式和必要时的数值(比如说符号表达式被修改为结果是数字的)。问候。
3 个解决方案
#1
2
The 1
in your second example is not a SymPy number, just a regular Python integer.
第二个示例中的1不是SymPy编号,只是常规Python整数。
You can use the S()
(or sympify()
) function to turn it into a SymPy number.
您可以使用S()(或sympify())函数将其转换为SymPy数字。
The official docs have a Gotchas entry that describes your problem and gives a few more examples, and a description of what sympify
can do.
官方文档有一个描述您的问题的Gotchas条目,并提供了一些示例,以及对可以做什么的描述。
#2
1
1
is not a Sympy expression. 1
is just a regular Python int. You will need to wrap it in a Sympy Integer if you want to perform substitutions on it. (Also, stop using import *
.):
1不是Sympy表达式。 1只是一个普通的Python int。如果要对其执行替换,则需要将其包装在Sympy Integer中。 (另外,停止使用import *。):
import sympy
expr = sympy.Integer(1)
Note that Sympy Integers display identically to regular ints, so if some 1
s seem to have a subs
method and others don't, those 1
s might not all be the same type. You can check with type
:
请注意,Sympy Integers与常规整数的显示方式相同,因此如果某些1s似乎有一个subs方法而另一些则没有,那么这些1可能并非都是同一类型。你可以检查类型:
In [14]: x = 1
In [15]: y = sympy.Integer(1)
In [16]: x
Out[16]: 1
In [17]: y
Out[17]: 1
In [18]: type(x)
Out[18]: int
In [19]: type(y)
Out[19]: sympy.core.numbers.One
#3
0
As in the other answers, 1
is not a SymPy integer, it's a Python integer. Therefore it doesn't have SymPy's methods.
与其他答案一样,1不是SymPy整数,而是Python整数。因此它没有SymPy的方法。
You could declare expr
as one of the following to make it work:
您可以将expr声明为以下之一以使其工作:
expr = S.One
- expr = S.One
expr = S(1)
- expr = S(1)
expr = Integer(1)
- expr =整数(1)
SymPy integers are instances of the class Integer
, whereas Python integers are int
objects.
SymPy整数是Integer类的实例,而Python整数是int对象。
There is an additional trick to avoid this step: execute isympy -i
. The -i
parameter adds an additional input layer that replaces all digits with Integer(digits)
objects, as an example:
还有一个技巧可以避免这一步骤:执行isympy -i。 -i参数添加一个额外的输入层,用Integer(数字)对象替换所有数字,例如:
In [1]: 1
Out[1]: 1
In [2]: In[1]
Out[2]: u'Integer (1 )'
That is, inspecting with IPython what the input 1 is, gives u'Integer (1 )'
.
也就是说,用IPython检查输入1是什么,给出u'Integer(1)'。
#1
2
The 1
in your second example is not a SymPy number, just a regular Python integer.
第二个示例中的1不是SymPy编号,只是常规Python整数。
You can use the S()
(or sympify()
) function to turn it into a SymPy number.
您可以使用S()(或sympify())函数将其转换为SymPy数字。
The official docs have a Gotchas entry that describes your problem and gives a few more examples, and a description of what sympify
can do.
官方文档有一个描述您的问题的Gotchas条目,并提供了一些示例,以及对可以做什么的描述。
#2
1
1
is not a Sympy expression. 1
is just a regular Python int. You will need to wrap it in a Sympy Integer if you want to perform substitutions on it. (Also, stop using import *
.):
1不是Sympy表达式。 1只是一个普通的Python int。如果要对其执行替换,则需要将其包装在Sympy Integer中。 (另外,停止使用import *。):
import sympy
expr = sympy.Integer(1)
Note that Sympy Integers display identically to regular ints, so if some 1
s seem to have a subs
method and others don't, those 1
s might not all be the same type. You can check with type
:
请注意,Sympy Integers与常规整数的显示方式相同,因此如果某些1s似乎有一个subs方法而另一些则没有,那么这些1可能并非都是同一类型。你可以检查类型:
In [14]: x = 1
In [15]: y = sympy.Integer(1)
In [16]: x
Out[16]: 1
In [17]: y
Out[17]: 1
In [18]: type(x)
Out[18]: int
In [19]: type(y)
Out[19]: sympy.core.numbers.One
#3
0
As in the other answers, 1
is not a SymPy integer, it's a Python integer. Therefore it doesn't have SymPy's methods.
与其他答案一样,1不是SymPy整数,而是Python整数。因此它没有SymPy的方法。
You could declare expr
as one of the following to make it work:
您可以将expr声明为以下之一以使其工作:
expr = S.One
- expr = S.One
expr = S(1)
- expr = S(1)
expr = Integer(1)
- expr =整数(1)
SymPy integers are instances of the class Integer
, whereas Python integers are int
objects.
SymPy整数是Integer类的实例,而Python整数是int对象。
There is an additional trick to avoid this step: execute isympy -i
. The -i
parameter adds an additional input layer that replaces all digits with Integer(digits)
objects, as an example:
还有一个技巧可以避免这一步骤:执行isympy -i。 -i参数添加一个额外的输入层,用Integer(数字)对象替换所有数字,例如:
In [1]: 1
Out[1]: 1
In [2]: In[1]
Out[2]: u'Integer (1 )'
That is, inspecting with IPython what the input 1 is, gives u'Integer (1 )'
.
也就是说,用IPython检查输入1是什么,给出u'Integer(1)'。