Identity matrix and inverse matrix: Linear algebra in Python <4>

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

Identity matrix

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

Python code for identity matrix

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

Julia code of identity matrix

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

Inverse matrix

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}

Determinant

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.

Inverse Matrix Python code including determinant

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.

Julia code for the inverse matrix including the determinant

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

Finally

Next, I'm going to do linear transformation, so if you want to make the graph stronger. By all means.

Recommended Posts

Identity matrix and inverse matrix: Linear algebra in Python <4>
Matrix Calculations and Linear Equations: Linear Algebra in Python <3>
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Find and check inverse matrix in Python
Linear Independence and Basis: Linear Algebra in Python <6>
Capture linear algebra images in python (transpose, inverse matrix, matrix product)
Inner product and vector: Linear algebra in Python <2>
First Computational Physics: Quantum mechanics and linear algebra in python.
Basic Linear Algebra Learned in Python (Part 1)
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
Transposed matrix in Python standard
Unittest and CI in Python
Online linear regression in Python
Identity and equivalence Python is and ==
Difference between list () and [] in Python
Difference between == and is in python
Draw a scatterplot matrix in python
Manipulate files and folders in Python
Assignments and changes in Python objects
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
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-
Differences in identity, equivalence, and aliases
Create and read messagepacks in Python
Solving simultaneous linear equations in Python (sweeping method and fractional representation)
[Python3] Save the mean and covariance matrix in json with pandas
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.
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
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 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