o
    ¦ƒðjÊ$  ã                   @   sÞ  d dl mZ d dlmZ 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 d dlmZmZmZmZ d dlmZ dd	„ Zd
d„ Zdd„ ZG dd„ dƒZeƒ Ze
dƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZe e¡dd„ ƒZejdd„ ej dd„ ej!dd„ ej"d d„ ej#d!d„ ej$d"d„ ej%d#d„ ej&d$d„ ej'd%d„ ej(d&d„ i
Z)e ee	e¡d'd„ ƒZd(S ))é    )Údefaultdict)ÚQ)ÚAddÚMulÚPowÚNumberÚNumberSymbolÚSymbol)ÚImaginaryUnit)ÚAbs)Ú
EquivalentÚAndÚOrÚImplies)ÚMatMulc                    ó   t ‡ ‡fdd„|jD ƒŽ S )aú  
    Apply all arguments of the expression to the fact structure.

    Parameters
    ==========

    symbol : Symbol
        A placeholder symbol.

    fact : Boolean
        Resulting ``Boolean`` expression.

    expr : Expr

    Examples
    ========

    >>> from sympy import Q
    >>> from sympy.assumptions.sathandlers import allargs
    >>> from sympy.abc import x, y
    >>> allargs(x, Q.negative(x) | Q.positive(x), x*y)
    (Q.negative(x) | Q.positive(x)) & (Q.negative(y) | Q.positive(y))

    c                    ó   g | ]}ˆ   ˆ|¡‘qS © ©Úsubs©Ú.0Úarg©ÚfactÚsymbolr   úŸ/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/assumptions/sathandlers.pyÚ
<listcomp>(   ó    zallargs.<locals>.<listcomp>)r   Úargs©r   r   Úexprr   r   r   Úallargs   ó   r"   c                    r   )a÷  
    Apply any argument of the expression to the fact structure.

    Parameters
    ==========

    symbol : Symbol
        A placeholder symbol.

    fact : Boolean
        Resulting ``Boolean`` expression.

    expr : Expr

    Examples
    ========

    >>> from sympy import Q
    >>> from sympy.assumptions.sathandlers import anyarg
    >>> from sympy.abc import x, y
    >>> anyarg(x, Q.negative(x) & Q.positive(x), x*y)
    (Q.negative(x) & Q.positive(x)) | (Q.negative(y) & Q.positive(y))

    c                    r   r   r   r   r   r   r   r   D   r   zanyarg.<locals>.<listcomp>)r   r   r    r   r   r   Úanyarg+   r#   r$   c                    s8   ‡ ‡fdd„|j D ƒ‰t‡fdd„ttˆƒƒD ƒŽ }|S )aÿ  
    Apply exactly one argument of the expression to the fact structure.

    Parameters
    ==========

    symbol : Symbol
        A placeholder symbol.

    fact : Boolean
        Resulting ``Boolean`` expression.

    expr : Expr

    Examples
    ========

    >>> from sympy import Q
    >>> from sympy.assumptions.sathandlers import exactlyonearg
    >>> from sympy.abc import x, y
    >>> exactlyonearg(x, Q.positive(x), x*y)
    (Q.positive(x) & ~Q.positive(y)) | (Q.positive(y) & ~Q.positive(x))

    c                    r   r   r   r   r   r   r   r   `   r   z!exactlyonearg.<locals>.<listcomp>c              	      sB   g | ]}t ˆ | gd d„ ˆ d|… ˆ |d d…  D ƒ¢R Ž ‘qS )c                 S   s   g | ]}| ‘qS r   r   )r   Úlitr   r   r   r   a   s    z,exactlyonearg.<locals>.<listcomp>.<listcomp>Né   )r   )r   Úi)Ú	pred_argsr   r   r   a   s
    ÿÿ)r   r   ÚrangeÚlen)r   r   r!   Úresr   )r   r(   r   r   ÚexactlyoneargG   s
   
ÿr,   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚClassFactRegistrya¦  
    Register handlers against classes.

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

    ``register`` method registers the handler function for a class. Here,
    handler function should return a single fact. ``multiregister`` method
    registers the handler function for multiple classes. Here, handler function
    should return a container of multiple facts.

    ``registry(expr)`` returns a set of facts for *expr*.

    Examples
    ========

    Here, we register the facts for ``Abs``.

    >>> from sympy import Abs, Equivalent, Q
    >>> from sympy.assumptions.sathandlers import ClassFactRegistry
    >>> reg = ClassFactRegistry()
    >>> @reg.register(Abs)
    ... def f1(expr):
    ...     return Q.nonnegative(expr)
    >>> @reg.register(Abs)
    ... def f2(expr):
    ...     arg = expr.args[0]
    ...     return Equivalent(~Q.zero(arg), ~Q.zero(expr))

    Calling the registry with expression returns the defined facts for the
    expression.

    >>> from sympy.abc import x
    >>> reg(Abs(x))
    {Q.nonnegative(Abs(x)), Equivalent(~Q.zero(x), ~Q.zero(Abs(x)))}

    Multiple facts can be registered at once by ``multiregister`` method.

    >>> reg2 = ClassFactRegistry()
    >>> @reg2.multiregister(Abs)
    ... def _(expr):
    ...     arg = expr.args[0]
    ...     return [Q.even(arg) >> Q.even(expr), Q.odd(arg) >> Q.odd(expr)]
    >>> reg2(Abs(x))
    {Implies(Q.even(x), Q.even(Abs(x))), Implies(Q.odd(x), Q.odd(Abs(x)))}

    c                 C   s   t tƒ| _t tƒ| _d S ©N)r   Ú	frozensetÚsinglefactsÚ
multifacts)Úselfr   r   r   Ú__init__˜   s   
zClassFactRegistry.__init__c                    ó   ‡ ‡fdd„}|S )Nc                    s   ˆj ˆ   | hO  < | S r.   )r0   )Úfunc©Úclsr2   r   r   Ú_�   s   z%ClassFactRegistry.register.<locals>._r   )r2   r7   r8   r   r6   r   Úregisterœ   s   zClassFactRegistry.registerc                    r4   )Nc                    s"   ˆ D ]}ˆj |  | hO  < q| S r.   )r1   )r5   r7   ©Úclassesr2   r   r   r8   £   s   z*ClassFactRegistry.multiregister.<locals>._r   )r2   r;   r8   r   r:   r   Úmultiregister¢   s   zClassFactRegistry.multiregisterc                 C   sd   | j | }| j D ]}t||ƒr|| j | O }q| j| }| jD ]}t||ƒr-|| j| O }q||fS r.   )r0   Ú
issubclassr1   )r2   ÚkeyÚret1ÚkÚret2r   r   r   Ú__getitem__©   s   


€


€zClassFactRegistry.__getitem__c                    sJ   t ƒ }| tˆ ƒ \}}| ‡ fdd„|D ƒ¡ |D ]	}| |ˆ ƒ¡ q|S )Nc                 3   s   � | ]}|ˆ ƒV  qd S r.   r   )r   Úh©r!   r   r   Ú	<genexpr>»   s   € z-ClassFactRegistry.__call__.<locals>.<genexpr>)ÚsetÚtypeÚupdate)r2   r!   ÚretÚ	handlers1Ú	handlers2rC   r   rD   r   Ú__call__¶   s   zClassFactRegistry.__call__N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r9   r<   rB   rL   r   r   r   r   r-   h   s    /r-   Úxc                 C   sd   | j d }t | ¡tt |¡ t | ¡ ƒt |¡t | ¡? t |¡t | ¡? t |¡t | ¡? gS )Nr   )r   r   Únonnegativer   ÚzeroÚevenÚoddÚinteger)r!   r   r   r   r   r8   Ê   s   
ür8   c              
   C   s¤   t tt t¡| ƒt | ¡? t tt t¡| ƒt | ¡? t tt t¡| ƒt | ¡? t tt t¡| ƒt | ¡? t tt t¡| ƒt | ¡? ttt t¡ | ƒt | ¡ ? gS r.   )	r"   rQ   r   ÚpositiveÚnegativeÚrealÚrationalrV   r,   rD   r   r   r   r8   ×   s   ûc                 C   ó:   t tt t¡| ƒ}ttt t¡| ƒ}t|t|t | ¡ƒƒS r.   ©r"   rQ   r   rY   r,   Ú
irrationalr   ©r!   Úallargs_realÚonearg_irrationalr   r   r   r8   á   ó   c                 C   sÀ   t t | ¡ttt t¡| ƒƒttt t¡| ƒt | ¡? ttt t¡| ƒt | ¡? ttt t¡| ƒt | ¡? ttt 	t¡| ƒt 	| ¡? t
tt t¡ | ƒt 	| ¡ ? ttt t¡| ƒt | ¡? gS r.   )r   r   rS   r$   rQ   r"   rW   rY   rZ   rV   r,   ÚcommutativerD   r   r   r   r8   ê   s   úc                 C   s$   t tt t¡| ƒ}t|t | ¡ ƒS r.   )r"   rQ   r   Úprimer   )r!   Úallargs_primer   r   r   r8   õ   s   c                 C   sD   t tt t¡t t¡B | ƒ}ttt t¡| ƒ}t|t|t | ¡ƒƒS r.   )r"   rQ   r   Ú	imaginaryrY   r,   r   )r!   Úallargs_imag_or_realÚonearg_imaginaryr   r   r   r8   þ   s   c                 C   r[   r.   r\   r^   r   r   r   r8     ra   c                 C   s:   t tt t¡| ƒ}ttt t¡| ƒ}t|t|t | ¡ƒƒS r.   )r"   rQ   r   rV   r$   rT   r   r   )r!   Úallargs_integerÚanyarg_evenr   r   r   r8     s   c                 C   s:   t tt t¡| ƒ}t tt t¡| ƒ}t|tt | ¡|ƒƒS r.   )r"   rQ   r   ÚsquareÚ
invertibler   r   )r!   Úallargs_squareÚallargs_invertibler   r   r   r8     ra   c              	   C   s¢   | j | j}}t |¡t |¡@ t |¡@ t | ¡? t |¡t |¡@ t |¡@ t | ¡? t |¡t |¡@ t |¡@ t | ¡? tt 	| ¡t 	|¡t 
|¡@ ƒgS r.   )ÚbaseÚexpr   rY   rT   rR   rU   Únonpositiver   rS   rW   )r!   rn   ro   r   r   r   r8      s   &&&üc                 C   ó   | j S r.   )Úis_positive©Úor   r   r   Ú<lambda>.  ó    ru   c                 C   rq   r.   )Úis_zerors   r   r   r   ru   /  rv   c                 C   rq   r.   )Úis_negativers   r   r   r   ru   0  rv   c                 C   rq   r.   )Úis_rationalrs   r   r   r   ru   1  rv   c                 C   rq   r.   )Úis_irrationalrs   r   r   r   ru   2  rv   c                 C   rq   r.   )Úis_evenrs   r   r   r   ru   3  rv   c                 C   rq   r.   )Úis_oddrs   r   r   r   ru   4  rv   c                 C   rq   r.   )Úis_imaginaryrs   r   r   r   ru   5  rv   c                 C   rq   r.   )Úis_primers   r   r   r   ru   6  rv   c                 C   rq   r.   )Úis_compositers   r   r   r   ru   7  rv   c                 C   sB   g }t  ¡ D ]\}}|| ƒ}|| ƒ}|d ur| t||ƒ¡ q|S r.   )Ú_old_assump_gettersÚitemsÚappendr   )r!   rI   ÚpÚgetterÚpredÚpropr   r   r   r8   :  s   €N)*Úcollectionsr   Úsympy.assumptions.askr   Ú
sympy.corer   r   r   r   r   r	   Úsympy.core.numbersr
   Ú$sympy.functions.elementary.complexesr   Úsympy.logic.boolalgr   r   r   r   Úsympy.matrices.expressionsr   r"   r$   r,   r-   Úclass_fact_registryrQ   r<   r8   r9   rW   rS   rX   rZ   r]   rT   rU   re   rc   Ú	compositer€   r   r   r   r   Ú<module>   s\     !X

	


















ö