erf.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createErf = void 0;
  6. var _collection = require("../../utils/collection.js");
  7. var _number = require("../../utils/number.js");
  8. var _factory = require("../../utils/factory.js");
  9. /* eslint-disable no-loss-of-precision */
  10. var name = 'erf';
  11. var dependencies = ['typed'];
  12. var createErf = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  13. var typed = _ref.typed;
  14. /**
  15. * Compute the erf function of a value using a rational Chebyshev
  16. * approximations for different intervals of x.
  17. *
  18. * This is a translation of W. J. Cody's Fortran implementation from 1987
  19. * ( https://www.netlib.org/specfun/erf ). See the AMS publication
  20. * "Rational Chebyshev Approximations for the Error Function" by W. J. Cody
  21. * for an explanation of this process.
  22. *
  23. * For matrices, the function is evaluated element wise.
  24. *
  25. * Syntax:
  26. *
  27. * math.erf(x)
  28. *
  29. * Examples:
  30. *
  31. * math.erf(0.2) // returns 0.22270258921047847
  32. * math.erf(-0.5) // returns -0.5204998778130465
  33. * math.erf(4) // returns 0.9999999845827421
  34. *
  35. * @param {number | Array | Matrix} x A real number
  36. * @return {number | Array | Matrix} The erf of `x`
  37. */
  38. return typed('name', {
  39. number: function number(x) {
  40. var y = Math.abs(x);
  41. if (y >= MAX_NUM) {
  42. return (0, _number.sign)(x);
  43. }
  44. if (y <= THRESH) {
  45. return (0, _number.sign)(x) * erf1(y);
  46. }
  47. if (y <= 4.0) {
  48. return (0, _number.sign)(x) * (1 - erfc2(y));
  49. }
  50. return (0, _number.sign)(x) * (1 - erfc3(y));
  51. },
  52. 'Array | Matrix': typed.referToSelf(function (self) {
  53. return function (n) {
  54. return (0, _collection.deepMap)(n, self);
  55. };
  56. })
  57. // TODO: For complex numbers, use the approximation for the Faddeeva function
  58. // from "More Efficient Computation of the Complex Error Function" (AMS)
  59. });
  60. /**
  61. * Approximates the error function erf() for x <= 0.46875 using this function:
  62. * n
  63. * erf(x) = x * sum (p_j * x^(2j)) / (q_j * x^(2j))
  64. * j=0
  65. */
  66. function erf1(y) {
  67. var ysq = y * y;
  68. var xnum = P[0][4] * ysq;
  69. var xden = ysq;
  70. var i;
  71. for (i = 0; i < 3; i += 1) {
  72. xnum = (xnum + P[0][i]) * ysq;
  73. xden = (xden + Q[0][i]) * ysq;
  74. }
  75. return y * (xnum + P[0][3]) / (xden + Q[0][3]);
  76. }
  77. /**
  78. * Approximates the complement of the error function erfc() for
  79. * 0.46875 <= x <= 4.0 using this function:
  80. * n
  81. * erfc(x) = e^(-x^2) * sum (p_j * x^j) / (q_j * x^j)
  82. * j=0
  83. */
  84. function erfc2(y) {
  85. var xnum = P[1][8] * y;
  86. var xden = y;
  87. var i;
  88. for (i = 0; i < 7; i += 1) {
  89. xnum = (xnum + P[1][i]) * y;
  90. xden = (xden + Q[1][i]) * y;
  91. }
  92. var result = (xnum + P[1][7]) / (xden + Q[1][7]);
  93. var ysq = parseInt(y * 16) / 16;
  94. var del = (y - ysq) * (y + ysq);
  95. return Math.exp(-ysq * ysq) * Math.exp(-del) * result;
  96. }
  97. /**
  98. * Approximates the complement of the error function erfc() for x > 4.0 using
  99. * this function:
  100. *
  101. * erfc(x) = (e^(-x^2) / x) * [ 1/sqrt(pi) +
  102. * n
  103. * 1/(x^2) * sum (p_j * x^(-2j)) / (q_j * x^(-2j)) ]
  104. * j=0
  105. */
  106. function erfc3(y) {
  107. var ysq = 1 / (y * y);
  108. var xnum = P[2][5] * ysq;
  109. var xden = ysq;
  110. var i;
  111. for (i = 0; i < 4; i += 1) {
  112. xnum = (xnum + P[2][i]) * ysq;
  113. xden = (xden + Q[2][i]) * ysq;
  114. }
  115. var result = ysq * (xnum + P[2][4]) / (xden + Q[2][4]);
  116. result = (SQRPI - result) / y;
  117. ysq = parseInt(y * 16) / 16;
  118. var del = (y - ysq) * (y + ysq);
  119. return Math.exp(-ysq * ysq) * Math.exp(-del) * result;
  120. }
  121. });
  122. /**
  123. * Upper bound for the first approximation interval, 0 <= x <= THRESH
  124. * @constant
  125. */
  126. exports.createErf = createErf;
  127. var THRESH = 0.46875;
  128. /**
  129. * Constant used by W. J. Cody's Fortran77 implementation to denote sqrt(pi)
  130. * @constant
  131. */
  132. var SQRPI = 5.6418958354775628695e-1;
  133. /**
  134. * Coefficients for each term of the numerator sum (p_j) for each approximation
  135. * interval (see W. J. Cody's paper for more details)
  136. * @constant
  137. */
  138. var P = [[3.16112374387056560e00, 1.13864154151050156e02, 3.77485237685302021e02, 3.20937758913846947e03, 1.85777706184603153e-1], [5.64188496988670089e-1, 8.88314979438837594e00, 6.61191906371416295e01, 2.98635138197400131e02, 8.81952221241769090e02, 1.71204761263407058e03, 2.05107837782607147e03, 1.23033935479799725e03, 2.15311535474403846e-8], [3.05326634961232344e-1, 3.60344899949804439e-1, 1.25781726111229246e-1, 1.60837851487422766e-2, 6.58749161529837803e-4, 1.63153871373020978e-2]];
  139. /**
  140. * Coefficients for each term of the denominator sum (q_j) for each approximation
  141. * interval (see W. J. Cody's paper for more details)
  142. * @constant
  143. */
  144. var Q = [[2.36012909523441209e01, 2.44024637934444173e02, 1.28261652607737228e03, 2.84423683343917062e03], [1.57449261107098347e01, 1.17693950891312499e02, 5.37181101862009858e02, 1.62138957456669019e03, 3.29079923573345963e03, 4.36261909014324716e03, 3.43936767414372164e03, 1.23033935480374942e03], [2.56852019228982242e00, 1.87295284992346047e00, 5.27905102951428412e-1, 6.05183413124413191e-2, 2.33520497626869185e-3]];
  145. /**
  146. * Maximum/minimum safe numbers to input to erf() (in ES6+, this number is
  147. * Number.[MAX|MIN]_SAFE_INTEGER). erf() for all numbers beyond this limit will
  148. * return 1
  149. */
  150. var MAX_NUM = Math.pow(2, 53);