# 3주차 강의

## 64 days 전, mindo 작성

# 예제 4 A = matrix(QQ, [[1,4,3],[2,5,6],[0,0,0]]) A.is_invertible() # A가 가역인지 아닌지 확인
 False False
# 예제 5 A = matrix(2, 2, [3,5,1,2]) print 'A=' print A print print 'inverse of A = ' print A.inverse()
 A= [3 5] [1 2] inverse of A = [ 2 -5] [-1 3] A= [3 5] [1 2] inverse of A = [ 2 -5] [-1 3]
# 예제 # A, B가 가역일 때, AB = BA, A^(-1)*B^(-1) = B^(-1)*A^(-1) A = matrix(ZZ, 2, 2, [3,5,1,2]) B = matrix(ZZ, 2, 2, [1,3,2,7]) AB = A*B print AB.inverse() print print B.inverse()*A.inverse()
 [ 17 -44] [ -5 13] [ 17 -44] [ -5 13] [ 17 -44] [ -5 13] [ 17 -44] [ -5 13]
# 기본행렬 3가지 유형 E1 = elementary_matrix(4, row1=2, row2=3) # R3 <-> R4 E2 = elementary_matrix(4, row1=2, scale=-3) # (-3)*R3 E3 = elementary_matrix(4, row1=0, row2=3, scale=7) # 7*R4 + R1 print E1 print E2 print E3
 [1 0 0 0] [0 1 0 0] [0 0 0 1] [0 0 1 0] [ 1 0 0 0] [ 0 1 0 0] [ 0 0 -3 0] [ 0 0 0 1] [1 0 0 7] [0 1 0 0] [0 0 1 0] [0 0 0 1] [1 0 0 0] [0 1 0 0] [0 0 0 1] [0 0 1 0] [ 1 0 0 0] [ 0 1 0 0] [ 0 0 -3 0] [ 0 0 0 1] [1 0 0 7] [0 1 0 0] [0 0 1 0] [0 0 0 1]
# 기본행렬의 역행렬은 기본행렬 print E1.inverse()
 [1 0 0 0] [0 1 0 0] [0 0 0 1] [0 0 1 0] [1 0 0 0] [0 1 0 0] [0 0 0 1] [0 0 1 0]
# RREF를 이용해 A의 역행렬 구하기 A = matrix([[1,-1,2],[-1,0,2],[-6,4,11]]) I = identity_matrix(3) AI = A.augment(I) print '[A:I]=' print AI AI_rref = AI.rref() print print 'RREF([A:I])=' print AI_rref print A2 = AI_rref.submatrix(0, 3, 3, 3) print 'A^(-1)=' print A2
 [A:I]= [ 1 -1 2 1 0 0] [-1 0 2 0 1 0] [-6 4 11 0 0 1] RREF([A:I])= [ 1 0 0 8/15 -19/15 2/15] [ 0 1 0 1/15 -23/15 4/15] [ 0 0 1 4/15 -2/15 1/15] A^(-1)= [ 8/15 -19/15 2/15] [ 1/15 -23/15 4/15] [ 4/15 -2/15 1/15] [A:I]= [ 1 -1 2 1 0 0] [-1 0 2 0 1 0] [-6 4 11 0 0 1] RREF([A:I])= [ 1 0 0 8/15 -19/15 2/15] [ 0 1 0 1/15 -23/15 4/15] [ 0 0 1 4/15 -2/15 1/15] A^(-1)= [ 8/15 -19/15 2/15] [ 1/15 -23/15 4/15] [ 4/15 -2/15 1/15]
# 특수행렬 # 대각선행렬 H = diagonal_matrix([-3, -2, 1]) # 단위행렬 K = identity_matrix(3) # 영행렬 J = matrix(2, 2, 0)
A = matrix([[1,2,3],[4,5,6],[7,8,9]]) D = diagonal_matrix([1,2,3]) print A*D
 [ 1 4 9] [ 4 10 18] [ 7 16 27] [ 1 4 9] [ 4 10 18] [ 7 16 27]
# 대칭행렬, 반대칭행렬 확인하기 A = matrix([[1,2,3],[2,4,5],[3,5,6]]) B = matrix([[0,1,-2],[-1,0,3],[2,-3,0]]) print bool(A == A.transpose()) print bool(B == -B.transpose())
 True True True True

# 3주차 과제 # 7차 이상의 랜덤 정사각행렬 구성 n = 7 A = random_matrix(QQ, n, n) print 'A= ' print A print if A.is_invertible(): print 'matrix A is invertible.' print 'A^-1=' print A.inverse() else: print 'matrix A is not invertible.' print # solution using rref I = identity_matrix(n) AI_rref = A.augment(I).rref() print 'RREF([A:I])=' print AI_rref A1 = AI_rref.submatrix(0,0,n,n) A2 = AI_rref.submatrix(0,n,n,n) print # find out if A1 is identity matrix if A1 == I: print 'A2 is A^-1' print 'A^-1 using rref=' print A2 else: print 'matrix A is not invertible.'
 A= [ -1 -1 0 -1 0 -1 -2] [ 0 0 0 -2 0 1 1] [ 0 0 1 0 2 -1 -2] [-1/2 -1 -2 0 -2 1/2 1] [ 0 0 -2 2 1 1 1] [ -1 0 1 1/2 1 -1 -1] [ 1 -1 0 -1 1 -1 -1] matrix A is invertible. A^-1= [ -11 2 18 14 -5 0 -3] [ 115 -24 -190 -150 52 -6 34] [ -77 16 127 100 -35 4 -23] [ -40 8 66 52 -18 2 -12] [ 6 -1 -10 -8 3 0 2] [ -95 20 158 124 -43 4 -29] [ 15 -3 -26 -20 7 0 5] RREF([A:I])= [ 1 0 0 0 0 0 0 -11 2 18 14 -5 0 -3] [ 0 1 0 0 0 0 0 115 -24 -190 -150 52 -6 34] [ 0 0 1 0 0 0 0 -77 16 127 100 -35 4 -23] [ 0 0 0 1 0 0 0 -40 8 66 52 -18 2 -12] [ 0 0 0 0 1 0 0 6 -1 -10 -8 3 0 2] [ 0 0 0 0 0 1 0 -95 20 158 124 -43 4 -29] [ 0 0 0 0 0 0 1 15 -3 -26 -20 7 0 5] A2 is A^-1 A^-1 using rref= [ -11 2 18 14 -5 0 -3] [ 115 -24 -190 -150 52 -6 34] [ -77 16 127 100 -35 4 -23] [ -40 8 66 52 -18 2 -12] [ 6 -1 -10 -8 3 0 2] [ -95 20 158 124 -43 4 -29] [ 15 -3 -26 -20 7 0 5] A= [ -1 -1 0 -1 0 -1 -2] [ 0 0 0 -2 0 1 1] [ 0 0 1 0 2 -1 -2] [-1/2 -1 -2 0 -2 1/2 1] [ 0 0 -2 2 1 1 1] [ -1 0 1 1/2 1 -1 -1] [ 1 -1 0 -1 1 -1 -1] matrix A is invertible. A^-1= [ -11 2 18 14 -5 0 -3] [ 115 -24 -190 -150 52 -6 34] [ -77 16 127 100 -35 4 -23] [ -40 8 66 52 -18 2 -12] [ 6 -1 -10 -8 3 0 2] [ -95 20 158 124 -43 4 -29] [ 15 -3 -26 -20 7 0 5] RREF([A:I])= [ 1 0 0 0 0 0 0 -11 2 18 14 -5 0 -3] [ 0 1 0 0 0 0 0 115 -24 -190 -150 52 -6 34] [ 0 0 1 0 0 0 0 -77 16 127 100 -35 4 -23] [ 0 0 0 1 0 0 0 -40 8 66 52 -18 2 -12] [ 0 0 0 0 1 0 0 6 -1 -10 -8 3 0 2] [ 0 0 0 0 0 1 0 -95 20 158 124 -43 4 -29] [ 0 0 0 0 0 0 1 15 -3 -26 -20 7 0 5] A2 is A^-1 A^-1 using rref= [ -11 2 18 14 -5 0 -3] [ 115 -24 -190 -150 52 -6 34] [ -77 16 127 100 -35 4 -23] [ -40 8 66 52 -18 2 -12] [ 6 -1 -10 -8 3 0 2] [ -95 20 158 124 -43 4 -29] [ 15 -3 -26 -20 7 0 5]