From 545b3a5c9651e53d87121de566910cb7a1354d7b Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Sun, 2 Oct 2022 18:17:01 +0530 Subject: [PATCH 1/4] Newton raphson better implementation --- arithmetic_analysis/newton_raphson_new.py | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 arithmetic_analysis/newton_raphson_new.py diff --git a/arithmetic_analysis/newton_raphson_new.py b/arithmetic_analysis/newton_raphson_new.py new file mode 100644 index 000000000000..5587cc48adc0 --- /dev/null +++ b/arithmetic_analysis/newton_raphson_new.py @@ -0,0 +1,73 @@ + +# 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 real-valued function +# The method can also be extended to complex functions +# +# Newton's Method - https://en.wikipedia.org/wiki/Newton's_method + +from sympy import diff, symbols, lambdify +from sympy.functions import * + + +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) + diff_function = lambdify(x, diff(function, x)) + function = lambdify(x, function) + + prev_guess = starting_point + + while True: + if diff_function(prev_guess) != 0: + next_guess = prev_guess - multiplicity * function(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')}") + # 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)}") + \ No newline at end of file From 9010d88f702c31cdb47b7d00c2047a21b7981bc0 Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Sun, 2 Oct 2022 19:05:05 +0530 Subject: [PATCH 2/4] flake8 test passed --- arithmetic_analysis/newton_raphson_new.py | 41 ++++++++++++++--------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/arithmetic_analysis/newton_raphson_new.py b/arithmetic_analysis/newton_raphson_new.py index 5587cc48adc0..1ec2115010f2 100644 --- a/arithmetic_analysis/newton_raphson_new.py +++ b/arithmetic_analysis/newton_raphson_new.py @@ -1,26 +1,26 @@ - # Implementing Newton Raphson method in Python -# Author: Saksham Gupta +# 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 real-valued function +# 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, symbols, lambdify -from sympy.functions import * +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 + 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 + Refer to https://docs.sympy.org/latest/modules/functions/index.html + for usable mathematical functions >>> newton_raphson("sin(x)", 2) 3.141592653589793 @@ -37,19 +37,21 @@ def newton_raphson( """ x = symbols(variable) + func = lambdify(x, function) diff_function = lambdify(x, diff(function, x)) - function = lambdify(x, function) prev_guess = starting_point while True: if diff_function(prev_guess) != 0: - next_guess = prev_guess - multiplicity * function(prev_guess) / diff_function(prev_guess) + 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: + if abs(next_guess - prev_guess) < precision: return next_guess prev_guess = next_guess @@ -61,13 +63,22 @@ def newton_raphson( # 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')}") + print( + f"The root of log(y) - 1 = 0 is \ +{newton_raphson('log(y) - 1', 2, variable='y')}" + ) + # Exponential Roots - print(f"The root of exp(x) - 1 = 0 is {newton_raphson('exp(x) - 1', 10, precision=0.005)}") + 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)}") - \ No newline at end of file From c67d0a604cee3511fc6a924a3210fd5b512bdef3 Mon Sep 17 00:00:00 2001 From: Saksham1970 <45041294+Saksham1970@users.noreply.github.com> Date: Sun, 2 Oct 2022 22:12:29 +0530 Subject: [PATCH 3/4] Update arithmetic_analysis/newton_raphson_new.py Co-authored-by: Christian Clauss --- arithmetic_analysis/newton_raphson_new.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arithmetic_analysis/newton_raphson_new.py b/arithmetic_analysis/newton_raphson_new.py index 1ec2115010f2..1c8971219d5b 100644 --- a/arithmetic_analysis/newton_raphson_new.py +++ b/arithmetic_analysis/newton_raphson_new.py @@ -70,8 +70,8 @@ def newton_raphson( # Find value of e print( - f"The root of log(y) - 1 = 0 is \ -{newton_raphson('log(y) - 1', 2, variable='y')}" + "The root of log(y) - 1 = 0 is " + f"{newton_raphson('log(y) - 1', 2, variable='y')}" ) # Exponential Roots From 7c94e0cd1da2d024fa370a17f3e24ab16c2f3dcf Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Sun, 2 Oct 2022 22:33:10 +0530 Subject: [PATCH 4/4] added multiline suggestions --- arithmetic_analysis/newton_raphson_new.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arithmetic_analysis/newton_raphson_new.py b/arithmetic_analysis/newton_raphson_new.py index 1c8971219d5b..19ea4ce21806 100644 --- a/arithmetic_analysis/newton_raphson_new.py +++ b/arithmetic_analysis/newton_raphson_new.py @@ -70,14 +70,14 @@ def newton_raphson( # Find value of e print( - "The root of log(y) - 1 = 0 is " - f"{newton_raphson('log(y) - 1', 2, variable='y')}" + "The root of log(y) - 1 = 0 is ", + f"{newton_raphson('log(y) - 1', 2, variable='y')}", ) # Exponential Roots print( - f"The root of exp(x) - 1 = 0 is \ -{newton_raphson('exp(x) - 1', 10, precision=0.005)}" + "The root of exp(x) - 1 = 0 is", + f"{newton_raphson('exp(x) - 1', 10, precision=0.005)}", ) # Find root of cos(x)