David C. Lay의 Linear Algebra에서 1.2장의 Example 3에 sympy적용.

  • RREF는 unique하기 때문에 sympy에서 지원함.

import numpy as np
import sympy as sp

# ----------------------------------
# colab에서 sympy의 값을 latex 지원하여 출력하기 위해 정의
def custom_latex_printer(exp, **options):
    from google.colab.output._publish import javascript
    url = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default"
    javascript(url=url)
    return sp.printing.latex(exp, **options)

sp.init_printing(use_latex="mathjax", latex_printer=custom_latex_printer)
# end ----------------------------------


A = np.array([
    [0 , 3, -6, 6, 4,-5],
    [3 ,-7 , 8,-5, 8, 9],
    [3 ,-9 ,12,-9, 6,15]
], dtype=float)

A = sp.Matrix(A)
rref_A = A.rref()
rref_A # rref and pivot columns

결과는 다음과 같음.
$$\displaystyle \left( \left[\begin{matrix}1 & 0 & -2.0 & 3.0 & 0 & -24.0\\0 & 1 & -2.0 & 2.0 & 0 & -7.0\\0 & 0 & 0 & 0 & 1 & 4.0\end{matrix}\right], \ \left( 0, \ 1, \ 4\right)\right)$$

References

https://gist.github.com/dsaint31x/72fa6d150b869c561eded8f745004913

 

LA_Ch01_02_Ex03.ipynb

LA_Ch01_02_Ex03.ipynb. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

반응형

+ Recent posts