Hits



eye / identity

  • np.eye : band에 1을 채워 넣는 함수이다.

Desktop View

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

Desktop View

  • 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
4
0.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
2
0.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
2
1.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
8
0.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)