0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonを使って余因子行列を用いて逆行列を求める。

Last updated at Posted at 2019-04-06

余因子行列を用いて逆行列を求めたい。

今回は余因子行列を用いて逆行列を求めてみたいと思います。


まずは正則行列Aをひとつ定める。

例えば今回はAとして以下の様な行列をとることにします。

import numpy as np
A = np.array([[2.,1.,1.],[0.,-2.,1.],[0.,-1.,-1.]])

行列式を定義。

np.linalgを使えばnp.linalg.det(A)でおしまいですが、ここでは
あえてdet(A)という関数を以下のようにきちんと書いておくことにします。

def det(A):
    return  A[0][0]*A[1][1]* A[2][2] + A[0][2]*A[1][0]*A[2][1] + A[0][1]*A[1][2]*A[2][0]\
           - A[0][2]*A[1][1]*A[2][0] - A[0][1]*A[1][0]*A[2][2] - A[0][0]*A[1][2]*A[2][1]

余因子行列を与える関数(写像)を定義。


def Cof(A):
    C = np.array([[0,0,0] for q in range(3)])
    C[0] = C[0]+[A[1][1]*A[2][2]-A[1][2]*A[2][1], -A[0][1]*A[2][2]+A[0][2]*A[2][1], \
           A[0][1]*A[1][2]-A[0][2]*A[1][1]]
    C[1] = C[1]+[-A[1][0]*A[2][2]+A[1][2]*A[2][0], A[0][0]*A[2][2]-A[0][2]*A[2][0], \
          -A[0][0]*A[1][2]+A[0][2]*A[1][0]]
    C[2] = C[2]+[A[1][0]*A[2][1]-A[1][1]*A[2][0],  -A[0][0]*A[2][1]+A[0][1]*A[2][0], \
           A[0][0]*A[1][1]-A[0][1]*A[1][0]]
    return C

逆行列を与える関数(写像)を定義。

ここまで来ればあとは以下のように定めて、

def Inv(A):
    R = Cof(A)/det(A)
    return R

この関数にAを代入すれば完了です。

# 逆行列を余因子行列により求める。
print(Inv(A))
0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?