Skip to content

Implement gaussian elimination in python #370

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 5 commits into from
Sep 23, 2018

Conversation

jasmaa
Copy link
Contributor

@jasmaa jasmaa commented Sep 7, 2018

Python implementation using numpy

@june128 june128 added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Sep 7, 2018
Copy link
Member

@berquist berquist left a comment

Choose a reason for hiding this comment

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

Hi!

  • I didn't mark them, but there are quite a few PEP 8 violations. The most important here are missing spaces after commas and missing spaces between binary operators. This line
A[r,:] = A[r,:] + frac*A[pivot_row,:]

should change to

A[r, :] = A[r, :] + frac * A[pivot_row, :]

A tool like pylint will help you catch a lot of these.

  • This is not a requirement, but if you have A[r, :] with NumPy, the colon is redundant. In fact, if you have a quantity like tensor[a, b, c, d, e], and want to take all of the last four dimension but have a specific index into the first dimension, tensor[r] does the same thing as tensor[r, :, :, :, :]. Choose what you think is clearer.


print("Back subsitution")
print(back_substitution(A))
print()
Copy link
Member

Choose a reason for hiding this comment

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

Can you make this more consistent with the other printing sections?

print("Back substitution")
print(back_substitution(A), "\n")



def main():
A = np.matrix('2. 3 4 6; 1 2 3 4; 3 -4 0 10')
Copy link
Member

Choose a reason for hiding this comment

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

  1. Generally, you want array and not matrix.
  2. The explicit form of initialization, rather than a string, is clearer.
  3. The single float to force the type is too easy to overlook. Either do
A = np.array([[2,  3, 4,  6],
              [1,  2, 3,  4],
              [3, -4, 0, 10]], dtype=float)

or

A = np.array([[2.0,  3.0, 4.0,  6.0],
              [1.0,  2.0, 3.0,  4.0],
              [3.0, -4.0, 0.0, 10.0]])


"""
Assumes A is already ref
"""
Copy link
Member

Choose a reason for hiding this comment

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

If you want a code comment and not an actual function docstring (which would go under the def), use the usual # syntax. Better to say "row echelon form" than "ref".


"""
Assumes A has a unique solution and A in ref
"""
Copy link
Member

Choose a reason for hiding this comment

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

Same docstring and ref comment.

print(A, "\n")

gaussian_elimination(A)
print("Gaussian elimination, REF")
Copy link
Member

Choose a reason for hiding this comment

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

I would not abbreviate REF in print output.

"""
Assumes A is already ref
"""
def gauss_jordon_elimination(A):
Copy link
Member

Choose a reason for hiding this comment

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

jordon -> jordan

print(back_substitution(A))
print()

gauss_jordon_elimination(A)
Copy link
Member

Choose a reason for hiding this comment

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

jordon -> jordan

# Step 3: Get fraction
frac = -A[r,pivot_col] / A[pivot_row,pivot_col]
# Step 4: Add rows
A[r,:] = A[r,:] + frac*A[pivot_row,:]
Copy link
Member

Choose a reason for hiding this comment

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

Use += here.

continue

# Set each pivot to one via row scaling
A[row,:] = A[row,:] / A[row,col]
Copy link
Member

Choose a reason for hiding this comment

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

Use /= here.


# Zero out elements above pivot
for r in range(row):
A[r,:] = A[r,:] - A[r,col] * A[row,:]
Copy link
Member

Choose a reason for hiding this comment

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

Use -= here.

@leios
Copy link
Member

leios commented Sep 21, 2018

Hey guys, checking up on this PR. Are you happy with the current code @berquist ? If so, I'll approve. Otherwise, I'll also do my own review.

Copy link
Member

@berquist berquist left a comment

Choose a reason for hiding this comment

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

No. There are still two "ref"s in code comments that IMO should be changed to "row echelon form". Other than that, I'm fine.

@berquist
Copy link
Member

The windows for viewing code in the book are ok too.

Copy link
Member

@june128 june128 left a comment

Choose a reason for hiding this comment

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

Since @berquist approved this PR, I think it's ready to be merged.

@june128 june128 merged commit 0f16f25 into algorithm-archivists:master Sep 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants