def __init__(self, src, dst, projection=None, inclusion=None, integral=None): self._src = src self._dst = dst self._projection = None self._inclusion = None self._homotopy = None if projection is not None: self._projection = projection else: self._projection = ChainMap(self._src, self._dst) if inclusion is not None: self._inclusion = inclusion else: self._inclusion = ChainMap(self._dst, self._src) if integral is not None: self._homotopy = integral else: self._homotopy = ChainMap(self._src, self._dst, 1)
def SNF_reduction(self): while True: self._computedSNF = False self.computeSNF() all_diag = True for p in self.dimensions: T = np.zeros_like(self.D[p]) T[:min(self.D[p].shape), :min(self.D[p].shape)] = np.diag(np.diag(self.D[p])) all_diag = all_diag and np.all(T == self.D[p]) if not all_diag: continue if all_diag: break f_A = ChainMap(self, matrices={q: self.A[q].T for q in self.dimensions}) f_G = ChainMap(self, matrices={q: self.G[q].T for q in self.dimensions}) f_D = ChainMap(self, matrices={q: self.D[q].T for q in self.dimensions}, degree=self._degree) M = ChainComplex(cell_class=self.cell_class) M._degree = +1 for q in self.dimensions: M[q] = self[q] M.d = f_D return Reduction( src=self, dst=M, projection=f_G, inclusion=f_A, integral=ChainMap(self, degree=-self._degree) )
def chain_complex(self): result = ChainComplex(cell_class=self.cell_class) for q in self.dimensions: result[q] = tuple(cell for cell in self(q)) diff = ChainMap(result, result, degree=-1) for cell in self: if cell.dim > 0: diff.set_image(cell, cell.differential()) result.d = diff return result
def diagonal(self): M, f, g = self.dst, self.projection, self.inclusion M2 = M @ M D = ChainMap(M, M2) for cell in M: aw = AW(g(cell)) f_aw = sum((aw[cell] * (f(cell[0]) @ f(cell[1])) for cell in aw), Chain()) D.set_image(cell, f_aw) return D
def from_am_model(cls, am_model): H = am_model.dst d = ChainMap(H, degree=+1, matrices={(q - 1): mat.T for (q, mat) in H.d._matrices.items()}) free = { q: [cell for cell in H[q] if d(cell) == 0] for q in H.dimensions } tor = {q: [] for q in H.dimensions} for q in H.dimensions: for cell in H[q - 1]: delta = d(cell) if delta != 0: for face in delta: l = delta[face] tor[q].append((l, face)) free[q].remove(face) objects = {} generators = {} for q in free: rank = len(free[q]) torsion_list = tuple(abs(t[0]) for t in tor[q]) objects[q] = FiniteGeneratedAbelianGroup(rank, torsion_list) generators[q] = tuple(free[q]) + tuple(t[1] for t in tor[q]) return CohomologyGroups(objects, generators)
def cup_product(self): D = self.diagonal() M2 = D.dst M = D.src cup = ChainMap(M2, M) for cell in M2: t_0, t_1 = cell[0], cell[1] c_0, c_1 = Chain(t_0), Chain(t_1) p = t_0.dim q = t_1.dim result = Chain() for s in M[p + q]: d = D(s) result += sum(d[c] * c_0(c[0]) * c_1(c[1]) for c in d) * s cup.set_image(cell, result) return cup
def test_chain_map(): local_env = {"a": 1, "b": 2} global_env = {"a": 3, "c": 4} chain_map = ChainMap(local_env, global_env) assert 'a' in chain_map assert 'c' in chain_map assert chain_map['a'] == 1 assert chain_map['b'] == 2 assert chain_map['c'] == 4
def chain_complex(self): result = ChainComplex(cell_class=CubicalCell) for q in self.dimensions: result[q] = tuple(cell for cell in self(q)) matrices = {} for q in range(1, self.dim + 1): src = np.array([cell.cube_map for cell in result[q]]).astype(np.int32) dst = np.array([cell.cube_map for cell in result[q - 1]]).astype(np.int32) threadsperblock = (16, 16) blockspergrid_src = math.ceil(src.shape[0] / threadsperblock[0]) blockspergrid_dst = math.ceil(dst.shape[0] / threadsperblock[1]) blockspergrid = (blockspergrid_src, blockspergrid_dst) diff = np.zeros((dst.shape[0], src.shape[0]), dtype=np.int32) create_cubical_differential_array[blockspergrid, threadsperblock](src, dst, diff) matrices[q] = diff result.d = ChainMap(result, result, degree=-1, matrices=matrices) return result
def __matmul__(self, other): assert isinstance(other, self.__class__) result = ChainComplex(cell_class=TensorCell) dimensions = {} for i, j in product(self.dimensions, other.dimensions): if i + j in dimensions: dimensions[i + j].append((i, j)) else: dimensions[i + j] = [(i, j)] for p in dimensions: result[p] = tuple(map(lambda l: l[0] @ l[1], chain(*[product(self[i], other[j]) for i, j in dimensions[p]]))) d = ChainMap(result, degree=-1) # for cell1, cell2 in product(self, other): # t = cell1 @ cell2 # if t.dim > 0: # d_t = t.differential() # d.set_image(t, d_t) result.d = d return result
def reduction(self): cplx = self.decomposition() d = self.src.d pi_t, pi_s, pi_c, iota_t, iota_s, iota_c = self._projections_inclusions(cplx) d_33 = pi_c * d * iota_c d_31 = pi_c * d * iota_t d_21 = pi_s * d * iota_t d_21_inv = ChainMap(d_21.dst, d_21.src, degree=+1) for p in self.dimensions: d_21_inv[p] = np.linalg.inv(d_21[p + 1]).astype(np.int32) d_23 = pi_s * d * iota_c cplx['c'].d = d_33 - d_31 * d_21_inv * d_23 f = pi_c - d_31 * d_21_inv * pi_s g = iota_c - iota_t * d_21_inv * d_23 h = iota_t * d_21_inv * pi_s return Reduction(self.src, cplx['c'], f, g, h)
def id(self): return ChainMap.identity(self)
pi_c = {p: np.zeros((n, C.n[p]), np.int32) for p, n in n_c.items()} for p, m in m_s.items(): for i, j in enumerate(m): pi_s[p][i, j] = 1 for p, m in m_t.items(): for i, j in enumerate(m): pi_t[p][i, j] = 1 for p, m in m_c.items(): for i, j in enumerate(m): pi_c[p][i, j] = 1 cplx_s = ChainComplex(C.cell_class) for p, m in m_s.items(): cplx_s[p] = tuple(C[p][i] for i in m) cplx_t = ChainComplex(C.cell_class) for p, m in m_t.items(): cplx_t[p] = tuple(C[p][i] for i in m) cplx_c = ChainComplex(C.cell_class) for p, m in m_c.items(): cplx_c[p] = tuple(C[p][i] for i in m) pi_s_cm = ChainMap(C, cplx_s, matrices=pi_s) pi_t_cm = ChainMap(C, cplx_t, matrices=pi_t) pi_c_cm = ChainMap(C, cplx_c, matrices=pi_c) print('V Decomposition calculated in {:.3f}s'.format(time() - st))
from chain import ChainLink import sys from Selector import Selector from chain_map import ChainMap import getopt opts, args = getopt.getopt(sys.argv[1:], "i:m:") opts = dict(opts) my_index = opts.get("-i") if my_index is not None: my_index = int(my_index) map_file = opts["-m"] map = ChainMap(map_file) sel = Selector() class CallBack(object): def messageConfirmed(self, msg): print("Message confirmed: %s" % (msg,)) def messageReceived(self, msg): print("Message received: %s" % (msg,)) cs = ChainLink(my_index, map.chainMap(), CallBack(), sel) cs.broadcast("Hello all", need_confirmation=True) while True:
def am_model(self, do_SNF=True, verbose=False): st = time() pt = time() reduction = self.reduction() if verbose: print('Vector field reduction calculated in {:.3f}s'.format(time() - pt)) pt = time() V = create_vector_field(reduction.dst) if verbose: print('Reduced vector field built in {:.3f}s'.format(time() - pt)) while V != 0: pt = time() reduction = V.reduction() * reduction if verbose: print('Vector field reduction calculated in {:.3f}s'.format(time() - pt)) pt =time() V = create_vector_field(reduction.dst) if verbose: print('Reduced vector field built in {:.3f}s'.format(time() - pt)) if do_SNF: # Compute SNF if necessary if reduction.dst.d != 0: pt = time() snf_reduction = reduction.dst.SNF_reduction() if verbose: print('SNF reduction calculated in {:.3f}s'.format(time() - pt)) pt = time() reduction = snf_reduction * reduction if verbose: print('Final reduction built in {:.3f}s'.format(time() - pt)) # Make all differentials have positive coefficient pt = time() f_sign = ChainMap(reduction.dst) h_sign = ChainMap(reduction.dst, degree=1) for sigma in reduction.dst: co_d = reduction.dst.d.T c = co_d(sigma) if c: tau = next(iter(c)) if c[tau] < 0: f_sign.set_image(sigma, -sigma) else: f_sign.set_image(sigma, sigma) else: f_sign.set_image(sigma, sigma) new_dst = ChainComplex(cell_class=reduction.dst.cell_class) for p in reduction.dst.dimensions: new_dst[p] = reduction.dst[p] new_d = ChainMap(new_dst, degree=-1) for p, M in reduction.dst.d.matrices.items(): new_d[p] = np.abs(M) new_dst.d = new_d reduction = Reduction(reduction.dst, new_dst, f_sign, f_sign, h_sign) * reduction if verbose: print('Signs adjusted in {:.3f}s'.format(time() - pt)) print('Full time {:.3f}s'.format(time() - pt)) return reduction
def _projections_inclusions(self, decomposition): cplx = decomposition target_projection = ChainMap(self.src, cplx['t']) source_projection = ChainMap(self.src, cplx['s']) critical_projection = ChainMap(self.src, cplx['c']) target_inclusion = ChainMap(cplx['t'], self.src) source_inclusion = ChainMap(cplx['s'], self.src) critical_inclusion = ChainMap(cplx['c'], self.src) for sigma in self.src: if self.is_source(sigma): source_projection.set_image(sigma, sigma) source_inclusion.set_image(sigma, sigma) elif self.is_target(sigma): target_projection.set_image(sigma, sigma) target_inclusion.set_image(sigma, sigma) else: critical_projection.set_image(sigma, sigma) critical_inclusion.set_image(sigma, sigma) return (target_projection, source_projection, critical_projection, target_inclusion, source_inclusion, critical_inclusion)