
    \hfL                         S SK JrJrJrJrJrJr  S SKJr  S SK	J
r
  S SKJrJrJrJr  S SKJr  S SKJrJr  S SKJr  S SKJrJr   " S	 S
\5      r\rg)    )SsympifyExprDummyAddMul)cacheit)Tuple)Function	PoleErrorexpand_power_base
expand_logdefault_sort_key)explog)
Complement)uniqis_sequencec                       \ rS rSrSrSrSr\S 5       rSS jr	\
S 5       r\
S 5       r\
S	 5       r\
S
 5       rS rS rS rS r\S 5       rS rS rS rS rS rS rSrg)Order   a
  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 there
exists a `\delta > 0` and an `M > 0` such that `|g(x)| \leq M|f(x)|` for
`|x-a| < \delta`.  This is equivalent to `\limsup_{x \rightarrow a}
|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`, there is a `\delta > 0` and 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           	        ^! [        U5      nU(       d]  UR                  (       a  UR                  nUR                  m!O[	        UR
                  5      n[        R                  /[        U5      -  m!O[	        [        U5      (       a  UOU/5      n/ / snm![        US   5      (       aG  U H@  n[	        [        [         U5      5      u  pgUR                  U5        T!R                  U5        MB     O6[	        [        [         U5      5      n[        R                  /[        U5      -  m![        S U 5       5      (       d  [        SU-  5      e[        [	        [        U5      5      5      [        U5      :w  a  [        SU-  5      eUR                  (       a  [!        UR"                  SS  5      n[!        U5      n	[!        [%        UT!5      5      n
U
R'                  5        H1  u  pgXiR)                  5       ;   a  XyU   :w  a  [+        S5      eM-  XyU'   M3     [-        UR)                  5       5      [-        U	R)                  5       5      :X  a  U$ [	        U	R)                  5       5      nU Vs/ s H  oiU   PM	     snm!U[        R.                  L a  [        R.                  $ [1        U!4S jU 5       5      (       a  [        ST!-  5      eU(       GaH  [1        U!4S	 jT! 5       5      (       a  [+        S
5      eT!S   [        R2                  [        R2                  [        R4                  -  4;   aj  U Vs0 s H  oS[7        5       -  _M     nnUR'                  5        VVs0 s H  u  pSU-  SU-  _M     nnnT! Vs/ s H  n[        R                  PM     nnGOFT!S   [        R8                  [        R8                  [        R4                  -  4;   ai  U Vs0 s H  oS[7        5       -  _M     nnUR'                  5        VVs0 s H  u  pSU-  SU-  _M     nnnT! Vs/ s H  n[        R                  PM     nnOT!S   [        R                  La  U Vs0 s H  o[7        5       T!S   -   _M     nnUR'                  5        VVs0 s H"  u  pUT!S   -
  R;                  5       UT!S   -
  _M$     nnnT! Vs/ s H  n[        R                  PM     nnOSnSn[	        T!5      nUR=                  U5      nUR>                  (       a  URA                  5       nU(       a-  [C        UR'                  5        Vs/ s H  oS   PM	     sn5      nO[C        U5      n[        U5      S:  a  URE                  5       nS nUU:w  Ga  UnUR>                  (       a;  URG                  U5      n[I        U VVs/ s H  u  nnURJ                  PM     snn6 nGO:U(       Ga2   URL                  " U6 nURb                  (       a  [        R                  nOURd                  " USS06S   n[g        U5      n[i        U5      n[        U5      S:X  Ga  US   n[	        [X        Rj                  " URe                  USS9S   5      5      n[m        U5       GHx  u  nnURZ                  (       d  M  UR"                  u  nnUUU* 4;   a1  URn                  (       a   URq                  U5      (       d
  UU-  UU'   Mc  URZ                  (       aZ  UR\                  Rq                  U5      (       d:  UR"                  u  nnUUU* 4;   a   URn                  (       a  UUU-  -  UU'   M  M  M  URV                  (       d  M  UR"                  S   [        Rr                  L d  GM  U* nURZ                  (       d  GM  UR\                  Rq                  U5      (       a  GM>  UR"                  u  nnUUU* 4;   d  GMY  URn                  (       d  GMm  UUU-  -  UU'   GM{     [Y        U6 nUU:w  a  GM  UR=                  U5      nUR                  (       a  URJ                  nURp                  " U6 (       d!  URb                  (       d  [        Rt                  n[!        [%        UT!5      5      n
URw                  [x        S9  U Vs/ s H  ojU   PM	     snm!U4[{        [%        UT!5      6 -   n[|        R~                  " U /UQ76 n U $ s  snf s  snf s  snnf s  snf s  snf s  snnf s  snf s  snf s  snnf s  snf s  snf s  snnf ! [N         Ga    [Q        U[R        5      (       d!  [        S UR"                   5       5      (       a   GN~/ n[C        [%        X.5      5      nUR"                   HU  n URL                  " U6 nO! [N         a    Un Of = fUU;  a  [U        U5      nO[U        U/UQ76 nUR                  U5        MW     UR>                  (       an  [U        [I        U6 /UQ76 nUR>                  (       a<  [U        [I        UR"                   Vs/ s H  oURJ                  PM     Os  snf sn6 /UQ76 nURJ                  n GNxURV                  (       a+  [Y        U Vs/ s H  oURJ                  PM     Os  snf sn6 n GNURZ                  (       a/  UR\                  nUR^                  n[]        U[a        U5      -  5      n GNf = fs  snf )Nr   c              3   8   #    U  H  oR                   v   M     g 7fN)	is_symbol).0vs     J/var/www/auris/envauris/lib/python3.13/site-packages/sympy/series/order.py	<genexpr> Order.__new__.<locals>.<genexpr>   s     2	1;;	s   z!Variables are not symbols, got %sz3Variables are supposed to be unique symbols, got %s   z2Mixing Order at different points is not supported.c              3   T   >#    U  H  nT  H  o!UR                   ;   v   M     M     g 7fr   )free_symbols)r   xppoints      r    r!   r"      s!     EIqu!ANN"u"Is   %(zGot %s as a point.c              3   2   >#    U  H  oTS    :g  v   M     g7fr   Nr   r   r'   r(   s     r    r!   r"      s     0%Qa=%s   z;Multivariable orders at different points are not supported.r   c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )
isinstancer   )r   args     r    r!   r"      s     #S#JsH$=$=s   as_AddFr0   key)@r   is_Order	variablesr(   listr%   r   Zerolenr   mapappendall	TypeErrorr   
ValueErrordictargszipitemskeysNotImplementedErrorsetNaNanyInfinityImaginaryUnitr   NegativeInfinitytogethersubsis_Addfactortupleexpandextract_leading_orderr   expras_leading_termr   r.   r   r   is_Mulr   is_Powr   baser   is_zeroas_independentr   r   	make_args	enumerateis_realhasNegativeOneOnesortr   r
   r   __new__)"clsrQ   r?   kwargsr5   ar   r'   expr_vpnew_vpvpksrspsrold_exprlstefordersptsr/   ltordernew_exprbr&   margsitqobjr(   s"                                    @r    r_   Order.__new__   s   t}}} NN	

 !2!23	Y/D 1 1v>D!2Iu47##AGQ0DA$$Q'LLO 
 !Wd!34	Y/2	222?)KLLtDO$%Y7RU^^__==499QR=)G']Fc)U+,B
%1I~1PR R & !"1I # 7<<>"c&++-&88 /	,56IqI6155=55LEIEEE1E9::0%000)QS SQxAJJ

1??(BCC+459a%'	\95+,7795941ac1Q3h95&+,eaffe,qa00!2D2DQ__2TUU,56Iq57
]I6-.WWY7YTQbdBqDjY7&+,eaffe,q'4=>Iq%(**I>JK'')T)$!q58|--/U1X=)T&+,eaffe,%[99Q<D{{{{}BHHJ7JqdJ78Y'9~! {{}Hd";;44T:Cc :cFQc :;D 7#33T:H || vv#22DGGJ,T2D%d+D4yA~ !G $S]] //%/@C&E !F %.e$4DAq xxx'(vv1#$QB<AIIaeeAhh/0!tE!H%&XXaeeiill+,66DAq'(QG|		34qs8a 9B|%&XXX!&&)q}}2L)*A'(xxx		!/0vv1+,QB<AIII781Q3xE!H %5   #E{] d"` 99R=D==99Dxx#DLL55D #i'(+, )*	1A	*wIu 566ll3&&
] 7 65,67,>T, 8" !;
 % 7%dH55 ##S#S S S !%'F"'D"6C'+yy!-),)<)<d)CB'0 !-),B!-#%T>,1"IE,1"OsOE &e 4 (1  ${{+0f+D+D#+??/4S8==:Y=a66=:Y5Z/a]`/aH'/}}!%'*V,DVVVV,D'E!%$(HH$(II'*1s1v:=7d +s   "c'cc/c	c!5c&c,c17)c6'c<#dd
d kAk
%k
6fk
fk
fBk
h-,k

k
$i:
9
k
A k
	k
c                     U $ r   r   )selfr&   nlogxcdirs        r    _eval_nseriesOrder._eval_nseries>          c                      U R                   S   $ Nr   r?   r|   s    r    rQ   
Order.exprA  s    yy|r   c                 j    U R                   SS  (       a  [        S U R                   SS   5       5      $ g)Nr#   c              3   *   #    U  H	  oS    v   M     g7fr*   r   r   r&   s     r    r!   "Order.variables.<locals>.<genexpr>H       5}!1}   r   r?   rN   r   s    r    r5   Order.variablesE  -    99QR=5tyy}555r   c                 j    U R                   SS  (       a  [        S U R                   SS   5       5      $ g)Nr#   c              3   *   #    U  H	  oS    v   M     g7fr#   Nr   r   s     r    r!   Order.point.<locals>.<genexpr>O  r   r   r   r   r   s    r    r(   Order.pointL  r   r   c                 Z    U R                   R                  [        U R                  5      -  $ r   )rQ   r%   rD   r5   r   s    r    r%   Order.free_symbolsS  s     yy%%DNN(;;;r   c                     UR                   (       a>  UR                  (       a-  U R                  " U R                  U-  /U R                  SS  Q76 $ U[        S5      :X  a  U $ g Nr#   )	is_Numberis_nonnegativefuncrQ   r?   O)rt   rm   s     r    _eval_powerOrder._eval_powerW  sJ    ;;1++66!&&A+3qr
33!9Hr   c                 8  ^ ^ Tc  T R                   SS  mO[        U4S jT 5       5      (       d<  [        U 4S jT R                   5       5      (       d  [        ST R                  -  5      eT(       a$  TS   S   T R                  S   :w  a  [        S5      e[	        T5      m[	        T R                   SS  5      R                  5        H   u  p#UTR                  5       ;  d  M  UTU'   M"     [        TR                  5       S S9mT R                  [        T5      4$ )	Nr#   c              3   >   >#    U  H  oS    TS   S    :H  v   M     g7f)r#   r   Nr   )r   oorder_symbolss     r    r!   *Order.as_expr_variables.<locals>.<genexpr>b  s"     K]!a 0 33]s   c              3   F   >#    U  H  oTR                   S    :H  v   M     g7fr*   )r(   )r   r'   r|   s     r    r!   r   c  s     C
1A.
   !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                     [        U S   5      $ r   r   )r&   s    r    <lambda>)Order.as_expr_variables.<locals>.<lambda>m  s    HXYZ[\Y]H^r   r2   )
r?   r;   r(   rC   r>   rA   rB   sortedrQ   rN   )r|   r   rg   r'   s   ``  r    as_expr_variablesOrder.as_expr_variables^  s     IIabMMK]KKKC

CCC) +>@D

+K L Lq!1!!4

1!E)QS S /MTYYqr]+113M..00'(M!$ 4 #=#6#6#8>^_Myy%...r   c                 "    [         R                  $ r   )r   r7   r   s    r    removeOOrder.removeOp  s    vvr   c                     U $ r   r   r   s    r    getO
Order.getOs  r   r   c                 0
  ^ ^^ [        T5      mTR                  (       a  gT[        R                  L a  gT R                  (       a  T R                  S   O[        R
                  mTR                  (       GaY  [        U4S jTR                   5       5      (       d$  [        U4S jT R                   5       5      (       a  gTR                  T R                  :X  a"  [        U 4S jTR                  SS  5       5      $ TR                  R                  (       a)  [        U 4S	 jTR                  R                   5       5      $ T R                  R                  (       a;  TR                  (       a*  [        UU 4S
 jT R                  R                   5       5      $ T R                  (       aI  TR                  (       a8  [        T R                   Vs/ s H  o"TR                  ;   d  M  UPM     sn5      nO*T R                  (       a  T R                  nOTR                  nU(       d  gT R                  R                  (       Ga  [        T R                  5      S:X  Ga  T R                  TR                  :X  a  T R                  S   nTR                  R!                  USS9S   nUR                  (       a  UR"                  U:X  a  T R                  R"                  U:X  a  TR                  (       a-  T R                  R$                  UR$                  -
  R&                  nTR(                  (       a-  T R                  R$                  UR$                  -
  R*                  nWb  U$ SSKJn  SnT R                  TR                  -  n	U" U	SSS9n	U HE  nSSKJn
  U
" XT5      R5                  SS9n[7        X5      (       d  US:g  nOSnUc  UnM>  X:w  d  ME    g   U$ T R                  R                  (       a  [        T R                  5      S:X  a  T R                  S   nTR!                  USS9S   nUR                  (       a  UR"                  U:X  a  T R                  R"                  U:X  a  TR                  (       a-  T R                  R$                  UR$                  -
  R&                  nTR(                  (       a-  T R                  R$                  UR$                  -
  R*                  nWb  U$ T R8                  " T/T R                  SS Q76 nT R;                  U5      $ s  snf )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   ,   >#    U  H	  oT:g  v   M     g 7fr   r   r+   s     r    r!   !Order.contains.<locals>.<genexpr>  s     3
1J
   c              3   ,   >#    U  H	  oT:g  v   M     g 7fr   r   r+   s     r    r!   r     s     6:aEz:r   Nc              3   F   >#    U  H  oTR                   S S ;   v   M     g7fr   r   r   r&   r|   s     r    r!   r     s     E}!		!"-}r   r#   c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr   )containsr   s     r    r!   r     s     D^4==++^r   c              3      >#    U  H4  nTR                   " U/TR                  S S Q76 R                  T5      v   M6     g7fr   )r   r?   r   )r   r&   rQ   r|   s     r    r!   r     s>      5%3  99Q7127@@FF%3s   <?r1   )powsimpr   )deepcombine)Limit)
heuristics)r   rV   r   rE   r(   r7   r4   rF   rQ   r;   r?   rL   r5   rN   rT   r8   rW   rU   r   is_nonpositiveis_infiniter   sympy.simplify.powsimpr   sympy.series.limitsr   doitr.   r   r   )r|   rQ   rg   common_symbolssymbolotherrvr   rj   ratior   lry   r(   s   ``           @r    r   Order.containsv  s    t}<<155=!%

1===3

33364::666yyDII%Etyy}EEEyyDTYY^^DDDyyEMM 5%)YY^^5 5 5~~$..!& $F1t~~2EQF"H!%!%!		   S%8A%=NNdnn4!^^A.F II44VE4J1MEv)=		&0$}}&*iimmeii&?%O%O$00&*iimmeii&?%O%O!~')	6AIIdii'EEe<E#5%E*//5/A!!++QAA9Av $ H99DNN 3q 8^^A&F''u'=a@Ev!5		&(}}"iimmeii7GG(("iimmeii7GG~!	ii-tyy}-}}S!!g Gs   T"Tc                 D    U R                  U5      nUc  [        S5      eU$ )Nz#contains did not evaluate to a bool)r   r<   )r|   r   results      r    __contains__Order.__contains__  s&    u%>ABBr   c                    XR                   ;   Ga  U R                  R                  X5      nU R                   R                  U5      n[	        U R                   5      n[	        U R
                  5      nUR                  (       a  X%U'   GO=UR                  n[        U5      S:X  d  X;   Ga  X;   a  U R                   U   nOUR                  5       nSSK
Jn	  UR                  [        5      (       a  U	" UR                  5       R                  XR                  5       R
                  S   5      U R
                  U   :X  a5  UR                  5       R
                  S   n
[        U/[        U/U
/5      Q76 $ UR                  XR
                  U   5      n
XR
                  U   :w  a  SSKJn  [%        5       nU" XR                  X5      -
  U5      n['        U[(        5      (       a5  UR*                  S   nUR*                  S   n[-        U5      [-        U5      -
  n[/        [        U4U5      5      /nUR                  US   5      R                  XR
                  U   5      n
XU'   XU'   OaX;  a[  XT	 Xd	 U(       dO  X R
                  U   :X  a=  UR1                  U5        UR1                  [2        R4                  /[        U5      -  5        Og [        U/[        XV5      Q76 $ g )Nr#   r   )limit)solveset)r5   rQ   rK   indexr6   r(   r   r%   r8   popsympyr   r[   r   r   r@   sympy.solvers.solvesetr   r   r.   r   r?   rD   r>   extendr   r7   )r|   oldnewnewexprrv   newvarsnewptsymsvarr   r(   r   dsole1e2ress                    r    
_eval_subsOrder._eval_subs  s8   .. iinnS.G$$S)A4>>*G$E}} 
''t9>S[{"nnQ/"hhj ,wwu~~%
hhjFVFVWXFY*Z^b^h^hij^k*k #
 0 0 3$WCsC55'/BCC #jjm <

1-C!G&sXXc-='=qA%c:66!$!B!$!B"%b'CG"3C#CsO45 !s1v 4 4S**Q- H!$AJ$!H_
EHC::a=$8t,affXc$i%7873w#677U !r   c                 ~    U R                   R                  5       nUb   U R                  " U/U R                  SS  Q76 $ g r   )rQ   _eval_conjugater   r?   r|   rQ   s     r    r   Order._eval_conjugate  =    yy((*99T2DIIabM22 r   c                     U R                   " U R                  R                  U5      /U R                  SS  Q76 =(       d    U $ r   )r   rQ   diffr?   )r|   r&   s     r    _eval_derivativeOrder._eval_derivative  s2    yy*;TYYqr];CtCr   c                 ~    U R                   R                  5       nUb   U R                  " U/U R                  SS  Q76 $ g r   )rQ   _eval_transposer   r?   r   s     r    r   Order._eval_transpose  r   r   c                     U $ r   r   r   s    r    __neg__Order.__neg__  r   r   N)r   )__name__
__module____qualname____firstlineno____doc__r4   	__slots__r	   r_   r   propertyrQ   r5   r(   r%   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__r   r   r    r   r      s    pd HIy yv       < </$ N" N"`+8Z3
D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     s9    8 8 $ ' R R / ; & 7}D }~ 
r   