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, Julia1.4
Since it is described in Markdown notation, there are some parts that represent the product such as *. Also, regarding mathematical formulas, the Backford
part is for the purpose of emphasizing the legibility of mathematical formulas, and is not a part of the code.
v = (v₁, v₂) w = (w₁, w₂) Let's say the vector.
The inner product is expressed as *** v · w ***. It is also called *** dot product ***.
v ・ w = v₁w₁ + v₂ * w₂
Will be.
At this time, if *** v · w = 0, they are orthogonal ***.
The length is when the vectors of the inner products are the same => squared. *** (Inner product v ・ v) = (Length squared) ***
When v = (1, 2, 3)
│v│² = 1² + 2² + 3² = 14
-> This represents *** the square of the length ***
That is, if this square is removed, it becomes the length, so
│v│ = √14
Will be. As another way of writing
norm(v) = √14
Write. From now on, write with norm ().
In this regard, the [Three Squares Theorem](https://ja.wikipedia.org/wiki/%E3%83%94%E3%82%BF%E3%82%B4%E3%83%A9 It is clear when compared with% E3% 82% B9% E3% 81% AE% E5% AE% 9A% E7% 90% 86).
It is a vector whose length is 1.
In the case of the above vector v, the length is √14, so to set this to 1, divide by √14. That is, if the vector is divided by the original length itself, it becomes a unit vector.
When the unit vector is u,
u = v/norm(v)
From this, u represents a vector of length 1 in the same direction as v.
Expressed in concrete numbers, when v = (1, 2, 3), it is norm (v) = √14
, so if you divide by that,
u = (1/√14, 2/√14, 3/√14)
It turns out that
Now, let's write a program that calculates the inner product and a program that calculates the length.
Python
import numpy as np
import math
v = list()
w = list()
#Fill in the elements of a vector with three elements
for i in range(3):
vvec = int(input())
v.append(vvec)
for i in range(3):
wvec = int(input())
w.append(wvec)
#Conversion of the contents of the array
v = np.array(v)
w = np.array(w)
#Calculation of inner product
print(np.dot(v, w))
#Calculation of length
normv = math.sqrt(np.dot(v, v))
print(normv)
#Since √ is used when actually calculating
print("√",np.dot(v, v))
Julia is more difficult than python, so make it simple code.
v = [1 2 3]
w = [3; 2; 1]
#Inner product calculation
v*w
#Length squared
normv2 = sum(v.^ 2)
#length
sqrt(normv2)
Regarding julia's code, I'm forced to bring it with me in the inner product calculation, so I'd like to raise a slightly more efficient code later.
The norm should be done originally, but it is omitted because it is only an outline.
Recommended Posts