Skip to content

fixed mypy annotations for arithmetic_analysis #6040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions arithmetic_analysis/gaussian_elimination.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@


import numpy as np
from numpy import float64
from numpy.typing import NDArray


def retroactive_resolution(coefficients: np.matrix, vector: np.ndarray) -> np.ndarray:
def retroactive_resolution(
coefficients: NDArray[float64], vector: NDArray[float64]
) -> NDArray[float64]:
"""
This function performs a retroactive linear system resolution
for triangular matrix
Expand All @@ -27,7 +31,7 @@ def retroactive_resolution(coefficients: np.matrix, vector: np.ndarray) -> np.nd

rows, columns = np.shape(coefficients)

x = np.zeros((rows, 1), dtype=float)
x: NDArray[float64] = np.zeros((rows, 1), dtype=float)
for row in reversed(range(rows)):
sum = 0
for col in range(row + 1, columns):
Expand All @@ -38,7 +42,9 @@ def retroactive_resolution(coefficients: np.matrix, vector: np.ndarray) -> np.nd
return x


def gaussian_elimination(coefficients: np.matrix, vector: np.ndarray) -> np.ndarray:
def gaussian_elimination(
coefficients: NDArray[float64], vector: NDArray[float64]
) -> NDArray[float64]:
"""
This function performs Gaussian elimination method

Expand All @@ -60,7 +66,7 @@ def gaussian_elimination(coefficients: np.matrix, vector: np.ndarray) -> np.ndar
return np.array((), dtype=float)

# augmented matrix
augmented_mat = np.concatenate((coefficients, vector), axis=1)
augmented_mat: NDArray[float64] = np.concatenate((coefficients, vector), axis=1)
augmented_mat = augmented_mat.astype("float64")

# scale the matrix leaving it triangular
Expand Down
9 changes: 5 additions & 4 deletions arithmetic_analysis/in_static_equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"""
from __future__ import annotations

from numpy import array, cos, cross, ndarray, radians, sin
from numpy import array, cos, cross, float64, radians, sin
from numpy.typing import NDArray


def polar_force(
Expand All @@ -27,7 +28,7 @@ def polar_force(


def in_static_equilibrium(
forces: ndarray, location: ndarray, eps: float = 10**-1
forces: NDArray[float64], location: NDArray[float64], eps: float = 10**-1
) -> bool:
"""
Check if a system is in equilibrium.
Expand All @@ -46,7 +47,7 @@ def in_static_equilibrium(
False
"""
# summation of moments is zero
moments: ndarray = cross(location, forces)
moments: NDArray[float64] = cross(location, forces)
sum_moments: float = sum(moments)
return abs(sum_moments) < eps

Expand All @@ -61,7 +62,7 @@ def in_static_equilibrium(
]
)

location = array([[0, 0], [0, 0], [0, 0]])
location: NDArray[float64] = array([[0, 0], [0, 0], [0, 0]])

assert in_static_equilibrium(forces, location)

Expand Down
14 changes: 9 additions & 5 deletions arithmetic_analysis/jacobi_iteration_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from __future__ import annotations

import numpy as np
from numpy import float64
from numpy.typing import NDArray


# Method to find solution of system of linear equations
def jacobi_iteration_method(
coefficient_matrix: np.ndarray,
constant_matrix: np.ndarray,
init_val: list,
coefficient_matrix: NDArray[float64],
constant_matrix: NDArray[float64],
init_val: list[int],
iterations: int,
) -> list[float]:
"""
Expand Down Expand Up @@ -99,7 +101,9 @@ def jacobi_iteration_method(
if iterations <= 0:
raise ValueError("Iterations must be at least 1")

table = np.concatenate((coefficient_matrix, constant_matrix), axis=1)
table: NDArray[float64] = np.concatenate(
(coefficient_matrix, constant_matrix), axis=1
)

rows, cols = table.shape

Expand All @@ -125,7 +129,7 @@ def jacobi_iteration_method(


# Checks if the given matrix is strictly diagonally dominant
def strictly_diagonally_dominant(table: np.ndarray) -> bool:
def strictly_diagonally_dominant(table: NDArray[float64]) -> bool:
"""
>>> table = np.array([[4, 1, 1, 2], [1, 5, 2, -6], [1, 2, 4, -4]])
>>> strictly_diagonally_dominant(table)
Expand Down
6 changes: 5 additions & 1 deletion arithmetic_analysis/lu_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
from __future__ import annotations

import numpy as np
import numpy.typing as NDArray
from numpy import float64


def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
def lower_upper_decomposition(
table: NDArray[float64],
) -> tuple[NDArray[float64], NDArray[float64]]:
"""Lower-Upper (LU) Decomposition

Example:
Expand Down