diff --git a/src/sage/algebras/hecke_algebras/ariki_koike_algebra.py b/src/sage/algebras/hecke_algebras/ariki_koike_algebra.py index b78b7691e7b..2e4ddb6a4bd 100644 --- a/src/sage/algebras/hecke_algebras/ariki_koike_algebra.py +++ b/src/sage/algebras/hecke_algebras/ariki_koike_algebra.py @@ -1126,7 +1126,7 @@ def inverse_T(self, i): return self._from_dict({m: ~self._q, self.one_basis(): c}) class Element(CombinatorialFreeModule.Element): - def inverse(self): + def __invert__(self): r""" Return the inverse if ``self`` is a basis element. @@ -1135,7 +1135,7 @@ def inverse(self): sage: LT = algebras.ArikiKoike(3, 4).LT() sage: t = LT.T(1) * LT.T(2) * LT.T(3); t T[1,2,3] - sage: t.inverse() + sage: t.inverse() # indirect doctest (q^-3-3*q^-2+3*q^-1-1) + (q^-3-2*q^-2+q^-1)*T[3] + (q^-3-2*q^-2+q^-1)*T[2] + (q^-3-q^-2)*T[3,2] + (q^-3-2*q^-2+q^-1)*T[1] + (q^-3-q^-2)*T[1,3] @@ -1149,8 +1149,6 @@ def inverse(self): H = self.parent() return ~self[l,w] * H.prod(H.inverse_T(i) for i in reversed(w.reduced_word())) - __invert__ = inverse - class T(_Basis): r""" The basis of the Ariki-Koike algebra given by monomials of the diff --git a/src/sage/algebras/iwahori_hecke_algebra.py b/src/sage/algebras/iwahori_hecke_algebra.py index ae50b39a3e0..b779269b2f8 100644 --- a/src/sage/algebras/iwahori_hecke_algebra.py +++ b/src/sage/algebras/iwahori_hecke_algebra.py @@ -1727,7 +1727,7 @@ class Element(CombinatorialFreeModule.Element): sage: T1.parent() Iwahori-Hecke algebra of type A2 in 1,-1 over Integer Ring in the T-basis """ - def inverse(self): + def __invert__(self): r""" Return the inverse if ``self`` is a basis element. @@ -1741,7 +1741,7 @@ def inverse(self): sage: R. = LaurentPolynomialRing(QQ) sage: H = IwahoriHeckeAlgebra("A2", q).T() sage: [T1,T2] = H.algebra_generators() - sage: x = (T1*T2).inverse(); x + sage: x = (T1*T2).inverse(); x # indirect doctest (q^-2)*T[2,1] + (q^-2-q^-1)*T[1] + (q^-2-q^-1)*T[2] + (q^-2-2*q^-1+1) sage: x*T1*T2 1 @@ -1766,8 +1766,6 @@ def inverse(self): return H.prod(H.inverse_generator(i) for i in reversed(w.reduced_word())) - __invert__ = inverse - standard = T class _KLHeckeBasis(_Basis): diff --git a/src/sage/algebras/yokonuma_hecke_algebra.py b/src/sage/algebras/yokonuma_hecke_algebra.py index dde7772e227..a8c74e25f0b 100644 --- a/src/sage/algebras/yokonuma_hecke_algebra.py +++ b/src/sage/algebras/yokonuma_hecke_algebra.py @@ -454,7 +454,7 @@ def inverse_g(self, i): return self.g(i) + (~self._q + self._q) * self.e(i) class Element(CombinatorialFreeModule.Element): - def inverse(self): + def __invert__(self): r""" Return the inverse if ``self`` is a basis element. @@ -463,7 +463,7 @@ def inverse(self): sage: Y = algebras.YokonumaHecke(3, 3) sage: t = prod(Y.t()); t t1*t2*t3 - sage: ~t + sage: t.inverse() # indirect doctest t1^2*t2^2*t3^2 sage: [3*~(t*g) for g in Y.g()] [(q^-1+q)*t2*t3^2 + (q^-1+q)*t1*t3^2 @@ -494,6 +494,3 @@ def inverse(self): c = ~self.coefficients()[0] telt = H.monomial( (tuple((H._d - e) % H._d for e in t), H._Pn.one()) ) return c * telt * H.prod(H.inverse_g(i) for i in reversed(w.reduced_word())) - - __invert__ = inverse - diff --git a/src/sage/categories/complex_reflection_or_generalized_coxeter_groups.py b/src/sage/categories/complex_reflection_or_generalized_coxeter_groups.py index 720d0097632..daa36275cf9 100644 --- a/src/sage/categories/complex_reflection_or_generalized_coxeter_groups.py +++ b/src/sage/categories/complex_reflection_or_generalized_coxeter_groups.py @@ -1141,7 +1141,7 @@ def _mul_(self, other): """ return self.apply_simple_reflections(other.reduced_word()) - def inverse(self): + def __invert__(self): """ Return the inverse of ``self``. @@ -1149,7 +1149,7 @@ def inverse(self): sage: W = WeylGroup(['B',7]) sage: w = W.an_element() - sage: u = w.inverse() + sage: u = w.inverse() # indirect doctest sage: u == ~w True sage: u * w == w * u @@ -1165,8 +1165,6 @@ def inverse(self): """ return self.parent().one().apply_simple_reflections(self.reduced_word_reverse_iterator()) - __invert__ = inverse - def apply_conjugation_by_simple_reflection(self, i): r""" Conjugate ``self`` by the ``i``-th simple reflection. diff --git a/src/sage/categories/monoids.py b/src/sage/categories/monoids.py index 42b55b8dda4..d598a6cecb3 100644 --- a/src/sage/categories/monoids.py +++ b/src/sage/categories/monoids.py @@ -313,6 +313,36 @@ def powers(self, n): l.append(x) return l + def __invert__(self): + r""" + Return the inverse of ``self``. + + The default implementation is to divide ``self.one()``. + + EXAMPLES:: + + sage: A = Matrix([[1, 0], [1, 1]]) + sage: ~A + [ 1 0] + [-1 1] + """ + return self.parent().one()._div_(self) + + def inverse(self): + """ + Return the inverse of ``self``. + + This an alias for inversion, defined in ``__invert__``. + + Element classes should implement ``__invert__`` only. + + EXAMPLES:: + + sage: AA(sqrt(~2)).inverse() + 1.414213562373095? + """ + return self.__invert__() + class Commutative(CategoryWithAxiom): r""" Category of commutative (abelian) monoids. diff --git a/src/sage/combinat/affine_permutation.py b/src/sage/combinat/affine_permutation.py index 5da8030f454..b519553a777 100644 --- a/src/sage/combinat/affine_permutation.py +++ b/src/sage/combinat/affine_permutation.py @@ -159,21 +159,19 @@ def __mul__(self, q): return self.__rmul__(q) @cached_method - def inverse(self): + def __invert__(self): r""" Return the inverse affine permutation. EXAMPLES:: sage: p = AffinePermutationGroup(['A',7,1])([3, -1, 0, 6, 5, 4, 10, 9]) - sage: p.inverse() + sage: p.inverse() # indirect doctest Type A affine permutation with window [0, -1, 1, 6, 5, 4, 10, 11] """ - inv = [self.position(i) for i in range(1,len(self)+1)] + inv = [self.position(i) for i in range(1, len(self) + 1)] return type(self)(self.parent(), inv, check=False) - __invert__=inverse - def apply_simple_reflection(self, i, side='right'): r""" Apply a simple reflection. @@ -853,8 +851,8 @@ def to_lehmer_code(self, typ='decreasing', side='right'): a=self(i) for j in range(i-self.k, i): b=self(j) - #A small rotation is necessary for the reduced word from - #the lehmer code to match the element. + # A small rotation is necessary for the reduced word from + # the Lehmer code to match the element. if a < b: code[i-1]+=((b-a)//(self.k+1)+1) elif typ[0] == 'i' and side[0] == 'l': @@ -2348,6 +2346,7 @@ def from_lehmer_code(self, C, typ='decreasing', side='right'): Element = AffinePermutationTypeA + class AffinePermutationGroupTypeC(AffinePermutationGroupGeneric): #------------------------ #Type-specific methods. diff --git a/src/sage/combinat/colored_permutations.py b/src/sage/combinat/colored_permutations.py index eb139497a52..4ce14eb3f66 100644 --- a/src/sage/combinat/colored_permutations.py +++ b/src/sage/combinat/colored_permutations.py @@ -96,7 +96,7 @@ def _mul_(self, other): p = self._perm._left_to_right_multiply_on_right(other._perm) return self.__class__(self.parent(), colors, p) - def inverse(self): + def __invert__(self): """ Return the inverse of ``self``. @@ -104,7 +104,7 @@ def inverse(self): sage: C = ColoredPermutations(4, 3) sage: s1,s2,t = C.gens() - sage: ~t + sage: ~t # indirect doctest [[0, 0, 3], [1, 2, 3]] sage: all(x * ~x == C.one() for x in C.gens()) True @@ -114,8 +114,6 @@ def inverse(self): tuple([-self._colors[i - 1] for i in ip]), # -1 for indexing ip) - __invert__ = inverse - def __eq__(self, other): """ Check equality. @@ -981,7 +979,7 @@ def _mul_(self, other): p = self._perm._left_to_right_multiply_on_right(other._perm) return self.__class__(self.parent(), colors, p) - def inverse(self): + def __invert__(self): """ Return the inverse of ``self``. @@ -990,7 +988,7 @@ def inverse(self): sage: S = SignedPermutations(4) sage: s1,s2,s3,s4 = S.gens() sage: x = s4*s1*s2*s3*s4 - sage: ~x + sage: ~x # indirect doctest [2, 3, -4, -1] sage: x * ~x == S.one() True @@ -1000,8 +998,6 @@ def inverse(self): tuple([self._colors[i - 1] for i in ip]), # -1 for indexing ip) - __invert__ = inverse - def __iter__(self): """ Iterate over ``self``. diff --git a/src/sage/combinat/root_system/fundamental_group.py b/src/sage/combinat/root_system/fundamental_group.py index 7c097ad7fe3..53117f4213c 100644 --- a/src/sage/combinat/root_system/fundamental_group.py +++ b/src/sage/combinat/root_system/fundamental_group.py @@ -249,7 +249,7 @@ def _repr_(self): """ return self.parent()._prefix + "[" + repr(self.value()) + "]" - def inverse(self): + def __invert__(self): r""" Return the inverse element of ``self``. @@ -257,7 +257,7 @@ def inverse(self): sage: from sage.combinat.root_system.fundamental_group import FundamentalGroupOfExtendedAffineWeylGroup sage: F = FundamentalGroupOfExtendedAffineWeylGroup(['A',3,1]) - sage: F(1).inverse() + sage: F(1).inverse() # indirect doctest pi[3] sage: F = FundamentalGroupOfExtendedAffineWeylGroup(['E',6,1], prefix="f") sage: F(1).inverse() @@ -266,8 +266,6 @@ def inverse(self): par = self.parent() return self.__class__(par, par.dual_node(self.value())) - __invert__ = inverse - def _richcmp_(self, x, op): r""" Compare ``self`` with `x`. diff --git a/src/sage/geometry/hyperbolic_space/hyperbolic_isometry.py b/src/sage/geometry/hyperbolic_space/hyperbolic_isometry.py index 418e3eccbc5..bcdbde6063d 100644 --- a/src/sage/geometry/hyperbolic_space/hyperbolic_isometry.py +++ b/src/sage/geometry/hyperbolic_space/hyperbolic_isometry.py @@ -323,7 +323,7 @@ def matrix(self): """ return self._matrix - def inverse(self): + def __invert__(self): r""" Return the inverse of the isometry ``self``. @@ -331,13 +331,11 @@ def inverse(self): sage: UHP = HyperbolicPlane().UHP() sage: A = UHP.get_isometry(matrix(2,[4,1,3,2])) - sage: B = A.inverse() + sage: B = A.inverse() # indirect doctest sage: A*B == UHP.get_isometry(identity_matrix(2)) True """ - return self.__class__(self.domain(), self.matrix().inverse()) - - __invert__ = inverse + return self.__class__(self.domain(), self.matrix().__invert__()) def is_identity(self): """ diff --git a/src/sage/groups/abelian_gps/element_base.py b/src/sage/groups/abelian_gps/element_base.py index d5b93a3f918..c1eec7ecb1f 100644 --- a/src/sage/groups/abelian_gps/element_base.py +++ b/src/sage/groups/abelian_gps/element_base.py @@ -15,7 +15,7 @@ # Copyright (C) 2012 Volker Braun # # Distributed under the terms of the GNU General Public License (GPL) -# http://www.gnu.org/licenses/ +# https://www.gnu.org/licenses/ ########################################################################### from sage.structure.element import MultiplicativeGroupElement @@ -298,14 +298,14 @@ def __pow__(self, n): for e,order in zip(self._exponents, G.gens_orders()) ] return G.element_class(G, exponents) - def inverse(self): + def __invert__(self): """ - Returns the inverse element. + Return the inverse element. EXAMPLES:: sage: G. = AbelianGroup([0,5]) - sage: a.inverse() + sage: a.inverse() # indirect doctest a^-1 sage: a.__invert__() a^-1 @@ -319,12 +319,10 @@ def inverse(self): (-1, 4) """ G = self.parent() - exponents = [ (-e)%order if order!=0 else -e - for e,order in zip(self._exponents, G.gens_orders()) ] + exponents = [(-e) % order if order != 0 else -e + for e, order in zip(self._exponents, G.gens_orders())] return G.element_class(G, exponents) - __invert__ = inverse - def is_trivial(self): """ Test whether ``self`` is the trivial group element ``1``. diff --git a/src/sage/groups/abelian_gps/values.py b/src/sage/groups/abelian_gps/values.py index 288fb95b93c..e3d681af428 100644 --- a/src/sage/groups/abelian_gps/values.py +++ b/src/sage/groups/abelian_gps/values.py @@ -330,14 +330,14 @@ def __pow__(self, n): pow_self._value = pow(self.value(), m) return pow_self - def inverse(self): + def __invert__(self): """ Return the inverse element. EXAMPLES:: sage: G. = AbelianGroupWithValues([2,-1], [0,4]) - sage: a.inverse() + sage: a.inverse() # indirect doctest a^-1 sage: a.inverse().value() 1/2 @@ -350,13 +350,10 @@ def inverse(self): sage: (a*b).inverse().value() -1/2 """ - m = AbelianGroupElement.inverse(self) + m = AbelianGroupElement.__invert__(self) m._value = ~self.value() return m - __invert__ = inverse - - class AbelianGroupWithValues_class(AbelianGroup_class): """ diff --git a/src/sage/groups/affine_gps/group_element.py b/src/sage/groups/affine_gps/group_element.py index 72d2a4d77cf..8ff43ccc365 100644 --- a/src/sage/groups/affine_gps/group_element.py +++ b/src/sage/groups/affine_gps/group_element.py @@ -404,7 +404,7 @@ def _act_on_(self, x, self_on_left): if self_on_left: return self(x) - def inverse(self): + def __invert__(self): """ Return the inverse group element. @@ -422,19 +422,17 @@ def inverse(self): sage: ~g [1 1] [1] x |-> [0 1] x + [0] - sage: g * g.inverse() + sage: g * g.inverse() # indirect doctest [1 0] [0] x |-> [0 1] x + [0] sage: g * g.inverse() == g.inverse() * g == G(1) True """ parent = self.parent() - A = parent.matrix_space()(self._A.inverse()) - b = -A*self.b() + A = parent.matrix_space()(~self._A) + b = -A * self.b() return parent.element_class(parent, A, b, check=False) - __invert__ = inverse - def _richcmp_(self, other, op): """ Compare ``self`` with ``other``. diff --git a/src/sage/groups/group_exp.py b/src/sage/groups/group_exp.py index bf0db4aa837..3ffa13a6ebf 100644 --- a/src/sage/groups/group_exp.py +++ b/src/sage/groups/group_exp.py @@ -211,20 +211,18 @@ def __init__(self, parent, x): raise ValueError("%s is not an element of %s" % (x, parent._G)) ElementWrapper.__init__(self, parent, x) - def inverse(self): + def __invert__(self): r""" Invert the element ``self``. EXAMPLES:: sage: EZ = GroupExp()(ZZ) - sage: EZ(-3).inverse() + sage: EZ(-3).inverse() # indirect doctest 3 """ return GroupExpElement(self.parent(), -self.value) - __invert__ = inverse - def __mul__(self, x): r""" Multiply ``self`` by `x`. diff --git a/src/sage/groups/group_semidirect_product.py b/src/sage/groups/group_semidirect_product.py index e7e87530d1c..fad2978d443 100644 --- a/src/sage/groups/group_semidirect_product.py +++ b/src/sage/groups/group_semidirect_product.py @@ -58,9 +58,9 @@ def wrapper(prefix, s): return gstr return gstr + " * " + hstr - def inverse(self): + def __invert__(self): r""" - The inverse of ``self``. + Return the inverse of ``self``. EXAMPLES:: @@ -75,19 +75,16 @@ def inverse(self): s1*s2 * t[2*alpha[1] + 2*alpha[2]] sage: g.inverse() s2*s1 * t[2*alpha[1]] - """ par = self.parent() g = self.cartesian_projection(0) h = self.cartesian_projection(1) if par.act_to_right(): - return self.__class__(par,(~g, par._twist(g,~h))) + return self.__class__(par, (~g, par._twist(g, ~h))) else: hi = ~h - return self.__class__(par,(par._twist(hi,~g),hi)) - - __invert__ = inverse + return self.__class__(par, (par._twist(hi, ~g), hi)) def to_opposite(self): r""" diff --git a/src/sage/modular/pollack_stevens/sigma0.py b/src/sage/modular/pollack_stevens/sigma0.py index 6ff9b24b551..eed9e244cd2 100644 --- a/src/sage/modular/pollack_stevens/sigma0.py +++ b/src/sage/modular/pollack_stevens/sigma0.py @@ -295,15 +295,17 @@ def matrix(self): """ return self._mat - def inverse(self): + def __invert__(self): r""" - Return the inverse of self. This will raise an error if the result is not in the monoid. + Return the inverse of ``self``. + + This will raise an error if the result is not in the monoid. EXAMPLES:: sage: from sage.modular.pollack_stevens.sigma0 import Sigma0 sage: s = Sigma0(3)([1,4,3,13]) - sage: s.inverse() + sage: s.inverse() # indirect doctest [13 -4] [-3 1] sage: Sigma0(3)([1, 0, 0, 3]).inverse() diff --git a/src/sage/monoids/free_monoid_element.py b/src/sage/monoids/free_monoid_element.py index e9c04dca256..7345a251464 100644 --- a/src/sage/monoids/free_monoid_element.py +++ b/src/sage/monoids/free_monoid_element.py @@ -21,7 +21,7 @@ # See the GNU General Public License for more details; the full text # is available at: # -# http://www.gnu.org/licenses/ +# https://www.gnu.org/licenses/ #***************************************************************************** from sage.rings.integer import Integer @@ -99,7 +99,7 @@ def __hash__(self): def __iter__(self): """ - Returns an iterator which yields tuples of variable and exponent. + Return an iterator which yields tuples of variable and exponent. EXAMPLES:: @@ -254,6 +254,9 @@ def _mul_(self, y): z._element_list = x_elt[:k] + [ m ] + y_elt[1:] return z + def __invert__(self): + raise NotImplementedError + def __len__(self): """ Return the degree of the monoid element ``self``, where each diff --git a/src/sage/rings/number_field/class_group.py b/src/sage/rings/number_field/class_group.py index 46d0ca8c9d3..04e1b06983a 100644 --- a/src/sage/rings/number_field/class_group.py +++ b/src/sage/rings/number_field/class_group.py @@ -184,7 +184,7 @@ def inverse(self): sage: ~G(2, a) Fractional ideal class (2, a^2 + 2*a - 1) """ - m = AbelianGroupElement.inverse(self) + m = AbelianGroupElement.__invert__(self) m._value = (~self.ideal()).reduce_equiv() return m diff --git a/src/sage/rings/polynomial/polynomial_quotient_ring.py b/src/sage/rings/polynomial/polynomial_quotient_ring.py index 66af8957b9d..f9529952d59 100644 --- a/src/sage/rings/polynomial/polynomial_quotient_ring.py +++ b/src/sage/rings/polynomial/polynomial_quotient_ring.py @@ -321,7 +321,7 @@ class of the category, and store the current class of the quotient sage: isinstance(Q.an_element(),Q.element_class) True sage: [s for s in dir(Q.category().element_class) if not s.startswith('_')] - ['cartesian_product', 'inverse_of_unit', 'is_idempotent', 'is_one', 'is_unit', 'lift', 'powers'] + ['cartesian_product', 'inverse', 'inverse_of_unit', 'is_idempotent', 'is_one', 'is_unit', 'lift', 'powers'] sage: first_class = Q.__class__ We try to find out whether `Q` is a field. Indeed it is, and thus its category, @@ -339,6 +339,7 @@ class of the category, and store the current class of the quotient 'euclidean_degree', 'factor', 'gcd', + 'inverse', 'inverse_of_unit', 'is_idempotent', 'is_one', @@ -1029,8 +1030,10 @@ def is_field(self, proof = True): try: ret = ret and self.modulus().is_irreducible() except NotImplementedError: - if proof: raise - else: ret = False + if proof: + raise + else: + ret = False if ret: from sage.categories.all import Fields @@ -1202,7 +1205,8 @@ def _S_decomposition(self, S): j = 0 for D_iso,_ in iso_classes: if D_abs.is_isomorphic(D_iso): - seen_before = True; break + seen_before = True + break j += 1 if not seen_before: S_abs = [] diff --git a/src/sage/structure/element.pyx b/src/sage/structure/element.pyx index f145e66c7a1..9ac162cbc54 100644 --- a/src/sage/structure/element.pyx +++ b/src/sage/structure/element.pyx @@ -2611,12 +2611,6 @@ cdef class MultiplicativeGroupElement(MonoidElement): """ return self * ~right - def __invert__(self): - r""" - Return the inverse of ``self``. - """ - return self._parent.one() / self - def is_RingElement(x): """ @@ -2624,6 +2618,7 @@ def is_RingElement(x): """ return isinstance(x, RingElement) + cdef class RingElement(ModuleElement): cpdef _mul_(self, other): """