def solve_ide_implicit_from_kicks(kicks): A = construct_A(kicks) b = np.zeros(len(kicks), dtype=np.complex128) b[0] = 1 a = splinalg.spsolve_triangular( # a = linalg.solve_triangular( A, b, lower=True, overwrite_b=True, # check_finite = False, overwrite_A=True, ) # a, info = splinalg.bicgstab(A, b, x0 = np.ones_like(b)) # print(info) return a, kicks
def solve_qr(A, b): # TODO: return x, R s.t. Ax = b, and |Ax - b|^2 = |Rx - d|^2 + |e|^2 # https://github.com/theNded/PySPQR z, R, E, rank = rz(A, b, permc_spec='NATURAL') x = spsolve_triangular(R, z, lower=False) return x, R
matpath = "..//FD_class/Apy_UW1.npz" save_npz(matpath, A) # # ### --------------- 2nd-order scheme --------------- ### scheme = "UW2" dt = CFL_safety * buw.get_CFL_limit_1d(scheme, hx, cx) dt = 0.25 t = dt * np.arange(0, np.ceil(Tmax / dt) + 1) nt = t.shape[0] print("{}: nt = {}, dt = {:.2e}, nx = {}, hx = {:.2e}, cx = {}\n".format( scheme, nt, dt, nx, hx, cx)) print("{}: assembling...\n".format(scheme)) A, b = buw.get_UW2_1D(nt, dt, nx, hx, cx, u0_wrapper) A = A.tocsr() print("{}: solving...\n".format(scheme)) z = spsolve_triangular(A, b, lower=True) u = z[0::2].copy() #v = z[1::2].copy() plt.plot(x, u[nx * (nt - 1)::], label=scheme) matpath = "..//FD_class/Apy_UW2.npz" save_npz(matpath, A) ### --------------- 4th-order scheme --------------- ### scheme = "UW4" dt = CFL_safety * buw.get_CFL_limit_1d(scheme, hx, cx) dt = 0.25 t = dt * np.arange(0, np.ceil(Tmax / dt) + 1) nt = t.shape[0] print("{}: nt = {}, dt = {:.2e}, nx = {}, hx = {:.2e}, cx = {}\n".format( scheme, nt, dt, nx, hx, cx))
def test_spsolve_triangular(matrices): A_dense, A_sparse, b = matrices A_sparse = sparse.tril(A_sparse) x = splin.spsolve_triangular(A_sparse, b) assert_allclose(A_sparse @ x, b)
def GS_precond(x): return spla.spsolve_triangular(L, x, lower=True)
def get_z(r): PLz = datdot(Dinv, linalg.spsolve_triangular(L, r[P])) return linalg.spsolve_triangular(U, PLz, lower=False)[Pt]
import numpy as np import scipy.sparse.linalg as ssl from scipy.sparse import csr_matrix from ssl import * A = csr_matrix([[3, 0, 0], [1, -1, 0], [2, 0, 1]], dtype=float) B = np.array([[2, 0], [-1, 0], [2, 0]], dtype=float) x = ssl.spsolve_triangular(A, B) #np.allclose(A.dot(x), B)
def recover_inverse(theta): theta2 = theta**2 Y, _ = Xmatrix(theta2) params = Y.dot(theta2) X, _ = Xmatrix(params) return spsolve_triangular(X, theta2, lower=False)
def timing(): algorithm_runtime = [[], [], [], [], [], [], []] matrix_size = [] for m in range(1, 21): n = m * 100 matrix_size.append(n) R, diagnal, factored, factored_d = initalize_matrix(n) b = -1 / (n**4) * np.ones((n, )) dense_R = R.toarray() dense_f = factored.toarray() s0 = time.perf_counter() r0 = sla.solve(dense_R, b) e0 = time.perf_counter() algorithm_runtime[0].append(e0 - s0) ab = np.ones((5, n)) ab[2, :] = diagnal[0] ab[1, 1:] = diagnal[1] ab[3, 0:-1] = diagnal[2] ab[0, 2:] = diagnal[3] ab[4, 0:-2] = diagnal[4] s1 = time.perf_counter() r1 = sla.solve_banded((2, 2), ab, b) e1 = time.perf_counter() algorithm_runtime[1].append(e1 - s1) s2 = time.perf_counter() r2 = spsolve(R, b) e2 = time.perf_counter() algorithm_runtime[2].append(e2 - s2) transpose = np.transpose(dense_f) s3 = time.perf_counter() r3 = solve_triangular(dense_f, b, lower=False) r3 = solve_triangular(transpose, r3, lower=True) e3 = time.perf_counter() algorithm_runtime[3].append(e3 - s3) AB = np.ones((3, n)) AB[2, :] = factored_d[0] AB[1, 1:] = factored_d[1] AB[0, 2:] = factored_d[2] AB_t = np.empty((3, n)) AB_t[0, :] = factored_d[0] AB_t[1, :-1] = factored_d[1] AB_t[2, :-2] = factored_d[2] s4 = time.perf_counter() r4 = solve_banded((0, 2), AB, b) r4 = solve_banded((2, 0), AB_t, r4) e4 = time.perf_counter() algorithm_runtime[4].append(e4 - s4) factored_transpose = sp.csr_matrix(factored.transpose()) s5 = time.perf_counter() r5 = spsolve_triangular(factored, b, lower=False) r5 = spsolve_triangular(factored_transpose, r5, lower=True) e5 = time.perf_counter() algorithm_runtime[5].append(e5 - s5) s6 = time.perf_counter() c, lower = sla.cho_factor(dense_R) r6 = sla.cho_solve((c, lower), b) e6 = time.perf_counter() algorithm_runtime[6].append(e6 - s6) plt.plot(matrix_size, algorithm_runtime[0], label='LU') plt.plot(matrix_size, algorithm_runtime[1], label='Banded') plt.plot(matrix_size, algorithm_runtime[2], label='Sparse LU') plt.plot(matrix_size, algorithm_runtime[3], label='R') plt.plot(matrix_size, algorithm_runtime[4], label='Banded R') plt.plot(matrix_size, algorithm_runtime[5], label='Sparse R') plt.plot(matrix_size, algorithm_runtime[6], label='Chol') plt.xlabel("Matrix size") plt.ylabel("Runtime") plt.title("Performance of different solver") plt.legend() plt.savefig("Q1.png") plt.show()
def applyRight(self, vec): '''Apply the preconditioner from the right.''' # Apply L^{-1} u = spla.spsolve_triangular(self._L, vec, lower=True) # Apply L^{-T} return spla.spsolve_triangular(self._Lt, u, lower=False)
def update(self, current_x, residue): #return current_x + self.__forward_substitution(self.__triangular_P, residue) return current_x + spsolve_triangular(self.__triangular_P, residue)
# + pycharm={"name": "#%%\n"} A * x # + pycharm={"name": "#%%\n"} A @ x # + pycharm={"name": "#%%\n"} diag = sparse.spdiags(x, 0, len(x), len(x)) (A @ diag).toarray() # + [markdown] pycharm={"name": "#%% md\n"} # #### Solve diagonal # + pycharm={"name": "#%%\n"} from scipy.sparse import linalg as splinalg splinalg.spsolve_triangular(diag, x) # + [markdown] pycharm={"name": "#%% md\n"} # #### Solve lower triangular # + pycharm={"name": "#%%\n"} L = sparse.tril(np.random.randint(0, 3, (4, 4)), k=-1) + sparse.eye(4) splinalg.spsolve_triangular(L, x, unit_diagonal=True) # + pycharm={"name": "#%%\n"} #### Solving upper triangular # + pycharm={"name": "#%%\n"} U = sparse.triu(np.random.randint(0, 3, (4, 4)), k=1) + sparse.eye(4) splinalg.spsolve_triangular(U, x, lower=False, unit_diagonal=True)
def preconditionedConjugateGradient(A, b, R, tolerance): Rinv = sparse.linalg.inv(R.tocsc()) y,norms = conjugateGradient(Rinv.conj().T@A@Rinv,Rinv.conj().T@b,tolerance) x = linalg.spsolve_triangular(R.tocsr(),y,lower=False) return x,norms