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
|
# 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]):
|
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")
|
raise ValueError("Invalid shapes for A and b")
|
||||||
if start is None:
|
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):
|
if len(start) != len(b):
|
||||||
raise ValueError("Start vector has incorrect length")
|
raise ValueError("Start vector has incorrect length")
|
||||||
|
|
||||||
for _ in range(24):
|
for _ in range(iterations):
|
||||||
for i in range(start.size):
|
for i in range(start.size):
|
||||||
restx = np.concatenate([start[:i], start[i + 1 :]])
|
restx = np.concatenate([start[:i], start[i + 1 :]])
|
||||||
resta = np.concatenate([A[i, :i], A[i, i + 1 :]])
|
resta = np.concatenate([A[i, :i], A[i, i + 1 :]])
|
||||||
buh = np.sum(restx * resta)
|
buh = np.sum(restx * resta)
|
||||||
start[i] = (b[i] - buh) / A[i, i]
|
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
|
return start
|
||||||
|
|
||||||
|
|
||||||
@@ -49,19 +69,15 @@ aa, bb = (
|
|||||||
),
|
),
|
||||||
np.array([6, 25, -11, 15]),
|
np.array([6, 25, -11, 15]),
|
||||||
)
|
)
|
||||||
import math
|
|
||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
def isclda(*w):
|
def isclda(*w):
|
||||||
for i in itertools.zip_longest(*w):
|
return (np.allclose(a, b) for a, b in itertools.pairwise(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)
|
|
||||||
|
|
||||||
|
|
||||||
r1 = specific1(aa, bb)
|
r1 = jacobi(aa, bb)
|
||||||
r2 = real(aa, bb)
|
r2 = real(aa, bb)
|
||||||
r3 = stupid(aa, bb)
|
r3 = stupid(aa, bb)
|
||||||
print(r1, r2, r3)
|
r4 = gauss_seidel(aa, bb)
|
||||||
assert all(isclda(r1, r2, r3)) # im cooked
|
print(r1, r2, r3, r4)
|
||||||
|
assert isclda(r1, r2, r3, r4) # it works yo
|
||||||
Reference in new issue
Block a user