本文整理汇总了Python中pysgpp.Grid.createLinearGrid方法的典型用法代码示例。如果您正苦于以下问题:Python Grid.createLinearGrid方法的具体用法?Python Grid.createLinearGrid怎么用?Python Grid.createLinearGrid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pysgpp.Grid
的用法示例。
在下文中一共展示了Grid.createLinearGrid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createGrid
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def createGrid(grid, dim, deg=1, addTruncatedBorder=False):
# create new grid
gridType = grid.getType()
if gridType in [Poly, PolyBoundary]:
deg = max(deg, grid.getDegree())
# print gridType, deg
if deg > 1:
if gridType in [LinearBoundary, PolyBoundary]:
return Grid.createPolyBoundaryGrid(dim, deg)
elif gridType == LinearL0Boundary:
raise NotImplementedError("there is no full boundary polynomial grid")
elif gridType in [Linear, Poly]:
return Grid.createPolyGrid(dim, deg)
else:
raise Exception('unknown grid type %s' % gridType)
else:
if gridType == Linear:
return Grid.createLinearGrid(dim)
elif gridType == LinearBoundary:
return Grid.createLinearBoundaryGrid(dim)
elif gridType == LinearL0Boundary:
return Grid.createLinearBoundaryGrid(dim, 0)
else:
raise Exception('unknown grid type %s' % gridType)
示例2: checkPositivity
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def checkPositivity(grid, alpha):
# define a full grid of maxlevel of the grid
gs = grid.getStorage()
fullGrid = Grid.createLinearGrid(gs.dim())
fullGrid.createGridGenerator().full(gs.getMaxLevel())
fullHashGridStorage = fullGrid.getStorage()
A = DataMatrix(fullHashGridStorage.size(), fullHashGridStorage.dim())
p = DataVector(gs.dim())
for i in xrange(fullHashGridStorage.size()):
fullHashGridStorage.get(i).getCoords(p)
A.setRow(i, p)
res = evalSGFunctionMulti(grid, alpha, A)
ymin, ymax, cnt = 0, -1e10, 0
for i, yi in enumerate(res.array()):
if yi < 0. and abs(yi) > 1e-13:
cnt += 1
ymin = min(ymin, yi)
ymax = max(ymax, yi)
A.getRow(i, p)
print " %s = %g" % (p, yi)
if cnt > 0:
print "warning: function is not positive"
print "%i/%i: [%g, %g]" % (cnt, fullHashGridStorage.size(), ymin, ymax)
return cnt == 0
示例3: testOperationB
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def testOperationB(self):
from pysgpp import Grid, DataVector, DataMatrix
factory = Grid.createLinearGrid(1)
gen = factory.createGridGenerator()
gen.regular(2)
alpha = DataVector(factory.getStorage().size())
p = DataMatrix(1,1)
beta = DataVector(1)
alpha.setAll(0.0)
p.set(0,0,0.25)
beta[0] = 1.0
opb = factory.createOperationB()
opb.mult(beta, p, alpha)
self.failUnlessAlmostEqual(alpha[0], 0.5)
self.failUnlessAlmostEqual(alpha[1], 1.0)
self.failUnlessAlmostEqual(alpha[2], 0.0)
alpha.setAll(0.0)
alpha[0] = 1.0
p.set(0,0, 0.25)
beta[0] = 0.0
opb.multTranspose(alpha, p, beta)
self.failUnlessAlmostEqual(beta[0], 0.5)
示例4: testSerializationLinearBoundingBox
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def testSerializationLinearBoundingBox(self):
"""Uses Linear grid for tests"""
from pysgpp import Grid
factory = Grid.createLinearGrid(2)
self.failIfEqual(factory, None)
gen = factory.createGridGenerator()
gen.regular(3)
boundingBox = factory.getBoundingBox()
tempBound = boundingBox.getBoundary(0)
tempBound.leftBoundary = 0.0
tempBound.rightBoundary = 100.0
tempBound.bDirichletLeft = False;
tempBound.bDirichletRight = False;
boundingBox.setBoundary(0, tempBound)
str = factory.serialize()
self.assert_(len(str) > 0)
newfac = Grid.unserialize(str)
self.failIfEqual(newfac, None)
self.assertEqual(factory.getStorage().size(), newfac.getStorage().size())
boundingBox = newfac.getBoundingBox()
tempBound = boundingBox.getBoundary(0)
self.assertEqual(0.0, tempBound.leftBoundary)
self.assertEqual(100.0, tempBound.rightBoundary)
self.assertEqual(False, tempBound.bDirichletLeft)
self.assertEqual(False, tempBound.bDirichletRight)
示例5: testHierarchisationD
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def testHierarchisationD(self):
from pysgpp import Grid
dim = 3
level = 5
function = buildParable(dim)
grid = Grid.createLinearGrid(dim)
testHierarchisationDehierarchisation(self, grid, level, function)
示例6: testCreation
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def testCreation(self):
"""Uses Linear grid for tests"""
from pysgpp import Grid
factory = Grid.createLinearGrid(2)
self.failIfEqual(factory, None)
storage = factory.getStorage()
self.failIfEqual(storage, None)
示例7: setUp
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def setUp(self):
self.grid = Grid.createLinearGrid(2) # a simple 2D grid
self.grid.createGridGenerator().regular(3) # max level 3 => 17 points
self.HashGridStorage = self.grid.getStorage()
alpha = DataVector(self.grid.getSize())
alpha.setAll(1.0)
for i in [9, 10, 11, 12]:
alpha[i] = 0.0
coarseningFunctor = SurplusCoarseningFunctor(alpha, 4, 0.5)
self.grid.createGridGenerator().coarsen(coarseningFunctor, alpha)
示例8: testHatRegulardD
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def testHatRegulardD(self):
from pysgpp import Grid
factory = Grid.createLinearGrid(3)
m = generateLaplaceMatrix(factory, 3)
m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/C_laplace_phi_li_hut_dim_3_nopsgrid_31_float.dat.gz')
# compare
compareStiffnessMatrices(self, m, m_ref)
示例9: general_test
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def general_test(self, d, l, bb, xs):
test_desc = "dim=%d, level=%d, len(x)=%s" % (d, l, len(xs))
print test_desc
self.grid = Grid.createLinearGrid(d)
self.grid_gen = self.grid.createGridGenerator()
self.grid_gen.regular(l)
alpha = DataVector([self.get_random_alpha() for i in xrange(self.grid.getSize())])
bb_ = BoundingBox(d)
for d_k in xrange(d):
dimbb = DimensionBoundary()
dimbb.leftBoundary = bb[d_k][0]
dimbb.rightBoundary = bb[d_k][1]
bb_.setBoundary(d_k, dimbb)
# Calculate the expected value without the bounding box
expected_normal = [self.calc_exp_value_normal(x, d, bb, alpha) for x in xs]
#expected_transposed = [self.calc_exp_value_transposed(x, d, bb, alpha) for x in xs]
# Now set the bounding box
self.grid.getStorage().setBoundingBox(bb_)
dm = DataMatrix(len(xs), d)
for k, x in enumerate(xs):
dv = DataVector(x)
dm.setRow(k, dv)
multEval = createOperationMultipleEval(self.grid, dm)
actual_normal = DataVector(len(xs))
#actual_transposed = DataVector(len(xs))
multEval.mult(alpha, actual_normal)
#multEval.mult(alpha, actual_transposed)
actual_normal_list = []
for k in xrange(len(xs)):
actual_normal_list.append(actual_normal.__getitem__(k))
#actual_transposed_list = []
#for k in xrange(len(xs)):
# actual_transposed_list.append(actual_transposed.__getitem__(k))
self.assertAlmostEqual(actual_normal_list, expected_normal)
#self.assertAlmostEqual(actual_tranposed_list, expected_tranposed)
del self.grid
示例10: makePositive
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def makePositive(grid, alpha):
"""
insert full grid points if they are negative and the father
node is part of the sparse grid
@param grid:
@param alpha:
"""
# copy old sg function
jgrid = copyGrid(grid)
# evaluate the sparse grid function at all full grid points
level = grid.getStorage().getMaxLevel()
fg = Grid.createLinearGrid(grid.getStorage().dim())
fg.createGridGenerator().full(level)
# copy the old grid and use it as reference
jgs = jgrid.getStorage()
fgs = fg.getStorage()
# run over all results and check where the function value
# is lower than zero
cnt = 1
while True:
print "run %i: full grid size = %i" % (cnt, fgs.size())
gps = []
# insert those fg points, which are not yet positive
values = computeNodalValues(fg, grid, alpha)
for i in xrange(len(values)):
gp = fgs.get(i)
if values[i] < 0 and not jgs.has_key(gp):
gps += insertPoint(jgrid, gp)
gps += insertHierarchicalAncestors(jgrid, gp)
jgrid.getStorage().recalcLeafProperty()
# 1. compute nodal values for new grid points
jnodalValues = computeNodalValues(jgrid, grid, alpha)
# 2. set the new ones to zero
jgs = jgrid.getStorage()
for gp in gps:
jnodalValues[jgs.seq(gp)] = 0.
# 3. hierarchize
jalpha = hierarchize(jgrid, jnodalValues)
# stop loop if no points have been added
if len(gps) == 0:
break
# 4. reset values for next loop
grid = copyGrid(jgrid)
alpha = DataVector(jalpha)
cnt += 1
return jgrid, jalpha
示例11: setUp
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def setUp(self):
#
# Grid
#
DIM = 2
LEVEL = 2
self.grid = Grid.createLinearGrid(DIM)
self.grid_gen = self.grid.createGridGenerator()
self.grid_gen.regular(LEVEL)
#
# trainData, classes, errors
#
xs = []
DELTA = 0.05
DELTA_RECI = int(1/DELTA)
for i in xrange(DELTA_RECI):
for j in xrange(DELTA_RECI):
xs.append([DELTA*i, DELTA*j])
random.seed(1208813)
ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)]
# print xs
# print ys
self.trainData = DataMatrix(xs)
self.classes = DataVector(ys)
self.alpha = DataVector([3, 6, 7, 9, -1])
self.errors = DataVector(DELTA_RECI**2)
coord = DataVector(DIM)
for i in xrange(self.trainData.getNrows()):
self.trainData.getRow(i, coord)
self.errors.__setitem__ (i, self.classes[i] - self.grid.eval(self.alpha, coord))
#print "Errors:"
#print self.errors
#
# Functor
#
self.functor = WeightedErrorRefinementFunctor(self.alpha, self.grid)
self.functor.setTrainDataset(self.trainData)
self.functor.setClasses(self.classes)
self.functor.setErrors(self.errors)
示例12: setUp
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def setUp(self,):
self.__gridFormatter = None
self.filename = pathlocal + "/datasets/grid.gz"
self.savefile = pathlocal + "/datasets/savetest.grid.gz"
self.correct_str = ""
self.grid = None
self.__gridFormatter = GridFormatter()
dim = 3
self.grid = Grid.createLinearGrid(dim)
self.grid.createGridGenerator().regular(3)
self.correct_str = self.grid.serialize()
示例13: general_test
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def general_test(self, d, l, bb, x):
test_desc = "dim=%d, level=%d, bb=%s, x=%s" % (d, l, bb, x)
print test_desc
self.grid = Grid.createLinearGrid(d)
self.grid_gen = self.grid.createGridGenerator()
self.grid_gen.regular(l)
alpha = DataVector([1] * self.grid.getSize())
bb_ = BoundingBox(d)
for d_k in xrange(d):
dimbb = DimensionBoundary()
dimbb.leftBoundary = bb[d_k][0]
dimbb.rightBoundary = bb[d_k][1]
bb_.setBoundary(d_k, dimbb)
# Calculate the expected value without the bounding box
expected = 0.0
inside = True
x_trans = DataVector(d)
for d_k in xrange(d):
if not (bb[d_k][0] <= x[d_k] and x[d_k] <= bb[d_k][1]):
inside = False
break
else:
x_trans[d_k] = (x[d_k] - bb[d_k][0]) / (bb[d_k][1] - bb[d_k][0])
if inside:
p = DataVector(x_trans)
expected = self.grid.eval(alpha, p)
else:
expected = 0.0
# Now set the bounding box
self.grid.getStorage().setBoundingBox(bb_)
p = DataVector(x)
actual = self.grid.eval(alpha, p)
self.assertAlmostEqual(actual, expected)
del self.grid
示例14: testGeneration
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def testGeneration(self):
from pysgpp import Grid, DataVector
factory = Grid.createLinearGrid(2)
storage = factory.getStorage()
gen = factory.createGridGenerator()
self.failIfEqual(gen, None)
self.failUnlessEqual(storage.size(), 0)
gen.regular(3)
self.failUnlessEqual(storage.size(), 17)
#This should fail
self.failUnlessRaises(Exception, gen.regular, 3)
示例15: testHatRegulardD_two
# 需要导入模块: from pysgpp import Grid [as 别名]
# 或者: from pysgpp.Grid import createLinearGrid [as 别名]
def testHatRegulardD_two(self):
from pysgpp import Grid
factory = Grid.createLinearGrid(3)
training = buildTrainingVector(readDataVector('data/data_dim_3_nops_512_float.arff.gz'))
level = 4
gen = factory.createGridGenerator()
gen.regular(level)
m = generateBBTMatrix(factory, training)
m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/BBT_phi_li_hut_dim_3_nopsgrid_111_float.dat.gz')
# compare
compareBBTMatrices(self, m, m_ref)