Skip to content

Initial commit of conjugate gradient method #2486

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

Conversation

zakademic
Copy link
Contributor

@zakademic zakademic commented Sep 26, 2020

Describe your change:

This is an implementation of the conjugate gradient method for solving a linear system given an SPD matrix A.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

This looks really great. I am not a numpy guy so I can not comment on the usage of numpy. In Python, UPPERCASE variable names are reserved for constants that never change during the runtime of the script. Function parameters should never be uppercase. Also, please use more self-documenting variable names. Single-letter variable names look so old school. Instead of A, use matrix or spd_matrix or some other name that is even more self-documenting of the data that the variable contains. It is OK to have a comment that links the Python variable name back to the formula name: spd_matrix is often called A in discussions of the conjugate gradient algorithm.

Returns a symmetric positive definite matrix given a dimension.

Input:
N is an integer.
Copy link
Member

Choose a reason for hiding this comment

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

This line is redundant now that we have type hints so let's instead give N a more self-documenting variable name so that this comment is no longer required or make this comment document the what & why, not the datatype.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes good point. I changed this.

@@ -0,0 +1,172 @@
import numpy as np
Copy link
Member

Choose a reason for hiding this comment

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

Please add a URL that can help the reader learn about the problems that your algorithm is trying to solve...
https://en.wikipedia.org/wiki/Conjugate_gradient_method
https://en.wikipedia.org/wiki/Definite_symmetric_matrix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added :)

zakademic and others added 3 commits September 28, 2020 23:28
…ore descriptive naming, added check for symmetry in _is_matrix_spd
@zakademic
Copy link
Contributor Author

This looks really great. I am not a numpy guy so I can not comment on the usage of numpy. In Python, UPPERCASE variable names are reserved for constants that never change during the runtime of the script. Function parameters should never be uppercase. Also, please use more self-documenting variable names. Single-letter variable names look so old school. Instead of A, use matrix or spd_matrix or some other name that is even more self-documenting of the data that the variable contains. It is OK to have a comment that links the Python variable name back to the formula name: spd_matrix is often called A in discussions of the conjugate gradient algorithm.

Totally agree. I changed many namings to be more meaningful. Thanks!

return np.all(eigen_values > 0)


def _create_spd_matrix(N: np.int64) -> np.array:
Copy link
Member

Choose a reason for hiding this comment

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

N? A more self-documenting name please with no uppercase in variable names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed this :)

@stale
Copy link

stale bot commented Nov 22, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Nov 22, 2020
@ghost ghost added require descriptive names This PR needs descriptive function and/or variable names require type hints https://docs.python.org/3/library/typing.html labels Nov 23, 2020
@stale stale bot removed the stale Used to mark an issue or pull request stale. label Nov 23, 2020
Copy link
Contributor

@amaank404 amaank404 left a comment

Choose a reason for hiding this comment

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

@dhruvmanila, check this

@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed require type hints https://docs.python.org/3/library/typing.html require descriptive names This PR needs descriptive function and/or variable names labels Dec 10, 2020
@dhruvmanila
Copy link
Member

@algorithms-keeper review

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Dec 12, 2020
@dhruvmanila dhruvmanila merged commit 533e36d into TheAlgorithms:master Dec 12, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Initial commit of the conjugate gradient method
* Update linear_algebra/src/conjugate_gradient.py
* Added documentation links, changed variable names to lower case and more descriptive naming, added check for symmetry in _is_matrix_spd
* Made changes to some variable naming to be more clear
* Update conjugate_gradient.py

Co-authored-by: Zeyad Zaky <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Dhruv Manilawala <[email protected]>
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
* Initial commit of the conjugate gradient method
* Update linear_algebra/src/conjugate_gradient.py
* Added documentation links, changed variable names to lower case and more descriptive naming, added check for symmetry in _is_matrix_spd
* Made changes to some variable naming to be more clear
* Update conjugate_gradient.py

Co-authored-by: Zeyad Zaky <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Dhruv Manilawala <[email protected]>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
* Initial commit of the conjugate gradient method
* Update linear_algebra/src/conjugate_gradient.py
* Added documentation links, changed variable names to lower case and more descriptive naming, added check for symmetry in _is_matrix_spd
* Made changes to some variable naming to be more clear
* Update conjugate_gradient.py

Co-authored-by: Zeyad Zaky <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Dhruv Manilawala <[email protected]>
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* Initial commit of the conjugate gradient method
* Update linear_algebra/src/conjugate_gradient.py
* Added documentation links, changed variable names to lower case and more descriptive naming, added check for symmetry in _is_matrix_spd
* Made changes to some variable naming to be more clear
* Update conjugate_gradient.py

Co-authored-by: Zeyad Zaky <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Dhruv Manilawala <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants