[go: up one dir, main page]
More Web Proxy on the site http://driver.im/ M.Hiroi's Home Page

Python3 Programming

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

[ Home | Light | Python3 ]

●論理代数

>>> sy.S.true
True
>>> type(sy.S.true)
<class 'sympy.logic.boolalg.BooleanTrue'>
>>> sy.S.false
False
>>> type(sy.S.false)
<class 'sympy.logic.boolalg.BooleanFalse'>

>>> sy.var('x y z')
(x, y, z)
>>> ~x
¬x
>>> x & y
x ∧ y
>>> x | y
x ∨ y
>>> x ^ y
x ⊻ y
>>> x >> y
x → y
>>> x << y
y → x
>>> sy.Equivalent(x, y)
x ⇔ y
>>> ~x.subs(x, True)
False
>>> ~x.subs(x, False)
True
>>> (x | y).subs(x, True)
True
>>> (x | y).subs(x, False)
y
>>> (x & y).subs(x, True)
y
>>> (x & y).subs(x, False)
False
>>> (x ^ y).subs(x, True)
¬y
>>> (x ^ y).subs(x, False)
y
>>> (x >> y).subs(x, True)
y
>>> (x >> y).subs(x, False)
True
>>> sy.Equivalent(x, y).subs(x, True)
y
>>> sy.Equivalent(x, y).subs(x, False)
¬y
>>> a = sy.POSform([x, y], [[0, 1], [1, 0]])
>>> a
(x ∨ y) ∧ (¬x ∨ ¬y)
>>> a.subs([[x, False], [y, False]])
False
>>> a.subs([[x, True], [y, False]])
True
>>> a.subs([[x, False], [y, True]])
True
>>> a.subs([[x, True], [y, True]])
False
>>> b = sy.SOPform([x, y], [[0, 1], [1, 0]])
>>> b
(x ∧ ¬y) ∨ (y ∧ ¬x)
>>> b.subs([[x, False], [y, False]])
False
>>> b.subs([[x, False], [y, True]])
True
>>> b.subs([[x, True], [y, False]])
True
>>> b.subs([[x, True], [y, True]])
False
>>> sy.boolalg.is_cnf(x & y & z)
True
>>> sy.boolalg.is_cnf(x | y | z)
True
>>> sy.boolalg.is_cnf(x & y | z)
False
>>> sy.to_cnf(x & y | z)
(x ∨ z) ∧ (y ∨ z)
>>> sy.boolalg.is_dnf(x & y | z)
True
>>> sy.to_dnf(x & y | z)
z ∨ (x ∧ y)
>>> sy.to_cnf(x ^ z)
(x ∨ z) ∧ (¬x ∨ ¬z)
>>> sy.to_dnf(x ^ z)
(x ∧ ¬x) ∨ (x ∧ ¬z) ∨ (z ∧ ¬x) ∨ (z ∧ ¬z)

>>> sy.simplify_logic(x & y | z)
z ∨ (x ∧ y)
>>> sy.simplify_logic(x & y | z, form='cnf')
(x ∨ z) ∧ (y ∨ z)
>>> sy.simplify_logic(x & y | z, form='dnf')
z ∨ (x ∧ y)
>>> a = sy.to_dnf(x ^ z)
>>> a
(x ∧ ¬x) ∨ (x ∧ ¬z) ∨ (z ∧ ¬x) ∨ (z ∧ ¬z)
>>> sy.simplify_logic(a)
(x ∧ ¬z) ∨ (z ∧ ¬x)

>>> sy.satisfiable(x ^ y)
{x: True, y: False}
>>> for a in sy.satisfiable(x ^ y, all_models=True): print(a)
...
{x: True, y: False}
{y: True, x: False}
>>> sy.satisfiable(False)
False

●等式と不等式

>>> sy.var('x y z')
(x, y, z)
>>> sy.init_printing()
>>> a = sy.Eq(x + y, z)
>>> a
x + y = z
>>> a.subs([[x, 1], [y, 2], [z, 3]])
True
>>> a.subs([[x, 1], [y, 2], [z, 4]])
False
>>> b = sy.Eq(1 + 2, 3, evaluate=False)
>>> b
3 = 3
>>> b.doit()
True
>>> c = sy.Ne(1 + 2, 3, evaluate=False)
>>> c
3 ≠ 3
>>> c.doit()
False

>>> sy.Eq((x + 1)**2, x**2 + 2*x + 1)
       2    2
(x + 1)  = x  + 2⋅x + 1
>>> sy.Eq(sy.expand((x + 1)**2), x**2 + 2*x + 1)
True
>>> sy.solveset(x**2 - 4)
{-2, 2}
>>> sy.solveset(sy.Eq(x**2, 4))
{-2, 2}

>>> x + y < z
x + y < z
>>> x + y <= z
x + y ≤ z
>>> x + y > z
x + y > z
>>> x + y >= z
x + y ≥ z

>>> a = (x > 2) | (x < -2)
>>> a
x > 2 ∨ x < -2
>>> a.subs(x, 0)
False
>>> a.subs(x, 10)
True
>>> a.subs(x, -10)
True
>>> a = sy.Symbol('a')
>>> b = sy.Symbol('b')
>>> e = sy.Ge((a + b) / 2, sy.sqrt(a * b))
>>> e
a    b      _____
── + ── ≥ ╲╱ a⋅b
2    2
>>> e.subs([[a, 10], [b, 20]])
True
>>> e.subs([[a, 10], [b, 10]])
True
>>> e.subs([[a, -10], [b, -10]])
False

>>> e = sy.Ge((a**2 + b**2)*(x**2 + y**2), (a*x + b*y)**2)
>>> e
⎛ 2    2⎞ ⎛ 2    2⎞              2
⎝a  + b ⎠⋅⎝x  + y ⎠ ≥ (a⋅x + b⋅y)
>>> e.subs([[a, 1], [b, 2], [x, 3], [y, 4]])
True
>>> e.subs([[a, 1], [b, -2], [x, -3], [y, 4]])
True

>>> e = sy.Ge((a*x + b*y)/2, ((a + b)/2)*((x + y)/2))
>>> e
a⋅x    b⋅y    ⎛a    b ⎞ ⎛x    y ⎞
──── + ──── ≥ ⎜── + ──⎟⋅⎜── + ──⎟
 2      2     ⎝2    2 ⎠ ⎝2    2 ⎠
>>> e.subs([[a, 2], [b, 1], [x, 4], [y, 3]])
True
>>> e.subs([[a, 2], [b, 3], [x, 4], [y, 3]])
False
>>> e.subs([[a, 2], [b, 1], [x, 4], [y, 5]])
False

●微分方程式

●いろいろな関数

●特殊関数

>>> sy.gamma(sy.Rational(1, 2))
√π
>>> sy.gamma(sy.Rational(2, 2))
1
>>> sy.gamma(sy.Rational(3, 2))
√π
──
2
>>> sy.gamma(10)
362880
>>> sy.factorial(9)
362880
>>> sy.gamma(11)
3628800
>>> sy.factorial(10)
3628800
>>> sy.plot(sy.gamma(x), (x, -sy.pi, sy.pi), ylim=(-10, 10))

>>> sy.plotting.plot3d(sy.Abs(sy.gamma(x + y * sy.I)), (x, -4, 4), (y, -4, 4))

>>> for i in range(2, 9): print(i, sy.zeta(i))
...
2 pi**2/6
3 zeta(3)
4 pi**4/90
5 zeta(5)
6 pi**6/945
7 zeta(7)
8 pi**8/9450

>>> for i in range(2, 9): print(i, sy.zeta(i).evalf())
...
2 1.64493406684823
3 1.20205690315959
4 1.08232323371114
5 1.03692775514337
6 1.01734306198445
7 1.00834927738192
8 1.00407735619794

>>> for i in range(0, 10): print(i, sy.bernoulli(i))
...
0 1
1 -1/2
2 1/6
3 0
4 -1/30
5 0
6 1/42
7 0
8 -1/30
9 0
>>> sy.plot(sy.zeta(x, 1), (x, 1.1, 8))

>>> sy.besselj(0, 0)
1
>>> sy.besselj(0, sy.oo)
0
>>> p1 = sy.plot(sy.besselj(0, x), (x, 0, 30), show=False)
>>> p2 = sy.plot(sy.besselj(2, x), (x, 0, 30), line_color="red", show=False)
>>> p3 = sy.plot(sy.besselj(4, x), (x, 0, 30), line_color="green", show=False)
>>> p1.extend(p2)
>>> p1.extend(p3)
>>> p1.show()

>>> sy.bessely(0, sy.oo)
0
>>> sy.bessely(0, 0)
-∞
>>> p1 = sy.plot(sy.bessely(0, x), (x, 0.5, 20), show=False)
>>> p2 = sy.plot(sy.bessely(1, x), (x, 0.5, 20), line_color="red", show=False)
>>> p3 = sy.plot(sy.bessely(2, x), (x, 0.5, 20), line_color="green", show=False)
>>> p1.extend(p2)
>>> p1.extend(p3)
>>> p1.show()

>>> sy.erf(sy.oo)
1
>>> sy.erf(-sy.oo)
-1
>>> for i in range(-3, 4): print(i, sy.erf(i))
...
-3 -erf(3)
-2 -erf(2)
-1 -erf(1)
0 0
1 erf(1)
2 erf(2)
3 erf(3)
>>> for i in range(-3, 4): print(i, sy.erf(i).evalf())
...
-3 -0.999977909503001
-2 -0.995322265018953
-1 -0.842700792949715
0 0
1 0.842700792949715
2 0.995322265018953
3 0.999977909503001
>>> sy.plot(sy.erf(x), (x, -3, 3))

●関数のグラフ


Copyright (C) 2018-2023 Makoto Hiroi
All rights reserved.

[ Home | Light | Python3 ]