fix: allow zero values in multinomial#3664
Open
chatman-media wants to merge 1 commit into
Open
Conversation
multinomial threw a TypeError on any array containing a zero, e.g. multinomial([2, 0, 1]). A zero count is mathematically valid since 0! = 1, and the multinomial coefficient is the standard generalization of the binomial coefficient: multinomial([k, n - k]) === combinations(n, k). The validation rejected zero via !isPositive(ai) (isPositive(0) is false). Reject only negatives with isNegative(ai) instead, matching the non-negative-integer semantics already used by combinations, factorial, and combinationsWithRep. Also fix the doc comment typo (ai <= 0 -> ai >= 0).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
multinomialthrew aTypeErrorfor any input array containing a zero:A zero count is mathematically valid. The multinomial coefficient is
and since
0! = 1, a zero part simply contributes a factor of 1. The expected values are:multinomial([2, 0, 1])multinomial([3, 0])multinomial([0])multinomial([0, 4, 4])(verified against Python's
math.factorial.)Why it's a bug (inconsistency)
The multinomial coefficient is the standard generalization of the binomial coefficient, and
multinomial([k, n - k])should equalcombinations(n, k). But:combinations(both thenumberandBigNumberpaths),combinationsWithRep, andfactorialall accept non-negative integers (their "positive integer" checks use>= 0). Onlymultinomialrejected zero, because it validated with!isPositive(ai)andisPositive(0) === false.Fix
Reject only negatives (
isNegative(ai)) instead of non-positives (!isPositive(ai)); the existingisIntegercheck still rejects non-integers.isNegative(0)isfalsefornumber,BigNumber,bigint, andFraction, so a zero now passes while negatives and non-integers still throw. Also fixes the doc-comment typo (every ai <= 0→every ai >= 0).Tests
numberandBigNumber, including themultinomial([k, n - k]) === combinations(n, k)identity.TypeError) and passes with it.6653 passing. ESLint clean.