ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 07 Dec 2017 11:08:37 -0600square root of a polynomial with variable coefficientshttps://ask.sagemath.org/question/39978/square-root-of-a-polynomial-with-variable-coefficients/I want to calculate the square root of a polynomial with variable coefficients in sage. For example:
i,z=var('i,z')
c=function('c')
p=sum(c(i)*z^i,i,0,6)
sqrt(p)
returns
sqrt(z^6*c(6) + z^5*c(5) + z^4*c(4) + z^3*c(3) + z^2*c(2) + z*c(1) + c(0))
I want a Taylor or Laurent expansion in z. How do I get this?
Wed, 06 Dec 2017 13:10:06 -0600https://ask.sagemath.org/question/39978/square-root-of-a-polynomial-with-variable-coefficients/Answer by dan_fulea for <p>I want to calculate the square root of a polynomial with variable coefficients in sage. For example:</p>
<p>i,z=var('i,z')
c=function('c')
p=sum(c(i)*z^i,i,0,6)
sqrt(p)</p>
<p>returns</p>
<p>sqrt(z^6<em>c(6) + z^5</em>c(5) + z^4<em>c(4) + z^3</em>c(3) + z^2<em>c(2) + z</em>c(1) + c(0))</p>
<p>I want a Taylor or Laurent expansion in z. How do I get this?</p>
https://ask.sagemath.org/question/39978/square-root-of-a-polynomial-with-variable-coefficients/?answer=39996#post-id-39996I will suppose `c0` is `1`, else we divide with its square root, which should exist in the ground field for the coefficients. (Feel free to change the following code, so that `c0` is also a general variable.)
The following does the job for me:
c = [1,] + list( var( 'c1,c2,c3,c4,c5,c6' ) )
print "c is", c
var( 'x' );
T = taylor( sqrt( 1 + sum( [ c[k]*x^k for k in [1..6] ] ) ), x, 0, 6 )
# print T # for a long story
print "T has the following coefficients:"
for coeff, deg in T.coefficients( x ):
print "Degree %s: %s" % ( deg, coeff )
Results:
c is [1, c1, c2, c3, c4, c5, c6]
x
T has the following coefficients:
Degree 0: 1
Degree 1: 1/2*c1
Degree 2: -1/8*c1^2 + 1/2*c2
Degree 3: 1/16*c1^3 - 1/4*c1*c2 + 1/2*c3
Degree 4: -5/128*c1^4 + 3/16*c1^2*c2 - 1/8*c2^2 - 1/4*c1*c3 + 1/2*c4
Degree 5: 7/256*c1^5 - 5/32*c1^3*c2 + 3/16*c1*c2^2 + 1/16*(3*c1^2 - 4*c2)*c3 - 1/4*c1*c4 + 1/2*c5
Degree 6: -21/1024*c1^6 + 35/256*c1^4*c2 - 15/64*c1^2*c2^2 + 1/16*c2^3 - 1/32*(5*c1^3 - 12*c1*c2)*c3 - 1/8*c3^2 + 1/16*(3*c1^2 - 4*c2)*c4 - 1/4*c1*c5 + 1/2*c6
Thu, 07 Dec 2017 07:15:19 -0600https://ask.sagemath.org/question/39978/square-root-of-a-polynomial-with-variable-coefficients/?answer=39996#post-id-39996Comment by dan_fulea for <p>I will suppose <code>c0</code> is <code>1</code>, else we divide with its square root, which should exist in the ground field for the coefficients. (Feel free to change the following code, so that <code>c0</code> is also a general variable.)</p>
<p>The following does the job for me:</p>
<pre><code>c = [1,] + list( var( 'c1,c2,c3,c4,c5,c6' ) )
print "c is", c
var( 'x' );
T = taylor( sqrt( 1 + sum( [ c[k]*x^k for k in [1..6] ] ) ), x, 0, 6 )
# print T # for a long story
print "T has the following coefficients:"
for coeff, deg in T.coefficients( x ):
print "Degree %s: %s" % ( deg, coeff )
</code></pre>
<p>Results:</p>
<pre><code>c is [1, c1, c2, c3, c4, c5, c6]
x
T has the following coefficients:
Degree 0: 1
Degree 1: 1/2*c1
Degree 2: -1/8*c1^2 + 1/2*c2
Degree 3: 1/16*c1^3 - 1/4*c1*c2 + 1/2*c3
Degree 4: -5/128*c1^4 + 3/16*c1^2*c2 - 1/8*c2^2 - 1/4*c1*c3 + 1/2*c4
Degree 5: 7/256*c1^5 - 5/32*c1^3*c2 + 3/16*c1*c2^2 + 1/16*(3*c1^2 - 4*c2)*c3 - 1/4*c1*c4 + 1/2*c5
Degree 6: -21/1024*c1^6 + 35/256*c1^4*c2 - 15/64*c1^2*c2^2 + 1/16*c2^3 - 1/32*(5*c1^3 - 12*c1*c2)*c3 - 1/8*c3^2 + 1/16*(3*c1^2 - 4*c2)*c4 - 1/4*c1*c5 + 1/2*c6
</code></pre>
https://ask.sagemath.org/question/39978/square-root-of-a-polynomial-with-variable-coefficients/?comment=39997#post-id-39997Somebody wanted really to see the `c(0)` squared appearing explicitly at all places and/or `c(1), c(2), c(3), c(4), c(5), c(6)` instead of `c1`, `c2`, `c3`, `c4`, `c5`, `c6`... else i cannot figure out the reason for the downgrade.
(There was some effort to make the output shorter and without this obvious squared root, so that the result is an algebraic, weighted homogenous expression in each `x^k` with predictible polynomials in `c(1)^k` and `c(k)`... This was the reason for setting `c(0)=1` explicitly.)
Next time the solution will be:
Copy+format the given code:
k, z = var( 'k,z' );
c = function('c')
p = sum( c(k)*z^k, k, 0, 6 )
Then deliver the **one line** solution
T = taylor( sqrt(p), z, 0, 6 )
as required till degree 6 **without** showing the lenghty answer.Thu, 07 Dec 2017 11:08:37 -0600https://ask.sagemath.org/question/39978/square-root-of-a-polynomial-with-variable-coefficients/?comment=39997#post-id-39997Answer by B r u n o for <p>I want to calculate the square root of a polynomial with variable coefficients in sage. For example:</p>
<p>i,z=var('i,z')
c=function('c')
p=sum(c(i)*z^i,i,0,6)
sqrt(p)</p>
<p>returns</p>
<p>sqrt(z^6<em>c(6) + z^5</em>c(5) + z^4<em>c(4) + z^3</em>c(3) + z^2<em>c(2) + z</em>c(1) + c(0))</p>
<p>I want a Taylor or Laurent expansion in z. How do I get this?</p>
https://ask.sagemath.org/question/39978/square-root-of-a-polynomial-with-variable-coefficients/?answer=39994#post-id-39994*[Side remark: Use the button with `101` and `010` to format your code.]*
You simply need to ask for the taylor expansion in `z` at $0$:
sage: i,z = var('i,z')
sage: c = function('c')
sage: p = sum(c(i)*z^i,i,0,6)
sage: psqrt = sqrt(p)
sage: psqrt.taylor(z, 0, 3) # I take 3 to limit the size of the output...
1/2*z*c(1)/sqrt(c(0)) + 1/16*(c(1)^3*sqrt(c(0)) - 4*c(2)*c(1)*c(0)^(3/2) + 8*c(3)*c(0)^(5/2))*z^3/c(0)^3 - 1/8*(c(1)^2*sqrt(c(0)) - 4*c(2)*c(0)^(3/2))*z^2/c(0)^2 + sqrt(c(0))
Thu, 07 Dec 2017 07:04:33 -0600https://ask.sagemath.org/question/39978/square-root-of-a-polynomial-with-variable-coefficients/?answer=39994#post-id-39994