def __init__(self, K, Y, init=None, threshold=1e-9): N = np.shape(K)[0] f = np.zeros((N,1)) converged = False k = 0 innerC = 0 for i in xrange(N): pdfDiff = norm.logpdf(f) - norm.logcdf(Y*f) W = np.exp(2*pdfDiff) + Y*f*np.exp(pdfDiff) Wsqrt = np.sqrt(W) Wdiag= np.diag(Wsqrt.flatten()) B = np.identity(N) + np.dot(Wdiag, np.dot(K, Wdiag)) grad = Y*np.exp(pdfDiff) b = W*f + grad interim = np.dot(Wdiag, np.dot(K, b)) cgRes = Cg(B, interim, threshold=threshold) s1 = cgRes.result innerC = innerC + cgRes.iterations a = b - Wsqrt*s1 if(converged): break f_prev = f f = np.dot(K, a) diff = f - f_prev if (np.dot(diff.T,diff).flatten() < threshold*N or innerC>15000): converged = True k = k+1 self.result = f self.iterations = k + innerC
def applyBoundaryConditions(self, bFunc, cFunc): bcs = {} nt = len(self.boundaryPoints) for i0 in range(nt): x0, y0 = self.boundaryPoints[i0] i1 = (i0 + 1) % nt x1, y1 = self.boundaryPoints[i1] x, y = 0.5 * (x0 + x1), 0.5 * (y0 + y1) b, c = eval(bFunc), eval(cFunc) bcs[i0, i1] = (b, c) self.ellipt.applyBoundaryConditions(bcs) self.slvr = Cg(self.ellipt.getStiffnessMatrix(), \ self.ellipt.getSourceVector())
def __init__(self, K, Y, P, init=None, threshold=1e-9, innerThreshold=1e-9): mMax = 15 N = np.shape(K)[0] if init is None: init = np.zeros((N, 1)) self.K = K self.P = P self.Y = Y.flatten() x = init r_prev = np.zeros((N, 1)) r = Y - np.dot(self.K, x) p = np.zeros(6000, dtype=object) k = 0 innerC = 0 while True: if (np.dot(r.T, r).flatten() < threshold * N or k > 50000): break interim = Cg(P, r, threshold=innerThreshold) z = interim.result count = interim.iterations innerC = innerC + count if (k == 0): p[k] = z else: m = max(1, k % (mMax + 1)) sum = 0 if (k - m < 0): start = 0 else: start = k - m for i in xrange((k - m), k): frac = np.dot(z.T, np.dot(self.K, p[i])) / np.dot( p[i].T, np.dot(self.K, p[i])) sum = sum + frac * p[i] p[k] = z - sum alpha = np.dot(p[k].T, r) / np.dot(p[k].T, np.dot(self.K, p[k])) x = x + alpha * p[k] r_prev = r r = r - alpha * np.dot(K, p[k]) k = k + 1 self.outer_iterations = k self.result = x self.iterations = innerC + k
def __init__(self, K, Y, P, init=None, threshold=1e-9, innerThreshold=1e-9): N = np.shape(K)[0] if init is None: init = np.zeros(N) self.K = K self.P = P self.Y = Y.flatten() x = init r_prev = np.zeros(N) r = self.Y - np.dot(self.K, x) p = np.zeros(6000,dtype=object) k = 0 innerC = 0 while True: diff = r - r_prev if (np.dot(diff.T,diff).flatten() < threshold) or k>5000: break interim = Cg(P, r, threshold=innerThreshold) z = interim.result count = interim.iterations innerC = innerC + count if (k == 0): p[k] = z else: sum = 0 for i in xrange(k): frac = np.dot(z.T,np.dot(self.K,p[i]))/np.dot(p[i].T, np.dot(self.K, p[i])) sum = sum + np.dot(frac, p[i]) p[k] = z - sum alpha = np.dot(p[k].T, r) / np.dot(p[k].T, np.dot(self.K, p[k])) x = x + np.dot(alpha,p[k]) r_prev = r r = r - np.dot(alpha, np.dot(K, p[k])) k = k + 1 self.result = x self.iterations = innerC + k
class App(object): def run(self): self.window = self.create_window() context = self.create_cg_context() context.register_states() context.manage_texture_parameters = True effect = context.create_effect_from_file(join(ROOT, 'effect.cgfx')) self.load_texture(join(ROOT, 'texture.png'), effect.parameters.by_name['texture']) self.time_parameter = effect.parameters.by_name['time'] self.model_view_proj = effect.parameters.by_name['modelViewProj'] valid_techniques = [t for t in effect.techniques if t.valid] assert valid_techniques self.technique = valid_techniques[0] print('techniques:') import pprint; pprint.pprint(effect.techniques.by_name) print('parameters:') import pprint; pprint.pprint(effect.parameters.by_name) self.running = True while self.running: self.process_events() self.window.clear(sfml.Color.BLACK) self.render() self.window.display() context.dispose() self.cg.dispose() self.window.close() def load_texture(self, filename, parameter): image = Image.open(filename) data = image.tostring('raw', 'RGB', 0, -1) gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1) to = gl.glGenTextures(1) gl.glBindTexture(gl.GL_TEXTURE_2D, to) parameter.set_value(to) gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, image.size[0], image.size[1], 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, data) def render(self): t = clock() self.time_parameter.set_value(t) data = ( ((-0.5, -0.5, 0), (0.0, 0.0)), ((0.5, -0.5, 0), (1.0, 0.0)), ((0, 0.5, 0), (0.5, 1.0)), ) proj = matrix44.create_perspective_projection_matrix( 90, 4.0 / 3.0, 0.01, 100.0) mv = matrix44.multiply( matrix44.create_from_translation((sin(t * 10), 0.0, -3.0 * abs(sin(t * 30)))), matrix44.create_from_z_rotation(t * 50), ) mvp = matrix44.multiply(mv, proj) self.model_view_proj.set_value(mvp) for pass_ in self.technique.passes: pass_.begin() gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glBegin(gl.GL_TRIANGLES) for position, texcoord in data: gl.glMultiTexCoord2fv(gl.GL_TEXTURE0, texcoord) gl.glVertex3fv(position) gl.glEnd() pass_.end() def create_window(self): window = sfml.RenderWindow(sfml.VideoMode(800, 600), 'python-cg example') window.vertical_sync_enabled = True return window def create_cg_context(self): self.cg = Cg() return self.cg.create_context() def process_events(self): for event in self.window.iter_events(): if event.type == sfml.Event.CLOSED: self.running = False
def create_cg_context(self): self.cg = Cg() return self.cg.create_context()
class Elliptic2dDriverBase: def __init__(self, fFunc, gFunc, sFunc): self.fFuncStr = fFunc self.gFuncStr = gFunc self.sFuncStr = sFunc self.boundaryPoints = [] self.delny = None self.ellipt = Elliptic2d(self.ffunc, self.gfunc, self.sfunc) self.slvr = None self.solution = None def getTriangulation(self): return self.delny def getSolution(self): return self.solution def assemble(self): self.ellipt.assemble(self.delny) def ffunc(self, xy): x, y = xy return eval(self.fFuncStr) def gfunc(self, xy): x, y = xy return eval(self.gFuncStr) def sfunc(self, xy): x, y = xy return eval(self.sFuncStr) def applyBoundaryConditions(self, bFunc, cFunc): bcs = {} nt = len(self.boundaryPoints) for i0 in range(nt): x0, y0 = self.boundaryPoints[i0] i1 = (i0 + 1) % nt x1, y1 = self.boundaryPoints[i1] x, y = 0.5 * (x0 + x1), 0.5 * (y0 + y1) b, c = eval(bFunc), eval(cFunc) bcs[i0, i1] = (b, c) self.ellipt.applyBoundaryConditions(bcs) self.slvr = Cg(self.ellipt.getStiffnessMatrix(), \ self.ellipt.getSourceVector()) def solve(self): # initial guess self.n = len(self.delny.points) mat = self.ellipt.getStiffnessMatrix() # use diagonal matrix as preconditioner precond = numpy.array([mat[i, i] for i in range(self.n)]) # initial guess x0 = numpy.zeros((self.n, ), numpy.float64) # max number of iterations numIters = self.n diag = {} self.solution = self.slvr.solve(precond=precond, x0=x0, numIters=numIters, tol=1.e-10, verbose=True, diag=diag) return diag def show(self): from plot import Plot pl = Plot(self.delny, width=500, height=500) pl.show(self.solution)