I am trying to calculate a seemingly simple integral. Here is the MMA code:
In[1]:= rect[x_] = If[x <= 0.5, 1, 0];
In[2]:= N[Integrate[Integrate[rect[(x^2 + y^2)^.5], {x, 0, 100.}], {y, 0., 100.}]]
Out[3]= 0.19635
However, when I do that with the cubature package in Python, I cannot get anything reasonable for the integration limits over 13, and even for (-13,13) it lies. It really has to be something like +/- 10.
Here is the python code:
import time
import numpy as np
from cubature import cubature
start = time.time()
def rect(r):
return np.where(abs(r) <= 0.5, 1, 0)
def Wp(r1, r2):
return rect((r1 ** 2 + r2 ** 2) ** 0.5)
def integrand_rectangle(x_array):
return Wp(x_array[:, 0], x_array[:, 1])
# boundaries
a = 0.0
b_x = 100.0
xmin_t = [a, a]
xmax_t = [b_x, b_x]
val_t, err_t = cubature(integrand_rectangle, 2, 1, xmin_t, xmax_t, vectorized=True)
print(val_t)
end = time.time()
print(f"Runtime of the program is {end - start}")
I have tried to play with the number of interactions, relative and absolute error tolerances, nothing seems to cure this. Is there a solution? Why can't this well-tested package perform such a simple integration? Am I doing it wrong?
I have tried original C-cubature and it works perfect and fast! It is a problem of the python -- C interface! It is a bug!