Skip to content
Snippets Groups Projects
Commit 7b3a8b08 authored by stepan's avatar stepan
Browse files

Implement rwilcox

parent 80cefdbf
No related branches found
No related tags found
No related merge requests found
......@@ -166,7 +166,7 @@ public final class RandGenerationFunctions {
double aValue = a.getDataAt(i % aLength);
double bValue = b.getDataAt(i % bLength);
double value = function.evaluate(aValue, bValue, rand);
if (Double.isNaN(value)) {
if (Double.isNaN(value) || Double.isNaN(value)) {
profiles.nan.enter();
nans = true;
}
......
/*
* This material is distributed under the GNU General Public License
* Version 2. You may review the terms of this license at
* http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright (c) 1999--2014, The R Core Team
* Copyright (c) 2016, Oracle and/or its affiliates
*
* All rights reserved.
*/
package com.oracle.truffle.r.library.stats;
import static com.oracle.truffle.r.runtime.RError.Message.CALLOC_COULD_NOT_ALLOCATE_INF;
import com.oracle.truffle.r.library.stats.RandGenerationFunctions.RandFunction2_Double;
import com.oracle.truffle.r.library.stats.RandGenerationFunctions.RandomNumberProvider;
import com.oracle.truffle.r.runtime.RError;
import com.oracle.truffle.r.runtime.RRuntime;
public final class Wilcox {
public static final class RWilcox implements RandFunction2_Double {
@Override
public double evaluate(double m, double n, RandomNumberProvider rand) {
int i;
int j;
int k;
double r;
/* NaNs propagated correctly */
if (Double.isNaN(m) || Double.isNaN(n)) {
return (m + n);
}
if (!Double.isFinite(m) || !Double.isFinite(n)) {
// GnuR does not check this and tries to allocate the memory, we do check this, but
// fail with the same error message for compatibility reasons.
throw RError.error(RError.SHOW_CALLER, CALLOC_COULD_NOT_ALLOCATE_INF);
}
m = Math.round(m);
n = Math.round(n);
if ((m < 0) || (n < 0)) {
// TODO: for some reason the macro in GNUR here returns NA instead of NaN...
// return StatsUtil.mlError();
return RRuntime.DOUBLE_NA;
}
if ((m == 0) || (n == 0)) {
return (0);
}
r = 0.0;
k = (int) (m + n);
int[] x = new int[k];
for (i = 0; i < k; i++) {
x[i] = i;
}
for (i = 0; i < n; i++) {
j = (int) Math.floor(k * rand.unifRand());
r += x[j];
x[j] = x[--k];
}
return (r - n * (n - 1) / 2);
}
}
}
......@@ -70,6 +70,7 @@ import com.oracle.truffle.r.library.stats.SplineFunctionsFactory.SplineCoefNodeG
import com.oracle.truffle.r.library.stats.SplineFunctionsFactory.SplineEvalNodeGen;
import com.oracle.truffle.r.library.stats.StatsFunctionsFactory;
import com.oracle.truffle.r.library.stats.StatsUtil;
import com.oracle.truffle.r.library.stats.Wilcox.RWilcox;
import com.oracle.truffle.r.library.tools.C_ParseRdNodeGen;
import com.oracle.truffle.r.library.tools.DirChmodNodeGen;
import com.oracle.truffle.r.library.tools.Rmd5NodeGen;
......@@ -390,6 +391,8 @@ public class ForeignFunctions {
return RandGenerationFunctionsFactory.Function2_DoubleNodeGen.create(new RNchisq());
case "rnbinom_mu":
return RandGenerationFunctionsFactory.Function2_DoubleNodeGen.create(new RNbinomMu());
case "rwilcox":
return RandGenerationFunctionsFactory.Function2_DoubleNodeGen.create(new RWilcox());
case "qgamma":
return StatsFunctionsFactory.Function3_2NodeGen.create(new QgammaFunc());
case "dbinom":
......
......@@ -457,6 +457,7 @@ public final class RError extends RuntimeException {
UNUSED_ARGUMENT("unused argument (%s)"),
UNUSED_ARGUMENTS("unused arguments (%s)"),
INFINITE_MISSING_VALUES("infinite or missing values in '%s'"),
CALLOC_COULD_NOT_ALLOCATE_INF("'Calloc' could not allocate memory (18446744071562067968 of 4 bytes)"),
NON_SQUARE_MATRIX("non-square matrix in '%s'"),
LAPACK_ERROR("error code %d from Lapack routine '%s'"),
VALUE_OUT_OF_RANGE("value out of range in '%s'"),
......
......@@ -31,7 +31,7 @@ import com.oracle.truffle.r.test.TestBase;
* tests for its specific corner cases if those are not covered here.
*/
public class TestRandGenerationFunctions extends TestBase {
private static final String[] FUNCTION2_NAMES = {"rnorm", "runif", "rgamma", "rbeta", "rcauchy", "rf", "rlogis", "rweibull", "rchisq"};
private static final String[] FUNCTION2_NAMES = {"rnorm", "runif", "rgamma", "rbeta", "rcauchy", "rf", "rlogis", "rweibull", "rchisq", "rwilcox"};
private static final String[] FUNCTION2_PARAMS = {
"10, 10, 10",
"20, c(-1, 0, 0.2, 2:5), c(-1, 0, 0.1, 0.9, 3)",
......
......@@ -64,6 +64,7 @@ com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RLogis.java,
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Rf.java,gnu_r_ihaka_core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RChisq.java,gnu_r_ihaka_core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RNchisq.java,gnu_r.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Wilcox.java,gnu_r.core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RWeibull.java,gnu_r_ihaka_core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/DirChmod.java,gnu_r.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/tools/ToolsText.java,gnu_r.copyright
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment