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.Fri, 25 Oct 2019 14:00:13 +0200How can I map functions into polynomial coefficientshttps://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/Let's say I have the following expression (from a wide range of possibilities) :
pol = 3*a*x^(-b)*log(x)*b^2 - 6*a*b*c*sin(x*b) + 3*a*c^2 + 5
And I want to extract the coefficients of the polynomial over the polynomial ring over a & c, so that these result in:
a^0*c^0 : 5
a^1*c^0 : 3*x^(-b)*log(x)*b^2
a^0*c^1 : 0
a^1*c^1 : -6*b*sin(x*b)
etc.
How can I define the polynomial ring?
How can I map an existing expression that defines "pol" (which is the result of other manipulations) into such ring?Fri, 25 Oct 2019 03:07:32 +0200https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/Comment by vdelecroix for <p>Let's say I have the following expression (from a wide range of possibilities) : </p>
<pre><code>pol = 3*a*x^(-b)*log(x)*b^2 - 6*a*b*c*sin(x*b) + 3*a*c^2 + 5
</code></pre>
<p>And I want to extract the coefficients of the polynomial over the polynomial ring over a & c, so that these result in:</p>
<pre><code>a^0*c^0 : 5
a^1*c^0 : 3*x^(-b)*log(x)*b^2
a^0*c^1 : 0
a^1*c^1 : -6*b*sin(x*b)
etc.
</code></pre>
<p>How can I define the polynomial ring?</p>
<p>How can I map an existing expression that defines "pol" (which is the result of other manipulations) into such ring?</p>
https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/?comment=48493#post-id-48493what is `sinx`?Fri, 25 Oct 2019 06:38:38 +0200https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/?comment=48493#post-id-48493Comment by Edgar Brown for <p>Let's say I have the following expression (from a wide range of possibilities) : </p>
<pre><code>pol = 3*a*x^(-b)*log(x)*b^2 - 6*a*b*c*sin(x*b) + 3*a*c^2 + 5
</code></pre>
<p>And I want to extract the coefficients of the polynomial over the polynomial ring over a & c, so that these result in:</p>
<pre><code>a^0*c^0 : 5
a^1*c^0 : 3*x^(-b)*log(x)*b^2
a^0*c^1 : 0
a^1*c^1 : -6*b*sin(x*b)
etc.
</code></pre>
<p>How can I define the polynomial ring?</p>
<p>How can I map an existing expression that defines "pol" (which is the result of other manipulations) into such ring?</p>
https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/?comment=48499#post-id-48499@vdelecroix A typoFri, 25 Oct 2019 14:00:13 +0200https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/?comment=48499#post-id-48499Answer by rburing for <p>Let's say I have the following expression (from a wide range of possibilities) : </p>
<pre><code>pol = 3*a*x^(-b)*log(x)*b^2 - 6*a*b*c*sin(x*b) + 3*a*c^2 + 5
</code></pre>
<p>And I want to extract the coefficients of the polynomial over the polynomial ring over a & c, so that these result in:</p>
<pre><code>a^0*c^0 : 5
a^1*c^0 : 3*x^(-b)*log(x)*b^2
a^0*c^1 : 0
a^1*c^1 : -6*b*sin(x*b)
etc.
</code></pre>
<p>How can I define the polynomial ring?</p>
<p>How can I map an existing expression that defines "pol" (which is the result of other manipulations) into such ring?</p>
https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/?answer=48496#post-id-48496Here is a way:
sage: var('a,b,c,x')
sage: sinx = function('sinx',nargs=1)
sage: pol = 3*a*x^(-b)*log(x)*b^2 - 6*a*b*c*sinx(x*b) + 3*a*c^2 + 5
sage: R = PolynomialRing(SR, names='a,c')
sage: f = pol.polynomial(ring=R)
sage: dict(zip(f.monomials(), f.coefficients()))
{1: 5, a: 3*b^2*log(x)/x^b, a*c: -6*b*sinx(b*x), a*c^2: 3}
Note the generators of the ring `R` are not the same as the symbolic variables `a` and `c`.
To get the coefficient of `c` as in your example, you can do:
sage: f.monomial_coefficient(R.gen(1))
0
Or maybe more conveniently, something like:
sage: A,C = R.gens()
sage: f.monomial_coefficient(C)
0Fri, 25 Oct 2019 09:43:38 +0200https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/?answer=48496#post-id-48496Comment by Edgar Brown for <p>Here is a way:</p>
<pre><code>sage: var('a,b,c,x')
sage: sinx = function('sinx',nargs=1)
sage: pol = 3*a*x^(-b)*log(x)*b^2 - 6*a*b*c*sinx(x*b) + 3*a*c^2 + 5
sage: R = PolynomialRing(SR, names='a,c')
sage: f = pol.polynomial(ring=R)
sage: dict(zip(f.monomials(), f.coefficients()))
{1: 5, a: 3*b^2*log(x)/x^b, a*c: -6*b*sinx(b*x), a*c^2: 3}
</code></pre>
<p>Note the generators of the ring <code>R</code> are not the same as the symbolic variables <code>a</code> and <code>c</code>.</p>
<p>To get the coefficient of <code>c</code> as in your example, you can do:</p>
<pre><code>sage: f.monomial_coefficient(R.gen(1))
0
</code></pre>
<p>Or maybe more conveniently, something like:</p>
<pre><code>sage: A,C = R.gens()
sage: f.monomial_coefficient(C)
0
</code></pre>
https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/?comment=48498#post-id-48498Thanks! That works.
I guess the generator syntax is critical as R.<a,c> = Poly... definitively does not work.
But why is the "show" method not implemented on a PolynomialRing????Fri, 25 Oct 2019 13:52:10 +0200https://ask.sagemath.org/question/48492/how-can-i-map-functions-into-polynomial-coefficients/?comment=48498#post-id-48498