Can not do byparts integral

22 views
Skip to first unread message

pallab

unread,
Jul 11, 2012, 1:23:01 PM7/11/12
to sy...@googlegroups.com
simple by parts integral can not be done:

integrate(diff(x*f(x),x),x)


also can not do simple integral like:

integrate(diff(x*sqrt(sin(x)),x),x)


best,

Pallab

Message has been deleted
Message has been deleted

pallab

unread,
Jul 12, 2012, 7:10:18 PM7/12/12
to sy...@googlegroups.com
Here I am attaching a very lazy code to integrate by parts.

from sympy import *
import itertools as it
import operator as op

def byparts(expr,x,depth=1):
    print depth
    hintarr=[]
    if expr.is_Mul:
        for part in it.combinations(expr.args,depth):
            part=reduce(op.mul,part)
            rest=simplify(expr/part)
            part_int=integrate(part,x)
            #print part_int,rest
            hint=simplify(part_int*diff(rest,x))
            integral=rest*part_int
            hintarr=hintarr+[(hint,integral)]
           
    hintarr=hintarr+[(x*diff(expr,x),x*expr)]
    return(dict(hintarr))

def integrate2(expr,x,depth=1):
    if expr.is_Add:
        for part in expr.args:
            rest=simplify(expr-part)
           
            for depth in range(1,len(part.args)+1):
                byparts_dict=byparts(part,x,depth)
                #print rest, list(byparts_dict)
                if rest in byparts_dict:
                    return(byparts_dict[rest])

Aaron Meurer

unread,
Jul 14, 2012, 10:49:21 PM7/14/12
to sy...@googlegroups.com
The problem with this method is that looking at all combinations
becomes *very* inefficient for Muls with many terms. For example:

In [41]: %timeit integrate2(diff(x*sqrt(sin(x))), x)
1
2
1
2
1
2
1
2
1 loops, best of 3: 5.2 s per loop

At the end of the day, you'll get even worse performance than the
currently used heurisch, with a much lower success rate. That isn't
to say that it isn't possible to make it more efficient, though.

For computing symbolic integration by parts, I think some logic in the
ODE module can be used, in particular, the exact ODE solver. For
example:

In [34]: print dsolve(diff(f(x)*x), simplify=False, hint='1st_exact')
x*f(x) == C1

It's possible to recognize exact equations of any order, though
solvers for them have not been implemented yet. But in general, if
you are trying to integrate an expression with a symbolic f(x),
converting it to a problem for dsolve() will give you better luck than
integrate() (but of course, we really should "integrate" the two).

Aaron Meurer

On Thu, Jul 12, 2012 at 5:05 PM, pallab <palla...@gmail.com> wrote:
> <code>
> <\code>
>
>
> On Wednesday, July 11, 2012 1:23:01 PM UTC-4, pallab wrote:
>>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/kqWwCQXDF1YJ.
>
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
Reply all
Reply to author
Forward
0 new messages