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, 17 Jun 2015 18:15:00 +0200issue with variable assumptionshttps://ask.sagemath.org/question/27122/issue-with-variable-assumptions/I am using sage to compute a variety of algebra, and often times, when calculating the squareroots of variables, sage gets the assumptions wrong. Here is a short example:
> sage: r,M=var('r','M')
> sage: assume(r>0,M>0,r>2*M)
>sage: sqrt((2*M-r)^2)
>2M-r
Clearly the last line is incorrect (should be r-2M), but I have no idea how to fix it. While this is a trivial example, I frequently work with equations that are many variables in length, where ensuring that the correct variables are positive is a requirement.
Edit: I found a partial solution, however it does not seem to work well with simplifying radicals. If there is an alternative to canonicalize_radical which takes assumptions into account, it would be helpful.Tue, 16 Jun 2015 19:49:11 +0200https://ask.sagemath.org/question/27122/issue-with-variable-assumptions/Answer by eric_g for <p>I am using sage to compute a variety of algebra, and often times, when calculating the squareroots of variables, sage gets the assumptions wrong. Here is a short example:</p>
<blockquote>
<p>sage: r,M=var('r','M')</p>
<p>sage: assume(r>0,M>0,r>2*M)</p>
<p>sage: sqrt((2*M-r)^2)</p>
<p>2M-r</p>
</blockquote>
<p>Clearly the last line is incorrect (should be r-2M), but I have no idea how to fix it. While this is a trivial example, I frequently work with equations that are many variables in length, where ensuring that the correct variables are positive is a requirement.</p>
<p>Edit: I found a partial solution, however it does not seem to work well with simplifying radicals. If there is an alternative to canonicalize_radical which takes assumptions into account, it would be helpful.</p>
https://ask.sagemath.org/question/27122/issue-with-variable-assumptions/?answer=27123#post-id-27123Hi,
Apparently you are using an old version of Sage, because with Sage 6.7, one has
sage: r,M=var('r','M')
sage: assume(r>0,M>0,r>2*M)
sage: sqrt((2*M-r)^2)
sqrt((2*M - r)^2)
To simplify it, simply run
sage: s = sqrt((2*M-r)^2)
sage: s.simplify_real()
-2*M + r
which is correct, given the assumption.
Tue, 16 Jun 2015 20:31:07 +0200https://ask.sagemath.org/question/27122/issue-with-variable-assumptions/?answer=27123#post-id-27123Comment by zalba for <p>Hi, </p>
<p>Apparently you are using an old version of Sage, because with Sage 6.7, one has</p>
<pre><code>sage: r,M=var('r','M')
sage: assume(r>0,M>0,r>2*M)
sage: sqrt((2*M-r)^2)
sqrt((2*M - r)^2)
</code></pre>
<p>To simplify it, simply run</p>
<pre><code>sage: s = sqrt((2*M-r)^2)
sage: s.simplify_real()
-2*M + r
</code></pre>
<p>which is correct, given the assumption.</p>
https://ask.sagemath.org/question/27122/issue-with-variable-assumptions/?comment=27136#post-id-27136Thank you! I was using canonicalize.radical(), which is what I assume was the issue.Wed, 17 Jun 2015 16:45:56 +0200https://ask.sagemath.org/question/27122/issue-with-variable-assumptions/?comment=27136#post-id-27136Comment by zalba for <p>Hi, </p>
<p>Apparently you are using an old version of Sage, because with Sage 6.7, one has</p>
<pre><code>sage: r,M=var('r','M')
sage: assume(r>0,M>0,r>2*M)
sage: sqrt((2*M-r)^2)
sqrt((2*M - r)^2)
</code></pre>
<p>To simplify it, simply run</p>
<pre><code>sage: s = sqrt((2*M-r)^2)
sage: s.simplify_real()
-2*M + r
</code></pre>
<p>which is correct, given the assumption.</p>
https://ask.sagemath.org/question/27122/issue-with-variable-assumptions/?comment=27141#post-id-27141However, this does not really simplify. Is there something like canonicalize_radicals() that takes assumptions into account? For example, I have the following expression, that won't simplify further sqrt(((M - r)*mu + M)^2 + (mu^2 - 1)*(2*M*r - r^2))), but in Mathematica correctly simplifies to sqrt((mu*M+M-r)^2). canonical_radical finds the solution, but does not get the correct assumptionsWed, 17 Jun 2015 18:15:00 +0200https://ask.sagemath.org/question/27122/issue-with-variable-assumptions/?comment=27141#post-id-27141