SymPy学习之Simplification

来源:互联网 发布:淘宝江南布衣假货 编辑:程序博客网 时间:2024/06/06 02:25

Simplification

>>> from sympy import *>>> x, y, z = symbols('x y z')>>> init_printing(use_unicode=True)
simplify
#对式子进行化简>>> simplify(sin(x)**2 + cos(x)**2)1>>> simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))x - 1>>> simplify(gamma(x)/gamma(x - 2))(x - 2)⋅(x - 1)

Polynomial/Rational Function Simplification

expand
#将式子展开成多项式>>> expand((x + 1)**2) 2x  + 2⋅x + 1>>> expand((x + 2)*(x - 3)) 2x  - x - 6>>> expand((x + 1)*(x - 2) - (x - 1)*x)-2
factor
#进行因式分解>>> factor(x**3 - x**2 + x - 1)        ⎛ 2    ⎞(x - 1)⋅⎝x  + 1⎠>>> factor(x**2*z + 4*x*y*z + 4*y**2*z)           2z⋅(x + 2⋅y)>>> factor_list(x**2*z + 4*x*y*z + 4*y**2*z)(1, [(z, 1), (x + 2⋅y, 2)])>>> expand((cos(x) + sin(x))**2) 2 2sin (x) + 2⋅sin(x)⋅cos(x) + cos (x)>>> factor(cos(x)**2 + 2*cos(x)*sin(x) + sin(x)**2) 2(sin(x) + cos(x))
collect
#合并同类项>>> expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3>>> expr 3    2        2x  - x ⋅z + 2⋅x  + x⋅y + x - 3>>> collected_expr = collect(expr, x)>>> collected_expr 3    2x  + x ⋅(-z + 2) + x⋅(y + 1) - 3>>> collected_expr.coeff(x, 2)-z + 2
cancel
#化成最简分式>>> cancel((x**2 + 2*x + 1)/(x**2 + x))x + 1─────  x>>> expr = 1/x + (3*x/2 - 2)/(x - 4)>>> expr3*x--- - 2 2        1------- + - x - 4    x>>> cancel(expr)  23*x - 2*x - 8--------------    2 2*x - 8*x>>> expr = (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2)/(x**2 - 1)>>> expr   2               2   2           2x*y - 2*x*y*z + x*z + y - 2*y*z + z---------------------------------------                 2                x - 1>>> cancel(expr) 2           2y - 2*y*z + z---------------    x - 1>>> factor(expr)       2(y - z)-------- x - 1
apart
#与cancel相反,将分式展开>>> expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)>>> expr   3      24*x + 21*x + 10*x + 12------------------------  4     3     2 x + 5*x + 5*x + 4*x>>> apart(expr) 2*x - 1       1     3---------- - ----- + - 2           x + 4   xx + x + 1

Trigonometric Simplification

trigsimp
>>> trigsimp(sin(x)**2 + cos(x)**2)1>>> trigsimp(sin(x)**4 - 2*cos(x)**2*sin(x)**2 + cos(x)**4)cos(4*x)   1-------- + -   2       2>>> trigsimp(sin(x)*tan(x)/sec(x))   2sin (x)>>> trigsimp(cosh(x)**2 + sinh(x)**2)cosh(2⋅x)>>> trigsimp(sinh(x)/tanh(x))cosh(x)
expand_trig
>>> expand_trig(sin(x + y))sin(x)⋅cos(y) + sin(y)⋅cos(x)>>> trigsimp(sin(x)*cos(y) + sin(y)*cos(x))sin(x + y)>>> expand_trig(tan(2*x)) 2*tan(x)-------------     2- tan (x) + 1
#sqrt(x) is just a shortcut to x**Rational(1, 2)>>> sqrt(x) == x**Rational(1, 2)True

Powers

powsimp
>>> x, y = symbols('x y', positive=True)>>> a, b = symbols('a b', real=True)>>> z, t, c = symbols('z t c')>>> powsimp(x**a*x**b)  a + b x>>> powsimp(x**a*y**a)     a(x⋅y)>>> powsimp(t**c*z**c)  #c没有指明类型 c ct ⋅z>>> powsimp(t**c*z**c, force=True)    c(t⋅z)>>> powsimp(z**2*t**2)  #指数为实数时无法合并  2  2 t ⋅z>>> powsimp(sqrt(x)*sqrt(y)) √x⋅√y
expand_power_exp / expand_power_base
>>> expand_power_exp(x**(a + b)) a  bx ⋅x
>>> expand_power_base((x*y)**a) a  ax ⋅y
powdenest
>>> powdenest((x**a)**b) a⋅bx

Exponentials and logarithms

>>> x, y = symbols('x y', positive=True)>>> n = symbols('n', real=True)
expand_log
>>> expand_log(log(x*y))log(x) + log(y)>>> expand_log(log(x/y))log(x) - log(y)>>> expand_log(log(x**2))2⋅log(x)>>> expand_log(log(x**n))n⋅log(x)>>> expand_log(log(z*t))log(t⋅z)
logcombine
>>> logcombine(log(x) + log(y))log(x⋅y)>>> logcombine(n*log(x))   ⎛ n⎞log⎝x ⎠>>> logcombine(n*log(z))n⋅log(z)

Special Functions

>>> x, y, z = symbols('x y z')>>> k, m, n = symbols('k m n')
>>> factorial(n)n!
>>> binomial(n, k)⎛n⎞⎜ ⎟⎝k⎠
>>> gamma(z)Γ(z)
rewrite
>>> tan(x).rewrite(sin)     22⋅sin (x)───────── sin(2⋅x)>>> factorial(x).rewrite(gamma)Γ(x + 1)
expand_func
>>> expand_func(gamma(x + 3))x⋅(x + 1)⋅(x + 2)⋅Γ(x)
combsimp
#化简组合函数>>> combsimp(factorial(n)/factorial(n - 3))n⋅(n - 2)⋅(n - 1)>>> combsimp(binomial(n+1, k+1)/binomial(n, k))n + 1─────k + 1

Example: Continued Fractions

>>> def list_to_frac(l):...     expr = Integer(0)...     for i in reversed(l[1:]):...         expr += i...         expr = 1/expr...     return l[0] + expr>>> list_to_frac([x, y, z])      1x + ─────        1    y + ─        z
>>> list_to_frac([1, 2, 3, 4])43──30
>>> syms = symbols('a0:5')  #创建编号变量>>> syms(a₀, a₁, a₂, a₃, a₄)>>> a0, a1, a2, a3, a4 = syms>>> frac = list_to_frac(syms)>>> frac             1a₀ + ─────────────────               1     a₁ + ────────────                  1          a₂ + ───────                    1               a₃ + ──                    a₄

0 0