diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/BinDist.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/BinDist.java new file mode 100644 index 0000000000000000000000000000000000000000..2df583879a906d0a99ce1ed127850e9ec01c904a --- /dev/null +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/BinDist.java @@ -0,0 +1,80 @@ +/* + * 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) 1996-2012, The R Core Team + * Copyright (c) 2005, The R Foundation + * Copyright (c) 2017, Oracle and/or its affiliates + * + * All rights reserved. + */ +/* Acknowledgement from the original header: + * "HACKED" to allow weights by Adrian Baddeley + * Changes indicated by 'AB' + */ +package com.oracle.truffle.r.library.stats; + +import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.gt0; +import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.intNA; +import static com.oracle.truffle.r.runtime.RError.NO_CALLER; + +import java.util.Arrays; + +import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.r.nodes.builtin.CastBuilder; +import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode; +import com.oracle.truffle.r.runtime.RError.Message; +import com.oracle.truffle.r.runtime.RRuntime; +import com.oracle.truffle.r.runtime.data.RDataFactory; +import com.oracle.truffle.r.runtime.data.RDoubleVector; +import com.oracle.truffle.r.runtime.data.model.RAbstractDoubleVector; + +/** + * Implements the C_BinDist external. + */ +public abstract class BinDist extends RExternalBuiltinNode.Arg5 { + public static BinDist create() { + return BinDistNodeGen.create(); + } + + @Override + protected void createCasts(CastBuilder casts) { + casts.arg(0).asDoubleVector(); + casts.arg(1).asDoubleVector(); + casts.arg(2).asDoubleVector().findFirst(); + casts.arg(3).asDoubleVector().findFirst(); + casts.arg(4).asIntegerVector().findFirst().mustBe(gt0().and(intNA().not()), NO_CALLER, Message.INVALID_ARGUMENT, "n"); + } + + @Specialization + RDoubleVector bindist(RAbstractDoubleVector x, RAbstractDoubleVector w, double xlo, double xhi, int n) { + int ixmin = 0; + int ixmax = n - 2; + double xdelta = (xhi - xlo) / (n - 1); + double[] result = new double[2 * n]; + Arrays.fill(result, 0); + int wLength = w.getLength(); + + for (int i = 0; i < x.getLength(); i++) { + if (RRuntime.isFinite(x.getDataAt(i))) { + double xpos = (x.getDataAt(i) - xlo) / xdelta; + if (!Double.isFinite(xpos)) { + continue; + } + int ix = (int) Math.floor(xpos); + double fx = xpos - ix; + double wi = w.getDataAt(i % wLength); + if (ixmin <= ix && ix <= ixmax) { + result[ix] += (1 - fx) * wi; + result[ix + 1] += fx * wi; + } else if (ix == -1) { + result[0] += fx * wi; + } else if (ix == ixmax + 1) { + result[ix] += (1 - fx) * wi; + } + } + } + return RDataFactory.createDoubleVector(result, RDataFactory.COMPLETE_VECTOR); + } +} diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/CallAndExternalFunctions.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/CallAndExternalFunctions.java index 1c9a963a3b3d7295ea6b767315e95187e9db7072..ce574740684cddb53bcd2983f1495d6190491a3b 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/CallAndExternalFunctions.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/CallAndExternalFunctions.java @@ -40,6 +40,7 @@ import com.oracle.truffle.r.library.methods.SlotFactory.R_getSlotNodeGen; import com.oracle.truffle.r.library.methods.SlotFactory.R_setSlotNodeGen; import com.oracle.truffle.r.library.methods.SubstituteDirectNodeGen; import com.oracle.truffle.r.library.parallel.ParallelFunctionsFactory.MCIsChildNodeGen; +import com.oracle.truffle.r.library.stats.BinDist; import com.oracle.truffle.r.library.stats.CdistNodeGen; import com.oracle.truffle.r.library.stats.CompleteCases; import com.oracle.truffle.r.library.stats.CovcorNodeGen; @@ -494,6 +495,8 @@ public class CallAndExternalFunctions { return DoubleCentreNodeGen.create(); case "cutree": return CutreeNodeGen.create(); + case "BinDist": + return BinDist.create(); case "isoreg": case "monoFC_m": case "numeric_deriv": @@ -536,7 +539,6 @@ public class CallAndExternalFunctions { case "cfilter": case "rfilter": case "lowess": - case "BinDist": case "Rsm": case "tukeyline": case "runmed": diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test index 11101209e074ae2526f79f00b4611c232bd34823..461c59d9fe2fa4c2cea15b48b88153cf1c7ab4ee 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test @@ -130056,6 +130056,39 @@ In qwilcox(log(c(0, 4.2e-79, 0.1, 0.5, 0.7, 1 - 4.2e-79, 1)), 4, : #qt(0.8312011718749999778, 5.42101086242752217e-20, 34.00390632153467152, lower.tail=T, log.p=F) [1] Inf +##com.oracle.truffle.r.test.library.stats.TestExternal_BinDist.testBinDist# +#.Call(stats:::C_BinDist, c('1','2','3'), c(4,5,6), 0, c(3, 4), c('5', '8')) + [1] 0.000000 2.666667 3.000000 3.333333 6.000000 0.000000 0.000000 0.000000 + [9] 0.000000 0.000000 + +##com.oracle.truffle.r.test.library.stats.TestExternal_BinDist.testBinDist# +#.Call(stats:::C_BinDist, c(0,0,0), c(1,2,3), 1, 10, 10) + [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + +##com.oracle.truffle.r.test.library.stats.TestExternal_BinDist.testBinDist# +#.Call(stats:::C_BinDist, c(1,2,3), c(4,5,6), 0, 3, 5) + [1] 0.000000 2.666667 3.000000 3.333333 6.000000 0.000000 0.000000 0.000000 + [9] 0.000000 0.000000 + +##com.oracle.truffle.r.test.library.stats.TestExternal_BinDist.testBinDist# +#.Call(stats:::C_BinDist, c(2.2,4,3), 5:7, -1, 5, 8) + [1] 0.000000 0.000000 0.000000 1.333333 6.000000 5.666667 5.000000 0.000000 + [9] 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 + +##com.oracle.truffle.r.test.library.stats.TestExternal_BinDist.testBinDistWrongArgs#Output.IgnoreWarningContext# +#.Call(stats:::C_BinDist, 0, 0, 'string', 3, 5) + [1] 0 0 0 0 0 0 0 0 0 0 +Warning message: +NAs introduced by coercion + +##com.oracle.truffle.r.test.library.stats.TestExternal_BinDist.testBinDistWrongArgs# +#.Call(stats:::C_BinDist, c(1,2,3), c(4,5,6), 0, 3, -5L) +Error: invalid 'n' argument + +##com.oracle.truffle.r.test.library.stats.TestExternal_BinDist.testBinDistWrongArgs# +#.Call(stats:::C_BinDist, c(1,2,3), c(4,5,6), 0, 3, c(NA, 3L)) +Error: invalid 'n' argument + ##com.oracle.truffle.r.test.library.stats.TestExternal_covcor.testCovcor# #.Call(stats:::C_cov, 1:5, 1:5, 4, FALSE) [1] 2.5 diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/stats/TestExternal_BinDist.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/stats/TestExternal_BinDist.java new file mode 100644 index 0000000000000000000000000000000000000000..2d8525a759b25452c31dfe7d7fb5a8f0f0ac8265 --- /dev/null +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/stats/TestExternal_BinDist.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.r.test.library.stats; + +import org.junit.Test; + +import com.oracle.truffle.r.test.TestBase; + +public class TestExternal_BinDist extends TestBase { + @Test + public void testBinDist() { + assertEval(".Call(stats:::C_BinDist, c(1,2,3), c(4,5,6), 0, 3, 5)"); + assertEval(".Call(stats:::C_BinDist, c('1','2','3'), c(4,5,6), 0, c(3, 4), c('5', '8'))"); + assertEval(".Call(stats:::C_BinDist, c(0,0,0), c(1,2,3), 1, 10, 10)"); + assertEval(".Call(stats:::C_BinDist, c(2.2,4,3), 5:7, -1, 5, 8)"); + } + + @Test + public void testBinDistWrongArgs() { + assertEval(Output.IgnoreWarningContext, ".Call(stats:::C_BinDist, 0, 0, 'string', 3, 5)"); + assertEval(".Call(stats:::C_BinDist, c(1,2,3), c(4,5,6), 0, 3, c(NA, 3L))"); + assertEval(".Call(stats:::C_BinDist, c(1,2,3), c(4,5,6), 0, 3, -5L)"); + } +} diff --git a/mx.fastr/copyrights/overrides b/mx.fastr/copyrights/overrides index c62113ad16ccb4ec439c056d315759303bb745ce..012e56015970dcfb0066ab7f11d1688a8630bd34 100644 --- a/mx.fastr/copyrights/overrides +++ b/mx.fastr/copyrights/overrides @@ -36,6 +36,7 @@ com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nmath/distr/Cauchy com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nmath/distr/QTukey.java,gnu_r_ihaka_core.copyright com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/nmath/distr/PTukey.java,gnu_r_ihaka_core.copyright com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Cdist.java,gnu_r_gentleman_ihaka.copyright +com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/BinDist.java,gnu_r.copyright com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/CompleteCases.java,gnu_r_gentleman_ihaka2.copyright com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Covcor.java,gnu_r.copyright com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RandFunctionsNodes.java,gnu_r_gentleman_ihaka.copyright