Post

Numpy & Scipy - 1.2 Convenient Functions of Matrix

Numpy & Scipy - 1.2 Convenient Functions of Matrix
Visitors


eye / identity

  • np.eye: Function to fill 1s into a band.

Desktop View

1
2
a = np.eye(2,3, k=1, dtype=np.float64)
# 2, 3 are rows and columns respectively, k is the band id, dtype is data type
1
2
[[0. 1. 0.]
[0. 0. 1.]]


  • np.identity: Function to create an identity matrix.
1
2
3
4
5
6
a = np.identity(3, dtype=np.float64)
print(a)
# Creates a 3x3 identity matrix.
# To express 3x3 identity matrix with eye:
a = np.eye(3)
a = np.eye(3,3)
1
2
3
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]



tri

  • np.tri: Function to create a lower triangular matrix. Non-zero entry = 1.

Desktop View

  • Fills 1s in all bands where $k \ge$ band id, and 0s elsewhere.
1
2
3
4
5
6
a = np.tri(4, 3, k=1, dtype=np.float64)
# 4,3 are row, column, k is band id, and data type

prt(tri1, fmt="%0.2f", delimiter=",")
# from print_lecture import print_custom as prt
# prt is a custom package for printing.
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: Fills a (row x column) matrix entirely with 0s.
1
2
3
a = np.zeros((2,3)) # Entered as a (row, col) tuple.

prt(a, fmt="%0.2f")
1
2
0.00,  0.00,  0.00
0.00,  0.00,  0.00


  • np.ones: Fills a (row x column) matrix entirely with 1s.
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: Fills a (row x column) matrix entirely with a specified 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: Fills a (row x column) matrix with random values.
  • Only real matrices and np.float64 data types are allowed.
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


  • Of course, you can express complex matrices using random.rand.

Example 1
Create a 3x3 matrix where the real part is 0 and the imaginary part is 0i ~ 1i using np.random.rand.

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)
This post is licensed under CC BY 4.0 by the author.