forked from ThomIves/BasicLinearAlgebraToolsPurePy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumpyToolsPractice.py
More file actions
69 lines (55 loc) · 1.53 KB
/
NumpyToolsPractice.py
File metadata and controls
69 lines (55 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
# Zeros Matrix
print(np.zeros((3, 3)), '\n')
# Identity Matrix
print(np.identity(3), '\n')
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
C = np.array([[2, 4, 6], [8, 10, 12], [14, 16, 18]])
# Matrix Copy
AC = A.copy()
print(AC, '\n')
# Transpose a matrix
AT = np.transpose(A)
print(AT, '\n')
# Add and Subtract
SumAC = A + C
print(SumAC, '\n')
DifCA = C - A
print(DifCA, '\n')
# Matrix Multiply
ProdAC = np.matmul(A, C)
print(ProdAC, '\n')
# Multiply a List of Matrices
arr = [A, C, A, C, A, C]
Prod = np.matmul(A, C)
num = len(arr)
for i in range(2, num):
Prod = np.matmul(Prod, arr[i])
print(Prod, '\n')
ChkP = np.matmul(
np.matmul(
np.matmul(
np.matmul(
np.matmul(arr[0], arr[1]),
arr[2]), arr[3]), arr[4]), arr[5])
print(ChkP, '\n')
# Check Equality of Matrices
print(Prod == ChkP, '\n')
# https://docs.scipy.org/doc/numpy/reference/generated/numpy.allclose.html
print(np.allclose(Prod, ChkP), '\n')
# Dot Product (follows the same rules as matrix multiplication)
# https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html
v1 = np.array([[2, 4, 6]])
v2 = np.array([[1], [2], [3]])
ans1 = np.dot(v1, v2)
ans2 = np.dot(v1, v2)[0, 0]
print(f'ans1 = {ans1}, ans2 = {ans2}\n')
# Unitize an array
mag1 = (1*1 + 2*2 + 3*3) ** 0.5
mag2 = np.linalg.norm(v2)
norm1 = v2 / mag1
norm2 = v2 / mag2
print(f'mag1 = {mag1}, mag2 = {mag2}, they are equal: {mag1 == mag2}\n')
print(norm1, '\n')
print(norm2, '\n')
print(norm1 == norm2)