Skip to main content
14 events
when toggle format what by license comment
Feb 24, 2021 at 4:02 vote accept Tom Solberg
Feb 23, 2021 at 4:10 comment added Iosif Pinelis I was indeed thinking, and trying a bit, to do this in Mathematica, but something did distract me.
Feb 23, 2021 at 1:04 comment added fedja @IosifPinelis OK, I guess I've finally figured out how I would do it (if not how it should be done). It would be interesting to compare with Mathematica in terms of time versus precision but I'm not a Mathematica user myself and you, probably, have some other fish to fry. Thank you for your interest in this problem anyway :-)
Feb 23, 2021 at 0:58 history edited fedja CC BY-SA 4.0
updated the code and added the description
Feb 23, 2021 at 0:30 history edited fedja CC BY-SA 4.0
updated the code
Feb 22, 2021 at 15:07 comment added fedja @J.J.Green Yeah, but this call of the square root does not affect performance in any noticeable way. And $S$ is really used only in the patch for degenerate rectangles, which should be really made cleaner anyway. Original multiple calls of sin() and cos() were a disaster though, so I replaced them with pure algebra. Qualitatively the precision is essentially the inverse time (even to the power $6/5$, but who cares). Full 4 convolutions would make it the inverse time squared, but that is quite ugly to implement so, unless very high precision is desired, I would rather not.
Feb 22, 2021 at 14:43 comment added Iosif Pinelis @fedja : Thank you for your comments.
Feb 22, 2021 at 9:03 comment added J.J. Green Your S would be better calculated using the (standard library) function hypot.
Feb 22, 2021 at 6:01 comment added fedja @IosifPinelis And yes, I decided to convolve $|x|$ only with 2 (largest) intervals analytically. The convolution with the remaining trapezoid is done by the Simpson approximation. You can do 3 convolutions with a bunch of "if" constructs but it is actually slower because branching is expensive and all four would be a nightmare.
Feb 22, 2021 at 5:56 comment added fedja @IosifPinelis 5 times as large really. The accuracy (in the updated version) is something like $1.3/N^2+0.04/n^3$ where $N$ is the number of lines and $n$ is the number of intervals in the Simpson approximation while the complexity is now proportional to $Nn$. So, for $N=50, n=4$ (the current setup) we have relative precision $0.0012$. To raise it ten times from there would take $N=160, n=9$, which promptly gives 105 second execution time (there is some fixed cost). Ten more times would be $N=500, n=20$ with 640 seconds to execute and that's where I would stop.
Feb 22, 2021 at 5:27 history edited fedja CC BY-SA 4.0
added 602 characters in body
Feb 21, 2021 at 22:28 comment added Iosif Pinelis Previous comment continued: However, this piecewise expression will consist of a very large number of pieces -- so far I have not been able to find it even with Mathematica's help. So, I guess you evaluate $I_t$ numerically. I can hardly read your C code. So, I am wondering, if from the $0.005$ precision you go down to $0.001$, will the execution time be something like $5^4$ or $5^5$ times as large?
Feb 21, 2021 at 22:27 comment added Iosif Pinelis This seems to be a clever use of the special form of the integrand. Are you using the identity $\|x\|=\frac14\,\int_0^{2\pi}|x\cdot e_t|\,dt$, where $x=(x_1,x_2)\in\mathbb R^2$, $\|x\|$ is the Euclidean norm of $x$, and $e_t=(\cos t,\sin t)$? So, in principle you can reduce the $4$-fold integral to an ordinary one (in $t\in[0,2\pi)$), using the explicit piecewise expression for $I_t:=\int_{P_1}dx\int_{P_2}dy\,|(x-y)\cdot e_t|$, where $P_1$ and $P_2$ are the rectangles.
Feb 21, 2021 at 19:38 history answered fedja CC BY-SA 4.0