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

Commit

Permalink
Trac 29375: clean up some _element_constructor_() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbruin committed Mar 20, 2020
1 parent be1e22c commit 586f977
Show file tree
Hide file tree
Showing 21 changed files with 46 additions and 77 deletions.
10 changes: 4 additions & 6 deletions src/sage/algebras/clifford_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,10 @@ def _element_constructor_(self, x):
return self.element_class(self, {(i,): c for i,c in iteritems(x)})
return self.element_class(self, {(i,): R(c) for i,c in iteritems(x) if R(c) != R.zero()})

if isinstance(x, CliffordAlgebraElement):
if x.parent() is self:
return x
if self.has_coerce_map_from(x.parent()):
R = self.base_ring()
return self.element_class(self, {i: R(c) for i,c in x if R(c) != R.zero()})
if (isinstance(x, CliffordAlgebraElement)
and self.has_coerce_map_from(x.parent())):
R = self.base_ring()
return self.element_class(self, {i: R(c) for i,c in x if R(c) != R.zero()})

return super(CliffordAlgebra, self)._element_constructor_(x)

Expand Down
6 changes: 6 additions & 0 deletions src/sage/algebras/commutative_dga.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,12 @@ def _element_constructor_(self, x, coerce=True):
sage: A.<x,y,z,t> = GradedCommutativeAlgebra(GF(5))
sage: A({(1,3,0,1): 2, (2,2,1,2): 3})
0
TESTS::
sage: B = A.cdg_algebra({})
sage: B(x, coerce=False)
x
"""
if isinstance(x, QuotientRingElement):
if x.parent() is self:
Expand Down
4 changes: 1 addition & 3 deletions src/sage/algebras/free_algebra_quotient.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _element_constructor_(self, x):
EXAMPLES::
sage: H, (i,j,k) = sage.algebras.free_algebra_quotient.hamilton_quatalg(QQ)
sage: H._element_constructor_(i) is i
sage: H(i) is i
True
sage: a = H._element_constructor_(1); a
1
Expand All @@ -170,8 +170,6 @@ def _element_constructor_(self, x):
sage: a = H._element_constructor_([1,2,3,4]); a
1 + 2*i + 3*j + 4*k
"""
if isinstance(x, FreeAlgebraQuotientElement) and x.parent() is self:
return x
return self.element_class(self,x)

def _coerce_map_from_(self,S):
Expand Down
2 changes: 0 additions & 2 deletions src/sage/groups/raag.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ def _element_constructor_(self, x):
1
"""
if isinstance(x, RightAngledArtinGroup.Element):
if x.parent() is self:
return x
raise ValueError("there is no coercion from {} into {}".format(x.parent(), self))
if x == 1:
return self.one()
Expand Down
5 changes: 5 additions & 0 deletions src/sage/modular/modform/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,11 @@ def _element_constructor_(self, x, check=True):
sage: N(M.basis()[0])
q - q^3 - 2*q^4 + q^5 + O(q^6)
TESTS::
sage: M = ModularForms(13, 4)
sage: M(M([1, 2, 3, 4, 5]), check=True)
4 + 6*q + 47*q^2 + 143*q^3 + 358*q^4 + 630*q^5 + O(q^6)
"""
if isinstance(x, self.element_class):
if x.parent() is self:
Expand Down
2 changes: 0 additions & 2 deletions src/sage/modular/overconvergent/genus0.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,6 @@ def _element_constructor_(self, input):
input = ZZ(input)

if isinstance(input, OverconvergentModularFormElement):
if input.parent() is self:
return input
return self._coerce_from_ocmf(input)

elif isinstance(input, ModularFormElement):
Expand Down
5 changes: 5 additions & 0 deletions src/sage/monoids/free_monoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ def _element_constructor_(self, x, check=True):
a^2*b^2*c*a*b*a*c
sage: F(Word([]))
1
TESTS::
sage: F(F(w), check=False)
a^2*b^2*c*a*b*a*c
"""
# There should really be some careful type checking here...
if isinstance(x, FreeMonoidElement) and x.parent() is self:
Expand Down
5 changes: 1 addition & 4 deletions src/sage/numerical/linear_functions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,7 @@ cdef class LinearFunctionsParent_class(Parent):
False
"""
if is_LinearFunction(x):
if x.parent() is self:
return x
else:
return LinearFunction(self, (<LinearFunction>x)._f)
return LinearFunction(self, (<LinearFunction>x)._f)
return LinearFunction(self, x)

cpdef _coerce_map_from_(self, R):
Expand Down
5 changes: 1 addition & 4 deletions src/sage/numerical/linear_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,7 @@ def _element_constructor_(self, x):
"""
M = self.free_module()
if is_LinearTensor(x):
if x.parent() is self:
return x
else:
x = x.dict()
x = x.dict()
elif is_LinearFunction(x):
x = dict([key, self._convert_constant(value)] for key, value in x.dict().items())
elif isinstance(x, dict):
Expand Down
2 changes: 0 additions & 2 deletions src/sage/rings/fraction_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,6 @@ def _element_constructor_(self, x, y=None, coerce=True):
(s^2 + 2*s)/(s^2 - 1)
"""
if y is None:
if isinstance(x, Element) and x.parent() is self:
return x
ring_one = self.ring().one()
try:
return self._element_class(self, x, ring_one, coerce=coerce)
Expand Down
5 changes: 1 addition & 4 deletions src/sage/rings/infinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,7 @@ def _element_constructor_(self, x):

# Handle all ways to represent infinity first
if isinstance(x, InfinityElement):
if x.parent() is self:
return x
else:
return self.gen()
return self.gen()
elif isinstance(x, float):
if x in [float('+inf'), float('-inf')]:
return self.gen()
Expand Down
7 changes: 4 additions & 3 deletions src/sage/rings/laurent_series_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ def _element_constructor_(self, x, n=0, prec=infinity):
- ``prec`` -- (default: ``infinity``) the precision of the series
as an integer.
EXAMPLES::
sage: R.<u> = LaurentSeriesRing(Qp(5, 10))
Expand All @@ -393,11 +392,13 @@ def _element_constructor_(self, x, n=0, prec=infinity):
sage: R(t + t^2 + O(t^3), prec=2)
(1 + O(5^10))*u + O(u^2)
Note that coercing an element into its own parent just produces
that element again (since Laurent series are immutable)::
Coercing an element into its own parent produces that element
again, unless a different ``n`` or ``prec`` is given::
sage: u is R(u)
True
sage: R(u, n=3, prec=7)
(1 + O(5^10))*u^4 + O(u^7)
Rational functions are accepted::
Expand Down
2 changes: 0 additions & 2 deletions src/sage/rings/number_field/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,6 @@ def _element_constructor_(self, x):
3*a^2 + 2*a + 1
"""
if is_Element(x) and x.parent() is self:
return x
if isinstance(x, (tuple, list)):
x = sum(xi*gi for xi,gi in zip(x,self.gens()))
if not is_Element(x) or x.parent() is not self._K:
Expand Down
5 changes: 5 additions & 0 deletions src/sage/rings/padics/generic_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,11 @@ def _element_constructor_(self, x, prec=None):
1 + O(2^10)
sage: x - y
O(2^50)
TESTS::
sage: R(x, prec=5)
1 + O(2^5)
"""
# We first try the _copy method which is sharp on precision
try:
Expand Down
3 changes: 0 additions & 3 deletions src/sage/rings/polynomial/infinite_polynomial_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,6 @@ def _element_constructor_(self, x):
...
ValueError: Can't convert 1/3 into an element of Infinite polynomial ring in x over Integer Ring
"""
# if x is in self, there's nothing left to do
if parent(x) is self:
return x
from sage.rings.polynomial.infinite_polynomial_element import InfinitePolynomial
# In many cases, the easiest solution is to "simply" evaluate
# the string representation.
Expand Down
35 changes: 0 additions & 35 deletions src/sage/rings/polynomial/multi_polynomial_libsingular.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -843,41 +843,6 @@ cdef class MPolynomialRing_libsingular(MPolynomialRing_base):
if base_ring.has_coerce_map_from(element.parent()._mpoly_base_ring(self.variable_names())):
return self(element._mpoly_dict_recursive(self.variable_names(), base_ring))

if isinstance(element, CommutativeRingElement):
# base ring elements
if element.parent() is base_ring:
# shortcut for GF(p)
if isinstance(base_ring, FiniteField_prime_modn):
_p = p_ISet(int(element) % _ring.cf.ch, _ring)
else:
_n = sa2si(element,_ring)
_p = p_NSet(_n, _ring)
return new_MP(self, _p)
# also accepting ZZ
elif is_IntegerRing(element.parent()):
if isinstance(base_ring, FiniteField_prime_modn):
_p = p_ISet(int(element),_ring)
else:
_n = sa2si(base_ring(element),_ring)
_p = p_NSet(_n, _ring)
return new_MP(self, _p)
# fall back to base ring
try:
element = base_ring._coerce_c(element)
_n = sa2si(element,_ring)
_p = p_NSet(_n, _ring)
return new_MP(self, _p)
except TypeError:
pass

elif isinstance(element, int) or isinstance(element, long):
if isinstance(base_ring, FiniteField_prime_modn):
_p = p_ISet(element % _ring.cf.ch, _ring)
else:
_n = sa2si(base_ring(element), _ring)
_p = p_NSet(_n, _ring)
return new_MP(self, _p)

if isinstance(element, (SingularElement, cypari2.gen.Gen)):
element = str(element)

Expand Down
3 changes: 0 additions & 3 deletions src/sage/rings/polynomial/polynomial_quotient_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,6 @@ def _element_constructor_(self, x):
-x
"""
P = parent(x)
if P is self:
return x
if not isinstance(x, six.string_types):
try:
return self.element_class(self, self.__ring(x) , check=True)
Expand Down
5 changes: 5 additions & 0 deletions src/sage/rings/polynomial/skew_polynomial_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ def _element_constructor_(self, a=None, check=True, construct=False, **kwds):
0
sage: S(0).list()
[]
TESTS::
sage: S(x, check=True)
x
"""
C = self._polynomial_class
if isinstance(a, list):
Expand Down
5 changes: 5 additions & 0 deletions src/sage/rings/quotient_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,11 @@ def _element_constructor_(self, x, coerce=True):
Traceback (most recent call last):
...
TypeError: no canonical coercion from Finite Field of size 7 to Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 + y^2)
TESTS::
sage: S(x, coerce=False)
a
"""
if isinstance(x, quotient_ring_element.QuotientRingElement):
if x.parent() is self:
Expand Down
2 changes: 2 additions & 0 deletions src/sage/schemes/toric/divisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ def _element_constructor_(self, x, check=True, reduce=True):
Traceback (most recent call last):
...
TypeError: 'sage.rings.integer.Integer' object is not iterable
sage: TDiv(TDiv.gen(0), check=True)
V(x)
"""
if is_ToricDivisor(x):
if x.parent() is self:
Expand Down
5 changes: 1 addition & 4 deletions src/sage/symbolic/ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,7 @@ cdef class SymbolicRing(CommutativeRing):
"""
cdef GEx exp
if is_Expression(x):
if (<Expression>x)._parent is self:
return x
else:
return new_Expression_from_GEx(self, (<Expression>x)._gobj)
return new_Expression_from_GEx(self, (<Expression>x)._gobj)
if hasattr(x, '_symbolic_'):
return x._symbolic_(self)
elif isinstance(x, str):
Expand Down

0 comments on commit 586f977

Please sign in to comment.