2020년 11월 25일 수요일

선형대수코드_CH4

4-1

A=Matrix([[6,2,3,5,0],[4,0,2,6,4],[-2,0,-1,-3,-2],[4,1,2,2,-1]])
print(A)
Matrix([[6, 2, 3, 5, 0], [4, 0, 2, 6, 4], [-2, 0, -1, -3, -2], [4, 1, 2, 2, -1]])
A.rref()
(Matrix([[1, 0, 1/2, 0, -1/2],
        [0, 1, 0, 0, -1],
        [0, 0, 0, 1, 1],
        [0, 0, 0, 0, 0]]),
        (0, 1, 3))


S=A[:,[0,1,3]]
print(S)
Matrix([[6, 2, 5], [4, 0, 6], [-2, 0, -3], [4, 1, 2]])

S.columnspace()
[Matrix([[ 6],
        [ 4],
        [-2],
        [ 4]]),
   Matrix([[2],
        [0],
        [0],
        [1]]),
   Matrix([[ 5],
        [ 6],
        [-3],
        [ 2]])]

S.nullspace()
[]

4-3(c)

a, b, c, d, t=symbols('a b c d t')
A=Matrix([t**2+2,2*t**2-t+1,t+2, t**2+t+4])
print(A)
Matrix([[t**2 + 2], [2*t**2 - t + 1], [t + 2], [t**2 + t + 4]])

B=Matrix([[a],[b],[c],[d]])
AB=A.T*B
print(AB)
Matrix([[a*(t**2 + 2) + b*(2*t**2 - t + 1) + c*(t + 2) + d*(t**2 + t + 4)]])

AB2=collect(expand(AB[0]), t)
print(AB2)
2*a + b + 2*c + 4*d + t**2*(a + 2*b + d) + t*(-b + c + d)

print(AB2.coeff(t, 2))
a + 2*b + d

print(AB2.coeff(t, 1))
-b + c + d

print(AB2.coeff(t,0))
2*a + b + 2*c + 4*d

coef=Matrix([[1,2,0,1],[0,-1, 1,1], [2, 1, 2, 4]])
print(coef)
Matrix([[1, 2, 0, 1], [0, -1, 1, 1], [2, 1, 2, 4]])

coef.rref()
(Matrix([[1, 0, 0, 1],
        [0, 1, 0, 0],
        [0, 0, 1, 1]]),
        (0, 1, 2))

다항식 집합들 중 기저는 3개이므로 3차원입니다.

4-4

이 문제의 집합 S의 각 다항식은 t4, t3, t2, t, 1에 대한 계수행렬의 형식으로 다음과 같이 나타낼 수 있습니다.

$\left[\begin{matrix}1 & 0 & 1 & 2 & 1\\1 & 0 & 1 & 2 & 2\\2 & 1 & 0 & 1 & 2\\1 & 1 & -1 & -1 & 0\end{matrix}\right]\left[\begin{matrix}t^4\\t^3\\t^2\\t\\1\end{matrix}\right]$

위 계수행렬에서 기저를 발견하는 것은 그 행렬의 피벗열을 찾는 것입니다. 계수행렬을 A라고 하면 다음과 같습니다.

A=Matrix([[1,0,1,2,1],[1,0,1,2,2],[2,1,0,1,2],[1,1,-1,-1,0]])
print(A)
Matrix([[1, 0, 1, 2, 1],
        [1, 0, 1, 2, 2],
        [2, 1, 0, 1, 2],
        [1, 1, -1, -1, 0]])


A.rref()
(Matrix([[1, 0, 1, 2, 0],
        [0, 1, -2, -3, 0],
        [0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0]]),
        (0, 1, 4))

위 결과로 부터 A 행렬 중 3개의 열벡터가 기저임을 알 수 있습니다. 이 결과는 A의 열공간과 같습니다.

A.columnspace()
[Matrix([[1],[1],[2],[1]]),
   Matrix([[0],[0],[1],[1]]),
   Matrix([[1],[2],[2],[0]])]

4-5

5. 다음 행렬 A에 대해 (a)와 (b)를 답하세요.

x1, x2, x3, x4=symbols('x1, x2, x3, x4')
A=Matrix([[1,1,1,1],[1,1,2,2],[2,2,3,3]])
X=Matrix(4,1, [x1, x2, x3, x4])
A
$\left[\begin{matrix}1 & 1 & 1 & 1\\1 & 1 & 2 & 2\\2 & 2 & 3 & 3\end{matrix}\right]$

X
$\left[\begin{matrix}x_{1}\\x_{2}\\x_{3}\\x_{4}\end{matrix}\right]$

C=Matrix(3,1, [0,0,0])
C
$\left[\begin{matrix}0\\0\\0\end{matrix}\right]$

(a)Ax=0의 해와 그 해공간의 기저?

au=A.row_join(C)
au
$\left[\begin{matrix}1 & 1 & 1 & 1 & 0\\1 & 1 & 2 & 2 & 0\\2 & 2 & 3 & 3 & 0\end{matrix}\right]$

au.rref()
(Matrix([[1, 1, 0, 0, 0],
        [0, 0, 1, 1, 0],
        [0, 0, 0, 0, 0]]),
        (0, 2))

선도변수 0, 2 열, 자유변수 1, 3 열이므로 다음과 같이 나타낼 수 있습니다.
$\begin{align}x_1&=-x_2\\x_3&=-x_4\end{align}$
즉, 다양한 해 존재

sol=X.subs({x1:-x2, x3:-x4})
sol
$\left[\begin{matrix}- x_{2}\\x_{2}\\- x_{4}\\x_{4}\end{matrix}\right]$

$\left[\begin{matrix}- x_{2}\\x_{2}\\- x_{4}\\x_{4}\end{matrix}\right]=\left[\begin{matrix}0&-1&0&0\\0&1&0&0\\0&0&0&-1\\0&0&0&1\end{matrix}\right]\left[\begin{matrix} x_{1}\\x_{2}\\x_{3}\\x_{4}\end{matrix}\right]$
위 식의 계수행렬에 대한 기저를 계산해 보면 다음과 같습니다.

sol1=Matrix([[0,-1,0,0],[0,1,0,0],[0,0,0,-1],[0,0,0,1]])
sol1.rref()
(Matrix([[0, 1, 0, 0],
        [0, 0, 0, 1],
        [0, 0, 0, 0],
        [0, 0, 0, 0]]),
(1, 3))

결과적으로 (1, 3)열이 기저이며 이것은 위 계수행렬의 열공간과 같습니다.

sol1.columnspace()
[Matrix([[-1],[ 1],[ 0],[ 0]]),
   Matrix([[ 0],[ 0],[-1],[ 1]])]

(b) Ax=b (b는 3차원)은 해를 가집니까?

b1,b2,b3=symbols("b1 b2 b3")
b=Matrix(3,1, [b1, b2, b3])
b
$\left[\begin{matrix}b_{1}\\b_{2}\\b_{3}\end{matrix}\right]$

위 행렬 b는 다음과 같이 나타낼수 있습니다.
$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]\left[\begin{matrix}b_{1}\\b_{2}\\b_{3}\end{matrix}\right]$
그러므로 Ax=b는 다음과 같이 정리됩니다.
$\left[\begin{matrix}1 & 1 & 1 & 1\\1 & 1 & 2 & 2\\2 & 2 & 3 & 3\end{matrix}\right]\left[\begin{matrix}x_{1}\\x_{2}\\x_{3}\\x_{4}\end{matrix}\right]=\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]\left[\begin{matrix}b_{1}\\b_{2}\\b_{3}\end{matrix}\right]$

au_b=A.row_join(Matrix([[1,0,0],[0,1,0],[0,0,1]]))
au_b
$\left[\begin{matrix}1 & 1 & 1 & 1 & 1 & 0 & 0\\1 & 1 & 2 & 2 & 0 & 1 & 0\\2 & 2 & 3 & 3 & 0 & 0 & 1\end{matrix}\right]$
au_b=A.row_join(b)
au_b
$\left[\begin{matrix}1 & 1 & 1 & 1 & b_{1}\\1 & 1 & 2 & 2 & b_{2}\\2 & 2 & 3 & 3 & b_{3}\end{matrix}\right]$
au_b.rref()
(Matrix([[1, 1, 0, 0, 0],
        [0, 0, 1, 1, 0],
        [0, 0, 0, 0, 1]]),
        (0, 2, 4))

위 결과에서 2행은 성립할 수 없습니다. 그러므로 Ax=b의 해는 존재하지 않습니다.

4_6

0벡터는 포함하는 모든 집합은 선형 종속이므로 부분공간의 기저가 될 수 없습니다.

4_7

다음 행렬 A의 열공간과 영공간?
$\left[\begin{matrix}-3 & 5 & -22 & 5 & -49\\-6 & 13 & -59 & 10 & -107\\-6 & 4 & -14 & 15 & -105\end{matrix}\right]$

A=Matrix([[-3, 5, -22, 5, -49],[-6, 13, -59, 10, -107],[-6, 4, -14, 15, -105]])
A.rref()
(Matrix([[1, 0, -1, 0, 3],
        [0, 1, -5, 0, -3],
        [0, 0, 0, 1, -5]]),
        (0, 1, 3))


A.columnspace()
[Matrix([[-3],
        [-6],
        [-6]]),
   Matrix([[ 5],
        [13],
        [ 4]]),
   Matrix([[ 5],
        [10],
        [15]])]


A.nullspace()
[Matrix([[1],
         [5],
         [1],
         [0],
         [0]]),
   Matrix([[-3],
        [ 3],
        [ 0],
        [ 5],
  [ 1]])]


위의 A의 기약행사다리꼴로부터 0, 1, 3 열이 피벗열이 되며 나머지 2, 4열은 자유변수를 포함하는 것으로 x3, x4가 자유변수가 됩니다. 그러므로 A의 영공간(해공간)은 이 두 변수에 따라 매우 다양합니다.
$\begin{align}x_1&=x_3-3x_5\\x_2&=5x_3+3x_5\\x_4&=5x_5\end{align}$
위 해집합을 행렬식으로 정리하면 다음과 같습니다.
$\left[\begin{matrix}x_1\\x_2\\x_3\\x_4\\x_5\end{matrix}\right] = \left[\begin{matrix}0 & 0 & 1 & 0 & -1\\0 & 0 & 5 & 0 & 3\\0 & 0 & 1 & 0 & 0\\0 & 0 & 0 & 0 & 5\\0 & 0 & 0 & 0 & 1\end{matrix}\right]\left[\begin{matrix}x_1\\x_2\\x_3\\x_4\\x_5\end{matrix}\right]$

$S=\left[\begin{matrix}0 & 0 & 1 & 0 & -1\\0 & 0 & 5 & 0 & 3\\0 & 0 & 1 & 0 & 0\\0 & 0 & 0 & 0 & 5\\0 & 0 & 0 & 0 & 1\end{matrix}\right]$

A에 의한 해 집합은 R5 공간에서 행렬 S와의 선형결합이 성립됩니다. 이 부분의 열공간이 A의 영공간이 됩니다.

S=Matrix([[0,0,1,0,-1],[0,0,5,0,3],[0,0,1,0,0],[0,0,0,0,5],[0,0,0,0,1]])
latex(S.rref())
$\left( \left[\begin{matrix}0 & 0 & 1 & 0 & 0\\0 & 0 & 0 & 0 & 1\\0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0\end{matrix}\right], \ \left( 2, \ 4\right)\right)$

latex(S.columnspace())
$\left[ \left[\begin{matrix}1\\5\\1\\0\\0\end{matrix}\right], \ \left[\begin{matrix}-1\\3\\0\\5\\1\end{matrix}\right]\right]$

4_8

x1+x2+x3+x4+x5=0의 부분공간을 위한 기저?

위 식의 해는 각 변수에 상호 의존관계입니다. 예를들어 $x_1 = -x_2-x_3-x_4-x_5$로 x1의 경우 나머지 변수들의 값에 의해 결정됩니다. 물론 변수 역시 동일합니다. 이 관계를 행렬 형태로 나타내보면 다음과 같습니다.

$\left[\begin{matrix}x_1\\x_2\\x_3\\x_4\\x_5\end{matrix}\right] = \left[\begin{matrix}-x_2-x_3-x_4-x_5\\x_2\\x_3\\x_4\\x_5\end{matrix}\right]$

위 식의 우항에 대한 표준행렬 A는 다음과 같습니다.

$A=\left[\begin{matrix}0 & -1 & -1 & -1 & -1\\0 & 1 & 0 & 0 & 0\\0 & 0 & 1 & 0 & 0\\0 & 0 & 0 & 1 & 0\\0 & 0 & 0 & 0 & 1\end{matrix}\right]$

S의 columnspace가 기저가 됩니다. 다시말하면 S의 기약행사다리꼴에서의 피벗열이 기저가 됩니다.

A=diag(1,1,1,1,1)
A[0,0]=0
for i in range(1, 5):
A[0, i]=-1
A
$\left[\begin{matrix}0 & -1 & -1 & -1 & -1\\0 & 1 & 0 & 0 & 0\\0 & 0 & 1 & 0 & 0\\0 & 0 & 0 & 1 & 0\\0 & 0 & 0 & 0 & 1\end{matrix}\right]$

latex(A.rref())
$\left( \left[\begin{matrix}1 & 0 & 0 & 0 & 0\\0 & 1 & 0 & 0 & 0\\0 & 0 & 1 & 0 & 0\\0 & 0 & 0 & 1 & 0\\0 & 0 & 0 & 0 & 1\end{matrix}\right], \ \left( 0, \ 1, \ 2, \ 3, \ 4\right)\right)$

latex(A.columnspace())
$\left[ \left[\begin{matrix}-1\\1\\0\\0\\0\end{matrix}\right],\; \left[\begin{matrix}-1\\0\\1\\0\\0\end{matrix}\right], \; \left[\begin{matrix}-1\\0\\0\\1\\0\end{matrix}\right], \; \left[\begin{matrix}-1\\0\\0\\0\\1\end{matrix}\right]\right]$

댓글 없음:

댓글 쓰기

CalculusMadeEasy_CH1

1-6 a=np.array([[1],[2],[3]]) la.norm(a) 3.7416573867739413 b=np.array([[-1],[-3],[-4]]) la.norm(b) 5.0990195135927845 c=np.array([[...