[PYTHON] I read Strang's linear algebra

[World Standard MIT Textbook Strang: Linear Algebra Introduction](https://www.amazon.co.jp/s?k=%E3%82%B9%E3%83%88%E3%83%A9%E3%83] % B3% E3% 82% B0 +% E7% B7% 9A% E5% BD% A2% E4% BB% A3% E6% 95% B0 & __ mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & crid = 2C5QCY8N0F5B9 & sprefix =% E3% 82% B9% E3% 83% 88% E3% 83% A9% E3% 83% B3% E3% 82% B0% 2Caps% 2C236 & ref = nb_sb_ss_i_1_5) image.png

July 21st 19: 00-20: 30 Introduction to Linear Algebra 2nd Free Online Study Session Beginners Welcome to hold. Please join us.

Introduction

vector

The vector is derived from the Latin word vehere, which means "carry", and was first used by astronomers in the 18th century.

It is used in the field of mathematical science and is used in each field.

In the field of computers, data structures expressed as one-dimensional arrays and sequences are regarded. It is often used in the world of computer graphics.

It is also widely used in the field of physics. It is often used when dealing with problems in three-dimensional space or two-dimensional space, and is applied in a wide range of fields such as electromagnetism and fluid mechanics.

In mathematics, a quantity with size and orientation in geometric space. It can be regarded as a directed line segment. Vector in a broad sense in general. It is a quantity that has linearity, that is, it can take sum and scalar times. It is represented by a base and a set of components for it.

There are vertical vector and horizontal vector as notation.

Vertical vector\begin{bmatrix}1 \\ 20\end{bmatrix}
Horizontal vector\begin{bmatrix}1 &20\end{bmatrix}

It is easy to understand if you capture the vector on a geometric plane.

a= \begin{bmatrix}2 \\ 1\end{bmatrix}\\

b=\begin{bmatrix}1 \\ 3\end{bmatrix}

Then

a+b = \begin{bmatrix}2+1\\1+3\end{bmatrix} = \begin{bmatrix}3\\4\end{bmatrix}Is
# coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
a = np.array([2, 1])
b = np.array([1, 3])
c = a + b
origin = [0, 0, 0] #origin(0, 0)

d = np.array([a, b, c])
U = d.T[0] #Take the transpose and take out the x component
V = d.T[1] #Take the transpose and take out the y component
    
plt.quiver(origin, origin, U, V, angles='xy', scale=1, scale_units='xy', color=['r', 'g', 'b'])
plt.xlim([-1, 5])
plt.ylim([-1, 5])

plt.grid()
plt.show()

image.png However, it is easier to understand if the red arrow is translated by the green arrow rather than this way of thinking.

plt.quiver(origin, origin, U, V, angles='xy', scale=1, scale_units='xy', color=['r', 'g', 'b'])

b1=np.array([2,1])
origin1 = [1] #origin(0, 0)
origin2 = [3]

d = np.array([b1])
U = d.T[0] #Take the transpose and take out the x component
V = d.T[1] #Take the transpose and take out the y component
    
plt.quiver(origin1, origin2, U, V, angles='xy', scale=1, scale_units='xy',color=['r'])

plt.xlim([-1, 5])
plt.ylim([-1, 5])

plt.grid()
plt.show()

image.png

It is expressed as.

queue

A matrix is an array of numbers, symbols, expressions, etc. in a rectangular shape vertically and horizontally. Horizontally arranged numbers, symbols and expressions are called rows, and vertically arranged ones are called columns. For example

\begin{bmatrix}1&9&-13\\20&5&-6\end{bmatrix}

Is called a (2,3) type or 2x3 type matrix because it consists of two rows and three columns.

Use for solving simultaneous equations

The matrix has a long history of solving simultaneous equations, and the world's first example is in the Chinese book The Nine Chapters on Arithmetic (10th century BC-2nd century BC).

As an example of a simultaneous equation of two variables

\begin{cases}x+2y=5\\2x+3y=8\end{cases}

And. In this equation, the solution is (x, y) = (1, 2), which satisfies two linear equations at the same time.

Given a system of equations, if you compare the number of variables with the number of equations, the solution is roughly

If the number of variables is larger, the variables can be freely defined by the number of (number of variables) − (number of equations), and one solution cannot be determined.

If the number of variables and the number of equations match, there is a solution and one is determined.

If there are more equations, then there are too many constraints and there is no solution.

Do this using a matrix

\begin{bmatrix}1&2\\2&3&\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}5\\8\end{bmatrix}

, The solution can be found.

this is

\begin{cases}x+2y=5\\2x+3y=8\end{cases}

Just like solving, you can delete $ y $ in the formula on the second line. You can get $ x $ by dividing the formula in the first line by 2 and multiplying by 3 and subtracting from the formula in the second line.

\begin{bmatrix}1&2\\2-3/2&0&\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}5\\8/3-5\cdot3/2\end{bmatrix}
\begin{bmatrix}1&2\\1/2&0&\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}5\\1/2\end{bmatrix}

Therefore, $ x = 1 $, and substitute this into the formula on the first line to get $ y = 2 $.

Similarly

\begin{bmatrix}1&2\\2&3&\end{bmatrix}\begin{bmatrix}z\\w\end{bmatrix}=\begin{bmatrix}3\\8\end{bmatrix}

Solving, gives $ (x, y) = (2,1) $. If you write this all together

\begin{bmatrix}1&2\\2&3&\end{bmatrix}\begin{bmatrix}x&z\\y&w\end{bmatrix}=\begin{bmatrix}5&3\\8&8\end{bmatrix}

Can be written.

Determinant

\begin{bmatrix}a&b\\c&d&\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}e\\f\end{bmatrix}

To erase $ d $, subtract the second line multiplied by $ b $ from the first line multiplied by $ d $.

\begin{bmatrix}a&b\\ad-bd &0&\end{bmatrix} \begin{bmatrix}x\\y\end{bmatrix}=\begin{bmatrix}e\\de-bf\end{bmatrix}

Is obtained. Therefore,

x=\frac{de-bf}{ad-bd}

inner product

The inner product is also called a scalar product because it is a binary operation that determines a certain number (scalar) for two vectors. Dot product is a type of vector operation that returns one numerical value from two sequences of the same length.

For example, let's say you bought an apple for 100 yen and sold it for 200 yen. If the volume of the transaction is $ (q_1, q_2) = (-1,1) $ and the price is $ (p_1, p_2) = (100,200) $, the profit will be

(q_1,q_2)\cdot(p_1,p_2)=q_1p_1+q_2p_2=-1\cdot100+1\cdot 200=100

Will be.

q=np.array([-1,1])
p=np.array([100,200])
np.dot(p,q)

#100

projection

Straight line fitting

Next, consider the case where there are many expressions.

\begin{bmatrix}1&-2\\1&0&\\1&2\end{bmatrix}\begin{bmatrix}\alpha\\ \beta\end{bmatrix}=\begin{bmatrix}1\\2\\4\end{bmatrix}

This is a type often seen in both the financial and engineering fields. It is a straight fitting.

The right side is the explained variable, and the left side is the explanatory variable and the regression coefficient. Often $ Ax = b $ has no solution. A common reason is that there are too many expressions. The reason why this cannot be solved is that the explained variable contains noise. If the error is $ e = b-Ax $, the error may not be zero. If $ e = 0 $, the solution can be found. When $ e $ is minimized rather than zero, $ \ hat {x} $ is called the least squares solution. This solution is

A^TA\hat{x}=A^Tb

It is obtained by solving. $ ^ T $ represents the transposed matrix. $ Ax $ is on a plane consisting of columns (1,1,1) and (0,1,2). Find the point closest to $ b $ on that plane. The closest point is the projection $ p $. This is the least squares method using projection.

When approximating a measured value using a specific function such as a linear function, a quadratic function, a trigonometric function, or a hyperbola assumed from an appropriate model, the assumed function should be a good approximation to the measured value. , The model can be determined to minimize the sum of squares of the residuals.

Eigenvalues, eigenvectors

In linear algebra, there are eigenvalues and eigenvectors as indexes that represent the characteristics of linear transformation. It is important for the analysis of dynamic problems that occur when we want to discuss a stable state (steady state).

Ax=\lambda x

And $ A $ is a matrix, $ x $ is the eigenvector and $ \ lambda $ is the eigenvalue. Looking at this basic equation, we can see that there are eigenvectors on the right and left sides. In addition, there is an eigenvalue $ \ lambda $ on the right side. The eigenvalue is not always one.

\begin{bmatrix}4\\2\end{bmatrix}=2\cdot\begin{bmatrix}2\\1\end{bmatrix}\\
\begin{bmatrix}2\\1\end{bmatrix}=1\cdot\begin{bmatrix}2\\1\end{bmatrix} \\
\begin{bmatrix}-2\\-1\end{bmatrix}=-1\cdot \begin{bmatrix}2\\1\end{bmatrix} 

Then the eigenvector is

\begin{bmatrix}2\\1\end{bmatrix} 

And the eigenvalues are 2,1, -1 from the top. Expressed on a plane

# coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
a = np.array([4, 2])
b = np.array([2, 1])
c = np.array([-2, -1])
origin = [0, 0, 0] #origin(0, 0)

d = np.array([a, b, c])
U = d.T[0] #Take the transpose and take out the x component
V = d.T[1] #Take the transpose and take out the y component
    
plt.quiver(origin, origin, U, V, angles='xy', scale=1, scale_units='xy', color=['r', 'g', 'b'])
plt.xlim([-3, 5])
plt.ylim([-3, 5])

plt.grid()

image.png

Will be. For example

\begin{bmatrix}0.8&0.3\\0.2&0.7\end{bmatrix}x=\lambda x

And. Move the right side to the left side

\begin{bmatrix}0.8-\lambda&0.3\\0.2&0.7-\lambda\end{bmatrix}x=0

$ x \ ne 0 $. Therefore

\begin{cases}(0.8-\lambda)x_1+0.3x_2=0\\0.2x_1+(0.7-\lambda)x_2=0\end{cases}

To solve. Divide 1 expression by 0.3, divide 2 expressions by $ 0.7- \ lambda $ and subtract from it

\begin{bmatrix}0.8-\lambda&0.3\\0.2/(0.7-\lambda)-(0.8-\lambda)/0.3 &0 \end{bmatrix}x=0

Because it is $ x \ ne 0 $

0.2/(0.7-\lambda)-(0.8-\lambda)/0.3 =0

Therefore, $ \ lambda = 1,0.5 $.

Then $ x_1 = -x_2 $ or $ 1.5 \ cdot x_1 = 1 \ cdot x_2 $.

this is When $ \ lambda = 1 $

x=\begin{bmatrix}0.6\\0.4\end{bmatrix}

When $ \ lambda = 0.5 $

x=\begin{bmatrix}1\\-1\end{bmatrix}

Will be.

This is done by multiplying the matrix $ A $ by a positive vector $ u_0 $ many times, such as $ u_1 = Au_0 $, $ u_2 = Au_1 = A ^ 2u_0 . $Au_{\infty}=u_{\infty}$$ become. Let's try $ u_0 = [0.9,0.1] ^ T $.

\begin{bmatrix}0.8&0.3\\0.2&0.7\end{bmatrix}\begin{bmatrix}0.9\\0.1\end{bmatrix}=
\begin{bmatrix}0.72+0.03\\0.18+0.07\end{bmatrix}=
\begin{bmatrix}0.75\\0.25\end{bmatrix}
\begin{bmatrix}0.8&0.3\\0.2&0.7\end{bmatrix}\begin{bmatrix}0.75\\0.25\end{bmatrix}=
\begin{bmatrix}0.6+0.075\\0.15+0.175\end{bmatrix}=
\begin{bmatrix}0.675\\0.325\end{bmatrix}
\begin{bmatrix}0.8&0.3\\0.2&0.7\end{bmatrix}\begin{bmatrix}0.675\\0.325\end{bmatrix}=
\begin{bmatrix}0.54+0.0975\\0.135+0.2275\end{bmatrix}=
\begin{bmatrix}0.6375\\0.3625\end{bmatrix}

Little by little

x=\begin{bmatrix}0.6\\0.4\end{bmatrix}

Is approaching.

In the financial example, it is easy to imagine that $ u_1 = Au_0 + \ epsilon_1 $ is used as the price.

Recommended Posts

I read Strang's linear algebra
I read "Linux standard textbook"!
I read the SHAP paper
I read PEP 613 (Explicit Type Aliases)
I read PEP 612 (Parameter Specification Variables)