Skip to content

Commit a8b6bda

Browse files
tianyizheng02github-actions
and
github-actions
authoredOct 23, 2023
Delete arithmetic_analysis/ directory and relocate its contents (TheAlgorithms#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>
1 parent a9cee1d commit a8b6bda

26 files changed

+335
-344
lines changed
 

‎DIRECTORY.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11

2-
## Arithmetic Analysis
3-
* [Bisection](arithmetic_analysis/bisection.py)
4-
* [Gaussian Elimination](arithmetic_analysis/gaussian_elimination.py)
5-
* [In Static Equilibrium](arithmetic_analysis/in_static_equilibrium.py)
6-
* [Intersection](arithmetic_analysis/intersection.py)
7-
* [Jacobi Iteration Method](arithmetic_analysis/jacobi_iteration_method.py)
8-
* [Lu Decomposition](arithmetic_analysis/lu_decomposition.py)
9-
* [Newton Forward Interpolation](arithmetic_analysis/newton_forward_interpolation.py)
10-
* [Newton Method](arithmetic_analysis/newton_method.py)
11-
* [Newton Raphson](arithmetic_analysis/newton_raphson.py)
12-
* [Newton Raphson New](arithmetic_analysis/newton_raphson_new.py)
13-
* [Secant Method](arithmetic_analysis/secant_method.py)
14-
152
## Audio Filters
163
* [Butterworth Filter](audio_filters/butterworth_filter.py)
174
* [Iir Filter](audio_filters/iir_filter.py)
@@ -520,6 +507,9 @@
520507
* [Test Knapsack](knapsack/tests/test_knapsack.py)
521508

522509
## Linear Algebra
510+
* [Gaussian Elimination](linear_algebra/gaussian_elimination.py)
511+
* [Jacobi Iteration Method](linear_algebra/jacobi_iteration_method.py)
512+
* [Lu Decomposition](linear_algebra/lu_decomposition.py)
523513
* Src
524514
* [Conjugate Gradient](linear_algebra/src/conjugate_gradient.py)
525515
* [Lib](linear_algebra/src/lib.py)
@@ -583,7 +573,6 @@
583573
* [Binary Multiplication](maths/binary_multiplication.py)
584574
* [Binomial Coefficient](maths/binomial_coefficient.py)
585575
* [Binomial Distribution](maths/binomial_distribution.py)
586-
* [Bisection](maths/bisection.py)
587576
* [Ceil](maths/ceil.py)
588577
* [Chebyshev Distance](maths/chebyshev_distance.py)
589578
* [Check Polygon](maths/check_polygon.py)
@@ -617,7 +606,6 @@
617606
* [Germain Primes](maths/germain_primes.py)
618607
* [Greatest Common Divisor](maths/greatest_common_divisor.py)
619608
* [Hardy Ramanujanalgo](maths/hardy_ramanujanalgo.py)
620-
* [Integration By Simpson Approx](maths/integration_by_simpson_approx.py)
621609
* [Interquartile Range](maths/interquartile_range.py)
622610
* [Is Int Palindrome](maths/is_int_palindrome.py)
623611
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
@@ -644,10 +632,24 @@
644632
* [Modular Exponential](maths/modular_exponential.py)
645633
* [Monte Carlo](maths/monte_carlo.py)
646634
* [Monte Carlo Dice](maths/monte_carlo_dice.py)
647-
* [Nevilles Method](maths/nevilles_method.py)
648-
* [Newton Raphson](maths/newton_raphson.py)
649635
* [Number Of Digits](maths/number_of_digits.py)
650-
* [Numerical Integration](maths/numerical_integration.py)
636+
* Numerical Analysis
637+
* [Bisection](maths/numerical_analysis/bisection.py)
638+
* [Bisection 2](maths/numerical_analysis/bisection_2.py)
639+
* [Integration By Simpson Approx](maths/numerical_analysis/integration_by_simpson_approx.py)
640+
* [Intersection](maths/numerical_analysis/intersection.py)
641+
* [Nevilles Method](maths/numerical_analysis/nevilles_method.py)
642+
* [Newton Forward Interpolation](maths/numerical_analysis/newton_forward_interpolation.py)
643+
* [Newton Method](maths/numerical_analysis/newton_method.py)
644+
* [Newton Raphson](maths/numerical_analysis/newton_raphson.py)
645+
* [Newton Raphson 2](maths/numerical_analysis/newton_raphson_2.py)
646+
* [Newton Raphson New](maths/numerical_analysis/newton_raphson_new.py)
647+
* [Numerical Integration](maths/numerical_analysis/numerical_integration.py)
648+
* [Runge Kutta](maths/numerical_analysis/runge_kutta.py)
649+
* [Runge Kutta Fehlberg 45](maths/numerical_analysis/runge_kutta_fehlberg_45.py)
650+
* [Secant Method](maths/numerical_analysis/secant_method.py)
651+
* [Simpson Rule](maths/numerical_analysis/simpson_rule.py)
652+
* [Square Root](maths/numerical_analysis/square_root.py)
651653
* [Odd Sieve](maths/odd_sieve.py)
652654
* [Perfect Cube](maths/perfect_cube.py)
653655
* [Perfect Number](maths/perfect_number.py)
@@ -673,8 +675,6 @@
673675
* [Radians](maths/radians.py)
674676
* [Radix2 Fft](maths/radix2_fft.py)
675677
* [Remove Digit](maths/remove_digit.py)
676-
* [Runge Kutta](maths/runge_kutta.py)
677-
* [Runge Kutta Fehlberg 45](maths/runge_kutta_fehlberg_45.py)
678678
* [Segmented Sieve](maths/segmented_sieve.py)
679679
* Series
680680
* [Arithmetic](maths/series/arithmetic.py)
@@ -687,7 +687,6 @@
687687
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
688688
* [Sigmoid](maths/sigmoid.py)
689689
* [Signum](maths/signum.py)
690-
* [Simpson Rule](maths/simpson_rule.py)
691690
* [Simultaneous Linear Equation Solver](maths/simultaneous_linear_equation_solver.py)
692691
* [Sin](maths/sin.py)
693692
* [Sock Merchant](maths/sock_merchant.py)
@@ -709,7 +708,6 @@
709708
* [Proth Number](maths/special_numbers/proth_number.py)
710709
* [Ugly Numbers](maths/special_numbers/ugly_numbers.py)
711710
* [Weird Number](maths/special_numbers/weird_number.py)
712-
* [Square Root](maths/square_root.py)
713711
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
714712
* [Sum Of Digits](maths/sum_of_digits.py)
715713
* [Sum Of Geometric Progression](maths/sum_of_geometric_progression.py)
@@ -812,6 +810,7 @@
812810
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
813811
* [Hubble Parameter](physics/hubble_parameter.py)
814812
* [Ideal Gas Law](physics/ideal_gas_law.py)
813+
* [In Static Equilibrium](physics/in_static_equilibrium.py)
815814
* [Kinetic Energy](physics/kinetic_energy.py)
816815
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
817816
* [Malus Law](physics/malus_law.py)

‎arithmetic_analysis/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎arithmetic_analysis/image_data/__init__.py

Whitespace-only changes.

‎arithmetic_analysis/jacobi_iteration_method.py renamed to ‎linear_algebra/jacobi_iteration_method.py

Lines changed: 203 additions & 203 deletions
Large diffs are not rendered by default.
File renamed without changes.

‎arithmetic_analysis/newton_raphson.py renamed to ‎maths/numerical_analysis/newton_raphson.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,41 @@
55
from __future__ import annotations
66

77
from decimal import Decimal
8-
from math import * # noqa: F403
98

10-
from sympy import diff
9+
from sympy import diff, lambdify, symbols
1110

1211

13-
def newton_raphson(
14-
func: str, a: float | Decimal, precision: float = 10**-10
15-
) -> float:
12+
def newton_raphson(func: str, a: float | Decimal, precision: float = 1e-10) -> float:
1613
"""Finds root from the point 'a' onwards by Newton-Raphson method
1714
>>> newton_raphson("sin(x)", 2)
1815
3.1415926536808043
19-
>>> newton_raphson("x**2 - 5*x +2", 0.4)
16+
>>> newton_raphson("x**2 - 5*x + 2", 0.4)
2017
0.4384471871911695
2118
>>> newton_raphson("x**2 - 5", 0.1)
2219
2.23606797749979
23-
>>> newton_raphson("log(x)- 1", 2)
20+
>>> newton_raphson("log(x) - 1", 2)
2421
2.718281828458938
2522
"""
26-
x = a
23+
x = symbols("x")
24+
f = lambdify(x, func, "math")
25+
f_derivative = lambdify(x, diff(func), "math")
26+
x_curr = a
2727
while True:
28-
x = Decimal(x) - (
29-
Decimal(eval(func)) / Decimal(eval(str(diff(func)))) # noqa: S307
30-
)
31-
# This number dictates the accuracy of the answer
32-
if abs(eval(func)) < precision: # noqa: S307
33-
return float(x)
28+
x_curr = Decimal(x_curr) - Decimal(f(x_curr)) / Decimal(f_derivative(x_curr))
29+
if abs(f(x_curr)) < precision:
30+
return float(x_curr)
3431

3532

36-
# Let's Execute
3733
if __name__ == "__main__":
38-
# Find root of trigonometric function
34+
import doctest
35+
36+
doctest.testmod()
37+
3938
# Find value of pi
4039
print(f"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}")
4140
# Find root of polynomial
4241
print(f"The root of x**2 - 5*x + 2 = 0 is {newton_raphson('x**2 - 5*x + 2', 0.4)}")
43-
# Find Square Root of 5
42+
# Find value of e
4443
print(f"The root of log(x) - 1 = 0 is {newton_raphson('log(x) - 1', 2)}")
45-
# Exponential Roots
44+
# Find root of exponential function
4645
print(f"The root of exp(x) - 1 = 0 is {newton_raphson('exp(x) - 1', 0)}")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
1-
"""
2-
Checks if a system of forces is in static equilibrium.
3-
"""
4-
from __future__ import annotations
5-
6-
from numpy import array, cos, cross, float64, radians, sin
7-
from numpy.typing import NDArray
8-
9-
10-
def polar_force(
11-
magnitude: float, angle: float, radian_mode: bool = False
12-
) -> list[float]:
13-
"""
14-
Resolves force along rectangular components.
15-
(force, angle) => (force_x, force_y)
16-
>>> import math
17-
>>> force = polar_force(10, 45)
18-
>>> math.isclose(force[0], 7.071067811865477)
19-
True
20-
>>> math.isclose(force[1], 7.0710678118654755)
21-
True
22-
>>> force = polar_force(10, 3.14, radian_mode=True)
23-
>>> math.isclose(force[0], -9.999987317275396)
24-
True
25-
>>> math.isclose(force[1], 0.01592652916486828)
26-
True
27-
"""
28-
if radian_mode:
29-
return [magnitude * cos(angle), magnitude * sin(angle)]
30-
return [magnitude * cos(radians(angle)), magnitude * sin(radians(angle))]
31-
32-
33-
def in_static_equilibrium(
34-
forces: NDArray[float64], location: NDArray[float64], eps: float = 10**-1
35-
) -> bool:
36-
"""
37-
Check if a system is in equilibrium.
38-
It takes two numpy.array objects.
39-
forces ==> [
40-
[force1_x, force1_y],
41-
[force2_x, force2_y],
42-
....]
43-
location ==> [
44-
[x1, y1],
45-
[x2, y2],
46-
....]
47-
>>> force = array([[1, 1], [-1, 2]])
48-
>>> location = array([[1, 0], [10, 0]])
49-
>>> in_static_equilibrium(force, location)
50-
False
51-
"""
52-
# summation of moments is zero
53-
moments: NDArray[float64] = cross(location, forces)
54-
sum_moments: float = sum(moments)
55-
return abs(sum_moments) < eps
56-
57-
58-
if __name__ == "__main__":
59-
# Test to check if it works
60-
forces = array(
61-
[
62-
polar_force(718.4, 180 - 30),
63-
polar_force(879.54, 45),
64-
polar_force(100, -90),
65-
]
66-
)
67-
68-
location: NDArray[float64] = array([[0, 0], [0, 0], [0, 0]])
69-
70-
assert in_static_equilibrium(forces, location)
71-
72-
# Problem 1 in image_data/2D_problems.jpg
73-
forces = array(
74-
[
75-
polar_force(30 * 9.81, 15),
76-
polar_force(215, 180 - 45),
77-
polar_force(264, 90 - 30),
78-
]
79-
)
80-
81-
location = array([[0, 0], [0, 0], [0, 0]])
82-
83-
assert in_static_equilibrium(forces, location)
84-
85-
# Problem in image_data/2D_problems_1.jpg
86-
forces = array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]])
87-
88-
location = array([[0, 0], [6, 0], [10, 0], [12, 0]])
89-
90-
assert in_static_equilibrium(forces, location)
91-
92-
import doctest
93-
94-
doctest.testmod()
1+
"""
2+
Checks if a system of forces is in static equilibrium.
3+
"""
4+
from __future__ import annotations
5+
6+
from numpy import array, cos, cross, float64, radians, sin
7+
from numpy.typing import NDArray
8+
9+
10+
def polar_force(
11+
magnitude: float, angle: float, radian_mode: bool = False
12+
) -> list[float]:
13+
"""
14+
Resolves force along rectangular components.
15+
(force, angle) => (force_x, force_y)
16+
>>> import math
17+
>>> force = polar_force(10, 45)
18+
>>> math.isclose(force[0], 7.071067811865477)
19+
True
20+
>>> math.isclose(force[1], 7.0710678118654755)
21+
True
22+
>>> force = polar_force(10, 3.14, radian_mode=True)
23+
>>> math.isclose(force[0], -9.999987317275396)
24+
True
25+
>>> math.isclose(force[1], 0.01592652916486828)
26+
True
27+
"""
28+
if radian_mode:
29+
return [magnitude * cos(angle), magnitude * sin(angle)]
30+
return [magnitude * cos(radians(angle)), magnitude * sin(radians(angle))]
31+
32+
33+
def in_static_equilibrium(
34+
forces: NDArray[float64], location: NDArray[float64], eps: float = 10**-1
35+
) -> bool:
36+
"""
37+
Check if a system is in equilibrium.
38+
It takes two numpy.array objects.
39+
forces ==> [
40+
[force1_x, force1_y],
41+
[force2_x, force2_y],
42+
....]
43+
location ==> [
44+
[x1, y1],
45+
[x2, y2],
46+
....]
47+
>>> force = array([[1, 1], [-1, 2]])
48+
>>> location = array([[1, 0], [10, 0]])
49+
>>> in_static_equilibrium(force, location)
50+
False
51+
"""
52+
# summation of moments is zero
53+
moments: NDArray[float64] = cross(location, forces)
54+
sum_moments: float = sum(moments)
55+
return abs(sum_moments) < eps
56+
57+
58+
if __name__ == "__main__":
59+
# Test to check if it works
60+
forces = array(
61+
[
62+
polar_force(718.4, 180 - 30),
63+
polar_force(879.54, 45),
64+
polar_force(100, -90),
65+
]
66+
)
67+
68+
location: NDArray[float64] = array([[0, 0], [0, 0], [0, 0]])
69+
70+
assert in_static_equilibrium(forces, location)
71+
72+
# Problem 1 in image_data/2D_problems.jpg
73+
forces = array(
74+
[
75+
polar_force(30 * 9.81, 15),
76+
polar_force(215, 180 - 45),
77+
polar_force(264, 90 - 30),
78+
]
79+
)
80+
81+
location = array([[0, 0], [0, 0], [0, 0]])
82+
83+
assert in_static_equilibrium(forces, location)
84+
85+
# Problem in image_data/2D_problems_1.jpg
86+
forces = array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]])
87+
88+
location = array([[0, 0], [6, 0], [10, 0], [12, 0]])
89+
90+
assert in_static_equilibrium(forces, location)
91+
92+
import doctest
93+
94+
doctest.testmod()

0 commit comments

Comments
 (0)
Please sign in to comment.