Source code for manim.utils.simple_functions

"""A collection of simple functions."""

from __future__ import annotations

__all__ = [
    "binary_search",
    "choose",
    "clip",
    "sigmoid",
]

import math
from collections.abc import Callable
from functools import lru_cache
from typing import Any, Protocol, TypeVar

import numpy as np






[docs] @lru_cache(maxsize=10) def choose(n: int, k: int) -> int: r"""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 """ value: int = math.comb(n, k) return value
[docs] class Comparable(Protocol): def __lt__(self, other: Any) -> bool: ... def __gt__(self, other: Any) -> bool: ...
ComparableT = TypeVar("ComparableT", bound=Comparable)
[docs] def clip(a: ComparableT, min_a: ComparableT, max_a: ComparableT) -> ComparableT: """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' """ if a < min_a: return min_a elif a > max_a: return max_a return a
[docs] def sigmoid(x: float) -> float: r"""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 """ value: float = 1.0 / (1 + np.exp(-x)) return value