
    wg-                         d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
  G d de      Z G d d	ee
      Z G d de      Z e       Z G d de      Z e       Z G d de      Z e       Z G d d      Zy)ax  
Module to efficiently partition SymPy objects.

This system is introduced because class of SymPy object does not always
represent the mathematical classification of the entity. For example,
``Integral(1, x)`` and ``Integral(Matrix([1,2]), x)`` are both instance
of ``Integral`` class. However the former is number and the latter is
matrix.

One way to resolve this is defining subclass for each mathematical type,
such as ``MatAdd`` for the addition between matrices. Basic algebraic
operation such as addition or multiplication take this approach, but
defining every class for every mathematical object is not scalable.

Therefore, we define the "kind" of the object and let the expression
infer the kind of itself from its arguments. Function and class can
filter the arguments by their kind, and behave differently according to
the type of itself.

This module defines basic kinds for core objects. Other kinds such as
``ArrayKind`` or ``MatrixKind`` can be found in corresponding modules.

.. notes::
       This approach is experimental, and can be replaced or deleted in the future.
       See https://github.com/sympy/sympy/pull/20549.
    )defaultdict   )cacheit)
Dispatcherambiguity_warn#ambiguity_register_error_ignore_dupstr_signatureRaiseNotImplementedErrorc                   "     e Zd ZdZ fdZ xZS )KindMetaz
    Metaclass for ``Kind``.

    Assigns empty ``dict`` as class attribute ``_inst`` for every class,
    in order to endow singleton-like behavior.
    c                 2    i |d<   t         |   | |||      S )N_instsuper__new__)clsclsnamebasesdct	__class__s       T/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/core/kind.pyr   zKindMeta.__new__+   s!    GwsGUC88    __name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r   $   s    9 9r   r   c                   "     e Zd ZdZ fdZ xZS )Kinda  
    Base class for kinds.

    Kind of the object represents the mathematical classification that
    the entity falls into. It is expected that functions and classes
    recognize and filter the argument by its kind.

    Kind of every object must be carefully selected so that it shows the
    intention of design. Expressions may have different kind according
    to the kind of its arguments. For example, arguments of ``Add``
    must have common kind since addition is group operator, and the
    resulting ``Add()`` has the same kind.

    For the performance, each kind is as broad as possible and is not
    based on set theory. For example, ``NumberKind`` includes not only
    complex number but expression containing ``S.Infinity`` or ``S.NaN``
    which are not strictly number.

    Kind may have arguments as parameter. For example, ``MatrixKind()``
    may be constructed with one element which represents the kind of its
    elements.

    ``Kind`` behaves in singleton-like fashion. Same signature will
    return the same object.

    c                     || j                   v r| j                   |   }|S t        | 	  |       }|| j                   |<   |S N)r   r   r   )r   argsinstr   s      r   r   zKind.__new__K   sE    39999T?D  7?3'D"CIIdOr   r   r   s   @r   r!   r!   0   s    4 r   r!   )	metaclassc                   (     e Zd ZdZ fdZd Z xZS )_UndefinedKinda  
    Default kind for all SymPy object. If the kind is not defined for
    the object, or if the object cannot infer the kind from its
    arguments, this will be returned.

    Examples
    ========

    >>> from sympy import Expr
    >>> Expr().kind
    UndefinedKind
    c                 "    t         |   |       S r#   r   r   r   s    r   r   z_UndefinedKind.__new__a       ws##r   c                      y)NUndefinedKind selfs    r   __repr__z_UndefinedKind.__repr__d   s    r   r   r   r   r   r   r1   r   r   s   @r   r(   r(   T   s    $r   r(   c                   (     e Zd ZdZ fdZd Z xZS )_NumberKinda  
    Kind for all numeric object.

    This kind represents every number, including complex numbers,
    infinity and ``S.NaN``. Other objects such as quaternions do not
    have this kind.

    Most ``Expr`` are initially designed to represent the number, so
    this will be the most common kind in SymPy core. For example
    ``Symbol()``, which represents a scalar, has this kind as long as it
    is commutative.

    Numbers form a field. Any operation between number-kind objects will
    result this kind as well.

    Examples
    ========

    >>> from sympy import S, oo, Symbol
    >>> S.One.kind
    NumberKind
    >>> (-oo).kind
    NumberKind
    >>> S.NaN.kind
    NumberKind

    Commutative symbol are treated as number.

    >>> x = Symbol('x')
    >>> x.kind
    NumberKind
    >>> Symbol('y', commutative=False).kind
    UndefinedKind

    Operation between numbers results number.

    >>> (x+1).kind
    NumberKind

    See Also
    ========

    sympy.core.expr.Expr.is_Number : check if the object is strictly
    subclass of ``Number`` class.

    sympy.core.expr.Expr.is_number : check if the object is number
    without any free symbol.

    c                 "    t         |   |       S r#   r   r*   s    r   r   z_NumberKind.__new__   r+   r   c                      y)N
NumberKindr.   r/   s    r   r1   z_NumberKind.__repr__   s    r   r2   r   s   @r   r4   r4   j   s    0b$r   r4   c                   (     e Zd ZdZ fdZd Z xZS )_BooleanKinda8  
    Kind for boolean objects.

    SymPy's ``S.true``, ``S.false``, and built-in ``True`` and ``False``
    have this kind. Boolean number ``1`` and ``0`` are not relevant.

    Examples
    ========

    >>> from sympy import S, Q
    >>> S.true.kind
    BooleanKind
    >>> Q.even(3).kind
    BooleanKind
    c                 "    t         |   |       S r#   r   r*   s    r   r   z_BooleanKind.__new__   r+   r   c                      y)NBooleanKindr.   r/   s    r   r1   z_BooleanKind.__repr__   s    r   r2   r   s   @r   r9   r9      s    $r   r9   c                   J    e Zd ZdZd	dZd Zd Zd Zed        Z	e
d        Zy)
KindDispatchera  
    Dispatcher to select a kind from multiple kinds by binary dispatching.

    .. notes::
       This approach is experimental, and can be replaced or deleted in
       the future.

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

    SymPy object's :obj:`sympy.core.kind.Kind()` vaguely represents the
    algebraic structure where the object belongs to. Therefore, with
    given operation, we can always find a dominating kind among the
    different kinds. This class selects the kind by recursive binary
    dispatching. If the result cannot be determined, ``UndefinedKind``
    is returned.

    Examples
    ========

    Multiplication between numbers return number.

    >>> from sympy import NumberKind, Mul
    >>> Mul._kind_dispatcher(NumberKind, NumberKind)
    NumberKind

    Multiplication between number and unknown-kind object returns unknown kind.

    >>> from sympy import UndefinedKind
    >>> Mul._kind_dispatcher(NumberKind, UndefinedKind)
    UndefinedKind

    Any number and order of kinds is allowed.

    >>> Mul._kind_dispatcher(UndefinedKind, NumberKind)
    UndefinedKind
    >>> Mul._kind_dispatcher(NumberKind, UndefinedKind, NumberKind)
    UndefinedKind

    Since matrix forms a vector space over scalar field, multiplication
    between matrix with numeric element and number returns matrix with
    numeric element.

    >>> from sympy.matrices import MatrixKind
    >>> Mul._kind_dispatcher(MatrixKind(NumberKind), NumberKind)
    MatrixKind(NumberKind)

    If a matrix with number element and another matrix with unknown-kind
    element are multiplied, we know that the result is matrix but the
    kind of its elements is unknown.

    >>> Mul._kind_dispatcher(MatrixKind(NumberKind), MatrixKind(UndefinedKind))
    MatrixKind(UndefinedKind)

    Parameters
    ==========

    name : str

    commutative : bool, optional
        If True, binary dispatch will be automatically registered in
        reversed order as well.

    doc : str, optional

    Nc                 N    || _         || _        || _        t        |      | _        y r#   )namedoccommutativer   _dispatcher)r0   r@   rB   rA   s       r   __init__zKindDispatcher.__init__  s%    	&%d+r   c                      d| j                   z  S )Nz<dispatched %s>)r@   r/   s    r   r1   zKindDispatcher.__repr__  s     499,,r   c                      j                  dd      }|s j                  rt        }nt        }j	                  |       t              dk(  s$t        dt              dt              d       fd}|S )	z
        Register the binary dispatcher for two kind classes.

        If *self.commutative* is ``True``, signature in reversed order is
        automatically registered as well.
        on_ambiguityN)rG      z+Only binary dispatch is supported, but got z	 types: <z>.c                      j                   j                  | fi  j                  r1 j                   j                  t        t	                    | fi  y y r#   )rC   addrB   tuplereversed)funckwargsr0   typess    r   _z"KindDispatcher.register.<locals>._  sT     D  77$  $$U8E?%;TLVL  r   )poprB   r   r   updatelenRuntimeErrorr	   )r0   rO   rN   rG   rP   s   ```  r   registerzKindDispatcher.register
  sq     zz.$7B-<05zQE
M%0 
	M r   c                     | j                   rt        |      }n#g }d }|D ]  }||us|j                  |       |}  | j                  |fi |S r#   )rB   	frozensetappenddispatch_kinds)r0   r$   rN   kindsprevas         r   __call__zKindDispatcher.__call__%  s^    dOEED q=LLOD #t""53F33r   c                 $   t        |      dk(  r$|\  }t        |t              st        d|z        |S t	        |      D ]  \  }}t        |t              st        d|z        |dk(  r|},}t        |      t        |      }}||}
}	| j                  j                  ||      }|,| j                  r | j                  j                  ||      }|
|	}
}	|t        }n	 ||	|
      }t        |t              rt        dj                  |||             S )Nr   z%s is not a kind.r   z=Dispatcher for {!r} and {!r} must return a Kind, but got {!r})rS   
isinstancer!   rT   	enumeratetyperC   dispatchrB   r-   format)r0   rZ   rN   resultikind	prev_kindt1t2k1k2rM   s               r   rY   zKindDispatcher.dispatch_kinds1  s"    u:?GFfd+"#6#?@@M& 	FAddD)"#6#=>>Av"	i$t*B"DB''00R8<D$4$4++44R<DB<*F!"b\F!&$/&W^^!4 -	6 r   c                    d| j                   z  dg}| j                  r|j                  | j                         d}|dt        |      z  z  }|j                  |       g }t	        t
              }| j                  j                  d d d   D ]/  }| j                  j                  |   }||   j                  |       1 |j                         D ]  \  }}dj                  d |D              }t        |t              r|j                  |       @d|z  }|d	t        |      z  d
z   z  }|j                  r||j                  j                         z  }n||j                  z  }|j                  |        |rFd}|dt        |      z  z  }|j                  |       d
j                  |      }|j                  |       dj                  |      S )NzKind dispatcher : %sz`Note that support for this is experimental. See the docs for :class:`KindDispatcher` for detailszRegistered kind classes
=z, c              3   8   K   | ]  }d t        |      z    yw)z<%s>N)r	   ).0sigs     r   	<genexpr>z)KindDispatcher.__doc__.<locals>.<genexpr>n  s      M-*<!< Ms   zInputs: %s
-
zAmbiguous kind classes
z

)r@   rA   rX   rS   r   listrC   orderingfuncsitemsjoinr_   r
   r   stripr   )	r0   docssamb_sigstyp_sigssigskeyrM   sigs_strs	            r   r   zKindDispatcher.__doc__W  s    #TYY.n

 88KK!'	S3q6\At$$$--dd3 	'D""((.CSM  &	' #..* 	JD$yy M MMH$ 89))As1v$$A||T\\''))T]]"KKN	  *As1vAKKN		(#AKKN{{4  r   )FN)r   r   r   r   rD   r1   rU   r]   r   rY   propertyr.   r   r   r>   r>      sF    AD,-6
4 # #J ,! ,!r   r>   N)r   collectionsr   cacher   !sympy.multipledispatch.dispatcherr   r   r   r	   r
   ra   r   objectr!   r(   r-   r4   r7   r9   r<   r>   r.   r   r   <module>r      s   6 $ - -
	9t 	9!6X !HT &  6$ 6p ]
4 , nF! F!r   