def random_naub_pos(self): a = Vec2d(350, 0) b = Vec2d(0, 220) if random() < 0.5: a,b = b,a if random() < 0.5: b = -b return random_vec(a.x, a.y) + b
def spam_naub_pair(self): pos = self.random_naub_pos() rot = random() * math.pi * 2 a, b = self.create_naub_pair(pos, rot) impulse = lambda: random_vec(50, 50) a.body.apply_impulse(impulse()) b.body.apply_impulse(impulse()) a.color = self.random_naub_color() b.color = self.random_naub_color() self.add_naubs(a, b)
def slq(A, step, nv, f): """ input: A : sparse positive semi-definite matrix (real symmetric) step : nv : f : output: tr(f(L)) """ N = A.shape[0] sum_of_gauss_quadrature = 0 for l in range(nv): x = random_vec(N) z = normalize_vec(x) T, _ = lanczos(A, z, step) w, vs = LA.eigh(T) # if T is not symmetric, use eig(T) ts = vs[0] # frist elements of eigenvectors sum_of_gauss_quadrature += np.dot((ts**2), f(w)) # sum(t**2 f(eigenval)) return (N / nv) * sum_of_gauss_quadrature
elif Gtype == "adjacency": L = nx.adjacency_matrix(G) def f(x): return np.power(x, k) return slq(L.astype(np.float64), step, nv, f) if __name__ == "__main__": # ---- generate matrix A n = 4 step = 100 sqrtA = np.random.rand(n, n) - 0.5 A = np.dot(sqrtA, np.transpose(sqrtA)) print("A:") print(A) A = csr_matrix(A) x = random_vec(n) z = normalize_vec(x) T, V = lanczos(A, z, step) print("T:") print(T) print("V:") print(V) print("V.T A V") V = csr_matrix(V) print(V.T.dot(A).dot(V).toarray())