def __init__(self, npc_matvec, leg, dtype, charge_sector=0, vec_label=None, compact_flat=None): self.npc_matvec = npc_matvec self.leg = leg self.possible_charge_sectors = leg.charge_sectors() self.shape = (leg.ind_len, leg.ind_len) self.dtype = dtype if compact_flat is None: compact_flat = charge_sector is not None and leg.is_blocked() elif compact_flat: if not leg.is_blocked(): raise ValueError( "FlatLinearOperator with `compact_flat` works only " "for blocked `leg`.") self.compact_flat = compact_flat self.vec_label = vec_label self.matvec_count = 0 self._charge_sector = None self._mask = None self.charge_sector = charge_sector # uses the setter self._npc_matvec_multileg = None self._labels_split = None ScipyLinearOperator.__init__(self, self.dtype, self.shape)
def __init__(self, adjacency: Union[sparse.csr_matrix, np.ndarray], regularization: float = 0.): LinearOperator.__init__(self, dtype=float, shape=adjacency.shape) self.regularization = regularization self.weights = adjacency.dot(np.ones(adjacency.shape[1])) self.laplacian = sparse.diags(self.weights, format='csr') - adjacency
def __init__(self, n, c): """n is dimension, c is a constant to be multiplied by the identity matrix.""" self.c = c if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype = float, shape = (n, n)) else: LinearOperator.__init__(self, dtype = float, shape = (n, n), matvec = lambda x : type(self)._matvec(self, x))
def __init__(self, D): """D is a 1D array containing the diagonal entries.""" self.D = D if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype = float, shape = (len(D), len(D))) else: LinearOperator.__init__(self, dtype = float, shape = (len(D), len(D)), matvec = lambda x : type(self)._matvec(self, x))
def __init__(self, F, Delta, u): n = F.shape[0] assert (F.shape[1] == n) and (u.shape == (n,)) self.F, self.Delta, self.u = F, Delta, u self.u_prime = self.Delta - self.u self.ones = np.ones(n, dtype=float) LinearOperator.__init__(self, dtype=float, shape=self.F.shape)
def __init__(self, u, v): assert (len(u) == len(v)) self.u, self.v = u, v if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype = float, shape = (len(u), len(v))) else: LinearOperator.__init__(self, dtype = float, shape = (len(u), len(v)), matvec = lambda x : type(self)._matvec(self, x))
def __init__(self, block_grid): """Input is a 2D list of SparseLinearOperators. The resulting operator is the corresponding operator comprised of these operator blocks. The dimensions must match correctly. This assumes the number of blocks in each row and column is the same.""" self.block_grid_shape = (len(block_grid), len(block_grid[0])) # validate block dimensions assert all( [len(row) == self.block_grid_shape[1] for row in block_grid] ), "Must be same number of blocks in each row." assert all( [ len(set([block_grid[i][j].shape[0] for j in range(self.block_grid_shape[1])])) == 1 for i in range(self.block_grid_shape[0]) ] ), "dimension mismatch" assert all( [ len(set([block_grid[i][j].shape[1] for i in range(self.block_grid_shape[0])])) == 1 for j in range(self.block_grid_shape[1]) ] ), "dimension mismatch" shape = ( sum([block_grid[i][0].shape[0] for i in range(len(block_grid))]), sum([block_grid[0][j].shape[1] for j in range(len(block_grid[0]))]), ) # compute transition indices between blocks self.row_indices = [0] + list(np.cumsum([row[0].shape[0] for row in block_grid])) self.column_indices = [0] + list(np.cumsum([block.shape[1] for block in block_grid[0]])) self.block_grid = block_grid LinearOperator.__init__(self, dtype=float, shape=shape)
def __init__(self, A, coarsening=pyamgcl_ext.coarsening.smoothed_aggregation, relaxation=pyamgcl_ext.relaxation.spai0, prm={} ): """ Class constructor. Creates algebraic multigrid hierarchy. Parameters ---------- A : the system matrix in scipy.sparse format coarsening : {ruge_stuben, aggregation, *smoothed_aggregation*, smoothed_aggr_emin} The coarsening type to use for construction of the multigrid hierarchy. relaxation : {damped_jacobi, gauss_seidel, chebyshev, *spai0*, ilu0} The relaxation scheme to use for multigrid cycles. prm : dictionary with amgcl parameters """ Acsr = A.tocsr() self.P = pyamgcl_ext.make_preconditioner( coarsening, relaxation, prm, Acsr.indptr.astype(numpy.int32), Acsr.indices.astype(numpy.int32), Acsr.data.astype(numpy.float64) ) LinearOperator.__init__(self, A.shape, self.P)
def __init__(self, adjacency: sparse.csr_matrix, damping_factor: float = 0.85, personalization=None, fb_mode: bool = False, verbose: bool = False): VerboseMixin.__init__(self, verbose) n1, n2 = adjacency.shape restart_prob: np.ndarray = restart_probability(n1, personalization) if fb_mode: restart_prob = np.hstack((restart_prob, np.zeros(n2))) adjacency = bipartite2undirected(adjacency) LinearOperator.__init__(self, shape=adjacency.shape, dtype=float) n = adjacency.shape[0] out_degrees = adjacency.dot(np.ones(n)) damping_matrix = damping_factor * sparse.eye(n, format='csr') if fb_mode: damping_matrix.data[n1:] = 1 self.a = (damping_matrix.dot(transition_matrix(adjacency))).T.tocsr() self.b = (np.ones(n) - damping_factor * out_degrees.astype(bool)) * restart_prob
def __init__(self, A): assert isinstance(A, SymmetricSparseLinearOperator) self.A = A if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype = float, shape = A.shape) else: LinearOperator.__init__(self, dtype = float, shape = A.shape, matvec = lambda x : type(self)._matvec(self, x)) self.D_ratio = self.A._matvec(np.ones(self.A.shape[1], dtype = float)) / self.shape[0]
def __init__(self, shape, matvec): self._shape = shape self._action = matvec LinearOperator.__init__(self, shape=self._shape, matvec=self.parallelDot, dtype=np.complex128)
def __init__(self, shape, matvec): self._shape = shape self._action = matvec LinearOperator.__init__(self, shape=self._shape, matvec=self.parallelDot, dtype=np.complex128)
def __init__(self, u): self.u = u if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype=float, shape=(len(u), len(u))) else: LinearOperator.__init__( self, dtype=float, shape=(len(u), len(u)), matvec=lambda x: type(self)._matvec(self, x))
def __init__(self, K, quad, **kwargs): assert len(K.shape) == 1 shape = (K.shape[0]-4, K.shape[0]-4) N = shape[0] LinearOperator.__init__(self, shape, None, **kwargs) ck = ones(K.shape) if quad == "GC": ck[N-1] = 2 self.dd = -4*pi*(K[:N]+1)/(K[:N]+3)*(K[:N]+2)**2 self.ud = [2*pi*(K[:N-2]+1)*(K[:N-2]+2)] self.ld = [2*pi*(K[2:N]-1)*(K[2:N]+2)]
def __init__(self, adjacency: Union[sparse.csr_matrix, np.ndarray], regularization: float = 0.): LinearOperator.__init__(self, dtype=float, shape=adjacency.shape) self.adjacency = adjacency self.regularization = regularization n = self.adjacency.shape[0] self.sqrt_weights = np.sqrt( self.adjacency.dot(np.ones(n)) + self.regularization * n)
def __init__(self, F, Delta, u): n = F.shape[0] assert ((F.shape[1] == n) and (u.shape == (n,))) self.F, self.Delta, self.u = F, Delta, u self.u_prime = self.Delta - self.u self.ones = np.ones(n, dtype = float) if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype = float, shape = self.F.shape) else: LinearOperator.__init__(self, dtype = float, shape = self.F.shape, matvec = lambda x : type(self)._matvec(self, x))
def __init__(self, K, quad, **kwargs): assert len(K.shape) == 1 shape = (K.shape[0]-4, K.shape[0]-4) N = shape[0] LinearOperator.__init__(self, shape, None, **kwargs) ck = ones(K.shape) if quad == "GC": ck[N-1] = 2 self.dd = -4*pi*(K[:N]+1)/(K[:N]+3)*(K[:N]+2)**2 self.ud = [2*pi*(K[:N-2]+1)*(K[:N-2]+2)] self.ld = [2*pi*(K[2:N]-1)*(K[2:N]+2)]
def __init__(self, D): """D is a 1D array containing the diagonal entries.""" self.D = D if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype=float, shape=(len(D), len(D))) else: LinearOperator.__init__( self, dtype=float, shape=(len(D), len(D)), matvec=lambda x: type(self)._matvec(self, x))
def __init__(self, K, **kwargs): assert len(K.shape) == 1 shape = (K.shape[0]-4, K.shape[0]-4) N = shape[0] LinearOperator.__init__(self, shape, None, **kwargs) self.dd = 8*pi*(K[:N]+1)**2*(K[:N]+2)*(K[:N]+4) self.ud = [] for i in range(2, N, 2): self.ud.append(8*pi*(K[:-(i+4)]+1)*(K[:-(i+4)]+2)*(K[:-(i+4)]*(K[:-(i+4)]+4)+3*(arange(i, N)+2)**2)/((arange(i, N)+3))) self.ld = None
def __init__(self, u, v): assert (len(u) == len(v)) self.u, self.v = u, v if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype=float, shape=(len(u), len(v))) else: LinearOperator.__init__( self, dtype=float, shape=(len(u), len(v)), matvec=lambda x: type(self)._matvec(self, x))
def __init__(self, n, c): """n is dimension, c is a constant to be multiplied by the identity matrix.""" self.c = c if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype=float, shape=(n, n)) else: LinearOperator.__init__( self, dtype=float, shape=(n, n), matvec=lambda x: type(self)._matvec(self, x))
def __init__(self, K, **kwargs): assert len(K.shape) == 1 shape = (K.shape[0]-4, K.shape[0]-4) N = shape[0] LinearOperator.__init__(self, shape, None, **kwargs) self.dd = 8*pi*(K[:N]+1)**2*(K[:N]+2)*(K[:N]+4) self.ud = [] for i in range(2, N, 2): self.ud.append(8*pi*(K[:-(i+4)]+1)*(K[:-(i+4)]+2)*(K[:-(i+4)]*(K[:-(i+4)]+4)+3*(arange(i, N)+2)**2)/((arange(i, N)+3))) self.ld = None
def __init__(self, A): assert isinstance(A, SymmetricSparseLinearOperator) self.A = A self.D = self.A.matvec(np.ones(self.A.shape[1], dtype=float)) if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype=float, shape=A.shape) else: LinearOperator.__init__( self, dtype=float, shape=A.shape, matvec=lambda x: type(self)._matvec(self, x))
def __init__(self, K, quad, **kwargs): assert len(K.shape) == 1 shape = (K.shape[0]-4, K.shape[0]-4) N = shape[0] LinearOperator.__init__(self, shape, None, **kwargs) ck = ones(K.shape) if quad == "GC": ck[N-1] = 2 self.dd = pi/2*(ck[:-4] + 4*(K[:N]+2)**2/(K[:N]+3)**2 + (K[:N]+1)**2/(K[:N]+3)**2) self.ud = [-pi*((K[:N-2]+2)/(K[:N-2]+3) + (K[:N-2]+4)/(K[:N-2]+5)*(K[:N-2]+1)/(K[:N-2]+3)), pi/2*(K[:N-4]+1)/(K[:N-4]+3)] self.ld = [pi/2*(K[:N-4]+1)/(K[:N-4]+3), -pi*((K[:N-2]+2)/(K[:N-2]+3) + (K[:N-2]+4)/(K[:N-2]+5)*(K[:N-2]+1)/(K[:N-2]+3))]
def __init__(self, K, quad, **kwargs): assert len(K.shape) == 1 shape = (K.shape[0]-4, K.shape[0]-4) N = shape[0] LinearOperator.__init__(self, shape, None, **kwargs) ck = ones(K.shape) if quad == "GC": ck[N-1] = 2 self.dd = pi/2*(ck[:-4] + 4*(K[:N]+2)**2/(K[:N]+3)**2 + (K[:N]+1)**2/(K[:N]+3)**2) self.ud = [-pi*((K[:N-2]+2)/(K[:N-2]+3) + (K[:N-2]+4)/(K[:N-2]+5)*(K[:N-2]+1)/(K[:N-2]+3)), pi/2*(K[:N-4]+1)/(K[:N-4]+3)] self.ld = [pi/2*(K[:N-4]+1)/(K[:N-4]+3), -pi*((K[:N-2]+2)/(K[:N-2]+3) + (K[:N-2]+4)/(K[:N-2]+5)*(K[:N-2]+1)/(K[:N-2]+3))]
def __init__(self, npc_matvec, leg, dtype, charge_sector=0, vec_label=None): self.npc_matvec = npc_matvec self.leg = leg self.possible_charge_sectors = leg.charge_sectors() self.shape = (leg.ind_len, leg.ind_len) self.dtype = dtype self.vec_label = vec_label self.matvec_count = 0 self._charge_sector = None self._mask = None self.charge_sector = charge_sector # uses the setter self._npc_matvec_multileg = None self._labels_split = None ScipyLinearOperator.__init__(self, self.dtype, self.shape)
def __init__(self, F, Delta, u): n = F.shape[0] assert ((F.shape[1] == n) and (u.shape == (n, ))) self.F, self.Delta, self.u = F, Delta, u self.u_prime = self.Delta - self.u self.ones = np.ones(n, dtype=float) if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype=float, shape=self.F.shape) else: LinearOperator.__init__( self, dtype=float, shape=self.F.shape, matvec=lambda x: type(self)._matvec(self, x))
def __init__(self, mat, r=10, leaf_side=16): LinearOperator.__init__(self, dtype=mat.dtype, shape=mat.shape, matvec=self._matvec, rmatvec=self._rmatvec) self.mat = BlackBox(mat) self.r = r self.leaf_side = leaf_side self.leaf_size = leaf_side**2 N = int(np.sqrt(self.mat.shape[0])) self.pattern = distance_matrix(N, format='coo') perm = hilbert_traverse(N) conjugate_sparse(self.pattern, perm) self.pattern = self.pattern.tocsr() self.mat.permutate(perm) self.root = hmat_node(self, tuple(zip((0, 0), self.mat.shape))) return
def __init__(self, A, prm={}): """ Class constructor. Creates algebraic multigrid hierarchy. Parameters ---------- A : the system matrix in scipy.sparse format prm : dictionary with amgcl parameters """ Acsr = A.tocsr() self.P = pyamgcl_ext.make_preconditioner( Acsr.indptr, Acsr.indices, Acsr.data, prm) if [int(v) for v in scipy.__version__.split('.')] < [0, 16, 0]: LinearOperator.__init__(self, A.shape, self.P) else: LinearOperator.__init__(self, dtype=numpy.float64, shape=A.shape)
def __init__(self, mat, r=10, leaf_side=16): LinearOperator.__init__(self, dtype=mat.dtype, shape=mat.shape, matvec=self._matvec, rmatvec=self._rmatvec) self.mat = BlackBox(mat) self.r = r self.leaf_side = leaf_side self.leaf_size = leaf_side**2 N = int(np.sqrt(self.mat.shape[0])) self.pattern = distance_matrix(N, format='coo') perm = hilbert_traverse(N) conjugate_sparse(self.pattern, perm) self.pattern = self.pattern.tocsr() self.mat.permutate(perm) self.root = hmat_node(self, tuple(zip((0, 0), self.mat.shape))) return
def __init__(self, A, pmask, prm={}): """ Class constructor. Parameters ---------- A : the system matrix in scipy.sparse format prm : dictionary with amgcl parameters """ Acsr = A.tocsr() self.P = pyamgcl_ext.make_simple(prm, Acsr.indptr.astype(numpy.int32), Acsr.indices.astype(numpy.int32), Acsr.data.astype(numpy.float64), pmask.astype(numpy.int32)) if [int(v) for v in scipy.__version__.split('.')] < [0, 16, 0]: LinearOperator.__init__(self, A.shape, self.P) else: LinearOperator.__init__(self, dtype=numpy.float64, shape=A.shape)
def __init__(self,shape,n_blk): self.n_iter = 0 self.shape = shape LinearOperator.__init__(self,self.shape,self.mult) self.pc = [] self.size_blk = [] self.n_blk = n_blk assert shape[0] == shape[1], \ "Prec Shape Must be squared, %r rows not equal to %r cols" % (shape[0],shape[1]) for i in range(0,n_blk): row = (i+1)*[False] #for j in range(0,i+1): # row.append([]) self.pc.append(row) for i in range(0,n_blk): self.size_blk.append([]) return
def __init__(self, block_grid): """Input is a 2D list of SparseLinearOperators. The resulting operator is the corresponding operator comprised of these operator blocks. The dimensions must match correctly. This assumes the number of blocks in each row and column is the same.""" self.block_grid_shape = (len(block_grid), len(block_grid[0])) # validate block dimensions assert all([ len(row) == self.block_grid_shape[1] for row in block_grid ]), "Must be same number of blocks in each row." assert all([ len( set([ block_grid[i][j].shape[0] for j in range(self.block_grid_shape[1]) ])) == 1 for i in range(self.block_grid_shape[0]) ]), "dimension mismatch" assert all([ len( set([ block_grid[i][j].shape[1] for i in range(self.block_grid_shape[0]) ])) == 1 for j in range(self.block_grid_shape[1]) ]), "dimension mismatch" shape = (sum([ block_grid[i][0].shape[0] for i in range(len(block_grid)) ]), sum([block_grid[0][j].shape[1] for j in range(len(block_grid[0]))])) # compute transition indices between blocks self.row_indices = [0] + list( np.cumsum([row[0].shape[0] for row in block_grid])) self.column_indices = [0] + list( np.cumsum([block.shape[1] for block in block_grid[0]])) self.block_grid = block_grid if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype=float, shape=shape) else: LinearOperator.__init__( self, dtype=float, shape=shape, matvec=lambda x: type(self)._matvec(self, x))
def __init__(self, A, pmask, prm={}): """ Class constructor. Parameters ---------- A : the system matrix in scipy.sparse format prm : dictionary with amgcl parameters """ Acsr = A.tocsr() self.P = pyamgcl_ext.make_simple( prm, Acsr.indptr.astype(numpy.int32), Acsr.indices.astype(numpy.int32), Acsr.data.astype(numpy.float64), pmask.astype(numpy.int32) ) if [int(v) for v in scipy.__version__.split('.')] < [0, 16, 0]: LinearOperator.__init__(self, A.shape, self.P) else: LinearOperator.__init__(self, dtype=numpy.float64, shape=A.shape)
def __init__(self,shape): LinearOperator.__init__(self,shape,self.full) self.n_iter = 0
def __init__(self, A): assert isinstance(A, SymmetricSparseLinearOperator) self.A = A LinearOperator.__init__(self, dtype=float, shape=A.shape) self.D_ratio = self.A._matvec(np.ones(self.A.shape[1], dtype=float)) / self.shape[0]
def __init__(self, u, v): assert len(u) == len(v) self.u, self.v = u, v LinearOperator.__init__(self, dtype=float, shape=(len(u), len(v)))
def __init__(self, u): self.u = u LinearOperator.__init__(self, dtype=float, shape=(len(u), len(u)))
def __init__(self, D): """D is a 1D array containing the diagonal entries.""" self.D = D LinearOperator.__init__(self, dtype=float, shape=(len(D), len(D)))
def __init__(self, n, c): """n is dimension, c is a constant to be multiplied by the identity matrix.""" self.c = c LinearOperator.__init__(self, dtype=float, shape=(n, n))
def __init__(self,shape): LinearOperator.__init__(self,shape,self.mult) return
def __init__(self,A): LinearOperator.__init__(self,A.shape,self.mult) self.A = A return
def __init__(self, u): self.u = u if LINOP_SUBCLASSING: LinearOperator.__init__(self, dtype = float, shape = (len(u), len(u))) else: LinearOperator.__init__(self, dtype = float, shape = (len(u), len(u)), matvec = lambda x : type(self)._matvec(self, x))
def __init__(self,shape): LinearOperator.__init__(self,shape,self.block_diagonal) self.n_iter = 0
def __init__(self,shape): LinearOperator.__init__(self,shape,self.upper_tri) self.n_iter = 0