
    lj                        d Z ddlmZ g dZddlZddlmZ ddlmZ ddl	m
Z
mZmZ ddlZ	 d#d$dZ ed          d%d            Z G d de          Z ede          Zd&d Zd'd"ZdS )(z!A collection of simple functions.    )annotations)binary_searchchooseclipsigmoidN)Callable)	lru_cache)AnyProtocolTypeVar-C6?functionCallable[[float], float]targetfloatlower_boundupper_bound	tolerancereturnfloat | Nonec                    |}|}t          j        t          j        ||g                    }t          ||z
            |k    rt          j        t          j        ||g                    } fd|||fD             \  }}	}
||k    r|S |
|k    r|S ||cxk    r|
k    rn n|	|k    r|}n|}n||cxk    r|
k    rn n||}}ndS t          ||z
            |k    |S )a  Searches for a value in a range by repeatedly dividing the range in half.

    To be more precise, performs numerical binary search to determine the
    input to ``function``, between the bounds given, that outputs ``target``
    to within ``tolerance`` (default of 0.0001).
    Returns ``None`` if no input can be found within the bounds.

    Examples
    --------

    Consider the polynomial :math:`x^2 + 3x + 1` where we search for
    a target value of :math:`11`. An exact solution is :math:`x = 2`.

    ::

        >>> solution = binary_search(lambda x: x**2 + 3*x + 1, 11, 0, 5)
        >>> bool(abs(solution - 2) < 1e-4)
        True
        >>> solution = binary_search(lambda x: x**2 + 3*x + 1, 11, 0, 5, tolerance=0.01)
        >>> bool(abs(solution - 2) < 0.01)
        True

    Searching in the interval :math:`[0, 5]` for a target value of :math:`71`
    does not yield a solution::

        >>> binary_search(lambda x: x**2 + 3*x + 1, 71, 0, 5) is None
        True
    c              3  .   K   | ]} |          V  d S N ).0hr   s     W/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/utils/simple_functions.py	<genexpr>z binary_search.<locals>.<genexpr><   s+      88ahhqkk888888    N)npmeanarrayabs)r   r   r   r   r   lhrhmhlxmxrxs   `          r   r   r      s)   F 
B	B"b**++B
b2g,,
"
"WRXr2h''((8888BB<888
B<<I<<I2F{{&2BB4! b2g,,
"
"$ Ir   
   )maxsizenintkc                0    t          j        | |          }|S )a3  The binomial coefficient n choose k.

    :math:`\binom{n}{k}` describes the number of possible choices of
    :math:`k` elements from a set of :math:`n` elements.

    References
    ----------
    - https://en.wikipedia.org/wiki/Combination
    - https://docs.python.org/3/library/math.html#math.comb
    )mathcomb)r,   r.   values      r   r   r   O   s     1aELr   c                      e Zd ZddZddZdS )	
Comparableotherr
   r   boolc                    d S r   r   selfr5   s     r   __lt__zComparable.__lt__`         r   c                    d S r   r   r8   s     r   __gt__zComparable.__gt__b   r;   r   N)r5   r
   r   r6   )__name__
__module____qualname__r:   r=   r   r   r   r4   r4   _   s(        ----------r   r4   ComparableT)boundamin_amax_ac                &    | |k     r|S | |k    r|S | S )ao  Clips ``a`` to the interval [``min_a``, ``max_a``].

    Accepts any comparable objects (i.e. those that support <, >).
    Returns ``a`` if it is between ``min_a`` and ``max_a``.
    Otherwise, whichever of ``min_a`` and ``max_a`` is closest.

    Examples
    --------
    ::

        >>> clip(15, 11, 20)
        15
        >>> clip('a', 'h', 'k')
        'h'
    r   )rC   rD   rE   s      r   r   r   h   s%      	5yy	
UHr   xc                <    ddt          j        |            z   z  }|S )a/  Returns the output of the logistic function.

    The logistic function, a common example of a sigmoid function, is defined
    as :math:`\frac{1}{1 + e^{-x}}`.

    References
    ----------
    - https://en.wikipedia.org/wiki/Sigmoid_function
    - https://en.wikipedia.org/wiki/Logistic_function
    g      ?   )r    exp)rG   r2   s     r   r   r      s!     !bfaRjj.)ELr   )r   )r   r   r   r   r   r   r   r   r   r   r   r   )r,   r-   r.   r-   r   r-   )rC   rA   rD   rA   rE   rA   r   rA   )rG   r   r   r   )__doc__
__future__r   __all__r0   collections.abcr   	functoolsr	   typingr
   r   r   numpyr    r   r   r4   rA   r   r   r   r   r   <module>rR      sF   ' ' " " " " " "    $ $ $ $ $ $       ) ) ) ) ) ) ) ) ) )     8 8 8 8 8v 2   . . . . . . . . gm:666   .     r   