def simulate_lu_decom(sim_locations,sample_locations,vmodel): c11 = fill_cova(sample_locations,None,vmodel) c21 = fill_cova(sim_locations,sample_locations,vmodel) c22 = fill_cova(sim_locations,None,vmodel) u11 = cholesky(c11) l11 = u11.T u11_inv = inv(u11) l21 = c21 @ u11_inv u12 = l21.T l22 = cholesky(c22-l21@u12,lower=True) return u11_inv.T,l21,l22 l11,u11 = lu(c11,permute_l= True) l11_inv = inv(l11) a21t = l11_inv @ c21.T a21 = a21t.T b12 = a21t l22,u22 = lu(c22-l21@u12,permute_l= True) return a21,l11_inv,l22
def randomized_pca(A, n_components, n_oversamples=10, n_iter="auto", flip_sign=True, random_state=0): """Compute the randomized PCA decomposition of a given matrix. This method differs from the scikit-learn implementation in that it supports and handles sparse matrices well. """ if n_iter == "auto": # Checks if the number of iterations is explicitly specified # Adjust n_iter. 7 was found a good compromise for PCA. See sklearn #5299 n_iter = 7 if n_components < .1 * min(A.shape) else 4 n_samples, n_features = A.shape c = np.atleast_2d(A.mean(axis=0)) if n_samples >= n_features: Q = random_state.normal(size=(n_features, n_components + n_oversamples)) Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q) # Normalized power iterations for _ in range(n_iter): Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :]) Q, _ = lu(Q, permute_l=True) Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q) Q, _ = lu(Q, permute_l=True) Q, _ = qr(Q, mode="economic") QA = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :]) R, s, V = svd(QA.T, full_matrices=False) U = Q.dot(R) else: # n_features > n_samples Q = random_state.normal(size=(n_samples, n_components + n_oversamples)) Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :]) # Normalized power iterations for _ in range(n_iter): Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q) Q, _ = lu(Q, permute_l=True) Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :]) Q, _ = lu(Q, permute_l=True) Q, _ = qr(Q, mode="economic") QA = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q) U, s, R = svd(QA, full_matrices=False) V = R.dot(Q.T) if flip_sign: U, V = svd_flip(U, V) return U[:, :n_components], s[:n_components], V[:n_components, :]
def decompose( matrix ): # Returns the decomposition of a matrix A where # # Q.A.Q = P.L.U # # P.L.U is the factoring of Q.A.Q such that L is a lower triangular matrix with 1's # on the diagonal and U is an upper triangular matrix; P is the permutation (row-swapping # operations) required for this procedure. The permutation matrix Q is chosen such that # the last element of U is its smallest diagnoal element. If A has a zero eigenvalue, # then U's last element will be zero. dim = matrix.shape[ 0 ] # first decomposition ( P, L, U ) = lu( matrix ) # detect the smallest element of U smallestIndex = findsmallestdiag( U ) smallest = U[ smallestIndex, smallestIndex ] #show( matrix, "M" ) #show( U, "U" ) #print "Smallest element is %f at %d" % ( smallest, smallestIndex ) # is the permutation Q not just the identity matrix? Q = identity( dim ) if smallestIndex+1 != dim : # trick: exchange row 'smallestIndex' with row 'dim-1' of the identity matrix swaprow( Q, smallestIndex, dim-1 ) return ( P, L, U, Q )
def part_b(run_count, a, b): """ Solve using LU decomposition """ _, l, u = lu(a) for run in xrange(run_count): inv(u).dot(inv(l).dot(b))
def __get_Qd(self): """ Sets the integration matrices QI and QE for the IMEX sweeper Returns: QI: St. Martin's trick, use LU decomposition QE: explicit Euler matrix, will also act on u0 """ # QI = np.zeros(np.shape(self.coll.Qmat)) QE = np.zeros(np.shape(self.coll.Qmat)) for m in range(self.coll.num_nodes + 1): # QI[m, 1:m+1] = self.coll.delta_m[0:m] QE[m, 0:m] = self.coll.delta_m[0:m] # This is for using LU decomposition # strip Qmat by initial value u0 QT = self.coll.Qmat[1:,1:].T # do LU decomposition of QT [P,L,U] = LA.lu(QT,overwrite_a=True) # enrich QT by initial value u0 Qd = np.zeros(np.shape(self.coll.Qmat)) Qd[1:,1:] = U.T QI = Qd return QI, QE
def lu(A, b): #sol = [] # Edit here to implement your code L,U=lp.lu(A,True) y=lp.solve(L,b) x=lp.solve(U,y) return list(x)
def _traverse_grid_(self): """ Solve using linear systems of equations """ P, L, U = linalg.lu(self.M1) for j in reversed(range(self.N)): x1 = linalg.solve(L, np.dot(self.M2, self.grid[1:self.M, j+1])) x2 = linalg.solve(U, x1) self.grid[1:self.M, j] = x2
def find_basis(M): """Find the indices of the columns of M that form a basis or range(M)""" p,l,u = sla.lu(M) ind = [i for i in range(u.shape[0]) if u[i,i] != 0.0] if u[i,i] == 0: for j in range(i+1,u.shape[1]): if u[i,j] != 0.0: ind.append(j); break return ind
def __init__(self, num_nodes, tleft, tright): super(CollGaussRadau_Right_LU_Trick, self).__init__(num_nodes, tleft, tright) Q = self.Qmat p, l, u = lu(Q[1:, 1:].transpose()) # print np.diag(l) self.QDmat = u.transpose()
def _traverse_grid_(self): """ Solve using linear systems of equations """ P, L, U = linalg.lu(self.coeffs) aux = np.zeros(self.M-1) for j in reversed(range(self.N)): aux[0] = np.dot(-self.a[1], self.grid[0, j]) x1 = linalg.solve(L, self.grid[1:self.M, j+1]+aux) x2 = linalg.solve(U, x1) self.grid[1:self.M, j] = x2
def _traverse_grid_(self): P,L,U=linalg.lu(self.coeffs) aux=np.zeros(self.M-1) for j in reversed(range(self.N)): aux[0]=np.dot(-self.a[1],self.grid[0,j]) x1=linalg.solve(L,self.grid[1:self.M,j+1]+aux) x2=linalg.solve(U,x1) self.grid[1:self.M,j]=x2
def __init__(self,Z,Az,r,apply='LU'): M=dgemm(Z,Az.T) if apply=='eig': self.setting_inverse_w_eigenvalues(M) super(CoarseLO,self).__init__(nargin=r,nargout=r,matvec=self.mult_eig, symmetric=True) elif apply =='LU': self.L,self.U=lu(M,permute_l=True,overwrite_a=True,check_finite=False) super(CoarseLO,self).__init__(nargin=r,nargout=r,matvec=self.mult, symmetric=True)
def G2AH(G): n = len(G) A,B = npG2SVAR(G) P,L,U = linalg.lu(B) A = linalg.inv(L).tolist() B = B.tolist() A = listplace(A, 0.0, 0.0) for i in range(0,n): A[i][i] = 1 B = listplace(B, 0.0, 'e') for i in range(0,n): B[i][i] = 'e' return A,B,P
def ldl_decomposition(matrix): p, L, U = scl.lu(matrix) size = matrix.shape[0] D = np.zeros((size, size), dtype=np.float64) divisor = np.ones(size, dtype=np.float64) for i in range(size): D[i][i] = U[i][i] divisor = U[i][i] U[i] /= divisor return L, D, U
def lu(A, b): sol = [] P, L, U = sci.lu(A) #P is Permutation matrix #L is Lower Triangular matrix #U is Upper Triangular matrix #A = PLU #Ax = b => PLUx = b sol = np.dot(sci.inv(U),np.dot(sci.inv(L),np.dot(sci.inv(P),b))) return list(sol)
def CompleteBase(V, B, eps=1e-4): tbase = append(V, B, axis=1) p, l, u = lu(tbase) echelon = zeros(u.shape[1], int) for row in u: tmp = nonzero(abs(row) > eps)[0] if tmp.size: echelon[tmp[0]] = 1 return compress(echelon, tbase, axis=1)
def Lu(A): """(L,U,P) = Lu(A) Compute pivoted LU decompostion of a matrix. RETURNS: L,U,P where A = PLU """ (P,L,U) = lu(A) return ( L,U,P )
def forward(A,b): p,l,u=s.lu(A) newB=b solutionList=[] for z in range(0,newB.shape[0]): mySum=0 for j in range(0,z): mySum=mySum+ (l.item((z),j)*solutionList[j]) xm=(newB.item(z)-mySum)/l.item(z,z) solutionList.append(xm) return mat(asarray(solutionList)).T
def __refactorEtaFile(self): R = sps.identity(self.m) # multiply each eta-matrix for eta in self.etaFile: R = R * eta # decompose matrix R = R.todense() P, L, U = spl.lu(R) self.LU = [L, U] self.etaFile = []
def backwards(A,b): p,l,u=s.lu(A) newB=b solutionList=[] for z in range(0,newB.shape[0]): mySum=0 for j in range(0,z): mySum=mySum+ (u.item(z,z-j)*solutionList[z-1-j]) xm=(newB.item(newB.shape[0]-1 -z)-mySum)/u.item(newB.shape[0]-1 -z,newB.shape[0]-1 -z) solutionList.append(xm) solutionList=solutionList[::-1] return mat(asarray(solutionList)).T
def lu_decomposition(a, b): """ Solve a linear equation by LU-decomposition Comes from LU decomposition of a matrix A s.t. A = LU Then LUx = b => Ux = y => Ly = b """ _, l, u = lu(a) y = solve(l, b) return solve(u, y)
def check_lin_independence(vectors): """Gram-Schmidt only applies if vectors are linearly independent. We expect this to be tha case given Stab States for a mutually unbiased basis but it's worth checking anyway.""" if len(vectors) == 1: return True M = np.zeros([len(vectors), len(vectors[0])], dtype=np.complex_) for i in range(len(vectors)): M[i] = vectors[i].T pl, u = lu(M, permute_l=True) if any([np.count_nonzero(M[i]) == 0 for i in range(len(vectors))]): return False #M must be full rank for linear independence return True
def test_zgbtrf(self): """Compare zgbtrf LU factorisation with the LU factorisation result of linalg.lu.""" M,N = shape(self.comp_mat) lu_symm_band, ipiv, info = zgbtrf(self.bandmat_comp, self.KL, self.KU) # extract matrix u from lu_symm_band u = diag(lu_symm_band[2*self.KL,:]) for i in xrange(self.KL + self.KU): u += diag(lu_symm_band[2*self.KL-1-i,i+1:N], i+1) p_lin, l_lin, u_lin =lu(self.comp_mat, permute_l=0) assert_array_almost_equal(u, u_lin)
def fringeremoval(img_list, ref_list, mask='all', method='svd'): nimgs = len(img_list) nimgsR = len(ref_list) xdim = img_list[0].shape[0] ydim = img_list[0].shape[1] if mask == 'all': bgmask = np.ones([ydim, xdim]) # around 2% OD reduction with no mask else: bgmask = mask k = (bgmask == 1).flatten(1) # needs to be >float32 since float16 doesn't work with linalg R = np.dstack(ref_list).reshape((xdim*ydim, nimgsR)).astype(np.float32) A = np.dstack(img_list).reshape((xdim*ydim, nimgs)).astype(np.float32) # Timings: for 50 ref images lasso is twice as slow # lasso 1.00 # svd 0.54 # lu 0.54 optref_list = [] for j in range(A.shape[1]): if method == 'svd': b = R[k, :].T.dot(A[k, j]) Binv = pinv(R[k, :].T.dot(R[k, :])) # svd through pinv c = Binv.dot(b) # can also try linalg.svd() elif method == 'lu': b = R[k, :].T.dot(A[k, j]) p, L, U = lu(R[k, :].T.dot(R[k, :])) c = solve(U, solve(L, p.T.dot(b))) elif method == 'lasso': lasso = Lasso(alpha=0.01) lasso.fit(R, A) c = lasso.coef_ else: raise Exception('Invalid method.') optref_list.append(np.reshape(R.dot(c), (xdim, ydim))) return optref_list
def computeDecomposition( self, A ): # first decomposition ( P, L, U ) = lu( A ) smallestIndex = findsmallestdiag( U ) smallest = U[ smallestIndex, smallestIndex ] Q = identity( self.dim, dtype=float64 ) if smallestIndex+1 != self.dim : del P del L del U # exchange smallestIndex row with dim-1 row # multiplying A by this matrix on both sides (Q.A.Q) will ensure that the # smallest element of U will be in the lower right corner. swaprow( Q, smallestIndex, self.dim-1 ) # recompute the decomposition Q = matrix( Q ) A = matrix( A ) ( P, L, U ) = lu( Q*A*Q ) return ( P, L, U, Q )
def init_traj_dist(state, action, dynamics, hyperparameters): T, du = action.shape dx = state.shape[1] K = np.zeros((T, du, dx)) k = np.zeros((T, du)) inv_covar = np.zeros((T, du, du)) covar = np.zeros((T, du, du)) dx_slice = slice(dx) du_slice = slice(dx, dx+du) # slice out actions vt = np.zeros(dx) vtt = np.zeros((dx, dx)) eta = 1e-20 ctt, ct = get_jacobian_hessian(eta, state, action, hyperparameters) Fm = dynamics.Fm fv = dynamics.fv # backward pass for t in range(T-1, -1, -1): qtt = ctt[t, :, :] + Fm[t,:,:].T.dot(vtt).dot(Fm[t,:,:]) qt = ct[t, :] + Fm[t,:, :].T.dot(vt + vtt.dot(fv[t,:])) # LU decomposition P, L, U = splinalg.lu(qtt[du_slice, du_slice]) inv_covar[t, :, :] = qtt[du_slice, du_slice] covar[t, :, :] = sp.linalg.solve_triangular( U, splinalg.solve_triangular(L, np.eye(du), lower=True) ) K[t, :, :] = -sp.linalg.solve_triangular( U, splinalg.solve_triangular(L, qtt[du_slice, dx_slice], lower=True) ) k[t, :] = -sp.linalg.solve_triangular( U, splinalg.solve_triangular(L, qt[du_slice], lower=True) ) vtt = qtt[dx_slice, dx_slice] + qtt[dx_slice, du_slice].dot(K[t, :, :]) vt = qt[dx_slice] + qtt[dx_slice, du_slice].dot(k[t,:]) vtt = 0.5* (vtt + vtt.T) return LinearGaussianPolicy(K, k, covar, inv_covar)
def __get_Qd(self): """ Compute LU decomposition of Q^T Returns: Qd: U^T of Q^T = L*U """ # strip Qmat by initial value u0 QT = self.coll.Qmat[1:,1:].T # do LU decomposition of QT [P,L,U] = LA.lu(QT,overwrite_a=True) # enrich QT by initial value u0 Qd = np.zeros(np.shape(self.coll.Qmat)) Qd[1:,1:] = U.T return Qd
def LU(self): """ Return the LU decomposition of matrix, that is matrices :math:`L` and :math:`U` such that :math:`LU = \\text{self}`. Uses the Crout decomposition method, described at http://en.wikipedia.org/wiki/Crout_matrix_decomposition The input matrix needs to be square and the decomposition is actually performed on the pivoted matrix :math:`P \\cdot self` where :math:`P = self.pivot()`. The pivoting matrix is included as the first element of the return tuple. :return: matrices **P, L, U** :rtype: *tuple(Matrix)* """ from scipy.linalg import lu P, L, U = map(Matrix, lu(self._value,permute_l=False)) return (P, L, U)
def test_lu_D(): ## Test the LU factorization of a real double precision matrix ns = 357 gA = np.random.standard_normal((ns, ns)).astype(np.float64) gA = np.asfortranarray(gA) dA = core.DistributedMatrix.from_global_array(gA, rank=0) LU, ipiv = rt.lu(dA) gLU = LU.to_global_array(rank=0) # print 'Process %d has ipiv = %s' % (rank, ipiv) if rank == 0: P, L, U = la.lu(gA) # compare with scipy result assert allclose(gLU, L + U - np.eye(ns, dtype=np.float64))
def GaussEli(a, b): N = len(b) x = np.zeros(N) (pl, u) = lu(a, permute_l=True) pl = np.mat(pl) bvec = np.reshape(np.mat(b),(N,1)) b = np.linalg.inv(pl)*bvec for i in range(N-1, -1, -1): ans = b[i]/u[i,i] for n in range(i, N-1): ans -= u[i,n+1]*x[n+1]/u[i,i] x[i] = ans return x
def _intersections(x1, y1, x2, y2): """X0,Y0 = intersections(X1,Y1,X2,Y2) INTERSECTIONS Intersections of curves. Computes the (x,y) locations where two curves intersect. The curves can be broken with NaNs or have vertical segments. Example: [X0,Y0] = intersections(X1,Y1,X2,Y2); where X1 and Y1 are equal-length vectors of at least two points and represent curve 1. Similarly, X2 and Y2 represent curve 2. X0 and Y0 are column vectors containing the points at which the two curves intersect. The algorithm can return two additional vectors that indicate which segment pairs contain intersections and where they are: [X0,Y0,I,J] = intersections(X1,Y1,X2,Y2); For each element of the vector I, I(k) = (segment number of (X1,Y1)) + (how far along this segment the intersection is). For example, if I(k) = 45.25 then the intersection lies a quarter of the way between the line segment connecting (X1(45),Y1(45)) and (X1(46),Y1(46)). Similarly for the vector J and the segments in (X2,Y2). Version: 1.10, 25 February 2008 Converted to Python October 2010 by Jeffrey Bush [email protected] Author: Douglas M. Schwarz Email: dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu Real_email = regexprep(Email,{'=','*'},{'@','.'}) Theory of operation: Given two line segments, L1 and L2, L1 endpoints: (x1(1),y1(1)) and (x1(2),y1(2)) L2 endpoints: (x2(1),y2(1)) and (x2(2),y2(2)) we can write four equations with four unknowns and then solve them. The four unknowns are t1, t2, x0 and y0, where (x0,y0) is the intersection of L1 and L2, t1 is the distance from the starting point of L1 to the intersection relative to the length of L1 and t2 is the distance from the starting point of L2 to the intersection relative to the length of L2. So, the four equations are (x1(2) - x1(1))*t1 = x0 - x1(1) (x2(2) - x2(1))*t2 = x0 - x2(1) (y1(2) - y1(1))*t1 = y0 - y1(1) (y2(2) - y2(1))*t2 = y0 - y2(1) Rearranging and writing in matrix form, [x1(2)-x1(1) 0 -1 0; [t1; [-x1(1); 0 x2(2)-x2(1) -1 0; * t2; = -x2(1); y1(2)-y1(1) 0 0 -1; x0; -y1(1); 0 y2(2)-y2(1) 0 -1] y0] -y2(1)] Let's call that A*T = B. We can solve for T with T = A\B. Once we have our solution we just have to look at t1 and t2 to determine whether L1 and L2 intersect. If 0 <= t1 < 1 and 0 <= t2 < 1 then the two line segments cross and we can include (x0,y0) in the output. In principle, we have to perform this computation on every pair of line segments in the input data. This can be quite a large number of pairs so we will reduce it by doing a simple preliminary check to eliminate line segment pairs that could not possibly cross. The check is to look at the smallest enclosing rectangles (with sides parallel to the axes) for each line segment pair and see if they overlap. If they do then we have to compute t1 and t2 (via the A\B computation) to see if the line segments cross, but if they don't then the line segments cannot cross. In a typical application, this technique will eliminate most of the potential line segment pairs. """ # x1 and y1 must be vectors with same number of points (at least 2). if sp.sum(sp.size(x1) > 1) != 1 or sp.sum( sp.size(y1) > 1) != 1 or len(x1) != len(y1): raise ValueError( 'X1 and Y1 must be equal-length vectors of at least 2 points.') # x2 and y2 must be vectors with same number of points (at least 2). if sp.sum(sp.size(x2) > 1) != 1 or sp.sum( sp.size(y2) > 1) != 1 or len(x2) != len(y2): raise ValueError( 'X2 and Y2 must be equal-length vectors of at least 2 points.') # Compute number of line segments in each curve and some differences we'll # need later. n1 = len(x1) - 1 n2 = len(x2) - 1 xy1 = sp.column_stack((x1, y1)) xy2 = sp.column_stack((x2, y2)) dxy1 = sp.diff(xy1, axis=0) dxy2 = sp.diff(xy2, axis=0) # Determine the combinations of i and j where the rectangle enclosing the # i'th line segment of curve 1 overlaps with the rectangle enclosing the # j'th line segment of curve 2. i, j = sp.nonzero( sp.logical_and( sp.logical_and( sp.logical_and( sp.tile(sp.minimum(x1[0:-1], x1[1:]), (n2, 1)).T <= sp.tile(sp.maximum(x2[0:-1], x2[1:]), (n1, 1)), sp.tile(sp.maximum(x1[0:-1], x1[1:]), (n2, 1)).T >= sp.tile(sp.minimum(x2[0:-1], x2[1:]), (n1, 1))), sp.tile(sp.minimum(y1[0:-1], y1[1:]), (n2, 1)).T <= sp.tile(sp.maximum(y2[0:-1], y2[1:]), (n1, 1))), sp.tile(sp.maximum(y1[0:-1], y1[1:]), (n2, 1)).T >= sp.tile(sp.minimum(y2[0:-1], y2[1:]), (n1, 1)))) i = sp.copy(i) # make the arrays writable j = sp.copy(j) # Find segments pairs which have at least one vertex = NaN and remove them. # This line is a fast way of finding such segment pairs. We take # advantage of the fact that NaNs propagate through calculations, in # particular subtraction (in the calculation of dxy1 and dxy2, which we # need anyway) and addition. remove = sp.isnan(sp.sum(dxy1[i, :] + dxy2[j, :], axis=1)) i[remove] = [] j[remove] = [] # Initialize matrices. We'll put the T's and B's in matrices and use them # one column at a time. AA is a 3-D extension of A where we'll use one # plane at a time. n = len(i) T = sp.zeros((4, n)) AA = sp.zeros((4, 4, n)) AA[[0, 1], 2, :] = -1 AA[[2, 3], 3, :] = -1 AA[[0, 2], 0, :] = dxy1[i, :].T AA[[1, 3], 1, :] = dxy2[j, :].T B = -sp.array([x1[i], x2[j], y1[i], y2[j]]) # Loop through possibilities. Trap singularity warning and then use # lastwarn to see if that plane of AA is near singular. Process any such # segment pairs to determine if they are colinear (overlap) or merely # parallel. That test consists of checking to see if one of the endpoints # of the curve 2 segment lies on the curve 1 segment. This is done by # checking the cross product # # (x1(2),y1(2)) - (x1(1),y1(1)) x (x2(2),y2(2)) - (x1(1),y1(1)). # # If this is close to zero then the segments overlap. for k in sp.arange(n): L, U = lin.lu(AA[:, :, k], True) T[:, k] = lin.solve(U, lin.solve(L, B[:, k])) # Find where t1 and t2 are between 0 and 1 and return the corresponding # x0 and y0 values. in_range = sp.logical_and( sp.logical_and(sp.logical_and(T[0, :] >= 0, T[1, :] >= 0), T[0, :] < 1), T[1, :] < 1) x0 = T[2, in_range].T y0 = T[3, in_range].T return x0, y0
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == RIGHT: # checks mouse right click mouseState = "clickRight" rightHold = True elif event.type == pygame.MOUSEBUTTONUP and event.button == RIGHT: # checks mouse right up mouseState = "upRight" rightHold = False else: mouseState = "nothing" if mouseState == "clickRight": if mouse_x < (width + margin) * (wSize + 1) and mouse_y < ( width + margin) * (hSize + 1): if mouse_x > (width + margin) and mouse_y > (width + margin): if grid[xPos][yPos] == 0: grid[xPos][yPos] = 1 elif grid[xPos][yPos] == 1: grid[xPos][yPos] = 0 slv = lu(gauss, permute_l=True) print(slv) if mouseState == "clickLeft": if mouse_x < (width + margin) * (wSize + 1) and mouse_y < ( width + margin) * (hSize + 1): if mouse_x > (width + margin) and mouse_y > (width + margin): light_up(xPos, yPos) # pygame clock/////////////////////////////////////////////////////////////////////////////////// clock.tick(60) pygame.display.flip()
def haroldgcd(*args): """ Takes 1D numpy arrays and computes the numerical greatest common divisor polynomial. The polynomials are assumed to be in decreasing powers, e.g. :math:`s^2 + 5` should be given as ``numpy.array([1,0,5])``. Returns a numpy array holding the polynomial coefficients of GCD. The GCD does not cancel scalars but returns only monic roots. In other words, the GCD of polynomials :math:`2` and :math:`2s+4` is still computed as :math:`1`. Parameters ---------- args : iterable A collection of 1D array_likes. Returns -------- gcdpoly : ndarray Computed GCD of args. Examples -------- >>> a = haroldgcd(*map(haroldpoly,([-1,-1,-2,-1j,1j], [-2,-3,-4,-5], [-2]*10))) >>> a array([ 1., 2.]) .. warning:: It uses the LU factorization of the Sylvester matrix. Use responsibly. It does not check any certificate of success by any means (maybe it will in the future). I have played around with ERES method but probably due to my implementation, couldn't get satisfactory results. I am still interested in better methods. """ raw_arr_args = [np.atleast_1d(np.squeeze(x)) for x in args] arr_args = [np.trim_zeros(x, 'f') for x in raw_arr_args if x.size > 0] dimension_list = [x.ndim for x in arr_args] # do we have 2d elements? if max(dimension_list) > 1: raise ValueError('Input arrays must be 1D arrays, rows, or columns') degree_list = np.array([x.size - 1 for x in arr_args]) max_degree = np.max(degree_list) max_degree_index = np.argmax(degree_list) try: # There are polynomials of lesser degree second_max_degree = np.max(degree_list[degree_list < max_degree]) except ValueError: # all degrees are the same second_max_degree = max_degree n, p, h = max_degree, second_max_degree, len(arr_args) - 1 # If a single item is passed then return it back if h == 0: return arr_args[0] if n == 0: return np.array([1]) if n > 0 and p == 0: return arr_args.pop(max_degree_index) # pop out the max degree polynomial and zero pad # such that we have n+m columns S = np.array([ np.hstack((arr_args.pop(max_degree_index), np.zeros( (1, p - 1)).squeeze())) ] * p) # Shift rows to the left for rows in range(S.shape[0]): S[rows] = np.roll(S[rows], rows) # do the same to the remaining ones inside the regular_args for item in arr_args: _ = np.array([np.hstack( (item, [0] * (n + p - item.size)))] * (n + p - item.size + 1)) for rows in range(_.shape[0]): _[rows] = np.roll(_[rows], rows) S = np.r_[S, _] rank_of_sylmat = np.linalg.matrix_rank(S) if rank_of_sylmat == min(S.shape): return np.array([1]) else: p, l, u = lu(S) u[abs(u) < 1e-8] = 0 for rows in range(u.shape[0] - 1, 0, -1): if not any(u[rows, :]): u = np.delete(u, rows, 0) else: break gcdpoly = np.real(np.trim_zeros(u[-1, :], 'f')) # make it monic gcdpoly /= gcdpoly[0] return gcdpoly
def main(): # 1--Integral 积分 from scipy.integrate import quad, dblquad, nquad print(quad(lambda x: np.exp(-x), 0, np.inf)) print(dblquad(lambda t, x: np.exp(-x*t)/t**3, 0, np.inf, lambda x: 1, lambda x: np.inf)) def f(x, y): return x*y def bound_y(): return [0, 0.5] def bound_x(y): return [0, 1-2*y] print(nquad(f, [bound_x, bound_y])) # 2 optimizer from scipy.optimize import minimize def rosen(x): return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0) x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2]) res = minimize(rosen, x0, method="nelder-mead", options={"xtol": 1e-8, "disp": True}) # xtol 表示精度 disp 表示描述条件 print("ROSE MINI:", res) # res.x 也可以打印属性 def func(x): return -(2*x[0]*x[1]+2*x[0]-x[0]**2-2*x[1]**2) def func_deriv(x): dfdx0 = -(-2 * x[0] + 2 * x[1]+2) dfdx1 = -(2 * x[0] - 4 * x[1]) return np.array([dfdx0, dfdx1]) cons = ({"type": "eq", "fun": lambda x: np.array([x[0]**3-x[1]]), "jac": lambda x: np.array([3.0 * (x[0]**2.0), -1.0])}, {"type": "ineq", 'fun': lambda x: np.array([x[1]-1]), 'jac': lambda x: np.array([0.0, 1.0])}) res = minimize(func, [-1.0, 1.0], jac=func_deriv, constraints=cons, method='SLSQP', options={'disp': True}) print("RESTRICT:", res) from scipy.optimize import root # 求根 def fun(x): return x+2*np.cos(x) sol = root(fun, 0.1) print("ROOT:", sol.x, sol.fun) # 3--Interpolation x = np.linspace(0, 1, 10) y = np.sin(2*np.pi*x) from scipy.interpolate import interp1d li = interp1d(x, y, kind="cubic") x_new = np.linspace(0, 1, 50) y_new = li(x_new) figure() plot(x, y, "r") plot(x_new, y_new, "k") # show() print(y_new) # 4--Linear 线性函数 from scipy import linalg as lg arr = np.array([[1, 2], [3,4]]) print("Det:", lg.det(arr)) print("Inv", lg.inv(arr)) b = np.array([6, 14]) print("Sol:", lg.solve(arr, b)) print("Eig:", lg.eig(arr)) print("LU:", lg.lu(arr)) print("QR:", lg.qr(arr)) print("SVD:", lg.svd(arr)) print("Schur", lg.schur(arr))
from scipy import linalg as sl import numpy as np A = np.zeros((4,4)) for i in range(np.shape(A)[0]): for j in range(np.shape(A)[0]): if i == j: A[i][j] = 2 if i == (j-1): A[i][j] = 1 if j == (i-1): A[i][j] = 1 P = sl.lu(A) U = P[2] L = P[1] P = P[0] def CalcInv(A): U = np.copy(A) I = np.identity(np.shape(A)[0]) invlist = [] invlist2 = [] h = 0 for i in np.arange(0,np.shape(U)[0]): for k in np.arange(i+1,np.shape(U)[0]): d = 0 if U[k][h] == 0:
def run_GCLP(self, composition): """Function to compute the ground state phase equilibria for a certain composition. Parameters ---------- composition : CompositionEntry Composition to be considered. Returns ------- ground_state_energy : float Ground state energy. equilibrium : dict Dictionary containing the phase composition (CompositionEntry) as key and the fractions (float) as values. Raises ------ TypeError If composition is not CompositionEntry. """ if not isinstance(composition, CompositionEntry): raise TypeError("Composition should be of type CompositionEntry!") cur_elements = composition.get_element_ids() cur_fractions = composition.get_element_fractions() # List of composition entries. components = [] # List of energies. energies = [] # Get the current possible phases (i.e., those that contain # exclusively the elements in the current compound. for entry in self.phases: this_elements = entry.get_element_ids() # Check whether this entry is in the target phase diagram. if set(this_elements) <= set(cur_elements): components.append(entry) energies.append(self.phases[entry]) # Set up constraints. # Type #1: Mass conservation. l_components = len(components) l_composition = len(cur_elements) a_eq = np.ones((l_composition + 1, l_components)) b_eq = np.ones(l_composition + 1) for i in range(l_composition): b_eq[i] = cur_fractions[i] for j in range(l_components): a_eq[i][j] = components[j].get_element_fraction( id=cur_elements[i]) # Type #2: Normalization. # Taken care of when we initialized a_eq and b_eq to ones. # Perform LU decomposition to check if there are any linearly # dependent rows in the matrix a_eq. For some reason, linprog can't # handle linearly dependent matrices. _, u = lu(a_eq, permute_l=True) mask = np.all(abs(u) < 1e-14, axis=1) indices = [i for i in range(len(mask)) if mask[i]] if indices: a_eq = np.delete(a_eq, indices, axis=0) b_eq = np.delete(b_eq, indices) c = np.array(energies) # Call LP solver and store result. res = linprog(c=c, A_eq=a_eq, b_eq=b_eq) equilibrium = {} equilibrium_fractions = res.x for i in range(l_components): if equilibrium_fractions[i] > 1e-6: equilibrium[components[i]] = equilibrium_fractions[i] # Add zero to avoid returning -0.0 values. ground_state_energy = res.fun + 0 return ground_state_energy, equilibrium
def run(self): try: while True: try: request = self.client_socket.recv(1024) calcul = json.loads(request) if calcul['operateur'] == 'DISCONNECT': break elif calcul['operateur'] == '+': a = float(calcul['operandes'][0]) b = float(calcul['operandes'][1]) self.client_socket.send(json.dumps({'result': a + b})) elif calcul['operateur'] == '-': a = float(calcul['operandes'][0]) b = float(calcul['operandes'][1]) self.client_socket.send(json.dumps({'result': a - b})) elif calcul['operateur'] == '*': a = float(calcul['operandes'][0]) b = float(calcul['operandes'][1]) self.client_socket.send(json.dumps({'result': a * b})) elif calcul['operateur'] == '/': a = float(calcul['operandes'][0]) b = float(calcul['operandes'][1]) self.client_socket.send(json.dumps({'result': a / b})) elif calcul['operateur'] == 'INV': res = numpy.linalg.inv(calcul['operandes']) matrix = [[float(x) for x in v] for v in res] response = json.dumps({'result': matrix}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'TRANS': res = numpy.transpose(calcul['operandes']) matrix = convert_numpy_matrix(res) response = json.dumps({'result': matrix}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'DET': res = numpy.linalg.det(calcul['operandes']) response = json.dumps({'result': res}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'TR': res = numpy.trace(calcul['operandes']) response = json.dumps({'result': res}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'PROD': A = calcul['operandes'][0] B = calcul['operandes'][1] if len(A[0]) != len(B): err = "Les dimensions des deux matrices ne sont pas valides pour faire la multiplication!" response = json.dumps({'error': err}) else: res = numpy.dot(A, B) matrix = convert_numpy_matrix(res) response = json.dumps({'result': matrix}) #print 'Taille de la reponse %s '%len(response) self.client_socket.send(response) elif calcul['operateur'] == 'SOM': a = numpy.array(calcul['operandes'][0]) b = numpy.array(calcul['operandes'][1]) if len(a) == len(b) and len(a[0]) == len(b[0]): res = a + b matrix = convert_numpy_matrix(res) response = json.dumps({'result': matrix}) else: err = "Les deux matrices doivent avoir les memes dimensions!" response = json.dumps({'error': err}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'DIFF': a = numpy.array(calcul['operandes'][0]) b = numpy.array(calcul['operandes'][1]) if len(a) == len(b) and len(a[0]) == len(b[0]): res = a - b matrix = convert_numpy_matrix(res) response = json.dumps({'result': matrix}) else: err = "Les deux matrices doivent avoir les memes dimensions!" response = json.dumps({'error': err}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'SL': a = numpy.array(calcul['operandes'][0]) y = numpy.array(calcul['operandes'][1]) res = numpy.linalg.solve(a, y) vector = [[float(v)] for v in res] response = json.dumps({'result': vector}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'SPEC': res = numpy.linalg.eig(calcul['operandes']) vector = [[float(v) for v in res[0]]] response = json.dumps({'result': vector}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'VP': res = numpy.linalg.eig(calcul['operandes']) w = res[1].transpose() matrix = convert_numpy_matrix(w) response = json.dumps({'result': matrix}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'CHOLESKY': res = numpy.linalg.cholesky(calcul['operandes']) matrix = convert_numpy_matrix(res) response = json.dumps({'result': matrix}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'CHOLESKY': res = numpy.linalg.cholesky(calcul['operandes']) matrix = convert_numpy_matrix(res) response = json.dumps({'result': matrix}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'LU': L, U = linalg.lu(calcul['operandes'], permute_l=True) matrix = convert_numpy_matrix(L) matrix2 = convert_numpy_matrix(U) response = json.dumps({ 'result': matrix, 'result2': matrix2 }) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) elif calcul['operateur'] == 'PUIS': matrix = puissance(calcul['operandes'][0], calcul['operandes'][1]) response = json.dumps({'result': matrix}) #print 'Taille de la reponse %s ' %len(response) #print response self.client_socket.send(response) else: self.client_socket.send( json.dumps({ 'error': "Cet operateur n'est pas encore pris en charge!" })) except numpy.linalg.linalg.LinAlgError as err: if err.message == 'Singular matrix': self.client_socket.send( json.dumps({ 'error': "La matrice saisie n'est pas inversible!" })) elif err.message == 'Matrix is not positive definite': self.client_socket.send( json.dumps({ 'error': "La matrice saisie n'est pas definie positive!" })) else: self.client_socket.send( json.dumps({'error': err.message})) except (NameError, TypeError) as err: mess = "Les valeurs saisies sont incompletes!: " + err.message self.client_socket.send(json.dumps({'error': mess})) except ZeroDivisionError: self.client_socket.send( json.dumps({ 'error': "Imposible de diviser un nombre par zero" })) except ValueError as err: self.client_socket.send( json.dumps({ 'error': "L'une des valeurs saisie est invalide!: " + err.message })) except socket.error as msg: print 'Erreur socket:%s' % msg menu() finally: self.client_socket.close()
def dot_primitive(): return scl.lu(A)
def dot_standard(): return scl.lu(B)
Ar=Ac.tocsr() print "Matrice Ar ",Ar.shape,Ar.nnz # resolution Ur=dsolve.spsolve(Ar,B,use_umfpack=True,permc_spec="MMD_ATA") print "solution min/max=",amin(Ur),amax(Ur) # tracer if (True): plt.figure(1,figsize=(12,6)) plt.subplot(1,2,1) plt.axis('equal') plt.spy(A,markersize=3) plt.title("A dim=%d non-zero=%d"%(size(A),count_nonzero(A)),fontsize=16) plt.subplot(1,2,2) plt.axis('equal') PL,U=lu(A,permute_l=True) PLU=PL+U plt.spy(PLU,markersize=3) plt.title("LU dim=%d non-zero=%d"%(size(PLU),count_nonzero(PLU)),fontsize=16) plt.draw() plt.savefig("matrixEF1.png") # plt.figure(2,figsize=(12,6)) plt.subplot(1,2,1) plt.axis('equal') As=Ar.tocsc() LUs=splu(As,permc_spec="MMD_ATA") #LUs=splu(As,permc_spec="NATURAL") Pc,Pr=Permutation(LUs.perm_c,LUs.perm_r) A2=dot(Pr,dot(A,Pc)) plt.spy(A2,markersize=3)
def randomized_range_finder(A, size, n_iter, power_iteration_normalizer='auto', random_state=None): """Computes an orthonormal matrix whose range approximates the range of A. Parameters ---------- A : 2D array The input data matrix size : integer Size of the return array n_iter : integer Number of power iterations used to stabilize the result power_iteration_normalizer : 'auto' (default), 'QR', 'LU', 'none' Whether the power iterations are normalized with step-by-step QR factorization (the slowest but most accurate), 'none' (the fastest but numerically unstable when `n_iter` is large, e.g. typically 5 or larger), or 'LU' factorization (numerically stable but can lose slightly in accuracy). The 'auto' mode applies no normalization if `n_iter`<=2 and switches to LU otherwise. .. versionadded:: 0.18 random_state : RandomState or an int seed (0 by default) A random number generator instance Returns ------- Q : 2D array A (size x size) projection matrix, the range of which approximates well the range of the input matrix A. Notes ----- Follows Algorithm 4.3 of Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions Halko, et al., 2009 (arXiv:909) http://arxiv.org/pdf/0909.4061 An implementation of a randomized algorithm for principal component analysis A. Szlam et al. 2014 """ random_state = check_random_state(random_state) # Generating normal random vectors with shape: (A.shape[1], size) Q = random_state.normal(size=(A.shape[1], size)) # Deal with "auto" mode if power_iteration_normalizer == 'auto': if n_iter <= 2: power_iteration_normalizer = 'none' else: power_iteration_normalizer = 'LU' # Perform power iterations with Q to further 'imprint' the top # singular vectors of A in Q for i in range(n_iter): if power_iteration_normalizer == 'none': Q = safe_sparse_dot(A, Q) Q = safe_sparse_dot(A.T, Q) elif power_iteration_normalizer == 'LU': Q, _ = linalg.lu(safe_sparse_dot(A, Q), permute_l=True) Q, _ = linalg.lu(safe_sparse_dot(A.T, Q), permute_l=True) elif power_iteration_normalizer == 'QR': Q, _ = linalg.qr(safe_sparse_dot(A, Q), mode='economic') Q, _ = linalg.qr(safe_sparse_dot(A.T, Q), mode='economic') # Sample the range of A using by linear projection of Q # Extract an orthonormal basis Q, _ = linalg.qr(safe_sparse_dot(A, Q), mode='economic') return Q
def randomized_range_finder(A, *, size, n_iter, power_iteration_normalizer='auto', random_state=None): """Computes an orthonormal matrix whose range approximates the range of A. Parameters ---------- A : 2D array The input data matrix. size : int Size of the return array. n_iter : int Number of power iterations used to stabilize the result. power_iteration_normalizer : {'auto', 'QR', 'LU', 'none'}, default='auto' Whether the power iterations are normalized with step-by-step QR factorization (the slowest but most accurate), 'none' (the fastest but numerically unstable when `n_iter` is large, e.g. typically 5 or larger), or 'LU' factorization (numerically stable but can lose slightly in accuracy). The 'auto' mode applies no normalization if `n_iter` <= 2 and switches to LU otherwise. .. versionadded:: 0.18 random_state : int, RandomState instance or None, default=None The seed of the pseudo random number generator to use when shuffling the data, i.e. getting the random vectors to initialize the algorithm. Pass an int for reproducible results across multiple function calls. See :term:`Glossary <random_state>`. Returns ------- Q : ndarray A (size x size) projection matrix, the range of which approximates well the range of the input matrix A. Notes ----- Follows Algorithm 4.3 of Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions Halko, et al., 2009 (arXiv:909) https://arxiv.org/pdf/0909.4061.pdf An implementation of a randomized algorithm for principal component analysis A. Szlam et al. 2014 """ random_state = check_random_state(random_state) # Generating normal random vectors with shape: (A.shape[1], size) Q = random_state.normal(size=(A.shape[1], size)) if A.dtype.kind == 'f': # Ensure f32 is preserved as f32 Q = Q.astype(A.dtype, copy=False) # Deal with "auto" mode if power_iteration_normalizer == 'auto': if n_iter <= 2: power_iteration_normalizer = 'none' else: power_iteration_normalizer = 'LU' # Perform power iterations with Q to further 'imprint' the top # singular vectors of A in Q for i in range(n_iter): if power_iteration_normalizer == 'none': Q = safe_sparse_dot(A, Q) Q = safe_sparse_dot(A.T, Q) elif power_iteration_normalizer == 'LU': Q, _ = linalg.lu(safe_sparse_dot(A, Q), permute_l=True) Q, _ = linalg.lu(safe_sparse_dot(A.T, Q), permute_l=True) elif power_iteration_normalizer == 'QR': Q, _ = linalg.qr(safe_sparse_dot(A, Q), mode='economic') Q, _ = linalg.qr(safe_sparse_dot(A.T, Q), mode='economic') # Sample the range of A using by linear projection of Q # Extract an orthonormal basis Q, _ = linalg.qr(safe_sparse_dot(A, Q), mode='economic') return Q
# matrixEx.py import numpy as np from scipy.linalg import lu a= np.mat('1, 2: 3, 2: 2, 3') a= np.mat('1, 2; 3, 2; 2, 3') # define matrix aT= np.transpose(a) b=2*np.eye(3) c = b*a l= lu(a) print(l)
def compute_and_plot_specrad(): """ Compute and plot spectral radius of smoother iteration matrix for a whole range of eigenvalues """ # setup_list = [('LU', 'to0'), ('LU', 'toinf'), ('IE', 'to0'), ('IE', 'toinf')] # setup_list = [('LU', 'to0'), ('LU', 'toinf')] # setup_list = [('IE', 'to0'), ('IE', 'toinf')] # setup_list = [('LU', 'toinf'), ('IE', 'toinf')] setup_list = [('IE', 'full'), ('LU', 'full')] # setup_list = [('EX', 'to0'), ('PIC', 'to0')] # set up plotting parameters params = { 'legend.fontsize': 24, 'figure.figsize': (12, 8), 'axes.labelsize': 24, 'axes.titlesize': 24, 'xtick.labelsize': 24, 'ytick.labelsize': 24, 'lines.linewidth': 3 } plt.rcParams.update(params) Nnodes = 3 Nsteps = 4 coll = CollGaussRadau_Right(Nnodes, 0, 1) Qmat = coll.Qmat[1:, 1:] Nmat = np.zeros((Nnodes, Nnodes)) Nmat[:, -1] = 1 Emat = np.zeros((Nsteps, Nsteps)) np.fill_diagonal(Emat[1:, :], 1) for qd_type, conv_type in setup_list: if qd_type == 'LU': QT = coll.Qmat[1:, 1:].T [_, _, U] = LA.lu(QT, overwrite_a=True) QDmat = U.T elif qd_type == 'IE': QI = np.zeros(np.shape(coll.Qmat)) for m in range(coll.num_nodes + 1): QI[m, 1:m + 1] = coll.delta_m[0:m] QDmat = QI[1:, 1:] elif qd_type == 'EE': QE = np.zeros(np.shape(coll.Qmat)) for m in range(coll.num_nodes + 1): QE[m, 0:m] = coll.delta_m[0:m] QDmat = QE[1:, 1:] elif qd_type == 'PIC': QDmat = np.zeros(np.shape(coll.Qmat[1:, 1:])) elif qd_type == 'EX': QT = coll.Qmat[1:, 1:].T [_, _, U] = LA.lu(QT, overwrite_a=True) QDmat = np.tril(U.T, k=-1) print(QDmat) else: raise NotImplementedError('qd_type %s is not implemented' % qd_type) # lim_specrad = max(abs(np.linalg.eigvals(np.eye(Nnodes) - np.linalg.inv(QDmat).dot(Qmat)))) # print('qd_type: %s -- lim_specrad: %6.4e -- conv_type: %s' % (qd_type, lim_specrad, conv_type)) if conv_type == 'to0': ilim_left = -4 ilim_right = 2 rlim_left = 2 rlim_right = -4 elif conv_type == 'toinf': ilim_left = 0 ilim_right = 11 rlim_left = 6 rlim_right = 0 elif conv_type == 'full': ilim_left = -10 ilim_right = 11 rlim_left = 10 rlim_right = -11 else: raise NotImplementedError('conv_type %s is not implemented' % conv_type) ilam_list = 1j * np.logspace(ilim_left, ilim_right, 201) rlam_list = -1 * np.logspace(rlim_left, rlim_right, 201) assert (rlim_right - rlim_left + 1) % 5 == 0 assert (ilim_right - ilim_left - 1) % 5 == 0 assert (len(rlam_list) - 1) % 5 == 0 assert (len(ilam_list) - 1) % 5 == 0 Prho = np.zeros((len(rlam_list), len(ilam_list))) for idr, rlam in enumerate(rlam_list): for idi, ilam in enumerate(ilam_list): dxlam = rlam + ilam mat = np.linalg.inv( np.eye(Nnodes * Nsteps) - dxlam * np.kron(np.eye(Nsteps), QDmat)).dot( dxlam * np.kron(np.eye(Nsteps), (Qmat - QDmat)) + np.kron(Emat, Nmat)) mat = np.linalg.matrix_power(mat, Nnodes) Prho[idr, idi] = max(abs(np.linalg.eigvals(mat))) print(np.amax(Prho)) fig, ax = plt.subplots(figsize=(15, 10)) ax.set_xticks([ i + 0.5 for i in range(0, len(rlam_list), int(len(rlam_list) / 5)) ]) ax.set_xticklabels([ r'-$10^{%d}$' % i for i in range( rlim_left, rlim_right, int((rlim_right - rlim_left + 1) / 5)) ]) ax.set_yticks([ i + 0.5 for i in range(0, len(ilam_list), int(len(ilam_list) / 5)) ]) ax.set_yticklabels([ r'$10^{%d}i$' % i for i in range( ilim_left, ilim_right, int((ilim_right - ilim_left - 1) / 5)) ]) cmap = plt.get_cmap('Reds') pcol = plt.pcolor(Prho.T, cmap=cmap, norm=LogNorm(vmin=1E-09, vmax=1E-00)) plt.colorbar(pcol) plt.xlabel(r'$Re(\Delta t\lambda)$') plt.ylabel(r'$Im(\Delta t\lambda)$') fname = 'data/heatmap_smoother_' + conv_type + '_Nsteps' + str(Nsteps) + '_M' + \ str(Nnodes) + '_' + qd_type + '.png' plt.savefig(fname, rasterized=True, transparent=True, bbox_inches='tight')
#### #### Matrix Elimination and LU Decomposition import numpy as np from scipy.linalg import lu # Input a n*n array as the determinant you want to analysis. D = np.array([[4, 1, 5, 4, 3], [3, 4, 6, 2, 5], [2, 7, 5, 1, 3], [1, 6, 4, 3, 6], [2, 1, 6, 5, 4]]) # LU Decomposition pl, U = lu(D, permute_l=True) print(U) # [[ 4. 1. 5. 4. 3. ] # [ 0. 6.5 2.5 -1. 1.5 ] # [ 0. 0. 3.30769231 3.07692308 2.38461538] # [ 0. 0. 0. 2.38372093 3.53488372] # [ 0. 0. 0. 0. 3.4 ]]
def main(): #1--Integral print(quad(lambda x: np.exp(-x), 0, np.inf)) #jifen 1~wuqiong print( dblquad( lambda t, x: np.exp(-x * t) / t**3, 0, np.inf, #t de fan wei lambda x: 1, lambda x: np.inf)) #erweijifen x de fanwei def f(x, y): return x * y def bound_y(): return [0, 0.5] def bound_x(y): return [0, 1 - 2 * y] print(nquad(f, (bound_x, bound_y))) #n weijifen # 2--Optimizer def rosen(x): return sum(100.0 * (x[1:] - x[:1]**2.0)**2.0 + (1 - x[:1])**2.0) x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2]) res = minimize(rosen, x0, method="nelder-mead", options={ "xtol": 1e-8, "disp": True }) print("ROSE MINI:", res.x) # def func(x): # return (2*x[0]*x[1]+2*x[0]-x[0]**2-2*x[1]**2) # def func_deriv(x): # dfdx0 = (-2*x[0]+2*x[1]+2) # dfdx1 = (2*x[0]-4*x[1]) # return np.array([dfdx0,dfdx1]) # cons=({"type":"eq","fun":lambda x:np.array([x[0]**3-x[1]]),"jac":lambda x:np.array([3.0*(x[0]**2.0,-1.0)])}, # {"type": "ineq", "fun": lambda x: np.array([x[1] - 1]), "jac": lambda x: np.array([0.0,1.0])}) # res = minimize(func,np.array([-1.0,1.0]),jac=func_deriv,constraints=cons,method='SLSQP',options={'disp':True}) # print("RESTRICT:",res) def fun(x): return x + 2 * np.cos(x) sol = root(fun, 0.1) print("ROOT:", sol.x, sol.fun) #3--Inter x = np.linspace(0, 1, 10) y = np.sin(2 * np.pi * x) li = interp1d(x, y, kind="cubic") x_new = np.linspace(0, 1, 50) y_new = li(x_new) figure() plot(x, y, "r") plot(x_new, y_new, "k") show() print(y_new) #4--Linear arr = np.array([[1, 2], [3, 4]]) print("Det:", lg.det(arr)) print("Inv:", lg.inv(arr)) b = np.array([6, 14]) print("Sol:", lg.solve(arr, b)) print("Eig:", lg.eig(arr)) print("LU:", lg.lu(arr)) print("QR:", lg.qr(arr)) print("SVD:", lg.svd(arr)) print("Schur:", lg.schur(arr))
import numpy as np from scipy.linalg import lu A = np.array([[6, 2, 3], [1, 1, 1], [0, 4, 9]]) P, L, U = lu(A) nswaps = len(np.diag(P)) - np.sum(np.diag(P)) - 1 detP = (-1)**nswaps detL = np.prod(np.diag(L)) detU = np.prod(np.diag(U)) print(detP * detL * detU) print(np.linalg.det(A)) print('job done')
def optimal_y(self, a_hist, t=None): """ - if t is NOT given it takes a_hist (list or numpy.array) as a deterministic a_t - if t is given, it solves the combined control prediction problem (section 7) (by default, t == None -> deterministic) for a given sequence of a_t (either deterministic or a particular realization), it calculates the optimal y_t sequence using the method of the lecture Note: ------ scipy.linalg.lu normalizes L, U so that L has unit diagonal elements To make things consistent with the lecture, we need an auxiliary diagonal matrix D which renormalizes L and U """ N = np.asarray(a_hist).shape[0] - 1 W, W_m = self.construct_W_and_Wm(N) L, U = la.lu(W, permute_l=True) D = np.diag(1 / np.diag(U)) U = D @ U L = L @ np.diag(1 / np.diag(D)) J = np.fliplr(np.eye(N + 1)) if t is None: # if the problem is deterministic a_hist = J @ np.asarray(a_hist).reshape(N + 1, 1) #-------------------------------------------- # Transform the 'a' sequence if β is given #-------------------------------------------- if self.β != 1: a_hist = a_hist * (self.β**(np.arange(N + 1) / 2))[::-1].reshape(N + 1, 1) a_bar = a_hist - W_m @ self.y_m # a_bar from the lecture Uy = np.linalg.solve(L, a_bar) # U @ y_bar = L^{-1} y_bar = np.linalg.solve(U, Uy) # y_bar = U^{-1}L^{-1} # Reverse the order of y_bar with the matrix J J = np.fliplr(np.eye(N + self.m + 1)) y_hist = J @ np.vstack([y_bar, self.y_m ]) # y_hist : concatenated y_m and y_bar #-------------------------------------------- # Transform the optimal sequence back if β is given #-------------------------------------------- if self.β != 1: y_hist = y_hist * (self.β**(-np.arange(-self.m, N + 1) / 2)).reshape(N + 1 + self.m, 1) return y_hist, L, U, y_bar else: # if the problem is stochastic and we look at it Ea_hist = self.predict(a_hist, t).reshape(N + 1, 1) Ea_hist = J @ Ea_hist a_bar = Ea_hist - W_m @ self.y_m # a_bar from the lecture Uy = np.linalg.solve(L, a_bar) # U @ y_bar = L^{-1} y_bar = np.linalg.solve(U, Uy) # y_bar = U^{-1}L^{-1} # Reverse the order of y_bar with the matrix J J = np.fliplr(np.eye(N + self.m + 1)) y_hist = J @ np.vstack([y_bar, self.y_m ]) # y_hist : concatenated y_m and y_bar return y_hist, L, U, y_bar
def compute_and_plot_specrad(Nnodes, lam): """ Compute and plot the spectral radius of the smoother for different step-sizes Args: Nnodes: number of collocation nodes lam: test parameter representing the spatial problem """ coll = CollGaussRadau_Right(Nnodes, 0, 1) Qmat = coll.Qmat[1:, 1:] # do LU decomposition of QT (St. Martin's trick) QT = coll.Qmat[1:, 1:].T [_, _, U] = LA.lu(QT, overwrite_a=True) QDmat = U.T Nmat = np.zeros((Nnodes, Nnodes)) Nmat[:, -1] = 1 Nsteps_list = [64, 256] color_list = ['red', 'blue'] marker_list = ['s', 'o'] setup_list = zip(Nsteps_list, color_list, marker_list) xlist = [0.1 ** i for i in range(11)] rc('font', **{"sans-serif": ["Arial"], "size": 24}) rc('legend', fontsize='small') rc('xtick', labelsize='small') rc('ytick', labelsize='small') plt.subplots(figsize=(15, 10)) for Nsteps, color, marker in setup_list: Emat = np.zeros((Nsteps, Nsteps)) np.fill_diagonal(Emat[1:, :], 1) Prho_list = [] predict_list = [] for x in xlist: mat = np.linalg.inv(np.eye(Nnodes * Nsteps) - x * lam * np.kron(np.eye(Nsteps), QDmat)).dot( x * lam * np.kron(np.eye(Nsteps), (Qmat - QDmat)) + np.kron(Emat, Nmat)) Prho_list.append(max(abs(np.linalg.eigvals(mat)))) # predict_list.append((1 + x) ** (1.0 - 1.0 / (Nnodes * Nsteps)) * x ** (1.0 / (Nnodes * Nsteps))) predict_list.append(x ** (1.0 / (Nsteps))) if len(predict_list) > 1: print(x, predict_list[-1], Prho_list[-1], Prho_list[-2] / Prho_list[-1], predict_list[-2] / predict_list[-1]) plt.loglog(xlist, Prho_list, linestyle='-', linewidth=3, color=color, marker=marker, markersize=10, label='spectral radius, L=' + str(Nsteps)) plt.loglog(xlist, [item for item in predict_list], linestyle='--', linewidth=2, color=color, marker=marker, markersize=10, label='estimate, L=' + str(Nsteps)) ax = plt.gca() ax.invert_xaxis() plt.xlabel('time-step size') plt.ylabel('spectral radius') plt.legend(loc=3, numpoints=1) plt.grid() plt.ylim([1E-02, 1E01]) if type(lam) is complex: fname = 'data/smoother_specrad_to0_L64+256_M' + str(Nnodes) + 'LU_imag.png' else: fname = 'data/smoother_specrad_to0_L64+256_M' + str(Nnodes) + 'LU_real.png' plt.savefig(fname, rasterized=True, transparent=True, bbox_inches='tight')
def main(): #--Integral from scipy.integrate import quad,dblquad,nquad print(quad(lambda x:np.exp(-x),0,np.inf)) print(dblquad(lambda t, x:np.exp(-x*t)/t**3,0,np.inf,lambda x:1, lambda x:np.inf)) def f(x,y): return x*y def bound_y(): return [0,0.5] def bound_x(y): return [0,1-2*y] print("NQUAD",nquad(f,[bound_x,bound_y])) #2--Optimizer from scipy.optimize import minimize def rosen(x): return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0+(1-x[:-1])**2.0) x0=np.array([1,3,0.7,0.8,1.9,1.2]) res=minimize(rosen,x0,method="nelder-mead",options={"xtol":1e-8,"disp":True}) print("ROSE MINI:",res) #求出函数全局的最小值,可看属性res.x def func(x): return (2*x[0]*x[1]+2*x[0]-x[0]**2-2*x[1]**2) def func_deriv(x): dfdx0 =(-2*x[0]+2*x[1]+2) dedx1 =(2*x[0]-4*x[1]) return np.array([dedx0,dedx1]) # cons = # 优化器,拉格朗日(条件极值),雅可比矩阵,求根(未知数值) from scipy.optimize import root def fun(x): return x+2*np.cos(x) sol=root(fun,0.1) print("ROOT:",sol.x,sol.fun) def fun_simple(x): return x+3 sol=root(fun,0.5) print("ROOT:",sol.x,sol.fun) #3--Interpolation插值 x=np.linspace(0,1,10) y=np.sin(2*np.pi*x) # You used interpld, i.e. INTERPLD. # You want interp1d, i.e. with the numeral 1, for one-dimensional. from scipy.interpolate import interp1d li=interp1d(x,y,kind='cubic') x_new=np.linspace(0,1,50) y_new=li(x_new) figure() plot(x,y,'r') #红色 plot(x_new,y_new,'k') #黑色 print(y_new) #4--Linear from scipy import linalg as lg arr = np.array([[1,2],[3,4]]) print("Det:",lg.det(arr)) print("Inv:",lg.inv(arr)) b=np.array([6,14]) print("Sol:",lg.solve(arr,b)) print("Eig:",lg.eig(arr)) print("LU:",lg.lu(arr)) print("QR:",lg.qr(arr)) print("SVD:",lg.svd(arr)) print("Schur:",lg.schur(arr))
plt.show() #------ b ------ A_inv = np.linalg.inv(A) z = np.matmul(A_inv, y) p = np.reshape(z, (28, 28)) plt.imshow(p, interpolation='nearest', cmap='Greys') plt.axis('off') plt.title('Question 11(b)') plt.show() #------ c ------ y = np.matmul(A, x) P, L, U = sp.lu(A, permute_l=False) Py = np.matmul(y, P) s = sp.solve_triangular(L, Py, lower=True) z = sp.solve_triangular(U, s) p = np.reshape(z, (28, 28)) plt.imshow(p, interpolation='nearest', cmap='Greys') plt.axis('off') plt.title('Question 11(c)') plt.show() #------ d ------
print("2nd column of A = ") print(A[:, 1]) print("flat list (column vector) of matrix elements of A: ") print(A.flatten()) print("Horizontal concatenation of A with itself: ") print(np.concatenate((A, A), axis=1)) print("Vertical concatenation of A with itself: ") print(np.concatenate((A, A), axis=0)) print("diagonal matrix: ") a1 = np.arange(10, 0, -1) # count backward, note endpoint excluded print(np.diag(a1)) print("LU decomposition of A (A=L*U): ") [P, L, U] = sla.lu(A) print("L = ") print(L) print("U = ") print(U) print("Eigenvalues of A = ") [lam, psi] = sla.eig(A) #note output args swapped from matlab print(lam) print("Eigenvectors of A = ") print(psi[:, 0]) print(psi[:, 1]) print(psi[:, 2])
target = 7.0 print "Searching array for " + str( target) + " using np.searchsorted() function " idx = np.searchsorted(arr, target) print "Return result = " + str(idx) print "" m = np.matrix([[2., 4., 3., 5.], [7., 2., 8., 9.], [1., 0., 3., 1.], [3., 6., 2., 4.]]) print "Matrix m is " print m print "" print "Performing LU decomposition on m using scipy.linalg.lu() " (perm, low, upp) = spla.lu(m) print "" print "Result lower decomposed matrix is " print low print "" arr = np.array([2.2, 5.5, 1.1, 4.4, 3.3]) med = np.median(arr) print "Median of " + str(arr) + " using np.median() is " + str(med) print "" print "Making 100 Normal values with mean = 5.0 and sd = 1.0 using normal() " # set seed here if you want to get reproducible results np.random.seed(0) values = np.random.normal(5.0, 1.0, 100)
#Symbolic approach A = sympy.Matrix([[3, 2], [1, -2]]) b = sympy.Matrix([3, 5]) print(A.rank()) print(A.condition_number()) sympy.N(1) print(A.norm()) L, U, _ = A.LUdecomposition() print(L) print(U) print(L * U) x = A.solve(b) print(x) ###Numerical approach A = np.array([[3, 2], [1, -2]]) b = np.array([3, 5]) print(np.linalg.matrix_rank(A)) print(np.linalg.cond(A)) print(np.linalg.norm(A)) P, L, U = la.lu(A) print(L) print(U) print(L * U) print(la.solve(A, b))
# Rozwiązanie dla układu z macierzą trójprzekątniową z przekątną, która nie jest dominująca elif test == 4: # macierz trójprzekątniowa z przekątną, która nie jest dominująca A = np.array([[1.0, 3.0, 0.0, 0.0], [2.0, 3.0, 4.0, 0.0], [0.0, 4.0, -2.0, -1.0], [0.0, 0.0, 8.0, 6.0]]) eq_no = 4 flaga = 0 print "Macierz A\n", A if (flaga == 0): #Rozwiązanie #dekompozycja LU z wykorzystaniem funkcji, P - macierz jednostkowa, permutacji P, L, U = sp.lu(A) print "\nmacierz P:" print P print "\nmacierz L:" print L print "\nmacierz U:" print U #wymiary macierzy permutacji dlugosc = len(P) b = [] for dl in range(0, dlugosc): b.append(0)
def __call__(self): p, l, u = lu(self.C) return p, l, u
#--------------Q7--------------- #------a------ I = np.identity(A.shape[0]) topHalf = np.hstack((I,A)) O = np.zeros((A.shape[1],A.shape[1])) bottomHalf = np.hstack((A.T,O)) AugM = np.vstack((topHalf,bottomHalf)) #------b------ P,L,U = sp.lu(AugM, permute_l=False) o = np.zeros(5) bo = np.hstack((b,o)) Pb = np.matmul(P,bo) s = sp.solve_triangular(L,Pb, lower = True) rx = sp.solve_triangular(U,s) print("\n\n\n")
def compute_ref_LUP_fast(u, threshold): """ Function for computing Row Echelon Form of u which is produced from LUP factorization. Parameters __________ threshold: (Float) See documentation of the function "sort_rows_fast" Returns __________ u[order]: (2D float64 numpy array) Modified u array that is in Row Echelon Form. """ _, sorted_scores, order = sort_rows_fast(u, threshold, remove_zero_columns_rows=False) rows, cols = u.shape halt = False i, j = 0, 0 in_ref = True not_ref_pos = [-1, -1] last_row = False while not halt: while (-threshold <= u[order[i], j] <= threshold) or last_row: in_ref = True if not_ref_pos[0] != -1: start, end = not_ref_pos _, u_prime = lu(u[order[start:end + 1], j:], permute_l=True) # u_prime = u_prime / np.abs(u_prime).max() # sorted_scores_t = np.ones(end - start + 1, dtype=np.uint64) * cols order_t = np.arange(end - start + 1, dtype=np.uint64) u_sorted_t, sorted_scores_t[:u_prime.shape[ 0]], order_t[:u_prime.shape[0]] = sort_rows_fast( u_prime, threshold, remove_zero_columns_rows=False, adder=j) idxs_1, idxs_2 = order[start:start + u_prime.shape[0]], order[ start + u_prime.shape[0]:end + 1] u[idxs_1, j:] = u_sorted_t u[idxs_2, j:] = 0 sorted_scores[start:], order[start:] = merge_sorted_arrays( sorted_scores_t, sorted_scores[end + 1:], np.append(idxs_1, idxs_2)[order_t], order[end + 1:]) i = start not_ref_pos[0], not_ref_pos[1] = -1, -1 last_row = False continue if j == cols - 1 or last_row: halt = True break j += 1 if not in_ref: if not_ref_pos[0] == -1: not_ref_pos[0] = i - 1 not_ref_pos[1] = i i += 1 in_ref = False if i == rows: last_row = True i -= 1 return u[order]
for i in range(10): for j in range(10): if i == j: np.fill_diagonal(A, 2, wrap=True) elif i == j + 1: A[i, j] = -1 elif i == j - 1: A[i, j] = -1 x = np.linspace(0, 1, n) f = 100 * np.exp(-10 * x) * h**2 #our given function for i in range(1, n): f[i] = f[i] + (i - 1) * f[i - 1] / i P, L, U = lu(A) #calculating the LU decompsition L_inv = np.linalg.inv(L) y = L_inv @ f * h**2 U_inv = np.linalg.inv(U) x = U_inv @ y print(x) print("--- %s seconds ---" % (time.time() - start_time)) A = np.zeros((100, 100), int) for i in range(10): for j in range(10):
A = np.array([[2, 1, 1], [1, 3, 2], [1, 0, 0]]) B = np.array([4, 5, 6]) print(np.linalg.solve(A, B)) #------------------------------------ """LU decomposition with Scipy""" import scipy.linalg as linalg import numpy as np A = np.array([[2., 1., 1.], [1., 3., 2.], [1., 0., 0.]]) B = np.array([4., 5., 6.]) LU = linalg.lu_factor(A) x = linalg.lu_solve(LU, B) print(x) #---------------------- P, L, U = linalg.lu(A) list(L) list(U) #---------------------------------- """Cholesky decomposition with Numpy""" import numpy as np A = np.array([[10., -1., 2., 0.], [-1., 11., -1., 3.], [2., -1., 10., -1.], [0.0, 3., -1., 8.]]) B = np.array([6., 25., -11., 15.]) L = np.linalg.cholesky(A) print(L) print(np.dot(L, L.T.conj())) y = np.linalg.solve(L, B) x = np.linalg.solve(L.T.conj(), y) print(x)