Find and check inverse matrix in Python

What I want to do: Implement in Python how to find the inverse matrix

When the size of the matrix becomes large, we have to think about optimization of processing, but In this article, we aim to combine the "conceptual part of finding the inverse matrix" with the Python implementation. (Use Numpy)

(Sample) Matrix definition

A=\left(
\begin{matrix}
1 & 2 \\
3 & 4 
\end{matrix}
\right)

will do. In the implementation it looks like this:

> import numpy as np
> A = np.array([[1,2],[3,4]])

For the time being, the contents of ck

> A 
array([[1, 2],
       [3, 4]])

Let's calculate the determinant

If the determinant = 0, check because there is no inverse matrix or hesitation:

{\rm det}(A)={\rm det}\left(
\begin{matrix}
1 & 2 \\
3 & 4 
\end{matrix}
\right)
=1\times 4-2\times3=-2

In terms of implementation

> np.linalg.det(A)
-2.0000000000000004

Let's find the inverse matrix

Mathematically

A = \left(
\begin{matrix}
a & b \\
c & d
\end{matrix}\right)

Against

A^{-1} = \frac{1}{{\rm det}A}\left(
\begin{matrix}
d & -b \\
-c & a
\end{matrix}\right)

Substitute as known.

A^{-1} = \frac{1}{-2}\left(
\begin{matrix}
4 & -2 \\
-3 & 1
\end{matrix}\right)
=\left(
\begin{matrix}
-2 & 1 \\
1.5 & -0.5
\end{matrix}
\right)

In terms of implementation

> inv_A = np.linalg.inv(A)
> inv_A
array([[-2. ,  1. ],
       [ 1.5, -0.5]])

Where linalg is a module of numpy. For more details http://docs.scipy.org/doc/numpy/reference/routines.linalg.html See

Finally check

The confirmation of the story, that is, that multiplying the inverse matrix and the original matrix gives an identity matrix, isn't it?

AA^{-1}=A^{-1}A=
\left(
\begin{matrix}
1 & 0 \\
0 & 1
\end{matrix}
\right)
> np.dot(A,inv_A)
array([[  1.00000000e+00,   1.11022302e-16],
       [  0.00000000e+00,   1.00000000e+00]])
> np.dot(inv_A,A)
array([[  1.00000000e+00,   4.44089210e-16],
       [  0.00000000e+00,   1.00000000e+00]])

It's a numerical thing that doesn't become a unit matrix beautifully, I'm not sure, but ...

If you want to experiment, try experimenting with different matrices $ A $.

Recommended Posts

Find and check inverse matrix in Python
Identity matrix and inverse matrix: Linear algebra in Python <4>
Find the Hermitian matrix and its eigenvalues in Python
Check and move directories in Python
Check and receive Serial port in Python (Port check)
List find in Python
[Python] Find the transposed matrix in a comprehension
Matrix Calculations and Linear Equations: Linear Algebra in Python <3>
Find the difference in Python
Find permutations / combinations in Python
Matrix multiplication in python numpy
Transposed matrix in Python standard
Write tests in Python to profile and check coverage
Stack and Queue in Python
Unittest and CI in Python
Let's find pi in Python
Capture linear algebra images in python (transpose, inverse matrix, matrix product)
Find the eigenvalues of a real symmetric matrix in Python
Check for memory leaks in Python
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Check for external commands in python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Draw a scatterplot matrix 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
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
[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
Check the behavior of destructor in Python
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Find files like find on linux in Python
Implementation module "deque" in queue and Python
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Check if the URL exists in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
How to check opencv version 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]