simple-squiggle

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

factorial.js (1299B)


      1 import { deepMap } from '../../utils/collection.js';
      2 import { factory } from '../../utils/factory.js';
      3 var name = 'factorial';
      4 var dependencies = ['typed', 'gamma'];
      5 export var createFactorial = /* #__PURE__ */factory(name, dependencies, _ref => {
      6   var {
      7     typed,
      8     gamma
      9   } = _ref;
     10 
     11   /**
     12    * Compute the factorial of a value
     13    *
     14    * Factorial only supports an integer value as argument.
     15    * For matrices, the function is evaluated element wise.
     16    *
     17    * Syntax:
     18    *
     19    *    math.factorial(n)
     20    *
     21    * Examples:
     22    *
     23    *    math.factorial(5)    // returns 120
     24    *    math.factorial(3)    // returns 6
     25    *
     26    * See also:
     27    *
     28    *    combinations, combinationsWithRep, gamma, permutations
     29    *
     30    * @param {number | BigNumber | Array | Matrix} n   An integer number
     31    * @return {number | BigNumber | Array | Matrix}    The factorial of `n`
     32    */
     33   return typed(name, {
     34     number: function number(n) {
     35       if (n < 0) {
     36         throw new Error('Value must be non-negative');
     37       }
     38 
     39       return gamma(n + 1);
     40     },
     41     BigNumber: function BigNumber(n) {
     42       if (n.isNegative()) {
     43         throw new Error('Value must be non-negative');
     44       }
     45 
     46       return gamma(n.plus(1));
     47     },
     48     'Array | Matrix': function ArrayMatrix(n) {
     49       return deepMap(n, this);
     50     }
     51   });
     52 });