본문 바로가기
코딩/Python

Sympy 대입 방법

by leco 2021. 2. 13.
# 라이브러리 불러오기
from sympy import symbols, Matrix
import numpy as np
# symbol 설정
x, y, z = symbols('x, y, z')

1. 컨테이너 없는 경우

equ = x + y + z

# 1개 대입
equ.subs(x,1)

# 2개 이상 대입
equ.subs([(x, 1), (y, 1)])

2. 컨테이너가 있는 경우 (list, ndarray...)

ex_list = [x, y, z]
ex_ndarray = np.array([x, y, z])
# list
ex_list.subs(x, 1)

# ndarray
ex_ndarray.subs(x, 1)

모두 오류가 발생한다.


# 방법1
for i in range(len(ex_ndarray)):
    ex_ndarray[i] = ex_ndarray[i].subs(x,1)
ex_ndarray
# 방법2 (Sympy의 Matrix)
ex_Matrix = Matrix(ex_ndarray)
ex_Matrix.subs(x,1)


Photo by delfi de la Rua on Unsplash

'코딩 > Python' 카테고리의 다른 글

Sympy 북마크  (0) 2021.02.12
Matplotlib 북마크  (0) 2021.02.12
Numpy 북마크  (0) 2021.02.12

댓글