Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# unpublished changes since 15.2.0

- Fix: `multinomial` threw a `TypeError` on arrays containing a zero, like
`multinomial([2, 0, 1])`. Zero is a valid count (`0! = 1`), consistent with
`combinations` and `factorial`.
- Docs: fix the browser example `rocket_trajectory_optimization.html` (#3654).
Thanks @dvd101x.

Expand Down
8 changes: 4 additions & 4 deletions src/function/probability/multinomial.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { deepForEach } from '../../utils/collection.js'
import { factory } from '../../utils/factory.js'

const name = 'multinomial'
const dependencies = ['typed', 'add', 'divide', 'multiply', 'factorial', 'isInteger', 'isPositive']
const dependencies = ['typed', 'add', 'divide', 'multiply', 'factorial', 'isInteger', 'isNegative']

export const createMultinomial = /* #__PURE__ */ factory(name, dependencies, ({ typed, add, divide, multiply, factorial, isInteger, isPositive }) => {
export const createMultinomial = /* #__PURE__ */ factory(name, dependencies, ({ typed, add, divide, multiply, factorial, isInteger, isNegative }) => {
/**
* Multinomial Coefficients compute the number of ways of picking a1, a2, ..., ai unordered outcomes from `n` possibilities.
*
* multinomial takes one array of integers as an argument.
* The following condition must be enforced: every ai <= 0
* The following condition must be enforced: every ai >= 0
*
* Syntax:
*
Expand All @@ -32,7 +32,7 @@ export const createMultinomial = /* #__PURE__ */ factory(name, dependencies, ({
let denom = 1

deepForEach(a, function (ai) {
if (!isInteger(ai) || !isPositive(ai)) {
if (!isInteger(ai) || isNegative(ai)) {
throw new TypeError('Positive integer value expected in function multinomial')
}
sum = add(sum, ai)
Expand Down
10 changes: 10 additions & 0 deletions test/unit-tests/function/probability/multinomial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ describe('multinomial', function () {
assert.deepStrictEqual(multinomial([math.bignumber(10), math.bignumber(1), math.bignumber(2)]), math.bignumber(858))
})

it('should allow zero values (0! = 1), consistent with combinations', function () {
assert.strictEqual(multinomial([0]), 1)
assert.strictEqual(multinomial([3, 0]), 1)
assert.strictEqual(multinomial([2, 0, 1]), 3)
assert.strictEqual(multinomial([0, 4, 4]), 70)
// multinomial([k, n - k]) must agree with combinations(n, k)
assert.strictEqual(multinomial([5, 0]), math.combinations(5, 5))
assert.deepStrictEqual(multinomial([math.bignumber(3), math.bignumber(0)]), math.bignumber(1))
})

it('should not work with non-integer and negative input', function () {
assert.throws(function () { multinomial([0.5, 3]) }, TypeError)
assert.throws(function () { multinomial([math.bignumber(3), math.bignumber(0.5)]) }, TypeError)
Expand Down