Search in sources :

Example 1 with DoubleBinaryOperator

use of java.util.function.DoubleBinaryOperator in project jdk8u_jdk by JetBrains.

the class PrimitiveSumMinMaxTest method testDoubleMethods.

public void testDoubleMethods() {
    BinaryOperator<Double> sum1 = Double::sum;
    DoubleBinaryOperator sum2 = Double::sum;
    BinaryOperator<Double> max1 = Double::max;
    DoubleBinaryOperator max2 = Double::max;
    BinaryOperator<Double> min1 = Double::min;
    DoubleBinaryOperator min2 = Double::min;
    Comparator<Double> cmp = Double::compare;
    double[] numbers = { -1, 0, 1, 100, Double.MAX_VALUE, Double.MIN_VALUE };
    for (double i : numbers) {
        for (double j : numbers) {
            assertEquals(i + j, (double) sum1.apply(i, j));
            assertEquals(i + j, sum2.applyAsDouble(i, j));
            assertEquals(Math.max(i, j), (double) max1.apply(i, j));
            assertEquals(Math.max(i, j), max2.applyAsDouble(i, j));
            assertEquals(Math.min(i, j), (double) min1.apply(i, j));
            assertEquals(Math.min(i, j), min2.applyAsDouble(i, j));
            assertEquals(((Double) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
Also used : DoubleBinaryOperator(java.util.function.DoubleBinaryOperator)

Example 2 with DoubleBinaryOperator

use of java.util.function.DoubleBinaryOperator in project jdk8u_jdk by JetBrains.

the class Serial method testDoubleAccumulator.

static void testDoubleAccumulator() {
    DoubleBinaryOperator plus = (DoubleBinaryOperator & Serializable) (x, y) -> x + y;
    DoubleAccumulator a = new DoubleAccumulator(plus, 13.9d);
    a.accumulate(17.5d);
    DoubleAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");
    checkSerialClassName(a, "java.util.concurrent.atomic.DoubleAccumulator$SerializationProxy");
}
Also used : DoubleBinaryOperator(java.util.function.DoubleBinaryOperator) DoubleAccumulator(java.util.concurrent.atomic.DoubleAccumulator)

Example 3 with DoubleBinaryOperator

use of java.util.function.DoubleBinaryOperator in project graal by oracle.

the class TruffleTCK method testFunctionAddNumbers.

/**
 * @since 0.16
 */
@Test
public void testFunctionAddNumbers() throws Exception {
    String id = functionAddNumbers();
    if (id == null) {
        return;
    }
    PolyglotEngine.Value apply = findGlobalSymbol(id);
    TruffleObject truffleObject = (TruffleObject) apply.execute().get();
    assertIsObjectOfLanguage(truffleObject);
    DoubleBinaryOperator object = JavaInterop.asJavaFunction(DoubleBinaryOperator.class, truffleObject);
    Assert.assertEquals(42.0, object.applyAsDouble(20.0, 22.0), 0.1);
}
Also used : DoubleBinaryOperator(java.util.function.DoubleBinaryOperator) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 4 with DoubleBinaryOperator

use of java.util.function.DoubleBinaryOperator in project vespa by vespa-engine.

the class Select method lazyGetFunction.

@Override
protected TensorFunction lazyGetFunction() {
    if (!allInputFunctionsPresent(3)) {
        return null;
    }
    TensorFlowOperation conditionOperation = inputs().get(0);
    TensorFunction a = inputs().get(1).function().get();
    TensorFunction b = inputs().get(2).function().get();
    // Shortcut: if we know during import which tensor to select, do that directly here.
    if (conditionOperation.getConstantValue().isPresent()) {
        Tensor condition = conditionOperation.getConstantValue().get().asTensor();
        if (condition.type().rank() == 0) {
            return ((int) condition.asDouble() == 0) ? b : a;
        }
        if (condition.type().rank() == 1 && dimensionSize(condition.type().dimensions().get(0)) == 1) {
            return condition.cellIterator().next().getValue().intValue() == 0 ? b : a;
        }
    }
    // The task is to select cells from 'x' or 'y' based on 'condition'.
    // If 'condition' is 0 (false), select from 'y', if 1 (true) select
    // from 'x'. We do this by individually joining 'x' and 'y' with
    // 'condition', and then joining the resulting two tensors.
    TensorFunction conditionFunction = conditionOperation.function().get();
    TensorFunction aCond = new com.yahoo.tensor.functions.Join(a, conditionFunction, ScalarFunctions.multiply());
    TensorFunction bCond = new com.yahoo.tensor.functions.Join(b, conditionFunction, new DoubleBinaryOperator() {

        @Override
        public double applyAsDouble(double a, double b) {
            return a * (1.0 - b);
        }

        @Override
        public String toString() {
            return "f(a,b)(a * (1-b))";
        }
    });
    return new com.yahoo.tensor.functions.Join(aCond, bCond, ScalarFunctions.add());
}
Also used : Tensor(com.yahoo.tensor.Tensor) DoubleBinaryOperator(java.util.function.DoubleBinaryOperator) TensorFunction(com.yahoo.tensor.functions.TensorFunction)

Aggregations

DoubleBinaryOperator (java.util.function.DoubleBinaryOperator)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 Tensor (com.yahoo.tensor.Tensor)1 TensorFunction (com.yahoo.tensor.functions.TensorFunction)1 DoubleAccumulator (java.util.concurrent.atomic.DoubleAccumulator)1 Test (org.junit.Test)1