Inner product and vector: Linear algebra in Python <2>

linear algebra

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.

environment

・ Jupyter Notebook ・ Language: Python3, Julia1.4

Formula notation

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.

Length and dot product

v = (v₁, v₂) w = (w₁, w₂) Let's say the vector.

inner product

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 ***.

length

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).

Unit vector

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.

Finally

The norm should be done originally, but it is omitted because it is only an outline.

Recommended Posts

Inner product and vector: Linear algebra in Python <2>
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Linear Independence and Basis: Linear Algebra in Python <6>
Identity matrix and inverse matrix: Linear algebra in Python <4>
Matrix Calculations and Linear Equations: Linear Algebra in Python <3>
First Computational Physics: Quantum mechanics and linear algebra in python.
Capture linear algebra images in python (transpose, inverse matrix, matrix product)
Basic Linear Algebra Learned in Python (Part 1)
Introduction to Vectors: Linear Algebra in Python <1>
Linear search in Python
Overview of generalized linear models and implementation in Python
Stack and Queue in Python
Unittest and CI in Python
Online linear regression in Python
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Infinite product in Python (using functools)
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Solving simultaneous linear equations in Python (sweeping method and fractional representation)
List of Linear Programming (LP) solvers and modelers available in Python
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Linear regression in Python (statmodels, scikit-learn, PyMC3)
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Line graphs and scale lines in python
Online Linear Regression in Python (Robust Estimate)
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Dealing with "years and months" in Python
Read and write JSON files in Python
Easily graph data in shell and Python
Private methods and fields in python [encryption]
Find and check inverse matrix in Python
Find the general terms of the Tribonacci sequence with linear algebra and Python
Call sudo in Python and autofill password
Module import and exception handling in python
How to use is and == in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Pauli matrices and biquaternions in Clifford algebra