lib-r-math.js
Advanced tools
Comparing version 1.0.12 to 1.0.13
{ | ||
"typescript.tsdk": "./node_modules/typescript/lib", | ||
"vsicons.presets.angular": false, | ||
"vsicons.presets.angular": true, | ||
"editor.tabSize": 2, | ||
@@ -5,0 +5,0 @@ "editor.renderWhitespace": "all", |
@@ -34,3 +34,3 @@ /* | ||
import { R_FINITE, NaN, fabs, DBL_MIN, log } from './general'; | ||
import { R_FINITE, NaN, fabs, DBL_MIN, log } from './_general'; | ||
@@ -37,0 +37,0 @@ |
/* | ||
* AUTHOR | ||
* Jacob Bogers, jkfbogers@gmail.com | ||
* Januari 22, 2017 | ||
* feb 10, 2017 | ||
* | ||
@@ -17,3 +16,3 @@ * ORIGINAL AUTHOR | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
@@ -23,5 +22,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, a copy is available at | ||
* https://www.R-project.org/Licenses/ | ||
* License for JS language implementation | ||
* https://www.jacob-bogers.com/libRmath.js/Licenses/ | ||
* | ||
* | ||
* License for R statistical package | ||
* https://www.r-project.org/Licenses/ | ||
*/ | ||
@@ -55,15 +57,8 @@ | ||
xmax_BESS_K, | ||
sqxmin_BESS_K | ||
} from './general'; | ||
sqxmin_BESS_K, | ||
max0, | ||
min0 | ||
} from './_general'; | ||
const min0 = (x: number, y: number): number => { return x <= y ? x : y; }; | ||
const max0 = (x: number, y: number): number => { return x <= y ? y : x; }; | ||
/* | ||
#define min0(x, y) (((x) <= (y)) ? (x) : (y)) | ||
#define max0(x, y) (((x) <= (y)) ? (y) : (x)) | ||
*/ | ||
export function bessel_k(x: number, alpha: number, expo: number): number { | ||
@@ -70,0 +65,0 @@ |
@@ -47,9 +47,9 @@ /* | ||
import {ISNAN, R_FINITE, ML_POSINF,ML_ERR_return_NAN, ML_ERROR, ME} from "./general" | ||
import {ISNAN, R_FINITE, ML_POSINF,ML_ERR_return_NAN, ML_ERROR, ME} from "./_general" | ||
import {gammafn} from "./gamma" | ||
import { lbeta } from "./lbeta" | ||
const xmin = - 170.5674972726612 | ||
const xmax = 171.61447887182298 | ||
const lnsml = - 708.39641853226412 | ||
//const xmin = - 170.5674972726612; | ||
const xmax = 171.61447887182298; | ||
const lnsml = - 708.39641853226412; | ||
@@ -56,0 +56,0 @@ |
@@ -52,3 +52,3 @@ /* | ||
import { fabs, NaN } from "./general" | ||
import { fabs, NaN } from "./_general" | ||
@@ -55,0 +55,0 @@ export function chebyshev_init(dos: number[], nos: number, eta: number): number { |
@@ -29,3 +29,3 @@ /* | ||
import { ISNAN, R_FINITE, ME, ML_ERROR, ML_NAN, fabs, M_PI, fmod } from "./general"; | ||
import { ISNAN, R_FINITE, ME, ML_ERROR, ML_NAN, fabs, M_PI, fmod } from "./_general"; | ||
@@ -32,0 +32,0 @@ /* HAVE_COSPI etc will not be defined in standalone-use: the |
@@ -27,3 +27,3 @@ /* | ||
import { ISNAN, log, R_D__0} from './general'; | ||
import { ISNAN, log, R_D__0} from './_general'; | ||
@@ -30,0 +30,0 @@ |
231
lib/gamma.ts
@@ -51,7 +51,7 @@ /* | ||
import { ISNAN, ML_ERROR, ME, ML_NAN, fabs, ML_POSINF, ML_NEGINF, M_PI, M_LN_SQRT_2PI, fmod, } from "./general"; | ||
import { chebyshev_eval } from "./chebyshev"; | ||
import { stirlerr } from "./stirlerror"; | ||
import { sinpi } from "./cospi"; | ||
import { lgammacor } from "./lgammacor"; | ||
import { ISNAN, ML_ERROR, ME, ML_NAN, fabs, ML_POSINF, ML_NEGINF, M_PI, M_LN_SQRT_2PI } from './_general'; | ||
import { chebyshev_eval } from './chebyshev'; | ||
import { stirlerr } from './stirlerror'; | ||
import { sinpi } from './cospi'; | ||
import { lgammacor } from './lgammacor'; | ||
@@ -106,3 +106,7 @@ const gamcs: number[] = [ | ||
let i: number, n: number, y: number, sinpiy: number, value: number; | ||
let i: number; | ||
let n: number; | ||
let y: number; | ||
let sinpiy: number; | ||
let value: number; | ||
@@ -130,6 +134,6 @@ /* #ifdef NOMORE_FOR_THREADS | ||
const ngam = 22; | ||
const xmin = -170.5674972726612 | ||
const xmax = 171.61447887182298 | ||
const xsml = 2.2474362225598545e-308 | ||
const dxrel = 1.490116119384765696e-8 | ||
const xmin = -170.5674972726612; | ||
const xmax = 171.61447887182298; | ||
const xsml = 2.2474362225598545e-308; | ||
const dxrel = 1.490116119384765696e-8; | ||
@@ -140,4 +144,4 @@ if (ISNAN(x)) return x; | ||
//then return NaN. | ||
if (x == 0 || (x < 0 && x == Math.round(x))) { | ||
ML_ERROR(ME.ME_DOMAIN, "gammafn"); | ||
if (x === 0 || (x < 0 && x === Math.round(x))) { | ||
ML_ERROR(ME.ME_DOMAIN, 'gammafn'); | ||
return ML_NAN; | ||
@@ -156,7 +160,7 @@ } | ||
if (x < 0)--n; | ||
y = x - n;// n = floor(x) ==> y in [ 0, 1 ) | ||
y = x - n; // n = floor(x) ==> y in [ 0, 1 ) | ||
--n; | ||
value = chebyshev_eval(y * 2 - 1, gamcs, ngam) + .9375; | ||
if (n == 0) | ||
return value;// x = 1.dddd = 1+y | ||
if (n === 0) | ||
return value; // x = 1.dddd = 1+y | ||
@@ -169,3 +173,3 @@ if (n < 0) { | ||
if (x < -0.5 && fabs(x - Math.trunc((x - 0.5)) / x) < dxrel) { | ||
ML_ERROR(ME.ME_PRECISION, "gammafn"); | ||
ML_ERROR(ME.ME_PRECISION, 'gammafn'); | ||
} | ||
@@ -175,3 +179,3 @@ | ||
if (y < xsml) { | ||
ML_ERROR(ME.ME_RANGE, "gammafn"); | ||
ML_ERROR(ME.ME_RANGE, 'gammafn'); | ||
if (x > 0) return ML_POSINF; | ||
@@ -201,3 +205,3 @@ else return ML_NEGINF; | ||
if (x > xmax) { // Overflow | ||
ML_ERROR(ME.ME_RANGE, "gammafn"); | ||
ML_ERROR(ME.ME_RANGE, 'gammafn'); | ||
return ML_POSINF; | ||
@@ -207,7 +211,7 @@ } | ||
if (x < xmin) { // Underflow | ||
ML_ERROR(ME.ME_UNDERFLOW, "gammafn"); | ||
ML_ERROR(ME.ME_UNDERFLOW, 'gammafn'); | ||
return 0.; | ||
} | ||
if (y <= 50 && y == Math.trunc(y)) { // compute (n - 1)! | ||
if (y <= 50 && y === Math.trunc(y)) { // compute (n - 1)! | ||
value = 1.; | ||
@@ -218,3 +222,3 @@ for (i = 2; i < y; i++) value *= i; | ||
value = Math.exp((y - 0.5) * Math.log(y) - y + M_LN_SQRT_2PI + | ||
((2 * y == Math.trunc(2) * y) ? stirlerr(y) : lgammacor(y))); | ||
((2 * y === Math.trunc(2) * y) ? stirlerr(y) : lgammacor(y))); | ||
} | ||
@@ -229,8 +233,8 @@ if (x > 0) | ||
ML_ERROR(ME.ME_PRECISION, "gammafn"); | ||
ML_ERROR(ME.ME_PRECISION, 'gammafn'); | ||
} | ||
sinpiy = sinpi(y); | ||
if (sinpiy == 0) { // Negative integer arg - overflow | ||
ML_ERROR(ME.ME_RANGE, "gammafn"); | ||
if (sinpiy === 0) { // Negative integer arg - overflow | ||
ML_ERROR(ME.ME_RANGE, 'gammafn'); | ||
return ML_POSINF; | ||
@@ -243,180 +247,1 @@ } | ||
/* | ||
double gammafn(double x) | ||
{ | ||
const static double gamcs[42] = { | ||
+.8571195590989331421920062399942e-2, | ||
+.4415381324841006757191315771652e-2, | ||
+.5685043681599363378632664588789e-1, | ||
-.4219835396418560501012500186624e-2, | ||
+.1326808181212460220584006796352e-2, | ||
-.1893024529798880432523947023886e-3, | ||
+.3606925327441245256578082217225e-4, | ||
-.6056761904460864218485548290365e-5, | ||
+.1055829546302283344731823509093e-5, | ||
-.1811967365542384048291855891166e-6, | ||
+.3117724964715322277790254593169e-7, | ||
-.5354219639019687140874081024347e-8, | ||
+.9193275519859588946887786825940e-9, | ||
-.1577941280288339761767423273953e-9, | ||
+.2707980622934954543266540433089e-10, | ||
-.4646818653825730144081661058933e-11, | ||
+.7973350192007419656460767175359e-12, | ||
-.1368078209830916025799499172309e-12, | ||
+.2347319486563800657233471771688e-13, | ||
-.4027432614949066932766570534699e-14, | ||
+.6910051747372100912138336975257e-15, | ||
-.1185584500221992907052387126192e-15, | ||
+.2034148542496373955201026051932e-16, | ||
-.3490054341717405849274012949108e-17, | ||
+.5987993856485305567135051066026e-18, | ||
-.1027378057872228074490069778431e-18, | ||
+.1762702816060529824942759660748e-19, | ||
-.3024320653735306260958772112042e-20, | ||
+.5188914660218397839717833550506e-21, | ||
-.8902770842456576692449251601066e-22, | ||
+.1527474068493342602274596891306e-22, | ||
-.2620731256187362900257328332799e-23, | ||
+.4496464047830538670331046570666e-24, | ||
-.7714712731336877911703901525333e-25, | ||
+.1323635453126044036486572714666e-25, | ||
-.2270999412942928816702313813333e-26, | ||
+.3896418998003991449320816639999e-27, | ||
-.6685198115125953327792127999999e-28, | ||
+.1146998663140024384347613866666e-28, | ||
-.1967938586345134677295103999999e-29, | ||
+.3376448816585338090334890666666e-30, | ||
-.5793070335782135784625493333333e-31 | ||
}; | ||
int i, n; | ||
double y; | ||
double sinpiy, value; | ||
#ifdef NOMORE_FOR_THREADS | ||
static int ngam = 0; | ||
static double xmin = 0, xmax = 0., xsml = 0., dxrel = 0.; | ||
// Initialize machine dependent constants, the first time gamma() is called. | ||
//FIXME for threads ! | ||
if (ngam == 0) { | ||
ngam = chebyshev_init(gamcs, 42, DBL_EPSILON/20);//was .1*d1mach(3) | ||
gammalims(&xmin, &xmax);//-> ./gammalims.c | ||
xsml = exp(fmax2(log(DBL_MIN), -log(DBL_MAX)) + 0.01); | ||
// = exp(.01)*DBL_MIN = 2.247e-308 for IEEE | ||
dxrel = sqrt(DBL_EPSILON);//was sqrt(d1mach(4)) | ||
} | ||
#else | ||
// For IEEE double precision DBL_EPSILON = 2^-52 = 2.220446049250313e-16 : | ||
// (xmin, xmax) are non-trivial, see ./gammalims.c | ||
// xsml = exp(.01)*DBL_MIN | ||
// dxrel = sqrt(DBL_EPSILON) = 2 ^ -26 | ||
// | ||
# define ngam 22 | ||
# define xmin -170.5674972726612 | ||
# define xmax 171.61447887182298 | ||
# define xsml 2.2474362225598545e-308 | ||
# define dxrel 1.490116119384765696e-8 | ||
#endif | ||
if(ISNAN(x)) return x; | ||
//If the argument is exactly zero or a negative integer | ||
//then return NaN. | ||
if (x == 0 || (x < 0 && x == round(x))) { | ||
ML_ERROR(ME_DOMAIN, "gammafn"); | ||
return ML_NAN; | ||
} | ||
y = fabs(x); | ||
if (y <= 10) { | ||
// Compute gamma(x) for -10 <= x <= 10 | ||
// Reduce the interval and find gamma(1 + y) for 0 <= y < 1 | ||
// first of all. | ||
n = (int) x; | ||
if(x < 0) --n; | ||
y = x - n;// n = floor(x) ==> y in [ 0, 1 ) | ||
--n; | ||
value = chebyshev_eval(y * 2 - 1, gamcs, ngam) + .9375; | ||
if (n == 0) | ||
return value;// x = 1.dddd = 1+y | ||
if (n < 0) { | ||
// compute gamma(x) for -10 <= x < 1 | ||
// exact 0 or "-n" checked already above | ||
// The answer is less than half precision | ||
// because x too near a negative integer. | ||
if (x < -0.5 && fabs(x - (int)(x - 0.5) / x) < dxrel) { | ||
ML_ERROR(ME_PRECISION, "gammafn"); | ||
} | ||
// The argument is so close to 0 that the result would overflow. | ||
if (y < xsml) { | ||
ML_ERROR(ME_RANGE, "gammafn"); | ||
if(x > 0) return ML_POSINF; | ||
else return ML_NEGINF; | ||
} | ||
n = -n; | ||
for (i = 0; i < n; i++) { | ||
value /= (x + i); | ||
} | ||
return value; | ||
} | ||
else { | ||
// gamma(x) for 2 <= x <= 10 | ||
for (i = 1; i <= n; i++) { | ||
value *= (y + i); | ||
} | ||
return value; | ||
} | ||
} | ||
else { | ||
// gamma(x) for y = |x| > 10. | ||
if (x > xmax) { // Overflow | ||
ML_ERROR(ME_RANGE, "gammafn"); | ||
return ML_POSINF; | ||
} | ||
if (x < xmin) { // Underflow | ||
ML_ERROR(ME_UNDERFLOW, "gammafn"); | ||
return 0.; | ||
} | ||
if(y <= 50 && y == (int)y) { // compute (n - 1)! | ||
value = 1.; | ||
for (i = 2; i < y; i++) value *= i; | ||
} | ||
else { // normal case | ||
value = exp((y - 0.5) * log(y) - y + M_LN_SQRT_2PI + | ||
((2*y == (int)2*y)? stirlerr(y) : lgammacor(y))); | ||
} | ||
if (x > 0) | ||
return value; | ||
if (fabs((x - (int)(x - 0.5))/x) < dxrel){ | ||
// The answer is less than half precision because | ||
// the argument is too near a negative integer. | ||
ML_ERROR(ME_PRECISION, "gammafn"); | ||
} | ||
sinpiy = sinpi(y); | ||
if (sinpiy == 0) { // Negative integer arg - overflow | ||
ML_ERROR(ME_RANGE, "gammafn"); | ||
return ML_POSINF; | ||
} | ||
return -M_PI / (y * sinpiy * value); | ||
} | ||
} | ||
*/ |
@@ -36,6 +36,6 @@ /* | ||
import { ISNAN, R_FINITE, ML_POSINF, ML_ERR_return_NAN, ML_NEGINF, M_LN_SQRT_2PI } from "./general" | ||
import { lgammacor } from "./lgammacor" | ||
import { lgammafn } from "./lgamma" | ||
import { gammafn } from "./gamma" | ||
import { ISNAN, R_FINITE, ML_POSINF, ML_ERR_return_NAN, ML_NEGINF, M_LN_SQRT_2PI } from './_general'; | ||
import { lgammacor } from './lgammacor'; | ||
import { lgammafn } from './lgamma'; | ||
import { gammafn } from './gamma'; | ||
@@ -42,0 +42,0 @@ |
@@ -50,3 +50,3 @@ /* | ||
import {M_LN_SQRT_PId2, ISNAN, fmod, ML_ERROR, ME, ML_POSINF, fabs, log, M_LN_SQRT_2PI,MATHLIB_WARNING,ML_ERR_return_NAN } from "./general" | ||
import {M_LN_SQRT_PId2, ISNAN, fmod, ML_ERROR, ME, ML_POSINF, fabs, log, M_LN_SQRT_2PI,MATHLIB_WARNING,ML_ERR_return_NAN } from './_general'; | ||
import {gammafn} from "./gamma" | ||
@@ -53,0 +53,0 @@ import {sinpi} from "./cospi" |
@@ -52,3 +52,3 @@ /* | ||
import { ME, ML_ERROR, ML_ERR_return_NAN } from './general' | ||
import { ME, ML_ERROR, ML_ERR_return_NAN } from './_general' | ||
import { chebyshev_eval } from './chebyshev'; | ||
@@ -89,3 +89,3 @@ | ||
else if (x >= xmax) { | ||
ML_ERROR(ME.ME_UNDERFLOW, "lgammacor"); | ||
ML_ERROR(ME.ME_UNDERFLOW, 'lgammacor'); | ||
// allow to underflow below | ||
@@ -92,0 +92,0 @@ } |
@@ -25,3 +25,3 @@ /* | ||
*/ | ||
import { R_FINITE, ML_ERR_return_NAN } from './general'; | ||
import { R_FINITE, ML_ERR_return_NAN } from './_general'; | ||
import { random2_64StepsAsFloat } from './_unif_random'; | ||
@@ -28,0 +28,0 @@ |
@@ -42,4 +42,4 @@ /* | ||
import {M_LN_SQRT_2PI} from "./general" | ||
import {lgammafn} from "./lgamma" | ||
import {M_LN_SQRT_2PI} from './_general'; | ||
import {lgammafn} from './lgamma'; | ||
@@ -80,7 +80,7 @@ const sferr_halves: number[] = [ | ||
const S0 = 0.083333333333333333333 // 1/12 | ||
const S1 = 0.00277777777777777777778 // 1/360 | ||
const S2 = 0.00079365079365079365079365 // 1/1260 | ||
const S3 = 0.000595238095238095238095238 // 1/1680 | ||
const S4 = 0.0008417508417508417508417508// 1/1188 | ||
const S0 = 0.083333333333333333333; // 1/12 | ||
const S1 = 0.00277777777777777777778; // 1/360 | ||
const S2 = 0.00079365079365079365079365; // 1/1260 | ||
const S3 = 0.000595238095238095238095238; // 1/1680 | ||
const S4 = 0.0008417508417508417508417508; // 1/1188 | ||
@@ -100,3 +100,3 @@ | ||
// error for 0, 0.5, 1.0, 1.5, ..., 14.5, 15.0. | ||
let nn:number; | ||
let nn: number; | ||
@@ -187,2 +187,2 @@ if (n <= 15.0) { | ||
} | ||
*/ | ||
*/ |
{ | ||
"name": "lib-r-math.js", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"description": "Javascript Pure Implementation of Statistical R \"core\" numerical libRmath.so", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,2 +20,7 @@ # libRmath.js | ||
|------------------|--------------------|-------------|--------------------| | ||
|gamma_cody.c | gamme_cody.ts| 19 feb 2017| GAMMA function using algo of W. J. Cody, | | ||
|bessel_i.c | bessel_i.ts | 19 feb 2017 | besseli | | ||
|bessel_j.c | bessel_j.ts | 19 feb 2017 | besselj | | ||
|bessel_k.c | bessel_k.ts | 19 feb 2017 | besselK | | ||
|bessel_y.c | bessel_y.ts | 19 feb 2017 | bessely | | ||
|sexp.c | ./sexp.ts| 8-feb-2017 | (Random variates from the standard exponential distribution) exp_rand (internally used) function | | ||
@@ -40,6 +45,6 @@ |dunif.c | ./dunif.ts| 8-feb-2017 | R "dunif" function | | ||
bd0.c | done ./lib/bd0.ts | no | hidden, used by modules dbinom.c ,dpois.c dt.c | | ||
bessel_i.c | TODO | | | | ||
bessel_j.c | TODO | | | | ||
bessel_i.c | done | no | R "besseli" Modified Bessel function of first kind. | | ||
bessel_j.c | done | no | R "besselj" gives the Bessel function of the first kind. | | ||
bessel_k.c | done | no | R "besselK" function http://www.netlib.org/specfun/rkbesl | | ||
bessel_y.c | TODO | | | | ||
bessel_y.c | done | no | R "bessely" gives the Bessel function of the second kind . | | ||
beta.c |done ./lib/beta.ts | no | [beta](https://en.wikipedia.org/wiki/Beta_function) | | ||
@@ -79,3 +84,3 @@ chebyshev.c | done ./lib/chebyshev.ts | no | chebyshev\_init , chebyshev\_eval | | ||
gamma.c |done ./lib/gamma.ts | no | [gammafn](https://en.wikipedia.org/wiki/Gamma_function) | | ||
gamma_cody.c | TODO| | | | ||
gamma_cody.c | done| no | GAMMA function using algo of W. J. Cody, | | ||
gammalims.c |TODO | | | | ||
@@ -82,0 +87,0 @@ i1mach.c |TODO | | | |
176628
29
3794
164