M.Hiroi's Home Page
http://www.geocities.jp/m_hiroi/

Python3 Programming

お気楽 SymPy プログラミング超入門

[ Home | Light | Python3 ]

●Hello, SymPy!!

●SymPy の基本

>>> import sympy as sy
>>> sy.Integer(12345)
12345
>>> sy.Integer('12345')
12345
>>> type(sy.Integer('12345'))
<class 'sympy.core.numbers.Integer'>

>>> sy.Float(1.2345)
1.23450000000000
>>> sy.Float('1.2345')
1.23450000000000
>>> type(sy.Float('1.2345'))
<class 'sympy.core.numbers.Float'>

>>> sy.Rational(1, 2)
1/2
>>> sy.Integer(1) / sy.Integer(3)
1/3
>>> sy.Rational(1, 2) + sy.Rational(1, 3)
5/6

>>> 1 + 2 * sy.I
1 + 2*I
>>> 1.1 + 2.2 * sy.I
1.1 + 2.2*I
>>> sy.sqrt(2)
sqrt(2)
>>> sy.sqrt(4)
2
>>> sy.sqrt(8)
2*sqrt(2)
>>> sy.pi
pi
>>> sy.E
E
>>> sy.oo
oo
>>> sy.init_printing()
>>> sy.sqrt(2)
√2
>>> 1 / sy.sqrt(2)
√2
──
2
>>> sy.pi
π

>>>sy.init_printing(use_unicode=False)
>>>ssy.sqrt(2)
  ___
\/ 2
>>>s1 / sy.sqrt(2)
  ___
\/ 2
-----
  2
>>> sy.pi
pi
>>> x, y, z = sy.symbols('x y z')
>>> x
x
>>> y
y
>>> z
z
>>> x + 2*y + 3*z
x + 2⋅y + 3⋅z
>>> (x + y) ** 2
       2
(x + y)
>>> (x + y + z) ** 3
           3
(x + y + z)
>>> x**3 + 2*x**2 + 3*x + 4
 3      2
x  + 2⋅x  + 3⋅x + 4
>>> sy.sqrt(x)
√x
>>> sy.sqrt(x*x + y*y)
   _________
  ╱  2    2
╲╱  x  + y

>>> sy.var('a:c')
(a, b, c)
>>> a
a
>>> b
b
>>> c
c
>>> sy.var('x1:4')
(x1, x2, x3)
>>> x1
x1
>>> x2
x2
>>> x3
x3
>>> sy.expand((x + y)**2)
 2            2
x  + 2⋅x⋅y + y
>>> sy.expand((x + y + z)**2)
 2                    2            2
x  + 2⋅x⋅y + 2⋅x⋅z + y  + 2⋅y⋅z + z
>>> sy.expand((x + y)**3)
 3      2          2    3
x  + 3⋅x ⋅y + 3⋅x⋅y  + y
>>> a = sy.expand((x + y)**4)
>>> a
 4      3        2  2        3    4
x  + 4⋅x ⋅y + 6⋅x ⋅y  + 4⋅x⋅y  + y
>>> sy.factor(a)
       4
(x + y)
>>> sy.factor(x**2 - 1)
(x - 1)⋅(x + 1)
>>> sy.factor(x**2 - 2*x + 1)
       2
(x - 1)
>>> sy.factor(x**3 + y**3)
        ⎛ 2          2⎞
(x + y)⋅⎝x  - x⋅y + y ⎠

>>> sy.expand_power_exp(x**(y+z))
 y  z
x ⋅x
>>> (x*y)**z
     z
(x⋅y)
>>> sy.expand_power_base((x*y)**z)
     z
(x⋅y)
>>> sy.expand_power_base((x*y)**z, force=True)
 z  z
x ⋅y

>>> sy.expand_trig(sy.sin(x + y))
sin(x)⋅cos(y) + sin(y)⋅cos(x)
>>> sy.expand_trig(sy.cos(x + y))
-sin(x)⋅sin(y) + cos(x)⋅cos(y)
>>> sy.expand_trig(sy.sin(2*x))
2⋅sin(x)⋅cos(x)
>>> sy.expand_trig(sy.cos(2*x))
     2
2⋅cos (x) - 1

>>> sy.expand_log(sy.log(x*y))
log(x⋅y)
>>> sy.expand_log(sy.log(x*y), force=True)
log(x) + log(y)

>>> sy.expand_log(sy.log(x/y))
   ⎛x⎞
log⎜─⎟
   ⎝y⎠
>>> sy.expand_log(sy.log(x/y), force=True)
log(x) - log(y)

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

>>> sy.var('x1 y1 z1', positive=True)
(x₁, y₁, z₁)
>>> sy.expand_log(sy.log(x1*y1))
log(x₁) + log(y₁)
>>> sy.expand_log(sy.log(x1/y1))
log(x₁) - log(y₁)
>>> sy.expand_log(sy.log(x1**2))
2⋅log(x₁)
>>> (x**2 + 2*x + 1)/(x + 1)
 2
x  + 2⋅x + 1
────────────
   x + 1
>>> sy.simplify((x**2 + 2*x + 1)/(x + 1))
x + 1
>>> sy.simplify((x**2 - 1)/(x + 1))
x - 1

>>> x**y * x**z
 y  z
x ⋅x
>>> sy.powsimp(x**y * x**z)
 y + z
x
>>> x**y / x**z
 y  -z
x ⋅x
>>> sy.powsimp(x**y / x**z)
 y - z
x

>>> sy.sin(x)**2 + sy.cos(x)**2
   2         2
sin (x) + cos (x)
>>> sy.trigsimp(sy.sin(x)**2 + sy.cos(x)**2)
1
>>> sy.sin(x)*sy.cos(y) + sy.cos(x)*sy.sin(y)
sin(x)⋅cos(y) + sin(y)⋅cos(x)
>>> sy.trigsimp(sy.sin(x)*sy.cos(y) + sy.cos(x)*sy.sin(y))
sin(x + y)
>>> sy.cos(x)*sy.cos(y) + sy.sin(x)*sy.sin(y)
sin(x)⋅sin(y) + cos(x)⋅cos(y)
>>> sy.trigsimp(sy.cos(x)*sy.cos(y) + sy.sin(x)*sy.sin(y))
cos(x - y)
>>> 1 + sy.tan(x)**2
   2
tan (x) + 1
>>> sy.trigsimp(1 + sy.tan(x)**2)
   1
───────
   2
cos (x)

>>> sy.log(x) + sy.log(y)
log(x) + log(y)
>>> sy.logcombine(sy.log(x) + sy.log(y))
log(x) + log(y)
>>> sy.logcombine(sy.log(x) + sy.log(y), force=True)
log(x⋅y)
>>> sy.log(x) - sy.log(y)
log(x) - log(y)
>>> sy.logcombine(sy.log(x) - sy.log(y))
log(x) - log(y)
>>> sy.logcombine(sy.log(x) - sy.log(y), force=True)
   ⎛x⎞
log⎜─⎟
   ⎝y⎠
>>> x * sy.log(y)
x⋅log(y)
>>> sy.logcombine(x * sy.log(y))
x⋅log(y)
>>> sy.logcombine(x * sy.log(y), force=True)
   ⎛ x⎞
log⎝y ⎠
>>> a = x**2
>>> a
 2
x
>>> a.subs(x, 10)
100
>>> a.subs(x, 1.2345)
1.52399025000000
>>> a.subs(x, y + z)
       2
(y + z)
>>> a.subs(x, sy.sqrt(2))
2
>>> b = a.subs(x, y + z)
>>> b.subs({y: sy.sqrt(2), z: sy.sqrt(3)})
         2
(√2 + √3)
>>> sy.pi.evalf()
3.14159265358979
>>> sy.N(sy.pi, 50)
3.1415926535897932384626433832795028841971693993751
>>> sy.sqrt(2).evalf(50)
1.4142135623730950488016887242096980785696718753769
>>> b.subs({y: sy.sqrt(2), z: sy.sqrt(3)}).evalf()
9.89897948556636
>>> b.subs({y: sy.sqrt(2), z: sy.sqrt(3)}).evalf(20)
9.8989794855663561964

●多項式

>>> sy.div(x**3 + x**2 - 4*x + 1, x - 2)
⎛ 2             ⎞
⎝x  + 3⋅x + 2, 5⎠

>>> sy.div(x**3 - x**2 - 4*x + 4, x - 2)
⎛ 2           ⎞
⎝x  + x - 2, 0⎠

>>> (x**3 - x**2 - 4*x + 4) / (x - 2)
 3    2
x  - x  - 4⋅x + 4
─────────
      x - 2

>>> sy.cancel((x**3 - x**2 - 4*x + 4) / (x - 2))
 2
x  + x - 2

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

>>> 1/(x - 1) + 1/(x - 2)
  1        1
─── + ───
x - 1    x - 2

>>> sy.cancel(1/(x - 1) + 1/(x - 2))
  2⋅x - 3
───────
 2
x  - 3⋅x + 2

>>> sy.apart((2*x - 3)/(x**2 - 3*x + 2))
  1        1
─── + ───
x - 1    x - 2

>>> sy.apart((6*x**2 + x - 17) / (x**3 - 7*x -6))
  1        3        2
─── + ─── + ───
x + 2    x + 1    x - 3

>>> sy.apart((x + 1) / (x**4 + x**2))
  x + 1    1     1
- ─── + ─ + ──
   2       x      2
  x  + 1         x

>>> x**2 + y*x + z*x + y*z
 2
x  + x⋅y + x⋅z + y⋅z
>>> sy.collect(x**2 + y*x + z*x + y*z, x)
 2
x  + x⋅(y + z) + y⋅z
>>> sy.collect(x**2 + y*x + z*x + y*z, x).coeff(x, 2)
1
>>> sy.collect(x**2 + y*x + z*x + y*z, x).coeff(x, 1)
y + z
>>> sy.collect(x**2 + y*x + z*x + y*z, x).coeff(x, 0)
y⋅z
>>> sy.solveset(x**2 - 1, x)
{-1, 1}
>>> sy.solveset(x**2 + 1, x)
{-ⅈ, ⅈ}

>>> sy.S.UniversalSet
UniversalSet()
>>> sy.S.EmptySet
∅
>>> sy.S.Complexes
ℂ
>>> sy.S.Reals
ℝ
>>> sy.S.Integers
ℤ
>>> sy.S.Naturals
ℕ

>>> sy.solveset(x**2 + 1, x, domain=sy.Reals)
∅
>>> sy.var('a b c')
(a, b, c)
>>> sy.solveset(a*x**2 + b*x + c, x)
⎧           _____________              _____________ ⎫
⎪          ╱           2              ╱           2  ⎪
⎨   b    ╲╱  -4⋅a⋅c + b        b    ╲╱  -4⋅a⋅c + b   ⎬
⎪- ── - ────────, - ── + ────────⎪
⎩  2⋅a         2⋅a            2⋅a         2⋅a        ⎭

>>> sy.solveset(x**3, x)
{0}
>>> sy.roots(x**3, x)
{0: 3}
>>> sy.solveset(x**3 - 4*x**2 + 5*x - 2, x)
{1, 2}
>>> sy.roots(x**3 - 4*x**2 + 5*x - 2, x)
{1: 2, 2: 1}

●微積分

●集合

>>> import sympy as sy
>>> sy.init_printing()
>>> sy.S.EmptySet
∅
>>> sy.S.EmptySet.is_EmptySet
True
>>> sy.S.EmptySet.is_iterable
False
>>> len(sy.S.EmptySet)
0
>>> sy.S.UniversalSet
UniversalSet()
>>> sy.S.UniversalSet.is_UniversalSet
True
>>> sy.S.UniversalSet.is_iterable
False
>>> sy.S.UniversalSet | sy.S.EmptySet
UniversalSet()
>>> sy.S.UniversalSet & sy.S.EmptySet
∅
>>> a = sy.Interval(1, 10)
>>> a
[1, 10]
>>> a.is_iterable
False
>>> 1 in a
True
>>> 0 in a
False
>>> 10 in a
True
>>> 11 in a
False
>>> b = sy.Interval(5, 15)
>>> b
[5, 15]
>>> a & b
[5, 10]
>>> a | b
[1, 15]
>>> a - b
[1, 5)
>>> b - a
(10, 15]
>>> a ^ b
[1, 5) ∪ (10, 15]
>>> 5 in a ^ b
False
>>> 1 in a ^ b
True
>>> 10 in a ^ b
False
>>> 15 in a ^ b
True
>>> a = sy.FiniteSet(1, 2, 3, 4)
>>> a
{1, 2, 3, 4}
>>> a.is_iterable
True
>>> for x in a: print(x)
...
1
2
3
4
>>> len(a)
4
>>> for x in range(0, 6):
...     print(x, x in a)
...
0 False
1 True
2 True
3 True
4 True
5 False
>>> b = sy.FiniteSet(3, 4, 5, 6)
>>> b
{3, 4, 5, 6}
>>> a | b
{1, 2, 3, 4, 5, 6}
>>> a & b
{3, 4}
>>> a - b
{1, 2}
>>> b - a
{5, 6}
>>> a ^ b
{1, 2, 5, 6}
>>> c = sy.Interval(5, 8)
>>> c
[5, 8]
>>> a | c
{1, 2, 3, 4} ∪ [5, 8]
>>> b | c
{3, 4} ∪ [5, 8]
>>> a & c
∅
>>> b & c
{5, 6}
>>> a - c
{1, 2, 3, 4}
>>> c - a
[5, 8]
>>> b - c
{3, 4}
>>> c - b
(5, 6) ∪ (6, 8]
>>> a ^ c
{1, 2, 3, 4} ∪ [5, 8]
>>> b ^ c
{3, 4} ∪ (5, 6) ∪ (6, 8]
>>> a * b
{1, 2, 3, 4} × {3, 4, 5, 6}
>>> for x in a * b: print(x)
...
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 3)
(2, 4)
(2, 5)
(2, 6)
(3, 3)
(3, 4)
(3, 5)
(3, 6)
(4, 3)
(4, 4)
(4, 5)
(4, 6)
>>> a * c
{1, 2, 3, 4} × [5, 8]
>>> (1, 5.5) in a * c
True
>>> (4, 8.1) in a * c
False
>>> (2.5, 6) in a * c
False
>>> c & sy.S.Naturals
{5, 6, 7, 8}
>>> c | sy.S.Naturals
ℕ ∪ [5, 8]
>>> sy.Reals - c
(-∞, 5) ∪ (8, ∞)
>>> sy.Integers - c
ℤ \ [5, 8]
>>> 5 in sy.Integers - c
False
>>> 5.5 in sy.Integers - c
False
>>> 6 in sy.Integers - c
False
>>> 7 in sy.Integers - c
False
>>> 8 in sy.Integers - c
False
>>> 9 in sy.Integers - c
True
>>> sy.Range(1, 5)
{1, 2, 3, 4}
>>> sy.Range(1, 8, 2)
{1, 3, 5, 7}
>>> d = sy.Range(1, sy.oo)
>>> d
{1, 2, …, ∞}
>>> d[0]
1
>>> d[1]
2
>>> d[100]
101
>>> d[-1]
∞

>>> sy.var('x y z')
(x, y, z)
>>> a = sy.ImageSet(sy.Lambda(x, x * x), sy.S.Naturals0)
>>> a
ImageSet(Lambda(x, x**2), Naturals0)
>>> 100 in a
True
>>> 200 in a
False
>>> 400 in a
True
>>> b = sy.ImageSet(sy.Lambda(x, x * x), sy.Range(10))
>>> b
ImageSet(Lambda(x, x**2), Range(0, 10, 1))
>>> for n in b: print(n)
...
0
1
4
9
16
25
36
49
64
81

Copyright (C) 2018 Makoto Hiroi
All rights reserved.

[ Home | Light | Python3 ]