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.Thu, 10 Oct 2013 12:05:06 -0500Bug in integration of sqrt functions?http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/I have been experiencing a problem integrating a function with several terms. I have managed to narrow it down to a problem with integration.
When I type the following command I get 1 as I should
integrate(cos(x),x,0,pi/2)
However, when I write the same integral in the form
integrate((sqrt(1-sin(x)*sin(x))),x,0,pi/2)
I get an error "RuntimeError: ECL says: Error executing code in Maxima: atanh: argument 1 isn't in the domain of atanh."
I guess it is a result of the fact that
integrate((sqrt(1-sin(x)*sin(x))),x,0,pi/2)
gives $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ and sage is trying to substitute $x=\pi/2$ without trying to simplify the expression first or taking the limit of x to $\pi/2$. Is there a way to avoid this problem when the function I wrote is a part of a bigger function and I can't simplify by hand.
Both integrals are identical and I don't understand why I am getting an error in one case. Is this a bug or am I doing something wrong?Wed, 09 Oct 2013 06:57:17 -0500http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/Comment by kcrisman for <p>I have been experiencing a problem integrating a function with several terms. I have managed to narrow it down to a problem with integration.</p>
<p>When I type the following command I get 1 as I should</p>
<pre><code>integrate(cos(x),x,0,pi/2)
</code></pre>
<p>However, when I write the same integral in the form</p>
<pre><code>integrate((sqrt(1-sin(x)*sin(x))),x,0,pi/2)
</code></pre>
<p>I get an error "RuntimeError: ECL says: Error executing code in Maxima: atanh: argument 1 isn't in the domain of atanh."</p>
<p>I guess it is a result of the fact that </p>
<pre><code>integrate((sqrt(1-sin(x)*sin(x))),x,0,pi/2)
</code></pre>
<p>gives $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ and sage is trying to substitute $x=\pi/2$ without trying to simplify the expression first or taking the limit of x to $\pi/2$. Is there a way to avoid this problem when the function I wrote is a part of a bigger function and I can't simplify by hand.</p>
<p>Both integrals are identical and I don't understand why I am getting an error in one case. Is this a bug or am I doing something wrong?</p>
http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?comment=16941#post-id-16941This has been reported upstream - http://www.math.utexas.edu/pipermail/maxima/2013/034222.html - thanks, Dima!Thu, 10 Oct 2013 03:47:19 -0500http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?comment=16941#post-id-16941Answer by dazedANDconfused for <p>I have been experiencing a problem integrating a function with several terms. I have managed to narrow it down to a problem with integration.</p>
<p>When I type the following command I get 1 as I should</p>
<pre><code>integrate(cos(x),x,0,pi/2)
</code></pre>
<p>However, when I write the same integral in the form</p>
<pre><code>integrate((sqrt(1-sin(x)*sin(x))),x,0,pi/2)
</code></pre>
<p>I get an error "RuntimeError: ECL says: Error executing code in Maxima: atanh: argument 1 isn't in the domain of atanh."</p>
<p>I guess it is a result of the fact that </p>
<pre><code>integrate((sqrt(1-sin(x)*sin(x))),x,0,pi/2)
</code></pre>
<p>gives $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ and sage is trying to substitute $x=\pi/2$ without trying to simplify the expression first or taking the limit of x to $\pi/2$. Is there a way to avoid this problem when the function I wrote is a part of a bigger function and I can't simplify by hand.</p>
<p>Both integrals are identical and I don't understand why I am getting an error in one case. Is this a bug or am I doing something wrong?</p>
http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?answer=15535#post-id-15535I don't think I can answer your questions but maybe we can solve the problem:
"Is there a way to avoid this problem when the function I wrote is a part of a bigger function and I can't simplify by hand.". For trig functions there is a simplification command, simplify_trig(), see [here](http://www.sagemath.org/tour-quickstart.html), that solves this particular problem but maybe a [combination of commands that simplify](http://ask.sagemath.org/question/1480/simplify-trig-abs-and-sqrt-expression) might be what you need. The command
integrate((tan(x)/(sqrt((tan(x)^2+1)))).simplify_trig(),(x,0,pi/2))
will evaluate to 1.
The function (sqrt(1-sin(x)*sin(x)) is really the absolute value of cosine (certainly over 0 to $\pi/2$ they're the same); maybe that's why we get the strange antiderivative. I tried `integrate(abs(cos(x)),(x,0,pi))` in a sage notebook and got -1 which is definitely wrong. I couldn't get an answer in the Sage Cell server.
The [Fundamental Theorem of Calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus#Formal_statements) expects the antiderivative to be defined on the closed interval $[0,\pi/2]$, but $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ isn't defined when x is a multiple of $\pi/2$. You'll find Sage gives the same error message if you integrate from 0 to $\pi$. In summary, $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ looks incorrect to me because the domain is not all real numbers but maybe using the simplify commands will let you get your work done.
Wed, 09 Oct 2013 15:27:33 -0500http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?answer=15535#post-id-15535Comment by Shashank for <p>I don't think I can answer your questions but maybe we can solve the problem:
"Is there a way to avoid this problem when the function I wrote is a part of a bigger function and I can't simplify by hand.". For trig functions there is a simplification command, simplify_trig(), see <a href="http://www.sagemath.org/tour-quickstart.html">here</a>, that solves this particular problem but maybe a <a href="http://ask.sagemath.org/question/1480/simplify-trig-abs-and-sqrt-expression">combination of commands that simplify</a> might be what you need. The command
integrate((tan(x)/(sqrt((tan(x)^2+1)))).simplify_trig(),(x,0,pi/2))
will evaluate to 1. </p>
<p>The function (sqrt(1-sin(x)*sin(x)) is really the absolute value of cosine (certainly over 0 to $\pi/2$ they're the same); maybe that's why we get the strange antiderivative. I tried <code>integrate(abs(cos(x)),(x,0,pi))</code> in a sage notebook and got -1 which is definitely wrong. I couldn't get an answer in the Sage Cell server.
The <a href="https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus#Formal_statements">Fundamental Theorem of Calculus</a> expects the antiderivative to be defined on the closed interval $[0,\pi/2]$, but $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ isn't defined when x is a multiple of $\pi/2$. You'll find Sage gives the same error message if you integrate from 0 to $\pi$. In summary, $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ looks incorrect to me because the domain is not all real numbers but maybe using the simplify commands will let you get your work done.</p>
http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?comment=16942#post-id-16942I know about simplify_trig() command. However, it is inconvenient when you are multiple calculations in a loop and there are several terms. My point was that you should not have to simplify and then take the limits. Wed, 09 Oct 2013 15:33:22 -0500http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?comment=16942#post-id-16942Comment by kcrisman for <p>I don't think I can answer your questions but maybe we can solve the problem:
"Is there a way to avoid this problem when the function I wrote is a part of a bigger function and I can't simplify by hand.". For trig functions there is a simplification command, simplify_trig(), see <a href="http://www.sagemath.org/tour-quickstart.html">here</a>, that solves this particular problem but maybe a <a href="http://ask.sagemath.org/question/1480/simplify-trig-abs-and-sqrt-expression">combination of commands that simplify</a> might be what you need. The command
integrate((tan(x)/(sqrt((tan(x)^2+1)))).simplify_trig(),(x,0,pi/2))
will evaluate to 1. </p>
<p>The function (sqrt(1-sin(x)*sin(x)) is really the absolute value of cosine (certainly over 0 to $\pi/2$ they're the same); maybe that's why we get the strange antiderivative. I tried <code>integrate(abs(cos(x)),(x,0,pi))</code> in a sage notebook and got -1 which is definitely wrong. I couldn't get an answer in the Sage Cell server.
The <a href="https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus#Formal_statements">Fundamental Theorem of Calculus</a> expects the antiderivative to be defined on the closed interval $[0,\pi/2]$, but $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ isn't defined when x is a multiple of $\pi/2$. You'll find Sage gives the same error message if you integrate from 0 to $\pi$. In summary, $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ looks incorrect to me because the domain is not all real numbers but maybe using the simplify commands will let you get your work done.</p>
http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?comment=16940#post-id-16940The above abs(cos(x)) error was already reported at Trac 11483 (currently Trac is down, but you can Google for it). Hopefully Trac 13973 will fix this for good.Thu, 10 Oct 2013 03:54:55 -0500http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?comment=16940#post-id-16940Comment by Shashank for <p>I don't think I can answer your questions but maybe we can solve the problem:
"Is there a way to avoid this problem when the function I wrote is a part of a bigger function and I can't simplify by hand.". For trig functions there is a simplification command, simplify_trig(), see <a href="http://www.sagemath.org/tour-quickstart.html">here</a>, that solves this particular problem but maybe a <a href="http://ask.sagemath.org/question/1480/simplify-trig-abs-and-sqrt-expression">combination of commands that simplify</a> might be what you need. The command
integrate((tan(x)/(sqrt((tan(x)^2+1)))).simplify_trig(),(x,0,pi/2))
will evaluate to 1. </p>
<p>The function (sqrt(1-sin(x)*sin(x)) is really the absolute value of cosine (certainly over 0 to $\pi/2$ they're the same); maybe that's why we get the strange antiderivative. I tried <code>integrate(abs(cos(x)),(x,0,pi))</code> in a sage notebook and got -1 which is definitely wrong. I couldn't get an answer in the Sage Cell server.
The <a href="https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus#Formal_statements">Fundamental Theorem of Calculus</a> expects the antiderivative to be defined on the closed interval $[0,\pi/2]$, but $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ isn't defined when x is a multiple of $\pi/2$. You'll find Sage gives the same error message if you integrate from 0 to $\pi$. In summary, $\tan(x)/\sqrt{(\tan(x)^2 + 1)}$ looks incorrect to me because the domain is not all real numbers but maybe using the simplify commands will let you get your work done.</p>
http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?comment=16939#post-id-16939Thanks a lot. It seems it will be fixed in 5.13. Is there a release schedule for this and future versions?Thu, 10 Oct 2013 12:05:06 -0500http://ask.sagemath.org/question/10603/bug-in-integration-of-sqrt-functions/?comment=16939#post-id-16939