mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
Delete arithmetic_analysis/
directory and relocate its contents (#10824)
* Remove eval from arithmetic_analysis/newton_raphson.py * Relocate contents of arithmetic_analysis/ Delete the arithmetic_analysis/ directory and relocate its files because the purpose of the directory was always ill-defined. "Arithmetic analysis" isn't a field of math, and the directory's files contained algorithms for linear algebra, numerical analysis, and physics. Relocated the directory's linear algebra algorithms to linear_algebra/, its numerical analysis algorithms to a new subdirectory called maths/numerical_analysis/, and its single physics algorithm to physics/. * updating DIRECTORY.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
86
linear_algebra/gaussian_elimination.py
Normal file
86
linear_algebra/gaussian_elimination.py
Normal file
@ -0,0 +1,86 @@
|
||||
"""
|
||||
Gaussian elimination method for solving a system of linear equations.
|
||||
Gaussian elimination - https://en.wikipedia.org/wiki/Gaussian_elimination
|
||||
"""
|
||||
|
||||
|
||||
import numpy as np
|
||||
from numpy import float64
|
||||
from numpy.typing import NDArray
|
||||
|
||||
|
||||
def retroactive_resolution(
|
||||
coefficients: NDArray[float64], vector: NDArray[float64]
|
||||
) -> NDArray[float64]:
|
||||
"""
|
||||
This function performs a retroactive linear system resolution
|
||||
for triangular matrix
|
||||
|
||||
Examples:
|
||||
2x1 + 2x2 - 1x3 = 5 2x1 + 2x2 = -1
|
||||
0x1 - 2x2 - 1x3 = -7 0x1 - 2x2 = -1
|
||||
0x1 + 0x2 + 5x3 = 15
|
||||
>>> gaussian_elimination([[2, 2, -1], [0, -2, -1], [0, 0, 5]], [[5], [-7], [15]])
|
||||
array([[2.],
|
||||
[2.],
|
||||
[3.]])
|
||||
>>> gaussian_elimination([[2, 2], [0, -2]], [[-1], [-1]])
|
||||
array([[-1. ],
|
||||
[ 0.5]])
|
||||
"""
|
||||
|
||||
rows, columns = np.shape(coefficients)
|
||||
|
||||
x: NDArray[float64] = np.zeros((rows, 1), dtype=float)
|
||||
for row in reversed(range(rows)):
|
||||
total = np.dot(coefficients[row, row + 1 :], x[row + 1 :])
|
||||
x[row, 0] = (vector[row][0] - total[0]) / coefficients[row, row]
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def gaussian_elimination(
|
||||
coefficients: NDArray[float64], vector: NDArray[float64]
|
||||
) -> NDArray[float64]:
|
||||
"""
|
||||
This function performs Gaussian elimination method
|
||||
|
||||
Examples:
|
||||
1x1 - 4x2 - 2x3 = -2 1x1 + 2x2 = 5
|
||||
5x1 + 2x2 - 2x3 = -3 5x1 + 2x2 = 5
|
||||
1x1 - 1x2 + 0x3 = 4
|
||||
>>> gaussian_elimination([[1, -4, -2], [5, 2, -2], [1, -1, 0]], [[-2], [-3], [4]])
|
||||
array([[ 2.3 ],
|
||||
[-1.7 ],
|
||||
[ 5.55]])
|
||||
>>> gaussian_elimination([[1, 2], [5, 2]], [[5], [5]])
|
||||
array([[0. ],
|
||||
[2.5]])
|
||||
"""
|
||||
# coefficients must to be a square matrix so we need to check first
|
||||
rows, columns = np.shape(coefficients)
|
||||
if rows != columns:
|
||||
return np.array((), dtype=float)
|
||||
|
||||
# augmented matrix
|
||||
augmented_mat: NDArray[float64] = np.concatenate((coefficients, vector), axis=1)
|
||||
augmented_mat = augmented_mat.astype("float64")
|
||||
|
||||
# scale the matrix leaving it triangular
|
||||
for row in range(rows - 1):
|
||||
pivot = augmented_mat[row, row]
|
||||
for col in range(row + 1, columns):
|
||||
factor = augmented_mat[col, row] / pivot
|
||||
augmented_mat[col, :] -= factor * augmented_mat[row, :]
|
||||
|
||||
x = retroactive_resolution(
|
||||
augmented_mat[:, 0:columns], augmented_mat[:, columns : columns + 1]
|
||||
)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
203
linear_algebra/jacobi_iteration_method.py
Normal file
203
linear_algebra/jacobi_iteration_method.py
Normal file
@ -0,0 +1,203 @@
|
||||
"""
|
||||
Jacobi Iteration Method - https://en.wikipedia.org/wiki/Jacobi_method
|
||||
"""
|
||||
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: NDArray[float64],
|
||||
constant_matrix: NDArray[float64],
|
||||
init_val: list[float],
|
||||
iterations: int,
|
||||
) -> list[float]:
|
||||
"""
|
||||
Jacobi Iteration Method:
|
||||
An iterative algorithm to determine the solutions of strictly diagonally dominant
|
||||
system of linear equations
|
||||
|
||||
4x1 + x2 + x3 = 2
|
||||
x1 + 5x2 + 2x3 = -6
|
||||
x1 + 2x2 + 4x3 = -4
|
||||
|
||||
x_init = [0.5, -0.5 , -0.5]
|
||||
|
||||
Examples:
|
||||
|
||||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
|
||||
>>> constant = np.array([[2], [-6], [-4]])
|
||||
>>> init_val = [0.5, -0.5, -0.5]
|
||||
>>> iterations = 3
|
||||
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations)
|
||||
[0.909375, -1.14375, -0.7484375]
|
||||
|
||||
|
||||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2]])
|
||||
>>> constant = np.array([[2], [-6], [-4]])
|
||||
>>> init_val = [0.5, -0.5, -0.5]
|
||||
>>> iterations = 3
|
||||
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Coefficient matrix dimensions must be nxn but received 2x3
|
||||
|
||||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
|
||||
>>> constant = np.array([[2], [-6]])
|
||||
>>> init_val = [0.5, -0.5, -0.5]
|
||||
>>> iterations = 3
|
||||
>>> jacobi_iteration_method(
|
||||
... coefficient, constant, init_val, iterations
|
||||
... ) # doctest: +NORMALIZE_WHITESPACE
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Coefficient and constant matrices dimensions must be nxn and nx1 but
|
||||
received 3x3 and 2x1
|
||||
|
||||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
|
||||
>>> constant = np.array([[2], [-6], [-4]])
|
||||
>>> init_val = [0.5, -0.5]
|
||||
>>> iterations = 3
|
||||
>>> jacobi_iteration_method(
|
||||
... coefficient, constant, init_val, iterations
|
||||
... ) # doctest: +NORMALIZE_WHITESPACE
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Number of initial values must be equal to number of rows in coefficient
|
||||
matrix but received 2 and 3
|
||||
|
||||
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
|
||||
>>> constant = np.array([[2], [-6], [-4]])
|
||||
>>> init_val = [0.5, -0.5, -0.5]
|
||||
>>> iterations = 0
|
||||
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Iterations must be at least 1
|
||||
"""
|
||||
|
||||
rows1, cols1 = coefficient_matrix.shape
|
||||
rows2, cols2 = constant_matrix.shape
|
||||
|
||||
if rows1 != cols1:
|
||||
msg = f"Coefficient matrix dimensions must be nxn but received {rows1}x{cols1}"
|
||||
raise ValueError(msg)
|
||||
|
||||
if cols2 != 1:
|
||||
msg = f"Constant matrix must be nx1 but received {rows2}x{cols2}"
|
||||
raise ValueError(msg)
|
||||
|
||||
if rows1 != rows2:
|
||||
msg = (
|
||||
"Coefficient and constant matrices dimensions must be nxn and nx1 but "
|
||||
f"received {rows1}x{cols1} and {rows2}x{cols2}"
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
if len(init_val) != rows1:
|
||||
msg = (
|
||||
"Number of initial values must be equal to number of rows in coefficient "
|
||||
f"matrix but received {len(init_val)} and {rows1}"
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
if iterations <= 0:
|
||||
raise ValueError("Iterations must be at least 1")
|
||||
|
||||
table: NDArray[float64] = np.concatenate(
|
||||
(coefficient_matrix, constant_matrix), axis=1
|
||||
)
|
||||
|
||||
rows, cols = table.shape
|
||||
|
||||
strictly_diagonally_dominant(table)
|
||||
|
||||
"""
|
||||
# Iterates the whole matrix for given number of times
|
||||
for _ in range(iterations):
|
||||
new_val = []
|
||||
for row in range(rows):
|
||||
temp = 0
|
||||
for col in range(cols):
|
||||
if col == row:
|
||||
denom = table[row][col]
|
||||
elif col == cols - 1:
|
||||
val = table[row][col]
|
||||
else:
|
||||
temp += (-1) * table[row][col] * init_val[col]
|
||||
temp = (temp + val) / denom
|
||||
new_val.append(temp)
|
||||
init_val = new_val
|
||||
"""
|
||||
|
||||
# denominator - a list of values along the diagonal
|
||||
denominator = np.diag(coefficient_matrix)
|
||||
|
||||
# val_last - values of the last column of the table array
|
||||
val_last = table[:, -1]
|
||||
|
||||
# masks - boolean mask of all strings without diagonal
|
||||
# elements array coefficient_matrix
|
||||
masks = ~np.eye(coefficient_matrix.shape[0], dtype=bool)
|
||||
|
||||
# no_diagonals - coefficient_matrix array values without diagonal elements
|
||||
no_diagonals = coefficient_matrix[masks].reshape(-1, rows - 1)
|
||||
|
||||
# Here we get 'i_col' - these are the column numbers, for each row
|
||||
# without diagonal elements, except for the last column.
|
||||
i_row, i_col = np.where(masks)
|
||||
ind = i_col.reshape(-1, rows - 1)
|
||||
|
||||
#'i_col' is converted to a two-dimensional list 'ind', which will be
|
||||
# used to make selections from 'init_val' ('arr' array see below).
|
||||
|
||||
# Iterates the whole matrix for given number of times
|
||||
for _ in range(iterations):
|
||||
arr = np.take(init_val, ind)
|
||||
sum_product_rows = np.sum((-1) * no_diagonals * arr, axis=1)
|
||||
new_val = (sum_product_rows + val_last) / denominator
|
||||
init_val = new_val
|
||||
|
||||
return new_val.tolist()
|
||||
|
||||
|
||||
# Checks if the given matrix is strictly diagonally dominant
|
||||
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)
|
||||
True
|
||||
|
||||
>>> table = np.array([[4, 1, 1, 2], [1, 5, 2, -6], [1, 2, 3, -4]])
|
||||
>>> strictly_diagonally_dominant(table)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Coefficient matrix is not strictly diagonally dominant
|
||||
"""
|
||||
|
||||
rows, cols = table.shape
|
||||
|
||||
is_diagonally_dominant = True
|
||||
|
||||
for i in range(rows):
|
||||
total = 0
|
||||
for j in range(cols - 1):
|
||||
if i == j:
|
||||
continue
|
||||
else:
|
||||
total += table[i][j]
|
||||
|
||||
if table[i][i] <= total:
|
||||
raise ValueError("Coefficient matrix is not strictly diagonally dominant")
|
||||
|
||||
return is_diagonally_dominant
|
||||
|
||||
|
||||
# Test Cases
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
111
linear_algebra/lu_decomposition.py
Normal file
111
linear_algebra/lu_decomposition.py
Normal file
@ -0,0 +1,111 @@
|
||||
"""
|
||||
Lower–upper (LU) decomposition factors a matrix as a product of a lower
|
||||
triangular matrix and an upper triangular matrix. A square matrix has an LU
|
||||
decomposition under the following conditions:
|
||||
- If the matrix is invertible, then it has an LU decomposition if and only
|
||||
if all of its leading principal minors are non-zero (see
|
||||
https://en.wikipedia.org/wiki/Minor_(linear_algebra) for an explanation of
|
||||
leading principal minors of a matrix).
|
||||
- If the matrix is singular (i.e., not invertible) and it has a rank of k
|
||||
(i.e., it has k linearly independent columns), then it has an LU
|
||||
decomposition if its first k leading principal minors are non-zero.
|
||||
|
||||
This algorithm will simply attempt to perform LU decomposition on any square
|
||||
matrix and raise an error if no such decomposition exists.
|
||||
|
||||
Reference: https://en.wikipedia.org/wiki/LU_decomposition
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
||||
"""
|
||||
Perform LU decomposition on a given matrix and raises an error if the matrix
|
||||
isn't square or if no such decomposition exists
|
||||
>>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
|
||||
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||
>>> lower_mat
|
||||
array([[1. , 0. , 0. ],
|
||||
[0. , 1. , 0. ],
|
||||
[2.5, 8. , 1. ]])
|
||||
>>> upper_mat
|
||||
array([[ 2. , -2. , 1. ],
|
||||
[ 0. , 1. , 2. ],
|
||||
[ 0. , 0. , -17.5]])
|
||||
|
||||
>>> matrix = np.array([[4, 3], [6, 3]])
|
||||
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||
>>> lower_mat
|
||||
array([[1. , 0. ],
|
||||
[1.5, 1. ]])
|
||||
>>> upper_mat
|
||||
array([[ 4. , 3. ],
|
||||
[ 0. , -1.5]])
|
||||
|
||||
# Matrix is not square
|
||||
>>> matrix = np.array([[2, -2, 1], [0, 1, 2]])
|
||||
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: 'table' has to be of square shaped array but got a 2x3 array:
|
||||
[[ 2 -2 1]
|
||||
[ 0 1 2]]
|
||||
|
||||
# Matrix is invertible, but its first leading principal minor is 0
|
||||
>>> matrix = np.array([[0, 1], [1, 0]])
|
||||
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ArithmeticError: No LU decomposition exists
|
||||
|
||||
# Matrix is singular, but its first leading principal minor is 1
|
||||
>>> matrix = np.array([[1, 0], [1, 0]])
|
||||
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||
>>> lower_mat
|
||||
array([[1., 0.],
|
||||
[1., 1.]])
|
||||
>>> upper_mat
|
||||
array([[1., 0.],
|
||||
[0., 0.]])
|
||||
|
||||
# Matrix is singular, but its first leading principal minor is 0
|
||||
>>> matrix = np.array([[0, 1], [0, 1]])
|
||||
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ArithmeticError: No LU decomposition exists
|
||||
"""
|
||||
# Ensure that table is a square array
|
||||
rows, columns = np.shape(table)
|
||||
if rows != columns:
|
||||
msg = (
|
||||
"'table' has to be of square shaped array but got a "
|
||||
f"{rows}x{columns} array:\n{table}"
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
lower = np.zeros((rows, columns))
|
||||
upper = np.zeros((rows, columns))
|
||||
|
||||
# in 'total', the necessary data is extracted through slices
|
||||
# and the sum of the products is obtained.
|
||||
|
||||
for i in range(columns):
|
||||
for j in range(i):
|
||||
total = np.sum(lower[i, :i] * upper[:i, j])
|
||||
if upper[j][j] == 0:
|
||||
raise ArithmeticError("No LU decomposition exists")
|
||||
lower[i][j] = (table[i][j] - total) / upper[j][j]
|
||||
lower[i][i] = 1
|
||||
for j in range(i, columns):
|
||||
total = np.sum(lower[i, :i] * upper[:i, j])
|
||||
upper[i][j] = table[i][j] - total
|
||||
return lower, upper
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
Reference in New Issue
Block a user