<jupyter_start><jupyter_text>`sympy` [`sympy`](https://www.sympy.org)는 *기호 처리기*로 숫자 대신 기호 연산을 지원한다..[`sympy`](https://www.sympy.org), a *symbolic processor* supports operations in symbols instead of numbers. 2006년 이후 2019 까지 800명이 넘는 개발자가 작성한 코드를 제공하였다.Since 2006, more than 800 developers contributed so far in 2019.  기호 연산 예Examples of symbolic processing `sympy` 모듈을 `sym` 라는 이름으로 불러온다.Import `sympy` module in the name of `sym`.<jupyter_code>import sympy as sym
sym.init_printing()<jupyter_output><empty_output><jupyter_text>비교를 위해 `numpy` 모듈도 불러온다.Import `numpy` module to compare.<jupyter_code>import numpy as np


np.pi


sym.pi<jupyter_output><empty_output><jupyter_text>오일러 공식Euler formula $$e ^ {\pi i} + 1 = 0$$<jupyter_code>np.exp(np.pi * 1j) + 1


sym.exp(sym.pi * 1j) + 1


sym.simplify(sym.exp(sym.pi * 1j) + 1)<jupyter_output><empty_output><jupyter_text>무한대Infinity<jupyter_code>np.inf, np.inf > 999999


sym.oo, sym.oo > 999999<jupyter_output><empty_output><jupyter_text>제곱근Square root 10의 제곱근을 구해보자.Let't find the square root of ten.<jupyter_code>np.sqrt(10)


sym.sqrt(10)<jupyter_output><empty_output><jupyter_text>결과를 숫자로 살펴보려면 `evalf()` 메소드를 사용한다.Use `evalf()` method to check the result in digits.<jupyter_code>sym.sqrt(10).evalf()


sym.sqrt(10).evalf(30)<jupyter_output><empty_output><jupyter_text>10의 제곱근을 제곱해보자.Let't square the square root of ten.<jupyter_code>np.sqrt(10) ** 2


sym.sqrt(10) ** 2<jupyter_output><empty_output><jupyter_text>위 결과의 차이에 대해 어떻게 생각하는가?What do you think about the differences of the results above?  분수Fractions 15 / 11 을 생각해보자.Let't think about 15/11.<jupyter_code>num = 15
den = 11


division = num / den


division


division * den


import fractions


fr_division = fractions.Fraction(num, den)


fr_division


fr_division * den


sym_division = sym.Rational(num, den)


sym_division


sym_division * den<jupyter_output><empty_output><jupyter_text>위 결과의 차이에 대해 어떻게 생각하는가?What do you think about the differences of the results above?  변수를 포함하는 수식Expressions with variables 사용할 변수를 정의.Define variables to use.<jupyter_code>a, b, c, x = sym.symbols('a b c x')
theta, phi = sym.symbols('theta phi')<jupyter_output><empty_output><jupyter_text>변수들을 한번 살펴보자.Let's take a look at the variables<jupyter_code>a, b, c, x


theta, phi<jupyter_output><empty_output><jupyter_text>변수를 조합하여 새로운 수식을 만들어 보자.Let's make equations using variables.<jupyter_code>y = a * x + b


y


z = a * x * x + b * x + c


z


w = a * sym.sin(theta) ** 2 + b


w


p = (x - a) * (x  - b) * (x - c)


p


sym.expand(p, x)


sym.collect(_, x)<jupyter_output><empty_output><jupyter_text>$$\frac{x + xy}{x}$$<jupyter_code>sym.simplify((x + x * y) / x)<jupyter_output><empty_output><jupyter_text>그래프Plot<jupyter_code>import sympy.plotting as splot


splot.plot(sym.sin(x));


import mpmath
splot.plot(sym.sin(mpmath.radians(x)), (x, -360, 360));


splot.plot_parametric((sym.cos(theta), sym.sin(theta)), (theta, -sym.pi, sym.pi));


splot.plot_parametric(
    16 * (sym.sin(theta)**3),
    13 * sym.cos(theta) - 5 * sym.cos(2*theta) - 2 * sym.cos(3*theta) - sym.cos(4*theta),
    (theta, -sym.pi, sym.pi)
);<jupyter_output><empty_output><jupyter_text>3차원 그래프3D Plot<jupyter_code>x, y = sym.symbols('x y')
splot.plot3d(sym.cos(x) + sym.sin(y), (x, -5, 5), (y, -5, 5));


splot.plot3d_parametric_line(x, 25-x**2, 25-x**2, (x, -5, 5));


u, v = sym.symbols('u v')
splot.plot3d_parametric_surface(u + v, sym.sin(u), sym.cos(u), (u, -1, 1), (v, -1, 1));<jupyter_output><empty_output><jupyter_text>극한Limits<jupyter_code>sym.limit(x ** x, x, 0)<jupyter_output><empty_output><jupyter_text>$$\lim_{x \to 0} \frac{sin x}{x}$$<jupyter_code>sym.limit(sym.sin(x) / x, x, 0)<jupyter_output><empty_output><jupyter_text>$$\lim_{x \to \infty} x$$<jupyter_code>sym.limit(x, x, sym.oo)<jupyter_output><empty_output><jupyter_text>$$\lim_{x \to \infty} \frac{1}{x}$$<jupyter_code>sym.limit(1 / x, x, sym.oo)<jupyter_output><empty_output><jupyter_text>$$\lim_{x \to 0} x^x$$<jupyter_code>sym.limit(x ** x, x, 0)<jupyter_output><empty_output><jupyter_text>미적분Calculus $$\frac{dz}{dx} =\frac{d}{dx} \left( a x^2 + bx + c \right)$$<jupyter_code>z.diff(x)<jupyter_output><empty_output><jupyter_text>$$\int{z}{dx} =\int{\left(a x^2 + bx + c \right)}{dx}$$<jupyter_code>sym.integrate(z, x)


w


w.diff(theta)


sym.integrate(w, theta)<jupyter_output><empty_output><jupyter_text>정적분Definite integral<jupyter_code>sym.integrate(w, (theta,0, sym.pi))<jupyter_output><empty_output><jupyter_text>근Root<jupyter_code>z_sol_list = sym.solve(z, x)


z_sol_list


sym.solve(2* sym.sin(theta) ** 2 - 1, theta)<jupyter_output><empty_output><jupyter_text>코드 생성Code generation<jupyter_code>print(sym.python(z_sol_list[0]))


import sympy.utilities.codegen as sc


[(c_name, c_code), (h_name, c_header)] = sc.codegen(
    ("z_sol", z_sol_list[0]), 
    "C89", 
    "test"
)


c_name


print(c_code)


h_name


print(c_header)<jupyter_output><empty_output><jupyter_text>방정식Equation solving $$x^4=1$$<jupyter_code>sym.solve(x ** 4 - 1, x)


sym.solveset(x ** 4 - 1, x)<jupyter_output><empty_output><jupyter_text>$$e^x=-1$$<jupyter_code>sym.solve(sym.exp(x) + 1, x)<jupyter_output><empty_output><jupyter_text>$$x^4 - 3x^2 +1$$<jupyter_code>f = x ** 4 - 3 * x ** 2 + 1
sym.factor(f)


sym.factor(f, modulus=5)<jupyter_output><empty_output><jupyter_text>Boolean equations<jupyter_code>sym.satisfiable(a & b)


sym.satisfiable(a ^ b)<jupyter_output><empty_output><jupyter_text>연립방정식System of equations<jupyter_code>a1, a2, a3 = sym.symbols('a1:4')
b1, b2, b3 = sym.symbols('b1:4')
c1, c2 = sym.symbols('c1:3')
x1, x2 = sym.symbols('x1:3')


eq1 = sym.Eq(
    a1 * x1 + a2 * x2, 
    c1,
)


eq1


eq2 = sym.Eq(
    b1 * x1 + b2 * x2,
    c2,
)


eq_list = [eq1, eq2]


eq_list


sym.solve(eq_list, (x1, x2))<jupyter_output><empty_output><jupyter_text>행렬Matrix<jupyter_code>identity = sym.Matrix([[1, 0], [0, 1]])
identity


A = sym.Matrix([[1, a], [b, 1]])
A


A * identity


A * A


A ** 2<jupyter_output><empty_output><jupyter_text>미분방정식Differential Equations $$\frac{d^2}{dx^2}f(x) + f(x)$$<jupyter_code>f = sym.Function('f', real=True)
(f(x).diff(x, x) + f(x))


sym.dsolve(f(x).diff(x, x) + f(x))<jupyter_output><empty_output><jupyter_text>기계진동Mechanical Vibration $$m \frac{d^2x(t)}{dt^2} +c \frac{dx(t)}{dt} + x(t) = 0$$<jupyter_code>m, c, k, t = sym.symbols('m c k t')
x = sym.Function('x', real=True)
vib_eq = m * x(t).diff(t, t) + c * x(t).diff(t) + k * x(t)
vib_eq


result = sym.dsolve(vib_eq)
result


sym.simplify(result)<jupyter_output><empty_output><jupyter_text>강제진동Forced Vibration $$m \frac{d^2x(t)}{dt^2} +c \frac{dx(t)}{dt} + x(t) = sin(t)$$<jupyter_code>forced_vib_eq = m * x(t).diff(t, t) + c * x(t).diff(t) + k * x(t) - sym.sin(t)
forced_vib_eq


result = sym.dsolve(forced_vib_eq)
result


sym.simplify(result)<jupyter_output><empty_output><jupyter_text>참고문헌References * SymPy Development Team, SymPy 1.4 documentation, sympy.org, 2019 04 10. [Online] Available : https://docs/sympy.org/latest/index.html.* SymPy Development Team, SymPy Tutorial, SymPy 1.4 documentation, sympy.org, 2019 04 10. [Online] Available : https://docs/sympy.org/latest/tutorial/index.html.* d84_n1nj4, "How to keep fractions in your equation output", Stackoverflow.com, 2017 08 12. [Online] Available : https://stackoverflow.com/a/45651175.* Python developers, "Fractions", Python documentation, 2019 10 12. [Online] Available : https://docs.python.org/3.7/library/fractions.html.* SymPy Development Team, codegen, SymPy 1.4 documentation, sympy.org, 2019 04 10. [Online] Available : https://docs/sympy.org/latest/modules/utilities/codegen.html.* <NAME>., Sympy : Symbolic Mathematics in Python, Scipy Lecture Notes, 2019 March,[Online] Available : http://www.scipy-lectures.org/packages/sympy.html [Accessed 2019 10 28]* MIT, Twitter, 2021 Feb. [Online] Available : https://twitter.com/MIT/status/1360971008325406721.  Final Bell마지막 종<jupyter_code># stackoverfow.com/a/24634221
import os
os.system("printf '\a'");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17151054010228264, 'fraction_numerical': 0.03717101160034926, 'mean_word_length': 5.481810832659661, 'pattern_counts': {'":': 0, '<': 138, '<?xml version=': 0, '>': 140, 'https://': 8, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '34', 'dir': 'jupyter-structured-clean-dedup', 'id': '257025', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3947, 'n_tokens_neox': 3815, 'n_words': 649, 'path': '45_sympy/10_sympy.ipynb', 'repo_name': 'kangwonlee/2109eca-nmisp-template', 'size': '8023'}