Numpy & Scipy - 1.2 行列の便利な関数
Numpy & Scipy - 1.2 行列の便利な関数
Numpy & Scipy シリーズ (2 / 9)
- Numpy & Scipy - 1.1 行列とベクトルの表記法、行列の入出力
- Numpy & Scipy - 1.2 行列の便利な関数
- Numpy & Scipy - 1.3 行列の基本操作 (1)
- Numpy & Scipy - 1.4 行列の基本操作 (2)
- Numpy & Scipy - 1.5 行列の基本操作 (3)
- Numpy & Scipy - 1.6 The Solution of Matrix Equation (General Matrices)
- Numpy & Scipy - 1.7 The Solution of Band Matrix
- Numpy & Scipy - 1.8 The Solution of Toeplitz Matrix and Circulant Matrix And How to Solve AX=B
- Numpy & Scipy - 1.9 Calculate Eigenvector and Eigenvalue of Matrix
eye / identity
np.eye:bandに1を埋める関数です。
1 2 a = np.eye(2,3, k=1, dtype=np.float64) # 2, 3 はそれぞれ row, column で k は band id を意味するパラメータ, dtype はデータタイプ
1 2[[0. 1. 0.] [0. 0. 1.]]
np.identity:identity matrix を作る関数です。
1 2 3 4 5 6 a = np.identity(3, dtype=np.float64) print(a) # 3x3 identity matrix を作ります。 # eye で 3x3 identity matrix を表現するなら.. a = np.eye(3) a = np.eye(3,3)
1 2 3[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
tri
np.tri:lower triangular matrix を作る関数です。nonzero entry = 1
- $k \ge$ band id のすべてのバンドに1を埋め、残りは0を埋めます。
1 2 3 4 5 6 a = np.tri(4, 3, k=1, dtype=np.float64) # 4,3 は row,column で、k は band id, data type prt(tri1, fmt="%0.2f", delimiter=",") # from print_lecture import print_custom as prt # prt は出力用カスタムパッケージです。
1 2 3 40.00, 0.00, 0.00 1.00, 0.00, 0.00 1.00, 1.00, 0.00 1.00, 1.00, 1.00
zeros / ones / full
np.zeros:(row x column) matrix をすべて0で埋める関数
1 2 3 a = np.zeros((2,3)) # (row, col) tuple 形式で入ります。 prt(a, fmt="%0.2f")
1 20.00, 0.00, 0.00 0.00, 0.00, 0.00
np.ones:(row x column) matrix をすべて1で埋める関数
1 2 3 a = np.ones((2,3)) prt(a, fmt="%0.2f")
1 21.00, 1.00, 1.00 1.00, 1.00, 1.00
np.full:(row x column) matrix をすべて指定された value で埋める関数
1 2 3 a = np.full((3,3), 1 + 0j) prt(a, fmt="%0.2f")
1 2 3( 1.00+0.00j), ( 1.00+0.00j), ( 1.00+0.00j) ( 1.00+0.00j), ( 1.00+0.00j), ( 1.00+0.00j) ( 1.00+0.00j), ( 1.00+0.00j), ( 1.00+0.00j)
random.rand
np.random.rand:(row x column) matrix に random 値を埋め込む関数- ただし、real matrix であり
np.float64データタイプのみ許可されます。
1 2 3 rand_a = np.random.rand(8,5) prt(rand_a, fmt="%0.4f")
1 2 3 4 5 6 7 80.7852, 0.7524, 0.8531, 0.0095, 0.9326 0.8960, 0.6175, 0.6236, 0.5146, 0.4580 0.4606, 0.6550, 0.0049, 0.1367, 0.3998 0.3829, 0.7723, 0.0422, 0.0209, 0.1061 0.1148, 0.0447, 0.1126, 0.7619, 0.9044 0.4618, 0.1164, 0.1075, 0.7190, 0.7792 0.1036, 0.8590, 0.8889, 0.7100, 0.9618 0.6096, 0.9913, 0.7568, 0.6786, 0.5567
- もちろん、
random.randを使って complex matrix で表現が可能です。
Example 1
np.random.randを使用して実数部分は0で虚数部分が0i 〜 1i である 3x3 matrix を作成する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import numpy as np from print_lecture import print_custom as prt identity = np.identity(20, dtype=np.float64) np.savetxt("Save/practice3.txt", identity, fmt="%0.1f", delimiter=" , ") rand_a = np.random.rand(3, 3) rand_a = rand_a.astype(dtype=np.complex128) rand_a = rand_a * 1j prt(rand_a, fmt="%0.2f", delimiter=" , ")
1 2 3( 0.00+0.33j) , ( 0.00+0.41j) , ( 0.00+0.52j) ( 0.00+0.11j) , ( 0.00+0.16j) , ( 0.00+0.45j) ( 0.00+0.92j) , ( 0.00+0.93j) , ( 0.00+0.81j)
この記事は著者の CC BY 4.0 ライセンスの下で提供されています。


