Simplification

使用simplify()函数进行简化
为了验证simplify()函数,先引入sympy变量,同时选择打印方式

  1. >>> from sympy import *
  2. >>> x, y, z = symbols('x y z')
  3. >>> init_printing(use_unicode = True)

simplify()函数能够简化大部分函数

  1. >>> simplify((x**2 + 2*x + 1)/(x+1))
  2. x + 1
  1. >>> simplify(cos(x)**2 + sin(x)**2)
  2. 1

但是也有弊端,simplify函数在计算时会使用多种简化方式,然后得出结果,这导致程序运算时间较长,因此如果了解多项式需要的检测结果,可以选用最适的函数。

  1. >>> simplify(x**2 + 2*x + 1)
  2. 2
  3. x + 2x + 1

Polynomial/Rational Function Simplification(多项式/有理函数简化)

expand

将函数展开

  1. >>> expand((x+1)**2)
  2. 2
  3. x + 2x + 1

factor

将函数简化

  1. >>> factor(x**2 + 2*x + 1)
  2. 2
  3. (x + 1)

collect

将函数中含有相同因式合并,格式collect(多项式,因式)

  1. >>> collect(x**3 + 2*x**2 + z*x**2 + x*2 +x*3*z + 1, x)
  2. 3 2
  3. x + x ⋅(z + 2) + x⋅(3z + 2) + 1

cancel

分式简化

  1. >>> expr = (x**2 + 2*x + 1)*x/(x**2 - 1)
  2. >>> cancel(expr)
  3. 2
  4. x + x
  5. ──────
  6. x - 1

apart

写成分数形式

  1. >>> apart(expr)
  2. 2
  3. x + 2 + ─────
  4. x - 1

Trigonometric Simplification(三角函数简化)

trigsimp

expand_trig