-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
cclauss
merged 5 commits into
TheAlgorithms:master
from
Saksham1970:newton-raphson-complex
Oct 2, 2022
Merged
Newton raphson complex #6545
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
545b3a5
Newton raphson better implementation
Saksham1970 9010d88
flake8 test passed
Saksham1970 c67d0a6
Update arithmetic_analysis/newton_raphson_new.py
Saksham1970 7c94e0c
added multiline suggestions
Saksham1970 1b71c84
Merge branch 'TheAlgorithms:master' into newton-raphson-complex
Saksham1970 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Implementing Newton Raphson method in Python | ||
# Author: Saksham Gupta | ||
# | ||
# The Newton-Raphson method (also known as Newton's method) is a way to | ||
# quickly find a good approximation for the root of a functreal-valued ion | ||
# The method can also be extended to complex functions | ||
# | ||
# Newton's Method - https://en.wikipedia.org/wiki/Newton's_method | ||
|
||
from sympy import diff, lambdify, symbols | ||
from sympy.functions import * # noqa: F401, F403 | ||
|
||
|
||
def newton_raphson( | ||
function: str, | ||
starting_point: complex, | ||
variable: str = "x", | ||
precision: float = 10**-10, | ||
multiplicity: int = 1, | ||
) -> complex: | ||
"""Finds root from the 'starting_point' onwards by Newton-Raphson method | ||
Refer to https://docs.sympy.org/latest/modules/functions/index.html | ||
for usable mathematical functions | ||
|
||
>>> newton_raphson("sin(x)", 2) | ||
3.141592653589793 | ||
>>> newton_raphson("x**4 -5", 0.4 + 5j) | ||
(-7.52316384526264e-37+1.4953487812212207j) | ||
>>> newton_raphson('log(y) - 1', 2, variable='y') | ||
2.7182818284590455 | ||
>>> newton_raphson('exp(x) - 1', 10, precision=0.005) | ||
1.2186556186174883e-10 | ||
>>> newton_raphson('cos(x)', 0) | ||
Traceback (most recent call last): | ||
... | ||
ZeroDivisionError: Could not find root | ||
""" | ||
|
||
x = symbols(variable) | ||
func = lambdify(x, function) | ||
diff_function = lambdify(x, diff(function, x)) | ||
|
||
prev_guess = starting_point | ||
|
||
while True: | ||
if diff_function(prev_guess) != 0: | ||
next_guess = prev_guess - multiplicity * func(prev_guess) / diff_function( | ||
prev_guess | ||
) | ||
else: | ||
raise ZeroDivisionError("Could not find root") from None | ||
|
||
# Precision is checked by comparing the difference of consecutive guesses | ||
if abs(next_guess - prev_guess) < precision: | ||
return next_guess | ||
|
||
prev_guess = next_guess | ||
|
||
|
||
# Let's Execute | ||
if __name__ == "__main__": | ||
|
||
# Find root of trigonometric function | ||
# Find value of pi | ||
print(f"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}") | ||
|
||
# Find root of polynomial | ||
# Find fourth Root of 5 | ||
print(f"The root of x**4 - 5 = 0 is {newton_raphson('x**4 -5', 0.4 +5j)}") | ||
|
||
# Find value of e | ||
print( | ||
f"The root of log(y) - 1 = 0 is \ | ||
{newton_raphson('log(y) - 1', 2, variable='y')}" | ||
) | ||
Saksham1970 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Exponential Roots | ||
print( | ||
f"The root of exp(x) - 1 = 0 is \ | ||
{newton_raphson('exp(x) - 1', 10, precision=0.005)}" | ||
) | ||
|
||
# Find root of cos(x) | ||
print(f"The root of cos(x) = 0 is {newton_raphson('cos(x)', 0)}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
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 see, what's the correct way of reducing the length of the line to pass the flake8 test?