ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 16 Feb 2015 02:10:46 -0600A symbolic convolution of arbitrary functionshttp://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/In [this manual](http://www.sagemath.org/doc/constructions/calculus.html#convolution) it is described how to get a symbolic result of "the convolution of any piecewise defined function with another". However, when I try this with functions like exponent, this does not work:
x = PolynomialRing(QQ, 'x').gen()
f = Piecewise([[(0,1),exp(x)]])
f.convolution(f)
I get an error "RuntimeError: Symbolic Ring still using old coercion framework"
If I use a 'RR' ring instead of 'QQ', Piecewise() returns another error "TypeError: cannot coerce arguments: __call__() takes exactly 1 positional argument (0 given)"
I have 2 questions:
1. Is there a way to get a function, which represent convolution of a gaussian function and a decaying exponent (and, generally, any functions)? I want to fit my data with such a function.
2. How to get a symbolical convolution of functions with parameters (they should be assumed to be constants during convolution computation)?Wed, 23 Nov 2011 17:01:36 -0600http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/Answer by ADuC812 for <p>In <a href="http://www.sagemath.org/doc/constructions/calculus.html#convolution">this manual</a> it is described how to get a symbolic result of "the convolution of any piecewise defined function with another". However, when I try this with functions like exponent, this does not work:</p>
<pre><code>x = PolynomialRing(QQ, 'x').gen()
f = Piecewise([[(0,1),exp(x)]])
f.convolution(f)
</code></pre>
<p>I get an error "RuntimeError: Symbolic Ring still using old coercion framework"</p>
<p>If I use a 'RR' ring instead of 'QQ', Piecewise() returns another error "TypeError: cannot coerce arguments: __call__() takes exactly 1 positional argument (0 given)" </p>
<p>I have 2 questions:</p>
<ol>
<li>Is there a way to get a function, which represent convolution of a gaussian function and a decaying exponent (and, generally, any functions)? I want to fit my data with such a function.</li>
<li>How to get a symbolical convolution of functions with parameters (they should be assumed to be constants during convolution computation)?</li>
</ol>
http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/?answer=12964#post-id-12964The possible way is to use convolution definition and to find symbolically the corresponding integral. This may be done without piecewise definition:
lg2=log(2).N()
gaussian(x,x_0,FWHM,A)=A*exp(-(x-x_0)^2/(2.0*(FWHM^2/4.0*lg2)))
expdec(x,S,x_0,tau)=S/tau*exp(-(x-x_0)/tau)
var('t, x_0, FWHM, y_0, S_1, tau_1')
assume (t>0)
subint_dec1(s,t,x_0,FWHM,a_1,tau_1)=gaussian(x=s,x_0=x_0,FWHM=FWHM,A=1)*expdec(x=t-s,x_0=x_0,S=S_1,tau=tau_1)
exp_decay=subint_dec1.integral(s,0,t)
However, the resulting function works rather slow for fitting purposesFri, 02 Dec 2011 21:55:41 -0600http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/?answer=12964#post-id-12964Comment by ADuC812 for <p>The possible way is to use convolution definition and to find symbolically the corresponding integral. This may be done without piecewise definition: </p>
<pre><code>lg2=log(2).N()
gaussian(x,x_0,FWHM,A)=A*exp(-(x-x_0)^2/(2.0*(FWHM^2/4.0*lg2)))
expdec(x,S,x_0,tau)=S/tau*exp(-(x-x_0)/tau)
var('t, x_0, FWHM, y_0, S_1, tau_1')
assume (t>0)
subint_dec1(s,t,x_0,FWHM,a_1,tau_1)=gaussian(x=s,x_0=x_0,FWHM=FWHM,A=1)*expdec(x=t-s,x_0=x_0,S=S_1,tau=tau_1)
exp_decay=subint_dec1.integral(s,0,t)
</code></pre>
<p>However, the resulting function works rather slow for fitting purposes</p>
http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/?comment=20772#post-id-20772I wonder how to make it faster. I tried fast_callable(), but it does not seem to help.Fri, 02 Dec 2011 21:58:31 -0600http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/?comment=20772#post-id-20772Answer by kcrisman for <p>In <a href="http://www.sagemath.org/doc/constructions/calculus.html#convolution">this manual</a> it is described how to get a symbolic result of "the convolution of any piecewise defined function with another". However, when I try this with functions like exponent, this does not work:</p>
<pre><code>x = PolynomialRing(QQ, 'x').gen()
f = Piecewise([[(0,1),exp(x)]])
f.convolution(f)
</code></pre>
<p>I get an error "RuntimeError: Symbolic Ring still using old coercion framework"</p>
<p>If I use a 'RR' ring instead of 'QQ', Piecewise() returns another error "TypeError: cannot coerce arguments: __call__() takes exactly 1 positional argument (0 given)" </p>
<p>I have 2 questions:</p>
<ol>
<li>Is there a way to get a function, which represent convolution of a gaussian function and a decaying exponent (and, generally, any functions)? I want to fit my data with such a function.</li>
<li>How to get a symbolical convolution of functions with parameters (they should be assumed to be constants during convolution computation)?</li>
</ol>
http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/?answer=12940#post-id-12940I can't fix this for you (yet?), but I see why this wasn't detected before - the example you reference uses
f = Piecewise([[(0,1),1*x^0]])
which becomes
sage: f
Piecewise defined function with 1 parts, [[(0, 1), 1]]
so the variables don't come into play. Also, the example (and yours) uses a polynomial variable, which of course (?) becomes non-polynomial once `e^x` is involved. So there are two strikes against this.
It's not clear to me that there is an easy way to get around this, because there isn't an obvious way to turn the polynomial generator `x` into an exponential, but the code for convolution relies pretty heavily on having those polynomial generators.
----------
Stock disclaimer: all piecewise material dates from before Sage had any true symbolic capabilities. It has since languished. See the piecewise tag here and questions under it, such as [this one](http://ask.sagemath.org/question/145/how-do-i-define-a-piecewise-function).
----------
I'm hoping someone more familiar with some of the numerical tools in Sage can help you with your underlying questions. You can certainly get some convolutions, e.g. [discrete ones in Numpy](http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html), but your use case might be a little trickier.
Maxima used directly might be of help, or perhaps using its integration as [at this Maxima feature request](http://sourceforge.net/tracker/?func=detail&aid=2863823&group_id=4933&atid=354933) or using its [piecewise capabilities](http://sourceforge.net/projects/piecewisefunc/files/pw%206.2/).Fri, 25 Nov 2011 14:48:18 -0600http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/?answer=12940#post-id-12940Comment by rws for <p>I can't fix this for you (yet?), but I see why this wasn't detected before - the example you reference uses</p>
<pre><code>f = Piecewise([[(0,1),1*x^0]])
</code></pre>
<p>which becomes</p>
<pre><code>sage: f
Piecewise defined function with 1 parts, [[(0, 1), 1]]
</code></pre>
<p>so the variables don't come into play. Also, the example (and yours) uses a polynomial variable, which of course (?) becomes non-polynomial once <code>e^x</code> is involved. So there are two strikes against this.</p>
<p>It's not clear to me that there is an easy way to get around this, because there isn't an obvious way to turn the polynomial generator <code>x</code> into an exponential, but the code for convolution relies pretty heavily on having those polynomial generators. </p>
<hr/>
<p>Stock disclaimer: all piecewise material dates from before Sage had any true symbolic capabilities. It has since languished. See the piecewise tag here and questions under it, such as <a href="http://ask.sagemath.org/question/145/how-do-i-define-a-piecewise-function">this one</a>.</p>
<hr/>
<p>I'm hoping someone more familiar with some of the numerical tools in Sage can help you with your underlying questions. You can certainly get some convolutions, e.g. <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html">discrete ones in Numpy</a>, but your use case might be a little trickier.</p>
<p>Maxima used directly might be of help, or perhaps using its integration as <a href="http://sourceforge.net/tracker/?func=detail&aid=2863823&group_id=4933&atid=354933">at this Maxima feature request</a> or using its <a href="http://sourceforge.net/projects/piecewisefunc/files/pw%206.2/">piecewise capabilities</a>.</p>
http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/?comment=25839#post-id-25839The last link (now) leads to a spam page.Mon, 16 Feb 2015 02:10:46 -0600http://ask.sagemath.org/question/8507/a-symbolic-convolution-of-arbitrary-functions/?comment=25839#post-id-25839