-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MatrixUtil.py
101 lines (89 loc) · 2.32 KB
/
MatrixUtil.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from math import *
def addOneLocalK(globalK, localK, localIEG):
localLen = len(localK)
localPiv = floor(localLen/2)
u = localIEG[0]*3
v = -3 + localIEG[1]*3
for i in range(localLen):
for j in range(localLen):
if i<localPiv and j<localPiv:
globalK[i+u][j+u] += localK[i][j]
elif i<localPiv and j>=localPiv:
globalK[i+u][j+v] += localK[i][j]
elif i>=localPiv and j<localPiv:
globalK[i+v][j+u] += localK[i][j]
else:
globalK[i+v][j+v] += localK[i][j]
return globalK
def calculateSystemMatrix(IEG, beamList, numVert):
tempK = newNullMatrix(numVert*3)
for i in range(len(IEG)):
addOneLocalK(tempK, beamList[i].getLocalK(), IEG[i])
return tempK
def newNullMatrix(lenK):
newMatrix = []
for i in range(lenK):
row = []
for i in range(lenK):
row.append(0.0)
newMatrix.append(row)
return newMatrix
def setDegreesOfFreedom(matrix, degsVector):
if degsVector == []:
return matrix
for i in range(len(matrix)):
if degsVector[i] == 0:
for j in range(len(matrix)):
if i == j:
matrix[i][j] = 1
else:
matrix[i][j] = 0
matrix[j][i] = 0
return matrix
def gaussSeidel(A, b, numSteps):
if testForDiagDom(A) == False:
print "Linear system is not diagonally dominant. Convergence is not gauranteed."
x0 = getTestX(A, b)
for n in range(numSteps):
x1 = gaussSeidelStep(A, b, x0)
x0 = x1
return x0
def testForDiagDom(matrix):
diagonalElement = matrix[0][0]
for i in range(len(matrix[0])):
rowSum = 0
for j in range(len(matrix[0])):
if i == j:
diagonalElement = matrix[i][j]
else:
rowSum += matrix[i][j]
if abs(rowSum) > abs(diagonalElement):
return False
return True
def getTestX(A, b):
x = []
for i in range(len(b)):
x.append(1)
return x
def gaussSeidelStep(A, b, x0):
x1 = []
for i in range(len(b)):
tempX1 = elementGauss(A, b, x0, x1, i)
x1.append(tempX1)
return x1
def elementGauss(A, b, x0, x1, i):
LUCorrFactor = pow(A[i][i], -1)
sumAX0 = sum(vectorMultiply(A[i], x0, i, len(x0)))
sumAX1 = sum(vectorMultiply(A[i], x1, 0, i))
return LUCorrFactor*(b[i]-sumAX0-sumAX1)
def vectorMultiply(V1, V2, n, m):
tempVector = []
for i in range(n, m):
tempVector.append(V1[i]*V2[i])
return tempVector
def appendLists(listOfLists):
newList = []
for liste in listOfLists:
for element in liste:
newList.append(element)
return newList