On Wed, Jun 7, 2017 at 11:35 AM, <
bonc...@udel.edu> wrote:
> I'm trying to write a generic function to convolve two functions (in one
> dimension, for now) and came across a few issues I don't know how to solve.
> Rather than give up, maybe I can start a discussion. Some Sympy follows:
>
> a , b = symbols('a b', positive=True)
> f = exp(-a*x)
> g = exp(-b*x)
> integrate(f.subs(x,tau)*g.subs(x,t-tau), (tau,0,t))
>
>
> 1. I had to compute the range of integration myself (tau, 0, t). This isn't
> a fatal problem, but piecewise doesn't have enough information to compute it
> simply. (My long term solution is to propose a replacement for the
> piecewise function, but I'm not there yet.)
>
> The answer is below:
>
> Piecewise((t*exp(-b*t), Eq(a, b)), (1/(a*exp(b*t) - b*exp(b*t)) -
> 1/(a*exp(a*t) - b*exp(a*t)), True))
>
>
> The answer is correct, but there are problems trying to incorporate it into
> a larger function.
>
> 2. It is not obvious in advance the answer will depend on whether a = b. Is
> there some way to assume in advance that a=b or a!=b?
The assumptions don't work with this yet, but the simplest way is to just do
expr = integrate(f.subs(x,tau)*g.subs(x,t-tau), (tau,0,t))
expr.subs(Eq(a, b), True)
>
> 3. The second condition, True, isn't helpful. To understand what True
> means, one has to keep track of the previous conditions. Is there some way
> to replace the True with a != b.
I don't think there's a simple way to do it, but it can be represented
that way. It might be useful to have a Piecewise method that replaces
the True condition with the negation of the other conditions.
>
> 4. This is a simpler question, but I can't figure it out. How do I
> manipulate the second answer to my preferred form?
This is harder, because it's hard to get SymPy to prefer exp(-a) over
exp(a). Using simplify gives
(exp(a*t) - exp(b*t))*exp(-t*(a + b))/(a - b)
There is a related bug here
https://github.com/sympy/sympy/issues/11506. If it were fixed you
could use cancel(expr, exp(-a*t), exp(-b*t)).
The only way I found to do it is
simplify(expr.subs({a: -a, b: -b})).subs({a: -a, b: -b})
Aaron Meurer
>
> (exp(-b*t)-exp(-a*t))/(a-b)
>
> Is there a simple way to do it programmatically?
>
> Thanks
>
> --
> 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/9d71a081-e038-4712-93de-aca17f951e15%40googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.