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.Fri, 01 Nov 2013 11:52:47 +0100Why Sage cannot pass a value of variable from one function to another nested function?https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/The first i ran this:
sage: f(x)=(2/n)*(sin(n*x)*(-1)^(n+1))
sage: sum(f, n, 1, 2) #using summation function
-sin(2*x) + 2*sin(x)
So, In this case the result was evaluated correctly.
But if i tried to combine the first line and the second line together:
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f(x,2)
-2*sum((-1)^n*sin(n*x)/n, n, 1, 2)
The result wasn't finished!
Why sage cannot evaluate mathematical expression in this case?
Another tried to prove that Sage can pass its variable from left function to right function even though the right function was a nested function:
sage: f(x) = sin(arcsin(x))
sage: f(0.5)
0.500000000000000
Edit:
(See [the same question on SO](http://stackoverflow.com/questions/19711247/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-fun).)
Thu, 31 Oct 2013 13:09:56 +0100https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/Answer by twch for <p>The first i ran this:</p>
<pre><code>sage: f(x)=(2/n)*(sin(n*x)*(-1)^(n+1))
sage: sum(f, n, 1, 2) #using summation function
-sin(2*x) + 2*sin(x)
</code></pre>
<p>So, In this case the result was evaluated correctly.</p>
<p>But if i tried to combine the first line and the second line together:</p>
<pre><code>sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f(x,2)
-2*sum((-1)^n*sin(n*x)/n, n, 1, 2)
</code></pre>
<p>The result wasn't finished! </p>
<p>Why sage cannot evaluate mathematical expression in this case?</p>
<p>Another tried to prove that Sage can pass its variable from left function to right function even though the right function was a nested function: </p>
<pre><code>sage: f(x) = sin(arcsin(x))
sage: f(0.5)
0.500000000000000
</code></pre>
<p>Edit:
(See <a href="http://stackoverflow.com/questions/19711247/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-fun">the same question on SO</a>.)</p>
https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?answer=15636#post-id-15636You can avoid this problem if you don't use a symbolic function but define a python function:
sage: var('n')
sage: def g(x,k):
sage: return sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
sage: print g(x,2)
-sin(2*x) + 2*sin(x)
Fri, 01 Nov 2013 09:36:13 +0100https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?answer=15636#post-id-15636Comment by kcrisman for <p>You can avoid this problem if you don't use a symbolic function but define a python function:</p>
<pre><code>sage: var('n')
sage: def g(x,k):
sage: return sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
sage: print g(x,2)
-sin(2*x) + 2*sin(x)
</code></pre>
https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16820#post-id-16820I like this! I tried this using lambdas but it didn't work correctly, but this is a good workaround.Fri, 01 Nov 2013 11:31:00 +0100https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16820#post-id-16820Answer by kcrisman for <p>The first i ran this:</p>
<pre><code>sage: f(x)=(2/n)*(sin(n*x)*(-1)^(n+1))
sage: sum(f, n, 1, 2) #using summation function
-sin(2*x) + 2*sin(x)
</code></pre>
<p>So, In this case the result was evaluated correctly.</p>
<p>But if i tried to combine the first line and the second line together:</p>
<pre><code>sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f(x,2)
-2*sum((-1)^n*sin(n*x)/n, n, 1, 2)
</code></pre>
<p>The result wasn't finished! </p>
<p>Why sage cannot evaluate mathematical expression in this case?</p>
<p>Another tried to prove that Sage can pass its variable from left function to right function even though the right function was a nested function: </p>
<pre><code>sage: f(x) = sin(arcsin(x))
sage: f(0.5)
0.500000000000000
</code></pre>
<p>Edit:
(See <a href="http://stackoverflow.com/questions/19711247/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-fun">the same question on SO</a>.)</p>
https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?answer=15506#post-id-15506 sage: n, k = var('n, k')
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f
(x, k) |--> -2*sum((-1)^n*sin(n*x)/n, n, 1, k)
I'm not sure what you think is wrong here. The 2 and a factor of -1 were both factored out, that's all.
However, I do agree that this doesn't expand. What is happening is that we are sending the sum to Maxima
if algorithm == 'maxima':
return maxima.sr_sum(expression,v,a,b)
and then ordinarily when it returns, it is still a Maxima object (which may be a bug?). But when we put it in the function, it becomes a Sage object - but we don't have a Sage "sum" object. So I think that is what would have to be fixed.
----------
That this is *possible* is shown by the following Maxima example (which I put on the ticket):
(%i1) f: -2*'sum((-1)^n*sin(n*x)/n,n,1,2);
2
==== n
\ (- 1) sin(n x)
(%o1) - 2 > ---------------
/ n
====
n = 1
(%i8) f, nouns;
sin(2 x)
(%o8) - 2 (-------- - sin(x))
2Thu, 31 Oct 2013 13:24:50 +0100https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?answer=15506#post-id-15506Comment by kcrisman for <pre><code>sage: n, k = var('n, k')
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f
(x, k) |--> -2*sum((-1)^n*sin(n*x)/n, n, 1, k)
</code></pre>
<p>I'm not sure what you think is wrong here. The 2 and a factor of -1 were both factored out, that's all.</p>
<p>However, I do agree that this doesn't expand. What is happening is that we are sending the sum to Maxima</p>
<pre><code>if algorithm == 'maxima':
return maxima.sr_sum(expression,v,a,b)
</code></pre>
<p>and then ordinarily when it returns, it is still a Maxima object (which may be a bug?). But when we put it in the function, it becomes a Sage object - but we don't have a Sage "sum" object. So I think that is what would have to be fixed.</p>
<hr/>
<p>That this is <em>possible</em> is shown by the following Maxima example (which I put on the ticket):</p>
<pre><code>(%i1) f: -2*'sum((-1)^n*sin(n*x)/n,n,1,2);
2
==== n
\ (- 1) sin(n x)
(%o1) - 2 > ---------------
/ n
====
n = 1
(%i8) f, nouns;
sin(2 x)
(%o8) - 2 (-------- - sin(x))
2
</code></pre>
https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16819#post-id-16819Well, I don't know about a bug, since deciding for an arbitrary expression whether it happens to be zero is very, very hard... but it is decidedly suboptimal, so I'll open a ticket for this issue.Fri, 01 Nov 2013 11:31:32 +0100https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16819#post-id-16819Comment by terces907 for <pre><code>sage: n, k = var('n, k')
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f
(x, k) |--> -2*sum((-1)^n*sin(n*x)/n, n, 1, k)
</code></pre>
<p>I'm not sure what you think is wrong here. The 2 and a factor of -1 were both factored out, that's all.</p>
<p>However, I do agree that this doesn't expand. What is happening is that we are sending the sum to Maxima</p>
<pre><code>if algorithm == 'maxima':
return maxima.sr_sum(expression,v,a,b)
</code></pre>
<p>and then ordinarily when it returns, it is still a Maxima object (which may be a bug?). But when we put it in the function, it becomes a Sage object - but we don't have a Sage "sum" object. So I think that is what would have to be fixed.</p>
<hr/>
<p>That this is <em>possible</em> is shown by the following Maxima example (which I put on the ticket):</p>
<pre><code>(%i1) f: -2*'sum((-1)^n*sin(n*x)/n,n,1,2);
2
==== n
\ (- 1) sin(n x)
(%o1) - 2 > ---------------
/ n
====
n = 1
(%i8) f, nouns;
sin(2 x)
(%o8) - 2 (-------- - sin(x))
2
</code></pre>
https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16824#post-id-16824That mean i cannot define the function like that right? It will be very nice if i can define it similar to the second example. it will be consistent with the real equation on my paper.Thu, 31 Oct 2013 13:58:54 +0100https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16824#post-id-16824Comment by kcrisman for <pre><code>sage: n, k = var('n, k')
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f
(x, k) |--> -2*sum((-1)^n*sin(n*x)/n, n, 1, k)
</code></pre>
<p>I'm not sure what you think is wrong here. The 2 and a factor of -1 were both factored out, that's all.</p>
<p>However, I do agree that this doesn't expand. What is happening is that we are sending the sum to Maxima</p>
<pre><code>if algorithm == 'maxima':
return maxima.sr_sum(expression,v,a,b)
</code></pre>
<p>and then ordinarily when it returns, it is still a Maxima object (which may be a bug?). But when we put it in the function, it becomes a Sage object - but we don't have a Sage "sum" object. So I think that is what would have to be fixed.</p>
<hr/>
<p>That this is <em>possible</em> is shown by the following Maxima example (which I put on the ticket):</p>
<pre><code>(%i1) f: -2*'sum((-1)^n*sin(n*x)/n,n,1,2);
2
==== n
\ (- 1) sin(n x)
(%o1) - 2 > ---------------
/ n
====
n = 1
(%i8) f, nouns;
sin(2 x)
(%o8) - 2 (-------- - sin(x))
2
</code></pre>
https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16818#post-id-16818This is now [Trac #15346](http://trac.sagemath.org/ticket/15346).Fri, 01 Nov 2013 11:52:47 +0100https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16818#post-id-16818Comment by twch for <pre><code>sage: n, k = var('n, k')
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f
(x, k) |--> -2*sum((-1)^n*sin(n*x)/n, n, 1, k)
</code></pre>
<p>I'm not sure what you think is wrong here. The 2 and a factor of -1 were both factored out, that's all.</p>
<p>However, I do agree that this doesn't expand. What is happening is that we are sending the sum to Maxima</p>
<pre><code>if algorithm == 'maxima':
return maxima.sr_sum(expression,v,a,b)
</code></pre>
<p>and then ordinarily when it returns, it is still a Maxima object (which may be a bug?). But when we put it in the function, it becomes a Sage object - but we don't have a Sage "sum" object. So I think that is what would have to be fixed.</p>
<hr/>
<p>That this is <em>possible</em> is shown by the following Maxima example (which I put on the ticket):</p>
<pre><code>(%i1) f: -2*'sum((-1)^n*sin(n*x)/n,n,1,2);
2
==== n
\ (- 1) sin(n x)
(%o1) - 2 > ---------------
/ n
====
n = 1
(%i8) f, nouns;
sin(2 x)
(%o8) - 2 (-------- - sin(x))
2
</code></pre>
https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16821#post-id-16821I agree with kcrisman that at first sight the result seems to be ok and only be presented in a different form. But as the example
sage: g(x)=(2/n)*(sin(n*x)*(-1)^(n+1))
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
sage: (f(x,2)-sum(g, n, 1, 2)).full_simplify()
doesn't return zero I would call this a bug.Fri, 01 Nov 2013 09:26:21 +0100https://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/?comment=16821#post-id-16821