gamma.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { factory } from '../../utils/factory.js';
  2. import { gammaG, gammaNumber, gammaP } from '../../plain/number/index.js';
  3. var name = 'gamma';
  4. var dependencies = ['typed', 'config', 'multiplyScalar', 'pow', 'BigNumber', 'Complex'];
  5. export var createGamma = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed,
  8. config,
  9. multiplyScalar,
  10. pow,
  11. BigNumber: _BigNumber,
  12. Complex
  13. } = _ref;
  14. /**
  15. * Compute the gamma function of a value using Lanczos approximation for
  16. * small values, and an extended Stirling approximation for large values.
  17. *
  18. * To avoid confusion with the matrix Gamma function, this function does
  19. * not apply to matrices.
  20. *
  21. * Syntax:
  22. *
  23. * math.gamma(n)
  24. *
  25. * Examples:
  26. *
  27. * math.gamma(5) // returns 24
  28. * math.gamma(-0.5) // returns -3.5449077018110335
  29. * math.gamma(math.i) // returns -0.15494982830180973 - 0.49801566811835596i
  30. *
  31. * See also:
  32. *
  33. * combinations, factorial, permutations
  34. *
  35. * @param {number | BigNumber | Complex} n A real or complex number
  36. * @return {number | BigNumber | Complex} The gamma of `n`
  37. */
  38. function gammaComplex(n) {
  39. if (n.im === 0) {
  40. return gammaNumber(n.re);
  41. }
  42. // Lanczos approximation doesn't work well with real part lower than 0.5
  43. // So reflection formula is required
  44. if (n.re < 0.5) {
  45. // Euler's reflection formula
  46. // gamma(1-z) * gamma(z) = PI / sin(PI * z)
  47. // real part of Z should not be integer [sin(PI) == 0 -> 1/0 - undefined]
  48. // thanks to imperfect sin implementation sin(PI * n) != 0
  49. // we can safely use it anyway
  50. var _t = new Complex(1 - n.re, -n.im);
  51. var r = new Complex(Math.PI * n.re, Math.PI * n.im);
  52. return new Complex(Math.PI).div(r.sin()).div(gammaComplex(_t));
  53. }
  54. // Lanczos approximation
  55. // z -= 1
  56. n = new Complex(n.re - 1, n.im);
  57. // x = gammaPval[0]
  58. var x = new Complex(gammaP[0], 0);
  59. // for (i, gammaPval) in enumerate(gammaP):
  60. for (var i = 1; i < gammaP.length; ++i) {
  61. // x += gammaPval / (z + i)
  62. var gammaPval = new Complex(gammaP[i], 0);
  63. x = x.add(gammaPval.div(n.add(i)));
  64. }
  65. // t = z + gammaG + 0.5
  66. var t = new Complex(n.re + gammaG + 0.5, n.im);
  67. // y = sqrt(2 * pi) * t ** (z + 0.5) * exp(-t) * x
  68. var twoPiSqrt = Math.sqrt(2 * Math.PI);
  69. var tpow = t.pow(n.add(0.5));
  70. var expt = t.neg().exp();
  71. // y = [x] * [sqrt(2 * pi)] * [t ** (z + 0.5)] * [exp(-t)]
  72. return x.mul(twoPiSqrt).mul(tpow).mul(expt);
  73. }
  74. return typed(name, {
  75. number: gammaNumber,
  76. Complex: gammaComplex,
  77. BigNumber: function BigNumber(n) {
  78. if (n.isInteger()) {
  79. return n.isNegative() || n.isZero() ? new _BigNumber(Infinity) : bigFactorial(n.minus(1));
  80. }
  81. if (!n.isFinite()) {
  82. return new _BigNumber(n.isNegative() ? NaN : Infinity);
  83. }
  84. throw new Error('Integer BigNumber expected');
  85. }
  86. });
  87. /**
  88. * Calculate factorial for a BigNumber
  89. * @param {BigNumber} n
  90. * @returns {BigNumber} Returns the factorial of n
  91. */
  92. function bigFactorial(n) {
  93. if (n < 8) {
  94. return new _BigNumber([1, 1, 2, 6, 24, 120, 720, 5040][n]);
  95. }
  96. var precision = config.precision + (Math.log(n.toNumber()) | 0);
  97. var Big = _BigNumber.clone({
  98. precision
  99. });
  100. if (n % 2 === 1) {
  101. return n.times(bigFactorial(new _BigNumber(n - 1)));
  102. }
  103. var p = n;
  104. var prod = new Big(n);
  105. var sum = n.toNumber();
  106. while (p > 2) {
  107. p -= 2;
  108. sum += p;
  109. prod = prod.times(sum);
  110. }
  111. return new _BigNumber(prod.toPrecision(_BigNumber.precision));
  112. }
  113. });