Ticket #9890: trac_9890.patch

File trac_9890.patch, 2.5 KB (added by ncohen, 6 years ago)
  • sage/combinat/perfect_matching.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1284133463 -7200
    # Node ID 26f363af6f8447221195ab0bebdea8662b4203a2
    # Parent  5ede04e6bce52102dca9323a193a09257b1dc027
    trac 9890 - A proper random_element() method for PerfectMatchings
    
    diff --git a/sage/combinat/perfect_matching.py b/sage/combinat/perfect_matching.py
    a b  
    796796        105
    797797        sage: M=PerfectMatchings(('a', 'e', 'b', 'f', 'c', 'd'))
    798798        sage: M.an_element()
    799         [('a', 'e'), ('c', 'd'), ('b', 'f')]
     799        [('a', 'b'), ('f', 'e'), ('c', 'd')]
    800800        sage: all([PerfectMatchings(i).an_element() in PerfectMatchings(i)
    801801        ...        for i in range(2,11,2)])
    802802        True
     
    805805
    806806        sage: PerfectMatchings(5).list()
    807807        []
    808         sage: TestSuite(PerfectMatchings(5)).run()
     808        sage: TestSuite(PerfectMatchings(6)).run()
    809809        sage: TestSuite(PerfectMatchings([])).run()
    810810    """
    811811
     
    960960        else:
    961961            return Integer(prod(i for i in range(n) if i%2==1))
    962962
    963     def an_element(self):
     963    def random_element(self):
    964964        r"""
    965         Returns an element of self.
     965        Returns a random element of self.
    966966
    967967        EXAMPLES::
    968968
    969969            sage: M=PerfectMatchings(('a', 'e', 'b', 'f', 'c', 'd'))
    970970            sage: M.an_element()
    971             [('a', 'e'), ('c', 'd'), ('b', 'f')]
    972             sage: all([PerfectMatchings(i).an_element() in PerfectMatchings(i)
     971            [('a', 'b'), ('f', 'e'), ('c', 'd')]
     972            sage: all([PerfectMatchings(2*i).an_element() in PerfectMatchings(2*i)
    973973            ...        for i in range(2,11,2)])
    974974            True
     975
     976        TESTS::
     977
     978            sage: p = PerfectMatchings(13).random_element()
     979            Traceback (most recent call last):
     980            ...
     981            ValueError: There is no perfect matching on an odd number of elements
     982
    975983        """
    976         n=len(self._objects)//2
    977         return self([(self._objects[i],self._objects[i+n])
    978                                 for i in range(n)])
     984        n=len(self._objects)
     985
     986        if n%2==1:
     987            raise ValueError("There is no perfect matching on an odd number of elements")
     988
     989        k = n//2
     990
     991        from sage.combinat.permutation import Permutations
     992        p = Permutations(n).random_element()
     993
     994        return self([(self._objects[p[2*i]-1],self._objects[p[2*i+1]-1]) for i in range(k)])
     995
     996    an_element = random_element
    979997
    980998    @cached_method
    981999    def Weingarten_matrix(self,N):