little change before i paste ai code
This commit is contained in:
1 parent
34eab5446c
commit
cb9d7236f1
2 files changed
+57
-5
No files matched your search
+6
-3
@@ -5,13 +5,15 @@ ass
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
import scipy
|
|
||||||
import sympy
|
|
||||||
import numpy.linalg
|
import numpy.linalg
|
||||||
import scipy.linalg
|
|
||||||
import numpy.polynomial
|
import numpy.polynomial
|
||||||
|
|
||||||
|
import scipy
|
||||||
|
import scipy.linalg
|
||||||
import scipy.optimize
|
import scipy.optimize
|
||||||
|
|
||||||
|
import sympy
|
||||||
|
|
||||||
|
|
||||||
scipy.linalg.eig
|
scipy.linalg.eig
|
||||||
scipy.linalg.companion
|
scipy.linalg.companion
|
||||||
@@ -27,6 +29,7 @@ c = sympy.solve(f, x_sym)
|
|||||||
print(p)
|
print(p)
|
||||||
print(c)
|
print(c)
|
||||||
|
|
||||||
|
|
||||||
def system(x_y): # trait Index<usize> for ndarray
|
def system(x_y): # trait Index<usize> for ndarray
|
||||||
x, y = x_y
|
x, y = x_y
|
||||||
u = x**2 + x * y - 10
|
u = x**2 + x * y - 10
|
||||||
|
|||||||
+51
-2
@@ -3,16 +3,65 @@
|
|||||||
i give you the roots back using either gauss-seidel or jacobi"""
|
i give you the roots back using either gauss-seidel or jacobi"""
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import numpy.typing as npt
|
||||||
|
|
||||||
|
|
||||||
def stupid(A: np.typing.NDArray, b: np.typing.NDArray) -> np.typing.NDArray:
|
def stupid(A: npt.NDArray, b: npt.NDArray) -> npt.NDArray:
|
||||||
"they say that in the slides the second method because operator\\ doesnt exist in python"
|
"they say that in the slides the second method because operator\\ doesnt exist in python"
|
||||||
return np.linalg.matmul(np.linalg.inv(A), b) # unbelievable
|
return np.linalg.matmul(np.linalg.inv(A), b) # unbelievable
|
||||||
# how tf did matlab implement operator\
|
# how tf did matlab implement operator\
|
||||||
|
|
||||||
|
|
||||||
def real(A: np.typing.NDArray, b: np.typing.NDArray) -> np.typing.NDArray:
|
def real(A: npt.NDArray, b: npt.NDArray) -> npt.NDArray:
|
||||||
|
"real optimized better"
|
||||||
return np.linalg.solve(A, b) # this uses the lu
|
return np.linalg.solve(A, b) # this uses the lu
|
||||||
|
|
||||||
|
|
||||||
# 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:
|
||||||
|
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
|
||||||
|
if len(start) != len(b):
|
||||||
|
raise ValueError("Start vector has incorrect length")
|
||||||
|
|
||||||
|
for _ in range(24):
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
aa, bb = (
|
||||||
|
np.array(
|
||||||
|
[
|
||||||
|
[10, -1, 2, 0],
|
||||||
|
[-1, 11, -1, 3],
|
||||||
|
[2, -1, 10, -1],
|
||||||
|
[0, 3, -1, 8],
|
||||||
|
]
|
||||||
|
),
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
r1 = specific1(aa, bb)
|
||||||
|
r2 = real(aa, bb)
|
||||||
|
r3 = stupid(aa, bb)
|
||||||
|
print(r1, r2, r3)
|
||||||
|
assert all(isclda(r1, r2, r3)) # im cooked
|
||||||
Reference in new issue
Block a user