Source code for manim.utils.simple_functions

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

from __future__ import annotations

__all__ = [

import inspect
from functools import lru_cache
from types import MappingProxyType
from typing import Callable

import numpy as np
from scipy import special

[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 ---------- - - """ return special.comb(n, k, exact=True)
[docs]def clip(a, min_a, max_a): """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 ---------- - - """ return 1.0 / (1 + np.exp(-x))