diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DBeta.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DBeta.java index d7f3839013c0a57016716592c3618467114164cd..4b60d50296b000f4429f2319139819b1356b9cf7 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DBeta.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DBeta.java @@ -16,7 +16,7 @@ import static com.oracle.truffle.r.library.stats.LBeta.lbeta; import com.oracle.truffle.r.library.stats.StatsFunctions.Function3_1; -public class DBeta implements Function3_1 { +public final class DBeta implements Function3_1 { @Override public double evaluate(double x, double a, double b, boolean log) { /* NaNs propagated correctly */ diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DHyper.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DHyper.java index 5e9496495f0f2323e784fe07a27a48c734b22735..1b569cd453911da9fad0b91b87b5804672d57dba 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DHyper.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DHyper.java @@ -41,24 +41,24 @@ public final class DHyper implements Function4_1 { return e.result; } - x = RMath.forceint(x); - r = RMath.forceint(r); - b = RMath.forceint(b); - n = RMath.forceint(n); + double ix = RMath.forceint(x); + double ir = RMath.forceint(r); + double ib = RMath.forceint(b); + double in = RMath.forceint(n); - if (n < x || r < x || n - x > b) { + if (in < ix || ir < ix || in - ix > ib) { return DPQ.rd0(giveLog); } - if (n == 0) { - return (x == 0) ? DPQ.rd1(giveLog) : DPQ.rd0(giveLog); + if (in == 0) { + return (ix == 0) ? DPQ.rd1(giveLog) : DPQ.rd0(giveLog); } - double p = n / (r + b); - double q = (r + b - n) / (r + b); + double p = in / (ir + ib); + double q = (ir + ib - in) / (ir + ib); - double p1 = dbinomRaw(x, r, p, q, giveLog); - double p2 = dbinomRaw(n - x, b, p, q, giveLog); - double p3 = dbinomRaw(n, r + b, p, q, giveLog); + double p1 = dbinomRaw(ix, ir, p, q, giveLog); + double p2 = dbinomRaw(in - ix, ib, p, q, giveLog); + double p3 = dbinomRaw(in, ir + ib, p, q, giveLog); return (giveLog) ? p1 + p2 - p3 : p1 * p2 / p3; } diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DNBeta.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DNBeta.java index 2a6fe146eb926f9c48ad9c23d71add58310e4cb1..c357b66df5d85cd3cc187e6fde1abc17771e07b1 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DNBeta.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DNBeta.java @@ -15,7 +15,7 @@ import static com.oracle.truffle.r.library.stats.GammaFunctions.dpoisRaw; import com.oracle.truffle.r.library.stats.StatsFunctions.Function4_1; -public class DNBeta implements Function4_1 { +public final class DNBeta implements Function4_1 { private static final double eps = 1.e-15; private final DBeta dbeta = new DBeta(); diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DNChisq.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DNChisq.java index 55515c3247d87af6f3b93b66b58ba2675f713bb8..8fe5845b3441e24e8921e601459456e7347eb6ac 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DNChisq.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DNChisq.java @@ -17,7 +17,7 @@ import static com.oracle.truffle.r.library.stats.GammaFunctions.dpoisRaw; import com.oracle.truffle.r.library.stats.Chisq.DChisq; import com.oracle.truffle.r.library.stats.StatsFunctions.Function3_1; -public class DNChisq implements Function3_1 { +public final class DNChisq implements Function3_1 { private static final double eps = 5e-15; private final DChisq dchisq = new DChisq(); @@ -85,24 +85,24 @@ public class DNChisq implements Function3_1 { /* upper tail */ /* LDOUBLE */double sum = mid; /* LDOUBLE */double term = mid; - df = dfmid; + double df2 = dfmid; double i = imax; double x2 = x * ncp2; double q; do { i++; - q = x2 / i / df; - df += 2; + q = x2 / i / df2; + df2 += 2; term *= q; sum += term; } while (q >= 1 || term * q > (1 - q) * eps || term > 1e-10 * sum); /* lower tail */ term = mid; - df = dfmid; + df2 = dfmid; i = imax; while (i != 0) { - df -= 2; - q = i * df / x2; + df2 -= 2; + q = i * df2 / x2; i--; term *= q; sum += term; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dnf.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dnf.java index 4b4713273b341e906316620b62f057d411884ea9..2afc8f5e130857118685fbf31b2c191a7e564833 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dnf.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dnf.java @@ -20,7 +20,7 @@ import static com.oracle.truffle.r.library.stats.GammaFunctions.dgamma; import com.oracle.truffle.r.library.stats.StatsFunctions.Function4_1; -public class Dnf implements Function4_1 { +public final class Dnf implements Function4_1 { private final DNChisq dnchisq = new DNChisq(); private final DNBeta dnbeta = new DNBeta(); diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dt.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dt.java index 7ae62d4958a193eee729c85463648881b25ae187..45eb01fe6b8bd4ead373dccfced2c307d3e50321 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dt.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dt.java @@ -13,7 +13,6 @@ package com.oracle.truffle.r.library.stats; import static com.oracle.truffle.r.library.stats.GammaFunctions.bd0; -import static com.oracle.truffle.r.library.stats.GammaFunctions.dnorm; import static com.oracle.truffle.r.library.stats.GammaFunctions.stirlerr; import static com.oracle.truffle.r.library.stats.MathConstants.DBL_EPSILON; import static com.oracle.truffle.r.library.stats.MathConstants.M_1_SQRT_2PI; @@ -22,6 +21,8 @@ import static com.oracle.truffle.r.library.stats.MathConstants.M_LN_SQRT_2PI; import com.oracle.truffle.r.library.stats.StatsFunctions.Function2_1; public final class Dt implements Function2_1 { + private static final DNorm dnorm = new DNorm(); + @Override public double evaluate(double x, double n, boolean giveLog) { if (Double.isNaN(x) || Double.isNaN(n)) { @@ -36,7 +37,7 @@ public final class Dt implements Function2_1 { return DPQ.rd0(giveLog); } if (!Double.isFinite(n)) { - return dnorm(x, 0., 1., giveLog); + return dnorm.evaluate(x, 0., 1., giveLog); } double u; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/GammaFunctions.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/GammaFunctions.java index 18c55e3ee9f79d79a4ee41c400b4f66f9d635988..8b221ce9340d4d5f9701d510a3ac14de224879ac 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/GammaFunctions.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/GammaFunctions.java @@ -1152,7 +1152,7 @@ public abstract class GammaFunctions { } f = res12 / elfb; - np = pnorm(s2pt, 0.0, 1.0, !lowerTail, logp); + np = new Pnorm().evaluate(s2pt, 0.0, 1.0, !lowerTail, logp); if (logp) { double ndOverP = dpnorm(s2pt, !lowerTail, np); @@ -1369,45 +1369,6 @@ public abstract class GammaFunctions { return giveLog ? pr - Math.log(scale) : pr / scale; } - // - // pnorm - // - - static double pnorm(double x, double mu, double sigma, boolean lowerTail, boolean logp) { - double p; - double cp = 0; - double localX = x; - - /* - * Note: The structure of these checks has been carefully thought through. For example, if x - * == mu and sigma == 0, we get the correct answer 1. - */ - if (Double.isNaN(localX) || Double.isNaN(mu) || Double.isNaN(sigma)) { - return localX + mu + sigma; - } - if (!RRuntime.isFinite(localX) && mu == localX) { - return Double.NaN; /* x-mu is NaN */ - } - if (sigma <= 0) { - if (sigma < 0) { - return RMathError.defaultError(); - } - /* sigma = 0 : */ - return (localX < mu) ? rdt0(lowerTail, logp) : rdt1(lowerTail, logp); - } - p = (localX - mu) / sigma; - if (!RRuntime.isFinite(p)) { - return (localX < mu) ? rdt0(lowerTail, logp) : rdt1(lowerTail, logp); - } - localX = p; - - double[] pa = new double[]{p}; - double[] cpa = new double[]{cp}; - pnormBoth(localX, pa, cpa, (lowerTail ? 0 : 1), logp); - - return lowerTail ? pa[0] : cpa[0]; - } - private static final double SIXTEN = 16; /* Cutoff allowing exact "*" and "/" */ @CompilationFinal private static final double[] pba = new double[]{2.2352520354606839287, 161.02823106855587881, 1067.6894854603709582, 18154.981253343561249, 0.065682337918207449113}; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Logis.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Logis.java index a6bc1963fc21cd4d007e14b6d1d66e7f815e5985..b810fbae9f35275e8b574f23c3dfd7c8ac127714 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Logis.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Logis.java @@ -28,15 +28,15 @@ public final class Logis { public static final class DLogis implements Function3_1 { @Override - public double evaluate(double x, double location, double scale, boolean giveLog) { - if (Double.isNaN(x) || Double.isNaN(location) || Double.isNaN(scale)) { - return x + location + scale; + public double evaluate(double xIn, double location, double scale, boolean giveLog) { + if (Double.isNaN(xIn) || Double.isNaN(location) || Double.isNaN(scale)) { + return xIn + location + scale; } if (scale <= 0.0) { return RMathError.defaultError(); } - x = TOMS708.fabs((x - location) / scale); + double x = TOMS708.fabs((xIn - location) / scale); double e = Math.exp(-x); double f = 1.0 + e; return giveLog ? -(x + Math.log(scale * f * f)) : e / (scale * f * f); @@ -64,31 +64,32 @@ public final class Logis { } /* p := logit(p) = Math.log( p / (1-p) ) : */ + double newP; if (logP) { if (lowerTail) { - p = p - DPQ.rlog1exp(p); + newP = p - DPQ.rlog1exp(p); } else { - p = DPQ.rlog1exp(p) - p; + newP = DPQ.rlog1exp(p) - p; } } else { - p = Math.log(lowerTail ? (p / (1. - p)) : ((1. - p) / p)); + newP = Math.log(lowerTail ? (p / (1. - p)) : ((1. - p) / p)); } - return location + scale * p; + return location + scale * newP; } } public static final class PLogis implements Function3_2 { @Override - public double evaluate(double x, double location, double scale, boolean lowerTail, boolean logP) { - if (Double.isNaN(x) || Double.isNaN(location) || Double.isNaN(scale)) { - return x + location + scale; + public double evaluate(double xIn, double location, double scale, boolean lowerTail, boolean logP) { + if (Double.isNaN(xIn) || Double.isNaN(location) || Double.isNaN(scale)) { + return xIn + location + scale; } if (scale <= 0.0) { return RMathError.defaultError(); } - x = (x - location) / scale; + double x = (xIn - location) / scale; if (Double.isNaN(x)) { return RMathError.defaultError(); } diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PGamma.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PGamma.java index 16ab8bd2e3d4479bba0dedb8b614503f661e320a..5886a01b16ae9cf1c6e7ae0f0a9fb72f194d8b1f 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PGamma.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PGamma.java @@ -16,7 +16,7 @@ import static com.oracle.truffle.r.library.stats.GammaFunctions.pgammaRaw; import com.oracle.truffle.r.library.stats.StatsFunctions.Function3_2; -public class PGamma implements Function3_2 { +public final class PGamma implements Function3_2 { @Override public double evaluate(double xIn, double alph, double scale, boolean lowerTail, boolean logP) { if (Double.isNaN(xIn) || Double.isNaN(alph) || Double.isNaN(scale)) { diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PHyper.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PHyper.java index 5a52dd603231eb1033985c12898d9d46d25b43fe..5a6922a91a1ae2e6f0af36d53bfbb8409d1be409 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PHyper.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PHyper.java @@ -29,21 +29,22 @@ public final class PHyper implements Function4_2 { private final DHyper dhyper = new DHyper(); @Override - public double evaluate(double x, double nr, double nb, double n, boolean lowerTail, boolean logP) { + public double evaluate(double xIn, double nrIn, double nbIn, double nIn, boolean lowerTailIn, boolean logP) { /* Sample of n balls from nr red and nb black ones; x are red */ - if (Double.isNaN(x) || Double.isNaN(nr) || Double.isNaN(nb) || Double.isNaN(n)) { - return x + nr + nb + n; + if (Double.isNaN(xIn) || Double.isNaN(nrIn) || Double.isNaN(nbIn) || Double.isNaN(nIn)) { + return xIn + nrIn + nbIn + nIn; } - x = Math.floor(x + 1e-7); - nr = RMath.forceint(nr); - nb = RMath.forceint(nb); - n = RMath.forceint(n); + double x = Math.floor(xIn + 1e-7); + double nr = RMath.forceint(nrIn); + double nb = RMath.forceint(nbIn); + double n = RMath.forceint(nIn); if (nr < 0 || nb < 0 || !Double.isFinite(nr + nb) || n < 0 || n > nr + nb) { return RMathError.defaultError(); } + boolean lowerTail = lowerTailIn; if (x * (nr + nb) > n * nr) { /* Swap tails. */ double oldNB = nb; @@ -66,7 +67,7 @@ public final class PHyper implements Function4_2 { return logP ? DPQ.rdtlog(d + pd, lowerTail, logP) : DPQ.rdlval(d * pd, lowerTail); } - static double pdhyper(double x, double nr, double nb, double n, boolean logP) { + static double pdhyper(double xIn, double nr, double nb, double n, boolean logP) { /* * Calculate * @@ -81,6 +82,7 @@ public final class PHyper implements Function4_2 { /* LDOUBLE */double sum = 0; /* LDOUBLE */double term = 1; + double x = xIn; while (x > 0 && term >= DBL_EPSILON * sum) { term *= x * (nb - n + x) / (n + 1 - x) / (nr + 1 - x); sum += term; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PNBeta.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PNBeta.java index 7734338fc8bb15fc79e2ee565225937defd79f91..012c7b2b16d0878f6b2128d164f3c7313b1d370a 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PNBeta.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PNBeta.java @@ -17,7 +17,7 @@ import com.oracle.truffle.r.library.stats.RMathError.MLError; import com.oracle.truffle.r.library.stats.StatsFunctions.Function4_2; import com.oracle.truffle.r.library.stats.TOMS708.Bratio; -public class PNBeta implements Function4_2 { +public final class PNBeta implements Function4_2 { @Override public double evaluate(double x, double a, double b, double ncp, boolean lowerTail, boolean logP) { if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b) || Double.isNaN(ncp)) { @@ -99,7 +99,7 @@ public class PNBeta implements Function4_2 { /* LDOUBLE */double sumq = 1. - q; /* LDOUBLE */double ans = q * temp; - /* LDOUBLE */double ax = ans; + /* LDOUBLE */double ax; /* recurse over subsequent terms until convergence is achieved */ double j = Math.floor(x0); // x0 could be billions, and is in package EnvStats diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PNChisq.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PNChisq.java index 561fd2740d8b730871432f8ce1fa6706ebd45234..c776e2c447895032b09072188ef2f05d050d5db4 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PNChisq.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PNChisq.java @@ -38,7 +38,7 @@ import com.oracle.truffle.r.library.stats.RMathError.MLError; import com.oracle.truffle.r.library.stats.StatsFunctions.Function3_2; import com.oracle.truffle.r.runtime.RError.Message; -public class PNChisq implements Function3_2 { +public final class PNChisq implements Function3_2 { private static final double _dbl_min_exp = M_LN2 * DBL_MIN_EXP; private final PChisq pchisq = new PChisq(); @@ -284,7 +284,8 @@ public class PNChisq implements Function3_2 { } } - private void debugPrintf(@SuppressWarnings("unused") String fmt, @SuppressWarnings("unused") Object... args) { + @SuppressWarnings("unused") + private void debugPrintf(String fmt, Object... args) { // System.out.printf(fmt + "\n", args); } diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PPois.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PPois.java index 1d15fa313533e6f9ffe2d39751405c0c3b4dbe3e..1ad3492f1ffb50c308b4f932f294dfc9bcd34c3f 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PPois.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/PPois.java @@ -15,7 +15,7 @@ import static com.oracle.truffle.r.library.stats.GammaFunctions.pgamma; import com.oracle.truffle.r.library.stats.StatsFunctions.Function2_2; -public class PPois implements Function2_2 { +public final class PPois implements Function2_2 { @Override public double evaluate(double x, double lambda, boolean lowerTail, boolean logP) { if (Double.isNaN(x) || Double.isNaN(lambda) || lambda < 0.) { @@ -30,8 +30,8 @@ public class PPois implements Function2_2 { if (!Double.isFinite(x)) { return DPQ.rdt1(lowerTail, logP); } - x = Math.floor(x + 1e-7); + double floorX = Math.floor(x + 1e-7); - return pgamma(lambda, x + 1, 1., !lowerTail, logP); + return pgamma(lambda, floorX + 1, 1., !lowerTail, logP); } } diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Pnf.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Pnf.java index 56ff4406e9a194b3fd901926ce30bf814d414e11..a48d487b9d972af9bb63f87a42f635fa67e2352e 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Pnf.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Pnf.java @@ -14,7 +14,7 @@ package com.oracle.truffle.r.library.stats; import com.oracle.truffle.r.library.stats.DPQ.EarlyReturn; import com.oracle.truffle.r.library.stats.StatsFunctions.Function4_2; -public class Pnf implements Function4_2 { +public final class Pnf implements Function4_2 { private final PNChisq pnchisq = new PNChisq(); private final PNBeta pnbeta = new PNBeta(); diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Pt.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Pt.java index fd2e60684a0cef5f1b32e4445056eabe88f48251..f1ef9bf8a51c42b38e04316414d2ced717bcf10a 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Pt.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Pt.java @@ -11,7 +11,6 @@ */ package com.oracle.truffle.r.library.stats; -import static com.oracle.truffle.r.library.stats.GammaFunctions.pnorm; import static com.oracle.truffle.r.library.stats.LBeta.lbeta; import static com.oracle.truffle.r.library.stats.MathConstants.M_LN2; import static com.oracle.truffle.r.library.stats.Pbeta.pbeta; @@ -19,8 +18,9 @@ import static com.oracle.truffle.r.library.stats.Pbeta.pbeta; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.r.library.stats.StatsFunctions.Function2_2; -public class Pt implements Function2_2 { +public final class Pt implements Function2_2 { private final BranchProfile pbetaNanProfile = BranchProfile.create(); + private final Pnorm pnorm = new Pnorm(); @Override public double evaluate(double x, double n, boolean lowerTail, boolean logP) { @@ -41,7 +41,7 @@ public class Pt implements Function2_2 { return (x < 0) ? DPQ.rdt0(lowerTail, logP) : DPQ.rdt1(lowerTail, logP); } if (!Double.isFinite(n)) { - return pnorm(x, 0.0, 1.0, lowerTail, logP); + return pnorm.evaluate(x, 0.0, 1.0, lowerTail, logP); } double nx = 1 + (x / n) * x; @@ -66,19 +66,17 @@ public class Pt implements Function2_2 { } /* Use "1 - v" if lower_tail and x > 0 (but not both): */ - if (x <= 0.) { - lowerTail = !lowerTail; - } + boolean newLowerTail = x <= 0. ? !lowerTail : lowerTail; if (logP) { - if (lowerTail) { + if (newLowerTail) { return RMath.log1p(-0.5 * Math.exp(val)); } else { return val - M_LN2; /* = Math.log(.5* pbeta(....)) */ } } else { val /= 2.; - return DPQ.rdcval(val, lowerTail); + return DPQ.rdcval(val, newLowerTail); } } diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QBeta.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QBeta.java index c704b4b59411031634a7d966c5ed60de81e2d7fd..8e8c7512edb90b564a5677756a68258f040b6c3a 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QBeta.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QBeta.java @@ -61,7 +61,8 @@ public final class QBeta implements Function3_2 { /** * Debugging printfs should print exactly the same output as GnuR, this can help with debugging. */ - private static void debugPrintf(@SuppressWarnings("unused") String fmt, @SuppressWarnings("unused") Object... args) { + @SuppressWarnings("unused") + private static void debugPrintf(String fmt, Object... args) { // System.out.printf(fmt + "\n", args); } @@ -84,7 +85,7 @@ public final class QBeta implements Function3_2 { // The fields and variables are named after local variables from GnuR, we keep the original // names for the ease of debugging // Checkstyle: stop field name check - private static class QBetaRawMethod { + private static final class QBetaRawMethod { private boolean give_log_q; private boolean use_log_x; private boolean add_N_step; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QNBeta.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QNBeta.java index ea00bc27cf0cd1b62cd7f87db5e6e7439f7263e1..ccbd2f19b13549768e8656333228e3b19e8c22bd 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QNBeta.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QNBeta.java @@ -16,16 +16,16 @@ import static com.oracle.truffle.r.library.stats.MathConstants.DBL_MIN; import com.oracle.truffle.r.library.stats.DPQ.EarlyReturn; import com.oracle.truffle.r.library.stats.StatsFunctions.Function4_2; -public class QNBeta implements Function4_2 { +public final class QNBeta implements Function4_2 { private static final double accu = 1e-15; private static final double Eps = 1e-14; /* must be > accu */ private final PNBeta pnbeta = new PNBeta(); @Override - public double evaluate(double p, double a, double b, double ncp, boolean lowerTail, boolean logP) { - if (Double.isNaN(p) || Double.isNaN(a) || Double.isNaN(b) || Double.isNaN(ncp)) { - return p + a + b + ncp; + public double evaluate(double pIn, double a, double b, double ncp, boolean lowerTail, boolean logP) { + if (Double.isNaN(pIn) || Double.isNaN(a) || Double.isNaN(b) || Double.isNaN(ncp)) { + return pIn + a + b + ncp; } if (!Double.isFinite(a)) { return RMathError.defaultError(); @@ -36,12 +36,12 @@ public class QNBeta implements Function4_2 { } try { - DPQ.rqp01boundaries(p, 0, 1, lowerTail, logP); + DPQ.rqp01boundaries(pIn, 0, 1, lowerTail, logP); } catch (EarlyReturn e) { return e.result; } - p = DPQ.rdtqiv(p, lowerTail, logP); + double p = DPQ.rdtqiv(pIn, lowerTail, logP); /* * Invert pnbeta(.) : 1. finding an upper and lower bound diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QNChisq.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QNChisq.java index 897f0875c98f71d88f6393779ab981589b33aa01..6673c76ce1cae3cb9c37a56ed8c5bc2e16d7b8f4 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QNChisq.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QNChisq.java @@ -21,7 +21,7 @@ import com.oracle.truffle.r.library.stats.DPQ.EarlyReturn; import com.oracle.truffle.r.library.stats.RMathError.MLError; import com.oracle.truffle.r.library.stats.StatsFunctions.Function3_2; -public class QNChisq implements Function3_2 { +public final class QNChisq implements Function3_2 { private static final double accu = 1e-13; private static final double racc = 4 * DBL_EPSILON; /* these two are for the "search" loops, can have less accuracy: */ @@ -32,21 +32,21 @@ public class QNChisq implements Function3_2 { private final PNChisq pnchisq = new PNChisq(); @Override - public double evaluate(double p, double df, double ncp, boolean lowerTail, boolean logP) { - if (Double.isNaN(p) || Double.isNaN(df) || Double.isNaN(ncp)) { - return p + df + ncp; + public double evaluate(double pIn, double df, double ncp, boolean lowerTail, boolean logP) { + if (Double.isNaN(pIn) || Double.isNaN(df) || Double.isNaN(ncp)) { + return pIn + df + ncp; } if (!Double.isFinite(df) || df < 0 || ncp < 0) { return RMathError.defaultError(); } try { - DPQ.rqp01boundaries(p, 0, Double.POSITIVE_INFINITY, lowerTail, logP); + DPQ.rqp01boundaries(pIn, 0, Double.POSITIVE_INFINITY, lowerTail, logP); } catch (EarlyReturn e) { return e.result; } - double pp = DPQ.rdqiv(p, logP); + double pp = DPQ.rdqiv(pIn, logP); if (pp > 1 - DBL_EPSILON) { return lowerTail ? Double.POSITIVE_INFINITY : 0.0; } @@ -61,30 +61,32 @@ public class QNChisq implements Function3_2 { double b = (ncp * ncp) / (df + 3 * ncp); double c = (df + 3 * ncp) / (df + 2 * ncp); double ff = (df + 2 * ncp) / (c * c); - double ux = b + c * qchisq.evaluate(p, ff, lowerTail, logP); + double ux = b + c * qchisq.evaluate(pIn, ff, lowerTail, logP); if (ux < 0) { ux = 1; } double ux0 = ux; - if (!lowerTail && ncp >= 80) { + double p = pIn; + boolean newLowerTail = lowerTail; + if (!newLowerTail && ncp >= 80) { /* in this case, pnchisq() works via lower_tail = true */ if (pp < 1e-10) { RMathError.error(MLError.PRECISION, "qnchisq"); } - p = DPQ.rdtqiv(p, lowerTail, logP); - lowerTail = true; + p = DPQ.rdtqiv(p, newLowerTail, logP); + newLowerTail = true; } else { p = pp; } pp = RMath.fmin2(1 - DBL_EPSILON, p * (1 + Eps)); - while (ux < DBL_MAX && isLower(lowerTail, pnchisq.pnchisqRaw(ux, df, ncp, Eps, rEps, 10000, lowerTail, false), pp)) { + while (ux < DBL_MAX && isLower(newLowerTail, pnchisq.pnchisqRaw(ux, df, ncp, Eps, rEps, 10000, newLowerTail, false), pp)) { ux *= 2; } pp = p * (1 - Eps); double lx = RMath.fmin2(ux0, DBL_MAX); - while (lx > DBL_MIN && isGreater(lowerTail, pnchisq.pnchisqRaw(lx, df, ncp, Eps, rEps, 10000, lowerTail, false), pp)) { + while (lx > DBL_MIN && isGreater(newLowerTail, pnchisq.pnchisqRaw(lx, df, ncp, Eps, rEps, 10000, newLowerTail, false), pp)) { lx *= 0.5; } @@ -92,8 +94,8 @@ public class QNChisq implements Function3_2 { double nx; do { nx = 0.5 * (lx + ux); - double raw = pnchisq.pnchisqRaw(nx, df, ncp, accu, racc, 100000, lowerTail, false); - if (isGreater(lowerTail, raw, p)) { + double raw = pnchisq.pnchisqRaw(nx, df, ncp, accu, racc, 100000, newLowerTail, false); + if (isGreater(newLowerTail, raw, p)) { ux = nx; } else { lx = nx; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QPois.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QPois.java index 34cbad5586728c66abb3153db046256b3f62d13d..95f1096a5de95f3192ba8b7460bf39c22932f149 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QPois.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/QPois.java @@ -16,14 +16,14 @@ import static com.oracle.truffle.r.library.stats.MathConstants.DBL_EPSILON; import com.oracle.truffle.r.library.stats.DPQ.EarlyReturn; import com.oracle.truffle.r.library.stats.StatsFunctions.Function2_2; -public class QPois implements Function2_2 { +public final class QPois implements Function2_2 { private final Qnorm qnorm = new Qnorm(); private final PPois ppois = new PPois(); @Override - public double evaluate(double p, double lambda, boolean lowerTail, boolean logP) { - if (Double.isNaN(p) || Double.isNaN(lambda)) { - return p + lambda; + public double evaluate(double pIn, double lambda, boolean lowerTail, boolean logP) { + if (Double.isNaN(pIn) || Double.isNaN(lambda)) { + return pIn + lambda; } if (!Double.isFinite(lambda)) { return RMathError.defaultError(); @@ -36,7 +36,7 @@ public class QPois implements Function2_2 { } try { - DPQ.rqp01boundaries(p, 0, Double.POSITIVE_INFINITY, lowerTail, logP); + DPQ.rqp01boundaries(pIn, 0, Double.POSITIVE_INFINITY, lowerTail, logP); } catch (EarlyReturn e) { return e.result; } @@ -50,6 +50,7 @@ public class QPois implements Function2_2 { * Note : "same" code in qpois.c, qbinom.c, qnbinom.c -- FIXME: This is far from optimal * [cancellation for p ~= 1, etc]: */ + double p = pIn; if (!lowerTail || logP) { p = DPQ.rdtqiv(p, lowerTail, logP); /* need check again (cancellation!): */ if (p == 0.) { @@ -109,7 +110,7 @@ public class QPois implements Function2_2 { double y = yIn; for (;;) { y = y + incr; - double z = zIn; + double z; if ((z = ppois.evaluate(y, lambda, /* l._t. */true, /* log_p */false)) >= p) { return new SearchResult(y, z); } diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qf.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qf.java index 4c0274673717603470b161e05700b6789d0bfeaa..94d0967d1c800f55b0e4636aa4d8d84f5c178279 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qf.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qf.java @@ -54,7 +54,7 @@ public final class Qf implements Function3_2 { } // FIXME: (1/qb - 1) = (1 - qb)/qb; if we know qb ~= 1, should use other tail - p = (1. / qbeta.evaluate(p, df2 / 2, df1 / 2, !lowerTail, logP) - 1.) * (df2 / df1); - return RMath.mlValid(p) ? p : Double.NaN; + double newP = (1. / qbeta.evaluate(p, df2 / 2, df1 / 2, !lowerTail, logP) - 1.) * (df2 / df1); + return RMath.mlValid(newP) ? newP : Double.NaN; } } diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qnf.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qnf.java index 7f06ef8e578ab80d6f28e4a67ba58ba53a8b45ae..54a6acf636c3f2224d467dd98b754912cff380ea 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qnf.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qnf.java @@ -13,7 +13,7 @@ package com.oracle.truffle.r.library.stats; import com.oracle.truffle.r.library.stats.DPQ.EarlyReturn; import com.oracle.truffle.r.library.stats.StatsFunctions.Function4_2; -public class Qnf implements Function4_2 { +public final class Qnf implements Function4_2 { private final QNChisq qnchisq = new QNChisq(); private final QNBeta qnbeta = new QNBeta(); diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qt.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qt.java index 2c4fd0191a3882cb62a484eac535e06cc20411e4..5c527504357c63d421a371b0f3df14191f277cbd 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qt.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Qt.java @@ -24,7 +24,7 @@ import static com.oracle.truffle.r.library.stats.MathConstants.M_SQRT2; import com.oracle.truffle.r.library.stats.DPQ.EarlyReturn; import com.oracle.truffle.r.library.stats.StatsFunctions.Function2_2; -public class Qt implements Function2_2 { +public final class Qt implements Function2_2 { private static final double eps = 1.e-12; private static final double accu = 1e-13; private static final double Eps = 1e-11; /* must be > accu */ @@ -34,14 +34,14 @@ public class Qt implements Function2_2 { private final Pt pt = new Pt(); @Override - public double evaluate(double p, double ndf, boolean lowerTail, boolean logP) { + public double evaluate(double pIn, double ndf, boolean lowerTail, boolean logP) { - if (Double.isNaN(p) || Double.isNaN(ndf)) { - return p + ndf; + if (Double.isNaN(pIn) || Double.isNaN(ndf)) { + return pIn + ndf; } try { - DPQ.rqp01boundaries(p, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, lowerTail, logP); + DPQ.rqp01boundaries(pIn, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, lowerTail, logP); } catch (EarlyReturn earlyReturn) { return earlyReturn.result; } @@ -50,6 +50,7 @@ public class Qt implements Function2_2 { return RMathError.defaultError(); } + double p = pIn; if (ndf < 1) { /* based on qnt */ int iter = 0; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RBeta.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RBeta.java index 7fab48d93886d5aec51fc209396f7eeef3442128..cc59560caa6b01c6559d67e1f37e6559e0e4e3ea 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RBeta.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RBeta.java @@ -24,6 +24,15 @@ public final class RBeta extends RandFunction2_Double { private static final double expmax = (DBL_MAX_EXP * M_LN2); /* = log(DBL_MAX) */ + // TODO: state variables + private static double beta = 0; + private static double gamma = 1; + private static double delta; + private static double k1 = 0; + private static double k2 = 0; + private static double olda = -1.0; + private static double oldb = -1.0; + @Override public double execute(double aa, double bb, RandomNumberProvider rand) { if (Double.isNaN(aa) || Double.isNaN(bb) || aa < 0. || bb < 0.) { @@ -43,18 +52,6 @@ public final class RBeta extends RandFunction2_Double { return 0.0; } - double v = 0; - double w = 0; - - // TODO: state variables - double beta = 0; - double gamma = 1; - double delta; - double k1 = 0; - double k2 = 0; - double olda = -1.0; - double oldb = -1.0; - /* Test if we need new "initializing" */ boolean qsame = (olda == aa) && (oldb == bb); if (!qsame) { @@ -66,6 +63,9 @@ public final class RBeta extends RandFunction2_Double { double b = fmax2(aa, bb); /* a <= b */ double alpha = a + b; + double v; + double w; + if (a <= 1.0) { /* --- Algorithm BC --- */ /* changed notation, now also a <= b (was reversed) */ if (!qsame) { /* initialize */ diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RGamma.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RGamma.java index 8d344b1625e7e68963c496047d185e8fd2603b6b..f3546d5e1a7b174d106d48e902dfef71e6a1aa83 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RGamma.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RGamma.java @@ -16,7 +16,7 @@ import static com.oracle.truffle.r.library.stats.TOMS708.fabs; import com.oracle.truffle.r.library.stats.RandGenerationFunctions.RandFunction2_Double; import com.oracle.truffle.r.library.stats.RandGenerationFunctions.RandomNumberProvider; -public class RGamma extends RandFunction2_Double { +public final class RGamma extends RandFunction2_Double { private static final double sqrt32 = 5.656854; private static final double exp_m1 = 0.36787944117144232159; /* exp(-1) = 1/e */ @@ -44,8 +44,8 @@ public class RGamma extends RandFunction2_Double { public double execute(double a, double scale, RandomNumberProvider rand) { // TODO: state variables - double aa = 0.; - double aaa = 0.; + double aa = Double.NaN; + double aaa = Double.NaN; double s = 0; double s2 = 0; double d = 0; /* no. 1 (step 1) */ diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RHyper.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RHyper.java index a233c0c549c1faa414ec875030ad7d5d56845c0b..fb549a90f9e39d7262d5793b9ef3b3cdff6ddfe4 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RHyper.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RHyper.java @@ -73,7 +73,14 @@ public final class RHyper extends RandFunction3_Double { private static double w; // III: // Checkstyle: stop - private static double a, d, s, xl, xr, kl, kr, lamdl, lamdr, p1, p2, p3; + private static double a; + private static double xl; + private static double xr; + private static double lamdl; + private static double lamdr; + private static double p1; + private static double p2; + private static double p3; // // Checkstyle: resume private static final double scale = 1e25; // scaling factor against (early) underflow @@ -201,18 +208,19 @@ public final class RHyper extends RandFunction3_Double { double u; double v; + double s; if (setup1 || setup2) { s = Math.sqrt((tn - k) * k * n1 * n2 / (tn - 1) / tn / tn); /* remark: d is defined in reference without int. */ /* the truncation centers the cell boundaries at 0.5 */ - d = (int) (1.5 * s) + .5; + double d = (int) (1.5 * s) + .5; xl = m - d + .5; xr = m + d + .5; a = afc(m) + afc(n1 - m) + afc(k - m) + afc(n2 - k + m); - kl = Math.exp(a - afc((int) (xl)) - afc((int) (n1 - xl)) - afc((int) (k - xl)) - afc((int) (n2 - k + xl))); - kr = Math.exp(a - afc((int) (xr - 1)) - afc((int) (n1 - xr + 1)) - afc((int) (k - xr + 1)) - afc((int) (n2 - k + xr - 1))); + double kl = Math.exp(a - afc((int) (xl)) - afc((int) (n1 - xl)) - afc((int) (k - xl)) - afc((int) (n2 - k + xl))); + double kr = Math.exp(a - afc((int) (xr - 1)) - afc((int) (n1 - xr + 1)) - afc((int) (k - xr + 1)) - afc((int) (n2 - k + xr - 1))); lamdl = -Math.log(xl * (n2 - k + xl) / (n1 - xl + 1) / (k - xl + 1)); lamdr = -Math.log((n1 - xr + 1) * (k - xr + 1) / xr / (n2 - k + xr)); p1 = d + d; @@ -329,11 +337,7 @@ public final class RHyper extends RandFunction3_Double { /* * * Stirling's formula to machine accuracy */ - if (alv <= (a - afc(ix) - afc(n1 - ix) - afc(k - ix) - afc(n2 - k + ix))) { - reject = false; - } else { - reject = true; - } + reject = !(alv <= (a - afc(ix) - afc(n1 - ix) - afc(k - ix) - afc(n2 - k + ix))); } } } // else diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMath.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMath.java index a7f1cd01da654d6c683b5f33d355995655b0e976..68347359d023a0f292c66d2618558cbe1c009954 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMath.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMath.java @@ -27,7 +27,7 @@ import com.oracle.truffle.r.runtime.RRuntime; * @see RMathError * @see MathConstants */ -public class RMath { +public final class RMath { public static boolean mlValid(double d) { return !Double.isNaN(d); diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinom.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinom.java index f6ec7325bbd3dff6af4f600aa1afdfea39ea27d8..9a0f80cd9a3702d9990ca94a00aeb02b60e4c37a 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinom.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RMultinom.java @@ -13,10 +13,10 @@ package com.oracle.truffle.r.library.stats; import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.notIntNA; -import static com.oracle.truffle.r.runtime.RError.SHOW_CALLER; import static com.oracle.truffle.r.runtime.RError.Message.NA_IN_PROB_VECTOR; import static com.oracle.truffle.r.runtime.RError.Message.NEGATIVE_PROBABILITY; import static com.oracle.truffle.r.runtime.RError.Message.NO_POSITIVE_PROBABILITIES; +import static com.oracle.truffle.r.runtime.RError.SHOW_CALLER; import java.math.BigDecimal; import java.math.RoundingMode; @@ -89,16 +89,16 @@ public abstract class RMultinom extends RExternalBuiltinNode.Arg3 { private static void fixupProb(double[] p) { double sum = 0.0; int npos = 0; - for (int i = 0; i < p.length; i++) { - if (!Double.isFinite(p[i])) { + for (double prob : p) { + if (!Double.isFinite(prob)) { throw RError.error(SHOW_CALLER, NA_IN_PROB_VECTOR); } - if (p[i] < 0.0) { + if (prob < 0.0) { throw RError.error(SHOW_CALLER, NEGATIVE_PROBABILITY); } - if (p[i] > 0.0) { + if (prob > 0.0) { npos++; - sum += p[i]; + sum += prob; } } if (npos == 0) { diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RPois.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RPois.java index 8ed6d66484b423f7b4c830ec6660764b4c9bb56f..36759275c32b8477b5fd67b7a68502abfa804f70 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RPois.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RPois.java @@ -40,8 +40,6 @@ public final class RPois extends RandFunction1_Double { // TODO: state variables private static int l = 0; private static int m = 0; - private static double b1; - private static double b2; private static double c = 0; private static double c0 = 0; private static double c1 = 0; @@ -193,8 +191,8 @@ public final class RPois extends RandFunction1_Double { * discrete normal probabilities fk. */ - b1 = one_24 / mu; - b2 = 0.3 * b1 * b1; + double b1 = one_24 / mu; + double b2 = 0.3 * b1 * b1; c3 = one_7 * b1 * b2; c2 = b2 - 15. * c3; c1 = b1 - 6. * b2 + 45. * c3; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RandGenerationFunctions.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RandGenerationFunctions.java index 19717a9b865e7c364a7b4c24cc8de51b0e1824a1..61aae8184e43a191d9c6f38f7941902da30f50dd 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RandGenerationFunctions.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RandGenerationFunctions.java @@ -184,8 +184,7 @@ public final class RandGenerationFunctions { RandGenerationNodeData nodeData) { int length = nodeData.resultVectorLengthProfile.profile(convertToLength.execute(lengthVec)); RNode.reportWork(this, length); - Object result = evaluate(length, a, b, c, nodeData, rand); - return result; + return evaluate(length, a, b, c, nodeData, rand); } @SuppressWarnings("unused") @@ -308,12 +307,14 @@ public final class RandGenerationFunctions { return RandFunction3NodeGen.create(RandGenerationFunctionsFactory.RandFunctionIntExecutorNodeGen.create(function)); } + // Note: for completeness of the API + @SuppressWarnings("unused") public static RandFunction3Node createDouble(RandFunction3_Double function) { return RandFunction3NodeGen.create(RandGenerationFunctionsFactory.RandFunctionDoubleExecutorNodeGen.create(function)); } @Override - protected void createCasts(CastBuilder casts) { + protected final void createCasts(CastBuilder casts) { ConvertToLength.addLengthCast(casts); casts.arg(1).asDoubleVector(); casts.arg(2).asDoubleVector(); @@ -343,7 +344,7 @@ public final class RandGenerationFunctions { } @Override - protected void createCasts(CastBuilder casts) { + protected final void createCasts(CastBuilder casts) { ConvertToLength.addLengthCast(casts); casts.arg(1).asDoubleVector(); casts.arg(2).asDoubleVector(); @@ -372,7 +373,7 @@ public final class RandGenerationFunctions { } @Override - protected void createCasts(CastBuilder casts) { + protected final void createCasts(CastBuilder casts) { ConvertToLength.addLengthCast(casts); casts.arg(1).asDoubleVector(); } diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Random2.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Random2.java index 729f0d0833ed777de42e12fc552206ce7d12f58d..de77422aefb34b79d6788ebc94672fe500c0c1be 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Random2.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Random2.java @@ -17,7 +17,10 @@ import static com.oracle.truffle.r.library.stats.DPQ.rdtqiv; /* * Logic derived from GNU-R, see inline comments. */ -public class Random2 { +public final class Random2 { + private Random2() { + // only static members + } // from GNUR: qnorm.c public static double qnorm5(double p, double mu, double sigma, boolean lowerTail, boolean logP) { diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Rbinom.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Rbinom.java index 37964392e6762bbac3cebd294e474e15b73c57cc..4f9080aae6e2193cc0313879512fbe35dcdd331c 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Rbinom.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Rbinom.java @@ -22,6 +22,9 @@ public final class Rbinom extends RandFunction2_Double { private final Qbinom qbinom = new Qbinom(); + // TODO: some of the variables below are static in GnuR, because they cache intermediate results + // that depend on paremeters that often do not change between calls. + @Override public double execute(double nin, double pp, RandomNumberProvider rand) { double psave = -1.0; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/TOMS708.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/TOMS708.java index f335b9f7b1c29ec9b320e822ce49d48e54e6c971..9f0ee17d7b44b2a40a399ec10a63b0beccd7c870 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/TOMS708.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/TOMS708.java @@ -25,7 +25,10 @@ import com.oracle.truffle.r.runtime.RRuntime; * transcribed from toms708.c - as the original file contains no copyright header, we assume that it is copyright R code and R foundation. */ -public class TOMS708 { +public final class TOMS708 { + private TOMS708() { + // only static members + } @SuppressWarnings("unused") private static void debugPrintf(String format, Object... args) { diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RErrorHandling.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RErrorHandling.java index 9fcf43914e98f05039582e68d6acd43abf911c10..6b055c7debfdeefaa0b4d6e0bf930a149c029d47 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RErrorHandling.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RErrorHandling.java @@ -634,7 +634,7 @@ public class RErrorHandling { throw RInternalError.unimplemented(); } else if (w == 1) { Utils.writeStderr(message, true); - } else if (w == 0) { + } else if (w == 0 && errorHandlingState.warnings.size() < errorHandlingState.maxWarnings) { errorHandlingState.warnings.add(new Warning(fmsg, call)); } } finally { @@ -695,7 +695,7 @@ public class RErrorHandling { if (nWarnings < errorHandlingState.maxWarnings) { Utils.writeStderr(String.format("There were %d warnings (use warnings() to see them)", nWarnings), true); } else { - nWarnings = errorHandlingState.maxWarnings; + assert nWarnings == errorHandlingState.maxWarnings : "warnings above the limit should not have been added"; Utils.writeStderr(String.format("There were %d or more warnings (use warnings() to see the first %d)", nWarnings, nWarnings), true); } } diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/stats/TestDistributions.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/stats/TestDistributions.java index 54841d07cd4906fc83f11d5136cf5efe764a9c62..b372e721801c039ad33509331914c61ad6b62b46 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/stats/TestDistributions.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/stats/TestDistributions.java @@ -228,6 +228,12 @@ public class TestDistributions extends TestBase { return this; } + /** + * Removes the predefined "error" values for parameters. These are so far always the same, + * but this method is here to make the API "complete". May be removed if all distributions + * already have tests and non of them needs it. + */ + @SuppressWarnings("unused") public DistrTest clearDefaultErrorParamValues() { errorParamValues.clear(); return this;