integration with the square root of a non-negative expressions

103 views
Skip to first unread message

Andrew Corrigan

unread,
Feb 14, 2016, 10:27:09 AM2/14/16
to sympy
I'm having trouble computing a definite integral involving the sqrt of a non-negative expression, as implemented below (computing the length of a quadratic line in 2D).  It seems to fail.  I've generally had success using sympy for integration, except for when a sqrt is present.

If anyone has any advice on how to make this work, I would appreciate it tremendously.  Thanks in advance!

from sympy import *
x0,x1,x2,y0,y1,y2,xi = symbols('x0 x1 x2 y0 y1 y2 xi', real=True)
f_squared = (-4*x0*xi + 3*x0 - 4*x1*xi + x1 + 8*x2*xi - 4*x2)**2 + (4*xi*y0 + 4*xi*y1 - 8*xi*y2 - 3*y0 - y1 + 4*y2)**2
f = sqrt(f_squared)
integrate(f, (xi,0,1))





Oscar Benjamin

unread,
Feb 14, 2016, 7:47:08 PM2/14/16
to sympy
Distilling this down you want to compute the integral of the square
root of a quadratic. Sympy can do this in some simple cases:

>>> sqrt(1 + x**2).integrate(x)
________
╱ 2
x⋅╲╱ x + 1 asinh(x)
───────────── + ────────
2 2

However it fails for slightly more complicated cases:

>>> sqrt(1 + x + x**2).integrate(x)

⎮ ____________
⎮ ╱ 2
⎮ ╲╱ x + x + 1 dx


This second form can always be reduced to the first by completing the
square and changing variables. It seems that sympy is currently unable
to do that though :(

--
Oscar

Aaron Meurer

unread,
Feb 14, 2016, 8:43:56 PM2/14/16
to sy...@googlegroups.com
It looks like it can do it if you complete the square manually. So we just need to have better support for doing this in the algorithm(s).

In [7]: integrate(sqrt(((x + S(1)/2)**2 + S(3)/4)), x)
                                Out[7]:
                                                         ⎛2⋅√3⋅(x + 1/2)⎞
               3                                  3⋅asinh⎜──────────────⎟
      (x + 1/2)               3⋅(x + 1/2)                ⎝      3       ⎠
───────────────────── + ─────────────────────── + ───────────────────────
   __________________        __________________              8
  ╱            2            ╱            2
╲╱  4⋅(x + 1/2)  + 3    4⋅╲╱  4⋅(x + 1/2)  + 3


Aaron Meurer



--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxS5ys%2Bkfmz1-ZM3ekadA-gwwQ9e%2BDSGd1P4oXAkcKFn%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Corrigan

unread,
Feb 15, 2016, 10:01:29 AM2/15/16
to sympy
Thank you both for your replies.  I'm not sure I follow the discussion to be honest as to how it applies to my original problem.  In particular: 
Distilling this down you want to compute the integral of the square
root of a quadratic
I'm not sure that is accurate.  If you are just referring to that it is (foo(xi))**2  + (bar(xi))**2, then yes the expression is quadratic in foo and bar.  But in general, foo(xi) and bar(xi) are themselves higher degree polynomials of xi (and in higher dimensions other coordinates too).  This is a very simple and minimal reproducing example: in this case foo and bar are linear polynomials so the whole expression is quadratic.  I have expressions I need to integrate, where foo(xi) and bar(xi) are higher-order polynomials terms of xi.

Alan Bromborsky

unread,
Feb 15, 2016, 10:43:02 AM2/15/16
to sy...@googlegroups.com
Your are talking about reduction to elliptic integrals -

https://en.wikipedia.org/wiki/Elliptic_integral

I do not think that sympy can currently do this (it would be a great project)!


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Oscar Benjamin

unread,
Feb 15, 2016, 11:11:46 AM2/15/16
to sympy
On 15 February 2016 at 15:01, Andrew Corrigan <andrew....@gmail.com> wrote:
> Thank you both for your replies. I'm not sure I follow the discussion to be
> honest as to how it applies to my original problem. In particular:
>>>
>>> Distilling this down you want to compute the integral of the square
>>> root of a quadratic
>
> I'm not sure that is accurate.

It is for the example you showed :).

> If you are just referring to that it is
> (foo(xi))**2 + (bar(xi))**2, then yes the expression is quadratic in foo
> and bar. But in general, foo(xi) and bar(xi) are themselves higher degree
> polynomials of xi (and in higher dimensions other coordinates too). This is
> a very simple and minimal reproducing example: in this case foo and bar are
> linear polynomials so the whole expression is quadratic.

The example is not minimal. Much of your expression is a red herring
with symbols that are unimportant to people reading on this list. A
minimal example would be something like:

sqrt(ax^2 + bx + c)

> I have expressions
> I need to integrate, where foo(xi) and bar(xi) are higher-order polynomials
> terms of xi.

If you want to do sqrt(P(x)) with P(x) polynomial of degree k then I
think you can have general solutions for k=1,2,3 and 4 (assuming P(x)
has no repeated roots). Sympy can do k=1 and should be able to do 2
with a bit of help. For 3 and 4 you want the elliptic integrals
although maybe sympy doesn't do them yet.

For k>4 there may be solutions for certain special cases of the
polynomial coefficients. In general for a polynomial with symbolic
coefficients I don't think that there exist well-known mathematical
functions to represent the results.

--
Oscar

Andrew Corrigan

unread,
Feb 15, 2016, 11:41:03 AM2/15/16
to sy...@googlegroups.com
Oscar,

Thanks again for your response and clarification,
 
If you want to do sqrt(P(x)) with P(x) polynomial of degree k then I
think you can have general solutions for k=1,2,3 and 4 (assuming P(x)
has no repeated roots). Sympy can do k=1 and should be able to do 2
with a bit of help. For 3 and 4 you want the elliptic integrals
although maybe sympy doesn't do them yet.

For k>4 there may be solutions for certain special cases of the
polynomial coefficients. In general for a polynomial with symbolic
coefficients I don't think that there exist well-known mathematical
functions to represent the results.

Since ultimately I need to deal with the more general case (arbitrary k), it sounds like I better try to use numerical integration instead.

Reply all
Reply to author
Forward
0 new messages