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.Thu, 29 Oct 2015 02:48:10 +0100sqrt function not working properlyhttps://ask.sagemath.org/question/30325/sqrt-function-not-working-properly/ I have the following code where I want to substitute `a, b, c` into `s`. Since `s` factors as a square, I want to get the square root of it :
p, t= var('p t')
a=(-2*p*t^2-p^2*t)+(2*t*p-p^2)+t+1
b=(p*t^2+2*p^2*t)+(2*t*p-t^2)-p+1
c=(p*t^2-p^2*t)+(t^2+2*t*p+p^2)+t-p #3 sides (a,b,c) in terms of theta and phi [equation (1.1)]
s=(factor(2*c^2+2*a^2-b^2));s
S=s.sqrt();S
Unfortunately the answer I get is
sqrt((3*p*t^2 - 2*p^2 - 2*p*t + t^2 + p + 2*t + 1)^2)
The `sqrt` and the square power does not cancel off which I want it to cancel. I tried using the code `S.simplify_full()` to simplify it hoping the sqrt and square power will cancel off but no luck. Is there any other specific code I can use for that.
Thu, 29 Oct 2015 00:45:04 +0100https://ask.sagemath.org/question/30325/sqrt-function-not-working-properly/Answer by kcrisman for <p>I have the following code where I want to substitute <code>a, b, c</code> into <code>s</code>. Since <code>s</code> factors as a square, I want to get the square root of it :</p>
<pre><code>p, t= var('p t')
a=(-2*p*t^2-p^2*t)+(2*t*p-p^2)+t+1
b=(p*t^2+2*p^2*t)+(2*t*p-t^2)-p+1
c=(p*t^2-p^2*t)+(t^2+2*t*p+p^2)+t-p #3 sides (a,b,c) in terms of theta and phi [equation (1.1)]
s=(factor(2*c^2+2*a^2-b^2));s
S=s.sqrt();S
</code></pre>
<p>Unfortunately the answer I get is </p>
<pre><code>sqrt((3*p*t^2 - 2*p^2 - 2*p*t + t^2 + p + 2*t + 1)^2)
</code></pre>
<p>The <code>sqrt</code> and the square power does not cancel off which I want it to cancel. I tried using the code <code>S.simplify_full()</code> to simplify it hoping the sqrt and square power will cancel off but no luck. Is there any other specific code I can use for that.</p>
https://ask.sagemath.org/question/30325/sqrt-function-not-working-properly/?answer=30326#post-id-30326The square root of a square is not the same as the number. (There is controversy over whether it is the absolute value of the number, or either plus or minus the number. Don't ask.)
But if you want to make it do that, use this:
sage: S.canonicalize_radical()
(3*p + 1)*t^2 - 2*p^2 - 2*(p - 1)*t + p + 1
which even does some factoring for you as a bonus.Thu, 29 Oct 2015 02:16:22 +0100https://ask.sagemath.org/question/30325/sqrt-function-not-working-properly/?answer=30326#post-id-30326Comment by Sha for <p>The square root of a square is not the same as the number. (There is controversy over whether it is the absolute value of the number, or either plus or minus the number. Don't ask.) </p>
<p>But if you want to make it do that, use this:</p>
<pre><code>sage: S.canonicalize_radical()
(3*p + 1)*t^2 - 2*p^2 - 2*(p - 1)*t + p + 1
</code></pre>
<p>which even does some factoring for you as a bonus.</p>
https://ask.sagemath.org/question/30325/sqrt-function-not-working-properly/?comment=30327#post-id-30327@kcrisman,.. Thank you! this worked fine..Thu, 29 Oct 2015 02:48:10 +0100https://ask.sagemath.org/question/30325/sqrt-function-not-working-properly/?comment=30327#post-id-30327