simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

multinomial.js (1496B)


      1 import { deepForEach } from '../../utils/collection.js';
      2 import { factory } from '../../utils/factory.js';
      3 var name = 'multinomial';
      4 var dependencies = ['typed', 'add', 'divide', 'multiply', 'factorial', 'isInteger', 'isPositive'];
      5 export var createMultinomial = /* #__PURE__ */factory(name, dependencies, _ref => {
      6   var {
      7     typed,
      8     add,
      9     divide,
     10     multiply,
     11     factorial,
     12     isInteger,
     13     isPositive
     14   } = _ref;
     15 
     16   /**
     17    * Multinomial Coefficients compute the number of ways of picking a1, a2, ..., ai unordered outcomes from `n` possibilities.
     18    *
     19    * multinomial takes one array of integers as an argument.
     20    * The following condition must be enforced: every ai <= 0
     21    *
     22    * Syntax:
     23    *
     24    *     math.multinomial(a) // a is an array type
     25    *
     26    * Examples:
     27    *
     28    *    math.multinomial([1,2,1]) // returns 12
     29    *
     30    * See also:
     31    *
     32    *    combinations, factorial
     33    *
     34    * @param {number[] | BigNumber[]} a    Integer numbers of objects in the subset
     35    * @return {Number | BigNumber}         Multinomial coefficient.
     36    */
     37   return typed(name, {
     38     'Array | Matrix': function ArrayMatrix(a) {
     39       var sum = 0;
     40       var denom = 1;
     41       deepForEach(a, function (ai) {
     42         if (!isInteger(ai) || !isPositive(ai)) {
     43           throw new TypeError('Positive integer value expected in function multinomial');
     44         }
     45 
     46         sum = add(sum, ai);
     47         denom = multiply(denom, factorial(ai));
     48       });
     49       return divide(factorial(sum), denom);
     50     }
     51   });
     52 });