2022-12-18 09:13:11 -08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-04-12 19:19:59 +08:00
|
|
|
from functools import lru_cache
|
2022-05-21 15:56:03 +08:00
|
|
|
import hashlib
|
2018-12-24 12:37:51 -08:00
|
|
|
import inspect
|
2022-02-13 15:16:16 -08:00
|
|
|
import math
|
2022-04-12 19:19:59 +08:00
|
|
|
|
|
|
|
import numpy as np
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2022-12-18 09:11:16 -08:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
2024-03-07 16:07:39 -03:00
|
|
|
from typing import Callable, TypeVar, Iterable
|
2022-12-18 09:11:16 -08:00
|
|
|
from manimlib.typing import FloatArray
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2022-12-18 09:11:16 -08:00
|
|
|
Scalable = TypeVar("Scalable", float, FloatArray)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sigmoid(x: float | FloatArray):
|
2018-04-06 13:58:59 -07:00
|
|
|
return 1.0 / (1 + np.exp(-x))
|
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2022-01-26 13:03:14 +08:00
|
|
|
@lru_cache(maxsize=10)
|
2022-12-18 09:11:16 -08:00
|
|
|
def choose(n: int, k: int) -> int:
|
2022-02-13 15:16:16 -08:00
|
|
|
return math.comb(n, k)
|
|
|
|
|
|
|
|
|
2022-12-18 09:11:16 -08:00
|
|
|
def gen_choose(n: int, r: int) -> int:
|
|
|
|
return int(np.prod(range(n, n - r, -1)) / math.factorial(r))
|
2018-03-30 18:19:23 -07:00
|
|
|
|
2018-08-12 12:17:32 -07:00
|
|
|
|
2022-12-18 09:11:16 -08:00
|
|
|
def get_num_args(function: Callable) -> int:
|
2024-03-07 16:39:45 -03:00
|
|
|
return function.__code__.co_argcount
|
2019-01-29 14:22:46 -08:00
|
|
|
|
|
|
|
|
2024-03-07 16:07:39 -03:00
|
|
|
def get_parameters(function: Callable) -> Iterable[str]:
|
|
|
|
return inspect.signature(function).parameters.keys()
|
2018-08-12 12:17:32 -07:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
# 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.
|
2018-04-06 13:58:59 -07:00
|
|
|
|
|
|
|
|
2022-12-18 09:11:16 -08:00
|
|
|
def clip(a: float, min_a: float, max_a: float) -> float:
|
2020-02-18 22:27:13 -08:00
|
|
|
if a < min_a:
|
|
|
|
return min_a
|
|
|
|
elif a > max_a:
|
|
|
|
return max_a
|
|
|
|
return a
|
|
|
|
|
|
|
|
|
2023-01-08 21:27:56 -05:00
|
|
|
def arr_clip(arr: np.ndarray, min_a: float, max_a: float) -> np.ndarray:
|
|
|
|
arr[arr < min_a] = min_a
|
|
|
|
arr[arr > max_a] = max_a
|
|
|
|
return arr
|
|
|
|
|
|
|
|
|
2022-12-18 09:11:16 -08:00
|
|
|
def fdiv(a: Scalable, b: Scalable, zero_over_zero_value: Scalable | None = None) -> Scalable:
|
2018-04-06 13:58:59 -07:00
|
|
|
if zero_over_zero_value is not None:
|
2018-03-30 18:19:23 -07:00
|
|
|
out = np.full_like(a, zero_over_zero_value)
|
2018-04-06 13:58:59 -07:00
|
|
|
where = np.logical_or(a != 0, b != 0)
|
2018-03-30 18:19:23 -07:00
|
|
|
else:
|
|
|
|
out = None
|
|
|
|
where = True
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
return np.true_divide(a, b, out=out, where=where)
|
2019-02-06 21:16:26 -08:00
|
|
|
|
|
|
|
|
2022-12-18 09:11:16 -08:00
|
|
|
def binary_search(function: Callable[[float], float],
|
|
|
|
target: float,
|
|
|
|
lower_bound: float,
|
|
|
|
upper_bound: float,
|
|
|
|
tolerance:float = 1e-4) -> float | None:
|
2019-02-06 21:16:26 -08:00
|
|
|
lh = lower_bound
|
|
|
|
rh = upper_bound
|
2022-12-18 09:11:16 -08:00
|
|
|
mh = (lh + rh) / 2
|
2019-02-06 21:16:26 -08:00
|
|
|
while abs(rh - lh) > tolerance:
|
|
|
|
lx, mx, rx = [function(h) for h in (lh, mh, rh)]
|
|
|
|
if lx == target:
|
|
|
|
return lx
|
|
|
|
if rx == target:
|
|
|
|
return rx
|
|
|
|
|
|
|
|
if lx <= target and rx >= target:
|
|
|
|
if mx > target:
|
|
|
|
rh = mh
|
|
|
|
else:
|
|
|
|
lh = mh
|
|
|
|
elif lx > target and rx < target:
|
|
|
|
lh, rh = rh, lh
|
|
|
|
else:
|
|
|
|
return None
|
2022-12-18 09:11:16 -08:00
|
|
|
mh = (lh + rh) / 2
|
2019-02-06 21:16:26 -08:00
|
|
|
return mh
|
2022-05-21 15:56:03 +08:00
|
|
|
|
|
|
|
|
2022-12-18 09:11:16 -08:00
|
|
|
def hash_string(string: str) -> str:
|
2022-05-21 15:56:03 +08:00
|
|
|
# Truncating at 16 bytes for cleanliness
|
|
|
|
hasher = hashlib.sha256(string.encode())
|
|
|
|
return hasher.hexdigest()[:16]
|