On Wed, Jun 7, 2017 at 3:12 PM, <[email protected]> wrote: > BTW, when I'm demonstrating sympy to people, I show them we can test our > preferred form for equality. (Sympy is handy for verifying hand > calculations or standard formulas found in books.) > > h = integrate(f.subs(x,tau)*g.subs(x,t-tau), (tau,0,t)) > simplify(h.args[1][0] - (exp(-b*t)-exp(-a*t))/(a-b)) > > 0 > > Your last simplify trick is impressive. It works, but isn't obvious.
It works because SymPy wants to treat exp(-a*t) as 1/exp(a*t), and simplify doesn't want to have nested fractions. So I force it into a form that isn't a fraction, call simplify(), then put it back. > > My thoughts on Piecewise is that it's trying to do too many things. There's > a difference between representing a function whose form changes depending on > the value of its argument (in my case, t) and an answer that depends on > parameter values (a and b). The latter distinction is best described with a > cases statement. I.e., t is different from a and b. I'm not sure I follow here. Are you suggesting to use a separate object. What would it look like? > > In signal processing (my area) we often have signals whose form changes as t > changes. Convolving them must be done piece by piece and the pieces > assembled into a whole. (The assembling difficulty is that each convolved > piece has a bigger domain than either of the input pieces--but this can be > managed.) My roadblock is when the integration depends on parameter values > (as above). I believe this old issue about integrating piecewise expressions where the variable is in the conditions is still relevant https://github.com/sympy/sympy/issues/5227#issuecomment-36998678. Aaron Meurer > > Charlie > > On Wednesday, June 7, 2017 at 12:31:15 PM UTC-4, Aaron Meurer wrote: >> >> On Wed, Jun 7, 2017 at 11:35 AM, <[email protected]> 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 [email protected]. >> > To post to this group, send email to [email protected]. >> > 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. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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/e0836c94-efe2-429d-ace6-c5385b181222%40googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAKgW%3D6JdW1MUhsUDxDLjkBMQjpVWgjjPuOiM%3DRdpX-e9RTZJWA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
