Skip to content

Commit c152208

Browse files
Wesley-Arringtonberquist
authored andcommitted
Tiny Tiny Edit to Thomas Algorithm in Python (#377)
* Adding A Single Character * Adding other spaces * Adding Spaces Between '#' and the Comment * Adding Blank Comment Line * Removing Line Ending Whitespace * Minor Code Cleanup * Changing Comment to Blank Line
1 parent 6b79ffe commit c152208

File tree

1 file changed

+15
-13
lines changed
  • contents/thomas_algorithm/code/python

1 file changed

+15
-13
lines changed

contents/thomas_algorithm/code/python/thomas.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22

33
# note this example is inplace and destructive
44
def thomas(a, b, c, d):
5-
6-
#set the initial elements
5+
6+
# set the initial elements
77
c[0] = c[0] / b[0]
88
d[0] = d[0] / b[0]
99

10-
n = len(d) #number of equations to solve
11-
for i in range(1,n):
12-
#scale factor for c and d
13-
scale = 1 / (b[i] - c[i-1]*a[i])
14-
15-
c[i] = c[i] * scale
16-
d[i] = (d[i] -a[i] * d[i-1]) * scale
10+
n = len(d) # number of equations to solve
11+
for i in range(1, n):
12+
# scale factor for c and d
13+
scale = 1 / (b[i] - c[i-1] * a[i])
14+
15+
c[i] *= scale
16+
d[i] = (d[i] - a[i] * d[i-1]) * scale
17+
1718

18-
1919
# do the back substitution
20-
for i in range(n-2,-1,-1):
21-
d[i] = d[i] - c[i]*d[i+1]
22-
20+
for i in range(n-2, -1, -1):
21+
d[i] -= c[i] * d[i+1]
22+
2323
return d
2424

2525
def main():
2626
# example for matrix
2727
# [1 4 0][x] [7]
2828
# [2 3 5][y] = [5]
2929
# [0 3 6][z] [3]
30+
3031
# [.8666]
3132
# soln will equal [1.533]
3233
# [-.266]
@@ -35,6 +36,7 @@ def main():
3536
b = [1, 3, 6]
3637
c = [4, 5, 0]
3738
d = [7, 5, 3]
39+
3840
soln = thomas(a, b, c, d)
3941
print(soln)
4042

0 commit comments

Comments
 (0)