From 872c949fb8e5a661f898a0d0cd0823cfe33eaf9e Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Tue, 18 Jan 2022 10:13:17 -0800 Subject: [PATCH] Simplify choose, and add gen_choose for fractional amounts --- manimlib/utils/simple_functions.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/manimlib/utils/simple_functions.py b/manimlib/utils/simple_functions.py index 5683db9d..864ad05d 100644 --- a/manimlib/utils/simple_functions.py +++ b/manimlib/utils/simple_functions.py @@ -22,15 +22,18 @@ def choose_using_cache(n, r): 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 + r = min(r, n - r) denom = reduce(op.mul, range(1, r + 1), 1) numer = reduce(op.mul, range(n, n - r, -1), 1) return numer // denom +def gen_choose(n, r): + numer = np.prod(np.arange(n, n - r, -1)) + denom = np.prod(np.arange(1, r + 1)) + return numer / denom + + def get_num_args(function): return len(get_parameters(function))