Skip to content

Commit a439902

Browse files
chore: improve comments and add tests to trapezoidal rule (#11640)
* chore: improve comments and add tests to trapezoidal rule * fix: too much characters in line * Update maths/trapezoidal_rule.py Co-authored-by: Tianyi Zheng <[email protected]> * Update maths/trapezoidal_rule.py Co-authored-by: Tianyi Zheng <[email protected]> * Update maths/trapezoidal_rule.py Co-authored-by: Tianyi Zheng <[email protected]> * Update maths/trapezoidal_rule.py Co-authored-by: Tianyi Zheng <[email protected]> * fix: change function name in calls * modify tests, changes numbers to remove coma * updating DIRECTORY.md * Fix doctest whitespace * Try to fix line length in doctest --------- Co-authored-by: Tianyi Zheng <[email protected]> Co-authored-by: tianyizheng02 <[email protected]>
1 parent 8439fa8 commit a439902

File tree

2 files changed

+58
-42
lines changed

2 files changed

+58
-42
lines changed

Diff for: DIRECTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
* [Haralick Descriptors](computer_vision/haralick_descriptors.py)
143143
* [Harris Corner](computer_vision/harris_corner.py)
144144
* [Horn Schunck](computer_vision/horn_schunck.py)
145+
* [Intensity Based Segmentation](computer_vision/intensity_based_segmentation.py)
145146
* [Mean Threshold](computer_vision/mean_threshold.py)
146147
* [Mosaic Augmentation](computer_vision/mosaic_augmentation.py)
147148
* [Pooling Functions](computer_vision/pooling_functions.py)
@@ -507,6 +508,7 @@
507508
* [Kahns Algorithm Long](graphs/kahns_algorithm_long.py)
508509
* [Kahns Algorithm Topo](graphs/kahns_algorithm_topo.py)
509510
* [Karger](graphs/karger.py)
511+
* [Lanczos Eigenvectors](graphs/lanczos_eigenvectors.py)
510512
* [Markov Chain](graphs/markov_chain.py)
511513
* [Matching Min Vertex Cover](graphs/matching_min_vertex_cover.py)
512514
* [Minimum Path Sum](graphs/minimum_path_sum.py)
@@ -886,6 +888,7 @@
886888
* [N Body Simulation](physics/n_body_simulation.py)
887889
* [Newtons Law Of Gravitation](physics/newtons_law_of_gravitation.py)
888890
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)
891+
* [Period Of Pendulum](physics/period_of_pendulum.py)
889892
* [Photoelectric Effect](physics/photoelectric_effect.py)
890893
* [Potential Energy](physics/potential_energy.py)
891894
* [Rainfall Intensity](physics/rainfall_intensity.py)

Diff for: maths/trapezoidal_rule.py

+55-42
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
"""
22
Numerical integration or quadrature for a smooth function f with known values at x_i
3-
4-
This method is the classical approach of suming 'Equally Spaced Abscissas'
5-
6-
method 1:
7-
"extended trapezoidal rule"
8-
int(f) = dx/2 * (f1 + 2f2 + ... + fn)
9-
103
"""
114

125

13-
def method_1(boundary, steps):
6+
def trapezoidal_rule(boundary, steps):
147
"""
15-
Apply the extended trapezoidal rule to approximate the integral of function f(x)
16-
over the interval defined by 'boundary' with the number of 'steps'.
17-
18-
Args:
19-
boundary (list of floats): A list containing the start and end values [a, b].
20-
steps (int): The number of steps or subintervals.
21-
Returns:
22-
float: Approximation of the integral of f(x) over [a, b].
23-
Examples:
24-
>>> method_1([0, 1], 10)
25-
0.3349999999999999
8+
Implements the extended trapezoidal rule for numerical integration.
9+
The function f(x) is provided below.
10+
11+
:param boundary: List containing the lower and upper bounds of integration [a, b]
12+
:param steps: The number of steps (intervals) used in the approximation
13+
:return: The numerical approximation of the integral
14+
15+
>>> abs(trapezoidal_rule([0, 1], 10) - 0.33333) < 0.01
16+
True
17+
>>> abs(trapezoidal_rule([0, 1], 100) - 0.33333) < 0.01
18+
True
19+
>>> abs(trapezoidal_rule([0, 2], 1000) - 2.66667) < 0.01
20+
True
21+
>>> abs(trapezoidal_rule([1, 2], 1000) - 2.33333) < 0.01
22+
True
2623
"""
2724
h = (boundary[1] - boundary[0]) / steps
2825
a = boundary[0]
@@ -31,57 +28,73 @@ def method_1(boundary, steps):
3128
y = 0.0
3229
y += (h / 2.0) * f(a)
3330
for i in x_i:
34-
# print(i)
3531
y += h * f(i)
3632
y += (h / 2.0) * f(b)
3733
return y
3834

3935

4036
def make_points(a, b, h):
4137
"""
42-
Generates points between 'a' and 'b' with step size 'h', excluding the end points.
43-
Args:
44-
a (float): Start value
45-
b (float): End value
46-
h (float): Step size
47-
Examples:
38+
Generates points between a and b with step size h for trapezoidal integration.
39+
40+
:param a: The lower bound of integration
41+
:param b: The upper bound of integration
42+
:param h: The step size
43+
:yield: The next x-value in the range (a, b)
44+
45+
>>> list(make_points(0, 1, 0.1)) # doctest: +NORMALIZE_WHITESPACE
46+
[0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, \
47+
0.8999999999999999]
4848
>>> list(make_points(0, 10, 2.5))
4949
[2.5, 5.0, 7.5]
50-
5150
>>> list(make_points(0, 10, 2))
5251
[2, 4, 6, 8]
53-
5452
>>> list(make_points(1, 21, 5))
5553
[6, 11, 16]
56-
5754
>>> list(make_points(1, 5, 2))
5855
[3]
59-
6056
>>> list(make_points(1, 4, 3))
6157
[]
6258
"""
6359
x = a + h
6460
while x <= (b - h):
6561
yield x
66-
x = x + h
62+
x += h
6763

6864

69-
def f(x): # enter your function here
65+
def f(x):
7066
"""
71-
Example:
72-
>>> f(2)
73-
4
67+
This is the function to integrate, f(x) = (x - 0)^2 = x^2.
68+
69+
:param x: The input value
70+
:return: The value of f(x)
71+
72+
>>> f(0)
73+
0
74+
>>> f(1)
75+
1
76+
>>> f(0.5)
77+
0.25
7478
"""
75-
y = (x - 0) * (x - 0)
76-
return y
79+
return x**2
7780

7881

7982
def main():
80-
a = 0.0 # Lower bound of integration
81-
b = 1.0 # Upper bound of integration
82-
steps = 10.0 # define number of steps or resolution
83-
boundary = [a, b] # define boundary of integration
84-
y = method_1(boundary, steps)
83+
"""
84+
Main function to test the trapezoidal rule.
85+
:a: Lower bound of integration
86+
:b: Upper bound of integration
87+
:steps: define number of steps or resolution
88+
:boundary: define boundary of integration
89+
90+
>>> main()
91+
y = 0.3349999999999999
92+
"""
93+
a = 0.0
94+
b = 1.0
95+
steps = 10.0
96+
boundary = [a, b]
97+
y = trapezoidal_rule(boundary, steps)
8598
print(f"y = {y}")
8699

87100

0 commit comments

Comments
 (0)