mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Delete arithmetic_analysis/
directory and relocate its contents (#10824)
* Remove eval from arithmetic_analysis/newton_raphson.py * Relocate contents of arithmetic_analysis/ Delete the arithmetic_analysis/ directory and relocate its files because the purpose of the directory was always ill-defined. "Arithmetic analysis" isn't a field of math, and the directory's files contained algorithms for linear algebra, numerical analysis, and physics. Relocated the directory's linear algebra algorithms to linear_algebra/, its numerical analysis algorithms to a new subdirectory called maths/numerical_analysis/, and its single physics algorithm to physics/. * updating DIRECTORY.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
a9cee1d933
commit
a8b6bda993
43
DIRECTORY.md
43
DIRECTORY.md
|
@ -1,17 +1,4 @@
|
||||||
|
|
||||||
## Arithmetic Analysis
|
|
||||||
* [Bisection](arithmetic_analysis/bisection.py)
|
|
||||||
* [Gaussian Elimination](arithmetic_analysis/gaussian_elimination.py)
|
|
||||||
* [In Static Equilibrium](arithmetic_analysis/in_static_equilibrium.py)
|
|
||||||
* [Intersection](arithmetic_analysis/intersection.py)
|
|
||||||
* [Jacobi Iteration Method](arithmetic_analysis/jacobi_iteration_method.py)
|
|
||||||
* [Lu Decomposition](arithmetic_analysis/lu_decomposition.py)
|
|
||||||
* [Newton Forward Interpolation](arithmetic_analysis/newton_forward_interpolation.py)
|
|
||||||
* [Newton Method](arithmetic_analysis/newton_method.py)
|
|
||||||
* [Newton Raphson](arithmetic_analysis/newton_raphson.py)
|
|
||||||
* [Newton Raphson New](arithmetic_analysis/newton_raphson_new.py)
|
|
||||||
* [Secant Method](arithmetic_analysis/secant_method.py)
|
|
||||||
|
|
||||||
## Audio Filters
|
## Audio Filters
|
||||||
* [Butterworth Filter](audio_filters/butterworth_filter.py)
|
* [Butterworth Filter](audio_filters/butterworth_filter.py)
|
||||||
* [Iir Filter](audio_filters/iir_filter.py)
|
* [Iir Filter](audio_filters/iir_filter.py)
|
||||||
|
@ -520,6 +507,9 @@
|
||||||
* [Test Knapsack](knapsack/tests/test_knapsack.py)
|
* [Test Knapsack](knapsack/tests/test_knapsack.py)
|
||||||
|
|
||||||
## Linear Algebra
|
## Linear Algebra
|
||||||
|
* [Gaussian Elimination](linear_algebra/gaussian_elimination.py)
|
||||||
|
* [Jacobi Iteration Method](linear_algebra/jacobi_iteration_method.py)
|
||||||
|
* [Lu Decomposition](linear_algebra/lu_decomposition.py)
|
||||||
* Src
|
* Src
|
||||||
* [Conjugate Gradient](linear_algebra/src/conjugate_gradient.py)
|
* [Conjugate Gradient](linear_algebra/src/conjugate_gradient.py)
|
||||||
* [Lib](linear_algebra/src/lib.py)
|
* [Lib](linear_algebra/src/lib.py)
|
||||||
|
@ -583,7 +573,6 @@
|
||||||
* [Binary Multiplication](maths/binary_multiplication.py)
|
* [Binary Multiplication](maths/binary_multiplication.py)
|
||||||
* [Binomial Coefficient](maths/binomial_coefficient.py)
|
* [Binomial Coefficient](maths/binomial_coefficient.py)
|
||||||
* [Binomial Distribution](maths/binomial_distribution.py)
|
* [Binomial Distribution](maths/binomial_distribution.py)
|
||||||
* [Bisection](maths/bisection.py)
|
|
||||||
* [Ceil](maths/ceil.py)
|
* [Ceil](maths/ceil.py)
|
||||||
* [Chebyshev Distance](maths/chebyshev_distance.py)
|
* [Chebyshev Distance](maths/chebyshev_distance.py)
|
||||||
* [Check Polygon](maths/check_polygon.py)
|
* [Check Polygon](maths/check_polygon.py)
|
||||||
|
@ -617,7 +606,6 @@
|
||||||
* [Germain Primes](maths/germain_primes.py)
|
* [Germain Primes](maths/germain_primes.py)
|
||||||
* [Greatest Common Divisor](maths/greatest_common_divisor.py)
|
* [Greatest Common Divisor](maths/greatest_common_divisor.py)
|
||||||
* [Hardy Ramanujanalgo](maths/hardy_ramanujanalgo.py)
|
* [Hardy Ramanujanalgo](maths/hardy_ramanujanalgo.py)
|
||||||
* [Integration By Simpson Approx](maths/integration_by_simpson_approx.py)
|
|
||||||
* [Interquartile Range](maths/interquartile_range.py)
|
* [Interquartile Range](maths/interquartile_range.py)
|
||||||
* [Is Int Palindrome](maths/is_int_palindrome.py)
|
* [Is Int Palindrome](maths/is_int_palindrome.py)
|
||||||
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
|
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
|
||||||
|
@ -644,10 +632,24 @@
|
||||||
* [Modular Exponential](maths/modular_exponential.py)
|
* [Modular Exponential](maths/modular_exponential.py)
|
||||||
* [Monte Carlo](maths/monte_carlo.py)
|
* [Monte Carlo](maths/monte_carlo.py)
|
||||||
* [Monte Carlo Dice](maths/monte_carlo_dice.py)
|
* [Monte Carlo Dice](maths/monte_carlo_dice.py)
|
||||||
* [Nevilles Method](maths/nevilles_method.py)
|
|
||||||
* [Newton Raphson](maths/newton_raphson.py)
|
|
||||||
* [Number Of Digits](maths/number_of_digits.py)
|
* [Number Of Digits](maths/number_of_digits.py)
|
||||||
* [Numerical Integration](maths/numerical_integration.py)
|
* Numerical Analysis
|
||||||
|
* [Bisection](maths/numerical_analysis/bisection.py)
|
||||||
|
* [Bisection 2](maths/numerical_analysis/bisection_2.py)
|
||||||
|
* [Integration By Simpson Approx](maths/numerical_analysis/integration_by_simpson_approx.py)
|
||||||
|
* [Intersection](maths/numerical_analysis/intersection.py)
|
||||||
|
* [Nevilles Method](maths/numerical_analysis/nevilles_method.py)
|
||||||
|
* [Newton Forward Interpolation](maths/numerical_analysis/newton_forward_interpolation.py)
|
||||||
|
* [Newton Method](maths/numerical_analysis/newton_method.py)
|
||||||
|
* [Newton Raphson](maths/numerical_analysis/newton_raphson.py)
|
||||||
|
* [Newton Raphson 2](maths/numerical_analysis/newton_raphson_2.py)
|
||||||
|
* [Newton Raphson New](maths/numerical_analysis/newton_raphson_new.py)
|
||||||
|
* [Numerical Integration](maths/numerical_analysis/numerical_integration.py)
|
||||||
|
* [Runge Kutta](maths/numerical_analysis/runge_kutta.py)
|
||||||
|
* [Runge Kutta Fehlberg 45](maths/numerical_analysis/runge_kutta_fehlberg_45.py)
|
||||||
|
* [Secant Method](maths/numerical_analysis/secant_method.py)
|
||||||
|
* [Simpson Rule](maths/numerical_analysis/simpson_rule.py)
|
||||||
|
* [Square Root](maths/numerical_analysis/square_root.py)
|
||||||
* [Odd Sieve](maths/odd_sieve.py)
|
* [Odd Sieve](maths/odd_sieve.py)
|
||||||
* [Perfect Cube](maths/perfect_cube.py)
|
* [Perfect Cube](maths/perfect_cube.py)
|
||||||
* [Perfect Number](maths/perfect_number.py)
|
* [Perfect Number](maths/perfect_number.py)
|
||||||
|
@ -673,8 +675,6 @@
|
||||||
* [Radians](maths/radians.py)
|
* [Radians](maths/radians.py)
|
||||||
* [Radix2 Fft](maths/radix2_fft.py)
|
* [Radix2 Fft](maths/radix2_fft.py)
|
||||||
* [Remove Digit](maths/remove_digit.py)
|
* [Remove Digit](maths/remove_digit.py)
|
||||||
* [Runge Kutta](maths/runge_kutta.py)
|
|
||||||
* [Runge Kutta Fehlberg 45](maths/runge_kutta_fehlberg_45.py)
|
|
||||||
* [Segmented Sieve](maths/segmented_sieve.py)
|
* [Segmented Sieve](maths/segmented_sieve.py)
|
||||||
* Series
|
* Series
|
||||||
* [Arithmetic](maths/series/arithmetic.py)
|
* [Arithmetic](maths/series/arithmetic.py)
|
||||||
|
@ -687,7 +687,6 @@
|
||||||
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
|
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
|
||||||
* [Sigmoid](maths/sigmoid.py)
|
* [Sigmoid](maths/sigmoid.py)
|
||||||
* [Signum](maths/signum.py)
|
* [Signum](maths/signum.py)
|
||||||
* [Simpson Rule](maths/simpson_rule.py)
|
|
||||||
* [Simultaneous Linear Equation Solver](maths/simultaneous_linear_equation_solver.py)
|
* [Simultaneous Linear Equation Solver](maths/simultaneous_linear_equation_solver.py)
|
||||||
* [Sin](maths/sin.py)
|
* [Sin](maths/sin.py)
|
||||||
* [Sock Merchant](maths/sock_merchant.py)
|
* [Sock Merchant](maths/sock_merchant.py)
|
||||||
|
@ -709,7 +708,6 @@
|
||||||
* [Proth Number](maths/special_numbers/proth_number.py)
|
* [Proth Number](maths/special_numbers/proth_number.py)
|
||||||
* [Ugly Numbers](maths/special_numbers/ugly_numbers.py)
|
* [Ugly Numbers](maths/special_numbers/ugly_numbers.py)
|
||||||
* [Weird Number](maths/special_numbers/weird_number.py)
|
* [Weird Number](maths/special_numbers/weird_number.py)
|
||||||
* [Square Root](maths/square_root.py)
|
|
||||||
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
|
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
|
||||||
* [Sum Of Digits](maths/sum_of_digits.py)
|
* [Sum Of Digits](maths/sum_of_digits.py)
|
||||||
* [Sum Of Geometric Progression](maths/sum_of_geometric_progression.py)
|
* [Sum Of Geometric Progression](maths/sum_of_geometric_progression.py)
|
||||||
|
@ -812,6 +810,7 @@
|
||||||
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
|
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
|
||||||
* [Hubble Parameter](physics/hubble_parameter.py)
|
* [Hubble Parameter](physics/hubble_parameter.py)
|
||||||
* [Ideal Gas Law](physics/ideal_gas_law.py)
|
* [Ideal Gas Law](physics/ideal_gas_law.py)
|
||||||
|
* [In Static Equilibrium](physics/in_static_equilibrium.py)
|
||||||
* [Kinetic Energy](physics/kinetic_energy.py)
|
* [Kinetic Energy](physics/kinetic_energy.py)
|
||||||
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
|
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
|
||||||
* [Malus Law](physics/malus_law.py)
|
* [Malus Law](physics/malus_law.py)
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
# Arithmetic analysis
|
|
||||||
|
|
||||||
Arithmetic analysis is a branch of mathematics that deals with solving linear equations.
|
|
||||||
|
|
||||||
* <https://en.wikipedia.org/wiki/System_of_linear_equations>
|
|
||||||
* <https://en.wikipedia.org/wiki/Gaussian_elimination>
|
|
||||||
* <https://en.wikipedia.org/wiki/Root-finding_algorithms>
|
|
|
@ -5,42 +5,41 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from math import * # noqa: F403
|
|
||||||
|
|
||||||
from sympy import diff
|
from sympy import diff, lambdify, symbols
|
||||||
|
|
||||||
|
|
||||||
def newton_raphson(
|
def newton_raphson(func: str, a: float | Decimal, precision: float = 1e-10) -> float:
|
||||||
func: str, a: float | Decimal, precision: float = 10**-10
|
|
||||||
) -> float:
|
|
||||||
"""Finds root from the point 'a' onwards by Newton-Raphson method
|
"""Finds root from the point 'a' onwards by Newton-Raphson method
|
||||||
>>> newton_raphson("sin(x)", 2)
|
>>> newton_raphson("sin(x)", 2)
|
||||||
3.1415926536808043
|
3.1415926536808043
|
||||||
>>> newton_raphson("x**2 - 5*x +2", 0.4)
|
>>> newton_raphson("x**2 - 5*x + 2", 0.4)
|
||||||
0.4384471871911695
|
0.4384471871911695
|
||||||
>>> newton_raphson("x**2 - 5", 0.1)
|
>>> newton_raphson("x**2 - 5", 0.1)
|
||||||
2.23606797749979
|
2.23606797749979
|
||||||
>>> newton_raphson("log(x)- 1", 2)
|
>>> newton_raphson("log(x) - 1", 2)
|
||||||
2.718281828458938
|
2.718281828458938
|
||||||
"""
|
"""
|
||||||
x = a
|
x = symbols("x")
|
||||||
|
f = lambdify(x, func, "math")
|
||||||
|
f_derivative = lambdify(x, diff(func), "math")
|
||||||
|
x_curr = a
|
||||||
while True:
|
while True:
|
||||||
x = Decimal(x) - (
|
x_curr = Decimal(x_curr) - Decimal(f(x_curr)) / Decimal(f_derivative(x_curr))
|
||||||
Decimal(eval(func)) / Decimal(eval(str(diff(func)))) # noqa: S307
|
if abs(f(x_curr)) < precision:
|
||||||
)
|
return float(x_curr)
|
||||||
# This number dictates the accuracy of the answer
|
|
||||||
if abs(eval(func)) < precision: # noqa: S307
|
|
||||||
return float(x)
|
|
||||||
|
|
||||||
|
|
||||||
# Let's Execute
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Find root of trigonometric function
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
|
||||||
# Find value of pi
|
# Find value of pi
|
||||||
print(f"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}")
|
print(f"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}")
|
||||||
# Find root of polynomial
|
# Find root of polynomial
|
||||||
print(f"The root of x**2 - 5*x + 2 = 0 is {newton_raphson('x**2 - 5*x + 2', 0.4)}")
|
print(f"The root of x**2 - 5*x + 2 = 0 is {newton_raphson('x**2 - 5*x + 2', 0.4)}")
|
||||||
# Find Square Root of 5
|
# Find value of e
|
||||||
print(f"The root of log(x) - 1 = 0 is {newton_raphson('log(x) - 1', 2)}")
|
print(f"The root of log(x) - 1 = 0 is {newton_raphson('log(x) - 1', 2)}")
|
||||||
# Exponential Roots
|
# Find root of exponential function
|
||||||
print(f"The root of exp(x) - 1 = 0 is {newton_raphson('exp(x) - 1', 0)}")
|
print(f"The root of exp(x) - 1 = 0 is {newton_raphson('exp(x) - 1', 0)}")
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 40 KiB |
Loading…
Reference in New Issue
Block a user