o
    jgK                     @   s   d Z ddlmZmZmZ ddl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mZmZ ddlmZ ddlmZ dd	lmZmZmZ G d
d dZe Zdd ZdefddZdd Z dd Z!dd Z"ddl#m$Z$m%Z% dS )z4Module for querying SymPy objects about assumptions.    )global_assumptions	PredicateAppliedPredicate)CNF
EncodedCNFLiteral)sympify)BooleanKind)EqNeGtLtGeLe)satisfiable)memoize_property)sympy_deprecation_warningSymPyDeprecationWarningignore_warningsc                   @   s  e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Zed4d5 Zed6d7 Zed8d9 Z ed:d; Z!ed<d= Z"ed>d? Z#ed@dA Z$edBdC Z%edDdE Z&edFdG Z'edHdI Z(edJdK Z)edLdM Z*edNdO Z+edPdQ Z,edRdS Z-edTdU Z.edVdW Z/edXdY Z0edZd[ Z1ed\d] Z2ed^d_ Z3ed`da Z4edbdc Z5eddde Z6edfdg Z7edhdi Z8edjdk Z9edldm Z:edndo Z;edpdq Z<drS )sAssumptionKeyszy
    This class contains all the supported keys by ``ask``.
    It should be accessed via the instance ``sympy.Q``.

    c                 C      ddl m} | S )N   )HermitianPredicate)handlers.setsr   )selfr    r   M/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/assumptions/ask.py	hermitian       zAssumptionKeys.hermitianc                 C   r   )Nr   )AntihermitianPredicate)r   r   )r   r   r   r   r   antihermitian%   r   zAssumptionKeys.antihermitianc                 C   r   )Nr   )RealPredicate)r   r!   )r   r!   r   r   r   real*   r   zAssumptionKeys.realc                 C   r   )Nr   )ExtendedRealPredicate)r   r#   )r   r#   r   r   r   extended_real/   r   zAssumptionKeys.extended_realc                 C   r   )Nr   )ImaginaryPredicate)r   r%   )r   r%   r   r   r   	imaginary4   r   zAssumptionKeys.imaginaryc                 C   r   )Nr   )ComplexPredicate)r   r'   )r   r'   r   r   r   complex9   r   zAssumptionKeys.complexc                 C   r   )Nr   )AlgebraicPredicate)r   r)   )r   r)   r   r   r   	algebraic>   r   zAssumptionKeys.algebraicc                 C   r   )Nr   )TranscendentalPredicate)predicates.setsr+   )r   r+   r   r   r   transcendentalC   r   zAssumptionKeys.transcendentalc                 C   r   )Nr   )IntegerPredicate)r   r.   )r   r.   r   r   r   integerH   r   zAssumptionKeys.integerc                 C   r   )Nr   )NonIntegerPredicate)r,   r0   )r   r0   r   r   r   
nonintegerM   r   zAssumptionKeys.nonintegerc                 C   r   )Nr   )RationalPredicate)r   r2   )r   r2   r   r   r   rationalR   r   zAssumptionKeys.rationalc                 C   r   )Nr   )IrrationalPredicate)r   r4   )r   r4   r   r   r   
irrationalW   r   zAssumptionKeys.irrationalc                 C   r   )Nr   )FinitePredicate)handlers.calculusr6   )r   r6   r   r   r   finite\   r   zAssumptionKeys.finitec                 C   r   )Nr   )InfinitePredicate)r7   r9   )r   r9   r   r   r   infinitea   r   zAssumptionKeys.infinitec                 C   r   )Nr   )PositiveInfinitePredicate)r7   r;   )r   r;   r   r   r   positive_infinitef   r   z AssumptionKeys.positive_infinitec                 C   r   )Nr   )NegativeInfinitePredicate)r7   r=   )r   r=   r   r   r   negative_infinitek   r   z AssumptionKeys.negative_infinitec                 C   r   )Nr   )PositivePredicate)handlers.orderr?   )r   r?   r   r   r   positivep   r   zAssumptionKeys.positivec                 C   r   )Nr   )NegativePredicate)r@   rB   )r   rB   r   r   r   negativeu   r   zAssumptionKeys.negativec                 C   r   )Nr   )ZeroPredicate)r@   rD   )r   rD   r   r   r   zeroz   r   zAssumptionKeys.zeroc                 C   r   )Nr   )ExtendedPositivePredicate)r@   rF   )r   rF   r   r   r   extended_positive   r   z AssumptionKeys.extended_positivec                 C   r   )Nr   )ExtendedNegativePredicate)r@   rH   )r   rH   r   r   r   extended_negative   r   z AssumptionKeys.extended_negativec                 C   r   )Nr   )NonZeroPredicate)r@   rJ   )r   rJ   r   r   r   nonzero   r   zAssumptionKeys.nonzeroc                 C   r   )Nr   )NonPositivePredicate)r@   rL   )r   rL   r   r   r   nonpositive   r   zAssumptionKeys.nonpositivec                 C   r   )Nr   )NonNegativePredicate)r@   rN   )r   rN   r   r   r   nonnegative   r   zAssumptionKeys.nonnegativec                 C   r   )Nr   )ExtendedNonZeroPredicate)r@   rP   )r   rP   r   r   r   extended_nonzero   r   zAssumptionKeys.extended_nonzeroc                 C   r   )Nr   )ExtendedNonPositivePredicate)r@   rR   )r   rR   r   r   r   extended_nonpositive   r   z#AssumptionKeys.extended_nonpositivec                 C   r   )Nr   )ExtendedNonNegativePredicate)r@   rT   )r   rT   r   r   r   extended_nonnegative   r   z#AssumptionKeys.extended_nonnegativec                 C   r   )Nr   )EvenPredicate)handlers.ntheoryrV   )r   rV   r   r   r   even   r   zAssumptionKeys.evenc                 C   r   )Nr   )OddPredicate)rW   rY   )r   rY   r   r   r   odd   r   zAssumptionKeys.oddc                 C   r   )Nr   )PrimePredicate)rW   r[   )r   r[   r   r   r   prime   r   zAssumptionKeys.primec                 C   r   )Nr   )CompositePredicate)rW   r]   )r   r]   r   r   r   	composite   r   zAssumptionKeys.compositec                 C   r   )Nr   )CommutativePredicate)handlers.commonr_   )r   r_   r   r   r   commutative   r   zAssumptionKeys.commutativec                 C   r   )Nr   )IsTruePredicate)r`   rb   )r   rb   r   r   r   is_true   r   zAssumptionKeys.is_truec                 C   r   )Nr   )SymmetricPredicate)handlers.matricesrd   )r   rd   r   r   r   	symmetric   r   zAssumptionKeys.symmetricc                 C   r   )Nr   )InvertiblePredicate)re   rg   )r   rg   r   r   r   
invertible   r   zAssumptionKeys.invertiblec                 C   r   )Nr   )OrthogonalPredicate)re   ri   )r   ri   r   r   r   
orthogonal   r   zAssumptionKeys.orthogonalc                 C   r   )Nr   )UnitaryPredicate)re   rk   )r   rk   r   r   r   unitary   r   zAssumptionKeys.unitaryc                 C   r   )Nr   )PositiveDefinitePredicate)re   rm   )r   rm   r   r   r   positive_definite   r   z AssumptionKeys.positive_definitec                 C   r   )Nr   )UpperTriangularPredicate)re   ro   )r   ro   r   r   r   upper_triangular   r   zAssumptionKeys.upper_triangularc                 C   r   )Nr   )LowerTriangularPredicate)re   rq   )r   rq   r   r   r   lower_triangular   r   zAssumptionKeys.lower_triangularc                 C   r   )Nr   )DiagonalPredicate)re   rs   )r   rs   r   r   r   diagonal   r   zAssumptionKeys.diagonalc                 C   r   )Nr   )FullRankPredicate)re   ru   )r   ru   r   r   r   fullrank   r   zAssumptionKeys.fullrankc                 C   r   )Nr   )SquarePredicate)re   rw   )r   rw   r   r   r   square   r   zAssumptionKeys.squarec                 C   r   )Nr   )IntegerElementsPredicate)re   ry   )r   ry   r   r   r   integer_elements   r   zAssumptionKeys.integer_elementsc                 C   r   )Nr   )RealElementsPredicate)re   r{   )r   r{   r   r   r   real_elements   r   zAssumptionKeys.real_elementsc                 C   r   )Nr   )ComplexElementsPredicate)re   r}   )r   r}   r   r   r   complex_elements  r   zAssumptionKeys.complex_elementsc                 C   r   )Nr   )SingularPredicate)predicates.matricesr   )r   r   r   r   r   singular  r   zAssumptionKeys.singularc                 C   r   )Nr   )NormalPredicate)r   r   )r   r   r   r   r   normal  r   zAssumptionKeys.normalc                 C   r   )Nr   )TriangularPredicate)r   r   )r   r   r   r   r   
triangular  r   zAssumptionKeys.triangularc                 C   r   )Nr   )UnitTriangularPredicate)r   r   )r   r   r   r   r   unit_triangular  r   zAssumptionKeys.unit_triangularc                 C   r   )Nr   )EqualityPredicate)relation.equalityr   )r   r   r   r   r   eq  r   zAssumptionKeys.eqc                 C   r   )Nr   )UnequalityPredicate)r   r   )r   r   r   r   r   ne  r   zAssumptionKeys.nec                 C   r   )Nr   )StrictGreaterThanPredicate)r   r   )r   r   r   r   r   gt$  r   zAssumptionKeys.gtc                 C   r   )Nr   )GreaterThanPredicate)r   r   )r   r   r   r   r   ge)  r   zAssumptionKeys.gec                 C   r   )Nr   )StrictLessThanPredicate)r   r   )r   r   r   r   r   lt.  r   zAssumptionKeys.ltc                 C   r   )Nr   )LessThanPredicate)r   r   )r   r   r   r   r   le3  r   zAssumptionKeys.leN)=__name__
__module____qualname____doc__r   r   r    r"   r$   r&   r(   r*   r-   r/   r1   r3   r5   r8   r:   r<   r>   rA   rC   rE   rG   rI   rK   rM   rO   rQ   rS   rU   rX   rZ   r\   r^   ra   rc   rf   rh   rj   rl   rn   rp   rr   rt   rv   rx   rz   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    























































r   c                 C   s   t  }| jD ]5}g }|D ]%}t|jtr0t|jjdkr0|jj|v r.|t	|jj
|j q n n	|r;|t| qt|S )a  
    Extract all relevant assumptions from *assump* with respect to given *exprs*.

    Parameters
    ==========

    assump : sympy.assumptions.cnf.CNF

    exprs : tuple of expressions

    Returns
    =======

    sympy.assumptions.cnf.CNF

    Examples
    ========

    >>> from sympy import Q
    >>> from sympy.assumptions.cnf import CNF
    >>> from sympy.assumptions.ask import _extract_all_facts
    >>> from sympy.abc import x, y
    >>> assump = CNF.from_prop(Q.positive(x) & Q.integer(y))
    >>> exprs = (x,)
    >>> cnf = _extract_all_facts(assump, exprs)
    >>> cnf.clauses
    {frozenset({Literal(Q.positive, False)})}

    r   )setclauses
isinstancelitr   len	argumentsargappendr   functionis_Notadd	frozensetr   )assumpexprsfactsclauseargsliteralr   r   r   _extract_all_facts;  s   
r   Tc                 C   s  ddl m} ddlm} ddlm} t| } t|}t| ts$| j	t
ur(tdt|ts2|j	t
ur6tdttjttjttjttjttjttji}t| trW| j| j}}n| j|v rg|t|  | j}}ntj| f}}t !|}	|	"| t#|	|}
t$ }t% }|&t | |'|
 |
j(rt)|du rt*d| t+||
}|d	ur|S || ,|}|d	urt-|S || ||d
}|d	ur|S z
|| ||d
}W |S  |y   Y d	S w )ay	  
    Function to evaluate the proposition with assumptions.

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

    This function evaluates the proposition to ``True`` or ``False`` if
    the truth value can be determined. If not, it returns ``None``.

    It should be discerned from :func:`~.refine` which, when applied to a
    proposition, simplifies the argument to symbolic ``Boolean`` instead of
    Python built-in ``True``, ``False`` or ``None``.

    **Syntax**

        * ask(proposition)
            Evaluate the *proposition* in global assumption context.

        * ask(proposition, assumptions)
            Evaluate the *proposition* with respect to *assumptions* in
            global assumption context.

    Parameters
    ==========

    proposition : Boolean
        Proposition which will be evaluated to boolean value. If this is
        not ``AppliedPredicate``, it will be wrapped by ``Q.is_true``.

    assumptions : Boolean, optional
        Local assumptions to evaluate the *proposition*.

    context : AssumptionsContext, optional
        Default assumptions to evaluate the *proposition*. By default,
        this is ``sympy.assumptions.global_assumptions`` variable.

    Returns
    =======

    ``True``, ``False``, or ``None``

    Raises
    ======

    TypeError : *proposition* or *assumptions* is not valid logical expression.

    ValueError : assumptions are inconsistent.

    Examples
    ========

    >>> from sympy import ask, Q, pi
    >>> from sympy.abc import x, y
    >>> ask(Q.rational(pi))
    False
    >>> ask(Q.even(x*y), Q.even(x) & Q.integer(y))
    True
    >>> ask(Q.prime(4*x), Q.integer(x))
    False

    If the truth value cannot be determined, ``None`` will be returned.

    >>> print(ask(Q.odd(3*x))) # cannot determine unless we know x
    None

    ``ValueError`` is raised if assumptions are inconsistent.

    >>> ask(Q.integer(x), Q.even(x) & Q.odd(x))
    Traceback (most recent call last):
      ...
    ValueError: inconsistent assumptions Q.even(x) & Q.odd(x)

    Notes
    =====

    Relations in assumptions are not implemented (yet), so the following
    will not give a meaningful result.

    >>> ask(Q.positive(x), x > 0)

    It is however a work in progress.

    See Also
    ========

    sympy.assumptions.refine.refine : Simplification using assumptions.
        Proposition is not reduced to ``None`` if the truth value cannot
        be determined.
    r   )satask)
lra_satask)UnhandledInputz.proposition must be a valid logical expressionz.assumptions must be a valid logical expressionFzinconsistent assumptions %sN)assumptionscontext).sympy.assumptions.sataskr   sympy.assumptions.lra_sataskr   !sympy.logic.algorithms.lra_theoryr   r   r   r   kindr	   	TypeErrorr
   Qr   r   r   r   r   r   r   r   r   r   r   r   r   r   functyper   rc   r   	from_propextendr   get_all_known_factsr   from_cnfadd_from_cnfr   r   
ValueError_ask_single_fact	_eval_askbool)propositionr   r   r   r   r   binrelpredskeyr   
assump_cnflocal_factsknown_facts_cnfenc_cnfresr   r   r   asko  sN   Z(






r   c           	      C   s   |j rgt }t|j dkr5|j \}t|dkr5|\}|| d}|dur(|d nt }|jr5|j|v r5dS |j D ].}t|dkrf|\}|jsM||jdnd}|du rTq8|\}}| |v r_ dS | |v rf dS q8dS )a  
    Compute the truth value of single predicate using assumptions.

    Parameters
    ==========

    key : sympy.assumptions.assume.Predicate
        Proposition predicate.

    local_facts : sympy.assumptions.cnf.CNF
        Local assumption in CNF form.

    Returns
    =======

    ``True``, ``False`` or ``None``

    Examples
    ========

    >>> from sympy import Q
    >>> from sympy.assumptions.cnf import CNF
    >>> from sympy.assumptions.ask import _ask_single_fact

    If prerequisite of proposition is rejected by the assumption,
    return ``False``.

    >>> key, assump = Q.zero, ~Q.zero
    >>> local_facts = CNF.from_prop(assump)
    >>> _ask_single_fact(key, local_facts)
    False
    >>> key, assump = Q.zero, ~Q.even
    >>> local_facts = CNF.from_prop(assump)
    >>> _ask_single_fact(key, local_facts)
    False

    If assumption implies the proposition, return ``True``.

    >>> key, assump = Q.even, Q.zero
    >>> local_facts = CNF.from_prop(assump)
    >>> _ask_single_fact(key, local_facts)
    True

    If proposition rejects the assumption, return ``False``.

    >>> key, assump = Q.even, Q.odd
    >>> local_facts = CNF.from_prop(assump)
    >>> _ask_single_fact(key, local_facts)
    False
    r   Nr   FT)r   get_known_facts_dictr   getr   r   r   )	r   r   known_facts_dictclf
prop_factsprop_reqr   prop_rejr   r   r   r     s.   3
r   c                 C   s\   t dddd t| tr| jj} tt| d}|dur!|| dS tt| t| |gd dS )z
    Register a handler in the ask system. key must be a string and handler a
    class inheriting from AskHandler.

    .. deprecated:: 1.8.
        Use multipledispatch handler instead. See :obj:`~.Predicate`.

    z
        The AskHandler system is deprecated. The register_handler() function
        should be replaced with the multipledispatch handler of Predicate.
        1.8deprecated-askhandlerdeprecated_since_versionactive_deprecations_targetN)handlers)r   r   r   namegetattrr   add_handlersetattr)r   handlerQkeyr   r   r   register_handlerY  s   	
r   c                 C   s^   t dddd t| tr| jj} tt tt| | W d   dS 1 s(w   Y  dS )z
    Removes a handler from the ask system.

    .. deprecated:: 1.8.
        Use multipledispatch handler instead. See :obj:`~.Predicate`.

    z
        The AskHandler system is deprecated. The remove_handler() function
        should be replaced with the multipledispatch handler of Predicate.
        r   r   r   N)	r   r   r   r   r   r   r   r   remove_handler)r   r   r   r   r   r   s  s   

"r   )r   r   N)&r   sympy.assumptions.assumer   r   r   sympy.assumptions.cnfr   r   r   
sympy.corer   sympy.core.kindr	   sympy.core.relationalr
   r   r   r   r   r   sympy.logic.inferencer   sympy.utilities.decoratorr   sympy.utilities.exceptionsr   r   r   r   r   r   r   r   r   r   sympy.assumptions.ask_generatedr   r   r   r   r   r   <module>   s(     
  &4 S