Exemplo n.º 1
0
def example5():
    ## First, we want to configure which grid points to use in which dimension.
    ## We use Chebyshev points in the 0th dimension. To make them nested, we have to use at least \f$n
    ## = 3^l\f$ points at level \f$l\f$. This is why this method contains the prefix exp.
    ## CombiHierarchies provides some matching configurations for grid points. If you nevertheless
    ## need your own configuration or you want to know which growth strategy and ordering fit to which
    ## point distribution, look up the implementation details in CombiHierarchies, it is not
    ## difficult to implement your own configuration.
    grids = pysgpp.AbstractPointHierarchyVector()
    grids.push_back(pysgpp.CombiHierarchies.expChebyshev())

    ## Our next set of grid points are Leja points with linear growth (\f$n = 1 + 3l\f$).
    ## For the last dimension, we use equidistant points with boundary. These are suited for linear
    ## interpolation. To make them nested, again the slowest possible exponential growth is selected
    ## by the CombiHierarchies class.
    grids.push_back(pysgpp.CombiHierarchies.linearLeja(3))
    grids.push_back(pysgpp.CombiHierarchies.expUniformBoundary())

    ## The next thing we have to configure is the linear operation that is performed in those
    ## directions. We will use polynomial interpolation in the 0th dimension, quadrature in the 1st
    ## dimension and linear interpolation in the 2nd dimension.
    ## Roughly spoken, this means that a quadrature is performed on the 1D function that is the
    ## interpolated function with two fixed parameters. But since those operators "commute", the
    ## result is invariant under the order that the operations are applied in.
    ## The CombiEvaluators class also provides analogous methods and typedefs for the multi-evaluation
    ## case.
    evaluators = pysgpp.FloatScalarAbstractLinearEvaluatorVector()
    evaluators.push_back(pysgpp.CombiEvaluators.polynomialInterpolation())
    evaluators.push_back(pysgpp.CombiEvaluators.quadrature())
    evaluators.push_back(pysgpp.CombiEvaluators.linearInterpolation())

    ## To create a CombigridOperation object with our own configuration, we have to provide a
    ## LevelManager as well:
    levelManager = pysgpp.WeightedRatioLevelManager()
    operation = pysgpp.CombigridOperation(grids, evaluators, levelManager,
                                          func)

    ## The two interpolations need a parameter \f$(x, z)\f$. If \f$\tilde{f}\f$ is the interpolated
    ## function, the operation approximates the result of \f$\int_0^1 \tilde{f}(x, y, z) \,dy\f$.
    parameters = pysgpp.DataVector([0.777, 0.14159])
    result = operation.evaluate(2, parameters)
    print("Result: " + str(result))
Exemplo n.º 2
0
def example6():

    ## To create a CombigridOperation, we currently have to use the longer way as in example 5.
    grids = pysgpp.AbstractPointHierarchyVector(
        d, pysgpp.CombiHierarchies.expUniformBoundary())
    evaluators = pysgpp.FloatScalarAbstractLinearEvaluatorVector(
        d, pysgpp.CombiEvaluators.cubicSplineInterpolation())
    levelManager = pysgpp.WeightedRatioLevelManager()

    ## We have to specify if the function always produces the same value for the same grid points.
    ## This can make the storage smaller if the grid points are nested. In this implementation, this
    ## is true. However, it would be false in the PDE case, so we set it to false here.
    exploitNesting = False

    ## Now create an operation as usual and evaluate the interpolation with a test parameter.
    operation = pysgpp.CombigridOperation(grids, evaluators, levelManager,
                                          pysgpp.gridFunc(gf), exploitNesting)

    parameter = pysgpp.DataVector([0.1, 0.2, 0.3])

    result = operation.evaluate(4, parameter)

    print("Target function value: " + str(func(parameter)))
    print("Numerical result: " + str(result))
Exemplo n.º 3
0
    plt.close(fig)

    w = pysgpp.singleFunc(marginal.pdf)

    grids = pysgpp.AbstractPointHierarchyVector()
    grids.push_back(pysgpp.CombiHierarchies.linearLeja(w))
    grids.push_back(pysgpp.CombiHierarchies.linearLeja(w))

    evaluators = pysgpp.FloatScalarAbstractLinearEvaluatorVector()
    evaluators.push_back(pysgpp.CombiEvaluators.polynomialInterpolation())
    evaluators.push_back(pysgpp.CombiEvaluators.polynomialInterpolation())

    # To create a CombigridOperation object with our own configuration, we have to provide a
    # LevelManager as well:
    levelManager = pysgpp.WeightedRatioLevelManager()
    operation = pysgpp.CombigridOperation(grids, evaluators, levelManager,
                                          func)

    # We can add regular levels like before:
    levelManager.addRegularLevels(args.level)

    # We can also fetch the used grid points and plot the grid:
    grid = levelManager.getGridPointMatrix()
    gridList = [[grid.get(r, c) for c in range(grid.getNcols())]
                for r in range(grid.getNrows())]

    fig = plt.figure()
    plt.plot(gridList[0],
             gridList[1],
             " ",
             color=load_color(0),
             marker='o',