mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Simplify choose, and add gen_choose for fractional amounts
This commit is contained in:
parent
eab97faf13
commit
872c949fb8
1 changed files with 7 additions and 4 deletions
|
@ -22,15 +22,18 @@ def choose_using_cache(n, r):
|
||||||
def choose(n, r, use_cache=True):
|
def choose(n, r, use_cache=True):
|
||||||
if use_cache:
|
if use_cache:
|
||||||
return choose_using_cache(n, r)
|
return choose_using_cache(n, r)
|
||||||
if n < r:
|
r = min(r, n - r)
|
||||||
return 0
|
|
||||||
if r == 0:
|
|
||||||
return 1
|
|
||||||
denom = reduce(op.mul, range(1, r + 1), 1)
|
denom = reduce(op.mul, range(1, r + 1), 1)
|
||||||
numer = reduce(op.mul, range(n, n - r, -1), 1)
|
numer = reduce(op.mul, range(n, n - r, -1), 1)
|
||||||
return numer // denom
|
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):
|
def get_num_args(function):
|
||||||
return len(get_parameters(function))
|
return len(get_parameters(function))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue