Exemple #1
0
    def rank(self, grid, gp, alphas, *args, **kws):
        gs = grid.getStorage()
        x = DataVector(gs.getDimension())
        gs.getCoordinates(gp, x)
        opEval = createOperationEvalNaive(grid)

        return abs(opEval.eval(alphas, x) - self.f(x))
Exemple #2
0
 def __init__(self, d, f):
   self.f = f
   self.d = d
   self.grid = pysgpp.Grid.createBsplineClenshawCurtisGrid(d, 3)
   self.gridStorage = self.grid.getStorage()
   try :
     self.hierarch = pysgpp.createOperationHierarchisation(self.grid)
   except :
     self.hierarch = pysgpp.createOperationMultipleHierarchisation(self.grid)
   self.opeval = pysgpp.createOperationEvalNaive(self.grid)
   self.alpha = pysgpp.DataVector(self.gridStorage.getSize())
Exemple #3
0
def evalSGFunction(grid, alpha, p, isConsistent=True):
    if grid.getSize() != len(alpha):
        raise AttributeError(
            "grid size differs from length of coefficient vector")
    if len(p.shape) == 1:
        if grid.getStorage().getDimension() != p.shape[0]:
            raise AttributeError(
                "grid dimension differs from dimension of sample")

        evalNaiveGridTypes = [
            GridType_Bspline, GridType_BsplineClenshawCurtis,
            GridType_BsplineBoundary, GridType_ModBsplineClenshawCurtis,
            GridType_ModBspline, GridType_LinearClenshawCurtis,
            GridType_LinearClenshawCurtisBoundary,
            GridType_ModLinearClenshawCurtis, GridType_PolyClenshawCurtis,
            GridType_PolyClenshawCurtisBoundary, GridType_ModPolyClenshawCurtis
        ]

        p_vec = DataVector(p)
        alpha_vec = DataVector(alpha)
        if isConsistent:
            if grid.getType() in evalNaiveGridTypes:
                opEval = createOperationEvalNaive(grid)
            else:
                opEval = createOperationEval(grid)
        else:
            opEval = createOperationEvalNaive(grid)

        ans = opEval.eval(alpha_vec, p_vec)

        del opEval
        del alpha_vec
        del p_vec

        return ans
    else:
        if grid.getStorage().getDimension() != p.shape[1]:
            raise AttributeError(
                "grid dimension differs from dimension of samples")
        return evalSGFunctionMulti(grid, alpha, p, isConsistent)
Exemple #4
0
def checkInterpolation(grid, alpha, nodalValues, epsilon=1e-13):
    # check if interpolation property is given
    gs = grid.getStorage()
    evalValues = np.ndarray(gs.getSize())
    x = DataVector(gs.getDimension())
    opEval = createOperationEvalNaive(grid)
    alpha_vec = DataVector(alpha)
    for i in range(gs.getSize()):
        gs.getCoordinates(gs.getPoint(i), x)
        evalValues[i] = opEval.eval(alpha_vec, x)

    error = np.array([])
    nodes = np.array([])
    head = True
    p = DataVector(gs.getDimension())
    for i, (nodal, value) in enumerate(zip(nodalValues, evalValues)):
        # compute the relative error
        abs_error = np.abs(nodal - value)
        rel_error = abs_error
        if abs(nodal) > 1e-14:
            rel_error = np.abs(abs_error / nodal)

        if abs_error > epsilon:
            spacing = 12
            if head:
                print()
                print( "%s | %s | %s | %s | %s | %s | %s" % \
                    ("index".rjust(spacing),
                     "levelsum".rjust(spacing),
                     "surplus".rjust(spacing),
                     "nodalValue".rjust(spacing),
                     "eval".rjust(spacing),
                     "rel err".rjust(spacing),
                     "abs err".rjust(spacing)))
                head = False
            gs.getCoordinates(gs.getPoint(i), p)
            print( "%s | %s | %s | %s | %s | %s | %s" % \
                (("%i" % i).rjust(spacing),
                    ("%i" % gs.getPoint(i).getLevelSum()).rjust(spacing),
                    ("%g" % alpha[i]).rjust(spacing),
                    ("%g" % nodal).rjust(spacing),
                    ("%g" % value).rjust(spacing),
                    ("%g" % rel_error).rjust(spacing),
                    ("%g" % abs_error).rjust(spacing)))
            nodes = np.append(nodes, [i])
            error = np.append(abs_error, [abs_error])

    return error, nodes