2주차 강의

21 days 전, mindo 작성

# 예제 1 # 선형연립방정식을 행렬의 곱을 이용해 나타내고 첨가행렬을 구하라 # x + y = 2z = 9 # 2x + 4y - 3z = 1 # 3x + 6y - 5z = 0 A = matrix([[1,1,2], [2,4,-3], [3,6,-5]]) #X = vector([x, y, z]) #b = vector([9,1,0]) b = matrix([[9],[1],[0]]) # 첨가 행렬 print(A.augment(b)) 
       
[ 1  1  2  9]
[ 2  4 -3  1]
[ 3  6 -5  0]
[ 1  1  2  9]
[ 2  4 -3  1]
[ 3  6 -5  0]
# 예제 2 # 행렬 A의 기약 행 사다리꼴(RREF)를 구하라 A = matrix([[1,1,1,4,4],[2,3,4,9,16],[-2,0,3,-7,11]]) print A print 'RREF(A) = ' print A.rref() 
       
[ 1  1  1  4  4]
[ 2  3  4  9 16]
[-2  0  3 -7 11]
RREF(A) = 
[ 1  0  0  2 -1]
[ 0  1  0  3  2]
[ 0  0  1 -1  3]
[ 1  1  1  4  4]
[ 2  3  4  9 16]
[-2  0  3 -7 11]
RREF(A) = 
[ 1  0  0  2 -1]
[ 0  1  0  3  2]
[ 0  0  1 -1  3]
# 예제 3 # Gauss-Jorda 소거법을 이용해 연립방정식의 해를 구하라 A = matrix([[2,4,6],[2,-1,1],[3,0,-1]]) b = vector([18,8,3]) print A.augment(b) # 첨가행렬로 구성 print 'RREF([A:b])===' print A.augment(b).rref() # 해집합 구하기 print 'x=', A.solve_right(b) 
       
[ 2  4  6 18]
[ 2 -1  1  8]
[ 3  0 -1  3]
RREF([A:b])===
[ 1  0  0  2]
[ 0  1  0 -1]
[ 0  0  1  3]
x= (2, -1, 3)
[ 2  4  6 18]
[ 2 -1  1  8]
[ 3  0 -1  3]
RREF([A:b])===
[ 1  0  0  2]
[ 0  1  0 -1]
[ 0  0  1  3]
x= (2, -1, 3)
# 예제 4 # Gauss-Jordan 소거법을 이용해 연립방정식의 해를 구하라 A = matrix([[1,3,-2,0,2,0],[2,6,-5,-2,4,-3],[0,0,5,10,0,15],[2,6,0,8,4,18]]) b = vector([0,-1,5,6]) # 첨가행렬 print '[A:b]===' print A.augment(b) # RREF print 'RREF([A:b])===' print A.augment(b).rref() # 해 print 'result===' print A.solve_right(b) 
       
[A:b]===
[ 1  3 -2  0  2  0  0]
[ 2  6 -5 -2  4 -3 -1]
[ 0  0  5 10  0 15  5]
[ 2  6  0  8  4 18  6]
RREF([A:b])===
[  1   3   0   4   2   0   0]
[  0   0   1   2   0   0   0]
[  0   0   0   0   0   1 1/3]
[  0   0   0   0   0   0   0]
result===
(0, 0, 0, 0, 0, 1/3)
[A:b]===
[ 1  3 -2  0  2  0  0]
[ 2  6 -5 -2  4 -3 -1]
[ 0  0  5 10  0 15  5]
[ 2  6  0  8  4 18  6]
RREF([A:b])===
[  1   3   0   4   2   0   0]
[  0   0   1   2   0   0   0]
[  0   0   0   0   0   1 1/3]
[  0   0   0   0   0   0   0]
result===
(0, 0, 0, 0, 0, 1/3)
# 행렬의 연산 # 예제 1 A = matrix([[1,2,-4], [-2,1,3]]) B = matrix([[0,1,4], [-1,3,1]]) C = matrix([[1,1], [2,2]]) print 'A + B = ' print A+B print print '2*A = ' print 2*A print print '(-1)*C = ' print (-1)*C 
       
A + B = 
[ 1  3  0]
[-3  4  4]

2*A = 
[ 2  4 -8]
[-4  2  6]

(-1)*C = 
[-1 -1]
[-2 -2]
A + B = 
[ 1  3  0]
[-3  4  4]

2*A = 
[ 2  4 -8]
[-4  2  6]

(-1)*C = 
[-1 -1]
[-2 -2]
# 예제 2 # 행렬 A, B의 곱을 구하라 A = matrix([[1,2,-1],[3,1,0]]) B = matrix([[-2,1],[0,-3],[2,1]]) print A*B 
       
[-4 -6]
[-6  0]
[-4 -6]
[-6  0]
# 대각합 trace 확인 # tr(AB) = tr(BA) A = matrix(QQ, [[1,-2],[4,5]]) B = matrix(QQ, [[5,4],[-3,2]]) print (A*B).trace() print (B*A).trace() 
       
37
37
37
37
# 과제 1 #A = matrix([[2,1,1,-2],[3,-2,1,-6],[1,1,-1,-1],[5,-1,2,-8]]) #b = vector([1,-2,-1,3]) # random_matrix A = random_matrix(ZZ, 4, 7) # random_vector b = random_vector(4) print 'A' print A print 'b' print b # 첨가행렬 print '[A:b]' print A.augment(b) # RREF print 'RREF([A:b])' print A.augment(b).rref() print A.solve_right(b) 
       
A
[-5 -4 -2  1  1  1 -6]
[ 2  2 -4 -1 -1 -1  0]
[ 1  2 -5  1 -1  2 -5]
[-4  2 -8 12  1 13 -1]
b
(-2, 0, -2, 0)
[A:b]
[-5 -4 -2  1  1  1 -6 -2]
[ 2  2 -4 -1 -1 -1  0  0]
[ 1  2 -5  1 -1  2 -5 -2]
[-4  2 -8 12  1 13 -1  0]
RREF([A:b])
[     1      0      0      0   37/3 -113/3  619/3     86]
[     0      1      0      0  -27/2   81/2 -441/2    -92]
[     0      0      1      0   -5/3   16/3  -86/3    -12]
[     0      0      0      1   16/3  -44/3  259/3     36]
(86, -92, -12, 36, 0, 0, 0)
A
[-5 -4 -2  1  1  1 -6]
[ 2  2 -4 -1 -1 -1  0]
[ 1  2 -5  1 -1  2 -5]
[-4  2 -8 12  1 13 -1]
b
(-2, 0, -2, 0)
[A:b]
[-5 -4 -2  1  1  1 -6 -2]
[ 2  2 -4 -1 -1 -1  0  0]
[ 1  2 -5  1 -1  2 -5 -2]
[-4  2 -8 12  1 13 -1  0]
RREF([A:b])
[     1      0      0      0   37/3 -113/3  619/3     86]
[     0      1      0      0  -27/2   81/2 -441/2    -92]
[     0      0      1      0   -5/3   16/3  -86/3    -12]
[     0      0      0      1   16/3  -44/3  259/3     36]
(86, -92, -12, 36, 0, 0, 0)
A = matrix([[2,1,1,-2],[3,-2,1,-6],[1,1,-1,-1],[5,-1,2,-8]]) b = vector([1,-2,-1,3]) print A.solve_right(b) 
       
Traceback (click to the left of this block for traceback)
...
ValueError: matrix equation has no solutions
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_25.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("QSA9IG1hdHJpeChbWzIsMSwxLC0yXSxbMywtMiwxLC02XSxbMSwxLC0xLC0xXSxbNSwtMSwyLC04XV0pCmIgPSB2ZWN0b3IoWzEsLTIsLTEsM10pCnByaW50IEEuc29sdmVfcmlnaHQoYik="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmp46bf_k/___code___.py", line 5, in <module>
    exec compile(u'print A.solve_right(b)
  File "", line 1, in <module>
    
  File "matrix2.pyx", line 358, in sage.matrix.matrix2.Matrix.solve_right (sage/matrix/matrix2.c:5361)
  File "matrix2.pyx", line 476, in sage.matrix.matrix2.Matrix._solve_right_general (sage/matrix/matrix2.c:6182)
ValueError: matrix equation has no solutions