我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用scipy.sparse.bmat()。
def _compute_jacobian(self, J_eq, J_ineq, s): if self.n_ineq == 0: return J_eq else: if spc.issparse(J_eq) or spc.issparse(J_ineq): # It is expected that J_eq and J_ineq # are already `csr_matrix` because of # the way ``BoxConstraint``, ``NonlinearConstraint`` # and ``LinearConstraint`` are defined. J_eq = spc.csr_matrix(J_eq) J_ineq = spc.csr_matrix(J_ineq) return self._assemble_sparse_jacobian(J_eq, J_ineq, s) else: S = np.diag(s) zeros = np.zeros((self.n_eq, self.n_ineq)) # Convert to matrix if spc.issparse(J_ineq): J_ineq = J_ineq.toarray() if spc.issparse(J_eq): J_eq = J_eq.toarray() # Concatenate matrices return np.asarray(np.bmat([[J_eq, zeros], [J_ineq, S]]))
def homogeneous_form(self): return sp.bmat([[self.P, self.q/2], [self.q.T/2, self.r]]) # Returns QuadraticFunction f1, f2 such that # f(x) = f1(x) - f2(x), with f1 and f2 both convex. # Affine and constant components are always put into f1.
def _assemble_sparse_jacobian(self, J_eq, J_ineq, s): """Assemble sparse jacobian given its components. Given ``J_eq``, ``J_ineq`` and ``s`` returns: jacobian = [ J_eq, 0 ] [ J_ineq, diag(s) ] It is equivalent to: spc.bmat([[ J_eq, None ], [ J_ineq, diag(s) ]], "csr") but significantly more efficient for this given structure. """ n_vars, n_ineq, n_eq = self.n_vars, self.n_ineq, self.n_eq J_aux = spc.vstack([J_eq, J_ineq], "csr") indptr, indices, data = J_aux.indptr, J_aux.indices, J_aux.data new_indptr = indptr + np.hstack((np.zeros(n_eq, dtype=int), np.arange(n_ineq+1, dtype=int))) size = indices.size+n_ineq new_indices = np.empty(size) new_data = np.empty(size) mask = np.full(size, False, bool) mask[new_indptr[-n_ineq:]-1] = True new_indices[mask] = n_vars+np.arange(n_ineq) new_indices[~mask] = indices new_data[mask] = s new_data[~mask] = data J = spc.csr_matrix((new_data, new_indices, new_indptr), (n_eq + n_ineq, n_vars + n_ineq)) return J
def factor_aug(z, DPhival, G, A): M, N = G.shape P, N = A.shape """Multiplier for inequality constraints""" l = z[N+P:N+P+M] """Slacks""" s = z[N+P+M:] """Sigma matrix""" SIG = diags(l/s, 0) """Condensed system""" if issparse(DPhival): if not issparse(A): A = csr_matrix(A) H = DPhival + mydot(G.T, mydot(SIG, G)) J = bmat([[H, A.T], [A, None]]) else: if issparse(A): A = A.toarray() J = np.zeros((N+P, N+P)) J[0:N, 0:N] = DPhival + mydot(G.T, mydot(SIG, G)) J[0:N, N:] = A.T J[N:, 0:N] = A LU = myfactor(J) return LU
def _sparse_block_diag(mats, format=None, dtype=None): """An implementation of scipy.sparse.block_diag since old versions of scipy don't have it. Forms a sparse matrix by stacking matrices in block diagonal form. Parameters ---------- mats : list of matrices Input matrices. format : str, optional The sparse format of the result (e.g. "csr"). If not given, the matrix is returned in "coo" format. dtype : dtype specifier, optional The data-type of the output matrix. If not given, the dtype is determined from that of blocks. Returns ------- res : sparse matrix """ nmat = len(mats) rows = [] for ia, a in enumerate(mats): row = [None] * nmat row[ia] = a rows.append(row) return sparse.bmat(rows, format=format, dtype=dtype)
def parametrize_volume(machina, singular_vertices, h): """Parametrize the volume as an atlas of maps based on the 3d frame field. Returns the discretized uvw map atlas (vertex-based).""" # Each vertex has multiple values, depending # on the number of tets it's a part of. ne = len(machina.tet_mesh.elements) # Minimum spanning tree of dual mesh as list of face indices. # Span until all tets have been visited. ti = 0 mst_edges = set() visited_tets = set() while ti < len(machina.tet_mesh.elements): for neigh_ti in machina.tet_mesh.neighbors[ti]: if neigh_ti in visited_tets or neigh_ti == -1: continue # Get face index from s-t tet pair. fi = machina.dual_edges[frozenset([ti, neigh_ti])] mst_edges.add(fi) visited_tets.add(ti) ti += 1 print('Computing laplacian and constraints...') # Create linear system based on laplacian and constraints. laplacian, cons = linear_system(machina, mst_edges, singular_vertices) n_cons = cons.get_shape()[0] A = sparse.bmat(([[laplacian, cons.transpose()],[cons, None]]), dtype=np.int32) # Discrete frame divergence. b = np.zeros((12*ne + n_cons)) for ti in range(ne): tet_vol = tet_volume(machina.tet_mesh, ti) frame = machina.frames[ti] div = [ np.sum(frame.uvw[:,0]), np.sum(frame.uvw[:,1]), np.sum(frame.uvw[:,2]) ] b[12*ti : 12*(ti+1)] = np.hstack([ div for _ in range(4)]) b = np.divide(b, h) print("Conjugate Gradient... (Round 1)", end=" ") sys.stdout.flush() x, info = sparse.linalg.cg(A, b, tol = 1e-4) say_ok() print('Adaptive rounding...') # uvw_map = adaptive_rounding(machina, A, x, b, singular_vertices) uvw_map = x return uvw_map