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, 02 Jun 2016 17:51:13 +0200Several variables, consider polynomail as polynomial only of $X$, group coefficientshttps://ask.sagemath.org/question/33646/several-variables-consider-polynomail-as-polynomial-only-of-x-group-coefficients/Suppose I have variables `d`, `e` and `x` and I somehow using symbolic calculation get polynomial like this:
$$9d^2e^2x^2 - 36d^2ex^3 + 18de^2x^2$$
I want sage to group coefficients at `x` and consider my polynomial as polynomial only of `x`, I want other variables to be no more than just parameters. In short I want to see something like this:
$$9de^2(d + 2)x^2 - 36d^2ex^3$$
Is it possible to exploit such an approach in Sage?
P.S. I shortened my example. In real life example it is not so easy to see such a grouping by hands.Thu, 02 Jun 2016 17:25:07 +0200https://ask.sagemath.org/question/33646/several-variables-consider-polynomail-as-polynomial-only-of-x-group-coefficients/Answer by tmonteil for <p>Suppose I have variables <code>d</code>, <code>e</code> and <code>x</code> and I somehow using symbolic calculation get polynomial like this:</p>
<p>$$9d^2e^2x^2 - 36d^2ex^3 + 18de^2x^2$$</p>
<p>I want sage to group coefficients at <code>x</code> and consider my polynomial as polynomial only of <code>x</code>, I want other variables to be no more than just parameters. In short I want to see something like this:</p>
<p>$$9de^2(d + 2)x^2 - 36d^2ex^3$$</p>
<p>Is it possible to exploit such an approach in Sage? </p>
<p>P.S. I shortened my example. In real life example it is not so easy to see such a grouping by hands.</p>
https://ask.sagemath.org/question/33646/several-variables-consider-polynomail-as-polynomial-only-of-x-group-coefficients/?answer=33648#post-id-33648If i understand correctly, you are looking for a polynomial in the indeterminate `x` whose coefficients belong to the polynomial ring with indeterminates `d` and `e`. You can define this as follows (assuming the numeral coefficients are rational numbers):
sage: R.<d,e> = PolynomialRing(QQ)
sage: R
Multivariate Polynomial Ring in d, e over Rational Field
sage: S.<x> = PolynomialRing(R)
sage: S
Univariate Polynomial Ring in x over Multivariate Polynomial Ring in d, e over Rational Field
sage: P = 9*d^2*e^2*x^2-36*d^2*e*x^3+18*d*e^2*x^2
sage: P
-36*d^2*e*x^3 + (9*d^2*e^2 + 18*d*e^2)*x^2
Thu, 02 Jun 2016 17:51:13 +0200https://ask.sagemath.org/question/33646/several-variables-consider-polynomail-as-polynomial-only-of-x-group-coefficients/?answer=33648#post-id-33648Answer by calc314 for <p>Suppose I have variables <code>d</code>, <code>e</code> and <code>x</code> and I somehow using symbolic calculation get polynomial like this:</p>
<p>$$9d^2e^2x^2 - 36d^2ex^3 + 18de^2x^2$$</p>
<p>I want sage to group coefficients at <code>x</code> and consider my polynomial as polynomial only of <code>x</code>, I want other variables to be no more than just parameters. In short I want to see something like this:</p>
<p>$$9de^2(d + 2)x^2 - 36d^2ex^3$$</p>
<p>Is it possible to exploit such an approach in Sage? </p>
<p>P.S. I shortened my example. In real life example it is not so easy to see such a grouping by hands.</p>
https://ask.sagemath.org/question/33646/several-variables-consider-polynomail-as-polynomial-only-of-x-group-coefficients/?answer=33647#post-id-33647You can try:
var('d e x')
f = 9*d^2*e^2*x^2-36*d^2*e*x^3+18*d*e^2*x^2
f.collect(x)
If your example is very complicated, you might wish to use
f.coefficients(x)
to obtain a list of the coefficients with respect to $x$.
Depending on what you are doing, it might also be useful to define the ring you are working in:
R.<x> = PolynomialRing(QQ)
Thu, 02 Jun 2016 17:46:07 +0200https://ask.sagemath.org/question/33646/several-variables-consider-polynomail-as-polynomial-only-of-x-group-coefficients/?answer=33647#post-id-33647