Friday, February 15, 2019

[tutorial][python] Inverse matrix


Inverse matrix is available in n×n matrix only. But n×n matrix may not means have inverse matrix.

If matrix A and matrix B are inverse matrix each others, it can be represented as

Suppose there are 2 matrix:
 
Let have a look how to get the result, work for a dot product on AB first:
A.      (2,3) • (-7,5) = (2*-7)+(3*5)  = -14+15 = 1
B.      (2,3) • (3,-2) = (2*3)+(3*-2) = 6-6       = 0
C.      (5,7) • (-7,5) = (5*7)+(7*5)  = -35+35 = 0
D.      (5,7) • (3,-2) = (5*3)+(7*-2) = 15-14   = 1

BA use same method to get.


Both of the them are resulted 1 , B is in inverse matrix of A and A is in inverse matrix of B. We can represent their relation in this math format :

 * Pay attention to the -1 sign.

Example 1 

The coming example use as source stored in A, and than use linalg.inv() inverse matrix of source and print it out:
import numpy as np
from scipy import linalg

A = np.array([[2,3],[5,7]])
print(A)
print(linalg.inv(A))
Result :
[[2 3]
 [5 7]]
[[-7.  3.]
 [ 5. -2.]]


Example 2

This example is to calculate dot product

(2,3) • (5,7) = 2*5 + 3*7 = 10+21 =31

import numpy as np
from scipy import linalg

A = np.array([2,3])
B = np.array([5,7])

print(A.dot(B))
Result :
31

Example 3

Example in python :

import numpy as np
from scipy import linalg

A = np.array([[1,3,4],[2,5,1],[2,3,8]])
print(A)
print(linalg.inv(A))
print(A.dot(linalg.inv(A)))
Result:
[[1 3 4]
 [2 5 1]
 [2 3 8]]
[[ -1.76190476e+00   5.71428571e-01   8.09523810e-01]
 [  6.66666667e-01   5.55111512e-17  -3.33333333e-01]
 [  1.90476190e-01  -1.42857143e-01   4.76190476e-02]]
[[  1.00000000e+00   2.22044605e-16   0.00000000e+00]
 [ -2.22044605e-16   1.00000000e+00  -2.77555756e-16]
 [  0.00000000e+00   2.22044605e-16   1.00000000e+00]]

No comments :

Post a Comment