def dampnewton(F, DF, x, rtol, atol, lmin=1e-3): lu = linalg.lu_factor(DF(x)) s = linalg.lu_solve(lu, F(x)) sn = linalg.norm(s) xn = x - s l = 1.0 f = F(xn) st = linalg.lu_solve(lu, f) stn = linalg.norm(st) while stn > rtol * linalg.norm(xn) and stn > atol: while linalg.norm(st) > (1.0 - l / 2.0) * sn: l /= 2.0 if l < lmin: raise ValueError('no convergence: lambda -> 0') xn = x - l * s f = F(xn) st = linalg.lu_solve(lu, f) x = xn lu = linalg.lu_factor(DF(x)) s = linalg.lu_solve(lu, f) sn = linalg.norm(s) l = min(2.0 * l, 1.0) xn = x - l * s f = F(xn) st = linalg.lu_solve(lu, f) stn = linalg.norm(st) return xn
def build_np_models(kernel, trans_samples, ter_samples, ter_rew_samples, lamb): Xa, Ra, Xpa = zip(*trans_samples) Xa_term, Ra_term = zip(*ter_samples) Xa = [ np.vstack((xa, xa_term)) if xa_term.size > 0 else xa for xa, xa_term in izip(Xa, Xa_term) ] Ra = [ np.hstack((ra, ra_term)) if ra_term.size > 0 else ra for ra, ra_term in izip(Ra, Ra_term) ] k = len(trans_samples) # build the K_a,b matrices Kab = dict() for a,b in product(xrange(k), xrange(k)): if Xa_term[b].size > 0: Kab[(a,b)] = np.hstack((kernel(Xa[a], Xpa[b]), np.zeros((Xa[a].shape[0], Xa_term[b].shape[0])))) else: Kab[(a,b)] = kernel(Xa[a], Xpa[b]) # build the K_a, D_a matrices Ka = [kernel(Xa[i], Xa[i]) for i in xrange(k)] Dainv = [Ka[i] + lamb*scipy.eye(*Ka[i].shape) for i in xrange(k)] Da = [lu_factor(Dainv[i], overwrite_a = False) for i in xrange(k)] # build K_ter matrix Kterma = [ np.hstack((kernel(ter_rew_samples[0], Xpa[i]), np.zeros((ter_rew_samples[0].shape[0], Xa_term[i].shape[0])))) if Xa_term[i].size > 0 else kernel(ter_rew_samples[0], Xpa[i]) for i in xrange(k)] K_ter = kernel(ter_rew_samples[0], ter_rew_samples[0]) D_ter = lu_factor(K_ter + lamb*scipy.eye(*K_ter.shape), overwrite_a = True) R_ter = ter_rew_samples[1] return kernel, Kab, Da, Dainv, Ra, Kterma, D_ter, R_ter, Xa
def __init__(self, N, a0, alfa, beta, quad="GL", solver="scipy"): self.quad = quad self.solver = solver k = arange(N) self.S = S = SBBmat(k) self.B = B = BBBmat(k, self.quad) self.A = A = ABBmat(k) self.a0 = a0 self.alfa = alfa self.beta = beta if not solver == "scipy": sii, siu, siuu = S.dd, S.ud[0], S.ud[1] ail, aii, aiu = A.ld, A.dd, A.ud bill, bil, bii, biu, biuu = B.lld, B.ld, B.dd, B.ud, B.uud M = sii[::2].shape[0] if hasattr(beta, "__len__"): Ny, Nz = beta.shape if solver == "scipy": self.Le = Le = [] self.Lo = Lo = [] for i in range(Ny): Lej = [] Loj = [] for j in range(Nz): AA = a0*S.diags().toarray() + alfa[i, j]*A.diags().toarray() + beta[i, j]*B.diags().toarray() Ae = AA[::2, ::2] Ao = AA[1::2, 1::2] Lej.append(lu_factor(Ae)) Loj.append(lu_factor(Ao)) Le.append(Lej) Lo.append(Loj) else: self.u0 = zeros((2, M, Ny, Nz)) self.u1 = zeros((2, M, Ny, Nz)) self.u2 = zeros((2, M, Ny, Nz)) self.l0 = zeros((2, M, Ny, Nz)) self.l1 = zeros((2, M, Ny, Nz)) self.ak = zeros((2, M, Ny, Nz)) self.bk = zeros((2, M, Ny, Nz)) SFTc.LU_Biharmonic_3D(a0, alfa, beta, sii, siu, siuu, ail, aii, aiu, bill, bil, bii, biu, biuu, self.u0, self.u1, self.u2, self.l0, self.l1) SFTc.Biharmonic_factor_pr_3D(self.ak, self.bk, self.l0, self.l1) else: if solver == "scipy": AA = a0*S.diags().toarray() + alfa*A.diags().toarray() + beta*B.diags().toarray() Ae = AA[::2, ::2] Ao = AA[1::2, 1::2] self.Le = lu_factor(Ae) self.Lo = lu_factor(Ao) else: self.u0 = zeros((2, M)) self.u1 = zeros((2, M)) self.u2 = zeros((2, M)) self.l0 = zeros((2, M)) self.l1 = zeros((2, M)) self.ak = zeros((2, M)) self.bk = zeros((2, M)) SFTc.LU_Biharmonic_1D(a0, alfa, beta, sii, siu, siuu, ail, aii, aiu, bill, bil, bii, biu, biuu, self.u0, self.u1, self.u2, self.l0, self.l1) SFTc.Biharmonic_factor_pr(self.ak, self.bk, self.l0, self.l1)
def solve_nonlinear(self, inputs, outputs): """ Use numpy to solve Ax=b for x. Parameters ---------- inputs : Vector unscaled, dimensional input variables read via inputs[key] outputs : Vector unscaled, dimensional output variables read via outputs[key] """ vec_size = self.options['vec_size'] vec_size_A = self.vec_size_A # lu factorization for use with solve_linear self._lup = [] if vec_size > 1: for j in range(vec_size_A): lhs = inputs['A'][j] if vec_size_A > 1 else inputs['A'] self._lup.append(linalg.lu_factor(lhs)) for j in range(vec_size): idx = j if vec_size_A > 1 else 0 outputs['x'][j] = linalg.lu_solve(self._lup[idx], inputs['b'][j]) else: self._lup = linalg.lu_factor(inputs['A']) outputs['x'] = linalg.lu_solve(self._lup, inputs['b'])
def lu_factor(A, rho): r""" Compute LU factorisation of either :math:`A^T A + \rho I` or :math:`A A^T + \rho I`, depending on which matrix is smaller. Parameters ---------- A : array_like Array :math:`A` rho : float Scalar :math:`\rho` Returns ------- lu : ndarray Matrix containing U in its upper triangle, and L in its lower triangle, as returned by :func:`scipy.linalg.lu_factor` piv : ndarray Pivot indices representing the permutation matrix P, as returned by :func:`scipy.linalg.lu_factor` """ N, M = A.shape # If N < M it is cheaper to factorise A*A^T + rho*I and then use the # matrix inversion lemma to compute the inverse of A^T*A + rho*I if N >= M: lu, piv = linalg.lu_factor(A.T.dot(A) + rho*np.identity(M, dtype=A.dtype)) else: lu, piv = linalg.lu_factor(A.dot(A.T) + rho*np.identity(N, dtype=A.dtype)) return lu, piv
def dampnewton(x,F,DF,q=0.5,tol=1e-10): cvg = [] lup = lu_factor(DF(x)) s = lu_solve(lup,F(x)) xn = x-s lam = 1 st = lu_solve(lup,F(xn)) # simplified Newton while norm(st) > tol*norm(xn): while norm(st) > (1-lam*0.5)*norm(s): lam *= 0.5 if lam < 1e-10: cvg = -1 print 'Failure of convergence' return x, cvg xn = x-lam*s st = lu_solve(lup,F(xn)) # simplified Newton cvg += [[lam, norm(xn), norm(F(xn))]] x = xn lup = lu_factor(DF(x)) s = lu_solve(lup,F(x)) lam = min(lam/q, 1.) # Wozu dieser Test? xn = x-lam*s st = lu_solve(lup,F(xn)) # simplified Newton x = xn return x, array(cvg)
def solve_nonlinear(self, inputs, outputs): """ Use numpy to solve Ax=b for x. Parameters ---------- inputs : Vector Unscaled, dimensional input variables read via inputs[key]. outputs : Vector Unscaled, dimensional output variables read via outputs[key]. """ vec_size = self.options['vec_size'] vec_size_A = self.vec_size_A # lu factorization for use with solve_linear self._lup = [] if vec_size > 1: for j in range(vec_size_A): lhs = inputs['A'][j] if vec_size_A > 1 else inputs['A'] self._lup.append(linalg.lu_factor(lhs)) for j in range(vec_size): idx = j if vec_size_A > 1 else 0 outputs['x'][j] = linalg.lu_solve(self._lup[idx], inputs['b'][j]) else: self._lup = linalg.lu_factor(inputs['A']) outputs['x'] = linalg.lu_solve(self._lup, inputs['b'])
def prob4(): """Time different scipy.linalg functions for solving square linear systems. For various values of n, generate a random nxn matrix A and a random n-vector b using np.random.random(). Time how long it takes to solve the system Ax = b with each of the following approaches: 1. Invert A with la.inv() and left-multiply the inverse to b. 2. Use la.solve(). 3. Use la.lu_factor() and la.lu_solve() to solve the system with the LU decomposition. 4. Use la.lu_factor() and la.lu_solve(), but only time la.lu_solve() (not the time it takes to do the factorization). Plot the system size n versus the execution times. Use log scales if needed. """ #set the domains domain = 2**np.arange(1,10) times1 = [] times2 = [] times3 = [] times4 = [] #record the start time and end time for each function for n in domain: A = np.random.random((n,n)) b = np.random.random(n) start1 = time.time() A_ = la.inv(A) X = A_@b times1.append(time.time() -start1) start2 = time.time() la.solve(A, b) times2.append(time.time() -start2) start3 = time.time() L, P = la.lu_factor(A) x= la.lu_solve((L,P), b) times3.append(time.time() -start3) L, P = la.lu_factor(A) start4 = time.time() x= la.lu_solve((L,P), b) times4.append(time.time() -start4) # plot all the plt.loglog(domain, times1, 'g.-', basex = 2, basey = 2, linewidth = 2, markersize = 15, label = "Invert A") plt.loglog(domain, times2, 'b.-', basex = 2, basey = 2, linewidth = 2, markersize = 15, label = "la.solve") plt.loglog(domain, times3, 'c.-', basex = 2, basey = 2, linewidth = 2, markersize = 15, label = "la.lu_factor()") plt.loglog(domain, times4, 'y.-', basex = 2, basey = 2, linewidth = 2, markersize = 15, label = "la.lu_solve only") plt.legend(loc = "upper left") plt.show()
def F_D_Method(Ybus, Sbus, V0, Bp, Bpp, islk, ipv, ipq, max_it=100): tol = 1e-8 ##收敛精度 iter = 0 converged = 0 V = V0 Va = np.angle(V) Vm = np.abs(V) npv = ipv.size npq = ipq.size mis = (V * np.conj(Ybus @ V) - Sbus ) / Vm P = np.real(np.vstack((mis[ipv-1], mis[ipq-1]))) Q = np.imag(mis[ipq-1]) normP = np.linalg.norm(P, ord=np.inf) normQ = np.linalg.norm(Q, ord=np.inf) if normP < tol and normQ < tol: converged = 1 # Bp_t1 = Bp[np.hstack((ipv-1, ipq-1)).tolist(), :] # Bp_t2 = Bp_t1[:, np.hstack((ipv-1, ipq-1)).tolist()] # 删除平衡节点所在行与列 Bp = Bp[np.ix_(np.hstack((ipv-1, ipq-1)).tolist(), np.hstack((ipv-1, ipq-1)).tolist())] Bpp = Bpp[np.ix_(ipq-1, ipq-1)] from scipy.linalg import lu_factor LpUp, Pp = lu_factor(Bp) Pp = np.eye(Pp.size) Lp, Up = np.tril(LpUp, k=-1) + Pp, np.triu(LpUp) LppUpp, Ppp = lu_factor(Bpp) Ppp = np.eye(Ppp.size) Lpp, Upp = np.tril(LppUpp, k=-1) + Ppp, np.triu(LppUpp) while not converged and iter < max_it: iter += 1 # P迭代 dVa = - scipy.linalg.solve(Up, scipy.linalg.solve(Lp, Pp @ P)) Va[np.hstack((ipv-1, ipq-1)).tolist()] += dVa V = Vm * np.exp(1j * Va) mis = (V * np.conj(Ybus @ V) - Sbus) / Vm P = np.real(np.vstack((mis[ipv - 1], mis[ipq - 1]))) Q = np.imag(mis[ipq - 1]) normP = np.linalg.norm(P, ord=np.inf) normQ = np.linalg.norm(Q, ord=np.inf) if normP < tol and normQ < tol: converged = 1 break # Q迭代 dVm = - scipy.linalg.solve(Upp, scipy.linalg.solve(Lpp, Ppp @ Q)) Vm[ipq-1] += dVm V = Vm * np.exp(1j * Va) mis = (V * np.conj(Ybus @ V) - Sbus) / Vm P = np.real(np.vstack((mis[ipv - 1], mis[ipq - 1]))) Q = np.imag(mis[ipq - 1]) normP = np.linalg.norm(P, ord=np.inf) normQ = np.linalg.norm(Q, ord=np.inf) if normP < tol and normQ < tol: converged = 1 break return V, converged, iter
def test_lu_slogdet(): K = array([[2.76405235, 0.40015721], [0.97873798, 3.2408932]]) K = dot(K, K.T) LU = lu_factor(K) assert_allclose(lu_slogdet(LU), slogdet(K)) random = RandomState(6) K = random.randn(3, 3) K = dot(K, K.T) LU = lu_factor(K) assert_allclose(lu_slogdet(LU), slogdet(K))
def Q1(): a = np.array([ [0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], #2 [(2**(1 / 2)) / 2, 0, 0, -1, -(2**(1 / 2)) / 2, 0, 0, 0, 0, 0, 0, 0, 0], [(2**(1 / 2)) / 2, 0, 1, 0, (2**(1 / 2)) / 2, 0, 0, 0, 0, 0, 0, 0, 0], #4 [0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], #6 [ 0, 0, 0, 0, (2**(1 / 2)) / 2, 1, 0, 0, -(2**(1 / 2)) / 2, -1, 0, 0, 0 ], [0, 0, 0, 0, (2**(1 / 2)) / 2, 0, 1, 0, (2**(1 / 2)) / 2, 0, 0, 0, 0], #8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], #10 [0, 0, 0, 0, 0, 0, 0, 1, (2**(1 / 2)) / 2, 0, 0, -(2**(1 / 2)) / 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, (2**(1 / 2)) / 2, 0, 1, (2**(1 / 2)) / 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (2**(1 / 2)) / 2, 1] ]) b = np.array([0, 10, 0, 0, 0, 0, 0, 15, 0, 20, 0, 0, 0]) print(lg.solve(a, b)) #2.5 a = [[21.0, 67.0, 88.0, 73.0], [76.0, 63.0, 7.0, 20.0], [0.0, 85.0, 56.0, 54.0], [19.3, 43.0, 30.2, 29.4]] aFloat64 = copy.deepcopy(a) b = np.array([141.0, 109.0, 218.0, 93.7]) bFloat64 = copy.deepcopy(b) for item in b: item = np.float32(item) for item in a: for it in item: it = np.float32(it) for item in bFloat64: item = np.float64(item) for item in aFloat64: for it in item: it = np.float64(it) lu, piv = lg.lu_factor(a) x = lg.lu_solve((lu, piv), b) print() print(x) lu, piv = lg.lu_factor(aFloat64) x = lg.lu_solve((lu, piv), bFloat64) print() print(x)
def __call__(self, A, shift=0): from scipy.linalg import lu_factor, lu_solve nrows = A.mshape[0] nblock = A.blockshape[0] #assume a square block! assert A.bandwidth == 3, "Matrix bust be tridiagonal block matrix" #Overwrite current matrix? if self.overwrite: self.Alu = A #Create new internal matrix if none elif not hasattr(self, 'Alu'): logging.debug("Creating new internal LU matrix") self.Alu = BlockArray(A.mshape, A.blockshape, dtype=A.dtype) #Create new internal matrix if A is different shape elif (self.Alu.mshape != A.mshape) or (self.Alu.blockshape != A.blockshape): logging.debug( "Internal LU matrix incorrect shape; creating new one") del self.Alu self.Alu = BlockArray(A.mshape, A.blockshape, dtype=A.dtype) #Vector for pivots self.shift = shift self.pivots = zeros((nrows, nblock), dtype=A.dtype) piv = 0 Ishift = shift * eye(nblock) self.Alu.set_raw_block(0, 1, A.get_raw_block(0, 1) - Ishift) bnew = self.Alu.get_raw_block(0, 1) for row in range(0, nrows - 1): a = A.get_raw_block(row + 1, 0) b = bnew c = A.get_raw_block(row, 2) b, self.pivots[row] = lu_factor(b) # anew = a inv(b) anew = lu_solve((b, self.pivots[row]), a.T, trans=1).T bnew = A.get_raw_block(row + 1, 1) - Ishift - dot(anew, c) self.Alu.set_raw_block(row, 1, b) #blu self.Alu.set_raw_block(row + 1, 0, anew) #b anew = a if not self.overwrite: #Copy over block c, if not overwriting self.Alu.set_raw_block(row, 2, c) #Final LU decomp of last block b, self.pivots[nrows - 1] = lu_factor(bnew) self.Alu.set_raw_block(nrows - 1, 1, b)
def __call__(self, A, shift=0): from scipy.linalg import lu_factor, lu_solve nrows = A.mshape[0] nblock = A.blockshape[0] #assume a square block! assert A.bandwidth==3, "Matrix bust be tridiagonal block matrix" #Overwrite current matrix? if self.overwrite: self.Alu = A #Create new internal matrix if none elif not hasattr(self, 'Alu'): logging.debug( "Creating new internal LU matrix" ) self.Alu=BlockArray(A.mshape, A.blockshape, dtype=A.dtype) #Create new internal matrix if A is different shape elif (self.Alu.mshape<>A.mshape) or (self.Alu.blockshape<>A.blockshape): logging.debug( "Internal LU matrix incorrect shape; creating new one" ) del self.Alu self.Alu = BlockArray(A.mshape, A.blockshape, dtype=A.dtype) #Vector for pivots self.shift = shift self.pivots = zeros((nrows, nblock), dtype=A.dtype) piv=0 Ishift = shift*eye(nblock) self.Alu.set_raw_block(0,1,A.get_raw_block(0, 1) - Ishift) bnew = self.Alu.get_raw_block(0, 1) for row in range(0,nrows-1): a = A.get_raw_block(row+1, 0) b = bnew c = A.get_raw_block(row, 2) b, self.pivots[row] = lu_factor(b) # anew = a inv(b) anew = lu_solve((b,self.pivots[row]), a.T, trans=1).T bnew = A.get_raw_block(row+1, 1) - Ishift - dot(anew, c) self.Alu.set_raw_block(row,1,b) #blu self.Alu.set_raw_block(row+1,0,anew) #b anew = a if not self.overwrite: #Copy over block c, if not overwriting self.Alu.set_raw_block(row,2,c) #Final LU decomp of last block b, self.pivots[nrows-1] = lu_factor(bnew) self.Alu.set_raw_block(nrows-1,1,b)
def factored(self): "Caches the LU factorisation of the matrix" try: return self.lu_factored except AttributeError: self.lu_factored = la.lu_factor(self.val().simple_view()) return self.lu_factored
def fit(Y, op=otimes2_ij, **kwargs): ''' Constructs a dictionary with a fixed dataset. No pruning of dictionary elements is done. Mean centering is done with respect to the entire set of data Y. DATA IS ASSUMED TO COME IN COLUMNS. ''' import numpy as np from scipy import linalg as spla if kwargs.get('debug', False): import pdb pdb.set_trace() # verbosity = kwargs.get('verbosity', 0) global _oDD # storage for precomputed operator (D \otimes D) global _oDD_lufactors # LU factorization for dictionary global _mu # current estimate for mean global _D # storage for dictionary (mean-centered by _mu) d, n = np.shape(Y) _mu = np.mean(Y, axis=1) _mu.shape = (d, 1) _D = (Y.T - _mu.T).T _oDD = otimes(_D, _D, op=op) _oDD_lufactors = spla.lu_factor(_oDD) return
def CN(w0, z, m, w, x0, T, N): tn = 0 def b(tn): b = np.array([[0], [math.cos(w * tn) / m]]) return b A = np.array([[0, 1], [-(w0)**2, -2 * z * w0]]) dt = T / N xn = np.zeros((2, N + 1)) xn[:, 0, None] = x0 t = [dt * n for n in range(N + 1)] dta = 0.5 * dt * A lhs = np.identity(2) - dta lu, piv = linalg.lu_factor(lhs) for n in range(N): tn = t[n] rhs = np.matmul((np.identity(2) + 0.5 * dt * A), xn[:, n, None]) + 0.5 * dt * (b(tn + dt) + b(tn)) solution = linalg.lu_solve((lu, piv), rhs) xn[:, n + 1, None] = solution x = xn[0] return (x, t)
def __init__(self, f, a, b, tol=1e-10, n=12, mw=1e-15, verbose=False): """ f: function to create evaluator for a: lower bound of evaluation interval b: upper bound of evaluation interval tol: accuracy to recreate function to n: degree of chebyshev polynomials to be used mw: minimum width of interval (accuracy no longer guaranteed!) verbose: generate verbose output """ self.f = f r = b-a a1 = a + r/3 a2 = a + 2*r/3 self.dtype = self.f(np.array([a1, a2])).dtype self.a = float(a) self.b = float(b) self.tol = tol self.n = n self.mw = mw self.verbose = verbose self.lbs = [] self.ubs = [] self.coefs = [] _x, _ = get_chebyshev_nodes(-1, 1, self.n) self.V = np.polynomial.chebyshev.chebvander(_x, self.n-1) self.VLU = lu_factor(self.V) self._fit(self.a, self.b) self.lbs = np.array(self.lbs) self.ubs = np.array(self.ubs) self.iscale = 2.0/(self.ubs - self.lbs) self.coef_mat = np.row_stack(self.coefs)
def fnewton(w): p1 = sgm(Phi @ w) # p1|phi # (150,) grad = Phi.T @ (p1 - t) # (2,) print("grad=", grad) R = np.diag(p1 * (1 - p1)) # (150,150) H = Phi.T @ R @ Phi return la.lu_solve(la.lu_factor(H), grad)
def LU_det(A): (lu,piv) = la.lu_factor(A) # determine whether an even or odd number of row swaps s = (piv != np.arange(A.shape[0])).sum() % 2 return ((-1)**s) * lu.diagonal().prod()
def time_LU(): """Print the times it takes to solve a system of equations using LU decomposition and (A^-1)B where A is 1000x1000 and B is 1000x500.""" A = np.random.random((1000,1000)) b = np.random.random((1000,500)) start = time.time() L = la.lu_factor(A) a = time.time() - start start = time.time() A_inv = la.inv(A) a2 = time.time() - start start = time.time() la.lu_solve(L,b) a3 = time.time() - start start = time.time() np.dot(A_inv, b) a4 = time.time() - start time_lu_factor = a # set this to the time it takes to perform la.lu_factor(A) time_inv = a2 # set this to the time it takes to take the inverse of A time_lu_solve = a3 # set this to the time it takes to perform la.lu_solve() time_inv_solve = a4 # set this to the time it take to perform (A^-1)B print "LU solve: " + str(time_lu_factor + time_lu_solve) print "Inv solve: " + str(time_inv + time_inv_solve) # What can you conclude about the more efficient way to solve linear systems? print "Better to use LU decomposition than inverse, cause it is NEVER a good idea to calculate an inerse" # print your answer here.
def SolveNextTime(self): r""" Calculate the next time (factorization) and update the time stack grids """ try: self.tstep += 1 except : self.tstep = 0 self.LinearSystem() # gets the m factor from the solved system self.mUtfactor = ln.lu_factor(self.mUt) self.Source(self.tstep) # in time # As t is in [0, 1, 2] (2nd order) # time t in this case is Utime[2] v = self.Independent() result = ln.lu_solve(self.mUtfactor, v) # reshape the vector to became a matrix again self.Ufuture = result # make the update in the time stack # before [t-2, t-1, t] # after [t-1, t, t+1] # so t-2 receive t-1 and etc. self.Uprevious = self.Ucurrent self.Ucurrent = self.Ufuture return self.Ufuture
def inversiter(eguess,wfguess,A,S): vk=np.matrix(wfguess) olu,opi=la.lu_factor(Ovr) for L in range(Klu): # get the LU decompostion of A-e*S lu,piv=la.lu_factor(A-eguess*S) for K in range(Kpower): vk=np.matrix(la.lu_solve((lu,piv),vk,overwrite_b=True)) ek=(vk.T*A*vk).diagonal()/(vk.T*S*vk) if (eguess-ek[0,0])/eguess<1.e-9: return ek[0,0],wk[:,0] eguess=ek[0,0] vk=S*vk
def invpower(e,A,B=None): if B is None: B = eye(len(A)) K = A - B*e G = lu_factor(K) x = ones([len(A),1],complex)/len(A) iter = 0 error = 10 #for i in range(8): while error > 1e-8 and iter < 20: try: x = lu_solve(G,x) except: print 'LU Exception' x = solve(K,x) x = x/norm(x,ord=inf) error = norm(dot(A,x)-e*dot(B,x)) iter = iter +1 print 'invpower error = ',error x = x*conj(x[0]) print 'Eval = ',e print 'Evect Real = ',norm(real(x)) print 'Evect Imag = ',norm(imag(x)) return x
def factiz(K): """ Helper function to behave the same way scipy.sparse.factorized does, but for dense matrices. """ luf = lu_factor(K) return lambda x: matrix(lu_solve(luf, x))
def marglike(x, y, hyper, white_noise = False): # FIXME: build optional white noise into this kernel # Calculate covariance matrix K = matrifysquare(hyper, x, 0) K = 0.5* ( K + K.T) # Forces K to be perfectly symmetric # Calculate derivatives # dKdsigma = matrifysquare(hyper, x, 1) # Derivative w.r.t. log(sigma) # dKdlambda1 = matrifysquare(hyper, x, 2) # Derivative w.r.t. log(lambda1) # dKdh1 = matrifysquare(hyper, x, 3) # Derivative w.r.t. log(h1) sign, logdetK = np.linalg.slogdet(K) invKy = -0.5 * y.T * np.mat(la.lu_solve(la.lu_factor(K),y)) \ - 0.5 * logdetK - (y.size/2.) * np.log(2*np.pi) U = np.linalg.cholesky(K) n = len(x) L = - sum(np.log(np.diag(U))) -0.5 * y * invKy - n*0.5*np.log(2*np.pi) # dLdsigma = 0.5 * sum(np.diag(invKy*invKy.T*dKdsigma - (np.linalg.solve(K, dKdsigma)) )) # dLdlambda1 = 0.5 * sum(np.diag(invKy*invKy.T*dKdlambda1 - (np.linalg.solve(K, dKdlambda1)) )) # dKdh1 = 0.5 * sum(np.diag(invKy*invKy.T*dKdh1 - (np.linalg.solve(K, dKdh1)) )) return -L #, [-dKdsigma, -dKdlambda1, -dKdh1]
def SolveMatrixSystem(A, C): # Solve the matrix system AB = C for B given A and C # This function can compute the inverse of A if C is the identity matrix # but numpy.linalg.inv is faster # # A [in]: NxN numpy array # C [in]: NxN numpy array # # B [out]: NxN numpy array # Insert input checking here import numpy as np import scipy.linalg as la (N, N) = C.shape (LU, P) = la.lu_factor(A) B = np.zeros((N, N)) for k in range(N): c = C[k, :] z = la.lu_solve((LU, P), c) B[k, :] = z return B
def test_lapack_misaligned(): M = np.eye(10, dtype=float) R = np.arange(100) R.shape = 10, 10 S = np.arange(20000, dtype=np.uint8) S = np.frombuffer(S.data, offset=4, count=100, dtype=np.float) S.shape = 10, 10 b = np.ones(10) v = np.ones(3, dtype=float) LU, piv = lu_factor(S) for (func, args, kwargs) in [ (eig, (S, ), dict(overwrite_a=True)), # crash (eigvals, (S, ), dict(overwrite_a=True)), # no crash (lu, (S, ), dict(overwrite_a=True)), # no crash (lu_factor, (S, ), dict(overwrite_a=True)), # no crash (lu_solve, ((LU, piv), b), dict(overwrite_b=True)), (solve, (S, b), dict(overwrite_a=True, overwrite_b=True)), (svd, (M, ), dict(overwrite_a=True)), # no crash (svd, (R, ), dict(overwrite_a=True)), # no crash (svd, (S, ), dict(overwrite_a=True)), # crash (svdvals, (S, ), dict()), # no crash (svdvals, (S, ), dict(overwrite_a=True)), #crash (cholesky, (M, ), dict(overwrite_a=True)), # no crash (qr, (S, ), dict(overwrite_a=True)), # crash (rq, (S, ), dict(overwrite_a=True)), # crash (hessenberg, (S, ), dict(overwrite_a=True)), # crash (schur, (S, ), dict(overwrite_a=True)), # crash ]: yield check_lapack_misaligned, func, args, kwargs
def SolveNextTime(self): r""" Calculate the next time (factorization) and update the time stack grids """ try: self.tstep += 1 except : self.tstep = 0 self.LinearSystem() # gets the m factor from the solved system self.mUtfactor = ln.lu_factor(self.mUt) # As t is in [0, 1, 2] (2nd order) # time t in this case is Utime[2] # the independent term of the matrix, due the pressure field v = self.Independent() result = ln.lu_solve(self.mUtfactor, v) # reshape the vector to become a matrix again self.Ufuture = np.reshape(result, (self.Nz, self.Nx)) # make the update in the time stack # before [t-2, t-1, t] # after [t-1, t, t+1] # so t-2 receive t-1 and etc. # make the update in the time stack self.Uprevious[:][:] = self.Ucurrent[:][:] self.Ucurrent[:][:] = self.Ufuture[:][:] return self.Ufuture
def lu_det(A): """Compute det(A) using the LU decomposition (via la.lu_factor()).""" lu, piv = la.lu_factor(A) # Determine if there were an even or odd number of row swaps. s = (piv != np.arange(A.shape[0])).sum() % 2 return ((-1)**s) * lu.diagonal().prod()
def test_simple_known(self): # Ticket #1458 for order in ['C', 'F']: A = np.array([[2, 1],[0, 1.]], order=order) LU, P = lu_factor(A) assert_array_almost_equal(LU, np.array([[2, 1], [0, 1]])) assert_array_equal(P, np.array([0, 1]))
def statdist(generator): """Compute the stationary distribution of a Markov chain. Args: generator (numpy.ndarray): The infinitesimal generator matrix of the Markov chain. Returns: dist (List[float]): The unnormalized stationary distribution of the Markov chain. """ n = generator.shape[0] with warnings.catch_warnings(): # The LU decomposition raises a warning when the generator matrix is # singular (which it, by construction, is!). warnings.filterwarnings('ignore') lu, piv = spl.lu_factor(generator.T, check_finite=False) # The last row contains 0's only. left = lu[:-1,:-1] right = -lu[:-1,-1] # Solves system `left * x = right`. Assumes that `left` is # upper-triangular (ignores lower triangle). res = spl.solve_triangular(left, right, check_finite=False) res = np.append(res, 1.0) return (n / res.sum()) * res
def test_lu_update_col(): size = 5 B = np.reshape(range(size**2), (size, size)) + np.eye(size) Bt = B.T aq = np.zeros(size) aq[0] = 1 lu, piv = linalg.lu_factor(B) piv_py = conv_piv(piv) piv_inv = inv_piv(piv_py) out = 1 B_new = np.row_stack((Bt[0:out], Bt[out + 1:], aq)).T ret = lu_update_col(lu, out, aq[piv_py]) H, pivs, trns = ret U = np.triu(H) L = calc_lu_lower(lu, out, trns) B_upd = L.dot(U)[piv_inv] print "\nTEST LU UPDATE" print "piv_lapack\t%s" % str(piv) print "piv_py\t%s" % str(piv_py) print "H\n%s" % str(H) print "update piv\t%s" % str(pivs) print "update trans\t%s" % str(trns) print "L\n%s" % str(L) print "B_new\n%s" % str(B_new) print "B_upd\n%s" % str(B_upd) print "B_upd is close to B_new.\t%s" % np.allclose(B_new, B_upd)
def linearize(self, params, unknowns, resids): """ Jacobian for disp.""" name = self.surface['name'] jac = self.alloc_jacobian() if fortran_flag: fd_jac = self.fd_jacobian( params, unknowns, resids, fd_params=['A', 'Iy', 'Iz', 'J', 'nodes', 'loads'], fd_states=[]) jac.update(fd_jac) else: cs_jac = self.complex_step_jacobian( params, unknowns, resids, fd_params=['A', 'Iy', 'Iz', 'J', 'nodes', 'loads'], fd_states=[]) jac.update(cs_jac) jac['disp_aug', 'disp_aug'] = self.K.real self.lup = lu_factor(self.K.real) return jac
def linearize(self, params, unknowns, resids): """ Jacobian for Sellar discipline 1.""" z1 = params['z'][0] z2 = params['z'][1] x1 = params['x'] y1 = unknowns['y1'] y2 = unknowns['y2'] J = {} J['y1', 'y2'] = -0.2 J['y1', 'z'] = np.array([[2.0*z1, 1.0]]) J['y1', 'x'] = 1.0 J['y2', 'y1'] = .5*y1**-.5 J['y2', 'z'] = np.array([[1.0, 1.0]]) J['y2', 'y2'] = -1.0 dRdy = np.zeros((2, 2)) dRdy[0, 1] = J['y1', 'y2'] dRdy[0, 0] = 1.0 dRdy[1, 0] = J['y2', 'y1'] dRdy[1, 1] = J['y2', 'y2'] # lu factorization for use with solve_linear self.lup = linalg.lu_factor(dRdy) return J
def updatedata(self, A): # Update b, c try: ALU = linalg.lu_factor(A) BC = linalg.lu_solve(ALU, c_[linalg.lu_solve(ALU, self.data.b + 1e-8*self.data.Brand[:,:1]), \ self.data.c + 1e-8*self.data.Crand[:,:1]], trans=1) C = linalg.lu_solve(ALU, BC[:,-1:]) B = BC[:,:1] except: if self.C.verbosity >= 1: print 'Warning: Problem updating border vectors. Using svd...' U, S, Vh = linalg.svd(A) B = U[:,-1:] C = num_transpose(Vh)[:,-1:] bmult = cmult = 1 if matrixmultiply(transpose(self.data.b), B) < 0: bmult = -1 if matrixmultiply(transpose(self.data.c), C) < 0: cmult = -1 self.data.b = bmult*B*(linalg.norm(A,1)/linalg.norm(B)) self.data.c = cmult*C*(linalg.norm(A,Inf)/linalg.norm(C)) # Update if self.update: self.data.B[:,0] = self.data.b*(linalg.norm(A,1)/linalg.norm(self.data.b)) self.data.C[:,0] = self.data.c*(linalg.norm(A,Inf)/linalg.norm(self.data.c)) self.data.B[:,1] = self.data.w[:,2]*(linalg.norm(A,1)/linalg.norm(self.data.w,1)) self.data.C[:,1] = self.data.v[:,2]*(linalg.norm(A,Inf)/linalg.norm(self.data.v,1)) self.data.D[0,1] = self.data.g[0,1] self.data.D[1,0] = self.data.g[1,0]
def SolveNextTime(self): r""" Calculate the next time (factorization) and update the time stack grids """ try: self.tstep += 1 except: self.tstep = 0 self.LinearSystem() # gets the m factor from the solved system self.mUtfactor = ln.lu_factor(self.mUt) # As t is in [0, 1, 2] (2nd order) # time t in this case is Utime[2] # the independent term of the matrix, due the pressure field v = self.Independent() result = ln.lu_solve(self.mUtfactor, v) # reshape the vector to become a matrix again self.Ufuture = np.reshape(result, (self.Nz, self.Nx)) # make the update in the time stack # before [t-2, t-1, t] # after [t-1, t, t+1] # so t-2 receive t-1 and etc. # make the update in the time stack self.Uprevious[:][:] = self.Ucurrent[:][:] self.Ucurrent[:][:] = self.Ufuture[:][:] return self.Ufuture
def get_loo_influences(self): X_train = self.data_sets.train.x Y_train = self.data_sets.train.labels * 2 - 1 theta = self.sess.run(self.params)[0] # Pre-calculate inverse covariance matrix n = X_train.shape[0] dim = X_train.shape[1] cov = np.zeros([dim, dim]) probs = expit(np.dot(X_train, theta.T)) weighted_X_train = np.reshape(probs * (1 - probs), (-1, 1)) * X_train cov = np.dot(X_train.T, weighted_X_train) / n cov += self.weight_decay * np.eye(dim) cov_lu_factor = slin.lu_factor(cov) assert (len(Y_train.shape) == 1) x_train_theta = np.reshape(X_train.dot(theta.T), [-1]) sigma = expit(-Y_train * x_train_theta) d_theta = slin.lu_solve(cov_lu_factor, X_train.T).T quad_x = np.sum(X_train * d_theta, axis=1) return sigma * quad_x
def factor_wout(self): """ LU factorization of inverse portion of Tikhonov least squares solution. This avoids inverse computation in order to give a speed boost when the reservoir readout matrix is updated frequently. (Adaptive control) """ self.W_out_factors = lu_factor(self.Hbar + self.ridge_alpha * np.eye(self.res_sz))
def LU_decomp_solve(A, b): lu, p = linalg.lu_factor(A) x = linalg.lu_solve((lu, p), b, True) print "Soultion using LU decomp is::" print x print("\n") return x
def lnlike(h, X, y, covfunc): y = np.matrix(np.array(y).flatten()).T K = covfunc(X, X, h, wn = True) sign, logdetK = np.linalg.slogdet(K) alpha = np.mat(la.lu_solve(la.lu_factor(K),y)) logL = -0.5*y.T * alpha - 0.5*logdetK - (y.size/2.)*np.log(2) return np.array(logL).flatten()
def linearize(self, inputs, outputs, partials): system_size = self.system_size self.lu = lu_factor(inputs['mtx']) partials['circulations', 'circulations'] = inputs['mtx'].flatten() partials['circulations', 'mtx'] = \ np.outer(np.ones(system_size), outputs['circulations']).flatten()
def LUD(M, f): start = time.clock() LU = linalg.lu_factor(M) x = linalg.lu_solve(LU, f) end = time.clock() print('CPU time (LUD case):', end - start) return (x)
def phi(self, tim1, yi): jac = jacobian(self.f, tim1, yi) a = eye(self.m, self.m) - 0.5 * (1 + 1j) * self.h * jac b = self.f(tim1 + 0.5 * self.h, yi) lu = linalg.lu_factor(a) w = linalg.lu_solve(lu, b) return w.real
def updatedata(self, A): if self.update: if self.corr: self.data.B = self.data.w*(linalg.norm(A,1)/linalg.norm(self.data.w,1)) self.data.C = self.data.v*(linalg.norm(A,Inf)/linalg.norm(self.data.v,1)) else: # Note: Problem when singular vectors switch smallest singular value (See NewLorenz). # To overcome this, I have implemented a 1e-8 random nudge. try: ALU = linalg.lu_factor(A) BC = linalg.lu_solve(ALU, c_[linalg.lu_solve(ALU, self.data.B + 1e-8*self.data.Brand), \ self.data.C + 1e-8*self.data.Crand], trans=1) C = linalg.lu_solve(ALU, BC[:,-1*self.data.q:]) B = BC[:,0:self.data.p] except: if self.C.verbosity >= 1: print 'Warning: Problem updating border vectors. Using svd...' U, S, Vh = linalg.svd(A) B = U[:,-1*self.data.p:] C = num_transpose(Vh)[:,-1*self.data.q:] bmult = cmult = 1 if matrixmultiply(transpose(self.data.B), B) < 0: bmult = -1 if matrixmultiply(transpose(self.data.C), C) < 0: cmult = -1 self.data.B = bmult*B*(linalg.norm(A,1)/linalg.norm(B)) self.data.C = cmult*C*(linalg.norm(A,Inf)/linalg.norm(C))
def test_lapack_misaligned(): M = np.eye(10,dtype=float) R = np.arange(100) R.shape = 10,10 S = np.arange(20000,dtype=np.uint8) S = np.frombuffer(S.data, offset=4, count=100, dtype=np.float) S.shape = 10, 10 b = np.ones(10) v = np.ones(3,dtype=float) LU, piv = lu_factor(S) for (func, args, kwargs) in [ (eig,(S,),dict(overwrite_a=True)), # crash (eigvals,(S,),dict(overwrite_a=True)), # no crash (lu,(S,),dict(overwrite_a=True)), # no crash (lu_factor,(S,),dict(overwrite_a=True)), # no crash (lu_solve,((LU,piv),b),dict(overwrite_b=True)), (solve,(S,b),dict(overwrite_a=True,overwrite_b=True)), (svd,(M,),dict(overwrite_a=True)), # no crash (svd,(R,),dict(overwrite_a=True)), # no crash (svd,(S,),dict(overwrite_a=True)), # crash (svdvals,(S,),dict()), # no crash (svdvals,(S,),dict(overwrite_a=True)), #crash (cholesky,(M,),dict(overwrite_a=True)), # no crash (qr,(S,),dict(overwrite_a=True)), # crash (rq,(S,),dict(overwrite_a=True)), # crash (hessenberg,(S,),dict(overwrite_a=True)), # crash (schur,(S,),dict(overwrite_a=True)), # crash ]: yield check_lapack_misaligned, func, args, kwargs
def factorise_interp_matrix(self): if not self.factorisation_current: A, self.left_scaling, self.right_scaling = self.interpolation_matrix( ) self.lu, self.piv = LA.lu_factor(A) self.factorisation_current = True return
def linearize(self, params, unknowns, resids): """ Jacobian for Sellar discipline 1.""" z1 = params['z'][0] z2 = params['z'][1] x1 = params['x'] y1 = unknowns['y1'] y2 = unknowns['y2'] J = {} J['y1', 'y2'] = -0.2 J['y1', 'z'] = np.array([[2.0 * z1, 1.0]]) J['y1', 'x'] = 1.0 J['y2', 'y1'] = .5 * y1**-.5 J['y2', 'z'] = np.array([[1.0, 1.0]]) J['y2', 'y2'] = -1.0 dRdy = np.zeros((2, 2)) dRdy[0, 1] = J['y1', 'y2'] dRdy[0, 0] = 1.0 dRdy[1, 0] = J['y2', 'y1'] dRdy[1, 1] = J['y2', 'y2'] # lu factorization for use with solve_linear self.lup = linalg.lu_factor(dRdy) return J
def max_likelihood_estimate(G,V_singular,U_singular,Sigma_singular): """ :param G: Vector of all average values of the Greensfunction for each time step :param V_singular: part of the SVD of K , K = V*Sigma.T*U.T which contains only the components for non zero singular values. :param U_singular: part of the SVD of K , K = V*Sigma.T*U.T which contains only the components for non zero singular values. :param Sigma_singular: part of the SVD of K , K = V*Sigma.T*U.T which contains only the components for non zero singular values. :return: Maximum likelihood estimate of the Lehmann spectral function A. """ inv_Sigma_singular = np.linalg.inv(Sigma_singular) inv_K_singular = np.dot(U_singular,np.dot(inv_Sigma_singular,V_singular.T)) return np.dot(inv_K_singular,G) s=len(u) max_val = np.sum(m) K_s = np.dot(V,np.dot(Sigma,U.T)) diff = 1. count1 = 1 max_iter = 1000 while diff > 1e-8 and count1 <= max_iter: A_appr = m * np.exp(np.dot(U,u)) inv_cov = (1. / np.diagonal(Cov)**2) inv_cov_mat = np.diag(inv_cov) dLdF = - inv_cov * (G - np.dot(K_s, A_appr)) F_u = - alpha * u - np.dot(Sigma,np.dot(V.T,dLdF)) M = np.dot(Sigma,np.dot(V.T,np.dot(inv_cov_mat,np.dot(V,Sigma)))) T = np.dot(U.T,np.dot(np.diag(A_appr),U)) J = alpha * np.diag(np.ones((s))) + np.dot(M,T) lu_and_piv = lu_factor(J) delta_u = lu_solve(lu_and_piv,F_u) count2 = 1 h = np.abs(np.dot(F_u.T,F_u))/np.abs(np.dot(F_u.T,np.dot(Jac,F_u))) mu = 1./h while np.dot(delta_u.T,np.dot(T,delta_u.T)) > max_val and count2 <= max_iter: J = (alpha+count2*mu) * np.diag(np.ones((s))) + np.dot(M,T) lu_and_piv = lu_factor(J) delta_u = lu_solve(lu_and_piv,F_u) count2 +=1 u_old = u u = u + delta_u diff = np.abs(np.sum(u-u_old)) count1 += 1 return u
def root_finding_newton(u, m, alpha, V, Sigma, U, G, Cov, dw): """ :param u: initial vector of u :param m: vector of default model for the Lehmann spectral function :param alpha: scalar value, controls the relative weight of maximizing entropie and minimizing kind. of least squares fit. :param V: part of singular SVD of K, K = V*Sigma*U.T :param Sigma: part of singular SVD of K, K = V*Sigma*U.T :param U: part of singular SVD of K, K = V*Sigma*U.T :param G: Vector of all average values of the Greensfunction for each time step :param Cov: Vector with variance of the re-binned, gaussian distributed QMC approximations for the different time-steps :param dw: omega step size :return: """ s=len(u) max_val = np.sum(m) K_s = np.dot(V,np.dot(Sigma,U.T)) diff = 1. count1 = 1 max_iter = 1000 while diff > 1e-8 and count1 <= max_iter: print(count1) A_appr = m * np.exp(np.dot(U,u)) A_old = A_appr inv_cov = (1. / np.diagonal(Cov)**2) inv_cov_mat = np.diag(inv_cov) dLdF = - inv_cov * (G - np.dot(K_s, A_appr)) F_u = - alpha * u - np.dot(Sigma,np.dot(V.T,dLdF)) M = np.dot(Sigma,np.dot(V.T,np.dot(inv_cov_mat,np.dot(V,Sigma)))) T = np.dot(U.T,np.dot(np.diag(A_appr),U)) J = alpha * np.diag(np.ones((s))) + np.dot(M,T) lu_and_piv = lu_factor(J) delta_u = lu_solve(lu_and_piv,F_u) A_appr = m * np.exp(np.dot(U,u + delta_u)) count2 = 1 while np.linalg.norm(A_appr - A_old) > max_val and count2 <= max_iter: J = (alpha+count2*1e10) * np.diag(np.ones((s))) + np.dot(M,T) lu_and_piv = lu_factor(J) delta_u = lu_solve(lu_and_piv,F_u) A_appr = m * np.exp(np.dot(U,u + delta_u)) count2 +=1 u_old = u u = u + delta_u diff = np.abs(np.sum(u-u_old)) count1 += 1 return u
def getVW(self, A): # V --> m, W --> n #print self.data MLU = linalg.lu_factor(c_[r_[A,transpose(self.data.C)], r_[self.data.B,self.data.D]]) V = linalg.lu_solve(MLU,r_[zeros((self.data.n,self.data.q), Float), eye(self.data.q)]) W = linalg.lu_solve(MLU,r_[zeros((self.data.m,self.data.p), Float), eye(self.data.p)],trans=1) return V, W
def inversiter(eguess, wfguess, A, S, Klu=100, Kpower=1): vk = np.matrix(np.random.random((wfguess.size, 1))) olu, opi = la.lu_factor(S) for L in range(Klu): # get the LU decompostion of A-e*S lu, piv = la.lu_factor(A - eguess * S) for K in range(Kpower): vk = np.matrix(S) * vk vk = np.matrix(la.lu_solve((lu, piv), vk, overwrite_b=False)) ek = (vk.T * A * vk).diagonal() / (vk.T * S * vk) if (eguess - ek[0, 0]) / eguess < 1.0e-9: return ek[0, 0], vk[:, 0] eguess = ek[0, 0]
def solve_nonlinear(self, params, unknowns, resids): """ Use np to solve Ax=b for x. """ # lu factorization for use with solve_linear self.lup = lu_factor(params['K']) unknowns['disp_aug'] = lu_solve(self.lup, params['forces']) resids['disp_aug'] = params['K'].dot(unknowns['disp_aug']) - params['forces']
def linearize(self, inputs, outputs, partials): num_elements = self.metadata['num_elements'] num_nodes = num_elements + 1 size = 2 * num_nodes + 2 self.lu = lu_factor(inputs['K']) partials['d', 'K'] = np.outer(np.ones(size), outputs['d']).flatten() partials['d', 'd'] = inputs['K']
def solve_nonlinear(self, params, unknowns, resids): """ Use numpy to solve Ax=b for x. """ # lu factorization for use with solve_linear self.lup = linalg.lu_factor(params['A']) unknowns['x'] = linalg.lu_solve(self.lup, params['b']) resids['x'] = params['A'].dot(unknowns['x']) - params['b']
def __init__(self, name, sizeDomain, meshSize, diff, bulk, dt): self.name = name self.sizeDomain = sizeDomain self.meshSize = meshSize self.xLoc = numpy.linspace(0.0,self.sizeDomain[0],num=self.meshSize[0]) self.dx = self.xLoc[1] - self.xLoc[0] self.yLoc = numpy.linspace(0.0,self.sizeDomain[1],num=self.meshSize[1]) self.dy = self.yLoc[1] - self.yLoc[0] self.pts = meshSize[0]*meshSize[1] self.conc = numpy.zeros((self.pts,),dtype=numpy.float) self.diff = -diff * dt self.bulk = bulk for i in range(self.meshSize[0]): # intitial conditions on top edge node = self.nodeNum(i,self.meshSize[1]-1) self.conc[node] = self.bulk # Build diffusion operator self.opA = numpy.zeros((self.pts,self.pts),dtype=numpy.float) for j in range(self.meshSize[1]): # loop through y nodes for i in range(self.meshSize[0]): # loop through x nodes node = self.nodeNum(i,j) nodeS = self.nodeNum(i,j-1) nodeE = self.nodeNum(i+1,j) nodeN = self.nodeNum(i,j+1) nodeW = self.nodeNum(i-1,j) bcX = False bcY = False self.opA[node,node] += 1.0 if i == 0: # west wall bcX = True self.opA[node,node] += -2.0*self.diff/(self.dx**2) self.opA[node,nodeE] += 2.0*self.diff/(self.dx**2) if i == (self.meshSize[0]-1): # east wall bcX = True self.opA[node,node] += -2.0*self.diff/(self.dx**2) self.opA[node,nodeW] += 2.0*self.diff/(self.dx**2) if j == 0: # south wall bcY = True self.opA[node,node] += -2.0*self.diff/(self.dy**2) self.opA[node,nodeN] += 2.0*self.diff/(self.dy**2) if j == (self.meshSize[1]-1): # top surface bcX = True bcY = True self.opA[node,node] = 1.0 if i == 0: self.opA[node,nodeE] = 0.0 if i == (self.meshSize[0]-1): self.opA[node,nodeW] = 0.0 if bcX == False: self.opA[node,node] += -2.0*self.diff/(self.dx**2) self.opA[node,nodeW] += 1.0*self.diff/(self.dx**2) self.opA[node,nodeE] += 1.0*self.diff/(self.dx**2) if bcY == False: self.opA[node,node] += -2.0*self.diff/(self.dy**2) self.opA[node,nodeS] += 1.0*self.diff/(self.dy**2) self.opA[node,nodeN] += 1.0*self.diff/(self.dy**2) self.lu = slin.lu_factor(self.opA)