P : False False True True Q : False True False True --------+------------------------- Not P : True True False False Not Q : True False True False P And Q : False False False True P Or Q : False True True True P Xor Q : False True True False P Equ Q : True False False True P Imp Q : True True False True Q Imp P : True False True True
>>> 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
POSform(vars, minterms, dontcares=None) => And_Object SOPform(vars, minterms, dontcares=None) => Or_Object
>>> 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
simplify_logic(expr, form=None, deep=True)
>>> 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
symbols('name ...', cls=Function)
>>> sy.var('n m x y z') (n, m, x, y, z) >>> sy.init_printing() >>> f = sy.symbols('f', cls=sy.Function) >>> f(x) f(x) >>> f(x).diff(x) d ──(f(x)) dx >>> sy.dsolve(f(x).diff(x) - n * f(x), f(x)) n⋅x f(x) = C₁⋅ℯ >>> sy.dsolve(sy.Eq(f(x).diff(x), n * f(x)), f(x)) n⋅x f(x) = C₁⋅ℯ
>>> sy.var('n m x y z') (n, m, x, y, z) >>> sy.init_printing() >>> sy.Abs(n) │n│ >>> sy.Abs(10) 10 >>> sy.Abs(-10) 10 >>> sy.sign(n) sign(n) >>> sy.sign(0) 0 >>> sy.sign(10) 1 >>> sy.sign(-10) -1 >>> sy.Max(x, y, z) Max(x, y, z) >>> sy.Max(1, 2, 3) 3 >>> sy.Min(x, y, z) Min(x, y, z) >>> sy.Min(1, 2, 3) 1 >>> sy.sqrt(2) √2 >>> sy.sqrt(2).evalf() 1.41421356237310 >>> sy.cbrt(3) 3 ___ ╲╱ 3 >>> sy.cbrt(3).evalf() 1.44224957030741 >>> sy.exp(1) ℯ >>> sy.exp(1).evalf() 2.71828182845905 >>> sy.log(2) log(2) >>> sy.log(2).evalf() 0.693147180559945 >>> sy.log(4, 2) 2 >>> sy.log(5, 2) log(5) ────── log(2) >>> for i in range(-5, 6): print(i - 0.5, sy.floor(i - 0.5)) ... -5.5 -6 -4.5 -5 -3.5 -4 -2.5 -3 -1.5 -2 -0.5 -1 0.5 0 1.5 1 2.5 2 3.5 3 4.5 4 >>> for i in range(-5, 6): print(i - 0.5, sy.ceiling(i - 0.5)) ... -5.5 -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
>>> sy.diff(sy.sin(x)) cos(x) >>> sy.diff(sy.cos(x)) -sin(x) >>> sy.diff(sy.tan(x)) 2 tan (x) + 1 >>> sy.integrate(sy.sin(x)) -cos(x) >>> sy.integrate(sy.cos(x)) sin(x) >>> sy.integrate(sy.tan(x)) -log(cos(x)) >>> sy.sin(x).series(x) 3 5 x x ⎛ 6⎞ x - ── + ──── + O⎝x ⎠ 6 120 >>> sy.cos(x).series(x) 2 4 x x ⎛ 6⎞ 1 - ── + ── + O⎝x ⎠ 2 24 >>> sy.tan(x).series(x) 3 5 x 2⋅x ⎛ 6⎞ x + ── + ──── + O⎝x ⎠ 3 15
>>> sy.factorial(n) n! >>> sy.factorial(10) 3628800 >>> sy.factorial(20) 2432902008176640000 >>> sy.factorial(30) 265252859812191058636308480000000 >>> sy.summation(1/sy.factorial(x), (x, 0, sy.oo)) ℯ >>> sy.summation(1/((x + 2) * sy.factorial(x)), (x, 0, sy.oo)) 1
>>> sy.fibonacci(n) fibonacci(n) >>> for i in range(40): print(sy.fibonacci(i), end=" ") ... 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986
>>> sy.lucas(n) lucas(n) >>> for i in range(40): print(sy.lucas(i), end=" ") ... 2 1 3 4 7 11 18 29 47 76 123 199 322 521 843 1364 2207 3571 5778 9349 15127 24476 39603 64079 103682 167761 271443 439204 710647 1149851 1860498 3010349 4870847 7881196 12752043 20633239 33385282 54018521 87403803 141422324
>>> sy.binomial(m, n) binomial(m, n) >>> for i in range(1, 16): ... for j in range(0, i+1): print(sy.binomial(i, j), end=" ") ... print("") ... 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1 1 12 66 220 495 792 924 792 495 220 66 12 1 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
>>> sy.ff(m, n) FallingFactorial(m, n) >>> for i in range(10): print(sy.ff(10, i), end=" ") ... 1 10 90 720 5040 30240 151200 604800 1814400 3628800
>>> sy.rf(m, n) RisingFactorial(m, n) >>> for i in range(10): print(sy.rf(10, i), end=" ") ... 1 10 110 1320 17160 240240 3603600 57657600 980179200 17643225600
1 分割 : {{1, 2, 3}} 2 分割 : {{1, 2}, {3}}, {{1, 3}, {2}}, {{1}, {2, 3}} 3 分割 ; {{1}, {2}, {3}}
>>> sy.bell(n) bell(n) >>> for i in range(20): print(sy.bell(i), end=" ") ... 1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322 1382958545 10480142147 82864869804 682076806159 5832742205057
>>> sy.catalan(n) catalan(n) >>> for i in range(25): print(sy.catalan(i), end=" ") ... 1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 35357670 129644790 477638700 1767263190 6564120420 24466267020 91482563640 343059613650 1289904147324
[5] [4, 1] [3, 2] [3, 1, 1] [2, 2, 1] [2, 1, 1, 1] [1, 1, 1, 1, 1] [6] [5, 1] [4, 2] [4, 1, 1] [3, 3] [3, 2, 1] [3, 1, 1, 1] [2, 2, 2] [2, 2, 1, 1] [2, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1] [7] [6, 1] [5, 2] [5, 1, 1] [4, 3] [4, 2, 1] [4, 1, 1, 1] [3, 3, 1] [3, 2, 2] [3, 2, 1, 1] [3, 1, 1, 1, 1] [2, 2, 2, 1] [2, 2, 1, 1, 1] [2, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1]
>>> sy.functions.combinatorial.numbers.nT(5) 7 >>> sy.functions.combinatorial.numbers.nT(6) 11 >>> sy.functions.combinatorial.numbers.nT(7) 15 >>> sy.functions.combinatorial.numbers.nT(5, 3) 2 >>> sy.functions.combinatorial.numbers.nT(6, 3) 3 >>> sy.functions.combinatorial.numbers.nT(7, 4) 3 >>> sy.functions.combinatorial.numbers.nT(100) 190569292 >>> sy.functions.combinatorial.numbers.nT(1000) 24061467864032622473692149727991
>>> sy.harmonic(n) harmonic(n) >>> for i in range(1, 10): print(sy.harmonic(i), end=" ") ... 1 3/2 11/6 25/12 137/60 49/20 363/140 761/280 7129/2520 >>> sy.harmonic(sy.oo) ∞ >>> sy.harmonic(sy.oo, 2) 2 π ─ 6 >>> sy.harmonic(sy.oo, 3) ζ(3)
>>> 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))
plot(expr1 [, expr2, ...], (var, low, high), ...)
>>> sy.var('n m x y z') (n, m, x, y, z) >>> p1 = sy.plot(x**2 - 4, (x, -4, 4), ylim=(-20, 20), show=False) >>> p2 = sy.plot(x**3 - 9*x, (x, -4, 4), line_color="red", ylim=(-20, 20), show=False) >>> p3 = sy.plot(x**4 - 10*x**2 + 9, (x, -4, 4), line_color="blue", ylim=(-20, 20), show=False) >>> p4 = sy.plot(x**5 - 5*x**3 + 4*x, (x, -4, 4), line_color="green", ylim=(-20, 20), show=False) >>> p1.extend(p2) >>> p1.extend(p3) >>> p1.extend(p4) >>> p1.show()
>>> p1 = sy.plot(2**x, (x, -2, 2), ylim=(-5, 5), show=False) >>> p2 = sy.plot(0.5**x, (x, -2, 2), ylim=(-5, 5), line_color="red", show=False) >>> p3 = sy.plot(sy.log(x, 2), (x, -2, 2), ylim=(-5, 5), line_color="blue", show=False) >>> p4 = sy.plot(sy.log(x, 0.5), (x, -2, 2), ylim=(-5, 5), line_color="green", show=False) >>> p1.extend(p2) >>> p1.extend(p3) >>> p1.extend(p4) >>> p1.show()
>>> p1 = sy.plot(sy.exp(x), (x, -2, 2), ylim=(-5, 5), show=False) >>> p2 = sy.plot(sy.log(x), (x, -2, 2), ylim=(-5, 5), line_color="red", show=False) >>> p1.extend(p2) >>> p1.show()
>>> p1 = sy.plot(sy.sqrt(x), (x, 0, 9), show=False) >>> p2 = sy.plot(sy.cbrt(x), (x, 0, 9), line_color="red", show=False) >>> p1.extend(p2) >>> p1.show()
>>> p1 = sy.plot(sy.sin(x), (x, -2*sy.pi, 2*sy.pi), show=False) >>> p2 = sy.plot(sy.cos(x), (x, -2*sy.pi, 2*sy.pi), line_color="red", show=False) >>> p1.extend(p2) >>> p1.show()
>>> sy.plot(sy.tan(x), (x, -2*sy.pi, 2*sy.pi), ylim=(-10,10))
>>> p1 = sy.plot(sy.sinh(x), (x, -4, 4), ylim=(-4, 4), show=False) >>> p2 = sy.plot(sy.cosh(x), (x, -4, 4), ylim=(-4, 4), line_color="green", show=False) >>> p3 = sy.plot(sy.tanh(x), (x, -4, 4), ylim=(-4, 4), line_color="red", show=False) >>> p1.extend(p2) >>> p1.extend(p3) >>> p1.show()
>>> import matplotlib.pyplot as plt >>> plt.rcParams['figure.figsize'] = (6, 6) >>> p1 = sy.plot_implicit(sy.Eq(x**2 +y**2, 1), (x, -1, 1), (y, -1, 1), show=False) >>> p2 = sy.plot_implicit(sy.Eq(x**2/0.25 +y**2, 1), (x, -1, 1), (y, -1, 1), line_color='red', show=False) >>> p3 = sy.plot_implicit(sy.Eq(x**2 +y**2/0.25, 1), (x, -1, 1), (y, -1, 1), line_color='green', show=False) >>> p1.extend(p2) >>> p1.extend(p3) >>> p1.show()
>>> p1 = sy.plotting.plot_parametric(sy.cos(x), sy.sin(x), (x, 0, 2*sy.pi), show=False) >>> p2 = sy.plotting.plot_parametric(0.5*sy.cos(x), sy.sin(x), (x, 0, 2*sy.pi), line_color="red", show=False) >>> p3 = sy.plotting.plot_parametric(sy.cos(x), 0.5*sy.sin(x), (x, 0, 2*sy.pi), line_color="green", show=False) >>> p1.extend(p2) >>> p1.extend(p3) >>> p1.show()
plot3d(expr, (x, low, high), (y, low, high), ...)
plot3d_parametric_line(expr_x, expr_y, expr_z, (t, low, high), ...) plot3d_parametric_surface(expr_x, expr_y, expr_z, (t, low, high), ...)
>>> sy.plotting.plot3d(x**2 + y**2, (x, -2, 2), (y, -2, 2))
>>> sy.plotting.plot3d(x**2 - y**2, (x, -2, 2), (y, -2, 2))
>>> sy.plotting.plot3d(sy.sin(sy.sqrt(x**2 + y**2)), (x, -3*sy.pi, 3*sy.pi), (y, -3*sy.pi, 3*sy.pi))
>>> sy.plotting.plot3d_parametric_line(sy.cos(x), sy.sin(x), x, (x, -5*sy.pi, 5*sy.pi))
>>> sy.plotting.plot3d_parametric_surface(sy.cos(x)*sy.cos(y), sy.cos(x)*sy.sin(y), sy.sin(x), (x, 0, 2*sy.pi), (y, 0, sy.pi))
>>> sy.plotting.plot3d_parametric_surface(sy.cosh(x)*sy.cos(y), sy.cosh(x)*sy.sin(y), sy.sinh(x), (x, -sy.pi, sy.pi), (y, -sy.pi, sy.pi))