-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Conversation
There was a problem hiding this 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 liketensor[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 astensor[r, :, :, :, :]
. Choose what you think is clearer.
|
||
print("Back subsitution") | ||
print(back_substitution(A)) | ||
print() |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Generally, you want
array
and notmatrix
. - The explicit form of initialization, rather than a string, is clearer.
- 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 | ||
""" |
There was a problem hiding this comment.
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 | ||
""" |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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,:] |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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,:] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use -=
here.
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. |
There was a problem hiding this 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.
The windows for viewing code in the book are ok too. |
There was a problem hiding this 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.
Python implementation using numpy