
    wg+\                     0   d Z ddlmZ ddlmZmZ ddlmZ ddlmZ	 ddl
mZ defd	Zd
 Z e       Zej                   j#                         Zej'                  d        ee      ZddZddZd ZddZ G d de      Zd Zd Zd Zd Z G d de      Zy)a  
This module contains the machinery handling assumptions.
Do also consider the guide :ref:`assumptions-guide`.

All symbolic objects have assumption attributes that can be accessed via
``.is_<assumption name>`` attribute.

Assumptions determine certain properties of symbolic objects and can
have 3 possible values: ``True``, ``False``, ``None``.  ``True`` is returned if the
object has the property and ``False`` is returned if it does not or cannot
(i.e. does not make sense):

    >>> from sympy import I
    >>> I.is_algebraic
    True
    >>> I.is_real
    False
    >>> I.is_prime
    False

When the property cannot be determined (or when a method is not
implemented) ``None`` will be returned. For example,  a generic symbol, ``x``,
may or may not be positive so a value of ``None`` is returned for ``x.is_positive``.

By default, all symbolic values are in the largest set in the given context
without specifying the property. For example, a symbol that has a property
being integer, is also real, complex, etc.

Here follows a list of possible assumption names:

.. glossary::

    commutative
        object commutes with any other object with
        respect to multiplication operation. See [12]_.

    complex
        object can have only values from the set
        of complex numbers. See [13]_.

    imaginary
        object value is a number that can be written as a real
        number multiplied by the imaginary unit ``I``.  See
        [3]_.  Please note that ``0`` is not considered to be an
        imaginary number, see
        `issue #7649 <https://github.com/sympy/sympy/issues/7649>`_.

    real
        object can have only values from the set
        of real numbers.

    extended_real
        object can have only values from the set
        of real numbers, ``oo`` and ``-oo``.

    integer
        object can have only values from the set
        of integers.

    odd
    even
        object can have only values from the set of
        odd (even) integers [2]_.

    prime
        object is a natural number greater than 1 that has
        no positive divisors other than 1 and itself.  See [6]_.

    composite
        object is a positive integer that has at least one positive
        divisor other than 1 or the number itself.  See [4]_.

    zero
        object has the value of 0.

    nonzero
        object is a real number that is not zero.

    rational
        object can have only values from the set
        of rationals.

    algebraic
        object can have only values from the set
        of algebraic numbers [11]_.

    transcendental
        object can have only values from the set
        of transcendental numbers [10]_.

    irrational
        object value cannot be represented exactly by :class:`~.Rational`, see [5]_.

    finite
    infinite
        object absolute value is bounded (arbitrarily large).
        See [7]_, [8]_, [9]_.

    negative
    nonnegative
        object can have only negative (nonnegative)
        values [1]_.

    positive
    nonpositive
        object can have only positive (nonpositive) values.

    extended_negative
    extended_nonnegative
    extended_positive
    extended_nonpositive
    extended_nonzero
        as without the extended part, but also including infinity with
        corresponding sign, e.g., extended_positive includes ``oo``

    hermitian
    antihermitian
        object belongs to the field of Hermitian
        (antihermitian) operators.

Examples
========

    >>> from sympy import Symbol
    >>> x = Symbol('x', real=True); x
    x
    >>> x.is_real
    True
    >>> x.is_complex
    True

See Also
========

.. seealso::

    :py:class:`sympy.core.numbers.ImaginaryUnit`
    :py:class:`sympy.core.numbers.Zero`
    :py:class:`sympy.core.numbers.One`
    :py:class:`sympy.core.numbers.Infinity`
    :py:class:`sympy.core.numbers.NegativeInfinity`
    :py:class:`sympy.core.numbers.ComplexInfinity`

Notes
=====

The fully-resolved assumptions for any SymPy expression
can be obtained as follows:

    >>> from sympy.core.assumptions import assumptions
    >>> x = Symbol('x',positive=True)
    >>> assumptions(x + I)
    {'commutative': True, 'complex': True, 'composite': False, 'even':
    False, 'extended_negative': False, 'extended_nonnegative': False,
    'extended_nonpositive': False, 'extended_nonzero': False,
    'extended_positive': False, 'extended_real': False, 'finite': True,
    'imaginary': False, 'infinite': False, 'integer': False, 'irrational':
    False, 'negative': False, 'noninteger': False, 'nonnegative': False,
    'nonpositive': False, 'nonzero': False, 'odd': False, 'positive':
    False, 'prime': False, 'rational': False, 'real': False, 'zero':
    False}

Developers Notes
================

The current (and possibly incomplete) values are stored
in the ``obj._assumptions dictionary``; queries to getter methods
(with property decorators) or attributes of objects/classes
will return values and update the dictionary.

    >>> eq = x**2 + I
    >>> eq._assumptions
    {}
    >>> eq.is_finite
    True
    >>> eq._assumptions
    {'finite': True, 'infinite': False}

For a :class:`~.Symbol`, there are two locations for assumptions that may
be of interest. The ``assumptions0`` attribute gives the full set of
assumptions derived from a given set of initial assumptions. The
latter assumptions are stored as ``Symbol._assumptions_orig``

    >>> Symbol('x', prime=True, even=True)._assumptions_orig
    {'even': True, 'prime': True}

The ``_assumptions_orig`` are not necessarily canonical nor are they filtered
in any way: they records the assumptions used to instantiate a Symbol and (for
storage purposes) represent a more compact representation of the assumptions
needed to recreate the full set in ``Symbol.assumptions0``.


References
==========

.. [1] https://en.wikipedia.org/wiki/Negative_number
.. [2] https://en.wikipedia.org/wiki/Parity_%28mathematics%29
.. [3] https://en.wikipedia.org/wiki/Imaginary_number
.. [4] https://en.wikipedia.org/wiki/Composite_number
.. [5] https://en.wikipedia.org/wiki/Irrational_number
.. [6] https://en.wikipedia.org/wiki/Prime_number
.. [7] https://en.wikipedia.org/wiki/Finite
.. [8] https://docs.python.org/3/library/math.html#math.isfinite
.. [9] https://numpy.org/doc/stable/reference/generated/numpy.isfinite.html
.. [10] https://en.wikipedia.org/wiki/Transcendental_number
.. [11] https://en.wikipedia.org/wiki/Algebraic_number
.. [12] https://en.wikipedia.org/wiki/Commutative_property
.. [13] https://en.wikipedia.org/wiki/Complex_number

    )sympy_deprecation_warning   )	FactRulesFactKB)sympify)_assumptions_shuffle)generated_assumptionsreturnc                  8    t        j                  t              } | S )z Load the assumption rules from pre-generated data

    To update the pre-generated data, see :method::`_generate_assumption_rules`
    )r   _from_python_assumptions_assume_ruless    [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/core/assumptions.py$_load_pre_generated_assumption_rulesr      s    
 ((6M    c                       t        g d      } | S )z Generate the default assumption rules

    This method should only be called to update the pre-generated
    assumption rules.

    To update the pre-generated assumptions run: bin/ask_update.py

    )+zinteger        ->  rationalzrational       ->  realzrational       ->  algebraiczalgebraic      ->  complexz'transcendental ==  complex & !algebraiczreal           ->  hermitianzimaginary      ->  complexz imaginary      ->  antihermitianzextended_real  ->  commutativezcomplex        ->  commutativezcomplex        ->  finitez"odd            ==  integer & !evenz!even           ==  integer & !oddzreal           ->  complexz"extended_real  ->  real | infinitez)real           ==  extended_real & finitezEextended_real        ==  extended_negative | zero | extended_positivez@extended_negative    ==  extended_nonpositive & extended_nonzeroz@extended_positive    ==  extended_nonnegative & extended_nonzeroz;extended_nonpositive ==  extended_real & !extended_positivez;extended_nonnegative ==  extended_real & !extended_negativez-real           ==  negative | zero | positivez(negative       ==  nonpositive & nonzeroz(positive       ==  nonnegative & nonzeroz#nonpositive    ==  real & !positivez#nonnegative    ==  real & !negativez-positive       ==  extended_positive & finitez-negative       ==  extended_negative & finitez0nonpositive    ==  extended_nonpositive & finitez0nonnegative    ==  extended_nonnegative & finitez,nonzero        ==  extended_nonzero & finitez zero           ->  even & finitez>zero           ==  extended_nonnegative & extended_nonpositivez,zero           ==  nonnegative & nonpositiveznonzero        ->  realz%prime          ->  integer & positivez.composite      ->  integer & positive & !primez,!composite     ->  !positive | !even | primez#irrational     ==  real & !rationalz!imaginary      ->  !extended_realzinfinite       ==  !finitez+noninteger     ==  extended_real & !integerz)extended_nonzero == extended_real & !zero)r   r   s    r   _generate_assumption_rulesr      s      9 9Mt r   polarNc                    t        |       }|j                  r8|j                  }|(t        |      t        |      z  D ci c]  }|||   
 }}|S i }|t        n|D ]%  }t        |dj                  |            }|!|||<   ' |S c c}w )z&return the T/F assumptions of ``expr``zis_{})r   	is_Symbolassumptions0set_assume_definedgetattrformat)expr_checknrvkvs         r   assumptionsr#   0  s    A{{^^$'Gc&k$9:q!RU(:B:		B &_F Aw~~a()=BqE I ;s   Bc                    |t         n
t        |      }|r| si S t        |       D cg c]  }t        ||       }}t	        |      D ](  \  }}t        |      |z  D ci c]  }|||   
 c}||<   * t        j
                  |D cg c]  }t        |       c} }|d   |D ci c]  t        fd|D              s     c}S c c}w c c}w c c}w c c}w )aM  return those assumptions which have the same True or False
    value for all the given expressions.

    Examples
    ========

    >>> from sympy.core import common_assumptions
    >>> from sympy import oo, pi, sqrt
    >>> common_assumptions([-4, 0, sqrt(2), 2, pi, oo])
    {'commutative': True, 'composite': False,
    'extended_real': True, 'imaginary': False, 'odd': False}

    By default, all assumptions are tested; pass an iterable of the
    assumptions to limit those that are reported:

    >>> common_assumptions([0, 1, 2], ['positive', 'integer'])
    {'integer': True}
    )r   r   c              3   4   K   | ]  }   |   k(    y wN ).0bar!   s     r   	<genexpr>z%common_assumptions.<locals>.<genexpr>`  s%      + ,-Q41Q4< +s   )r   r   r   r#   	enumerateintersectionall)exprscheckiassumeer!   commonr*   s        ` @r   common_assumptionsr5   @  s    &  %}O#e*E	 5<ENCqk!E*CFC&! 61&)!fun5Q!W5q	6 71A78Fq	A# s ++ (AqtG   D 67s   C CC(CCc                 h    t        |       } i }|D ]  }t        | d|z  d      }|||   us|||<   ! |S )a(  
    Return a dictionary containing assumptions with values not
    matching those of the passed assumptions.

    Examples
    ========

    >>> from sympy import failing_assumptions, Symbol

    >>> x = Symbol('x', positive=True)
    >>> y = Symbol('y')
    >>> failing_assumptions(6*x + y, positive=True)
    {'positive': None}

    >>> failing_assumptions(x**2 - 1, positive=True)
    {'positive': None}

    If *expr* satisfies all of the assumptions, an empty dictionary is returned.

    >>> failing_assumptions(x**2, positive=True)
    {}

    is_%sN)r   r   )r   r#   failedr!   tests        r   failing_assumptionsr:   d  sO    0 4=DF tWq[$/{1~%F1I Mr   c                     t        |       } ||rt        d      t        |      }d}|j                         D ]$  \  }}|	t	        | d|z   d      }|d}||k7  s$ y |S )a  
    Checks whether assumptions of ``expr`` match the T/F assumptions
    given (or possessed by ``against``). True is returned if all
    assumptions match; False is returned if there is a mismatch and
    the assumption in ``expr`` is not None; else None is returned.

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

    *assume* is a dict of assumptions with True or False values

    Examples
    ========

    >>> from sympy import Symbol, pi, I, exp, check_assumptions
    >>> check_assumptions(-5, integer=True)
    True
    >>> check_assumptions(pi, real=True, integer=False)
    True
    >>> check_assumptions(pi, negative=True)
    False
    >>> check_assumptions(exp(I*pi/7), real=False)
    True
    >>> x = Symbol('x', positive=True)
    >>> check_assumptions(2*x + 1, positive=True)
    True
    >>> check_assumptions(-2*x - 5, positive=True)
    False

    To check assumptions of *expr* against another variable or expression,
    pass the expression or variable as ``against``.

    >>> check_assumptions(2*x + 1, x)
    True

    To see if a number matches the assumptions of an expression, pass
    the number as the first argument, else its specific assumptions
    may not have a non-None value in the expression:

    >>> check_assumptions(x, 3)
    >>> check_assumptions(3, x)
    True

    ``None`` is returned if ``check_assumptions()`` could not conclude.

    >>> check_assumptions(2*x - 1, x)

    >>> z = Symbol('z')
    >>> check_assumptions(z, real=True)

    See Also
    ========

    failing_assumptions

    Nz*Expecting `against` or `assume`, not both.Tis_F)r   
ValueErrorr#   itemsr   )r   againstr2   knownr!   r"   r3   s          r   check_assumptionsrA     s    r 4=D<> >W%E 19D%!)T*9E!V Lr   c                   :     e Zd ZdZd fd	Zd Zed        Z xZS )	StdFactKBztA FactKB specialized for the built-in rules

    This is the only kind of FactKB that Basic objects should use.
    c                     t         |   t               |si | _        n7t	        |t
              s|j                         | _        n|j                  | _        |r| j                  |       y y r&   )	super__init__r   
_generator
isinstancer   copy	generatordeduce_all_facts)selffacts	__class__s     r   rF   zStdFactKB.__init__  sR    ' DOE6*#jjlDO#ooDO!!%( r   c                 $    | j                  |       S r&   rN   rL   s    r   rI   zStdFactKB.copy  s    ~~d##r   c                 6    | j                   j                         S r&   )rG   rI   rQ   s    r   rJ   zStdFactKB.generator  s    ##%%r   r&   )	__name__
__module____qualname____doc__rF   rI   propertyrJ   __classcell__rP   s   @r   rC   rC     s&    
)$ & &r   rC   c                     d| z  S )z=Convert a fact name to the name of the corresponding propertyr7   r'   )facts    r   as_propertyr[     s    T>r   c                 D      fd}t               |_        t        |      S )z6Create the automagic property corresponding to a fact.c                     	 | j                      S # t        $ rF | j                   | j                  u r| j                  j                         | _         t	        |       cY S w xY wr&   )r   KeyErrordefault_assumptionsrI   _ask)rL   rZ   s    r   getitzmake_property.<locals>.getit  s`    	$$$T** 	$  D$<$<<$($<$<$A$A$C!d##	$s    AA! A!)r[   	func_namerW   )rZ   ra   s   ` r   make_propertyrc     s     $ "$'EOE?r   c                    |j                   }|j                  }| g}| h}|D ]  }||v rd}|j                  |      }| ||      }||j                  ||ff       |j                  |       }	|	|	c S t	        t
        j                  |   |z
        }
t        |
       |j                  |
       |j                  |
        | |v r||    S |j                  | d       y)a  
    Find the truth value for a property of an object.

    This function is called when a request is made to see what a fact
    value is.

    For this we use several techniques:

    First, the fact-evaluation function is tried, if it exists (for
    example _eval_is_integer). Then we try related facts. For example

        rational   -->   integer

    another example is joined rule:

        integer & !odd  --> even

    so in the latter case if we are looking at what 'even' value is,
    'integer' and 'odd' facts will be asked.

    In all cases, when we settle on some fact value, its implications are
    deduced, and the result is cached in ._assumptions.
    N)r   _prop_handlergetrK   listr   prereqshuffleextendupdate_tell)rZ   objr#   handler_mapfacts_to_checkfacts_queuedfact_ifact_i_value	handler_i
fact_valuenew_facts_to_checks              r   r`   r`     s   2 ""K ##K VN6L ! -0 [  OOF+	 $S>L #((6<*@)BC !__T*
! "-"6"6v">"MN"#01./[-0l {4   dD!r   c           	         i }t         D ]\  }t        |      }| j                  j                  |d      }t	        |t
        t        t        d      f      sK|t        |      }|||<   ^ i }t        | j                        D ]#  }t        |dd      }||j                  |       % |j                  |       || _        t        |      | _        i | _        t         D ]$  }t        | d|z  d      }||| j                  |<   & | j                  j!                         D ]  \  }}t#        | t        |      |        t%               }	| j                  D ]#  }t        |dd      }
|
|	j                  |
       % |	t%        | j                        z
  D ]2  }t        |      }|| j                  vst#        | |t'        |             4 t         D ]0  }t        |      }t)        | |      rt#        | |t'        |             2 y)zPrecompute class level assumptions and generate handlers.

    This is called by Basic.__init_subclass__ each time a Basic subclass is
    defined.
     N_explicit_class_assumptionsz_eval_is_%sr_   )r   r[   __dict__rf   rH   boolinttypereversed	__bases__r   rk   rx   rC   r_   re   r>   setattrr   rc   hasattr)cls
local_defsr!   attrnamer"   defsbaser#   eval_is_methderived_from_basesr_   rZ   pnames                r   _prepare_class_assumptionsr   f  s    J q>LLXr*a$T$Z01}GJqM D' %d$A4H"KK$% 	KK
&*C#'oCC 0sMA$5t<##/Ca 0 ''--/ (1[^Q'(  ;%d,A4H*%%&9:	; #S)@)@%AA 5D!$Cd 345   5D!sE"Cd 345r   c                       e Zd Zd Zy)ManagedPropertiesc                 :    d}t        |dd       t        |        y )NzHThe ManagedProperties metaclass. Basic does not use metaclasses any morez1.12managedproperties)deprecated_since_versionactive_deprecations_target)r   r   )r   argskwargsmsgs       r   rF   zManagedProperties.__init__  s#    9!#%+':	< 	#3'r   N)rS   rT   rU   rF   r'   r   r   r   r     s    (r   r   r&   ) rV   sympy.utilities.exceptionsr   rM   r   r   r   sympy.core.randomr   ri    sympy.core.assumptions_generatedr	   r   r   r   r   defined_factsrI   r   add	frozensetr#   r5   r:   rA   rC   r[   rc   r`   r   r|   r   r'   r   r   <module>r      s   Qf A $  = Ri CL 56--224   G O, !HBHV& &2
fR55D( (r   