I want to perform a double integration over a line segment in 2D and I am wondering if can it be done in Mathematica. An added difficulty is that the integral is singular.

$$I = \int_{(4,4)}^{(2,8)}\int_{(4,4)}^{(2,8 )} \log|x-y| \,\mathrm{d}y \, \mathrm{d}x.$$

I am not that concerned with efficiency here, I would just like to have a nice way to integrate these types of integrals compared to how I have to do it in Python which involves analytically regularizing them first to deal with the singular integrands and then transformation to reference intervals before finally coding the actual integration.

So can this be done 'nicely' in Mathematica?

Edit My Mathematica code that gives the incorrect answer:

     Log[Norm[{x1, x2} - {y1, y2}]], 
     {x1, x2} ∈ Line[{{4,4},{2,8}}], 
     {y1, y2} ∈ Line[{{4,4},{2,8}}]
  • $\begingroup$ What do the integral limits mean? $\endgroup$ – Henrik Schumacher Feb 12 at 12:51
  • $\begingroup$ @HenrikSchumacher The limits mean integrate over the straight line segment from the point $[3,3]$ to the point $[2,5]$. $\endgroup$ – eurocoder Feb 12 at 13:36
  • $\begingroup$ @HenrikSchumacher Actually I just noticed it can be done using the method you used in a previous question I asked here - mathematica.stackexchange.com/questions/190728/… $\endgroup$ – eurocoder Feb 12 at 13:46
  • $\begingroup$ However it gives me the result: -0.0435146 whereas I get -0.042677 in Python! I get alot of warnings from Mathematica too before it gives me that result and the 'error estimate': 0.008498. $\endgroup$ – eurocoder Feb 12 at 13:47

Putting the singularities on the boundary of the integration domain makes NIntegrate take care of them. Plus, this utilizes the fact that the integrant is symmetric, so that the work can be reduced by half.

p = {2, 8};
q = {4, 4};
2 Norm[p - q]^2 NIntegrate[
  Log[Norm[(p (1 - t) + t q) - (p (1 - s) + s q)]], 
  {t, 0, 1}, {s, t, 1}]


If you are in need of integrating this integrant over more general and more complicated domains and if speed is an issue, the fast multipole method and related techniques might be your friends.

  • $\begingroup$ Ok cool, I got the same result in Python! I will edit my original post to show the Mathematica code I used that gave the wrong result. It would be great if you could identify why it is wrong as I would like to able to compute integrals in this form. $\endgroup$ – eurocoder Feb 12 at 13:53
  • $\begingroup$ I plan to implement the FMM eventually but for the moment I'm just trying to make sure all my singular integral evaluations are correct! $\endgroup$ – eurocoder Feb 12 at 13:56
  • $\begingroup$ Do you think the edited code I posted in the OP can be 'fixed' to give the correct answer? It would be great if I could evaluate integrals using expressions in Mathematica like that rather than having to transform them first like I have to do with Python. $\endgroup$ – eurocoder Feb 12 at 13:57
  • 2
    $\begingroup$ I'm not sure if this warrants an answer or not but NIntegrate[Log[Norm[{x1, x2} - {y1, y2}]] /. Abs -> Identity, {x1, x2} ∈ Line[{{4, 4}, {2, 8}}], {y1, y2} ∈ Line[{{4, 4}, {2, 8}}]] gets preprocessed to NIntegrate[20 Log[2 Sqrt[5] Sqrt[(x1 - x2)^2]], {x1, 0, 1}, {x2, 0, 1}] internally and this is where it chokes. $\endgroup$ – Chip Hurst Feb 12 at 14:33
  • 1
    $\begingroup$ @ChipHurst Hm. Interesting. From there one could go by NIntegrate[20 Log[2 Sqrt[5] Sqrt[(x1 - x2)^2]], {x1, 0, 1}, {x2, 0, 1}, Exclusions -> {x1 == x2} ]. Unfortunately, NIntegrate with regions refused to accept a setting for Exclusions. $\endgroup$ – Henrik Schumacher Feb 12 at 14:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.