# QMCPy for Lebesgue Integration¶

This notebook will give examples of how to use QMCPy for integration problems that not are defined in terms of a standard measure. i.e. Uniform or Gaussian.

from qmcpy import *
from numpy import *


## Sample Problem 1¶

abs_tol = .01
dim = 1
a = 0
b = 2
true_value = 8./3

# Lebesgue Measure
integrand = CustomFun(
true_measure = Lebesgue(Uniform(Halton(dim, seed=7),lower_bound=a, upper_bound=b)),
g = lambda x: (x**2).sum(1))
solution,data = CubQMCCLT(integrand, abs_tol=abs_tol).integrate()
print('y = %.3f'%solution)
error = abs((solution-true_value))
if error>abs_tol:
raise Exception("Not within error tolerance")

y = 2.668

# Uniform Measure
integrand = CustomFun(
true_measure = Uniform(IIDStdUniform(dim, seed=7), lower_bound=a, upper_bound=b),
g = lambda x: (2*(x**2)).sum(1))
solution,data = CubMCCLT(integrand, abs_tol=abs_tol).integrate()
print('y = %.3f'%solution)
error = abs((solution-true_value))
if error>abs_tol:
raise Exception("Not within error tolerance")

y = 2.669


## Sample Problem 2¶

abs_tol = .001
dim = 2
a = array([1.,2.])
b = array([2.,4.])
true_value = ((a[0]**3-b[0]**3)*(a[1]-b[1])+(a[0]-b[0])*(a[1]**3-b[1]**3))/3

Answer = 23.33333

# Lebesgue Measure
integrand = CustomFun(
true_measure = Lebesgue(Uniform(Sobol(dim, seed=7), lower_bound=a, upper_bound=b)),
g = lambda x: (x**2).sum(1))
solution,data = CubQMCCLT(integrand, abs_tol=abs_tol).integrate()
print('y = %.5f'%solution)
error = abs((solution-true_value))
if error>abs_tol:
raise Exception("Not within error tolerance")

y = 23.33326

# Uniform Measure
integrand = CustomFun(
true_measure = Uniform(Sobol(dim, seed=17), lower_bound=a, upper_bound=b),
g = lambda x: (b-a).prod()*(x**2).sum(1))
solution,data = CubQMCCLT(integrand, abs_tol=abs_tol).integrate()
print('y = %.5f'%solution)
error = abs((solution-true_value))
if error>abs_tol:
raise Exception("Not within error tolerance")

y = 23.33340


## Sample Problem 3¶

Integral that cannot be done in terms of any standard mathematical functions

Mathematica Code: Integrate[Sin[x]/Log[x], {x,a,b}]

abs_tol = .0001
dim = 1
a = 3
b = 5
true_value = -0.87961

# Lebesgue Measure
integrand = CustomFun(
true_measure = Lebesgue(Uniform(Lattice(dim, randomize=True, seed=7),a,b)),
g = lambda x: (sin(x)/log(x)).sum(1))
solution,data = CubQMCLatticeG(integrand, abs_tol=abs_tol).integrate()
print('y = %.3f'%solution)
error = abs((solution-true_value))
if error>abs_tol:
raise Exception("Not within error tolerance")

y = -0.880


## Sample Problem 4¶

Integral over

abs_tol = .1
dim = 2
true_value = pi

integrand = CustomFun(
true_measure = Lebesgue(Gaussian(Lattice(dim,seed=7))),
g = lambda x: exp(-x**2).prod(1))
solution,data = CubQMCLatticeG(integrand,abs_tol=abs_tol).integrate()
print('y = %.3f'%solution)
error = abs((solution-true_value))
if error>abs_tol:
raise Exception("Not within error tolerance")

y = 3.141