0
0

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.

n×n行列で、n乗して初めてゼロ行列になる行列が存在するのをnumpyで確認する

Last updated at Posted at 2021-04-22

はじめに

n×n で、n乗してゼロになる行列が存在するのをnumpyで確認する。
具体的には、対角成分の1つ上だけが1で、他が0の行列を、実際にn乗してみて、ゼロ行列になるのを示すプログラム。

プログラム

変数nに作りたい正方行列の大きさを指定する。ここでは、n=5とした。

power.py
import numpy as np

n = 5

O = np.zeros((n,n), dtype=int)#まずゼロ行列を作る

O = np.matrix(O)

for i in range(n-1):#対角成分の1つ上を1に
    O[i,i + 1] = 1

for i in range(n):#1乗からn乗までを出力
    print(O ** (i+1))

結果

power.py
[[0 1 0 0 0]
 [0 0 1 0 0]
 [0 0 0 1 0]
 [0 0 0 0 1]
 [0 0 0 0 0]]
[[0 0 1 0 0]
 [0 0 0 1 0]
 [0 0 0 0 1]
 [0 0 0 0 0]
 [0 0 0 0 0]]
[[0 0 0 1 0]
 [0 0 0 0 1]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
[[0 0 0 0 1]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]

0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?