Skip to content

Commit 2822416

Browse files
Update weierstrass_method.py
* add more tests
1 parent 3c720e5 commit 2822416

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

maths/numerical_analysis/weierstrass_method.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ def weierstrass_method(
3232
3333
Example:
3434
>>> import numpy as np
35-
>>> def f(x): return x**2 - 1
36-
>>> roots = weierstrass_method(f, 2)
37-
>>> np.allclose(np.sort(roots), np.sort(np.array([1, -1])))
35+
>>> def check(poly, degree, expected):
36+
... roots = weierstrass_method(poly, degree)
37+
... return np.allclose(np.sort(roots), np.sort(expected))
38+
39+
>>> check(lambda x: x**2 - 1, 2, np.array([-1, 1]))
40+
True
41+
42+
>>> check(lambda x: x**3 - 4.5*x**2 + 5.75*x - 1.875, 3, np.array([1.5, 0.5, 2.5]))
3843
True
3944
4045
See Also:
@@ -82,4 +87,4 @@ def weierstrass_method(
8287
if __name__ == "__main__":
8388
import doctest
8489

85-
doctest.testmod()
90+
doctest.testmod()

0 commit comments

Comments
 (0)