Source code for manim.utils.simple_functions

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

__all__ = [
    "sigmoid",
    "choose_using_cache",
    "choose",
    "get_num_args",
    "get_parameters",
    "clip_in_place",
    "fdiv",
    "binary_search",
]


import inspect
import operator as op
from functools import reduce

import numpy as np


[docs]def sigmoid(x): return 1.0 / (1 + np.exp(-x))
CHOOSE_CACHE = {}
[docs]def choose_using_cache(n, r): if n not in CHOOSE_CACHE: CHOOSE_CACHE[n] = {} if r not in CHOOSE_CACHE[n]: CHOOSE_CACHE[n][r] = choose(n, r, use_cache=False) return CHOOSE_CACHE[n][r]
[docs]def choose(n, r, use_cache=True): if use_cache: return choose_using_cache(n, r) if n < r: return 0 if r == 0: return 1 denominator = reduce(op.mul, range(1, r + 1), 1) numerator = reduce(op.mul, range(n, n - r, -1), 1) return numerator // denominator
[docs]def get_num_args(function): return len(get_parameters(function))
[docs]def get_parameters(function): return inspect.signature(function).parameters
# Just to have a less heavyweight name for this extremely common operation # # We may wish to have more fine-grained control over division by zero behavior # in the future (separate specifiable values for 0/0 and x/0 with x != 0), # but for now, we just allow the option to handle indeterminate 0/0.
[docs]def clip(a, min_a, max_a): if a < min_a: return min_a elif a > max_a: return max_a return a
[docs]def clip_in_place(array, min_val=None, max_val=None): if max_val is not None: array[array > max_val] = max_val if min_val is not None: array[array < min_val] = min_val return array
[docs]def fdiv(a, b, zero_over_zero_value=None): if zero_over_zero_value is not None: out = np.full_like(a, zero_over_zero_value) where = np.logical_or(a != 0, b != 0) else: out = None where = True return np.true_divide(a, b, out=out, where=where)