# `sympy`



`scipy` 계열은 [`sympy`](https://www.sympy.org)라는 *기호 처리기*도 포함하고 있다.<br>
`scipy` stack also includes [`sympy`](https://www.sympy.org), a *symbolic processor*.



2006년 이후 2019 까지 800명이 넘는 개발자가 작성한 코드를 제공하였다.<br>
Since 2006, more than 800 developers contributed so far in 2019.



## 기호 연산 예<br>Examples of symbolic processing



`sympy` 모듈을 `sy` 라는 이름으로 불러온다.<br>Import `sympy` module in the name of `sy`.




```python
import sympy as sy
sy.init_printing()


```

비교를 위해 `numpy` 모듈도 불러온다.<br>
Import `numpy` module to compare.




```python
import numpy as np


```

### 제곱근<br>Square root



10의 제곱근을 구해보자.<br>Let't find the square root of ten.




```python
np.sqrt(10)


```


```python
sy.sqrt(10)


```

10의 제곱근을 제곱해보자.<br>Let't square the square root of ten.




```python
np.sqrt(10) ** 2


```


```python
sy.sqrt(10) ** 2


```

위 결과의 차이에 대해 어떻게 생각하는가?<br>
What do you think about the differences of the results above?



### 분수<br>Fractions



10 / 3 을 생각해보자.<br>Let't think about 10/3.




```python
ten_over_three = 10 / 3


```


```python
ten_over_three


```


```python
ten_over_three * 3


```


```python
import fractions


```


```python
fr_ten_over_three = fractions.Fraction(10 ,3)


```


```python
fr_ten_over_three


```


```python
fr_ten_over_three * 3


```


```python
sy_ten_over_three = sy.Rational(10, 3)


```


```python
sy_ten_over_three


```


```python
sy_ten_over_three * 3


```

위 결과의 차이에 대해 어떻게 생각하는가?<br>
What do you think about the differences of the results above?



### 변수를 포함하는 수식<br>Expressions with variables



사용할 변수를 정의한다.<br>Define variables to use.




```python
a, b, c, x = sy.symbols('a b c x')
theta, phi = sy.symbols('theta phi')


```

변수들을 한번 살펴보자.<br>Let's take a look at the variables




```python
a, b, c, x


```


```python
theta, phi


```

변수를 조합하여 새로운 수식을 만들어 보자.<br>
Let's make equations using variables.




```python
y = a * x + b


```


```python
y


```


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


```


```python
z


```


```python
w = a * sy.sin(theta) ** 2 + b


```


```python
w


```


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


```


```python
p


```


```python
sy.expand(p, x)


```


```python
sy.collect(_, x)


```

### 미적분<br>Calculus




```python
z.diff(x)


```


```python
sy.integrate(z, x)


```


```python
w.diff(theta)


```


```python
sy.integrate(w, theta)


```

### 근<br>Root




```python
z_sol_list = sy.solve(z, x)


```


```python
z_sol_list


```


```python
sy.solve(2* sy.sin(theta) ** 2 - 1, theta)


```

### 코드 생성<br>Code generation




```python
print(sy.python(z_sol_list[0]))


```


```python
import sympy.utilities.codegen as sc


```


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


```


```python
c_name


```


```python
print(c_code)


```


```python
h_name


```


```python
print(c_header)


```

### 연립방정식<br>System of equations




```python
a1, a2, a3 = sy.symbols('a1:4')
b1, b2, b3 = sy.symbols('b1:4')
c1, c2 = sy.symbols('c1:3')
x1, x2 = sy.symbols('x1:3')


```


```python
eq1 = sy.Eq(
    a1 * x1 + a2 * x2, 
    c1,
)


```


```python
eq1


```


```python
eq2 = sy.Eq(
    b1 * x1 + b2 * x2,
    c2,
)


```


```python
eq_list = [eq1, eq2]


```


```python
eq_list


```


```python
sy.solve(eq_list, (x1, x2))


```

## 참고문헌<br>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.



## Final Bell<br>마지막 종




```python
# stackoverfow.com/a/24634221
import os
os.system("printf '\a'");


```


```python

```

e50ac68a04bda1da28083127d607b4e5d0de7770
ipynb
Jupyter Notebook
70_sympy/10_sympy.ipynb
kangwonlee/2009eca-nmisp-template
46a09c988c5e0c4efd493afa965d4a17d32985e8
BSD-3-Clause
70_sympy/10_sympy.ipynb
kangwonlee/2009eca-nmisp-template
46a09c988c5e0c4efd493afa965d4a17d32985e8
BSD-3-Clause
70_sympy/10_sympy.ipynb
kangwonlee/2009eca-nmisp-template
46a09c988c5e0c4efd493afa965d4a17d32985e8
BSD-3-Clause
Qwen/Qwen-72B
1. YES
2. YES