unused/index.txt
// function powAbs(x: number, n: number): number {
// return Math.sign(x) * Math.pow(Math.abs(x), n);
// }
/**
* Compute the gamma function of a number (Γ).
* @param x a number
* @returns Γ(x); for +ve integer Γ(x) = x!
*/
// function gamma(x: number): number {
// var x = x - 1;
// var e1 = Math.sqrt(Math.abs(2*Math.PI*x));
// var e2 = powAbs(x/Math.E, x);
// var e3 = powAbs(x*Math.sinh(1/x), x/2);
// var e4 = Math.exp((7/324)*(1/((x**3) * (35*x**2 + 33))));
// return e1*e2*e3*e4;
// }
// - https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5840229/
/**
* Compute the natural logarithm of the absolute value of the gamma function of a number (log-Γ).
* @param x a number
* @returns log(|Γ(x)|)
*/
// function lgamma(x: number): number {
// return Math.log(Math.abs(gamma(x)));
// }
// - https://en.cppreference.com/w/cpp/numeric/math/lgamma
const ERF_A1 = 0.254829592;
const ERF_A2 = -0.284496736;
const ERF_A3 = 1.421413741;
const ERF_A4 = -1.453152027;
const ERF_A5 = 1.061405429;
const ERF_P = 0.3275911;
/**
* Find error function value of number (approximation).
* @param x a number
* @returns erf(x)
*/
export function erf(x: number): number {
var sgn = x<0? -1:1;
var x = Math.abs(x);
var t = 1/(1+ ERF_P*x);
var y = 1-(((((ERF_A5*t+ERF_A4)*t)+ERF_A3)*t+ERF_A2)*t+ERF_A1)*t*Math.exp(-x*x);
return sgn*y;
}
// - https://github.com/AndreasMadsen/mathfn/blob/master/functions/erf.js
// - http://www.johndcook.com/cpp_erf.html
// - https://en.wikipedia.org/wiki/Error_function
const ERFC_TABLE = [
-2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
-6.886027e-12, 2.394038e-12, 9.6467911e-11,
-2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
1.303655835580e-6, -1.624290004647e-6,
-2.0278578112534e-5, 4.2523324806907e-5,
3.66839497852761e-4, -9.46595344482036e-4,
-9.561514786808631e-3, 1.9476473204185836e-2,
6.4196979235649026e-1, -1.3026537197817094
];
/**
* Find the complementary error function value of number (approximation).
* @param x a number
* @returns erfc(x)
*/
export function erfc(x: number): number {
if(x<0) return 2-erfc(-x);
var c = 0, d = 0, y = 2/(2+x), z = 4*y-2;
for(var i=0, I=ERFC_TABLE.length-1; i<I; i++) {
var t = d;
d = z*d-c+ERFC_TABLE[i];
c = t;
}
return y*Math.exp(-x*x+0.5*(ERFC_TABLE[I]+z*d)-c);
}
// - https://github.com/AndreasMadsen/mathfn/blob/master/functions/erf.js
// - http://pydoc.net/Python/timbre/1.0.0/timbre.stats/
// - https://github.com/jstat/jstat/blob/master/src/special.js
// - https://en.wikipedia.org/wiki/Error_function