Add pep8-naming to pre-commit hooks and fixes incorrect naming conventions (#7062)

* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038)

* refactor: Fix naming conventions (#7038)

* Update arithmetic_analysis/lu_decomposition.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038)

* chore: Fix naming conventions in doctests (#7038)

* fix: Temporarily disable project euler problem 104 (#7069)

* chore: Fix naming conventions in doctests (#7038)

Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Caeden
2022-10-12 23:54:20 +01:00
committed by GitHub
parent e2cd982b11
commit 07e991d553
140 changed files with 1552 additions and 1536 deletions

View File

@ -63,8 +63,8 @@ def power_iteration(
vector = w / np.linalg.norm(w)
# Find rayleigh quotient
# (faster than usual b/c we know vector is normalized already)
vectorH = vector.conj().T if is_complex else vector.T
lambda_ = np.dot(vectorH, np.dot(input_matrix, vector))
vector_h = vector.conj().T if is_complex else vector.T
lambda_ = np.dot(vector_h, np.dot(input_matrix, vector))
# Check convergence.
error = np.abs(lambda_ - lambda_previous) / lambda_

View File

@ -26,7 +26,7 @@ def is_hermitian(matrix: np.ndarray) -> bool:
return np.array_equal(matrix, matrix.conjugate().T)
def rayleigh_quotient(A: np.ndarray, v: np.ndarray) -> Any:
def rayleigh_quotient(a: np.ndarray, v: np.ndarray) -> Any:
"""
Returns the Rayleigh quotient of a Hermitian matrix A and
vector v.
@ -45,20 +45,20 @@ def rayleigh_quotient(A: np.ndarray, v: np.ndarray) -> Any:
array([[3.]])
"""
v_star = v.conjugate().T
v_star_dot = v_star.dot(A)
v_star_dot = v_star.dot(a)
assert isinstance(v_star_dot, np.ndarray)
return (v_star_dot.dot(v)) / (v_star.dot(v))
def tests() -> None:
A = np.array([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]])
a = np.array([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]])
v = np.array([[1], [2], [3]])
assert is_hermitian(A), f"{A} is not hermitian."
print(rayleigh_quotient(A, v))
assert is_hermitian(a), f"{a} is not hermitian."
print(rayleigh_quotient(a, v))
A = np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]])
assert is_hermitian(A), f"{A} is not hermitian."
assert rayleigh_quotient(A, v) == float(3)
a = np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]])
assert is_hermitian(a), f"{a} is not hermitian."
assert rayleigh_quotient(a, v) == float(3)
if __name__ == "__main__":

View File

@ -85,13 +85,13 @@ class Test(unittest.TestCase):
self.assertEqual(str(x * 3.0), "(3.0,6.0,9.0)")
self.assertEqual((a * b), 0)
def test_zeroVector(self) -> None:
def test_zero_vector(self) -> None:
"""
test for global function zero_vector()
"""
self.assertTrue(str(zero_vector(10)).count("0") == 10)
def test_unitBasisVector(self) -> None:
def test_unit_basis_vector(self) -> None:
"""
test for global function unit_basis_vector()
"""
@ -113,7 +113,7 @@ class Test(unittest.TestCase):
y = x.copy()
self.assertEqual(str(x), str(y))
def test_changeComponent(self) -> None:
def test_change_component(self) -> None:
"""
test for method change_component()
"""
@ -126,77 +126,77 @@ class Test(unittest.TestCase):
"""
test for Matrix method str()
"""
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(A))
a = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(a))
def test_minor(self) -> None:
"""
test for Matrix method minor()
"""
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
a = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
minors = [[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
for x in range(A.height()):
for y in range(A.width()):
self.assertEqual(minors[x][y], A.minor(x, y))
for x in range(a.height()):
for y in range(a.width()):
self.assertEqual(minors[x][y], a.minor(x, y))
def test_cofactor(self) -> None:
"""
test for Matrix method cofactor()
"""
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
a = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
cofactors = [[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
for x in range(A.height()):
for y in range(A.width()):
self.assertEqual(cofactors[x][y], A.cofactor(x, y))
for x in range(a.height()):
for y in range(a.width()):
self.assertEqual(cofactors[x][y], a.cofactor(x, y))
def test_determinant(self) -> None:
"""
test for Matrix method determinant()
"""
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
self.assertEqual(-5, A.determinant())
a = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
self.assertEqual(-5, a.determinant())
def test__mul__matrix(self) -> None:
"""
test for Matrix * operator
"""
A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3)
a = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3)
x = Vector([1, 2, 3])
self.assertEqual("(14,32,50)", str(A * x))
self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n", str(A * 2))
self.assertEqual("(14,32,50)", str(a * x))
self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n", str(a * 2))
def test_change_component_matrix(self) -> None:
"""
test for Matrix method change_component()
"""
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
A.change_component(0, 2, 5)
self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n", str(A))
a = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
a.change_component(0, 2, 5)
self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n", str(a))
def test_component_matrix(self) -> None:
"""
test for Matrix method component()
"""
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
self.assertEqual(7, A.component(2, 1), 0.01)
a = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
self.assertEqual(7, a.component(2, 1), 0.01)
def test__add__matrix(self) -> None:
"""
test for Matrix + operator
"""
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
B = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3)
self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n", str(A + B))
a = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
b = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3)
self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n", str(a + b))
def test__sub__matrix(self) -> None:
"""
test for Matrix - operator
"""
A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
B = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3)
self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n", str(A - B))
a = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3)
b = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3)
self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n", str(a - b))
def test_squareZeroMatrix(self) -> None:
def test_square_zero_matrix(self) -> None:
"""
test for global function square_zero_matrix()
"""