Skip to content

thomas python example #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 3, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions chapters/matrix_methods/thomas/code/python/thomas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#Author: gammison
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other Python code in the project puts a space between # and the comment text.


#note this example is inplace and destructive
def thomas(a:list,b:list,c:list,d:list):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use type hints elsewhere in the python code. The code should have the same style and I would request that this PR leave it out. If you strongly feel that type hints are a good thing, then either make an issue discussing it or create a brand new PR introducing it in all python codes.


#set the initial elements
c[0] = c[0] / b[0]
d[0] = d[0] / b[0]

n = len(d) #number of equations to solve
#range is exclusive, loop will run from 1 to n-1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think range() is common in enough to Python to leave this comment out.

for i in range(1,n):
#scale factor for c and d
scale = 1 / (b[i] - c[i-1]*a[i])

c[i] = c[i] * scale
d[i] = (d[i] -a[i] * d[i-1]) * scale


#do the back substitution, will loop n-2 to 0 inclusive
for i in range(n-2,-1,-1):
d[i] = d[i] - c[i]*d[i+1]

return d

#Complexity:
#Two loops,each take O(n), and a few contant time operations => T(n) = 2*O(n)+c*O(1) = O(n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complexity discussions should be left to the accompanying article and not in the source code. Please remove this


#example for matrix
#[1 4 0][x] [7]
#[2 3 5][y] = [5]
#[0 3 6][z] [3]
# [.8666]
#soln will equal [1.533]
# [-.266]
#note we index a from 1 and c from 0
a=[0,2,3]
b=[1,3,6]
c=[4,5,0]
d=[7,5,3]
soln = thomas(a,b,c,d)
print(soln)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap execution code into a main-method. See i.e. data_compression/huffman/code/python/huffman.py

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style changes pushed