SG++-Doxygen-Documentation
example_comparison.py
1 import pysgpp
2 import math
3 import matplotlib.pyplot as plt
4 from mpl_toolkits.mplot3d import Axes3D
5 from operator import mul
6 try:
7  from functools import reduce
8 except ImportError:
9  pass
10 
11 base = 0.1

The first thing we need is a function to evaluate. This function will be evaluated on the domain \([0, 1]^d\). This particular function can be used with any number of dimensions. The input parameter of the function is of type pysgpp.DataVector, so do not treat it like a list. The return type is float.

1 def f(x):
2  product = 1.0
3  for i in range(x.getSize()):
4  product *= math.exp(-pow(base, i)*x[i])
5  return product

We have to wrap f in a pysgpp.MultiFunction object.

1 func = pysgpp.multiFunc(f)

comparison function

1 def compare():
2  mydim = 5
3  operation = pysgpp.CombigridOperation.createLinearLejaQuadrature(mydim, func)
4  levelManager = operation.getLevelManager()
5  idx = pysgpp.IndexVector([1 for i in range(mydim)])
6  levelManager.addLevel(idx)
7 
8  result = operation.getResult()
9  analyticalResult = reduce(mul, [pow(base, -i) * (1.0 - pow(math.e, -pow(base, i))) for i in range(mydim)], 1.0)
10 
11  print("Full Grid Error: " + str(abs(result - analyticalResult)))
12  numGridPoints = operation.numGridPoints()
13  print("Number of grid points: " + str(numGridPoints))

Clear

1  operation.setParameters()
2  result = operation.evaluate(3)
3  print("Regular Grid Error: " + str(abs(result - analyticalResult)))
4  numGridPoints2 = operation.numGridPoints()
5  print("Number of grid points: " + str(numGridPoints2))

Clear

1  operation.setParameters()
2  levelManager.addLevelsAdaptive(numGridPoints2)
3  result = operation.getResult()
4  print("Adaptive Grid Error: " + str(abs(result - analyticalResult)))
5  numGridPoints3 = operation.numGridPoints()
6  print("Number of grid points: " + str(numGridPoints3))
7 
8 compare()