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.Wed, 06 Mar 2013 02:05:49 +0100Strange problem with double integralshttps://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/I need to calculate a double of integral a large expression numerically. But, I found a strange error. The code:
def doubleIntegral():
def f2(theta):
def f1(phi):
return theta*phi/pi
return numerical_integral(f1,0,2*pi)[0]
return numerical_integral(f2,0,pi)
works. but
a = theta*phi/pi # Suppose this is a huge expression that came out of previous calculations
def doubleIntegral():
def f2(theta):
def f1(phi):
return a
return numerical_integral(f1,0,2*pi)[0]
return numerical_integral(f2,0,pi)
doesn't work
unable to simplify to float approximation
unable to simplify to float approximation
.
.
.
What is going on? And how can I fix this?Tue, 05 Mar 2013 13:05:09 +0100https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/Comment by ebs for <p>I need to calculate a double of integral a large expression numerically. But, I found a strange error. The code:</p>
<pre><code>def doubleIntegral():
def f2(theta):
def f1(phi):
return theta*phi/pi
return numerical_integral(f1,0,2*pi)[0]
return numerical_integral(f2,0,pi)
</code></pre>
<p>works. but </p>
<pre><code>a = theta*phi/pi # Suppose this is a huge expression that came out of previous calculations
def doubleIntegral():
def f2(theta):
def f1(phi):
return a
return numerical_integral(f1,0,2*pi)[0]
return numerical_integral(f2,0,pi)
</code></pre>
<p>doesn't work</p>
<pre><code>unable to simplify to float approximation
unable to simplify to float approximation
.
.
.
</code></pre>
<p>What is going on? And how can I fix this?</p>
https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/?comment=18103#post-id-18103my function doubleIntegral does the same thing as what that nestled numerical_integral with lambda functions (from that question) does. It has the exact same problem. Trying the internet search...Tue, 05 Mar 2013 13:41:30 +0100https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/?comment=18103#post-id-18103Comment by kcrisman for <p>I need to calculate a double of integral a large expression numerically. But, I found a strange error. The code:</p>
<pre><code>def doubleIntegral():
def f2(theta):
def f1(phi):
return theta*phi/pi
return numerical_integral(f1,0,2*pi)[0]
return numerical_integral(f2,0,pi)
</code></pre>
<p>works. but </p>
<pre><code>a = theta*phi/pi # Suppose this is a huge expression that came out of previous calculations
def doubleIntegral():
def f2(theta):
def f1(phi):
return a
return numerical_integral(f1,0,2*pi)[0]
return numerical_integral(f2,0,pi)
</code></pre>
<p>doesn't work</p>
<pre><code>unable to simplify to float approximation
unable to simplify to float approximation
.
.
.
</code></pre>
<p>What is going on? And how can I fix this?</p>
https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/?comment=18104#post-id-18104Try doing an internet search for using lambda functions for this. http://ask.sagemath.org/question/68/double-integral might be helpful along these lines...Tue, 05 Mar 2013 13:30:15 +0100https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/?comment=18104#post-id-18104Answer by ppurka for <p>I need to calculate a double of integral a large expression numerically. But, I found a strange error. The code:</p>
<pre><code>def doubleIntegral():
def f2(theta):
def f1(phi):
return theta*phi/pi
return numerical_integral(f1,0,2*pi)[0]
return numerical_integral(f2,0,pi)
</code></pre>
<p>works. but </p>
<pre><code>a = theta*phi/pi # Suppose this is a huge expression that came out of previous calculations
def doubleIntegral():
def f2(theta):
def f1(phi):
return a
return numerical_integral(f1,0,2*pi)[0]
return numerical_integral(f2,0,pi)
</code></pre>
<p>doesn't work</p>
<pre><code>unable to simplify to float approximation
unable to simplify to float approximation
.
.
.
</code></pre>
<p>What is going on? And how can I fix this?</p>
https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/?answer=14617#post-id-14617In the second example function, you are returning a from the innermost loop, but a is a symbolic expression. In the first example, what you return from the innermost loop is just a number evaluated at theta and phi. What happens if you instead use `return a.subs(theta=theta, phi=phi)` in the second example?Tue, 05 Mar 2013 14:38:26 +0100https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/?answer=14617#post-id-14617Comment by ebs for <p>In the second example function, you are returning a from the innermost loop, but a is a symbolic expression. In the first example, what you return from the innermost loop is just a number evaluated at theta and phi. What happens if you instead use <code>return a.subs(theta=theta, phi=phi)</code> in the second example?</p>
https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/?comment=18097#post-id-18097Oh! I get it. I also found dblquad in scipy.integrate (instead of this function) which seems to be faster. ty :)Wed, 06 Mar 2013 02:05:49 +0100https://ask.sagemath.org/question/9880/strange-problem-with-double-integrals/?comment=18097#post-id-18097