Skip to content

Commit 45433da

Browse files
authored
Fix mypy errors for arithmetic analysis algorithms (TheAlgorithms#4053)
1 parent 7894fee commit 45433da

File tree

5 files changed

+90
-60
lines changed

5 files changed

+90
-60
lines changed

Diff for: arithmetic_analysis/in_static_equilibrium.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
"""
22
Checks if a system of forces is in static equilibrium.
3-
4-
python/black : true
5-
flake8 : passed
6-
mypy : passed
73
"""
4+
from typing import List
85

9-
from __future__ import annotations
10-
11-
from numpy import array, cos, cross, radians, sin # type: ignore
6+
from numpy import array, cos, cross, radians, sin
127

138

149
def polar_force(
1510
magnitude: float, angle: float, radian_mode: bool = False
16-
) -> list[float]:
11+
) -> List[float]:
1712
"""
1813
Resolves force along rectangular components.
1914
(force, angle) => (force_x, force_y)

Diff for: arithmetic_analysis/lu_decomposition.py

+50-20
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,64 @@
1-
"""Lower-Upper (LU) Decomposition."""
1+
"""Lower-Upper (LU) Decomposition.
22
3-
# lower–upper (LU) decomposition - https://en.wikipedia.org/wiki/LU_decomposition
4-
import numpy
3+
Reference:
4+
- https://en.wikipedia.org/wiki/LU_decomposition
5+
"""
6+
from typing import Tuple
57

8+
import numpy as np
9+
from numpy import ndarray
610

7-
def LUDecompose(table):
11+
12+
def lower_upper_decomposition(table: ndarray) -> Tuple[ndarray, ndarray]:
13+
"""Lower-Upper (LU) Decomposition
14+
15+
Example:
16+
17+
>>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
18+
>>> outcome = lower_upper_decomposition(matrix)
19+
>>> outcome[0]
20+
array([[1. , 0. , 0. ],
21+
[0. , 1. , 0. ],
22+
[2.5, 8. , 1. ]])
23+
>>> outcome[1]
24+
array([[ 2. , -2. , 1. ],
25+
[ 0. , 1. , 2. ],
26+
[ 0. , 0. , -17.5]])
27+
28+
>>> matrix = np.array([[2, -2, 1], [0, 1, 2]])
29+
>>> lower_upper_decomposition(matrix)
30+
Traceback (most recent call last):
31+
...
32+
ValueError: 'table' has to be of square shaped array but got a 2x3 array:
33+
[[ 2 -2 1]
34+
[ 0 1 2]]
35+
"""
836
# Table that contains our data
937
# Table has to be a square array so we need to check first
10-
rows, columns = numpy.shape(table)
11-
L = numpy.zeros((rows, columns))
12-
U = numpy.zeros((rows, columns))
38+
rows, columns = np.shape(table)
1339
if rows != columns:
14-
return []
40+
raise ValueError(
41+
f"'table' has to be of square shaped array but got a {rows}x{columns} "
42+
+ f"array:\n{table}"
43+
)
44+
lower = np.zeros((rows, columns))
45+
upper = np.zeros((rows, columns))
1546
for i in range(columns):
1647
for j in range(i):
17-
sum = 0
48+
total = 0
1849
for k in range(j):
19-
sum += L[i][k] * U[k][j]
20-
L[i][j] = (table[i][j] - sum) / U[j][j]
21-
L[i][i] = 1
50+
total += lower[i][k] * upper[k][j]
51+
lower[i][j] = (table[i][j] - total) / upper[j][j]
52+
lower[i][i] = 1
2253
for j in range(i, columns):
23-
sum1 = 0
54+
total = 0
2455
for k in range(i):
25-
sum1 += L[i][k] * U[k][j]
26-
U[i][j] = table[i][j] - sum1
27-
return L, U
56+
total += lower[i][k] * upper[k][j]
57+
upper[i][j] = table[i][j] - total
58+
return lower, upper
2859

2960

3061
if __name__ == "__main__":
31-
matrix = numpy.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
32-
L, U = LUDecompose(matrix)
33-
print(L)
34-
print(U)
62+
import doctest
63+
64+
doctest.testmod()

Diff for: arithmetic_analysis/newton_forward_interpolation.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# https://www.geeksforgeeks.org/newton-forward-backward-interpolation/
22

33
import math
4+
from typing import List
45

56

67
# for calculating u value
7-
def ucal(u, p):
8+
def ucal(u: float, p: int) -> float:
89
"""
910
>>> ucal(1, 2)
1011
0
@@ -19,9 +20,9 @@ def ucal(u, p):
1920
return temp
2021

2122

22-
def main():
23+
def main() -> None:
2324
n = int(input("enter the numbers of values: "))
24-
y = []
25+
y: List[List[float]] = []
2526
for i in range(n):
2627
y.append([])
2728
for i in range(n):

Diff for: arithmetic_analysis/newton_raphson.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
# quickly find a good approximation for the root of a real-valued function
55
from decimal import Decimal
66
from math import * # noqa: F401, F403
7+
from typing import Union
78

89
from sympy import diff
910

1011

11-
def newton_raphson(func: str, a: int, precision: int = 10 ** -10) -> float:
12+
def newton_raphson(
13+
func: str, a: Union[float, Decimal], precision: float = 10 ** -10
14+
) -> float:
1215
"""Finds root from the point 'a' onwards by Newton-Raphson method
1316
>>> newton_raphson("sin(x)", 2)
1417
3.1415926536808043

Diff for: arithmetic_analysis/secant_method.py

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
# Implementing Secant method in Python
2-
# Author: dimgrichr
3-
4-
5-
from math import exp
6-
7-
8-
def f(x):
9-
"""
10-
>>> f(5)
11-
39.98652410600183
12-
"""
13-
return 8 * x - 2 * exp(-x)
14-
15-
16-
def SecantMethod(lower_bound, upper_bound, repeats):
17-
"""
18-
>>> SecantMethod(1, 3, 2)
19-
0.2139409276214589
20-
"""
21-
x0 = lower_bound
22-
x1 = upper_bound
23-
for i in range(0, repeats):
24-
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
25-
return x1
26-
27-
28-
print(f"The solution is: {SecantMethod(1, 3, 2)}")
1+
"""
2+
Implementing Secant method in Python
3+
Author: dimgrichr
4+
"""
5+
from math import exp
6+
7+
8+
def f(x: float) -> float:
9+
"""
10+
>>> f(5)
11+
39.98652410600183
12+
"""
13+
return 8 * x - 2 * exp(-x)
14+
15+
16+
def secant_method(lower_bound: float, upper_bound: float, repeats: int) -> float:
17+
"""
18+
>>> secant_method(1, 3, 2)
19+
0.2139409276214589
20+
"""
21+
x0 = lower_bound
22+
x1 = upper_bound
23+
for i in range(0, repeats):
24+
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
25+
return x1
26+
27+
28+
if __name__ == "__main__":
29+
print(f"Example: {secant_method(1, 3, 2) = }")

0 commit comments

Comments
 (0)