ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 24 Nov 2014 02:34:40 +0100Condition in sum() function?https://ask.sagemath.org/question/24946/condition-in-sum-function/ Hello,
I'm trying to use the exponential form of the Fourier series representation of a function to plot an approximation of said function using the first five terms.
The actual function is f(t) = 1/2 + j/(2*pi) * E from (n = -5) to (n = 5) of ((-1)^n - 1)/n * e^(j*2*n*pi*t), where n =/= 0
Here I'm using 'E' to indicate summation notation. I apologize if this deviates from an established standard, but I'm having trouble uploading images right now (which would have made the function clearer).
The code I'm using for this function is
var('n, t')
j = i # imaginary unit
expr = ((-1)^n - 1)/n * exp(2*pi*n*j*t) # I think the problem is the division by 'n' here
assume(n, 'integer'); assume(n != 0)
f(t) = 1/2 + j/(2*pi) * sum(expr, n, -5, 5)
However, when I try this, I get the following exception:
<code>
RuntimeError: ECL says: Error executing code in Maxima: expt: undefined: 0 to a negative exponent.
</code>
This seems to be due to the division by <code>n</code> in <code>expr</code>. Are my assumptions not working here? Thus, my actual question is how can I add the condition <code>n != 0</code> to the <code>sum()</code> function in sage?Sat, 22 Nov 2014 19:50:08 +0100https://ask.sagemath.org/question/24946/condition-in-sum-function/Comment by kcrisman for <p>Hello,</p>
<p>I'm trying to use the exponential form of the Fourier series representation of a function to plot an approximation of said function using the first five terms. </p>
<p>The actual function is f(t) = 1/2 + j/(2<em>pi) * E from (n = -5) to (n = 5) of ((-1)^n - 1)/n * e^(j</em>2<em>n</em>pi*t), where n =/= 0</p>
<p>Here I'm using 'E' to indicate summation notation. I apologize if this deviates from an established standard, but I'm having trouble uploading images right now (which would have made the function clearer). </p>
<p>The code I'm using for this function is</p>
<pre><code>var('n, t')
j = i # imaginary unit
expr = ((-1)^n - 1)/n * exp(2*pi*n*j*t) # I think the problem is the division by 'n' here
assume(n, 'integer'); assume(n != 0)
f(t) = 1/2 + j/(2*pi) * sum(expr, n, -5, 5)
</code></pre>
<p>However, when I try this, I get the following exception:</p>
<p><code>
RuntimeError: ECL says: Error executing code in Maxima: expt: undefined: 0 to a negative exponent.
</code></p>
<p>This seems to be due to the division by <code>n</code> in <code>expr</code>. Are my assumptions not working here? Thus, my actual question is how can I add the condition <code>n != 0</code> to the <code>sum()</code> function in sage?</p>
https://ask.sagemath.org/question/24946/condition-in-sum-function/?comment=24949#post-id-24949Unfortunately, Maxima's assumption framework is not particularly strong. But Sage definitely won't know about it in your sum, since you are now using `n` also as a kind of dummy variable. I think it might be easier to just make two sums in this case, because 0 is *only* number you want to avoid in your general case, right?Sun, 23 Nov 2014 03:33:30 +0100https://ask.sagemath.org/question/24946/condition-in-sum-function/?comment=24949#post-id-24949Comment by mpatton for <p>Hello,</p>
<p>I'm trying to use the exponential form of the Fourier series representation of a function to plot an approximation of said function using the first five terms. </p>
<p>The actual function is f(t) = 1/2 + j/(2<em>pi) * E from (n = -5) to (n = 5) of ((-1)^n - 1)/n * e^(j</em>2<em>n</em>pi*t), where n =/= 0</p>
<p>Here I'm using 'E' to indicate summation notation. I apologize if this deviates from an established standard, but I'm having trouble uploading images right now (which would have made the function clearer). </p>
<p>The code I'm using for this function is</p>
<pre><code>var('n, t')
j = i # imaginary unit
expr = ((-1)^n - 1)/n * exp(2*pi*n*j*t) # I think the problem is the division by 'n' here
assume(n, 'integer'); assume(n != 0)
f(t) = 1/2 + j/(2*pi) * sum(expr, n, -5, 5)
</code></pre>
<p>However, when I try this, I get the following exception:</p>
<p><code>
RuntimeError: ECL says: Error executing code in Maxima: expt: undefined: 0 to a negative exponent.
</code></p>
<p>This seems to be due to the division by <code>n</code> in <code>expr</code>. Are my assumptions not working here? Thus, my actual question is how can I add the condition <code>n != 0</code> to the <code>sum()</code> function in sage?</p>
https://ask.sagemath.org/question/24946/condition-in-sum-function/?comment=24962#post-id-24962Yes, I only want to avoid n = 0. Thank you for your input, I ended up using the answer below.Mon, 24 Nov 2014 02:29:57 +0100https://ask.sagemath.org/question/24946/condition-in-sum-function/?comment=24962#post-id-24962Answer by tmonteil for <p>Hello,</p>
<p>I'm trying to use the exponential form of the Fourier series representation of a function to plot an approximation of said function using the first five terms. </p>
<p>The actual function is f(t) = 1/2 + j/(2<em>pi) * E from (n = -5) to (n = 5) of ((-1)^n - 1)/n * e^(j</em>2<em>n</em>pi*t), where n =/= 0</p>
<p>Here I'm using 'E' to indicate summation notation. I apologize if this deviates from an established standard, but I'm having trouble uploading images right now (which would have made the function clearer). </p>
<p>The code I'm using for this function is</p>
<pre><code>var('n, t')
j = i # imaginary unit
expr = ((-1)^n - 1)/n * exp(2*pi*n*j*t) # I think the problem is the division by 'n' here
assume(n, 'integer'); assume(n != 0)
f(t) = 1/2 + j/(2*pi) * sum(expr, n, -5, 5)
</code></pre>
<p>However, when I try this, I get the following exception:</p>
<p><code>
RuntimeError: ECL says: Error executing code in Maxima: expt: undefined: 0 to a negative exponent.
</code></p>
<p>This seems to be due to the division by <code>n</code> in <code>expr</code>. Are my assumptions not working here? Thus, my actual question is how can I add the condition <code>n != 0</code> to the <code>sum()</code> function in sage?</p>
https://ask.sagemath.org/question/24946/condition-in-sum-function/?answer=24961#post-id-24961Only ``t`` is a symbol, so you can define ``expr`` as a (Python) function of the (Python) variable ``n`` and then sum over the good set of ``n``'s.
sage: t = var('t')
sage: j = i
sage: expr = lambda n : ((-1)^n - 1)/n * exp(2*pi*n*j*t)
sage: f(t) = 1/2 + j/(2*pi) * sum([expr(n) for n in range(-5,6) if n != 0])
sage: f(t)
-1/15*I*(3*e^(10*I*pi*t) + 5*e^(6*I*pi*t) + 15*e^(2*I*pi*t) - 15*e^(-2*I*pi*t) - 5*e^(-6*I*pi*t) - 3*e^(-10*I*pi*t))/pi + 1/2
Mon, 24 Nov 2014 01:57:02 +0100https://ask.sagemath.org/question/24946/condition-in-sum-function/?answer=24961#post-id-24961Comment by mpatton for <p>Only <code>t</code> is a symbol, so you can define <code>expr</code> as a (Python) function of the (Python) variable <code>n</code> and then sum over the good set of <code>n</code>'s.</p>
<pre><code>sage: t = var('t')
sage: j = i
sage: expr = lambda n : ((-1)^n - 1)/n * exp(2*pi*n*j*t)
sage: f(t) = 1/2 + j/(2*pi) * sum([expr(n) for n in range(-5,6) if n != 0])
sage: f(t)
-1/15*I*(3*e^(10*I*pi*t) + 5*e^(6*I*pi*t) + 15*e^(2*I*pi*t) - 15*e^(-2*I*pi*t) - 5*e^(-6*I*pi*t) - 3*e^(-10*I*pi*t))/pi + 1/2
</code></pre>
https://ask.sagemath.org/question/24946/condition-in-sum-function/?comment=24963#post-id-24963This is precisely what I was looking for. I didn't even think to use <code>n</code> as a Python variable. Thank you sir.Mon, 24 Nov 2014 02:34:40 +0100https://ask.sagemath.org/question/24946/condition-in-sum-function/?comment=24963#post-id-24963