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

Commit 03bf755

Browse files
committed
Improved docstrings of homogenize() of polynomials
1 parent e5599ea commit 03bf755

File tree

2 files changed

+115
-73
lines changed

2 files changed

+115
-73
lines changed

src/sage/rings/polynomial/multi_polynomial.pyx

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -634,53 +634,89 @@ cdef class MPolynomial(CommutativeRingElement):
634634

635635
def homogenize(self, var='h'):
636636
r"""
637-
Return self if self is homogeneous. Otherwise return a homogenized
638-
polynomial for self. If a string is given, return a polynomial in one
639-
more variable named after the string such that setting that variable
640-
equal to 1 yields self. This variable is added to the end of the
641-
variables. If a variable in ``self.parent()`` is given, this variable
642-
is used to homogenize the polynomial. If an integer is given, the
643-
variable with this index is used for homogenization.
637+
Return the homogenization of this polynomial.
638+
639+
The polynomial itself is returned if it is homogeneous already.
640+
Otherwise, the monomials are multiplied with the smallest powers of
641+
``var`` such that they all have the same total degree.
644642
645643
INPUT:
646644
647-
- ``var`` -- either a variable name, variable index or a variable
648-
(default: 'h').
645+
- ``var`` -- a variable in the polynomial ring (as a string, an element of
646+
the ring, or a zero-based index in the list of variables) or a name
647+
for a new variable (default: ``'h'``)
649648
650649
OUTPUT:
651650
652-
a multivariate polynomial
651+
If ``var`` specifies a variable in the polynomial ring, then a
652+
homogeneous element in that ring is returned. Otherwise, a homogeneous
653+
element is returned in a polynomial ring with an extra last variable
654+
``var``.
653655
654656
EXAMPLES::
655657
656-
sage: P.<x,y> = PolynomialRing(QQ,2)
658+
sage: R.<x,y> = QQ[]
657659
sage: f = x^2 + y + 1 + 5*x*y^10
660+
sage: f.homogenize()
661+
5*x*y^10 + x^2*h^9 + y*h^10 + h^11
662+
663+
The parameter ``var`` can be used to specify the name of the variable::
664+
658665
sage: g = f.homogenize('z'); g
659666
5*x*y^10 + x^2*z^9 + y*z^10 + z^11
660667
sage: g.parent()
661668
Multivariate Polynomial Ring in x, y, z over Rational Field
662669
663-
sage: f.homogenize(x)
664-
2*x^11 + x^10*y + 5*x*y^10
670+
However, if the polynomial is homogeneous already, then that parameter
671+
is ignored and no extra variable is added to the polynomial ring::
665672
666-
sage: f.homogenize(0)
667-
2*x^11 + x^10*y + 5*x*y^10
673+
sage: f = x^2 + y^2
674+
sage: g = f.homogenize('z'); g
675+
x^2 + y^2
676+
sage: g.parent()
677+
Multivariate Polynomial Ring in x, y over Rational Field
668678
669-
sage: x, y = Zmod(3)['x', 'y'].gens()
670-
sage: (x + x^2).homogenize(y)
671-
x^2 + x*y
679+
If you want the ring of the result to be independent of whether the
680+
polynomial is homogenized, you can use ``var`` to use an existing
681+
variable to homogenize::
672682
673-
sage: x, y = Zmod(3)['x', 'y'].gens()
674-
sage: (x + x^2).homogenize(y).parent()
675-
Multivariate Polynomial Ring in x, y over Ring of integers modulo 3
683+
sage: R.<x,y,z> = QQ[]
684+
sage: f = x^2 + y^2
685+
sage: g = f.homogenize(z); g
686+
x^2 + y^2
687+
sage: g.parent()
688+
Multivariate Polynomial Ring in x, y, z over Rational Field
689+
sage: f = x^2 - y
690+
sage: g = f.homogenize(z); g
691+
x^2 - y*z
692+
sage: g.parent()
693+
Multivariate Polynomial Ring in x, y, z over Rational Field
694+
695+
The parameter ``var`` can also be given as a zero-based index in the
696+
list of variables::
697+
698+
sage: g = f.homogenize(2); g
699+
x^2 - y*z
700+
701+
If the variable specified by ``var`` is not present in the polynomial,
702+
then setting it to 1 yields the original polynomial::
703+
704+
sage: g(x,y,1)
705+
x^2 - y
676706
677-
sage: x, y = GF(3)['x', 'y'].gens()
678-
sage: (x + x^2).homogenize(y)
679-
x^2 + x*y
707+
If it is present already, this might not be the case::
680708
681-
sage: x, y = GF(3)['x', 'y'].gens()
682-
sage: (x + x^2).homogenize(y).parent()
683-
Multivariate Polynomial Ring in x, y over Finite Field of size 3
709+
sage: g = f.homogenize(x); g
710+
x^2 - x*y
711+
sage: g(1,y,z)
712+
-y + 1
713+
714+
In particular, this can be surprising in positive characteristic::
715+
716+
sage: R.<x,y> = GF(2)[]
717+
sage: f = x + 1
718+
sage: f.homogenize(x)
719+
0
684720
685721
TESTS::
686722
@@ -691,9 +727,6 @@ cdef class MPolynomial(CommutativeRingElement):
691727
sage: q1.parent() is q2.parent()
692728
True
693729
694-
sage: S.<t> = GF(2)[]
695-
sage: (1+t).homogenize('t')
696-
0
697730
"""
698731
P = self.parent()
699732

src/sage/rings/polynomial/polynomial_element.pyx

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6647,64 +6647,73 @@ cdef class Polynomial(CommutativeAlgebraElement):
66476647

66486648
def homogenize(self, var='h'):
66496649
r"""
6650+
Return the homogenization of this polynomial.
66506651
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``.
6652+
The polynomial itself is returned if it homogeneous already. Otherwise,
6653+
its monomials are multiplied with the smallest powers of ``var`` such
6654+
that they all have the same total degree.
66656655
66666656
INPUT:
66676657
6668-
- ``var`` -- either a variable name, variable index (0), or a variable
6669-
(default: 'h').
6658+
- ``var`` -- a variable in the polynomial ring (as a string, an element
6659+
of the ring, or ``0``) or a name for a new variable (default:
6660+
``'h'``)
66706661
66716662
OUTPUT:
66726663
6673-
An homogenization of self with respect to ``var``.
6664+
If ``var`` specifies the variable in the polynomial ring, then a
6665+
homogeneous element in that ring is returned. Otherwise, a homogeneous
6666+
element is returned in a polynomial ring with an extra last variable
6667+
``var``.
66746668
66756669
EXAMPLES::
66766670
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
6671+
sage: R.<x> = QQ[]
6672+
sage: f = x^2 + 1
6673+
sage: f.homogenize()
6674+
x^2 + h^2
6675+
6676+
The parameter ``var`` can be used to specify the name of the variable::
6677+
6678+
sage: g = f.homogenize('z'); g
6679+
x^2 + z^2
66816680
sage: g.parent()
6682-
Multivariate Polynomial Ring in x, h over Rational Field
6681+
Multivariate Polynomial Ring in x, z over Rational Field
66836682
6684-
sage: f.homogenize(x)
6685-
7*x^2
6686-
sage: f.homogenize(x).parent() is f.parent()
6687-
True
6683+
However, if the polynomial is homogeneous already, then that parameter
6684+
is ignored and no extra variable is added to the polynomial ring::
66886685
6689-
sage: f.homogenize(0)
6690-
7*x^2
6686+
sage: f = x^2
6687+
sage: g = f.homogenize('z'); g
6688+
x^2
6689+
sage: g.parent()
6690+
Univariate Polynomial Ring in x over Rational Field
6691+
6692+
For compatibility with the multivariate case, if ``var`` specifies the
6693+
variable of the polynomial ring, then the monomials are multiplied with
6694+
the smallest powers of ``var`` such that the result is homogeneous; in
6695+
other words, we end up with a monomial whose leading coefficient is the
6696+
sum of the coefficients of the polynomial::
66916697
6692-
sage: x = Zmod(3)['x'].gens()[0]
6693-
sage: (1 + x + x^2).homogenize(x)
6698+
sage: f = x^2 + x + 1
6699+
sage: f.homogenize('x')
6700+
3*x^2
6701+
6702+
In positive characterstic, the degree can drop in this case::
6703+
6704+
sage: R.<x> = GF(2)[]
6705+
sage: f = x + 1
6706+
sage: f.homogenize(x)
66946707
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
66996708
6700-
TESTS::
6709+
For compatibility with the multivariate case, the parameter ``var`` can
6710+
also be 0 to specify the variable in the polynomial ring::
6711+
6712+
sage: R.<x> = QQ[]
6713+
sage: f = x^2 + x + 1
6714+
sage: f.homogenize(0)
6715+
3*x^2
67016716
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
67086717
"""
67096718
if self.is_homogeneous():
67106719
return self
@@ -6728,9 +6737,9 @@ cdef class Polynomial(CommutativeAlgebraElement):
67286737

67296738
def is_homogeneous(self):
67306739
r"""
6731-
Return True if this polynomial is homogeneous.
6740+
Return ``True`` if this polynomial is homogeneous.
67326741
6733-
TESTS::
6742+
EXAMPLES::
67346743
67356744
sage: P.<x> = PolynomialRing(QQ)
67366745
sage: x.is_homogeneous()

0 commit comments

Comments
 (0)