simple-squiggle

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

combinationsWithRep.js (3092B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createCombinationsWithRep = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _number = require("../../utils/number.js");
     11 
     12 var _product = require("../../utils/product.js");
     13 
     14 var name = 'combinationsWithRep';
     15 var dependencies = ['typed'];
     16 var createCombinationsWithRep = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var typed = _ref.typed;
     18 
     19   /**
     20    * Compute the number of ways of picking `k` unordered outcomes from `n`
     21    * possibilities, allowing individual outcomes to be repeated more than once.
     22    *
     23    * CombinationsWithRep only takes integer arguments.
     24    * The following condition must be enforced: k <= n + k -1.
     25    *
     26    * Syntax:
     27    *
     28    *     math.combinationsWithRep(n, k)
     29    *
     30    * Examples:
     31    *
     32    *    math.combinationsWithRep(7, 5) // returns 462
     33    *
     34    * See also:
     35    *
     36    *    combinations, permutations, factorial
     37    *
     38    * @param {number | BigNumber} n    Total number of objects in the set
     39    * @param {number | BigNumber} k    Number of objects in the subset
     40    * @return {number | BigNumber}     Number of possible combinations with replacement.
     41    */
     42   return typed(name, {
     43     'number, number': function numberNumber(n, k) {
     44       if (!(0, _number.isInteger)(n) || n < 0) {
     45         throw new TypeError('Positive integer value expected in function combinationsWithRep');
     46       }
     47 
     48       if (!(0, _number.isInteger)(k) || k < 0) {
     49         throw new TypeError('Positive integer value expected in function combinationsWithRep');
     50       }
     51 
     52       if (n < 1) {
     53         throw new TypeError('k must be less than or equal to n + k - 1');
     54       }
     55 
     56       if (k < n - 1) {
     57         var _prodrange = (0, _product.product)(n, n + k - 1);
     58 
     59         return _prodrange / (0, _product.product)(1, k);
     60       }
     61 
     62       var prodrange = (0, _product.product)(k + 1, n + k - 1);
     63       return prodrange / (0, _product.product)(1, n - 1);
     64     },
     65     'BigNumber, BigNumber': function BigNumberBigNumber(n, k) {
     66       var BigNumber = n.constructor;
     67       var result, i;
     68       var one = new BigNumber(1);
     69       var nMinusOne = n.minus(one);
     70 
     71       if (!isPositiveInteger(n) || !isPositiveInteger(k)) {
     72         throw new TypeError('Positive integer value expected in function combinationsWithRep');
     73       }
     74 
     75       if (n.lt(one)) {
     76         throw new TypeError('k must be less than or equal to n + k - 1 in function combinationsWithRep');
     77       }
     78 
     79       result = one;
     80 
     81       if (k.lt(nMinusOne)) {
     82         for (i = one; i.lte(nMinusOne); i = i.plus(one)) {
     83           result = result.times(k.plus(i)).dividedBy(i);
     84         }
     85       } else {
     86         for (i = one; i.lte(k); i = i.plus(one)) {
     87           result = result.times(nMinusOne.plus(i)).dividedBy(i);
     88         }
     89       }
     90 
     91       return result;
     92     }
     93   });
     94 });
     95 /**
     96  * Test whether BigNumber n is a positive integer
     97  * @param {BigNumber} n
     98  * @returns {boolean} isPositiveInteger
     99  */
    100 
    101 exports.createCombinationsWithRep = createCombinationsWithRep;
    102 
    103 function isPositiveInteger(n) {
    104   return n.isInteger() && n.gte(0);
    105 }