# 7.8 역행렬. Gauss-Jordan 소거법

### 행렬 $A$

A=matrix(RDF, 3, 3, [-1, 1, 2, 3, -1, 1, -1, 3, 4]) print "A=" show(A)
 A= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} -1.0 & 1.0 & 2.0 \\ 3.0 & -1.0 & 1.0 \\ -1.0 & 3.0 & 4.0 \end{array}\right) A=

### 역행렬이 존재하는지 확인.

print "Is A invertible?" A.is_invertible()
 Is A invertible? True Is A invertible? True

### 행렬식 $\det A \neq 0$을 확인. $\Rightarrow$ 역행렬이 존재함.

print "det(A)=", A.det()
 det(A)= 10.0 det(A)= 10.0

### 내장함수를 이용해서 역행렬을 찾음.

invofA=A.inverse() print "A^(-1)=" show(invofA)
 A^(-1)= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} -0.7 & 0.2 & 0.3 \\ -1.3 & -0.2 & 0.7 \\ 0.8 & 0.2 & -0.2 \end{array}\right) A^(-1)=

### 단위행렬 $I$

I=identity_matrix(3) print "I=" show(I)
 I= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array}\right) I=

### 첨가행렬 $[A|I]$

AI=A.augment(I) print "[A:I] = " show(AI)
 [A:I] = \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr} -1.0 & 1.0 & 2.0 & 1.0 & 0.0 & 0.0 \\ 3.0 & -1.0 & 1.0 & 0.0 & 1.0 & 0.0 \\ -1.0 & 3.0 & 4.0 & 0.0 & 0.0 & 1.0 \end{array}\right) [A:I] =

### $$R_3 - R_1$$

AI.add_multiple_of_row(1,0,3) AI.add_multiple_of_row(2,0,-1) print "[U:H] = " show(AI)
 [U:H] = \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr} -1.0 & 1.0 & 2.0 & 1.0 & 0.0 & 0.0 \\ 0.0 & 2.0 & 7.0 & 3.0 & 1.0 & 0.0 \\ 0.0 & 2.0 & 2.0 & -1.0 & 0.0 & 1.0 \end{array}\right) [U:H] =

### $$R_3 - R_2$$

AI.add_multiple_of_row(2,1,-1) print "[U:H] = " show(AI)
 [U:H] = \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr} -1.0 & 1.0 & 2.0 & 1.0 & 0.0 & 0.0 \\ 0.0 & 2.0 & 7.0 & 3.0 & 1.0 & 0.0 \\ 0.0 & 0.0 & -5.0 & -4.0 & -1.0 & 1.0 \end{array}\right) [U:H] =

### $$-R_1, \quad \frac{1}{2} R_2, \quad -\frac{1}{5}R_3$$

AI.rescale_row(0,-1) AI.rescale_row(1,0.5) AI.rescale_row(2,-0.2) print "[U:H] = " show(AI)
 [U:H] = \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr} 1.0 & -1.0 & -2.0 & -1.0 & -0.0 & -0.0 \\ 0.0 & 1.0 & 3.5 & 1.5 & 0.5 & 0.0 \\ -0.0 & -0.0 & 1.0 & 0.8 & 0.2 & -0.2 \end{array}\right) [U:H] =

### $$R_1 + 2 R_3$$

AI.add_multiple_of_row(0,2,2) AI.add_multiple_of_row(1,2,-3.5) print "[U:H] = " show(AI)
 [U:H] = \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr} 1.0 & -1.0 & 0.0 & 0.6 & 0.4 & -0.4 \\ 0.0 & 1.0 & 0.0 & -1.3 & -0.2 & 0.7 \\ -0.0 & -0.0 & 1.0 & 0.8 & 0.2 & -0.2 \end{array}\right) [U:H] =

### $$R_1 + R_2$$

AI.add_multiple_of_row(0,1,1) print "[I:A^(-1)]=" show(AI)
 [I:A^(-1)]= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr} 1.0 & 0.0 & 0.0 & -0.7 & 0.2 & 0.3 \\ 0.0 & 1.0 & 0.0 & -1.3 & -0.2 & 0.7 \\ -0.0 & -0.0 & 1.0 & 0.8 & 0.2 & -0.2 \end{array}\right) [I:A^(-1)]=

### 6단계. 첨가행렬 $[A | I]$에서 $A \rightarrow I$로 변형되었으므로 $I \rightarrow A^{-1}$로 변형되었다. 역행렬 $A^{-1}$의 추출.

invA=AI[0:3,3:6] print "A^(-1)=" show(invA)
 A^(-1)= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} -0.7 & 0.2 & 0.3 \\ -1.3 & -0.2 & 0.7 \\ 0.8 & 0.2 & -0.2 \end{array}\right) A^(-1)=

### $A A^{-1} = I$ 와 $A^{-1} A = I$를 확인

E = invA*A I = E.n(digits=2) print "I=" show(I)
 I= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1.0 & 0.00 & 0.00 \\ 0.00 & 1.0 & 0.00 \\ 0.00 & 0.00 & 1.0 \end{array}\right) I=

### 행렬 $A$

A=matrix(QQ, 3, 3, [-0.5, 0, 0, 0, 4, 0, 0, 0, 1]) print "A=" show(A)
 A= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} -\frac{1}{2} & 0 & 0 \\ 0 & 4 & 0 \\ 0 & 0 & 1 \end{array}\right) A=

### 역행렬 $A^{-1}$는 $A$의 대각원소 각각의 역수를 취하여 구한다.

invA=A.inverse() print "A^(-1)=" show(invA)
 A^(-1)= \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} -2 & 0 & 0 \\ 0 & \frac{1}{4} & 0 \\ 0 & 0 & 1 \end{array}\right) A^(-1)=