B
    c‹dÔ$  ã               @   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                s   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                s   g | ]}ˆ   ˆ|¡‘qS © )Úsubs)Ú.0Úarg)ÚfactÚsymbolr   új/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sympy/assumptions/sathandlers.pyú
<listcomp>(   s    zallargs.<locals>.<listcomp>)r   Úargs)r   r   Úexprr   )r   r   r   Úallargs   s    r   c                s   t ‡ ‡fdd„|jD ƒŽ S )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                s   g | ]}ˆ   ˆ|¡‘qS r   )r   )r   r   )r   r   r   r   r   D   s    zanyarg.<locals>.<listcomp>)r   r   )r   r   r   r   )r   r   r   Úanyarg+   s    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                s   g | ]}ˆ   ˆ|¡‘qS r   )r   )r   r   )r   r   r   r   r   `   s    z!exactlyonearg.<locals>.<listcomp>c          	      s@   g | ]8}t ˆ | fd d„ ˆ d|… ˆ |d d…  D ƒžŽ ‘qS )c             S   s   g | ]
}| ‘qS r   r   )r   Z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                s   ‡ ‡fdd„}|S )Nc                s   ˆj ˆ   | hO  < | S )N)r&   )Úfunc)Úclsr(   r   r   Ú_   s    z%ClassFactRegistry.register.<locals>._r   )r(   r+   r,   r   )r+   r(   r   Úregisterœ   s    zClassFactRegistry.registerc                s   ‡ ‡fdd„}|S )Nc                s&   x ˆ D ]}ˆj |  | hO  < qW | S )N)r'   )r*   r+   )Úclassesr(   r   r   r,   £   s    
z*ClassFactRegistry.multiregister.<locals>._r   )r(   r.   r,   r   )r.   r(   r   Úmultiregister¢   s    zClassFactRegistry.multiregisterc             C   sl   | j | }x&| j D ]}t||ƒr|| j | O }qW | j| }x&| jD ]}t||ƒrD|| j| O }qDW ||fS )N)r&   Ú
issubclassr'   )r(   ÚkeyZret1ÚkZret2r   r   r   Ú__getitem__©   s    



zClassFactRegistry.__getitem__c             C   sR   t ƒ }| t|ƒ \}}x|D ]}| ||ƒ¡ qW x|D ]}| ||ƒ¡ q8W |S )N)ÚsetÚtypeÚaddÚupdate)r(   r   ÚretZ	handlers1Z	handlers2Úhr   r   r   Ú__call__¶   s    

zClassFactRegistry.__call__N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   r-   r/   r3   r:   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   r,   Ë   s    
r,   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 )N)	r   r?   r   ÚpositiveÚnegativeÚrealÚrationalrD   r#   )r   r   r   r   r,   Ø   s    c             C   s:   t tt t¡| ƒ}ttt t¡| ƒ}t|t|t | ¡ƒƒS )N)r   r?   r   rG   r#   Ú
irrationalr   )r   Úallargs_realÚonearg_irrationalr   r   r   r,   â   s    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 )N)r   r   rA   r   r?   r   rE   rG   rH   rD   r#   Zcommutative)r   r   r   r   r,   ë   s    c             C   s$   t tt t¡| ƒ}t|t | ¡ ƒS )N)r   r?   r   Úprimer   )r   Zallargs_primer   r   r   r,   ö   s    c             C   sD   t tt t¡t t¡B | ƒ}ttt t¡| ƒ}t|t|t | ¡ƒƒS )N)r   r?   r   Ú	imaginaryrG   r#   r   )r   Zallargs_imag_or_realZonearg_imaginaryr   r   r   r,   ÿ   s    c             C   s:   t tt t¡| ƒ}ttt t¡| ƒ}t|t|t | ¡ƒƒS )N)r   r?   r   rG   r#   rI   r   )r   rJ   rK   r   r   r   r,     s    c             C   s:   t tt t¡| ƒ}ttt t¡| ƒ}t|t|t | ¡ƒƒS )N)r   r?   r   rD   r   rB   r   r   )r   Zallargs_integerZanyarg_evenr   r   r   r,     s    c             C   s:   t tt t¡| ƒ}t tt t¡| ƒ}t|tt | ¡|ƒƒS )N)r   r?   r   ZsquareZ
invertibler   r   )r   Zallargs_squareZallargs_invertibler   r   r   r,     s    c          	   C   s¢   | j | j }}t |¡t |¡@ t |¡@ t | ¡? t |¡t |¡@ t |¡@ t | ¡? t |¡t |¡@ t |¡@ t | ¡? tt 	| ¡t 	|¡t 
|¡@ ƒgS )N)ÚbaseÚexpr   rG   rB   r@   rC   Únonpositiver   rA   rE   )r   rN   rO   r   r   r   r,   !  s
    &&&c             C   s   | j S )N)Zis_positive)Úor   r   r   Ú<lambda>/  ó    rR   c             C   s   | j S )N)Úis_zero)rQ   r   r   r   rR   0  rS   c             C   s   | j S )N)Zis_negative)rQ   r   r   r   rR   1  rS   c             C   s   | j S )N)Zis_rational)rQ   r   r   r   rR   2  rS   c             C   s   | j S )N)Zis_irrational)rQ   r   r   r   rR   3  rS   c             C   s   | j S )N)Zis_even)rQ   r   r   r   rR   4  rS   c             C   s   | j S )N)Zis_odd)rQ   r   r   r   rR   5  rS   c             C   s   | j S )N)Zis_imaginary)rQ   r   r   r   rR   6  rS   c             C   s   | j S )N)Zis_prime)rQ   r   r   r   rR   7  rS   c             C   s   | j S )N)Zis_composite)rQ   r   r   r   rR   8  rS   c             C   sF   g }x<t  ¡ D ]0\}}|| ƒ}|| ƒ}|d k	r| t||ƒ¡ qW |S )N)Ú_old_assump_gettersÚitemsÚappendr   )r   r8   ÚpÚgetterÚpredÚpropr   r   r   r,   ;  s    N)*Úcollectionsr   Zsympy.assumptions.askr   Z
sympy.corer   r   r   r   r   r	   Zsympy.core.numbersr
   Z$sympy.functions.elementary.complexesr   Zsympy.logic.boolalgr   r   r   r   Zsympy.matrices.expressionsr   r   r   r#   r$   Zclass_fact_registryr?   r/   r,   r-   rE   rA   rF   rH   rI   rB   rC   rM   rL   Z	compositerU   r   r   r   r   Ú<module>   sB    !Y
			








