
    EdJ                         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lmZmZ d dlmZ d dlmZmZ  G d	 d
e          ZeZdS )    )SsympifyExprDummyAddMul)cacheit)Tuple)Function	PoleErrorexpand_power_base
expand_logdefault_sort_key)explog)
Complement)uniqis_sequencec                       e Zd ZdZdZdZed             ZddZe	d             Z
e	d             Ze	d	             Ze	d
             Zd Zd Zd Zd Zed             Zd Zd Zd Zd Zd Zd ZdS )Ordera   Represents the limiting behavior of some function.

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

    The order of a function characterizes the function based on the limiting
    behavior of the function as it goes to some limit. Only taking the limit
    point to be a number is currently supported. This is expressed in
    big O notation [1]_.

    The formal definition for the order of a function `g(x)` about a point `a`
    is such that `g(x) = O(f(x))` as `x \rightarrow a` if and only if for any
    `\delta > 0` there exists a `M > 0` such that `|g(x)| \leq M|f(x)|` for
    `|x-a| < \delta`.  This is equivalent to `\lim_{x \rightarrow a}
    \sup |g(x)/f(x)| < \infty`.

    Let's illustrate it on the following example by taking the expansion of
    `\sin(x)` about 0:

    .. math ::
        \sin(x) = x - x^3/3! + O(x^5)

    where in this case `O(x^5) = x^5/5! - x^7/7! + \cdots`. By the definition
    of `O`, for any `\delta > 0` there is an `M` such that:

    .. math ::
        |x^5/5! - x^7/7! + ....| <= M|x^5| \text{ for } |x| < \delta

    or by the alternate definition:

    .. math ::
        \lim_{x \rightarrow 0} | (x^5/5! - x^7/7! + ....) / x^5| < \infty

    which surely is true, because

    .. math ::
        \lim_{x \rightarrow 0} | (x^5/5! - x^7/7! + ....) / x^5| = 1/5!


    As it is usually used, the order of a function can be intuitively thought
    of representing all terms of powers greater than the one specified. For
    example, `O(x^3)` corresponds to any terms proportional to `x^3,
    x^4,\ldots` and any higher power. For a polynomial, this leaves terms
    proportional to `x^2`, `x` and constants.

    Examples
    ========

    >>> from sympy import O, oo, cos, pi
    >>> from sympy.abc import x, y

    >>> O(x + x**2)
    O(x)
    >>> O(x + x**2, (x, 0))
    O(x)
    >>> O(x + x**2, (x, oo))
    O(x**2, (x, oo))

    >>> O(1 + x*y)
    O(1, x, y)
    >>> O(1 + x*y, (x, 0), (y, 0))
    O(1, x, y)
    >>> O(1 + x*y, (x, oo), (y, oo))
    O(x*y, (x, oo), (y, oo))

    >>> O(1) in O(1, x)
    True
    >>> O(1, x) in O(1)
    False
    >>> O(x) in O(1, x)
    True
    >>> O(x**2) in O(x)
    True

    >>> O(x)*x
    O(x**2)
    >>> O(x) - O(x)
    O(x)
    >>> O(cos(x))
    O(1)
    >>> O(cos(x), (x, pi/2))
    O(x - pi/2, (x, pi/2))

    References
    ==========

    .. [1] `Big O notation <https://en.wikipedia.org/wiki/Big_O_notation>`_

    Notes
    =====

    In ``O(f(x), x)`` the expression ``f(x)`` is assumed to have a leading
    term.  ``O(f(x), x)`` is automatically transformed to
    ``O(f(x).as_leading_term(x),x)``.

        ``O(expr*f(x), x)`` is ``O(f(x), x)``

        ``O(expr, x)`` is ``O(1)``

        ``O(0, x)`` is 0.

    Multivariate O is also supported:

        ``O(f(x, y), x, y)`` is transformed to
        ``O(f(x, y).as_leading_term(x,y).as_leading_term(y), x, y)``

    In the multivariate case, it is assumed the limits w.r.t. the various
    symbols commute.

    If no symbols are passed then all symbols in the expression are used
    and the limit point is assumed to be zero.

    T c                    t          |          }|sI|j        r|j        }|j        nt	          |j                  }t          j        gt          |          z  nt	          t          |          r|n|g          }g g c}t          |d                   rU|D ]Q}t	          t          t           |                    \  }}|                    |                               |           Rn?t	          t          t           |                    }t          j        gt          |          z  t          d |D                       st          d|z            t          t	          t          |                              t          |          k    rt          d|z            |j        rt!          |j        dd                    }t!          |          t!          t%          |                                                    D ]<\  }}|                                v r||         k    rt+          d          7||<   =t-          |                                          t-                                                    k    r|S t	                                                    }fd|D             |t          j        u rt          j        S t1          fd|D                       rt          d	z            |r!t1          fd
D                       rt+          d          d         t          j        u r7d |D             }	d |	                                D             }
d D             }nd         t          j        u r7d |D             }	d |	                                D             }
d D             }nbd         t          j        ur;fd|D             }	fd|	                                D             }
d D             }nd}	d}
t	                    }|                    |	          }|j        r|                                }|	r,t=          d |
                                D                       }nt=          |          }t          |          dk    r|                                }d }||k    rM|}|j        r*|                     |          }tC          d |D              }n|r	  |j"        | }n_# tF          $ rQ tI          |tJ                    st          d |j        D                       rng }t=          t%          ||                    }|j        D ]V}	  |j"        | }n# tF          $ r |}Y nw xY w||vrtM          |          }ntM          |g|R  }|                    |           W|j        rFtM          tC          | g|R  }|j        r#tM          tC          d |j        D              g|R  }|j'        }nO|j(        rtS          d |D              }n4|j*        r-|j+        }|j,        }tW          |t[          |          z            }Y nw xY w|j.        rt          j        }n |j/        |ddid         }ta          |          }tc          |          }t          |          dk    rK|d         }t	          tS          j2        |/                    |d          d                             }tg          |          D ]\  }}|j*        r|j        \  }}||| fv r%|j4        r|5                    |          s	||z  ||<   B|j*        r>|j+        5                    |          s$|j        \  }}||| fv r|j4        r|||z  z  ||<   |j(        r`|j        d         t          j6        u rG| }|j*        r=|j+        5                    |          s#|j        \  }}||| fv r|j4        r|||z  z  ||<   tS          | }||k    M|                    |
          }|j        r|j'        } |j5        | s|j.        st          j7        }t!          t%          |                    |8                    tr                     fd|D             |ftu          t%          |           z   }tw          j<        | g|R  }|S ) Nr   c              3   $   K   | ]}|j         V  d S N)	is_symbol).0vs     2lib/python3.11/site-packages/sympy/series/order.py	<genexpr>z Order.__new__.<locals>.<genexpr>   s$      2211;222222    z!Variables are not symbols, got %sz3Variables are supposed to be unique symbols, got %s   z2Mixing Order at different points is not supported.c                      g | ]
}|         S r   r   )r   r   new_vps     r   
<listcomp>z!Order.__new__.<locals>.<listcomp>   s    666q666r!   c              3   4   K   | ]}D ]}||j         v V  d S r   )free_symbols)r   xppoints      r   r    z Order.__new__.<locals>.<genexpr>   s7      EEquEE!qAN"EEEEEEEr!   zGot %s as a point.c              3   0   K   | ]}|d          k    V  dS r   Nr   r   r)   r*   s     r   r    z Order.__new__.<locals>.<genexpr>   s+      00Q1a=000000r!   z;Multivariable orders at different points are not supported.c                 2    i | ]}|d t                      z  S r"   r   r   ks     r   
<dictcomp>z!Order.__new__.<locals>.<dictcomp>   s"    555aQ%''	555r!   c                 &    i | ]\  }}d |z  d |z  S r/   r   r   r2   r   s      r   r3   z!Order.__new__.<locals>.<dictcomp>   s&    55541aac1Q3555r!   c                 &    g | ]}t           j        S r   r   Zeror   r)   s     r   r%   z!Order.__new__.<locals>.<listcomp>       ,,,af,,,r!   c                 2    i | ]}|d t                      z  S r0   r1   s     r   r3   z!Order.__new__.<locals>.<dictcomp>   s"    666qQ577
666r!   c                 &    i | ]\  }}d |z  d |z  S r<   r   r5   s      r   r3   z!Order.__new__.<locals>.<dictcomp>   s&    777TQbdBqD777r!   c                 &    g | ]}t           j        S r   r7   r9   s     r   r%   z!Order.__new__.<locals>.<listcomp>   r:   r!   c                 @    i | ]}|t                      d          z   S r   r0   )r   r2   r*   s     r   r3   z!Order.__new__.<locals>.<dictcomp>   s(    >>>qQ%(*>>>r!   c                 d    i | ],\  }}|d          z
                                   |d          z
  -S rA   )together)r   r2   r   r*   s      r   r3   z!Order.__new__.<locals>.<dictcomp>   s;    TTT$!Qq58|--//U1XTTTr!   c                 &    g | ]}t           j        S r   r7   r9   s     r   r%   z!Order.__new__.<locals>.<listcomp>   r:   r!   r   c                     g | ]
}|d          S rA   r   )r   rs     r   r%   z!Order.__new__.<locals>.<listcomp>   s    777qad777r!   c                 "    g | ]\  }}|j         S r   expr)r   efs      r   r%   z!Order.__new__.<locals>.<listcomp>   s     : : :FQ : : :r!   c              3   @   K   | ]}t          |t                    V  d S r   )
isinstancer   )r   args     r   r    z Order.__new__.<locals>.<genexpr>   s,      #S#S#JsH$=$=#S#S#S#S#S#Sr!   c                     g | ]	}|j         
S r   rH   r   as     r   r%   z!Order.__new__.<locals>.<listcomp>   s    :Y:Y:Ya16:Y:Y:Yr!   c                     g | ]	}|j         
S r   rH   rP   s     r   r%   z!Order.__new__.<locals>.<listcomp>  s    ,D,D,DQV,D,D,Dr!   as_AddFrS   keyc                      g | ]
}|         S r   r   )r   r   vps     r   r%   z!Order.__new__.<locals>.<listcomp>9  s    ***1A***r!   )=r   is_Order	variablesr*   listr'   r   r8   lenr   mapappendall	TypeErrorr   
ValueErrordictargszipitemskeysNotImplementedErrorsetNaNanyInfinityNegativeInfinitysubsis_Addfactortupleexpandextract_leading_orderr   as_leading_termr   rM   r   r   rI   is_Mulr   is_Powr   baser   is_zeroas_independentr   r   	make_args	enumerateis_realhasNegativeOneOnesortr   r
   r   __new__) clsrI   rc   kwargsrZ   rQ   r   r)   expr_vpsrspsold_exprlstordersptsrN   ltordernew_exprrJ   br(   margsitqrF   objr$   r*   rX   s                                 @@@r   r   zOrder.__new__   s	   t}} 	0} 0 N	
 !233	Y/D 1 1=v>>D!2Iu47## 0 $ $AGQ00DAq$$Q'''LLOOOO$
 !Wd!3!344	Y/22	22222 	M?)KLLLtDOO$$%%Y7 	`RU^^___= 	749QRR=))G']]Fc)U++,,B

 " "1% "F1I~ R1PR R RR !"F1II7<<>>""c&++--&8&88 7 //	6666I66615= 	5LEEEEIEEEEE 	;1E9::: x	!0000%00000 S)QS S SQx1:% !5595555517799555,,e,,,qQ// !66I66677QWWYY777,,e,,,q' !>>>>I>>>TTTT!''))TTT,,e,,,%[[99Q<<D{ %{{}} (77BHHJJ77788Y''9~~! % {{}}Hd" N+; L+44T::C : :c : : :;DD H+ 73t3T:$ 7 7 7%dH55 7 ##S#S#S#S#S S S7
 !%'F"'D""6"6C'+y 	5 	5!-)<)<d)CBB'0 !- !- !-),BBB!-#%T> !<,1"IIEE,1"OsOOOE &e 4 4 4 4#{ 
7+0f+D+D+D+D#+? !b/4S:Y:Y8=:Y:Y:Y5Z/a]`/a/a/aH'/}!% 7'*,D,DV,D,D,D'E!% 7$(H$(I'*1s1vv:=7F | K v2t2DGGGJ,T22D%d++D4yyA~ + !G $S] //%/@@C&E &E !F !F %.e$4$4 @ @DAq x @'(v1#$QB< !@AI !@aeeAhh !@/0!tE!HH%&X 	!@aeiill 	!@+,6DAq'(QG| %<	 %<34qs8a%&X !@!&)q}2L !@)*A'(x %@		! %@/0v1+,QB< )@AI )@781Q3xE!H"E{] d" N+` 99R==D= 	9Dtx# 	DL 	5D #i''((+,,,****	***wIu 5 566l3&&&&
s8   :
T A(Y"/
U:9Y":V	Y"V		CY"!Y"r   c                     | S r   r   )selfr(   nlogxcdirs        r   _eval_nserieszOrder._eval_nseries>      r!   c                     | j         d         S Nr   rc   r   s    r   rI   z
Order.exprA  s    y|r!   c                 p    | j         dd          r&t          d | j         dd          D                       S dS )Nr"   c              3   &   K   | ]}|d          V  dS r,   r   r   r(   s     r   r    z"Order.variables.<locals>.<genexpr>H  &      55!1555555r!   r   rc   rp   r   s    r   rZ   zOrder.variablesE  @    9QRR= 	55ty}5555552r!   c                 p    | j         dd          r&t          d | j         dd          D                       S dS )Nr"   c              3   &   K   | ]}|d          V  dS r"   Nr   r   s     r   r    zOrder.point.<locals>.<genexpr>O  r   r!   r   r   r   s    r   r*   zOrder.pointL  r   r!   c                 D    | j         j        t          | j                  z  S r   )rI   r'   rh   rZ   r   s    r   r'   zOrder.free_symbolsS  s    y%DN(;(;;;r!   c                     |j         r*|j        r# | j        | j        |z  g| j        dd          R  S |t          d          k    r| S d S Nr"   )	is_Numberis_nonnegativefuncrI   rc   O)r   rJ   s     r   _eval_powerzOrder._eval_powerW  s\    ; 	41+ 	416!&A+3qrr
3333!9 	Hr!   c                 \     j         dd          nt          fdD                       s7t           fd j        D                       st          d j        z            r,d         d          j        d         k    rt          d          t	                    t	           j         dd                                                    D ] \  }}|                                vr||<   !t                                          d            j        t                    fS )	Nr"   c              3   H   K   | ]}|d          d         d          k    V  dS )r"   r   Nr   )r   oorder_symbolss     r   r    z*Order.as_expr_variables.<locals>.<genexpr>b  s6      KK!a 0 33KKKKKKr!   c              3   :   K   | ]}|j         d          k    V  dS r,   )r*   )r   r)   r   s     r   r    z*Order.as_expr_variables.<locals>.<genexpr>c  s.      CC1AA.CCCCCCr!   zDOrder at points other than 0 or oo not supported, got %s as a point.r   z7Multiplying Order at different points is not supported.c                 ,    t          | d                   S r   r   )r(   s    r   <lambda>z)Order.as_expr_variables.<locals>.<lambda>m  s    HXYZ[\Y]H^H^ r!   rU   )
rc   r_   r*   rg   rb   re   rf   sortedrI   rp   )r   r   r   r)   s   ``  r   as_expr_variableszOrder.as_expr_variables^  sh    	` IabbMMMKKKK]KKKKK LCCCC
CCCCCL) +>@D
+K L L L Sq!1!!4
1!E S)QS S S //MTYqrr]++1133 ) )1M..000 )'(M!$"=#6#6#8#8>^>^___My%....r!   c                     t           j        S r   r7   r   s    r   removeOzOrder.removeOp  s	    vr!   c                     | S r   r   r   s    r   getOz
Order.getOs  r   r!   c                 P    t                    j        rdS t          j        u rdS  j        r j        d         nt          j        j        rpt          fdj        D                       s t          fd j        D                       rdS j         j        k    r(t           fdj
        dd         D                       S j        j        r%t           fd	j        j
        D                       S  j        j        r-j        r&t           fd
 j        j
        D                       S  j        r(j        r!t          fd j        D                       }n j        r j        }nj        }|sdS  j        j        rt           j                  dk    r j        j        k    r j        d         }j                            |d          d         }|j        r_|j        |k    rT j        j        |k    rDj        r j        j        |j        z
  j        }j        r j        j        |j        z
  j        }||S ddlm} d} j        j        z  } ||dd          }|D ]P}	ddlm}
  |
||	                              d          }t7          ||
          s|dk    }nd}||}G||k    r dS Q|S  j        j        rt           j                  dk    r j        d         }                    |d          d         }|j        r_|j        |k    rT j        j        |k    rDj        r j        j        |j        z
  j        }j        r j        j        |j        z
  j        }||S   j        g j
        dd         R  }                     |          S )z
        Return True if expr belongs to Order(self.expr, \*self.variables).
        Return False if self belongs to expr.
        Return None if the inclusion relation cannot be determined
        (e.g. when self and expr have different symbols).
        TFr   c              3   $   K   | ]
}|k    V  d S r   r   r-   s     r   r    z!Order.contains.<locals>.<genexpr>  s'      331AJ333333r!   c              3   $   K   | ]
}|k    V  d S r   r   r-   s     r   r    z!Order.contains.<locals>.<genexpr>  s'      66aqEz666666r!   Nc              3   :   K   | ]}|j         d d         v V  dS r   r   r   r(   r   s     r   r    z!Order.contains.<locals>.<genexpr>  s2      EE!1	!""-EEEEEEr!   r"   c              3   B   K   | ]}                     |          V  d S r   )containsr   s     r   r    z!Order.contains.<locals>.<genexpr>  s/      DD4==++DDDDDDr!   c              3   t   K   | ]2} j         |gj        d d         R                                V  3dS r   )r   rc   r   )r   r(   rI   r   s     r   r    z!Order.contains.<locals>.<genexpr>  sc       5 5 ! %49Q7122777@@FF 5 5 5 5 5 5r!   c                 &    g | ]}|j         v |S r   )rZ   )r   r   rI   s     r   r%   z"Order.contains.<locals>.<listcomp>  s%    FFF1!t~2EFQFFFr!   rT   )powsimpr   )deepcombine)Limit)
heuristics)r   rw   r   ri   r*   r8   rY   rj   rI   r_   rc   rn   rZ   rp   ru   r\   rx   rv   r   is_nonpositiveis_infiniter   sympy.simplify.powsimpr   sympy.series.limitsr   doitrM   r   r   )r   rI   common_symbolssymbolotherrvr   rF   ratior   r   lr   r*   s   ``           @r   r   zOrder.containsv  sn    t}}< 	415= 	5!%7
1= 2	3333
33333 66664:66666tyDI% FEEEEty}EEEEEEy EDDDDTY^DDDDDDy 5EM 5 5 5 5 5 5%)Y^5 5 5 5 5 5~ 0$. 0!&FFFFFFF"H "H 0!%!%! t	  *S%8%8A%= *Ndn4*!^A.F I44VE4JJ1ME *v)= *	&0*$} P&*imei&?%O$0 P&*imei&?%O! *')	666666AIdi'EGEe<<<E#  555555E%E**//5/AA!!U++ QAAA AAAv H9 
	"DN 3 3q 8 
	"^A&F''u'==a@E "v!5 "	&("} H"imei7G( H"imei7G "!	di-ty}---}}S!!!r!   c                 R    |                      |          }|t          d          |S )Nz#contains did not evaluate to a bool)r   r`   )r   r   results      r   __contains__zOrder.__contains__  s0    u%% 	CABBBr!   c                    || j         v rK| j                            ||          }| j                             |          }t	          | j                   }t	          | j                  }|j        r|||<   n|j        }t          |          dk    s||v rA||v r| j         |         }n|	                                }|                    || j        |                   }	|	| j        |         k    rddl
m}
 t                      } |
||                    ||          z
  |          }t          |t                    r9|j        d         }|j        d         }t!          |          t!          |          z
  }t#          t%          |f|                    g}|                    |d                                       || j        |                   }	|||<   |	||<   ne||vr_||= ||= |sV|| j        |         k    rE|                    |           |                    t*          j        gt          |          z             nd S t/          |gt%          ||          R  S d S )Nr"   r   )solveset)rZ   rI   rm   indexr[   r*   r   r'   r\   popsympy.solvers.solvesetr   r   rM   r   rc   rh   rb   rd   limitextendr   r8   r   )r   oldnewnewexprr   newvarsnewptsymsvarr*   r   dsole1e2ress                   r   
_eval_subszOrder._eval_subs  sT   $.  %	8innS#..G$$S))A4>**G$$E}  

't99> SD[ d{ )"nQ/"hhjj  HHS$*Q-88E
1- 	ICCCCCC!GG&hsSXXc1-=-='=qAA%c:66 4!$!B!$!B"%b''CGG"3C#CsOO445 !s1v 4 4S$*Q- H H!$GAJ$E!HH_ 
E!H 9C4:a=$8 9t,,,afXc$ii%7888F73w#6#67777K%	8 %	8r!   c                 r    | j                                         }| | j        |g| j        dd          R  S d S r   )rI   _eval_conjugater   rc   r   rI   s     r   r   zOrder._eval_conjugate  K    y((** 	349T2DIabbM2222	3 	3r!   c                 l     | j         | j                            |          g| j        dd          R  p| S r   )r   rI   diffrc   )r   r(   s     r   _eval_derivativezOrder._eval_derivative  s9    ty**;TYqrr];;;CtCr!   c                 r    | j                                         }| | j        |g| j        dd          R  S d S r   )rI   _eval_transposer   rc   r   s     r   r   zOrder._eval_transpose  r   r!   c                     | S r   r   r   s    r   __neg__zOrder.__neg__  r   r!   NrA   )__name__
__module____qualname____doc__rY   	__slots__r	   r   r   propertyrI   rZ   r*   r'   r   r   r   r   r   r   r   r   r   r   r   r   r!   r   r   r      s       p pd HIy y Wyv      X   X   X < < X<  / / /$     N" N" WN"`  &8 &8 &8P3 3 3
D D D3 3 3
    r!   r   N)
sympy.corer   r   r   r   r   r   sympy.core.cacher	   sympy.core.containersr
   sympy.core.functionr   r   r   r   sympy.core.sortingr   &sympy.functions.elementary.exponentialr   r   sympy.sets.setsr   sympy.utilities.iterablesr   r   r   r   r   r!   r   <module>r     s   8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 $ $ $ $ $ $ ' ' ' ' ' ' R R R R R R R R R R R R / / / / / / ; ; ; ; ; ; ; ; & & & & & & 7 7 7 7 7 7 7 7x x x x xD x x xt 
r!   