Ticket #8420: trac_8420-perfect_matchings_review-fh.patch

File trac_8420-perfect_matchings_review-fh.patch, 25.3 KB (added by hivert, 12 years ago)

Review patch.

  • doc/en/reference/combinat/index.rst

    # HG changeset patch
    # User Florent Hivert <Florent.Hivert@univ-rouen.fr>
    # Date 1268045939 -3600
    # Node ID 8480ffdf71d8ba3443662985cea81c3af91de408
    # Parent  325fcf76b921d88667e6a342e189ddb659b7c6c4
    #8420 review of Valentin's matching.
    
    diff --git a/doc/en/reference/combinat/index.rst b/doc/en/reference/combinat/index.rst
    a b Combinatorics 
    2828   ../sage/combinat/non_decreasing_parking_function
    2929   ../sage/combinat/partition
    3030   ../sage/combinat/permutation
     31   ../sage/combinat/perfect_matching
    3132   ../sage/combinat/q_analogues
    3233   ../sage/combinat/set_partition_ordered
    3334   ../sage/combinat/set_partition
  • sage/combinat/perfect_matching.py

    diff --git a/sage/combinat/perfect_matching.py b/sage/combinat/perfect_matching.py
    a b  
     1"""
     2Perfect matchings
     3"""
     4
    15#from sage.combinat.permutation import Permutation_Class
    26from sage.structure.unique_representation import UniqueRepresentation
    37from sage.structure.parent import Parent
    from sage.structure.element_wrapper impo 
    711from sage.structure.element import Element
    812from sage.misc.cachefunc import cached_method
    913from sage.rings.integer import Integer
     14from sage.misc.flatten import flatten
     15from sage.combinat.permutation import Permutations, Permutation
     16from sage.sets.set import Set
     17from sage.combinat.partition import Partition
     18from sage.misc.misc_c import prod
     19from sage.matrix.constructor import Matrix
    1020
    1121class PerfectMatching(ElementWrapper):
    1222    r"""
    1323    Class of perfect matching.
    1424
    15     An instance of the class can be created from a list of pairs or from a fixed point-free involution as follows::
     25    An instance of the class can be created from a list of pairs or from a
     26    fixed point-free involution as follows::
    1627
    1728        sage: m=PerfectMatching([('a','e'),('b','c'),('d','f')]);m
    1829        PerfectMatching [('a', 'e'), ('b', 'c'), ('d', 'f')]
    class PerfectMatching(ElementWrapper): 
    2132        sage: isinstance(m,PerfectMatching)
    2233        True
    2334
    24     The parent, which is the set of perfect matching of the ground set, is automaticly created::
     35    The parent, which is the set of perfect matching of the ground set, is
     36    automaticly created::
    2537
    2638        sage: n.parent()
    2739        Set of perfect matchings of {1, 2, 3, 4, 5, 6, 7, 8}
    2840
    29     If the ground set is ordered, one can, for example, ask if the matching is non crossing::
     41    If the ground set is ordered, one can, for example, ask if the matching is
     42    non crossing::
    3043
    3144        sage: PerfectMatching([(1, 4), (2, 3), (5, 6)]).is_non_crossing()
    3245        True
    3346    """
    3447    #the data structure of the element is a list (accessible via x.value)
    35     wrapper_class = list
     48    wrapped_class = list
    3649    __lt__ = ElementWrapper._lt_by_value
    3750    #During the creation of the instance of the class, the function __classcall_private__ will be called instead of __init__ directly.
    3851    __metaclass__ = ClasscallMetaclass
    3952
    4053    @staticmethod
    4154    def __classcall_private__(cls,p):
    42         from sage.combinat.permutation import Permutation
    43         from sage.misc.flatten import flatten
    4455        r"""
    45         This function tries to recognize the input (it can be either a list or a tuple of pairs, or a fix-point free involution given as a list or as a permutation), constructs the parent (enumerated set of PerfectMatchings of the ground set) and calls the __init__ function to construct our object.
     56        This function tries to recognize the input (it can be either a list or
     57        a tuple of pairs, or a fix-point free involution given as a list or as
     58        a permutation), constructs the parent (enumerated set of
     59        PerfectMatchings of the ground set) and calls the __init__ function to
     60        construct our object.
    4661
    4762        EXAMPLES::
    4863
    class PerfectMatching(ElementWrapper): 
    5671            Set of perfect matchings of {1, 2, 3, 4, 5, 6, 7, 8}
    5772            sage: PerfectMatching([(1, 4), (2, 3), (5, 6)]).is_non_crossing()
    5873            True
    59 
    60         TESTS::
    61 
    62             sage: TestSuite(m).run()
    6374        """
    64         #we have to extract from the argument p the set of objects of the matching and the list of pairs
    65         #First case: p is a list (resp tuple) of lists (resp tuple).
     75        # we have to extract from the argument p the set of objects of the
     76        # matching and the list of pairs.
     77        # First case: p is a list (resp tuple) of lists (resp tuple).
    6678        if (isinstance(p,list) or isinstance(p,tuple)) and (
    6779                all([isinstance(x,list) or isinstance(x,tuple) for x in p])):
    6880            objects=flatten(p)
    6981            data=(map(tuple,p))
    70         #Second case: p is a permutation or a list of integers, we have to check if it is a fix-point-free involution.
     82        # Second case: p is a permutation or a list of integers, we have to
     83        # check if it is a fix-point-free involution.
    7184        elif ((isinstance(p,list) and all(map(lambda x: (isinstance(x,Integer) or isinstance(x,int)),p )))
    7285                or isinstance(p,sage.combinat.permutation.Permutation_class)):
    7386            p=Permutation(p)
    class PerfectMatching(ElementWrapper): 
    7689                raise ValueError, "The permutation p (= %s) is not a fix-point-free involution"%p
    7790            objects=range(1,n+1)
    7891            data=p.to_cycles()
    79         #Third case: p is already a perfect matching
     92        # Third case: p is already a perfect matching
    8093        elif isinstance(p,PerfectMatching):
    8194            data=p.value
    8295            objects=flatten(data)
    8396        else:
    8497            raise ValueError, "cannot convert p (= %s) to a PerfectMatching"%p
    85         #Finally, we create the parent and the element using the element class of the parent. Note: as this function is private, when we create an object via parent.element_class(...), __init__ is directly executed and we do not have an infinite loop.
     98        # Finally, we create the parent and the element using the element
     99        # class of the parent. Note: as this function is private, when we
     100        # create an object via parent.element_class(...), __init__ is directly
     101        # executed and we do not have an infinite loop.
    86102        parent=PerfectMatchings(objects)
    87         return parent.element_class(data,parent)     
     103        return parent.element_class(data,parent)
    88104
    89105    def __init__(self,data,parent):
    90106        r"""
    91         see __classcall_private__?
     107        See :meth:`__classcall_private__`
     108
     109        TESTS::
     110
     111            sage: m=PerfectMatching([('a','e'),('b','c'),('d','f')])
     112            sage: TestSuite(m).run()
    92113        """
    93114        self.value=data
    94115        Element.__init__(self,parent=parent)
    class PerfectMatching(ElementWrapper): 
    107128        return 'PerfectMatching %s'%self.value
    108129
    109130    def __eq__(self,other):
    110         from sage.sets.set import Set
    111131        r"""
    112132        Compares two perfect matchings
    113133
    class PerfectMatching(ElementWrapper): 
    122142
    123143        """
    124144        try:
    125             if other.parent() <> self.parent():
     145            if other.parent() != self.parent():
    126146                return False
    127147        except AttributeError:
    128148            return False
    class PerfectMatching(ElementWrapper): 
    130150
    131151    def size(self):
    132152        r"""
    133         returns the size of the perfect matching, i.e. the number of elements in the ground set.
    134        
     153
     154        Returns the size of the perfect matching ``self``, i.e. the number of
     155        elements in the ground set.
     156
    135157        EXAMPLES::
    136            
     158
    137159            sage: m=PerfectMatching([(-3, 1), (2, 4), (-2, 7)]); m.size()
    138160            6
    139161        """
    140162        return 2*len(self.value)
    141163
    142     def partner(self,x):
     164    def partner(self, x):
    143165        r"""
    144         Returns the element in the same pair than x in the mathching `self`.
     166        Returns the element in the same pair than ``x`` in the matching ``self``.
    145167
    146168        EXAMPLES::
    147            
     169
    148170            sage: m=PerfectMatching([(-3, 1), (2, 4), (-2, 7)]); m.partner(4)
    149171            2
    150172            sage: n=PerfectMatching([('c','b'),('d','f'),('e','a')]); n.partner('c')
    class PerfectMatching(ElementWrapper): 
    157179                return self.value[i][0]
    158180        raise ValueError,"%s in not an element of the %s"%(x,self)
    159181
    160     def loop_type(self,other=None):
    161         from sage.combinat.permutation import Permutation
    162         from sage.combinat.partition import Partition
     182    def loop_type(self, other=None):
    163183        r"""
    164         input:
     184        INPUT:
    165185
    166             two perfect matchings of the same set (if the second argument is empty, the fonction an_element is called on the parent of the first)
     186             - ``other`` -- a perfet matching of the same set of ``self``.
     187             (if the second argument is empty, the method :meth:`an_element` is
     188             called on the parent of the first)
    167189
    168         output:
     190        OUTPUT:
    169191
    170             if we draw the two perfect matchings simultaneously as edges of a graph, the graph obtained is a union of cycles of even lengths. The function returns the ordered list of the semi-length of these cycles (considered as a partition)
     192            If we draw the two perfect matchings simultaneously as edges of a
     193            graph, the graph obtained is a union of cycles of even
     194            lengths. The function returns the ordered list of the semi-length
     195            of these cycles (considered as a partition)
    171196
    172197        EXAMPLES::
    173198
    class PerfectMatching(ElementWrapper): 
    196221
    197222    def number_of_loops(self,other=None):
    198223        r"""
    199         input:
     224        INPUT:
    200225
    201             two perfect matchings of the same set (if the second argument is empty, the fonction an_element is called on the parent of the first)
     226            - ``other`` -- a perfet matching of the same set of ``self``.
     227            (if the second argument is empty, the fonction an_element is
     228            called on the parent of the first)
    202229
    203         output:
     230        OUTPUT:
    204231
    205             if we draw the two perfect matchings simultaneously as edges of a graph, the graph obtained is a union of cycles of even lengths. The function returns their numbers
     232            If we draw the two perfect matchings simultaneously as edges of a
     233            graph, the graph obtained is a union of cycles of even
     234            lengths. The function returns their numbers.
    206235
    207236        EXAMPLES::
    208237
    class PerfectMatching(ElementWrapper): 
    213242        """
    214243        return len(self.loop_type(other))
    215244
    216     def crossings(self):
     245    def crossings_iterator(self):
    217246        r"""
    218         input:
     247        INPUT:
    219248
    220249            A perfect matching on an *totally ordered* ground set.
    221250
    222         output:
     251        OUTPUT:
    223252
    224             We place the element of a ground set and draw the perfect matching by linking the elements of the same pair in the upper half-plane. This function returns the list of the pairs of crossing lines (as a line correspond to a pair, it returns a list of pairs of pairs).
     253            We place the element of a ground set and draw the perfect matching
     254            by linking the elements of the same pair in the upper
     255            half-plane. This function returns an iterator over the pairs of
     256            crossing lines (as a line correspond to a pair, it returns a list
     257            of pairs of pairs).
     258
     259        EXAMPLES::
     260
     261            sage: n=PerfectMatching([3,8,1,7,6,5,4,2]); n
     262            PerfectMatching [(1, 3), (2, 8), (4, 7), (5, 6)]
     263            sage: it = n.crossings_iterator();
     264            sage: it.next()
     265            ((1, 3), (2, 8))
     266            sage: it.next()
     267            Traceback (most recent call last):
     268            ...
     269            StopIteration
     270        """
     271        x=self.value[:]
     272        if len(x)==0:
     273            return
     274        (i,j)=x.pop(0)
     275        for cr in PerfectMatching(x).crossings_iterator():
     276            yield cr
     277        for (a,b) in x:
     278            # if (i<a<j<b) or (i<b<j<a) or (j<a<i<b) or (j<b<i<a) or (
     279            #        a<i<b<j) or (a<j<b<i) or (b<i<a<j) or (b<j<a<i):
     280            labij = sorted([a,b,i,j])
     281            posij = sorted([labij.index(i), labij.index(j)])
     282            if posij == [0,2] or posij == [1,3]:
     283                yield ((i,j),(a,b))
     284
     285    def crossings(self):
     286        r"""
     287        INPUT:
     288
     289            A perfect matching on an *totally ordered* ground set.
     290
     291        OUTPUT:
     292
     293            We place the element of a ground set and draw the perfect matching
     294            by linking the elements of the same pair in the upper
     295            half-plane. This function returns the list of the pairs of
     296            crossing lines (as a line correspond to a pair, it returns a list
     297            of pairs of pairs).
    225298
    226299        EXAMPLES::
    227300
    class PerfectMatching(ElementWrapper): 
    230303            sage: n.crossings()
    231304            [((1, 3), (2, 8))]
    232305        """
    233         x=self.value[:]
    234         if len(x)==0:
    235             return []
    236         (i,j)=x.pop(0)
    237         res=PerfectMatching(x).crossings()
    238         for (a,b) in x:
    239             if (i<a<j<b) or (i<b<j<a) or (j<a<i<b) or (j<b<i<a) or (
    240                     a<i<b<j) or (a<j<b<i) or (b<i<a<j) or (b<j<a<i):
    241                 res.append(((i,j),(a,b)))
    242         return res
     306        return list(self.crossings_iterator())
    243307
    244308    def number_of_crossings(self):
    245309        r"""
    246         input:
     310        INPUT:
    247311
    248312            A perfect matching on an *totally ordered* ground set.
    249313
    250         output:
     314        OUTPUT:
    251315
    252             We place the element of a ground set and draw the perfect matching by linking the elements of the same pair in the upper half-plane. This function returns the number the pairs of crossing lines.
     316            We place the element of a ground set and draw the perfect matching
     317            by linking the elements of the same pair in the upper
     318            half-plane. This function returns the number the pairs of crossing
     319            lines.
    253320
    254321        EXAMPLES::
    255322
    class PerfectMatching(ElementWrapper): 
    258325            sage: n.number_of_crossings()
    259326            1
    260327        """
    261         x=self.value[:]
    262         if len(x)==0:
    263             return 0
    264         (i,j)=x.pop(0)
    265         res=PerfectMatching(x).number_of_crossings()
    266         for (a,b) in x:
    267             if (i<a<j<b) or (i<b<j<a) or (j<a<i<b) or (j<b<i<a) or (
    268                     a<i<b<j) or (a<j<b<i) or (b<i<a<j) or (b<j<a<i):
    269                 res+=1
    270         return res
     328        c = Integer(0)
     329        one = Integer(1)
     330        for _ in self.crossings_iterator():
     331            c += one
     332        return c
    271333
    272334    def is_non_crossing(self):
    273335        r"""
    274         input:
     336        INPUT:
    275337
    276338            A perfect matching on an *totally ordered* ground set.
    277339
    278         output:
     340        OUTPUT:
    279341
    280             We place the element of a ground set and draw the perfect matching by linking the elements of the same pair in the upper half-plane. This function returns True if the picture obtained this way has no crossings.
     342            We place the element of a ground set and draw the perfect matching
     343            by linking the elements of the same pair in the upper
     344            half-plane. This function returns ``True`` if the picture obtained
     345            this way has no crossings.
    281346
    282347        EXAMPLES::
    283348
    class PerfectMatching(ElementWrapper): 
    288353            sage: PerfectMatching([(1, 4), (2, 3), (5, 6)]).is_non_crossing()
    289354            True
    290355        """
    291         x=self.value[:]
    292         if len(x)==0:
     356        it = self.crossings_iterator()
     357        try:
     358            it.next()
     359        except StopIteration:
    293360            return True
    294         (i,j)=x.pop(0)
    295         for (a,b) in x:
    296             if (i<a<j<b) or (i<b<j<a) or (j<a<i<b) or (j<b<i<a) or (
    297                     a<i<b<j) or (a<j<b<i) or (b<i<a<j) or (b<j<a<i):
    298                 return False
    299         return PerfectMatching(x).is_non_crossing()
     361        else:
     362            return False
    300363
    301364    def Weingarten_function(self,other,N):
    302365        r"""
    303         Returns the Weingarten function of two pairings. This function is the value of some integrals over the orhtogonal groups O_N.
     366        Returns the Weingarten function of two pairings. This function is
     367        the value of some integrals over the orhtogonal groups `O_N`.
    304368
    305369        EXAMPLES::
    306370
    307         sage: var('N')
    308         N
    309         sage: m=PerfectMatching([(1,3),(2,4)])
    310         sage: n=PerfectMatching([(1,2),(3,4)])
    311         sage: factor(m.Weingarten_function(n,N))
    312         -1/((N - 1)*(N + 2)*N)
     371            sage: var('N')
     372            N
     373            sage: m=PerfectMatching([(1,3),(2,4)])
     374            sage: n=PerfectMatching([(1,2),(3,4)])
     375            sage: factor(m.Weingarten_function(n,N))
     376            -1/((N - 1)*(N + 2)*N)
    313377        """
    314378        W=self.parent().Weingarten_matrix(N)
    315379        return W[self.rank()][other.rank()]
    316380
    317381class PerfectMatchings(UniqueRepresentation,Parent):
    318382    r"""
    319     Class of perfect matchings of a ground set. At the creation, the set can be given as any iterable object. If the argument is an integer n, it will be transformed into [1 .. n]::
     383    Class of perfect matchings of a ground set. At the creation, the set
     384    can be given as any iterable object. If the argument is an integer `n`, it
     385    will be transformed into `[1 .. n]`::
    320386
    321387        sage: M=PerfectMatchings(6);M
    322388        Set of perfect matchings of {1, 2, 3, 4, 5, 6}
    323389        sage: PerfectMatchings([-1, -3, 1, 2])
    324390        Set of perfect matchings of {1, 2, -3, -1}
    325391
    326     One can ask for the list, the cardinality or an element of a set of perfect matching::
     392    One can ask for the list, the cardinality or an element of a set of
     393    perfect matching::
    327394
    328395        sage: PerfectMatchings(4).list()
    329396        [PerfectMatching [(4, 1), (3, 2)], PerfectMatching [(4, 2), (3, 1)], PerfectMatching [(4, 3), (2, 1)]]
    class PerfectMatchings(UniqueRepresentat 
    338405
    339406    @staticmethod
    340407    def _parse_input(objects):
    341         from sage.sets.set import Set
    342408        r"""
    343         This function tries to recognize the argument and to transform into a set. It is not meant to be called manually, but only as the first of the creation of an enumerated set of PerfectMatchings.
    344        
     409        This function tries to recognize the argument and to transform into a
     410        set. It is not meant to be called manually, but only as the first of
     411        the creation of an enumerated set of ``PerfectMatchings``.
     412
    345413        EXAMPLES::
    346414
    347415            sage: PerfectMatchings._parse_input(4)
    class PerfectMatchings(UniqueRepresentat 
    349417            sage: PerfectMatchings._parse_input(['a','b','c','e'])
    350418            {'a', 'c', 'b', 'e'}
    351419        """
    352         #if the argument is a python integer n, we replace it by the list [1 .. n]
     420        # if the argument is a python integer n, we replace it by the list [1 .. n]
    353421        if isinstance(objects,int):
    354422            objects=range(1,objects+1)
    355         #same thing if the argument is a sage integer.
     423        # same thing if the argument is a sage integer.
    356424        elif isinstance(objects,Integer):
    357425            objects=range(1,objects+1)
    358         #Finally, if it is iterable, we return the corresponding set. Note that it is important to return a hashable object here (in particular, NOT A LIST), see comment below.
     426        # Finally, if it is iterable, we return the corresponding set.
     427        # Note that it is important to return a hashable object here (in
     428        # particular, NOT A LIST), see comment below.
    359429        if not hasattr(objects,'__iter__'):
    360430            raise ValueError, "do not know how to construct a set of matchings from %s (it must be iterable)"
    361431        return Set(objects)
    class PerfectMatchings(UniqueRepresentat 
    363433    @staticmethod
    364434    def __classcall__(cls, objects):
    365435        r"""
    366         This function is called automatically when the user want to create an enumerated set of PerfectMatchings.
     436        This function is called automatically when the user want to
     437        create an enumerated set of PerfectMatchings.
    367438
    368439        EXAMPLES::
    369440
    class PerfectMatchings(UniqueRepresentat 
    372443            sage: PerfectMatchings([-1, -3, 1, 2])
    373444            Set of perfect matchings of {1, 2, -3, -1}
    374445
    375         If one has already created a set of perfect matchings of the same set, it does not create a new object, but returns the already existing one::
     446        If one has already created a set of perfect matchings of the same set,
     447        it does not create a new object, but returns the already existing
     448        one::
    376449
    377450            sage: N=PerfectMatchings((2, 3, 5, 4, 1, 6))
    378451            sage: N is M
    379452            True
    380453
    381         The class constructor does not check that the perfect matching is correct, one has to use the function __contains__ for that::
     454        The class constructor does not check that the perfect matching is
     455        correct, one has to use the method :meth:`__contains__` for that::
    382456
    383457            sage: m=PerfectMatching([(1,2,3),(4,5)])
    384458            sage: isinstance(m,PerfectMatching)
    385459            True
    386460            sage: m in m.parent()
    387461            False
    388 
    389         TEST::
    390 
    391             sage: TestSuite(M).run()
    392462        """
    393463        #we call the constructor of an other class, which will
    394         #    - check if the object has already been constructed (so the second argument, i.e. the output of _parse_input, must be hashable)
     464        #    - check if the object has already been constructed (so the
     465        # second argument, i.e. the output of _parse_input, must be hashable)
    395466        #    - look for a place in memory and call the __init__ function
    396467        return super(PerfectMatchings, cls).__classcall__(
    397468                         cls, cls._parse_input(objects))
    398469
    399470    def __init__(self,objects):
    400471        r"""
    401         See __classcall__?
     472        See :meth:`__classcall__`
     473
     474        TEST::
     475
     476            sage: M=PerfectMatchings(6)
     477            sage: TestSuite(M).run()
    402478        """
    403479        self._objects=objects
    404480        Parent.__init__(self, category = FiniteEnumeratedSets())
    405481
    406482    def _repr_(self):
    407483        r"""
    408         Returns a description of `self`
     484        Returns a description of ``self``.
    409485
    410486        EXAMPLES::
    411        
     487
    412488            sage: PerfectMatchings([-1, -3, 1, 2])
    413489            Set of perfect matchings of {1, 2, -3, -1}
    414490        """
    class PerfectMatchings(UniqueRepresentat 
    416492
    417493    def __iter__(self):
    418494        r"""
    419         Returns an iterator for the elements of self
     495        Returns an iterator for the elements of ``self``.
    420496
    421497        EXAMPLES::
    422498
    class PerfectMatchings(UniqueRepresentat 
    427503            yield self([])
    428504        elif len(self._objects) == 1:
    429505            pass
    430         else: 
     506        else:
    431507            l=list(self._objects)
    432508            a=l.pop(-1)
    433509            for i in range(len(l)):
    class PerfectMatchings(UniqueRepresentat 
    438514
    439515
    440516    def __contains__(self,x):
    441         from sage.misc.flatten import flatten
    442         from sage.combinat.permutation import Permutations
    443517        r"""
    444         Tests if x is an element of self.
     518        Tests if ``x`` is an element of ``self``.
    445519
    446520        EXAMPLES::
    447521
    class PerfectMatchings(UniqueRepresentat 
    453527            sage: all([m in PerfectMatchings(6) for m in PerfectMatchings(6)])
    454528            True
    455529
    456         Note that the class of x does not need to be PerfectMatching: if the data defines a perfect matching of the good set, the function returns True::
     530        Note that the class of ``x`` does not need to be ``PerfectMatching``:
     531        if the data defines a perfect matching of the good set, the function
     532        returns ``True``::
    457533
    458534            sage: [(1, 4), (2, 3)] in PerfectMatchings(4)
    459535            True
    460536
    461         The class constructor does not check that the perfect matching is correct, one has to use the function __contains__ for that::
     537        The class constructor does not check that the perfect matching is
     538        correct, one has to use the function :meth:`__contains__` for that::
    462539
    463540            sage: m=PerfectMatching([(1,2,3),(4,5)])
    464541            sage: isinstance(m,PerfectMatching)
    class PerfectMatchings(UniqueRepresentat 
    479556        if map(lambda a:A2N[a],flatten(x.value)) not in Permutations(x.size()):
    480557            return False
    481558        return True
    482  
     559
    483560    def cardinality(self):
    484         from sage.misc.misc_c import prod
    485561        r"""
    486         Returns the cardinality of the set of perfect matching `self`, that is 1*3*5*...*(2n-1), where 2n is the size of the ground set.
     562        Returns the cardinality of the set of perfect matching ``self``,
     563        that is `1*3*5*...*(2n-1)`, where `2n` is the size of the ground set.
    487564
    488565        EXAMPLES::
    489566
    class PerfectMatchings(UniqueRepresentat 
    509586            True
    510587        """
    511588        n=len(self._objects)//2
    512         return PerfectMatching([(self._objects[i],self._objects[i+n]) for i in range(n)])
     589        return PerfectMatching([(self._objects[i],self._objects[i+n])
     590                                for i in range(n)])
    513591
    514592    @cached_method
    515593    def Weingarten_matrix(self,N):
    516         from sage.matrix.constructor import Matrix
    517594        r"""
    518         Returns the Weingarten matrix corresponding to the set of PerfectMatchings `self`. It is a useful theoretical tool to compute polynomial integral over the orthogonal group O_N.
     595        Returns the Weingarten matrix corresponding to the set of
     596        PerfectMatchings ``self``. It is a useful theoretical tool to compute
     597        polynomial integral over the orthogonal group `O_N`.
    519598
    520599        EXAMPLES::
    521600