cleanup
This commit is contained in:
1 parent
cb9d7236f1
commit
b129a9f1a1
1 file changed
+29
-13
+29
-13
@@ -20,21 +20,41 @@ def real(A: npt.NDArray, b: npt.NDArray) -> npt.NDArray:
|
||||
# im actually forgetting yall linear algebra is ass
|
||||
|
||||
|
||||
def specific1(A: npt.NDArray, b: npt.NDArray, start=None) -> npt.NDArray:
|
||||
def gauss_seidel(
|
||||
A: npt.NDArray, b: npt.NDArray, start=None, iterations=24
|
||||
) -> npt.NDArray:
|
||||
if not (len(A.shape) == 2 and len(b.shape) == 1 and A.shape[0] == b.shape[0]):
|
||||
raise ValueError("Invalid shapes for A and b")
|
||||
if start is None:
|
||||
start = np.ones_like(b, dtype=np.float64) # insane
|
||||
start = np.zeros_like(b, dtype=np.float64)
|
||||
if len(start) != len(b):
|
||||
raise ValueError("Start vector has incorrect length")
|
||||
|
||||
for _ in range(24):
|
||||
for _ in range(iterations):
|
||||
for i in range(start.size):
|
||||
restx = np.concatenate([start[:i], start[i + 1 :]])
|
||||
resta = np.concatenate([A[i, :i], A[i, i + 1 :]])
|
||||
buh = np.sum(restx * resta)
|
||||
start[i] = (b[i] - buh) / A[i, i]
|
||||
# print(restx, resta, buh, start, b[i], A[i, i])
|
||||
return start
|
||||
|
||||
|
||||
def jacobi(A: npt.NDArray, b: npt.NDArray, start=None, iterations=24) -> npt.NDArray:
|
||||
if not (len(A.shape) == 2 and len(b.shape) == 1 and A.shape[0] == b.shape[0]):
|
||||
raise ValueError("Invalid shapes for A and b")
|
||||
if start is None:
|
||||
start = np.zeros_like(b, dtype=np.float64)
|
||||
if len(start) != len(b):
|
||||
raise ValueError("Start vector has incorrect length")
|
||||
|
||||
new_start = np.copy(start)
|
||||
for _ in range(iterations):
|
||||
for i in range(start.size):
|
||||
restx = np.concatenate([start[:i], start[i + 1 :]])
|
||||
resta = np.concatenate([A[i, :i], A[i, i + 1 :]])
|
||||
buh = np.sum(restx * resta)
|
||||
new_start[i] = (b[i] - buh) / A[i, i]
|
||||
start = np.copy(new_start)
|
||||
return start
|
||||
|
||||
|
||||
@@ -49,19 +69,15 @@ aa, bb = (
|
||||
),
|
||||
np.array([6, 25, -11, 15]),
|
||||
)
|
||||
import math
|
||||
import itertools
|
||||
|
||||
def isclda(*w):
|
||||
for i in itertools.zip_longest(*w):
|
||||
for a, b in itertools.pairwise(i):
|
||||
if a is None or b is None:
|
||||
raise ValueError("what the hell?")
|
||||
yield math.isclose(a,b)
|
||||
return (np.allclose(a, b) for a, b in itertools.pairwise(w))
|
||||
|
||||
|
||||
r1 = specific1(aa, bb)
|
||||
r1 = jacobi(aa, bb)
|
||||
r2 = real(aa, bb)
|
||||
r3 = stupid(aa, bb)
|
||||
print(r1, r2, r3)
|
||||
assert all(isclda(r1, r2, r3)) # im cooked
|
||||
r4 = gauss_seidel(aa, bb)
|
||||
print(r1, r2, r3, r4)
|
||||
assert isclda(r1, r2, r3, r4) # it works yo
|
||||
Reference in new issue
Block a user