Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit e5599ea

Browse files
committed
Ticket 2352: univariate homogenize
1 parent 169b80c commit e5599ea

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/sage/rings/polynomial/multi_polynomial.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,10 @@ cdef class MPolynomial(CommutativeRingElement):
690690
sage: q2 = p.homogenize()
691691
sage: q1.parent() is q2.parent()
692692
True
693+
694+
sage: S.<t> = GF(2)[]
695+
sage: (1+t).homogenize('t')
696+
0
693697
"""
694698
P = self.parent()
695699

src/sage/rings/polynomial/polynomial_element.pyx

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6645,6 +6645,103 @@ cdef class Polynomial(CommutativeAlgebraElement):
66456645
# otherwise not cyclotomic
66466646
return False
66476647

6648+
def homogenize(self, var='h'):
6649+
r"""
6650+
6651+
Homogenize this polynomial with respect to ``var``.
6652+
6653+
If the polynomial is homogeneous, return the same polynomial in the same
6654+
ring. If not, if this polynomial is in the polynomial ring
6655+
``self.parent()==R[x]``, return an homogeneous polynomial in ``R[x,var]``
6656+
such that setting ``var=1`` yields ``self``. The new variable ``var``
6657+
will be the second variable in the new polynomial ring ``R[x,var]``.
6658+
6659+
Due to compatibility reasons with the multivariate case:
6660+
:meth:`sage.rings.polynomial.multi_polynomial.MPolynomial.homogenize`,
6661+
if the variable given is the variable ``x`` in ``R[x]``, or ``var=0``,
6662+
the variable in ``R[x]`` is used to homogenize the polynomial. So, we
6663+
end up with a monomial of the same degree as ``self`` whose coefficient
6664+
is the sum of the coefficients of ``self``.
6665+
6666+
INPUT:
6667+
6668+
- ``var`` -- either a variable name, variable index (0), or a variable
6669+
(default: 'h').
6670+
6671+
OUTPUT:
6672+
6673+
An homogenization of self with respect to ``var``.
6674+
6675+
EXAMPLES::
6676+
6677+
sage: P.<x> = PolynomialRing(QQ)
6678+
sage: f = x^2 + 5*x + 1
6679+
sage: g = f.homogenize(); g
6680+
x^2 + 5*x*h + h^2
6681+
sage: g.parent()
6682+
Multivariate Polynomial Ring in x, h over Rational Field
6683+
6684+
sage: f.homogenize(x)
6685+
7*x^2
6686+
sage: f.homogenize(x).parent() is f.parent()
6687+
True
6688+
6689+
sage: f.homogenize(0)
6690+
7*x^2
6691+
6692+
sage: x = Zmod(3)['x'].gens()[0]
6693+
sage: (1 + x + x^2).homogenize(x)
6694+
0
6695+
sage: (x + x^2).homogenize('y').parent()
6696+
Multivariate Polynomial Ring in x, y over Ring of integers modulo 3
6697+
sage: (x + x^2).homogenize()
6698+
x^2 + x*h
6699+
6700+
TESTS::
6701+
6702+
sage: R = PolynomialRing(QQ, 'x')
6703+
sage: p = R.random_element()
6704+
sage: q1 = p.homogenize()
6705+
sage: q2 = p.homogenize()
6706+
sage: q1.parent() is q2.parent()
6707+
True
6708+
"""
6709+
if self.is_homogeneous():
6710+
return self
6711+
6712+
x, = self.variables()
6713+
6714+
if PY_TYPE_CHECK(var, int) or PY_TYPE_CHECK(var, Integer):
6715+
if var:
6716+
raise TypeError, "Variable index %d must be < 1."%var
6717+
else:
6718+
return sum(self.coefficients())*x**self.degree()
6719+
6720+
x_name = self.variable_name()
6721+
var = str(var)
6722+
6723+
if var == x_name:
6724+
return sum(self.coefficients())*x**self.degree()
6725+
6726+
P = PolynomialRing(self.base_ring(), [x_name, var])
6727+
return P(self)._homogenize(1)
6728+
6729+
def is_homogeneous(self):
6730+
r"""
6731+
Return True if this polynomial is homogeneous.
6732+
6733+
TESTS::
6734+
6735+
sage: P.<x> = PolynomialRing(QQ)
6736+
sage: x.is_homogeneous()
6737+
True
6738+
sage: P(0).is_homogeneous()
6739+
True
6740+
sage: (x+1).is_homogeneous()
6741+
False
6742+
"""
6743+
return len(self.exponents()) < 2
6744+
66486745
# ----------------- inner functions -------------
66496746
# Cython can't handle function definitions inside other function
66506747

0 commit comments

Comments
 (0)