1.1.8. 常见问题¶
等式使用:Eq
¶
可以借助 expand()
, simplify()
和 trigsimp()
进行真值判断:
import warnings
from sympy import *
warnings.filterwarnings('ignore')
x = symbols('x')
simplify((x + 1)**2 - (x**2 + 2*x + 1))
\[\displaystyle 0\]
eq = sin(2*x) - 2*sin(x)*cos(x)
simplify(eq)
\[\displaystyle 0\]
expand(eq, trig=True)
\[\displaystyle 0\]
变量¶
from sympy import Symbol
符号 \(a\) 被存储为变量: "a
"
a = Symbol('a')
a
\[\displaystyle a\]
将包含符号 \(a\) 的表达式存储为变量 "b
"
b = a + 1
b
\[\displaystyle a + 1\]
a = 4
a
4
b
\[\displaystyle a + 1\]
from sympy import var
r, t, d = var('rate time short_life')
r, t, d
(rate, time, short_life)
c, d = var('c d')
c*d
\[\displaystyle c d\]
s, t, w = symbols('s t w')
s, t, w
(s, t, w)
var('ww er, ty')
(ww, er, ty)
ww
\[\displaystyle ww\]
symbols('rrt')
rrt 会报错
File "<ipython-input-14-16cad0f696ed>", line 2
rrt 会报错
^
SyntaxError: invalid syntax
推荐使用 symbols
创建变量。
x, y, z = symbols('x:z')
symbols('x:4')
(x0, x1, x2, x3)
函数¶
from sympy import Function
f = Function('f')
x = symbols('x')
f(x)
\[\displaystyle f{\left(x \right)}\]
f1 = Function('f1')
f2 = Function('f2')(x)
\(f_1\) 是可调用的,但 \(f2\) 不行:
f1(1), f1(x)
(f1(1), f1(x))
f2(1) 会报错
File "<ipython-input-21-6ff94c9ea2c5>", line 1
f2(1) 会报错
^
SyntaxError: invalid syntax