-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Created Sherman Morrison method #1162
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.
This is really slick! I like the type hints and assertions to guard against bad data. Nice contribution.
A couple of optional things that I would be interested to see added are doctests and list comprehensions.
1. Added docstring tests 2. Tweaked __str__() using join 3. Added __repr__() 4. Changed index validation to be independent method
@cclauss I added doctests. By the way, I'm sorry, what do you mean for list comprehension? Do you want some method to iterate over all elements in matrix as single 1x1 element or row/column vector? |
matrix/sherman_morrison.py
Outdated
s += "[" | ||
s += ", ".join(string_format_identifier % (obj,) for obj in row_vector) | ||
s += "]\n" | ||
s += "\n".join("[" + ", ".join(string_format_identifier % (obj,) for obj in row_vector) + "]" for row_vector in self.array) |
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.
This line can not be read on GitHub without left-right scrolling. Please wrap it so this is no longer the case.
matrix/sherman_morrison.py
Outdated
assert(isinstance(another, Matrix)) | ||
assert(self.row == another.row and self.column == another.column) |
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.
assert(0)
assert(False)
Lose the parens ()... it is not a function.
1. Reduced line length for __str__() 2. Removed parens for assert
If you need more things to fix then please write comments. Thank you. |
@cclauss Is there anything needed to change? |
* Created Sherman Morrison * Added docstring for class * Updated Sherman morrison 1. Added docstring tests 2. Tweaked __str__() using join 3. Added __repr__() 4. Changed index validation to be independent method * Applied cclauss's point 1. Reduced line length for __str__() 2. Removed parens for assert
I created method to apply Sherman-Morrison formula to Matrix.
Sherman-Morrison formula is important to reduce time complexity on calculating inverse matrix on some special cases. This formula is also used to improve BFGS update performance. For deeper stuffs please look into this documents:
https://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula
https://en.wikipedia.org/wiki/Broyden%E2%80%93Fletcher%E2%80%93Goldfarb%E2%80%93Shanno_algorithm
https://en.wikipedia.org/wiki/Quasi-Newton_method