Matrix Calculations and Linear Equations: Linear Algebra in Python <3>

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, Julia 1.4.0

queue

Let's touch on the procession a little deeper. fundamentally,

A x = b

The standard is to shape it

About the matrix

The matrix has *** rows *** and *** columns ***. ・ The rows are lined up side by side ・ Lined up on the shield until now, What we have expressed as u = (1, 2, 3) is a column vector. *** row vector: u = [1, 2, 3] *** *** Column vector: u = (1, 2, 3) ***

And. Originally, it should be written as follows.

u =
\begin{bmatrix}
1 \\
2 \\
3
\end{bmatrix}

However, this article is annoying due to the markdown method, so I will write it as before.

Linear equations and vectors

ex) About simultaneous equations

\begin{matrix}
 x - 2y =  1 \\
3x + 2y = 11
\end{matrix}

Is considered as a column vector. (= Linear thinking) The merit of linear equations is that negative command can be expressed by one equation. It can be transformed as follows,

x
\begin{bmatrix}
1 \\
3
\end{bmatrix}
+
y
\begin{bmatrix}
-2 \\
2
\end{bmatrix}
=
\begin{bmatrix}
1 \\
11
\end{bmatrix}

This time,

u =
\begin{bmatrix}
1 \\
3
\end{bmatrix}
,
v = 
\begin{bmatrix}
-2 \\
2
\end{bmatrix}
,
b = 
\begin{bmatrix}
1 \\
11
\end{bmatrix}

I can do it. This can be written in the form of Ax = b as follows when u and v are collectively referred to as A.

Ax =
\begin{bmatrix}
1 & -2\\
3 & 2
\end{bmatrix}
\begin{bmatrix}
x\\
y
\end{bmatrix}
=
\begin{bmatrix}
1 \\
11
\end{bmatrix}
= b

Will be. In this linear equation, we need to consider the applicable x and y. By the way, from an analytical point of view, it shows the intersections of straight lines.

Matrix calculation (program)

a =
\begin{bmatrix}
0 & 1 & 2 \\
1 & 2 & 3
\end{bmatrix}
,
b =
\begin{bmatrix}
2 & 1 \\
2 & 1\\
2 & 1
\end{bmatrix}

And. Calculate the matrix.

Python code

import numpy as np

a =  ([[0, 1 ,2],
       [1, 2, 3]])
b = ([[2, 1],
      [2, 1],
      [2, 1]])
print(np.dot(a, b))

#=>[[ 6  3]
#   [12  6]]

Julia code

using LinearAlgebra
a = [0 1 2; 1 2 3]
b = [2 1; 2 1; 2 1]
a*b

#=>2×2 Array{Int64,2}:
#   6  3
#  12  6

In the previous comment, there was something about the LinearAlgebra module, so this time I tried to make it easier. If you want to rationalize the mechanism of the contents, please do it without a module. It may be good to use a module for calculation adjustment.

Recommended Posts

Matrix Calculations and Linear Equations: Linear Algebra in Python <3>
Identity matrix and inverse matrix: Linear algebra in Python <4>
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Inner product and vector: Linear algebra in Python <2>
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)
Find and check inverse matrix in Python
Introduction to Vectors: Linear Algebra in Python <1>
Linear search in Python
"Linear regression" and "Probabilistic version of linear regression" in Python "Bayesian linear regression"
Find the Hermitian matrix and its eigenvalues in Python
Introduction to Linear Algebra in Python: A = LU Decomposition
Overview of generalized linear models and implementation in Python
Matrix multiplication in python numpy
Transposed matrix in Python standard
Stack and Queue in Python
Unittest and CI in Python
MIDI packages in Python midi and pretty_midi
Difference between list () and [] 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
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
[GUI in Python] PyQt5-Menu and Toolbar-
Solve ordinary differential equations in Python
Create and read messagepacks in Python
[Python3] Save the mean and covariance matrix in json with pandas
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.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
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
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Online Linear Regression in Python (Robust Estimate)
Implement FIR filters in Python and C
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
[Python] Region Covariance: Covariance matrix and computer vision
Read and write JSON files in Python
Easily graph data in shell and Python
Private methods and fields in python [encryption]
Find the general terms of the Tribonacci sequence with linear algebra and Python
Call sudo in Python and autofill password