@Override public boolean test(double t) { if (!initialized) { result = t; initialized = true; } else { result = accumulator.applyAsDouble(result, t); } return true; } }
/** * Reduction with binary operator * * @param op the operator * @return Optional with the result of reducing all rows with operator */ default Optional<Double> reduce(DoubleBinaryOperator op) { boolean first = true; double acc = 0.0; for (int i = 0; i < size(); i++) { double d = getDouble(i); if (first) { acc = d; first = false; } else { acc = op.applyAsDouble(acc, d); } } return (first ? Optional.<Double>empty() : Optional.<Double>of(acc)); }
/** * Reduction with binary operator and initial value * * @param initial initial value * @param op the operator * @return the result of reducing initial value and all rows with operator */ default double reduce(double initial, DoubleBinaryOperator op) { double acc = initial; for (int i = 0; i < size(); i++) { acc = op.applyAsDouble(acc, getDouble(i)); } return acc; }
/** * Atomically update the current value using the given {@link DoubleBinaryOperator accumulator function}. The new * value is calculated by applying the accumulator function to the current value and the given {@code updateValue}. * * @param updateValue the value which will be passed into the accumulator function. * @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side * effects), because it will be applied several times if update attempts fail due to concurrent calls. Must * not be {@literal null}. * @return the updated value. * @since 2.2 */ public double accumulateAndGet(double updateValue, DoubleBinaryOperator accumulatorFunction) { Assert.notNull(accumulatorFunction, "Accumulator function must not be null!"); double previousValue, newValue; do { previousValue = get(); newValue = accumulatorFunction.applyAsDouble(previousValue, updateValue); } while (!compareAndSet(previousValue, newValue)); return newValue; }
/** * Atomically update the current value using the given {@link DoubleBinaryOperator accumulator function}. The new * value is calculated by applying the accumulator function to the current value and the given {@code updateValue}. * * @param updateValue the value which will be passed into the accumulator function. * @param accumulatorFunction the function which calculates the value to set. Should be a pure function (no side * effects), because it will be applied several times if update attempts fail due to concurrent calls. Must * not be {@literal null}. * @return the previous value. * @since 2.2 */ public double getAndAccumulate(double updateValue, DoubleBinaryOperator accumulatorFunction) { Assert.notNull(accumulatorFunction, "Accumulator function must not be null!"); double previousValue, newValue; do { previousValue = get(); newValue = accumulatorFunction.applyAsDouble(previousValue, updateValue); } while (!compareAndSet(previousValue, newValue)); return previousValue; }
public final void compute() { final ToDoubleFunction<Map.Entry<K,V>> transformer; final DoubleBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { double r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceEntriesToDoubleTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsDouble(r, transformer.applyAsDouble(p)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V> t = (MapReduceEntriesToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } } }
public final void compute() { final ToDoubleFunction<? super K> transformer; final DoubleBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { double r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceKeysToDoubleTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V> t = (MapReduceKeysToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } } }
public final void compute() { final ToDoubleFunction<? super V> transformer; final DoubleBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { double r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceValuesToDoubleTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V> t = (MapReduceValuesToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } } }
public final void compute() { final ToDoubleBiFunction<? super K, ? super V> transformer; final DoubleBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { double r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceMappingsToDoubleTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V> t = (MapReduceMappingsToDoubleTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsDouble(t.result, s.result); s = t.rights = s.nextRight; } } } } }
@Property public void works(DoubleBinaryOperator f, double first, double second) { double result = f.applyAsDouble(first, second); } }
public static void fillMatching(final double[][] target, final double[][] left, final DoubleBinaryOperator function, final double[][] right) { final int tmpLength = target.length; for (int i = 0; i < tmpLength; i++) { final int tmpInnerLength = target[i].length; for (int j = 0; j < tmpInnerLength; j++) { target[i][j] = function.applyAsDouble(left[i][j], right[i][j]); } } }
private static long apply(DoubleBinaryOperator fn, long v, double x) { double d = Double.longBitsToDouble(v); d = (fn == null) ? d + x : fn.applyAsDouble(d, x); return Double.doubleToRawLongBits(d); }
private static long apply(DoubleBinaryOperator fn, long v, double x) { double d = Double.longBitsToDouble(v); d = (fn == null) ? d + x : fn.applyAsDouble(d, x); return Double.doubleToRawLongBits(d); }
private static void binaryOp(Deque<Double> stack, DoubleBinaryOperator op) { double b = stack.removeFirst(); double a = stack.removeFirst(); stack.addFirst(op.applyAsDouble(a, b)); }
private static void binaryOp(Deque<Double> stack, DoubleBinaryOperator op) { double b = stack.removeFirst(); double a = stack.removeFirst(); stack.addFirst(op.applyAsDouble(a, b)); }
/** When both tensors have the same dimensions, at most one cell matches a cell in the other tensor */ private static Tensor singleSpaceJoin(Tensor a, Tensor b, TensorType joinedType, DoubleBinaryOperator combinator) { Tensor.Builder builder = Tensor.Builder.of(joinedType); for (Iterator<Tensor.Cell> i = a.cellIterator(); i.hasNext(); ) { Map.Entry<TensorAddress, Double> aCell = i.next(); double bCellValue = b.get(aCell.getKey()); if (Double.isNaN(bCellValue)) continue; // no match builder.cell(aCell.getKey(), combinator.applyAsDouble(aCell.getValue(), bCellValue)); } return builder.build(); }
/** * {@inheritDoc} */ @Override public double getInverseCDF(Double p) { ArgChecker.notNull(p, "p"); ArgChecker.isTrue(p >= 0 && p <= 1, "Probability must lie between 0 and 1"); return 2 * _inverseFunction.applyAsDouble(0.5 * _degrees, p); }
@Test public void test() { assertEquals(F.applyAsDouble(SIGMA, T), 0.3719, 1e-4); } }
private Matrix join(final Matrix matrix, final boolean isTwoWay, final DoubleBinaryOperator op) { if (!this.isSameSize(matrix)) { throw new IllegalArgumentException("matrix sizes must be equal"); } final Matrix result = this.create(this.getRowCount(), this.getColumnCount()); this.forEach((r, c, v) -> result.setAtUnchecked(r, c, op.applyAsDouble(v, matrix.getAtUnchecked(r, c)))); if (isTwoWay) { matrix.forEach((r, c, v) -> result.setAtUnchecked(r, c, op.applyAsDouble(v, this.getAtUnchecked(r, c)))); } return result; }
@Test public void testPredictedValue() { double[] z; for (int i = 0; i < 10; i++) { z = new double[] {RANDOM.nextDouble(), RANDOM.nextDouble() }; assertEquals(F1.applyAsDouble(z[0], z[1]), NO_INTERCEPT.getPredictedValue(z), EPS); assertEquals(F2.applyAsDouble(z[0], z[1]), INTERCEPT.getPredictedValue(z), EPS); } }