-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_equation.py
58 lines (51 loc) · 1.76 KB
/
solution_equation.py
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
import numpy as np
# Diagonal matrix
def diagonal_solution(matrixA, matrixB):
heightB, widthB = matrixB.shape
matrixX = np.zeros((heightB, widthB))
lengthA = len(matrixA[0])
for i in range(lengthA):
matrixX[i] = matrixB[i]/matrixA[i][i]
return matrixX
matA = np.array([[2,0,0],[0,3,0],[0,0,4]])
matB = np.array([[1],[2],[1]])
solutionX = diagonal_solution(matA,matB)
print(solutionX)
def lower_traingle(matrixA, matrixB):
heightB, widthB = matrixB.shape
matrixX = np.zeros((heightB, widthB))
matrixX[0] = matrixB[0]/matrixA[0][0]
for i in range(1, len(matrixA[0])):
sum = 0
for j in range(i):
sum = sum + matrixA[i][j]*matrixX[j]
matrixX[i] = (matrixB[i] - sum)/matrixA[i][i]
return matrixX
matlA = np.array([[1,0,0],[1,2,0],[3,2,5]])
matlB = np.array([[3],[2],[4]])
solution1X = lower_traingle(matlA, matlB)
print(solution1X)
def upper_traingle(matrixA, matrixB):
heightB, widthB = matrixB.shape
matrixX = np.zeros((heightB, widthB))
length = len(matrixA[0])
matrixX[length-1] = matrixB[length-1]/matrixA[length-1][length-1]
for i in reversed(range(length)):
sum = 0
for j in range(i+1, length):
sum += matrixA[i][j]*matrixX[j]
matrixX[i] = (matrixB[i] - sum)/matrixA[i][i]
return matrixX
matlA = np.array([[3,2,5],[0,2,1],[0,0,2]])
matlB = np.array([[3],[2],[4]])
solution_uX = upper_traingle(matlA, matlB)
print(solution_uX)
def unitary_matrix(matrixA, matrixB):
heightB, widthB = matrixB.shape
trans_A = matrixA.transpose()
matrixX = trans_A.dot(matrixB)
return matrixX
mat_uniA = np.array([[5,-2,2,7],[1,0,0,3],[-3,1,5,0],[3,-1,-9,4]])
mat_uniB = np.array([[1],[2],[3],[4]])
matX = unitary_matrix(mat_uniA, mat_uniB)
print(matX)