
    wgz                         d dl mZmZmZmZmZmZ d dlmZ d dl	m
Z
mZmZ d dlmZ  G d de
      Z G d de      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )SBasicDictSymbolTuplesympify)Str)Set	FiniteSetEmptySet)iterablec                       e Zd ZdZdZy)Classa,  
    The base class for any kind of class in the set-theoretic sense.

    Explanation
    ===========

    In axiomatic set theories, everything is a class.  A class which
    can be a member of another class is a set.  A class which is not a
    member of another class is a proper class.  The class `\{1, 2\}`
    is a set; the class of all sets is a proper class.

    This class is essentially a synonym for :class:`sympy.core.Set`.
    The goal of this class is to assure easier migration to the
    eventual proper implementation of set theory.
    FN)__name__
__module____qualname____doc__	is_proper     a/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/categories/baseclasses.pyr   r      s     Ir   r   c                       e Zd ZdZy)Objecta  
    The base class for any kind of object in an abstract category.

    Explanation
    ===========

    While technically any instance of :class:`~.Basic` will do, this
    class is the recommended way to create abstract objects in
    abstract categories.
    N)r   r   r   r   r   r   r   r   r      s    	r   r   c                   B    e Zd ZdZd Zed        Zed        Zd Zd Z	y)Morphisma  
    The base class for any morphism in an abstract category.

    Explanation
    ===========

    In abstract categories, a morphism is an arrow between two
    category objects.  The object where the arrow starts is called the
    domain, while the object where the arrow ends is called the
    codomain.

    Two morphisms between the same pair of objects are considered to
    be the same morphisms.  To distinguish between morphisms between
    the same objects use :class:`NamedMorphism`.

    It is prohibited to instantiate this class.  Use one of the
    derived classes instead.

    See Also
    ========

    IdentityMorphism, NamedMorphism, CompositeMorphism
    c                     t        d      )Nz:Cannot instantiate Morphism.  Use derived classes instead.NotImplementedError)clsdomaincodomains      r   __new__zMorphism.__new__?   s    !HJ 	Kr   c                      | j                   d   S )a#  
        Returns the domain of the morphism.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> A = Object("A")
        >>> B = Object("B")
        >>> f = NamedMorphism(A, B, "f")
        >>> f.domain
        Object("A")

        r   argsselfs    r   r    zMorphism.domainC         yy|r   c                      | j                   d   S )a'  
        Returns the codomain of the morphism.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> A = Object("A")
        >>> B = Object("B")
        >>> f = NamedMorphism(A, B, "f")
        >>> f.codomain
        Object("B")

           r$   r&   s    r   r!   zMorphism.codomainU   r(   r   c                     t        ||       S )a  
        Composes self with the supplied morphism.

        The order of elements in the composition is the usual order,
        i.e., to construct `g\circ f` use ``g.compose(f)``.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> g * f
        CompositeMorphism((NamedMorphism(Object("A"), Object("B"), "f"),
        NamedMorphism(Object("B"), Object("C"), "g")))
        >>> (g * f).domain
        Object("A")
        >>> (g * f).codomain
        Object("C")

        )CompositeMorphismr'   others     r   composezMorphism.composeg   s    2 !--r   c                 $    | j                  |      S )a  
        Composes self with the supplied morphism.

        The semantics of this operation is given by the following
        equation: ``g * f == g.compose(f)`` for composable morphisms
        ``g`` and ``f``.

        See Also
        ========

        compose
        )r/   r-   s     r   __mul__zMorphism.__mul__   s     ||E""r   N)
r   r   r   r   r"   propertyr    r!   r/   r1   r   r   r   r   r   '   s?    .K  "  ".6#r   r   c                   &    e Zd ZdZd Zed        Zy)IdentityMorphisma9  
    Represents an identity morphism.

    Explanation
    ===========

    An identity morphism is a morphism with equal domain and codomain,
    which acts as an identity with respect to composition.

    Examples
    ========

    >>> from sympy.categories import Object, NamedMorphism, IdentityMorphism
    >>> A = Object("A")
    >>> B = Object("B")
    >>> f = NamedMorphism(A, B, "f")
    >>> id_A = IdentityMorphism(A)
    >>> id_B = IdentityMorphism(B)
    >>> f * id_A == f
    True
    >>> id_B * f == f
    True

    See Also
    ========

    Morphism
    c                 .    t        j                  | |      S N)r   r"   )r   r    s     r   r"   zIdentityMorphism.__new__   s    }}S&))r   c                     | j                   S r6   )r    r&   s    r   r!   zIdentityMorphism.codomain   s    {{r   N)r   r   r   r   r"   r2   r!   r   r   r   r4   r4      s     8*  r   r4   c                   &    e Zd ZdZd Zed        Zy)NamedMorphisma2  
    Represents a morphism which has a name.

    Explanation
    ===========

    Names are used to distinguish between morphisms which have the
    same domain and codomain: two named morphisms are equal if they
    have the same domains, codomains, and names.

    Examples
    ========

    >>> from sympy.categories import Object, NamedMorphism
    >>> A = Object("A")
    >>> B = Object("B")
    >>> f = NamedMorphism(A, B, "f")
    >>> f
    NamedMorphism(Object("A"), Object("B"), "f")
    >>> f.name
    'f'

    See Also
    ========

    Morphism
    c                     |st        d      t        |t              st        |      }t        j                  | |||      S )Nz!Empty morphism names not allowed.)
ValueError
isinstancer	   r   r"   )r   r    r!   names       r   r"   zNamedMorphism.__new__   s9    @AA$$t9D}}S&(D99r   c                 4    | j                   d   j                  S )a  
        Returns the name of the morphism.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> A = Object("A")
        >>> B = Object("B")
        >>> f = NamedMorphism(A, B, "f")
        >>> f.name
        'f'

           r%   r=   r&   s    r   r=   zNamedMorphism.name   s      yy|   r   N)r   r   r   r   r"   r2   r=   r   r   r   r9   r9      s     6: ! !r   r9   c                   \    e Zd ZdZed        Zd Zed        Zed        Z	ed        Z
d Zy)	r,   a  
    Represents a morphism which is a composition of other morphisms.

    Explanation
    ===========

    Two composite morphisms are equal if the morphisms they were
    obtained from (components) are the same and were listed in the
    same order.

    The arguments to the constructor for this class should be listed
    in diagram order: to obtain the composition `g\circ f` from the
    instances of :class:`Morphism` ``g`` and ``f`` use
    ``CompositeMorphism(f, g)``.

    Examples
    ========

    >>> from sympy.categories import Object, NamedMorphism, CompositeMorphism
    >>> A = Object("A")
    >>> B = Object("B")
    >>> C = Object("C")
    >>> f = NamedMorphism(A, B, "f")
    >>> g = NamedMorphism(B, C, "g")
    >>> g * f
    CompositeMorphism((NamedMorphism(Object("A"), Object("B"), "f"),
    NamedMorphism(Object("B"), Object("C"), "g")))
    >>> CompositeMorphism(f, g) == g * f
    True

    c                     t        |t              r| |j                  z   S t        |t              r| S | t	        |      z   S )aC  
        Intelligently adds ``morphism`` to tuple ``t``.

        Explanation
        ===========

        If ``morphism`` is a composite morphism, its components are
        added to the tuple.  If ``morphism`` is an identity, nothing
        is added to the tuple.

        No composability checks are performed.
        )r<   r,   
componentsr4   r   )tmorphisms     r   _add_morphismzCompositeMorphism._add_morphism  sA     h 12 x****"23HuX&&r   c                    |r,t        |d   t              st        j                  | g|d    S t	               }t        ||dd        D ]j  \  }}t        |t              rt        |t              st        d      |j                  |j                  k7  rt        d      t        j                  ||      }l t        j                  ||d         }|s|d   S t        |      dk(  r|d   S t        j                  | |      S )Nr   r*   z!All components must be morphisms.zUncomposable morphisms.)r<   r   r,   r"   r   zip	TypeErrorr!   r    r;   rF   lenr   )r   rC   normalised_componentscurrent	followings        r   r"   zCompositeMorphism.__new__'  s   jAA %,,SA:a=AA %"%j*QR."A 		0GYgx0"9h7 CDD9#3#33 !:;;$5$C$C%w%0!		0 !2 ? ?!:b>!3 % a= &'1,(++}}S"788r   c                      | j                   d   S )a  
        Returns the components of this composite morphism.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> (g * f).components
        (NamedMorphism(Object("A"), Object("B"), "f"),
        NamedMorphism(Object("B"), Object("C"), "g"))

        r   r$   r&   s    r   rC   zCompositeMorphism.componentsJ  s    & yy|r   c                 4    | j                   d   j                  S )a  
        Returns the domain of this composite morphism.

        The domain of the composite morphism is the domain of its
        first component.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> (g * f).domain
        Object("A")

        r   )rC   r    r&   s    r   r    zCompositeMorphism.domain_  s    * q!(((r   c                 4    | j                   d   j                  S )a  
        Returns the codomain of this composite morphism.

        The codomain of the composite morphism is the codomain of its
        last component.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> (g * f).codomain
        Object("C")

        rH   )rC   r!   r&   s    r   r!   zCompositeMorphism.codomainv  s    * r"+++r   c                 D    t        | j                  | j                  |      S )a  
        Forgets the composite structure of this morphism.

        Explanation
        ===========

        If ``new_name`` is not empty, returns a :class:`NamedMorphism`
        with the supplied name, otherwise returns a :class:`Morphism`.
        In both cases the domain of the new morphism is the domain of
        this composite morphism and the codomain of the new morphism
        is the codomain of this composite morphism.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> (g * f).flatten("h")
        NamedMorphism(Object("A"), Object("C"), "h")

        )r9   r    r!   )r'   new_names     r   flattenzCompositeMorphism.flatten  s    4 T[[$--BBr   N)r   r   r   r   staticmethodrF   r"   r2   rC   r    r!   rT   r   r   r   r,   r,      sc    > ' '.!9F  ( ) ), , ,,Cr   r,   c                   X    e Zd ZdZeefdZed        Zed        Zed        Z	d Z
d Zy)	Categoryad  
    An (abstract) category.

    Explanation
    ===========

    A category [JoyOfCats] is a quadruple `\mbox{K} = (O, \hom, id,
    \circ)` consisting of

    * a (set-theoretical) class `O`, whose members are called
      `K`-objects,

    * for each pair `(A, B)` of `K`-objects, a set `\hom(A, B)` whose
      members are called `K`-morphisms from `A` to `B`,

    * for a each `K`-object `A`, a morphism `id:A\rightarrow A`,
      called the `K`-identity of `A`,

    * a composition law `\circ` associating with every `K`-morphisms
      `f:A\rightarrow B` and `g:B\rightarrow C` a `K`-morphism `g\circ
      f:A\rightarrow C`, called the composite of `f` and `g`.

    Composition is associative, `K`-identities are identities with
    respect to composition, and the sets `\hom(A, B)` are pairwise
    disjoint.

    This class knows nothing about its objects and morphisms.
    Concrete cases of (abstract) categories should be implemented as
    classes derived from this one.

    Certain instances of :class:`Diagram` can be asserted to be
    commutative in a :class:`Category` by supplying the argument
    ``commutative_diagrams`` in the constructor.

    Examples
    ========

    >>> from sympy.categories import Object, NamedMorphism, Diagram, Category
    >>> from sympy import FiniteSet
    >>> A = Object("A")
    >>> B = Object("B")
    >>> C = Object("C")
    >>> f = NamedMorphism(A, B, "f")
    >>> g = NamedMorphism(B, C, "g")
    >>> d = Diagram([f, g])
    >>> K = Category("K", commutative_diagrams=[d])
    >>> K.commutative_diagrams == FiniteSet(d)
    True

    See Also
    ========

    Diagram
    c                     |st        d      t        |t              st        |      }t        |t              st        |      }t	        j
                  | ||t        |       }|S )Nz%A Category cannot have an empty name.)r;   r<   r	   r   r   r"   r   )r   r=   objectscommutative_diagramsnew_categorys        r   r"   zCategory.__new__  s[    DEE$$t9D'5)GnG}}S$%.0D%EGr   c                 4    | j                   d   j                  S )z
        Returns the name of this category.

        Examples
        ========

        >>> from sympy.categories import Category
        >>> K = Category("K")
        >>> K.name
        'K'

        r   r@   r&   s    r   r=   zCategory.name  s     yy|   r   c                      | j                   d   S )an  
        Returns the class of objects of this category.

        Examples
        ========

        >>> from sympy.categories import Object, Category
        >>> from sympy import FiniteSet
        >>> A = Object("A")
        >>> B = Object("B")
        >>> K = Category("K", FiniteSet(A, B))
        >>> K.objects
        Class({Object("A"), Object("B")})

        r*   r$   r&   s    r   rY   zCategory.objects  s    " yy|r   c                      | j                   d   S )aW  
        Returns the :class:`~.FiniteSet` of diagrams which are known to
        be commutative in this category.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism, Diagram, Category
        >>> from sympy import FiniteSet
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> d = Diagram([f, g])
        >>> K = Category("K", commutative_diagrams=[d])
        >>> K.commutative_diagrams == FiniteSet(d)
        True

        r?   r$   r&   s    r   rZ   zCategory.commutative_diagrams  s    , yy|r   c                     t        d      )Nz)hom-sets are not implemented in Category.r   )r'   ABs      r   homzCategory.hom*  s    !79 	9r   c                     t        d      )Nz@Obtaining the class of morphisms is not implemented in Category.r   r&   s    r   all_morphismszCategory.all_morphisms.  s    !NP 	Pr   N)r   r   r   r   r   r"   r2   r=   rY   rZ   rb   rd   r   r   r   rW   rW     s[    5l $,(  ! !  $  .9Pr   rW   c                   ~    e Zd ZdZed        Ze	 	 dd       Zd Zed        Z	ed        Z
ed        Zd Zd	 Zd
 Zy)Diagrama}	  
    Represents a diagram in a certain category.

    Explanation
    ===========

    Informally, a diagram is a collection of objects of a category and
    certain morphisms between them.  A diagram is still a monoid with
    respect to morphism composition; i.e., identity morphisms, as well
    as all composites of morphisms included in the diagram belong to
    the diagram.  For a more formal approach to this notion see
    [Pare1970].

    The components of composite morphisms are also added to the
    diagram.  No properties are assigned to such morphisms by default.

    A commutative diagram is often accompanied by a statement of the
    following kind: "if such morphisms with such properties exist,
    then such morphisms which such properties exist and the diagram is
    commutative".  To represent this, an instance of :class:`Diagram`
    includes a collection of morphisms which are the premises and
    another collection of conclusions.  ``premises`` and
    ``conclusions`` associate morphisms belonging to the corresponding
    categories with the :class:`~.FiniteSet`'s of their properties.

    The set of properties of a composite morphism is the intersection
    of the sets of properties of its components.  The domain and
    codomain of a conclusion morphism should be among the domains and
    codomains of the morphisms listed as the premises of a diagram.

    No checks are carried out of whether the supplied object and
    morphisms do belong to one and the same category.

    Examples
    ========

    >>> from sympy.categories import Object, NamedMorphism, Diagram
    >>> from sympy import pprint, default_sort_key
    >>> A = Object("A")
    >>> B = Object("B")
    >>> C = Object("C")
    >>> f = NamedMorphism(A, B, "f")
    >>> g = NamedMorphism(B, C, "g")
    >>> d = Diagram([f, g])
    >>> premises_keys = sorted(d.premises.keys(), key=default_sort_key)
    >>> pprint(premises_keys, use_unicode=False)
    [g*f:A-->C, id:A-->A, id:B-->B, id:C-->C, f:A-->B, g:B-->C]
    >>> pprint(d.premises, use_unicode=False)
    {g*f:A-->C: EmptySet, id:A-->A: EmptySet, id:B-->B: EmptySet,
     id:C-->C: EmptySet, f:A-->B: EmptySet, g:B-->C: EmptySet}
    >>> d = Diagram([f, g], {g * f: "unique"})
    >>> pprint(d.conclusions,use_unicode=False)
    {g*f:A-->C: {unique}}

    References
    ==========

    [Pare1970] B. Pareigis: Categories and functors.  Academic Press, 1970.

    c                 .    || v r| |   |z  | |<   y|| |<   y)a(  
        If ``key`` is in ``dictionary``, set the new value of ``key``
        to be the union between the old value and ``value``.
        Otherwise, set the value of ``key`` to ``value.

        Returns ``True`` if the key already was in the dictionary and
        ``False`` otherwise.
        TFr   )
dictionarykeyvalues      r   _set_dict_unionzDiagram._set_dict_unionp  s-     *(o5JsO#JsOr   c                    t         j                  | ||      sSt        |t              r|rt	        d      y|r^t
        }t        |j                        }t        |j                        }t         j                  | ||       t         j                  | ||       t        | j                               D ]u  \  }}	|	|z  }
|j                  |j                  k(  r||z  }t         j                  | ||
       |j                  |j                  k(  sZ||z  }t         j                  | ||
       w t        |t              r3|r0t
        }|j                  D ]  }t         j                  | |||        yyyy)z
        Adds a morphism and its attributes to the supplied dictionary
        ``morphisms``.  If ``add_identities`` is True, also adds the
        identity morphisms for the domain and the codomain of
        ``morphism``.
        z5Instances of IdentityMorphism cannot have properties.N)rf   rk   r<   r4   r;   r   r    r!   listitemsr,   rC   _add_morphism_closure)	morphismsrE   propsadd_identitiesrecurse_compositesemptyid_domid_codexisting_morphismexisting_props	new_propsleftright	components                 r   ro   zDiagram._add_morphism_closure  sk    &&y(EB ($45 %OQ Q )(//:)(*;*;<''	65A''	65A59)//:K5L I1!>*U2	??&7&@&@@#&77D++ItYG$$(9(@(@@-8E++IuiHI ($56;M !!)!4!4 BI11)Y2@BB	 <N6A Cr   c                    i }i }t         }t        |      dk\  r|d   }t        |t              rHt         }|D ]<  }|t	        |j
                  |j                        z  }t        j                  |||       > nt        |t        t        f      rn|j                         D ][  \  }}|t	        |j
                  |j                        z  }t        j                  ||t        |      rt	        | n
t	        |             ] t        |      dk\  r)|d   }	t        |	t              rt         }|	D ]  }t        |j                  |j
                              t        j                   u s8t        |j                  |j                              t        j                   u smt        j                  |||dd        nt        |	t        t        f      rl|	j                         D ]Y  \  }}|j
                  |v s|j                  |v s$t        j                  ||t        |      rt	        | n
t	        |      dd       [ t#        j$                  | t        |      t        |      |      S )a  
        Construct a new instance of Diagram.

        Explanation
        ===========

        If no arguments are supplied, an empty diagram is created.

        If at least an argument is supplied, ``args[0]`` is
        interpreted as the premises of the diagram.  If ``args[0]`` is
        a list, it is interpreted as a list of :class:`Morphism`'s, in
        which each :class:`Morphism` has an empty set of properties.
        If ``args[0]`` is a Python dictionary or a :class:`Dict`, it
        is interpreted as a dictionary associating to some
        :class:`Morphism`'s some properties.

        If at least two arguments are supplied ``args[1]`` is
        interpreted as the conclusions of the diagram.  The type of
        ``args[1]`` is interpreted in exactly the same way as the type
        of ``args[0]``.  If only one argument is supplied, the diagram
        has no conclusions.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> from sympy.categories import IdentityMorphism, Diagram
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> d = Diagram([f, g])
        >>> IdentityMorphism(A) in d.premises.keys()
        True
        >>> g * f in d.premises.keys()
        True
        >>> d = Diagram([f, g], {g * f: "unique"})
        >>> d.conclusions[g * f]
        {unique}

        r*   r   r?   F)rr   rs   )r   rK   r<   rm   r   r    r!   rf   ro   dictr   rn   r   r   containsr   truer   r"   )
r   r%   premisesconclusionsrY   premises_argrt   rE   rq   conclusions_args
             r   r"   zDiagram.__new__  s(   V  t9>7L,- ! , MHy(:K:KLLG11(HeLM L4,7 (4'9'9'; hOHey(:K:KLLG11 (%Iu,=V_`eVfhh
 t9>"1gO/40 ! / 6H !1!1(//!BCqvvM !1!1(2C2C!DEO  55'5/4 6 66 OdD\: (7'<'<'> LOHe 72 ))W4  55'QV9e3D]fgl]m+0U 6 LL }}S$x.${2CWMMr   c                      | j                   d   S )aL  
        Returns the premises of this diagram.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> from sympy.categories import IdentityMorphism, Diagram
        >>> from sympy import pretty
        >>> A = Object("A")
        >>> B = Object("B")
        >>> f = NamedMorphism(A, B, "f")
        >>> id_A = IdentityMorphism(A)
        >>> id_B = IdentityMorphism(B)
        >>> d = Diagram([f])
        >>> print(pretty(d.premises, use_unicode=False))
        {id:A-->A: EmptySet, id:B-->B: EmptySet, f:A-->B: EmptySet}

        r   r$   r&   s    r   r   zDiagram.premises  s    * yy|r   c                      | j                   d   S )a  
        Returns the conclusions of this diagram.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism
        >>> from sympy.categories import IdentityMorphism, Diagram
        >>> from sympy import FiniteSet
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> d = Diagram([f, g])
        >>> IdentityMorphism(A) in d.premises.keys()
        True
        >>> g * f in d.premises.keys()
        True
        >>> d = Diagram([f, g], {g * f: "unique"})
        >>> d.conclusions[g * f] == FiniteSet("unique")
        True

        r*   r$   r&   s    r   r   zDiagram.conclusions0  s    4 yy|r   c                      | j                   d   S )a  
        Returns the :class:`~.FiniteSet` of objects that appear in this
        diagram.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism, Diagram
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> d = Diagram([f, g])
        >>> d.objects
        {Object("A"), Object("B"), Object("C")}

        r?   r$   r&   s    r   rY   zDiagram.objectsL  s    ( yy|r   c                 V   t         }t         }| j                  j                         D ]0  }|j                  |k(  s|j                  |k(  s#|t        |      z  }2 | j                  j                         D ]0  }|j                  |k(  s|j                  |k(  s#|t        |      z  }2 ||fS )a  
        Returns a 2-tuple of sets of morphisms between objects ``A`` and
        ``B``: one set of morphisms listed as premises, and the other set
        of morphisms listed as conclusions.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism, Diagram
        >>> from sympy import pretty
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> d = Diagram([f, g], {g * f: "unique"})
        >>> print(pretty(d.hom(A, C), use_unicode=False))
        ({g*f:A-->C}, {g*f:A-->C})

        See Also
        ========
        Object, Morphism
        )r   r   keysr    r!   r   r   )r'   r`   ra   r   r   rE   s         r   rb   zDiagram.homb  s    0 **, 	0H1$8+<+<+AIh//	0 ((--/ 	3H1$8+<+<+Ay22	3 +&&r   c                      t         fdj                  D              }|syt         fdj                  D              }|S )a  
        Checks whether ``diagram`` is a subdiagram of ``self``.
        Diagram `D'` is a subdiagram of `D` if all premises
        (conclusions) of `D'` are contained in the premises
        (conclusions) of `D`.  The morphisms contained
        both in `D'` and `D` should have the same properties for `D'`
        to be a subdiagram of `D`.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism, Diagram
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> d = Diagram([f, g], {g * f: "unique"})
        >>> d1 = Diagram([f])
        >>> d.is_subdiagram(d1)
        True
        >>> d1.is_subdiagram(d)
        False
        c              3   |   K   | ]3  }|j                   v xr j                   |   j                   |   k(   5 y wr6   )r   .0mdiagramr'   s     r   	<genexpr>z(Diagram.is_subdiagram.<locals>.<genexpr>  sJ      2 T]]* A((+t}}Q/??A 2   9<Fc              3   |   K   | ]3  }|j                   v xr j                   |   j                   |   k(   5 y wr6   )r   r   s     r   r   z(Diagram.is_subdiagram.<locals>.<genexpr>  sN      8  0 00 J"..q1T5E5Ea5HHJ 8r   )allr   r   )r'   r   r   r   s   ``  r   is_subdiagramzDiagram.is_subdiagram  sO    2  2 ' 0 02 2  8#*#6#68 8
 r   c                    |j                  | j                        st        d      i }| j                  j	                         D ]t  \  }}t        |j                  |j                              t        j                  u s;t        |j                  |j                              t        j                  u sp|||<   v i }| j                  j	                         D ]t  \  }}t        |j                  |j                              t        j                  u s;t        |j                  |j                              t        j                  u sp|||<   v t        ||      S )a  
        If ``objects`` is a subset of the objects of ``self``, returns
        a diagram which has as premises all those premises of ``self``
        which have a domains and codomains in ``objects``, likewise
        for conclusions.  Properties are preserved.

        Examples
        ========

        >>> from sympy.categories import Object, NamedMorphism, Diagram
        >>> from sympy import FiniteSet
        >>> A = Object("A")
        >>> B = Object("B")
        >>> C = Object("C")
        >>> f = NamedMorphism(A, B, "f")
        >>> g = NamedMorphism(B, C, "g")
        >>> d = Diagram([f, g], {f: "unique", g*f: "veryunique"})
        >>> d1 = d.subdiagram_from_objects(FiniteSet(A, B))
        >>> d1 == Diagram([f], {f: "unique"})
        True
        z2Supplied objects should all belong to the diagram.)	is_subsetrY   r;   r   rn   r   r   r    r   r   r!   r   rf   )r'   rY   new_premisesrE   rq   new_conclusionss         r   subdiagram_from_objectszDiagram.subdiagram_from_objects  s   ,   .DF F #}}224 	/OHe))(//:;qvvE))(*;*;<=G).X&	/
 #//557 	2OHe))(//:;qvvE))(*;*;<=G,1)	2
 |_55r   N)TT)r   r   r   r   rU   rk   ro   r"   r2   r   r   rY   rb   r   r   r   r   r   rf   rf   3  s    ;x    IM15.B .B`eNN  ,  6  *"'H$L&6r   rf   N)
sympy.corer   r   r   r   r   r   sympy.core.symbolr	   
sympy.setsr
   r   r   sympy.utilities.iterablesr   r   r   r   r4   r9   r,   rW   rf   r   r   r   <module>r      s    = = ! / / .C &
V 
h#u h#V"x "J5!H 5!pxC xCvFPu FPR_6e _6r   