The linear algebra that you will definitely learn at a science university is summarized in an easy-to-understand and logical manner. By the way, I implemented it in Python. Occasionally, it may be implemented in Julia. .. .. ・ Learn by running with Python! New Mathematics Textbook-Basic Knowledge Required for Machine Learning / Deep Learning- ・ World Standard MIT Textbook Strang Linear Algebra Introduction Understand linear algebra based on and implement it in python.
・ Jupyter Notebook ・ Language: Python3, Julia 1.4.0
E = \begin{pmatrix}1 & 0\\0 & 1\end{pmatrix},\begin{pmatrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{pmatrix}
It is a matrix in which 1s are lined up diagonally from the upper left to the lower right, and the other elements are 0.
Even if the nth-order square matrix is multiplied by the identity matrix with the same number of elements, the output values are the same. Furthermore, the order of products does not matter.
AE = \begin{pmatrix}a & b\\c & d\end{pmatrix}\begin{pmatrix}1 & 0\\0 & 1\end{pmatrix}=\begin{pmatrix}a & b\\c & d\end{pmatrix}=\begin{pmatrix}1 & 0\\0 & 1\end{pmatrix}\begin{pmatrix}a & b\\c & d\end{pmatrix}=EA
import numpy as np
# print("2 × 2 identity matrix")
print(np.eye(2))
# print("The 3x3 identity matrix")
print(np.eye(3))
# print("The 4x4 identity matrix")
print(np.eye(4))
#Implementation
2 × 2 identity matrix
[[1. 0.]
[0. 1.]]
The 3x3 identity matrix
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
The 4x4 identity matrix
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
using LinearAlgebra
Matrix{Int}(I, 2, 2)
Matrix{Int}(I, 3, 3)
Matrix{Int}(I, 4, 4)
2×2 Array{Int64,2}:
1 0
0 1
3×3 Array{Int64,2}:
1 0 0
0 1 0
0 0 1
4×4 Array{Int64,2}:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
The inverse matrix is
AA^{-1} = A^{-1}A = E
It has a relationship of. To give an example
A =\begin{pmatrix}1 & 1 \\1 & 2\end{pmatrix},
B =\begin{pmatrix}2 & -1 \\-1 & 1\end{pmatrix}
Find the product of AB when there are two matrices.
AB =\begin{pmatrix}1 & 1 \\1 & 2\end{pmatrix}\begin{pmatrix}2 & -1 \\-1 & 1\end{pmatrix}=\begin{pmatrix}1 & 0\\0 & 1\end{pmatrix}
Than this
AB = E = BA \Longrightarrow B = A^{-1}
A = \begin{pmatrix}a & b\\c & d\end{pmatrix}
\Longleftrightarrow
\begin{vmatrix}A\end{vmatrix}= det A = ad-bc
Furthermore, in the case of a 2x2 matrix,
A^{-1} = \frac{1}{ad-bc}\begin
{pmatrix}d & -b\\-c & a\end{pmatrix}
Can be expressed as, at this time,
ad-bc = 0 \Longleftrightarrow A^{-1}Does not exist
\\
ad-bc \neq 0 \Longleftrightarrow A^{-1}Exists.
Can be written as.
import numpy as np
a = [input().split() for i in range(2)]
#Enter numbers
#=>1 2
#=>3 4
a = np.array(a)
print(a)
#=>[['1' '2']
# ['3' '4']]
a = a.astype(np.float64)
print(np.linalg.det(a))
#=>-2.0000000000000004
if np.linalg.det(a) != 0:
print(np.linalg.inv(a))
else:
print("No exist")
#=>[[-2. 1. ]
# [ 1.5 -0.5]]
Actually, even if you calculate as it is with the np.linalg.inv () function
without using the if statement, an error will occur if the inverse matrix does not exist. However, I want to use the determinant this time, so I wrote it using the np.linalg.det () function
.
using LinearAlgebra
A = [1 2; 3 4]
if det(A) == 0
print("No exist")
else
inv(A)
end
#=>2×2 Array{Float64,2}:
# -2.0 1.0
# 1.5 -0.5
The inverse matrix is immediately obtained by the ʻinv () function`.
Next, I'm going to do linear transformation, so if you want to make the graph stronger. By all means.
Recommended Posts