def contribution(self, v): if self.dxdt is None: if len(v.shape) == 0: return pascal.dot(v, v*0) else: return pascal.dot(v, v[0]*0) else: return pascal.dot(v, self.dxdt) / pascal.dot(self.dxdt, self.dxdt)
def checkpoint(self, V, v): #Q, R = pascal.qr(V.T) #b = pascal.dot(Q.T, v) #V[:] = Q.T #v -= pascal.dot(Q, b) Q, R = pascal.qr_transpose(V) b = pascal.dot(Q, v) #V[:] = Q #v -= pascal.dot(b, Q) V = Q v = v - pascal.dot(b, Q) self.Rs.append(R) self.bs.append(b) return V, v
def checkpoint(self, V, v): #Q, R = pascal.qr(V.T) #b = pascal.dot(Q.T, v) #V[:] = Q.T #v -= pascal.dot(Q, b) Q, R = pascal.qr_transpose(V) b = pascal.dot(Q, v) #V[:] = Q #v -= pascal.dot(b, Q) V = Q v = v - pascal.dot(b, Q) self.Rs.append(R) self.bs.append(b) return V, v
def test_linalg(): for mod in list(sys.modules.keys()): if mod.startswith('pascal_lite'): del sys.modules[mod] import pascal_lite as pascal subspace_dimension = 4 V = pascal.symbolic_array(subspace_dimension) v = pascal.symbolic_array() v1 = pascal.dot(V, v) assert not v1.is_distributed v2 = pascal.outer(v1, v) assert v2.is_distributed v3 = pascal.qr_transpose(V)[0] assert v3.is_distributed g = pascal.ComputationalGraph([v1.value, v2.value, v3.value]) n = 16 A = np.random.rand(subspace_dimension, n) b = np.random.rand(n) def actual_inputs(x): if x is V.value: return A elif x is v.value: return b o1, o2, o3 = g(actual_inputs) assert np.allclose(o1, np.dot(A, b)) assert np.allclose(o2, np.outer(np.dot(A, b), b)) assert np.allclose(o3, np.linalg.qr(A.T)[0].T)
def project(self, v): if self.dxdt_normalized is None: return v else: dv = pascal.outer(pascal.dot(v, self.dxdt_normalized), self.dxdt_normalized) return v - dv.reshape(v.shape)