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.Tue, 08 Nov 2011 02:46:17 -0600strange behaviour when solving equations symbolicallyhttp://ask.sagemath.org/question/8452/strange-behaviour-when-solving-equations-symbolically/I'm new to sage and wanted to get startet. However, i tried solving equations symbolically and found a strang behaviour i do not understand. Can anybody explain to me this behaviour?
$ sage
----------------------------------------------------------------------
| Sage Version 4.7.1, Release Date: 2011-08-11 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: r, u, m, M = var('r, u, m, M')
sage: qe = (u == sqrt(r^2 + m^2) + sqrt(r^2 + M^2))
sage: qe
u == sqrt(m^2 + r^2) + sqrt(M^2 + r^2)
sage: print solve(qe, r)
[
sqrt(M^2 + r^2) == u - sqrt(m^2 + r^2)
]
sage:
Any suggestions?Tue, 08 Nov 2011 02:14:51 -0600http://ask.sagemath.org/question/8452/strange-behaviour-when-solving-equations-symbolically/Answer by kcrisman for <p>I'm new to sage and wanted to get startet. However, i tried solving equations symbolically and found a strang behaviour i do not understand. Can anybody explain to me this behaviour?</p>
<pre><code>$ sage
----------------------------------------------------------------------
| Sage Version 4.7.1, Release Date: 2011-08-11 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: r, u, m, M = var('r, u, m, M')
sage: qe = (u == sqrt(r^2 + m^2) + sqrt(r^2 + M^2))
sage: qe
u == sqrt(m^2 + r^2) + sqrt(M^2 + r^2)
sage: print solve(qe, r)
[
sqrt(M^2 + r^2) == u - sqrt(m^2 + r^2)
]
sage:
</code></pre>
<p>Any suggestions?</p>
http://ask.sagemath.org/question/8452/strange-behaviour-when-solving-equations-symbolically/?answer=12869#post-id-12869This is actually pretty normal behavior. Let's compare some examples from the documentation.
sage: x, y = var('x, y')
sage: solve([x+y==6, x-y==4], x, y)
[[x == 5, y == 1]]
So the answers appear in a [list](http://docs.python.org/tutorial/introduction.html#lists); in this case, the answer is two variables equalling something, so it is a list with one element which has two elements in it.
sage: solve([sin(x)==x,y^2==x],x,y)
[sin(x) == x, y^2 == x]
Here, Sage/Maxima doesn't know how to solve this, so it just returns a list with the original equations. And that is what is happening in your case too, only your equation is slightly rearranged and is long enough that the brackets appear above and below it because it doesn't fit on one line.
In general I don't know that one can expect all solutions - there is always a place a human will be smarter than the computer.
sage: S = solve(qe,r)[0]
sage: S.lhs()^2==S.rhs()^2
M^2 + r^2 == (u - sqrt(m^2 + r^2))^2
sage: S.lhs()^2==(S.rhs()^2).expand()
M^2 + r^2 == m^2 + r^2 + u^2 - 2*sqrt(m^2 + r^2)*u
But trying to solve the latter for `r` throws an error because it wants to know if something is positive. Here I intervene.
sage: T = M^2-m^2-u^2==-2*u*sqrt(m^2 + r^2)
sage: T^2
(M^2 - m^2 - u^2)^2 == 4*(m^2 + r^2)*u^2
sage: (T^2).expand()
M^4 - 2*M^2*m^2 - 2*M^2*u^2 + m^4 + 2*m^2*u^2 + u^4 == 4*m^2*u^2 + 4*r^2*u^2
sage: solve((T^2).expand(),r)
[r == -1/2*sqrt(M^4 - 2*M^2*m^2 - 2*M^2*u^2 + m^4 - 2*m^2*u^2 + u^4)/u, r == 1/2*sqrt(M^4 - 2*M^2*m^2 - 2*M^2*u^2 + m^4 - 2*m^2*u^2 + u^4)/u]
I have no idea if spurious solutions were introduced - squaring is dangerous! But the moral is that sometimes you have to do things 'by hand', even with computers.Tue, 08 Nov 2011 02:46:17 -0600http://ask.sagemath.org/question/8452/strange-behaviour-when-solving-equations-symbolically/?answer=12869#post-id-12869