Ticket #12510: trac_12510-nonzero_equal_consistency-fh.patch

File trac_12510-nonzero_equal_consistency-fh.patch, 18.6 KB (added by hivert, 7 years ago)
  • sage/algebras/letterplace/free_algebra_letterplace.pyx

    # HG changeset patch
    # User Florent Hivert <Florent.Hivert@univ-rouen.fr>
    # Date 1329468762 -3600
    # Node ID a4f1d9abb925eb2d6c94972226775d35427a5929
    # Parent  46c148d364a31d689e8f2ada71b44cdf611d2651
    #12510: Add consistency tests for __nonzero__ in TestSuite.
    
    diff --git a/sage/algebras/letterplace/free_algebra_letterplace.pyx b/sage/algebras/letterplace/free_algebra_letterplace.pyx
    a b cdef class FreeAlgebra_letterplace(Algeb 
    249249              Running the test suite of self.an_element()
    250250              running ._test_category() . . . pass
    251251              running ._test_eq() . . . pass
     252              running ._test_nonzero_equal() . . . pass
    252253              running ._test_not_implemented_methods() . . . pass
    253254              running ._test_pickling() . . . pass
    254255              pass
  • sage/categories/algebras_with_basis.py

    diff --git a/sage/categories/algebras_with_basis.py b/sage/categories/algebras_with_basis.py
    a b class AlgebrasWithBasis(Category_over_ba 
    6868          Running the test suite of self.an_element()
    6969          running ._test_category() . . . pass
    7070          running ._test_eq() . . . pass
     71          running ._test_nonzero_equal() . . . pass
    7172          running ._test_not_implemented_methods() . . . pass
    7273          running ._test_pickling() . . . pass
    7374          pass
  • sage/categories/category.py

    diff --git a/sage/categories/category.py b/sage/categories/category.py
    a b class Category(UniqueRepresentation, Sag 
    10791079        EXAMPLES::
    10801080
    10811081            sage: Algebras(QQ).required_methods()
    1082             {'parent': {'required': ['__contains__'], 'optional': []}, 'element': {'required': [], 'optional': ['_add_', '_mul_']}}
    1083 
     1082            {'parent': {'required': ['__contains__'], 'optional': []}, 'element': {'required': ['__nonzero__'], 'optional': ['_add_', '_mul_']}}
    10841083        """
    10851084        return { "parent"  : abstract_methods_of_class(self.parent_class),
    10861085                 "element" : abstract_methods_of_class(self.element_class) }
  • sage/categories/commutative_additive_monoids.py

    diff --git a/sage/categories/commutative_additive_monoids.py b/sage/categories/commutative_additive_monoids.py
    a b Commutative additive monoids 
    1010#******************************************************************************
    1111
    1212from sage.categories.category import Category
     13from sage.misc.abstract_method import abstract_method
    1314from sage.misc.cachefunc import cached_method
    1415from sage.categories.category_singleton import Category_singleton
    1516from sage.categories.commutative_additive_semigroups import CommutativeAdditiveSemigroups
    class CommutativeAdditiveMonoids(Categor 
    7980            # Check that zero is immutable by asking its hash:
    8081            tester.assertEqual(type(zero.__hash__()), int)
    8182            tester.assertEqual(zero.__hash__(), zero.__hash__())
     83            # Check that bool behave consistently on zero
     84            tester.assertFalse(bool(self.zero()))
    8285
    8386        @cached_method
    8487        def zero(self):
    class CommutativeAdditiveMonoids(Categor 
    138141            return sum(args, self.zero())
    139142
    140143    class ElementMethods:
    141         pass
    142144
    143145#         TODO: merge with the implementation in Element which currently
    144146#         overrides this one, and further requires self.parent()(0) to work.
    class CommutativeAdditiveMonoids(Categor 
    159161#             """
    160162#             return self == self.parent().zero()
    161163
     164        @abstract_method
     165        def __nonzero__(self):
     166            """
     167            Returns whether ``self`` is not zero
     168
     169            All parents in the category ``CommutativeAdditiveMonoids()``
     170            should implement this method.
     171
     172            .. note:: This is currently not useful because this method is
     173               overridden by ``Element``.
     174
     175            TESTS::
     176
     177                sage: S = CommutativeAdditiveMonoids().example()
     178                sage: bool(S.zero())
     179                False
     180                sage: bool(S.an_element())
     181                True
     182             """
     183
     184        def _test_nonzero_equal(self, **options):
     185            r"""
     186            Test that ``.__nonzero__()`` behave consistently with `` == 0``.
     187
     188            TESTS::
     189
     190                sage: S = CommutativeAdditiveMonoids().example()
     191                sage: S.zero()._test_nonzero_equal()
     192                sage: S.an_element()._test_nonzero_equal()
     193            """
     194            tester = self._tester(**options)
     195            tester.assertEqual(bool(self), self != self.parent().zero())
     196            tester.assertEqual(not self, self == self.parent().zero())
    162197    class WithRealizations(WithRealizationsCategory):
    163198
    164199        class ParentMethods:
  • sage/categories/examples/commutative_additive_monoids.py

    diff --git a/sage/categories/examples/commutative_additive_monoids.py b/sage/categories/examples/commutative_additive_monoids.py
    a b class FreeCommutativeAdditiveMonoid(Free 
    4646          Running the test suite of self.an_element()
    4747          running ._test_category() . . . pass
    4848          running ._test_eq() . . . pass
     49          running ._test_nonzero_equal() . . . pass
    4950          running ._test_not_implemented_methods() . . . pass
    5051          running ._test_pickling() . . . pass
    5152          pass
    class FreeCommutativeAdditiveMonoid(Free 
    103104        """
    104105        return self(())
    105106
     107    class Element(FreeCommutativeAdditiveSemigroup.Element):
     108        def __nonzero__(self):
     109            """
     110            Check if ``self`` is not the zero of the monoid
     111
     112            EXAMPLES::
     113
     114                sage: M = CommutativeAdditiveMonoids().example()
     115                sage: M.zero().__nonzero__()
     116                False
     117                sage: bool(M.zero())
     118                False
     119                sage: [bool(m) for m in M.additive_semigroup_generators()]
     120                [True, True, True, True]
     121            """
     122            return any(x for x in self.value.values())
     123
    106124Example = FreeCommutativeAdditiveMonoid
  • sage/categories/examples/facade_sets.py

    diff --git a/sage/categories/examples/facade_sets.py b/sage/categories/examples/facade_sets.py
    a b class PositiveIntegerMonoid(UniqueRepres 
    4040          Running the test suite of self.an_element()
    4141          running ._test_category() . . . pass
    4242          running ._test_eq() . . . pass
     43          running ._test_nonzero_equal() . . . pass
    4344          running ._test_not_implemented_methods() . . . pass
    4445          running ._test_pickling() . . . pass
    4546          pass
    class IntegersCompletion(UniqueRepresent 
    134135          Running the test suite of self.an_element()
    135136          running ._test_category() . . . pass
    136137          running ._test_eq() . . . pass
     138          running ._test_nonzero_equal() . . . pass
    137139          running ._test_not_implemented_methods() . . . pass
    138140          running ._test_pickling() . . . pass
    139141          pass
  • sage/categories/examples/finite_enumerated_sets.py

    diff --git a/sage/categories/examples/finite_enumerated_sets.py b/sage/categories/examples/finite_enumerated_sets.py
    a b class Example(UniqueRepresentation, Pare 
    4343          Running the test suite of self.an_element()
    4444          running ._test_category() . . . pass
    4545          running ._test_eq() . . . pass
     46          running ._test_nonzero_equal() . . . pass
    4647          running ._test_not_implemented_methods() . . . pass
    4748          running ._test_pickling() . . . pass
    4849          pass
  • sage/categories/examples/infinite_enumerated_sets.py

    diff --git a/sage/categories/examples/infinite_enumerated_sets.py b/sage/categories/examples/infinite_enumerated_sets.py
    a b class NonNegativeIntegers(UniqueRepresen 
    5656          Running the test suite of self.an_element()
    5757          running ._test_category() . . . pass
    5858          running ._test_eq() . . . pass
     59          running ._test_nonzero_equal() . . . pass
    5960          running ._test_not_implemented_methods() . . . pass
    6061          running ._test_pickling() . . . pass
    6162          pass
  • sage/categories/examples/sets_cat.py

    diff --git a/sage/categories/examples/sets_cat.py b/sage/categories/examples/sets_cat.py
    a b class PrimeNumbers(UniqueRepresentation, 
    6363          Running the test suite of self.an_element()
    6464          running ._test_category() . . . pass
    6565          running ._test_eq() . . . pass
     66          running ._test_nonzero_equal() . . . pass
    6667          running ._test_not_implemented_methods() . . . pass
    6768          running ._test_pickling() . . . pass
    6869          pass
    class PrimeNumbers_Facade(PrimeNumbers_A 
    620621          Running the test suite of self.an_element()
    621622          running ._test_category() . . . pass
    622623          running ._test_eq() . . . pass
     624          running ._test_nonzero_equal() . . . pass
    623625          running ._test_not_implemented_methods() . . . pass
    624626          running ._test_pickling() . . . pass
    625627          pass
  • sage/categories/hopf_algebras_with_basis.py

    diff --git a/sage/categories/hopf_algebras_with_basis.py b/sage/categories/hopf_algebras_with_basis.py
    a b class HopfAlgebrasWithBasis(Category_ove 
    7878          Running the test suite of self.an_element()
    7979          running ._test_category() . . . pass
    8080          running ._test_eq() . . . pass
     81          running ._test_nonzero_equal() . . . pass
    8182          running ._test_not_implemented_methods() . . . pass
    8283          running ._test_pickling() . . . pass
    8384          pass
  • sage/categories/modules.py

    diff --git a/sage/categories/modules.py b/sage/categories/modules.py
    a b class Modules(Category_module): 
    189189            return [Modules(self.base_category.base_ring())]
    190190
    191191        class ParentMethods:
     192
    192193            @cached_method
    193194            def zero(self):
    194195                """
    class Modules(Category_module): 
    206207                    0
    207208                    sage: f(E.monomial(3)) == F.zero()
    208209                    True
     210
     211                TESTS:
     212
     213                We check that ``H.zero()`` is picklable::
     214
     215                    sage: loads(dumps(f.parent().zero()))
     216                    Generic morphism:
     217                      From: Free module generated by {1, 2, 3} over Integer Ring
     218                      To:   Free module generated by {2, 3, 4} over Integer Ring
    209219                """
    210                 return self(lambda x: self.codomain().zero())
     220                from sage.misc.constant_function import ConstantFunction
     221                return self(ConstantFunction(self.codomain().zero()))
    211222
    212223    class EndCategory(HomCategory):
    213224        """
  • sage/categories/sets_cat.py

    diff --git a/sage/categories/sets_cat.py b/sage/categories/sets_cat.py
    a b class Sets(Category_singleton): 
    432432                  Running the test suite of self.an_element()
    433433                  running ._test_category() . . . pass
    434434                  running ._test_eq() . . . pass
     435                  running ._test_nonzero_equal() . . . pass
    435436                  running ._test_not_implemented_methods() . . . pass
    436437                  running ._test_pickling() . . . pass
    437438                <BLANKLINE>
  • sage/combinat/ncsf_qsym/generic_basis_code.py

    diff --git a/sage/combinat/ncsf_qsym/generic_basis_code.py b/sage/combinat/ncsf_qsym/generic_basis_code.py
    a b class AlgebraMorphism(ModuleMorphismByLi 
    855855            -Phi[3, 1, 2]
    856856            sage: f.__class__
    857857            <class 'sage.combinat.ncsf_qsym.generic_basis_code.AlgebraMorphism'>
    858             sage: TestSuite(f).run() # known issue; see ModuleMorphismByLinearity.__init__
     858            sage: TestSuite(f).run(skip=['_test_nonzero_equal']) # known issue; see ModuleMorphismByLinearity.__init__
    859859            Failure in _test_category:
    860860            ...
    861861            The following tests failed: _test_category
    862 
    863862        """
    864863        assert position == 0
    865864        assert codomain is not None
  • sage/misc/sage_unittest.py

    diff --git a/sage/misc/sage_unittest.py b/sage/misc/sage_unittest.py
    a b class TestSuite(object): 
    2929        sage: TestSuite(1).run(verbose = True)
    3030        running ._test_category() . . . pass
    3131        running ._test_eq() . . . pass
     32        running ._test_nonzero_equal() . . . pass
    3233        running ._test_not_implemented_methods() . . . pass
    3334        running ._test_pickling() . . . pass
    3435
    class TestSuite(object): 
    176177            sage: TestSuite(1).run(verbose = True)
    177178            running ._test_category() . . . pass
    178179            running ._test_eq() . . . pass
     180            running ._test_nonzero_equal() . . . pass
    179181            running ._test_not_implemented_methods() . . . pass
    180182            running ._test_pickling() . . . pass
    181183
    class TestSuite(object): 
    184186            sage: TestSuite(1).run(verbose = True, skip ="_test_pickling")
    185187            running ._test_category() . . . pass
    186188            running ._test_eq() . . . pass
     189            running ._test_nonzero_equal() . . . pass
    187190            running ._test_not_implemented_methods() . . . pass
    188191            sage: TestSuite(1).run(verbose = True, skip =["_test_pickling", "_test_category"])
    189192            running ._test_eq() . . . pass
     193            running ._test_nonzero_equal() . . . pass
    190194            running ._test_not_implemented_methods() . . . pass
    191195
    192196        We now show (and test) some standard error reports::
  • sage/modules/free_module_homspace.py

    diff --git a/sage/modules/free_module_homspace.py b/sage/modules/free_module_homspace.py
    a b import sage.categories.homset 
    7676import sage.matrix.all as matrix
    7777import free_module_morphism
    7878from inspect import isfunction
     79from sage.misc.cachefunc import cached_method
    7980
    8081
    8182def is_FreeModuleHomspace(x):
    class FreeModuleHomspace(sage.categories 
    188189                    pass
    189190        return free_module_morphism.FreeModuleMorphism(self, A)
    190191
     192    @cached_method
     193    def zero(self):
     194        """
     195        EXAMPLES::
     196
     197            sage: E = ZZ^2
     198            sage: F = ZZ^3
     199            sage: H = Hom(E, F)
     200            sage: f = H.zero()
     201            sage: f
     202            Free module morphism defined by the matrix
     203            [0 0 0]
     204            [0 0 0]
     205            Domain: Ambient free module of rank 2 over the principal ideal domain Integer Ring
     206            Codomain: Ambient free module of rank 3 over the principal ideal domain Integer Ring
     207            sage: f(E.an_element())
     208            (0, 0, 0)
     209            sage: f(E.an_element()) == F.zero()
     210            True
     211
     212        TESTS:
     213
     214        We check that ``H.zero()`` is picklable::
     215
     216            sage: loads(dumps(f.parent().zero()))
     217            Free module morphism defined by the matrix
     218            [0 0 0]
     219            [0 0 0]
     220            Domain: Ambient free module of rank 2 over the principal ideal domain Integer Ring
     221            Codomain: Ambient free module of rank 3 over the principal ideal domain Integer Ring
     222        """
     223        return self(lambda x: self.codomain().zero())
     224
    191225    def _matrix_space(self):
    192226        """
    193227        Return underlying matrix space that contains the matrices that define
  • sage/modules/module.pyx

    diff --git a/sage/modules/module.pyx b/sage/modules/module.pyx
    a b cdef class Module(sage.structure.parent. 
    9999        sage: M.category()
    100100        Category of modules over Integer Ring
    101101        sage: M.category().required_methods()
    102         {'parent': {'required': ['__contains__'],
    103                     'optional': []},
    104          'element': {'required': [],
    105                      'optional': ['_add_']}}
     102        {'parent': {'required': ['__contains__'], 'optional': []}, 'element': {'required': ['__nonzero__'], 'optional': ['_add_']}}
    106103        sage: M_QQ = Module(QQ)
    107104        sage: M_QQ.category()
    108105        Category of vector spaces over Rational Field
  • sage/rings/quotient_ring.py

    diff --git a/sage/rings/quotient_ring.py b/sage/rings/quotient_ring.py
    a b TESTS:: 
    1414    sage: R.<x> = PolynomialRing(ZZ)
    1515    sage: I = R.ideal([4 + 3*x + x^2, 1 + x^2])
    1616    sage: S = R.quotient_ring(I);
    17     sage: TestSuite(S).run()
     17
     18.. todo::
     19
     20    The following skipped tests should be removed once :trac:`13999` is fixed::
     21
     22        sage: TestSuite(S).run(skip=['_test_nonzero_equal', '_test_elements', '_test_zero'])
    1823
    1924In trac ticket #11068, non-commutative quotient rings `R/I` were
    2025implemented.  The only requirement is that the two-sided ideal `I`
  • sage/rings/quotient_ring_element.py

    diff --git a/sage/rings/quotient_ring_element.py b/sage/rings/quotient_ring_element.py
    a b class QuotientRingElement(ring_element.R 
    140140        is in `I`.
    141141
    142142        EXAMPLES::
    143        
     143
    144144            sage: R.<x,y> = QQ[]; S.<a,b> = R.quo(x^2 + y^2); type(a)
    145145            <class 'sage.rings.quotient_ring_element.QuotientRing_generic_with_category.element_class'>
    146146            sage: bool(a)     # indirect docteest
    class QuotientRingElement(ring_element.R 
    149149            False
    150150
    151151        TESTS::
    152        
     152
    153153            sage: S(0).__nonzero__()
    154154            False
    155155            sage: (a-a).__nonzero__()
  • sage/rings/ring.pyx

    diff --git a/sage/rings/ring.pyx b/sage/rings/ring.pyx
    a b cdef class Ring(ParentWithGens): 
    111111          Running the test suite of self.an_element()
    112112          running ._test_category() . . . pass
    113113          running ._test_eq() . . . pass
     114          running ._test_nonzero_equal() . . . pass
    114115          running ._test_not_implemented_methods() . . . pass
    115116          running ._test_pickling() . . . pass
    116117          pass
  • sage/structure/parent.pyx

    diff --git a/sage/structure/parent.pyx b/sage/structure/parent.pyx
    a b cdef class Parent(category_object.Catego 
    582582              Running the test suite of self.an_element()
    583583              running ._test_category() . . . pass
    584584              running ._test_eq() . . . pass
     585              running ._test_nonzero_equal() . . . pass
    585586              running ._test_not_implemented_methods() . . . pass
    586587              running ._test_pickling() . . . pass
    587588              pass