Perhaps the use() function can do what you want.  Here's the docstring:

use(expr, func, level=0, args=(), kwargs={})
    Use ``func`` to transform ``expr`` at the given level.

    Example
    =======

    >>> from sympy import use, expand
    >>> from sympy.abc import x, y

    >>> f = (x + y)**2*x + 1

    >>> use(f, expand, level=2)
    x*(x**2 + 2*x*y + y**2) + 1
    >>> expand(f)
    x**3 + 2*x**2*y + x*y**2 + 1

By the way, if you are using this to map integrate() across an Add,
might I suggest that this is a bad idea.  It turns out that you cannot
just do this, like you can with derivatives.  The reason is that you
can have a sum for which each term it does not have an expressible
integral, but the whole sum does.  For example,  Integral(x**x, x) and
Integral(x**x*log(x), x) are both nonelementary functions, but
Integral(x**x + x**x*log(x), x) is an elementary function, as it's
equal to x**x.

Aaron Meurer

On Wed, Aug 10, 2011 at 3:42 PM, Chris Smith <smi...@gmail.com> wrote:
> On Thu, Aug 11, 2011 at 12:40 AM, Roberto Colistete Jr.
> <roberto.colist...@gmail.com> wrote:
>>   Hi,
>>
>>   I have not found a simple way to map a function to a mathematical
>> expression with sum of items. Is there anything simple to do it ? I
>> was thinking it would be simples, like "map(expand,expr)" but it
>> doesn't work.
>>
>>   In the meantime, I have solved this issue with the code :
>>
>> def mapexpr(self,expr,func):
>>     if isinstance(expr,Add):
>>         return apply(Add,map(func,expr.args))
>>     else:
>>          return func(expr)
>>
>
> It sounds like you want to apply the function term-wise. What you have
> written above can be simplified to
>
>    def map_termwise(func, expr):
>        return Add(*[func(a) for a in Add.make_args(expr)])
>
> There is also a Mul.make_args. Both Add and Mul make_args methods
> interpret the expr as an Add or Mul, respectively.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to sympy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sympy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to