Skip to content

Commit 15e809f

Browse files
committed
remove redundant code
1 parent 74d9dbd commit 15e809f

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

maths/trapezoidal_rule.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,36 @@
88
99
"""
1010

11-
from collections.abc import Iterator
11+
from collections.abc import Callable, Iterator
1212

1313

14-
def method_1(boundary: list[float], steps: int) -> float:
14+
def method_1(f: Callable[[float], float], boundary: list[float], steps: int) -> float:
1515
"""
1616
"extended trapezoidal rule"
1717
int(f) = dx/2 * (f1 + 2f2 + ... + fn)
1818
19-
>>> def f(x): return x ** 2
20-
>>> abs(method_1([0, 1], 10) - 0.335) < 1e-9
19+
>>> def func(x): return x ** 2
20+
>>> abs(method_1(func, [0, 1], 10) - 0.335) < 1e-9
2121
True
2222
23-
>>> def f(x): return 1
24-
>>> abs(method_1([0, 10], 100) - 333.35) < 1e-9
23+
>>> def func(x): return 1
24+
>>> abs(method_1(func, [0, 10], 100) - 10.0) < 1e-9
2525
True
2626
27-
>>> def f(x): return x
28-
>>> method_1([0, 1], 1)
27+
>>> def func(x): return x
28+
>>> method_1(func, [0, 1], 1)
2929
0.5
3030
31-
>>> def f(x): return x ** 2
32-
>>> method_1([], 10) # Empty boundary list
31+
>>> method_1(func, [], 10) # Empty boundary list
3332
Traceback (most recent call last):
3433
...
3534
IndexError: list index out of range
3635
37-
>>> method_1([0, 1], 0) # Steps as zero
36+
>>> method_1(func, [0, 1], 0) # Steps as zero
3837
Traceback (most recent call last):
3938
...
4039
ZeroDivisionError: division by zero
41-
>>> method_1(['0', '1'], 10) # Boundary values as strings
40+
>>> method_1(func, ['0', '1'], 10) # Boundary values as strings
4241
Traceback (most recent call last):
4342
...
4443
TypeError: unsupported operand type(s) for -: 'str' and 'str'
@@ -47,6 +46,7 @@ def method_1(boundary: list[float], steps: int) -> float:
4746
- boundary (list of float): A two-element list specifying the lower and upper bounds
4847
of the integration interval.
4948
- steps (int): The number of steps (trapezoids) to divide the interval into.
49+
- f (Callable[[float], float]): The function to be integrated.
5050
5151
Returns:
5252
- float: The estimated value of the integral over the specified interval.
@@ -96,14 +96,6 @@ def make_points(a: float, b: float, h: float) -> Iterator[float]:
9696
x = x + h
9797

9898

99-
def f(x: float) -> float:
100-
"""
101-
Replace this function with any specific function you need to integrate.
102-
"""
103-
y = (x - 0) * (x - 0)
104-
return y
105-
106-
10799
if __name__ == "__main__":
108100
import doctest
109101

0 commit comments

Comments
 (0)