There are many similar questions but they all seem to be integrating functions that are more involved than just a product of polynomials and the issue has more to do with the integrand than the command Integrate[]
or the domain.
Edit: This is just an example of my more general problem. I'm writing an application that will need to integrate the product of two functions over arbitrarily complex domains. The user will not be able to validate all or any of the integrals. The functions will primarily be of the following types: Polynomials, Interpolation, and sometimes Gaussians
Example:
I'm integrating the product of two polynomials over a domain made up of many Disk[]
s and I get a small imaginary part!?
The functions:
Clear[p1, p2, reg];
p1[x_, y_] := 2 Sqrt[6] x y;
p2[x_, y_] :=
40 x^3 - 120 x y^2 - 120 x^3 (x^2 + y^2) + 360 x y^2 (x^2 + y^2) +
84 x^3 (x^2 + y^2)^2 - 252 x y^2 (x^2 + y^2)^2;
The Region:
reg = BooleanRegion[(#1 && ! #2 && #3 && #4) ||
(#5 && ! #6 && #7 && #8) ||
(#9 && ! #10 && #11 && #12) ||
(#13 && ! #14 && #15 && #16) ||
(#17 && ! #18 && #19 && #20) ||
(#21 && ! #22 && #23 && #24) ||
(#25 && ! #26 && #27 && #28) ||
(#29 && ! #30 && #31 && #32) ||
(#33 && ! #34 && #35 && #36) &,
{Disk[{4.0625`, 0.`}, 1],
Disk[{4.0625`, 0.`}, 0.1`], Disk[{2.8125`, 0.`}, 2.25`],
Disk[{0, 0}, 5.0625`], Disk[{2.1875`, 1.0825317547305482`}, 1],
Disk[{2.1875`, 1.0825317547305482`}, 0.1`],
Disk[{2.8125`, 0.`}, 2.25`], Disk[{0, 0}, 5.0625`],
Disk[{2.1875`, -1.0825317547305482`}, 1],
Disk[{2.1875`, -1.0825317547305482`}, 0.1`],
Disk[{2.8125`, 0.`}, 2.25`], Disk[{0, 0}, 5.0625`],
Disk[{-0.15625`, 2.4356964481437338`}, 1],
Disk[{-0.15625`, 2.4356964481437338`}, 0.1`],
Disk[{-1.40625`, 2.4356964481437338`}, 2.25`],
Disk[{0, 0}, 5.0625`], Disk[{-2.03125`, 3.518228202874282`}, 1],
Disk[{-2.03125`, 3.518228202874282`}, 0.1`],
Disk[{-1.40625`, 2.4356964481437338`}, 2.25`],
Disk[{0, 0}, 5.0625`], Disk[{-2.03125`, 1.3531646934131856`}, 1],
Disk[{-2.03125`, 1.3531646934131856`}, 0.1`],
Disk[{-1.40625`, 2.4356964481437338`}, 2.25`],
Disk[{0, 0}, 5.0625`], Disk[{-0.15625`, -2.4356964481437338`}, 1],
Disk[{-0.15625`, -2.4356964481437338`}, 0.1`],
Disk[{-1.40625`, -2.4356964481437338`}, 2.25`],
Disk[{0, 0}, 5.0625`], Disk[{-2.03125`, -1.3531646934131856`}, 1],
Disk[{-2.03125`, -1.3531646934131856`}, 0.1`],
Disk[{-1.40625`, -2.4356964481437338`}, 2.25`],
Disk[{0, 0}, 5.0625`], Disk[{-2.03125`, -3.518228202874282`}, 1],
Disk[{-2.03125`, -3.518228202874282`}, 0.1`],
Disk[{-1.40625`, -2.4356964481437338`}, 2.25`],
Disk[{0, 0}, 5.0625`]}];
I do the Intgral:
Integrate[p1[x, y] p2[x, y] , {x, y} ∈ reg]
and get:
(* output -3.99966 - 4.69926*10^-32 I *)
It is a very small Imaginary part and I'm tempted to just take the real part and pretend nothing happened. When I use NIntegrate[]
I get the same result but it complains about converging too slowly suggesting that Integrate[]
is not using NIntegrate[]
to do the evaluation.
What should I do? Can I trust the real part?
Rationalize
into your reg calculation thusreg=BooleanRegion[..., Rationalize[{YourListOfDisk},0]]
and then do your integral otherwise unchanged then I get exactly precisely0
. Can you reproduce this? Can you explain this? I don't think I have made a mistake, but that is always a possibility. And I certainly can't explain this. Well, not yet. $\endgroup$Integrate[p1[x, y]*p2[x, y], {x, y} \[Element] Rationalize[reg, 0]]
$\endgroup$Rationalize[reg, 0]
Idea. I also can force or encourage the creation of the domain to use rational numbers. But still, I do not see how an imaginary number slipped in from integrating simple real functions over a real domain. $\endgroup$Integrate
. Some of those algorithms are triggered by the presence of a decimal point. Others are triggered by the lack of a decimal point. Every time I see "Why do I get a microscopic complex part?" I think "get rid of the decimal points and see what happens. Since you got4-epsilon +I*epsilon
I expected to get4
and was surprised I got0
. But I served my role by getting someone to then look more deeply at your problem and see why the result should be0
. $\endgroup$