simple-squiggle

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

erf.js (5243B)


      1 /* eslint-disable no-loss-of-precision */
      2 import { deepMap } from '../../utils/collection.js';
      3 import { sign } from '../../utils/number.js';
      4 import { factory } from '../../utils/factory.js';
      5 var name = 'erf';
      6 var dependencies = ['typed'];
      7 export var createErf = /* #__PURE__ */factory(name, dependencies, _ref => {
      8   var {
      9     typed
     10   } = _ref;
     11 
     12   /**
     13    * Compute the erf function of a value using a rational Chebyshev
     14    * approximations for different intervals of x.
     15    *
     16    * This is a translation of W. J. Cody's Fortran implementation from 1987
     17    * ( https://www.netlib.org/specfun/erf ). See the AMS publication
     18    * "Rational Chebyshev Approximations for the Error Function" by W. J. Cody
     19    * for an explanation of this process.
     20    *
     21    * For matrices, the function is evaluated element wise.
     22    *
     23    * Syntax:
     24    *
     25    *    math.erf(x)
     26    *
     27    * Examples:
     28    *
     29    *    math.erf(0.2)    // returns 0.22270258921047847
     30    *    math.erf(-0.5)   // returns -0.5204998778130465
     31    *    math.erf(4)      // returns 0.9999999845827421
     32    *
     33    * @param {number | Array | Matrix} x   A real number
     34    * @return {number | Array | Matrix}    The erf of `x`
     35    */
     36   return typed('name', {
     37     number: function number(x) {
     38       var y = Math.abs(x);
     39 
     40       if (y >= MAX_NUM) {
     41         return sign(x);
     42       }
     43 
     44       if (y <= THRESH) {
     45         return sign(x) * erf1(y);
     46       }
     47 
     48       if (y <= 4.0) {
     49         return sign(x) * (1 - erfc2(y));
     50       }
     51 
     52       return sign(x) * (1 - erfc3(y));
     53     },
     54     'Array | Matrix': function ArrayMatrix(n) {
     55       return deepMap(n, this);
     56     } // TODO: For complex numbers, use the approximation for the Faddeeva function
     57     //  from "More Efficient Computation of the Complex Error Function" (AMS)
     58 
     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 
     67   function erf1(y) {
     68     var ysq = y * y;
     69     var xnum = P[0][4] * ysq;
     70     var xden = ysq;
     71     var i;
     72 
     73     for (i = 0; i < 3; i += 1) {
     74       xnum = (xnum + P[0][i]) * ysq;
     75       xden = (xden + Q[0][i]) * ysq;
     76     }
     77 
     78     return y * (xnum + P[0][3]) / (xden + Q[0][3]);
     79   }
     80   /**
     81    * Approximates the complement of the error function erfc() for
     82    * 0.46875 <= x <= 4.0 using this function:
     83    *                       n
     84    * erfc(x) = e^(-x^2) * sum (p_j * x^j) / (q_j * x^j)
     85    *                      j=0
     86    */
     87 
     88 
     89   function erfc2(y) {
     90     var xnum = P[1][8] * y;
     91     var xden = y;
     92     var i;
     93 
     94     for (i = 0; i < 7; i += 1) {
     95       xnum = (xnum + P[1][i]) * y;
     96       xden = (xden + Q[1][i]) * y;
     97     }
     98 
     99     var result = (xnum + P[1][7]) / (xden + Q[1][7]);
    100     var ysq = parseInt(y * 16) / 16;
    101     var del = (y - ysq) * (y + ysq);
    102     return Math.exp(-ysq * ysq) * Math.exp(-del) * result;
    103   }
    104   /**
    105    * Approximates the complement of the error function erfc() for x > 4.0 using
    106    * this function:
    107    *
    108    * erfc(x) = (e^(-x^2) / x) * [ 1/sqrt(pi) +
    109    *               n
    110    *    1/(x^2) * sum (p_j * x^(-2j)) / (q_j * x^(-2j)) ]
    111    *              j=0
    112    */
    113 
    114 
    115   function erfc3(y) {
    116     var ysq = 1 / (y * y);
    117     var xnum = P[2][5] * ysq;
    118     var xden = ysq;
    119     var i;
    120 
    121     for (i = 0; i < 4; i += 1) {
    122       xnum = (xnum + P[2][i]) * ysq;
    123       xden = (xden + Q[2][i]) * ysq;
    124     }
    125 
    126     var result = ysq * (xnum + P[2][4]) / (xden + Q[2][4]);
    127     result = (SQRPI - result) / y;
    128     ysq = parseInt(y * 16) / 16;
    129     var del = (y - ysq) * (y + ysq);
    130     return Math.exp(-ysq * ysq) * Math.exp(-del) * result;
    131   }
    132 });
    133 /**
    134  * Upper bound for the first approximation interval, 0 <= x <= THRESH
    135  * @constant
    136  */
    137 
    138 var THRESH = 0.46875;
    139 /**
    140  * Constant used by W. J. Cody's Fortran77 implementation to denote sqrt(pi)
    141  * @constant
    142  */
    143 
    144 var SQRPI = 5.6418958354775628695e-1;
    145 /**
    146  * Coefficients for each term of the numerator sum (p_j) for each approximation
    147  * interval (see W. J. Cody's paper for more details)
    148  * @constant
    149  */
    150 
    151 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]];
    152 /**
    153  * Coefficients for each term of the denominator sum (q_j) for each approximation
    154  * interval (see W. J. Cody's paper for more details)
    155  * @constant
    156  */
    157 
    158 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]];
    159 /**
    160  * Maximum/minimum safe numbers to input to erf() (in ES6+, this number is
    161  * Number.[MAX|MIN]_SAFE_INTEGER). erf() for all numbers beyond this limit will
    162  * return 1
    163  */
    164 
    165 var MAX_NUM = Math.pow(2, 53);