
    wg7K                         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y)    )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y)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 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           	        ! t        |      }|sX|j                  r|j                  }|j                  !nt	        |j
                        }t        j                  gt        |      z  !nt	        t        |      r|n|g      }g g c}!t        |d         rF|D ]@  }t	        t        t         |            \  }}|j                  |       !j                  |       B n6t	        t        t         |            }t        j                  gt        |      z  !t        d |D              st        d|z        t        t	        t        |                  t        |      k7  rt        d|z        |j                  rt!        |j"                  dd        }t!        |      }	t!        t%        |!            }
|
j'                         D ]0  \  }}||	j)                         v r||	|   k7  s!t+        d      ||	|<   2 t-        |j)                               t-        |	j)                               k(  r|S t	        |	j)                               }|D cg c]  }|	|   	 c}!|t        j.                  u rt        j.                  S t1        !fd|D              rt        d!z        |rt1        !fd	!D              rt+        d
      !d   t        j2                  t        j2                  t        j4                  z  fv rc|D ci c]  }|dt7               z   }}|j'                         D ci c]  \  }}d|z  d|z   }}}!D cg c]  }t        j                   }}n5!d   t        j8                  t        j8                  t        j4                  z  fv rb|D ci c]  }|dt7               z   }}|j'                         D ci c]  \  }}d|z  d|z   }}}!D cg c]  }t        j                   }}n!d   t        j                  ury|D ci c]  }|t7               !d   z    }}|j'                         D ci c]"  \  }}|!d   z
  j;                         |!d   z
  $ }}}!D cg c]  }t        j                   }}nd}d}t	        !      }|j=                  |      }|j>                  r|jA                         }|r+tC        |j'                         D cg c]  }|d   	 c}      }ntC        |      }t        |      dkD  r|jE                         }d }||k7  r6|}|j>                  r8|jG                  |      }tI        |D cg c]  \  }}|jJ                   c}} }n|r	  |jL                  | }|jb                  rt        j                  }n |jd                  |ddid   }tg        |      }ti        |      }t        |      dk(  r|d   }t	        tY        jj                  |je                  |d      d               }tm        |      D ]1  \  }}|jZ                  s|j"                  \  }}||| fv r&|jn                  r|jq                  |      s	||z  ||<   P|jZ                  rK|j\                  jq                  |      s0|j"                  \  }}||| fv s|jn                  s|||z  z  ||<   |jV                  s|j"                  d   t        jr                  u s| }|jZ                  s|j\                  jq                  |      r|j"                  \  }}||| fv s|jn                  s'|||z  z  ||<   4 tY        | }||k7  r6|j=                  |      }|j                  r|jJ                  } |jp                  | s|jb                  st        jt                  }t!        t%        |!            }
|jw                  tx               |D cg c]  }|
|   	 c}!|ft{        t%        |!       z   }t}        j~                  | g| } | S c c}w c c}w c c}}w c c}w c c}w c c}}w c c}w c c}w c c}}w c c}w c c}w c c}}w # tN        $ r tQ        |tR              st        d |j"                  D              rnVg }tC        t%        ||            }|j"                  D ]Q  }	  |jL                  | }n# tN        $ r |}Y nw xY w||vrtU        |      }ntU        |g| }|j                  |       S |j>                  retU        tI        | g| }|j>                  r:tU        tI        |j"                  D cg c]  }|jJ                   nc c}w c} g| }|jJ                  }nn|jV                  r'tY        |D cg c]  }|jJ                   nc c}w c} }n;|jZ                  r/|j\                  }|j^                  }t]        |ta        |      z        }Y nw xY wc c}w )Nr   c              3   4   K   | ]  }|j                     y wN)	is_symbol).0vs     W/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/series/order.py	<genexpr>z Order.__new__.<locals>.<genexpr>   s     211;;2s   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   H   K   | ]  }D ]  }||j                   v    y wr   )free_symbols)r   xppoints      r   r    z Order.__new__.<locals>.<genexpr>   s'     EquE!qANN"E"Es   "zGot %s as a point.c              3   .   K   | ]  }|d    k7    ywr   Nr   r   r%   r&   s     r   r    z Order.__new__.<locals>.<genexpr>   s     0Q1a=0s   z;Multivariable orders at different points are not supported.r   c              3   <   K   | ]  }t        |t                y wr   )
isinstancer   )r   args     r   r    z Order.__new__.<locals>.<genexpr>   s     #S#JsH$=#Ss   as_AddFr.   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__)"clsrO   r=   kwargsr3   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]   zOrder.__new__   su   t}}} NN	

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

1??(BCC+45aQ%'	\55+,779541aac1Q3h55&+,aff,,qa00!2D2DQ__2TUU,56qQ57
]66-.WWY7TQbdBqDj77&+,aff,,q'4=>qQ%(**>>JK'')T$!Qq58|--/U1X=TT&+,aff,,%[99Q<D{{{{}BHHJ7qad78Y'9~! {{}Hd";;44T:Cc :FQ :;D 73t33T:H || vv2t22DGGJ,T2D%d+D4yA~ !G $S]] //%/@C&E !F %.e$4 @DAq xx'(vv1#$QB<AIIaeeAh/0!tE!H%&XXaeeiil+,66DAq'(QG|		34qs8a%&XX!&&)q}}2L)*A'(xx		!/0vv1+,QB<AII781Q3xE!H@   #E{] d"` 99R=D==99Dtxx#DLL55D #i'(+, )*1A*wIu 566ll3&&
] 7 65,67,>T, 8" !;
 % 7%dH5 ##S#S S !%'F"'D""6C'+yy 	5!-)<)<)<d)CB'0 !-),B!-#%T>,1"IE,1"OsOE &e 4	5  ${{+0f+D+D#+??/4S8==:Ya166:Y:Y5Z/a]`/aH'/}}!%'*V,DQVV,D,D'E!%$(HH$(II'*1s1v:=7d +s   ?`.``2``1``#>`(+'`-`3`86`=
a gAg"b21g2c =g?c  A4g4e1g8f
Aggc                     | S r   r   )selfr$   nlogxcdirs        r   _eval_nserieszOrder._eval_nseries>          c                      | j                   d   S Nr   r=   ry   s    r   rO   z
Order.exprA  s    yy|r   c                 `    | j                   dd  rt        d | j                   dd  D              S y)Nr!   c              3   &   K   | ]	  }|d      ywr(   r   r   r$   s     r   r    z"Order.variables.<locals>.<genexpr>H       5!15   r   r=   rL   r   s    r   r3   zOrder.variablesE  -    99QR=5tyy}555r   c                 `    | j                   dd  rt        d | j                   dd  D              S y)Nr!   c              3   &   K   | ]	  }|d      yw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                 Z    | j                   j                  t        | j                        z  S r   )rO   r#   rB   r3   r   s    r   r#   zOrder.free_symbolsS  s     yy%%DNN(;;;r   c                     |j                   r9|j                  r- | j                  | j                  |z  g| j                  dd   S |t        d      k(  r| S y Nr!   )	is_Numberis_nonnegativefuncrO   r=   O)rr   rk   s     r   _eval_powerzOrder._eval_powerW  sL    ;;1++166!&&A+3qr
33!9Hr   c                      j                   dd  nt        fdD              s6t         fd j                  D              st        d j                  z        r#d   d    j                  d   k7  rt        d      t	              t	         j                   dd        j                         D ]  \  }}|j                         vs||<    t        j                         d        j                  t              fS )	Nr!   c              3   :   K   | ]  }|d    d   d    k(    yw)r!   r   Nr   )r   oorder_symbolss     r   r    z*Order.as_expr_variables.<locals>.<genexpr>b  s$     K!a 0 33Ks   c              3   B   K   | ]  }|j                   d    k(    ywr(   )r&   )r   r%   ry   s     r   r    z*Order.as_expr_variables.<locals>.<genexpr>c  s     C1AA.Cs   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^ r   r0   )
r=   r9   r&   rA   r<   r?   r@   sortedrO   rL   )ry   r   re   r%   s   ``  r   as_expr_variableszOrder.as_expr_variables^  s     IIabMMK]KKC

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

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

1!E)QS S /MTYYqr]+113 )1M..00'(M!$) #=#6#6#8>^_Myy%...r   c                 "    t         j                  S r   )r   r5   r   s    r   removeOzOrder.removeOp  s    vvr   c                     | S r   r   r   s    r   getOz
Order.getOs  r~   r   c                 F	    t              j                  ryt        j                  u ry j                  r j                  d   nt        j
                  j                  r	t        fdj                  D              st        fd j                  D              ryj                   j                  k(  r!t         fdj                  dd D              S j                  j                  r(t         fd	j                  j                  D              S  j                  j                  r5j                  r)t         fd
 j                  j                  D              S  j                  r?j                  r3t         j                  D cg c]  }|j                  v s| c}      }n% j                  r j                  }nj                  }|sy j                  j                  r
t         j                        dk(  r j                  j                  k(  rى j                  d   }j                  j!                  |d      d   }|j                  r|j"                  |k(  r j                  j"                  |k(  rv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 ]B  }ddlm}
  |
|	|      j5                  d      }t7        ||
      s|dk7  }nd}||}<||k7  sB y |S  j                  j                  rt         j                        dk(  rω j                  d   }j!                  |d      d   }|j                  r|j"                  |k(  r j                  j"                  |k(  rvj                  r- j                  j$                  |j$                  z
  j&                  }j(                  r- j                  j$                  |j$                  z
  j*                  }|S   j8                  g j                  dd  } j;                  |      S c c}w )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   | ]	  }|k7    y wr   r   r)   s     r   r    z!Order.contains.<locals>.<genexpr>  s     31AJ3   c              3   (   K   | ]	  }|k7    y wr   r   r)   s     r   r    z!Order.contains.<locals>.<genexpr>  s     6aqEz6r   Nc              3   @   K   | ]  }|j                   d d v   ywr   r   r   r$   ry   s     r   r    z!Order.contains.<locals>.<genexpr>  s      E!1		!"-E   r!   c              3   @   K   | ]  }j                  |        y wr   )containsr   s     r   r    z!Order.contains.<locals>.<genexpr>  s     D4==+Dr   c              3   |   K   | ]3  } j                   |gj                  d d  j                         5 ywr   )r   r=   r   )r   r$   rO   ry   s     r   r    z!Order.contains.<locals>.<genexpr>  s>      5 ! %499Q7127@@F 5s   9<r/   )powsimpr   )deepcombine)Limit)
heuristics)r   rT   r   rC   r&   r5   r2   rD   rO   r9   r=   rJ   r3   rL   rR   r6   rU   rS   r   is_nonpositiveis_infiniter   sympy.simplify.powsimpr   sympy.series.limitsr   doitr,   r   r   )ry   rO   re   common_symbolssymbolotherrvr   rh   ratior   lrw   r&   s   ``           @r   r   zOrder.containsv  s    t}<<155=!%

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

3364::66yyDII%Etyy}EEEyyDTYY^^DDDyyEMM 5%)YY^^5 5 5~~$..!& $F1!t~~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!!U+QAA9AAv H99DNN 3q 8^^A&F''u'=a@Ev!5		&(}}"iimmeii7GG(("iimmeii7GG~!	dii-tyy}-}}S!!g Gs   R&Rc                 B    | j                  |      }|t        d      |S )Nz#contains did not evaluate to a bool)r   r:   )ry   r   results      r   __contains__zOrder.__contains__  s&    u%>ABBr   c                 `   || j                   v r| j                  j                  ||      }| j                   j                  |      }t	        | j                         }t	        | j
                        }|j                  r|||<   n|j                  }t        |      dk(  s||v r||v r| j                   |   }n|j                         }|j                  || j
                  |         }	|	| j
                  |   k7  rddl
m}
 t               } |
||j                  ||      z
  |      }t        |t              r5|j                  d   }|j                  d   }t!        |      t!        |      z
  }t#        t%        |f|            g}|j                  |d         j'                  || j
                  |         }	|||<   |	||<   n]||vrX||= ||= |sQ|| j
                  |   k(  r?|j)                  |       |j)                  t*        j,                  gt        |      z         ny t/        |gt%        ||       S y )Nr!   r   )solveset)r3   rO   rI   indexr4   r&   r   r#   r6   popsympy.solvers.solvesetr   r   r,   r   r=   rB   r<   r>   limitextendr   r5   r   )ry   oldnewnewexprrt   newvarsnewptsymsvarr&   r   dsole1e2ress                   r   
_eval_subszOrder._eval_subs  s   $.. iinnS#.G$$S)A4>>*G$E}} 
''t9>SD[d{"nnQ/"hhj  HHS$**Q-8E

1-C!G&sSXXc1-='=qA%c:6!$!B!$!B"%b'CG"3C#CsO45 !s1v 4 4S$**Q- H!$GAJ$E!H_
E!HC4::a=$8t,affXc$i%7873w#677K !r   c                 |    | j                   j                         }|  | j                  |g| j                  dd   S y r   )rO   _eval_conjugater   r=   ry   rO   s     r   r   zOrder._eval_conjugate  ?    yy((*499T2DIIabM22 r   c                 |     | j                   | j                  j                  |      g| j                  dd   xs | S r   )r   rO   diffr=   )ry   r$   s     r   _eval_derivativezOrder._eval_derivative  s4    tyy*;TYYqr];CtCr   c                 |    | j                   j                         }|  | j                  |g| j                  dd   S y r   )rO   _eval_transposer   r=   r   s     r   r   zOrder._eval_transpose  r   r   c                     | S r   r   r   s    r   __neg__zOrder.__neg__  r~   r   N)r   )__name__
__module____qualname____doc__r2   	__slots__r	   r]   r}   propertyrO   r3   r&   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    pd HIy yv       < </$ N" N"`&8P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xD xt 
r   