Skip to content

Newton raphson complex #6545

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
Oct 2, 2022

Conversation

Saksham1970
Copy link
Contributor

Describe your change:

I noticed the Newton Raphson method could be improved, so I made an altered script for it.

There are currently 2 scripts for newton raphson method in Python/arithmetic_analysis/ :
newton_method.py
newton_raphson.py

There are a few issues with them,

  • They both don't support complex numbers, while the method itself in theory does.
  • newton_method.py: requires user to pass both the mathematical function and its derivative into the function
  • newton_raphson.py: uses the wrong precision check; it compares the value of the f(x) with the precision required, rather than the difference in the consecutive x's. This resulted in its values to be wrong as in their test cases they showed pi = 3.1415926536808043 while correct precision check would give a value of 3.141592653589793 which is way closer to actual value of pi.
  • newton_raphson.py: also extensively uses eval() statements, which are necessary for finding derivatives, but the implementation limits them from only taking the x as variable, and it was also used in every loop making the computation slower.
  • newton_raphson.py: it doesn't check for the division by 0, which is the biggest limitation to using newton method, since the root can't be found for those cases.

I have taken goods from both the files and added more improvements and created a third file for the time being.

  • 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}.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 2, 2022

# Find value of e
print(
f"The root of log(y) - 1 = 0 is \
Copy link
Member

Choose a reason for hiding this comment

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

PEP8 suggests that we avoid backslashes in Python code because whitespace to the right of the backslash breaks the script that is invisible to the reader.

Copy link
Contributor Author

@Saksham1970 Saksham1970 Oct 2, 2022

Choose a reason for hiding this comment

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

I see, what's the correct way of reducing the length of the line to pass the flake8 test?

@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 2, 2022
@cclauss cclauss merged commit f42b2b8 into TheAlgorithms:master Oct 2, 2022
@Saksham1970 Saksham1970 deleted the newton-raphson-complex branch October 2, 2022 18:26
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.

2 participants