SymPyまとめ (simplification)

1. From basic operations,

1.1. evalf() outputs numerical value(s).

>>> expr = sqrt(8)
>>> expr
2 * sqrt(2)
>>> expr.evalf()
2.82842712474619

1.2. lambdify acts like lambda function.

>>> import numpy
>>> a = numpy.arange(10)
>>> expr = sin(x)
>>> f = lambdify(x, expr, "numpy")
>>> f(a)
[ 0.          0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427
 -0.2794155   0.6569866   0.98935825  0.41211849]

1.3. lambbify for scalar input

>>> f = lambdify(x, expr, "math")
>>> f(0.1)
0.0998334166468

2. Simplify simplifies equations.
Simplify runs slow.

>>> 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)

2.1. simplification by expand()
式の展開

>>> expand((x + 1)**2)
 2
x  + 2⋅x + 1
>>> expand((x + 2)*(x - 3))
 2
x  - x - 6

2.2. simplification by 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)
           2
z⋅(x + 2⋅y)

2.3. simplification by collect()
ある変数に関して乗数毎に項をまとめる

>>> expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3
>>> expr
 3    2        2
x  - x ⋅z + 2⋅x  + x⋅y + x - 3
>>> collected_expr = collect(expr, x)
>>> collected_expr
 3    2
x  + x ⋅(-z + 2) + x⋅(y + 1) - 3

2.4. simplification by cancel()
式を標準形(p/q,ここでp,qは共通項を持たない多項式)に変換.

>>> cancel((x**2 + 2*x + 1)/(x**2 + x))
x + 1
─────
  x

>>> expr = 1/x + (3*x/2 - 2)/(x - 4)
>>> expr
3⋅x
─── - 2
 2        1
─────── + ─
 x - 4    x
>>> cancel(expr)
   2
3⋅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            2
x⋅y  - 2⋅x⋅y⋅z + x⋅z  + y  - 2⋅y⋅z + z
───────────────────────────────────────
                  2
                 x  - 1
>>> cancel(expr)
 2            2
y  - 2⋅y⋅z + z
───────────────
     x - 1

2.5. simplification by apart()
部分分数分解

>>> expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)
>>> expr
   3       2
4⋅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   x
x  + x + 1

2.6. simplification by 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))
   2
sin (x)

>>> trigsimp(cosh(x)**2 + sinh(x)**2)
cosh(2⋅x)
>>> trigsimp(sinh(x)/tanh(x))
cosh(x)

2.7. simplification by powsimp()
乗数の整理

>>> powsimp(x**a*x**b)
  a + b
 x
>>> powsimp(x**a*y**a)
     a
(x⋅y)

>>> powsimp(t**c*z**c)
 c  c
t ⋅z
>>> powsimp(t**c*z**c, force=True)
     c
(t⋅z)

2.8. simplification by expand_power_***()
乗数の分解

>>> expr = (x*y)**(a+b)
     a+b
(x⋅y)
>>> expand_power_exp(expr)
     a      b
(x⋅y)  (x⋅y)
>>> expand_power_base(expr)
     a+b
(x⋅y)
>>> expand_power_base(expr, force=True)
 a+b   a+b
x     y

2.9. simplification by expand_log()
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)

>>> expand_log(log(z**2))
   ⎛ 2⎞
log⎝z ⎠
>>> expand_log(log(z**2), force=True)
2⋅log(z)

2.10. simplification by logcombine()
log()の統合

>>> logcombine(log(x) + log(y))
log(x⋅y)
>>> logcombine(n*log(x))
   ⎛ n⎞
log⎝x ⎠
>>> logcombine(n*log(z))
n⋅log(z)

>>> logcombine(n*log(z), force=True)
   ⎛ n⎞
log⎝z ⎠
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s