o
    jg7K                     @   s   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
 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                   @   s   e Zd ZdZdZdZedd Zd(ddZe	d	d
 Z
e	dd Ze	dd Ze	dd Zdd Zdd Zdd Zdd Zedd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%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 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                    s  t |}|s|jr|j}|jnLt|j}tjgt| n>tt	|r%|n|g}g g }t	|d rM|D ]}tt
t |\}}|| | q6ntt
t |}tjgt| tdd |D sktd| ttt|t|kr}td| |jrt|jdd  }t| tt| D ]\}}|  v r| | krtdq| |< qt| t  kr|S t  } fdd	|D |tju rtjS tfd
d|D rtd |r*tfddD rtdd tjtjtj fv r dd |D }	dd |	 D }
dd	 D }nRd tjtjtj fv rFdd |D }	dd |	 D }
dd	 D }n,d tjurjfdd|D }	fdd|	 D }
dd	 D }nd}	d}
t}||	}|jr| }|	rtdd	 |
 D }nt|}t|dkr|  }d }||kr%|}|jr|!|}t"dd	 |D  }nf|r z|j#| }W n t$ya   t%|t&stdd |jD rng }tt||}|jD ].}z|j#| }W n t$y   |}Y nw ||vr
t'|}nt'|g|R  }|| q|jr?t't"| g|R  }|jr;t't"dd	 |jD  g|R  }|j(}n |j)rMt*dd	 |D  }n|j+r_|j,}|j-}t,|t.| }Y nw |j/rjtj}n
|j0|ddid }t1|}t2|}t|dkr |d }tt*3|j0|dd d }t4|D ]\}}|j+r|j\}}||| fv r|j5r|6|s|| ||< q|j+r|j,6|s|j\}}||| fv r|j5r|||  ||< q|j)r|jd tj7u r| }|j+r|j,6|s|j\}}||| fv r|j5r|||  ||< qt*| }||ks||
}|jr1|j(}|j6| s>|j/s>tj8}tt||j9t:d! fd"d	|D |ft;t|  }t<j=| g|R  }|S )#Nr   c                 s   s    | ]}|j V  qd S N)	is_symbol.0vr   r   J/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/series/order.py	<genexpr>   s    z Order.__new__.<locals>.<genexpr>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 | ]} | qS r   r   r   )new_vpr   r   
<listcomp>       z!Order.__new__.<locals>.<listcomp>c                 3   s$    | ]} D ]}||j v V  qqd S r   )free_symbols)r   xppointr   r   r         " zGot %s as a point.c                 3   s    | ]	}| d  kV  qdS r   Nr   r   r%   r&   r   r   r      s    z;Multivariable orders at different points are not supported.c                 S      i | ]}|d t   qS r   r   r   kr   r   r   
<dictcomp>       z!Order.__new__.<locals>.<dictcomp>c                 S      i | ]\}}d | d | qS r,   r   r   r/   r   r   r   r   r0          c                 S      g | ]}t jqS r   r   Zeror*   r   r   r   r!          c                 S   r+   r-   r.   r   r   r   r0      r1   c                 S   r2   r9   r   r3   r   r   r   r0      r4   c                 S   r5   r   r6   r*   r   r   r   r!      r8   c                    s   i | ]
}|t   d   qS r   r-   r.   r&   r   r   r0      s    c                    s*   i | ]\}}| d     | d   qS r;   )togetherr3   r&   r   r   r0      s   * c                 S   r5   r   r6   r*   r   r   r   r!      r8   r   c                 S   s   g | ]}|d  qS r;   r   )r   rr   r   r   r!      r"   c                 S   s   g | ]\}}|j qS r   expr)r   efr   r   r   r!      s    c                 s   s    | ]}t |tV  qd S r   )
isinstancer
   )r   argr   r   r   r          c                 S      g | ]}|j qS r   r>   r   ar   r   r   r!      r8   c                 S   rE   r   r>   rF   r   r   r   r!     r8   as_AddFrH   keyc                    r   r   r   r   )vpr   r   r!   9  r"   )>r   is_Order	variablesr'   listr#   r   r7   lenr   mapappendall	TypeErrorr   
ValueErrordictargszipitemskeysNotImplementedErrorsetNaNanyInfinityImaginaryUnitNegativeInfinitysubsis_Addfactortupleexpandextract_leading_orderr   as_leading_termr   rB   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__)clsr?   rW   kwargsrN   rG   r   r%   expr_vpsrspsold_exprlstordersptsrC   ltordernew_exprr@   br$   margsitqr=   objr   )r    r'   rL   r   ru      s(  










 #
$



PzOrder.__new__r   c                 C      | S r   r   )selfr$   nlogxcdirr   r   r   _eval_nseries>     zOrder._eval_nseriesc                 C   s
   | j d S Nr   rW   r   r   r   r   r?   A  s   
z
Order.exprc                 C   .   | j dd  rtdd | j dd  D S dS )Nr   c                 s       | ]}|d  V  qdS r)   r   r   r$   r   r   r   r   H      z"Order.variables.<locals>.<genexpr>r   rW   re   r   r   r   r   rN   E     zOrder.variablesc                 C   r   )Nr   c                 s   r   r   Nr   r   r   r   r   r   O  r   zOrder.point.<locals>.<genexpr>r   r   r   r   r   r   r'   L  r   zOrder.pointc                 C   s   | j jt| jB S r   )r?   r#   r\   rN   r   r   r   r   r#   S  s   zOrder.free_symbolsc                 C   sB   |j r|jr| j| j| g| jdd  R  S |tdkr| S d S Nr   )	is_Numberis_nonnegativefuncr?   rW   O)r   r@   r   r   r   _eval_powerW  s
   "zOrder._eval_powerc                    s    d u rj dd   nWt fdd D s*tfddjD s*tdj  r; d d jd kr;tdt  tj dd   D ]\}}|  vrX| |< qJt  dd	 d
 jt	 fS )Nr   c                 3   s$    | ]}|d   d d  kV  qdS )r   r   Nr   )r   o)order_symbolsr   r   r   b  r(   z*Order.as_expr_variables.<locals>.<genexpr>c                 3   s    | ]
}| j d  kV  qdS r)   r&   r*   r   r   r   r   c  s    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                 S   s   t | d S r   r   )r$   r   r   r   <lambda>m  s    z)Order.as_expr_variables.<locals>.<lambda>rJ   )
rW   rS   r'   r[   rV   rY   rZ   sortedr?   re   )r   r   ry   r%   r   )r   r   r   as_expr_variables^  s&   zOrder.as_expr_variablesc                 C   s   t jS r   r6   r   r   r   r   removeOp  s   zOrder.removeOc                 C   r   r   r   r   r   r   r   getOs  r   z
Order.getOc                    s  t    jr	dS  tju rdS jrjd ntj jr)tfdd jD s7tfddjD r9dS  jjkrOt	fdd j
d	d D S  jjr`t	fd
d jj
D S jjrujrut fddjj
D S jr jrt fddjD }n
jrj}n j}|sdS jjrtjd	krj jkrjd } jj|ddd	 }|jr|j|krjj|krjrψjj|j j}jrڈjj|j j}|dur|S ddlm} d}j j }||ddd}|D ]/}	ddlm}
 |
||	jdd}t||
s|dk}nd}|du r|}q||kr& dS q|S jjrttjd	krtjd } j|ddd	 }|jrt|j|krtjj|krtjrajj|j j}jrmjj|j j}|durt|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V  qd S r   r   r*   r&   r   r   r     r   z!Order.contains.<locals>.<genexpr>c                 3   r   r   r   r*   r&   r   r   r     r   Nc                 3   s"    | ]}| j d d v V  qdS r   r   r   r   r   r   r     s     r   c                 3   s    | ]}  |V  qd S r   )containsr   r   r   r   r     rD   c                 3   s2    | ]}j |gjd d R   V  qdS r   )r   rW   r   r   )r?   r   r   r   r     s    *c                    s   g | ]	}| j v r|qS r   )rN   )r   ry   r>   r   r   r!     s    z"Order.contains.<locals>.<listcomp>rI   )powsimpr   )deepcombine)Limit)
heuristics)r   rl   r   r]   r'   r7   rM   r^   r?   rS   rW   rc   rN   re   rj   rP   rm   rk   r   is_nonpositiveis_infiniter   sympy.simplify.powsimpr   sympy.series.limitsr   doitrB   r   r   )r   r?   common_symbolssymbolotherrvr   r=   ratiory   r   lr   r   )r?   r'   r   r   r   v  s   
 






zOrder.containsc                 C   s   |  |}|d u rtd|S )Nz#contains did not evaluate to a bool)r   rT   )r   r   resultr   r   r   __contains__  s   
zOrder.__contains__c                 C   s  || j v r| j||}| j |}t| j }t| j}|jr$|||< n|j}t|dks1||v r||v r;| j | }n|	 }||| j| }	|	| j| krddl
m}
 t }|
|||| |}t|trz|jd }|jd }t|t| }tt|f|g}||d || j| }	|||< |	||< n&||vr||= ||= |s|| j| kr|| |tjgt|  nd S t|gt||R  S d S )Nr   r   )solveset)rN   r?   rb   indexrO   r'   r   r#   rP   popsympy.solvers.solvesetr   r   rB   r   rW   r\   rV   rX   limitextendr   r7   r   )r   oldnewnewexprr   newvarsnewptsymsvarr'   r   dsole1e2resr   r   r   
_eval_subs  sD   








zOrder._eval_subsc                 C   2   | j  }|d ur| j|g| jdd  R  S d S r   )r?   _eval_conjugater   rW   r   r?   r   r   r   r        
zOrder._eval_conjugatec                 C   s(   | j | j|g| jdd  R  p| S r   )r   r?   diffrW   )r   r$   r   r   r   _eval_derivative  s   (zOrder._eval_derivativec                 C   r   r   )r?   _eval_transposer   rW   r   r   r   r   r     r   zOrder._eval_transposec                 C   r   r   r   r   r   r   r   __neg__  r   zOrder.__neg__Nr;   )__name__
__module____qualname____doc__rM   	__slots__r   ru   r   propertyr?   rN   r'   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s8    r
 
<




P(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   r   <module>   s        }