ó
¡¼™\c           @   s…   d  d l  m Z d  d l m Z d  d l m Z e j Z d „  Z e d „ Z	 d „  Z
 d d „ Z d d „ Z d	 „  Z d
 „  Z d S(   iÿÿÿÿ(   t   Permutation(   t   _distribute_gens_by_base(   t   rangec         C   s   d „  |  Dƒ d „  | Dƒ k S(   sN  
    Compare two lists of permutations as sets.

    This is used for testing purposes. Since the array form of a
    permutation is currently a list, Permutation is not hashable
    and cannot be put into a set.

    Examples
    ========

    >>> from sympy.combinatorics.permutations import Permutation
    >>> from sympy.combinatorics.testutil import _cmp_perm_lists
    >>> a = Permutation([0, 2, 3, 4, 1])
    >>> b = Permutation([1, 2, 0, 4, 3])
    >>> c = Permutation([3, 4, 0, 1, 2])
    >>> ls1 = [a, b, c]
    >>> ls2 = [b, c, a]
    >>> _cmp_perm_lists(ls1, ls2)
    True

    c         S   s   h  |  ] } t  | ƒ ’ q S(    (   t   tuple(   t   .0t   a(    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pys	   <setcomp>   s   	 c         S   s   h  |  ] } t  | ƒ ’ q S(    (   R   (   R   R   (    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pys	   <setcomp>   s   	 (    (   t   firstt   second(    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyt   _cmp_perm_lists   s    c   	         s?  d d l  m } d d l m ‰  t | d ƒ rî t |  j d t ƒ ƒ } g  | j D] } | j	 ^ qQ ‰ ‡  ‡ f d †  } g  } | s½ xc | D]+ } | | ƒ r‹ | j
 t j | ƒ ƒ q‹ q‹ Wn- x* | D]" } | | ƒ rÄ | j
 | ƒ qÄ qÄ W| St | d ƒ rt |  | | ƒ | ƒ St | d ƒ r;t |  | | g ƒ | ƒ Sd  S(	   Niÿÿÿÿ(   t   PermutationGroup(   t   _af_commutes_witht
   generatorst   afc            s   t  ‡ ‡  f d †  ˆ Dƒ ƒ S(   Nc         3   s   |  ] } ˆ  ˆ | ƒ Vq d  S(   N(    (   R   t   gen(   R
   t   x(    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pys	   <genexpr>>   s    (   t   all(   R   (   R
   t   gens(   R   s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyt   <lambda>>   t    t   getitemt
   array_form(   t   sympy.combinatorics.perm_groupsR	   t    sympy.combinatorics.permutationsR
   t   hasattrt   listt   generate_diminot   TrueR   t   _array_formt   appendR    t   _af_newt   _naive_list_centralizer(	   t   selft   otherR   R	   t   elementsR   t   commutes_with_genst   centralizer_listt   element(    (   R
   R   s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyR   "   s&     c         C   s›   d d l  m } t | | ƒ } |  } xY t t | ƒ ƒ D]E } | | | ƒ } | j ƒ  | j ƒ  k rj t S| j | | ƒ } q8 W| j ƒ  d k r— t St S(   s®  
    Verify the correctness of a base and strong generating set.

    This is a naive implementation using the definition of a base and a strong
    generating set relative to it. There are other procedures for
    verifying a base and strong generating set, but this one will
    serve for more robust testing.

    Examples
    ========

    >>> from sympy.combinatorics.named_groups import AlternatingGroup
    >>> from sympy.combinatorics.testutil import _verify_bsgs
    >>> A = AlternatingGroup(4)
    >>> A.schreier_sims()
    >>> _verify_bsgs(A, A.base, A.strong_gens)
    True

    See Also
    ========

    sympy.combinatorics.perm_groups.PermutationGroup.schreier_sims

    iÿÿÿÿ(   R	   i   (	   R   R	   R   R   t   lent   ordert   Falset
   stabilizerR   (   t   groupt   baseR   R	   t   strong_gens_distrt   current_stabilizert   it	   candidate(    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyt   _verify_bsgsO   s    c         C   sX   | d k r |  j | ƒ } n  t | j d t ƒ ƒ } t |  | d t ƒ} t | | ƒ S(   s3  
    Verify the centralizer of a group/set/element inside another group.

    This is used for testing ``.centralizer()`` from
    ``sympy.combinatorics.perm_groups``

    Examples
    ========

    >>> from sympy.combinatorics.named_groups import (SymmetricGroup,
    ... AlternatingGroup)
    >>> from sympy.combinatorics.perm_groups import PermutationGroup
    >>> from sympy.combinatorics.permutations import Permutation
    >>> from sympy.combinatorics.testutil import _verify_centralizer
    >>> S = SymmetricGroup(5)
    >>> A = AlternatingGroup(5)
    >>> centr = PermutationGroup([Permutation([0, 1, 2, 3, 4])])
    >>> _verify_centralizer(S, A, centr)
    True

    See Also
    ========

    _naive_list_centralizer,
    sympy.combinatorics.perm_groups.PermutationGroup.centralizer,
    _cmp_perm_lists

    R   N(   t   Nonet   centralizerR   R   R   R   R   (   R)   t   argt   centrt
   centr_listt   centr_list_naive(    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyt   _verify_centralizeru   s
    c   	      C   sÝ   d d l  m } | d  k r. |  j | ƒ } n  t ƒ  } t | d ƒ rR | j } n3 t | d ƒ rj | } n t | d ƒ r… | g } n  x6 |  j ƒ  D]( } x | D] } | j | | Aƒ qŸ Wq’ W| t	 | ƒ ƒ } | j
 | ƒ S(   Niÿÿÿÿ(   R	   R   t   __getitem__R   (   R   R	   R0   t   normal_closuret   setR   R   R   t   addR   t   is_subgroup(	   R)   R2   t   closureR	   t
   conjugatest
   subgr_genst   elR   t   naive_closure(    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyt   _verify_normal_closure™   s    		c         G   sf  d d l  m } d d l m } m } d d l m } m } g  }	 xP t t	 | ƒ ƒ D]< }
 | |
 \ } } } } |	 j
 | | g  g | | f ƒ qU W| |	 Œ  \ } } } | | | | d ƒ } t | t ƒ rê d } | g } | g } n t	 | ƒ } g  } x9 t | ƒ D]+ }
 | j | | |
 | |
 | d ƒ ƒ q	W| | ƒ } | g  | D] } | | ƒ ^ qNƒ } t | j d t ƒ ƒ } |  j }  t ƒ  } x\ | j d t ƒ D]H } | |  | ƒ } x0 | D]( } t | | | ƒ ƒ } | j | ƒ qÂWq¦Wt | ƒ } | j ƒ  d
 | } xC | D]; } | d	  | d	  k rN| d | d k rNd Sn  | } qWt | d ƒ S(   s  
    Canonicalize tensor formed by tensors of the different types

    g  permutation representing the tensor
    dummies  list of dummy indices
    msym symmetry of the metric

    v is a list of (base_i, gens_i, n_i, sym_i) for tensors of type `i`
    base_i, gens_i BSGS for tensors of this type
    n_i  number ot tensors of type `i`

    sym_i symmetry under exchange of two component tensors of type `i`
          None  no symmetry
          0     commuting
          1     anticommuting

    Return 0 if the tensor is zero, else return the array form of
    the permutation representing the canonical form of the tensor.

    Examples
    ========

    >>> from sympy.combinatorics.testutil import canonicalize_naive
    >>> from sympy.combinatorics.tensor_can import get_symmetric_group_sgs
    >>> from sympy.combinatorics import Permutation, PermutationGroup
    >>> g = Permutation([1, 3, 2, 0, 4, 5])
    >>> base2, gens2 = get_symmetric_group_sgs(2)
    >>> canonicalize_naive(g, [2, 3], 0, (base2, gens2, 2, 0))
    [0, 2, 1, 3, 4, 5]
    iÿÿÿÿ(   R	   (   t   gens_productst	   dummy_sgs(   R    t   _af_rmuli   i   R   i    iþÿÿÿ(   i    (   R   R	   t   sympy.combinatorics.tensor_canRB   RC   R   R    RD   R   R%   R   t
   isinstancet   intt   extendR   t   generateR   R   R9   R   R:   t   sort(   t   gt   dummiest   symt   vR	   RB   RC   R    RD   t   v1R-   t   base_it   gens_it   n_it   sym_it   sizet   sbaset   sgenst   dgenst	   num_typest   SR   t   Dt   dlistt   stt   st   ht   dt   qR   t   prev(    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyt   canonicalize_naiveÂ   sH    $	)%		


c         C   sŸ  d d l  m } d d l m } m } t |  j ƒ  ƒ } | j d d „  d t ƒ g  | D] } | d ^ qX } | | ƒ } d } x$ | D] \ } }	 | t	 |	 ƒ 7} q‡ Wg  | D] }
 g  ^ q® } d }
 xt | D]l \ } }	 x] |	 D]U } | | | | k  rà | | | j
 |
 ƒ | | | j
 |
 d ƒ |
 d	 7}
 qà qà WqÍ Wg  } x | D] } | j | ƒ qJWt	 | ƒ | k syt ‚ | | | d g 7} | d	 } t | ƒ t t | ƒ ƒ k s»t ‚ t | ƒ } d g t	 | d ƒ d } x$ | D] }	 | t	 |	 ƒ c d 7<qéWg  } xX t t	 | ƒ ƒ D]D }
 | |
 } | r"| |
 ƒ \ } } | j
 | | | d f ƒ q"q"W| j ƒ  t t | ƒ ƒ } | | | d | Œ } | S(
   sÑ  
    Return a certificate for the graph

    gr adjacency list

    The graph is assumed to be unoriented and without
    external lines.

    Associate to each vertex of the graph a symmetric tensor with
    number of indices equal to the degree of the vertex; indices
    are contracted when they correspond to the same line of the graph.
    The canonical form of the tensor gives a certificate for the graph.

    This is not an efficient algorithm to get the certificate of a graph.

    Examples
    ========

    >>> from sympy.combinatorics.testutil import graph_certificate
    >>> gr1 = {0:[1, 2, 3, 5], 1:[0, 2, 4], 2:[0, 1, 3, 4], 3:[0, 2, 4], 4:[1, 2, 3, 5], 5:[0, 4]}
    >>> gr2 = {0:[1, 5], 1:[0, 2, 3, 4], 2:[1, 3, 5], 3:[1, 2, 4, 5], 4:[1, 3, 5], 5:[0, 2, 3, 4]}
    >>> c1 = graph_certificate(gr1)
    >>> c2 = graph_certificate(gr2)
    >>> c1
    [0, 2, 4, 6, 1, 8, 10, 12, 3, 14, 16, 18, 5, 9, 15, 7, 11, 17, 13, 19, 20, 21]
    >>> c1 == c2
    True
    iÿÿÿÿ(   t
   _af_invert(   t   get_symmetric_group_sgst   canonicalizet   keyc         S   s   t  |  d ƒ S(   Ni   (   R%   (   R   (    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyR   (  R   t   reversei    i   i   (   R   Rc   RE   Rd   Re   R   t   itemsRJ   R   R%   R   RH   t   AssertionErrort   sortedR   R    Rg   (   t   grRc   Rd   Re   Rh   R   t   pvertt   num_indicesRN   t   neighR-   t   verticest   v2RK   RT   t   vlent   nR*   R   RL   t   can(    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyt   graph_certificate  sL    
$
 
N(   t   sympy.combinatoricsR    t   sympy.combinatorics.utilR   t   sympy.core.compatibilityR   t   rmulR   R'   R   R/   R0   R6   RA   Rb   Rt   (    (    (    s;   lib/python2.7/site-packages/sympy/combinatorics/testutil.pyt   <module>   s   		-	&$)	F