def computeBilinearForm(self, grid): """ Compute bilinear form for the current grid @param grid: Grid @return DataMatrix """ # create bilinear form of the grid gs = grid.getStorage() A = DataMatrix(gs.getSize(), gs.getSize()) A.setAll(0.) createOperationLTwoDotExplicit(A, grid) # multiply the entries with the pdf at the center of the support p = DataVector(gs.getDimension()) q = DataVector(gs.getDimension()) for i in range(gs.getSize()): gpi = gs.getPoint(i) gs.getCoordinates(gpi, p) for j in range(gs.getSize()): gpj = gs.getPoint(j) gs.getCoordinates(gpj, q) y = float(A.get(i, j) * self._U.pdf(p)) A.set(i, j, y) A.set(j, i, y) self._map[self.getKey([gpi, gpj])] = A.get(i, j) return A
def computeBilinearForm(self, grid): """ Compute bilinear form for the current grid @param grid: Grid @return DataMatrix """ # create bilinear form of the grid gs = grid.getStorage() A = DataMatrix(gs.size(), gs.size()) A.setAll(0.) createOperationLTwoDotExplicit(A, grid) gs = grid.getStorage() A = DataMatrix(gs.size(), gs.size()) createOperationLTwoDotExplicit(A, grid) # multiply the entries with the pdf at the center of the support p = DataVector(gs.dim()) q = DataVector(gs.dim()) for i in xrange(gs.size()): gpi = gs.get(i) gpi.getCoords(p) for j in xrange(gs.size()): gpj = gs.get(j) gpj.getCoords(q) y = float(A.get(i, j) * self._U.pdf(p)) A.set(i, j, y) A.set(j, i, y) self._map[self.getKey(gpi, gpj)] = A.get(i, j) return A
def computePiecewiseConstantBF(grid, U, admissibleSet): # create bilinear form of the grid gs = grid.getStorage() A = DataMatrix(gs.size(), gs.size()) createOperationLTwoDotExplicit(A, grid) # multiply the entries with the pdf at the center of the support p = DataVector(gs.getDimension()) q = DataVector(gs.getDimension()) B = DataMatrix(admissibleSet.getSize(), gs.size()) b = DataVector(admissibleSet.getSize()) # s = np.ndarray(gs.getDimension(), dtype='float') for k, gpi in enumerate(admissibleSet.values()): i = gs.getSequenceNumber(gpi) gs.getCoordinates(gpi, p) for j in range(gs.size()): gs.getCoordinates(gs.getPoint(j), q) # for d in xrange(gs.getDimension()): # # get level index # xlow = max(p[0], q[0]) # xhigh = min(p[1], q[1]) # s[d] = U[d].cdf(xhigh) - U[d].cdf(xlow) y = float(A.get(i, j) * U.pdf(p)) B.set(k, j, y) if i == j: b[k] = y return B, b
def computePiecewiseConstantBF(grid, U, admissibleSet): # create bilinear form of the grid gs = grid.getStorage() A = DataMatrix(gs.size(), gs.size()) createOperationLTwoDotExplicit(A, grid) # multiply the entries with the pdf at the center of the support p = DataVector(gs.dim()) q = DataVector(gs.dim()) B = DataMatrix(admissibleSet.getSize(), gs.size()) b = DataVector(admissibleSet.getSize()) # s = np.ndarray(gs.dim(), dtype='float') for k, gpi in enumerate(admissibleSet.values()): i = gs.seq(gpi) gpi.getCoords(p) for j in xrange(gs.size()): gs.get(j).getCoords(q) # for d in xrange(gs.dim()): # # get level index # xlow = max(p[0], q[0]) # xhigh = min(p[1], q[1]) # s[d] = U[d].cdf(xhigh) - U[d].cdf(xlow) y = float(A.get(i, j) * U.pdf(p)) B.set(k, j, y) if i == j: b[k] = y return B, b
def test_LTwoDot(grid, l): res = 10000 b = grid.getBasis(); grid.getGenerator().regular(l) gridStorage = grid.getStorage() size = gridStorage.getSize() # print(size) m = pysgpp.DataMatrix(size, size) opMatrix = pysgpp.createOperationLTwoDotExplicit(m, grid) # print m # m_comp = pysgpp.DataMatrix(size, size) for i in range(gridStorage.getSize()): for j in range(i, gridStorage.getSize()): gpi = gridStorage.getPoint(i) gpj = gridStorage.getPoint(j) sol = 1 # print "--------" # print "i:{} j:{}".format(i, j) for k in range(d): lik = gpi.getLevel(k) iik = gpi.getIndex(k) ljk = gpj.getLevel(k) ijk = gpj.getIndex(k) # print "i l,i: {},{} j l,i: {},{}".format(lik, iik, ljk, ijk) xs = np.linspace(0, 1, res) tmp = sum([b.eval(lik, iik, x)*b.eval(ljk, ijk, x) for x in xs]) / res sol *= tmp # print("lik:{} iik:{} ljk:{} ijk:{} k:{} tmp: {}".format(lik, iik, ljk, ijk, k,tmp)) # print(sol) error = abs(m.get(i,j) - sol) # print error if(error >= 10**-4): print("i:{} j:{} error: {}".format(i, j, error)) print("iik:{} lik:{} ijk:{} ljk:{} error: {}".format(iik, lik, ijk, ljk, error)) print("is:{} should:{}".format(m.get(i,j), sol))
def computeBilinearForm(self, grid): """ Compute bilinear form for the current grid @param grid: Grid @return: DataMatrix """ gs = grid.getStorage() A = DataMatrix(gs.size(), gs.size()) A.setAll(0.) createOperationLTwoDotExplicit(A, grid) # store the result in the hash map for i in xrange(gs.size()): gpi = gs.get(i) for j in xrange(gs.size()): gpj = gs.get(j) key = self.getKey(gpi, gpj) self._map[key] = A.get(i, j) return A
def computePiecewiseConstantBilinearForm(grid, U): # create bilinear form of the grid gs = grid.getStorage() A = DataMatrix(gs.size(), gs.size()) createOperationLTwoDotExplicit(A, grid) # multiply the entries with the pdf at the center of the support p = DataVector(gs.getDimension()) q = DataVector(gs.getDimension()) for i in range(gs.size()): gs.getCoordinates(gs.getPoint(i), p) for j in range(gs.size()): gs.getCoordinates(gs.getPoint(j), q) # compute center of the support p.add(q) p.mult(0.5) # multiply the entries in A with the pdf at p y = float(A.get(i, j) * U.pdf(p)) A.set(i, j, y) A.set(j, i, y) return A
def computePiecewiseConstantBilinearForm(grid, U): # create bilinear form of the grid gs = grid.getStorage() A = DataMatrix(gs.size(), gs.size()) createOperationLTwoDotExplicit(A, grid) # multiply the entries with the pdf at the center of the support p = DataVector(gs.dim()) q = DataVector(gs.dim()) for i in xrange(gs.size()): gs.get(i).getCoords(p) for j in xrange(gs.size()): gs.get(j).getCoords(q) # compute center of the support p.add(q) p.mult(0.5) # multiply the entries in A with the pdf at p y = float(A.get(i, j) * U.pdf(p)) A.set(i, j, y) A.set(j, i, y) return A
def test_LTwoDotImplicit(grid, l): grid.getGenerator().regular(l) gridStorage = grid.getStorage() size = gridStorage.getSize() # print(size) m = pysgpp.DataMatrix(size, size) opExplicit = pysgpp.createOperationLTwoDotExplicit(m, grid) op = pysgpp.createOperationLTwoDotProduct(grid) alpha = pysgpp.DataVector(size) resultExplicit = pysgpp.DataVector(size) result = pysgpp.DataVector(size) for i in range(size): alpha[i] = 1 opExplicit.mult(alpha, resultExplicit) op.mult(alpha, result) for i in range(size): if result[i] != resultExplicit[i]: print("Error result entry {} differs".format(i)) if abs(result[i] - resultExplicit[i]) > 1e-16: # print result[i] - resultExplicit[i] print("result:{}".format(result[i])) print("resultExplicit:{}".format(resultExplicit[i]))