/** * Form a new polynomial by operating on each like term of self and another polynomial. * This and the other is not changed. * * @param operator how to combine each term of this with each term of the `another` polynomial. * @param another the other polynomial involved in the computation. * @return the resulting polynomial. */ protected Polynomial linearlyMergedUsing(DoubleBinaryOperator operator, /*with*/ Polynomial another) { // Validate int thisDegree = getDegree(); int thatDegree = another.getDegree(); while (another.getCoefficientForExponent(thatDegree) == 0) thatDegree--; // Allocate according to the highest degree int maxDegree = Math.max(thisDegree, thatDegree); double[] resultCoefficientsInNaturalOrder = new double[maxDegree + 1]; // Apply operator on each term for (int exponent = maxDegree, i = 0; exponent >= 0; exponent--, i++) { if (exponent <= thisDegree) resultCoefficientsInNaturalOrder[i] += getCoefficientForExponent(exponent); if (exponent <= thatDegree) resultCoefficientsInNaturalOrder[i] = operator.applyAsDouble( resultCoefficientsInNaturalOrder[i], another.getCoefficientForExponent(exponent) ); } return new ArrayBasedPoly(resultCoefficientsInNaturalOrder); }
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)); } } }
@NonNull @Override public MuVector2d map(@NonNull final Vector2d that, @NonNull final DoubleBinaryOperator operator) { this.x = operator.applyAsDouble(this.x, that.x()); this.y = operator.applyAsDouble(this.y, that.y()); return this; }
@NonNull @Override public MuVector3d map(@NonNull final Vector3d that, @NonNull final DoubleBinaryOperator operator) { this.x = operator.applyAsDouble(this.x, that.x()); this.y = operator.applyAsDouble(this.y, that.y()); this.z = operator.applyAsDouble(this.z, that.z()); return this; }
@NonNull @Override public MuVector4d map(@NonNull final Vector4d that, @NonNull final DoubleBinaryOperator operator) { this.x = operator.applyAsDouble(this.x, that.x()); this.y = operator.applyAsDouble(this.y, that.y()); this.z = operator.applyAsDouble(this.z, that.z()); this.w = operator.applyAsDouble(this.w, that.w()); return this; }
@NonNull @Override public MuVector3f map(@NonNull final Vector3f that, @NonNull final DoubleBinaryOperator operator) { this.x = (float) operator.applyAsDouble(this.x, that.x()); this.y = (float) operator.applyAsDouble(this.y, that.y()); this.z = (float) operator.applyAsDouble(this.z, that.z()); return this; }
@NonNull @Override public MuVector2f map(@NonNull final Vector2f that, @NonNull final DoubleBinaryOperator operator) { this.x = (float) operator.applyAsDouble(this.x, that.x()); this.y = (float) operator.applyAsDouble(this.y, that.y()); return this; }
/** * Form a new polynomial by operating on each like term of self and another polynomial. * A non-existing term is treated as a term with coefficient of 0. * This and the other is not changed. * * @param operator how to combine each pair of like terms of this and `another` polynomial. * @param another the other polynomial involved in the computation. * @return the resulting polynomial. * @implSpec Complexity: O(n) */ protected Polynomial linearlyMergedUsing(DoubleBinaryOperator operator, /*with*/ Polynomial another) { // Validate int thisDegree = getDegree(); int thatDegree = another.getDegree(); while (another.getCoefficientForExponent(thatDegree) == 0) thatDegree--; // Allocate according to the highest degree int maxDegree = Math.max(thisDegree, thatDegree); TermListNode newHead = null, curTail = null, i = terms, /*for efficiency*/ tmp, copy; double coefficient; // Apply operator on each term for (int exponent = maxDegree; exponent >= 0; exponent--) { coefficient = 0; if (exponent <= thisDegree) if ((tmp = getTermForExponent(exponent, i)) != null) coefficient = (i = tmp).getCoefficient(); if (exponent <= thatDegree) coefficient = operator.applyAsDouble(coefficient, another.getCoefficientForExponent(exponent)); if (coefficient == 0) continue; // Skip null copy = new TermListNode(exponent, coefficient, null); if (newHead == null) newHead = curTail = copy; else curTail.setNext(curTail = copy); } SinglyBasedPolynomial result = new SinglyBasedPolynomial(); if (newHead != null) result.terms = newHead; return result; }
/** * Constructs a {@code TerminalOp} that implements a functional reduce on * {@code double} values. * * @param identity the identity for the combining function * @param operator the combining function * @return a {@code TerminalOp} implementing the reduction */ public static TerminalOp<Double, Double> makeDouble(double identity, DoubleBinaryOperator operator) { Objects.requireNonNull(operator); class ReducingSink implements AccumulatingSink<Double, Double, ReducingSink>, Sink.OfDouble { private double state; @Override public void begin(long size) { state = identity; } @Override public void accept(double t) { state = operator.applyAsDouble(state, t); } @Override public Double get() { return state; } @Override public void combine(ReducingSink other) { accept(other.state); } } return new ReduceOp<Double, Double, ReducingSink>(StreamShape.DOUBLE_VALUE) { @Override public ReducingSink makeSink() { return new ReducingSink(); } }; }
/** Root task constructor */ public DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function, double[] array, int lo, int hi) { super(parent); this.function = function; this.array = array; this.lo = this.origin = lo; this.hi = this.fence = hi; int p; this.threshold = (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3)) <= MIN_PARTITION ? MIN_PARTITION : p; }
/** Subtask constructor */ DoubleCumulateTask(DoubleCumulateTask parent, DoubleBinaryOperator function, double[] array, int origin, int fence, int threshold, int lo, int hi) { super(parent); this.function = function; this.array = array; this.origin = origin; this.fence = fence; this.threshold = threshold; this.lo = lo; this.hi = hi; }
MapReduceKeysToDoubleTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceKeysToDoubleTask<K,V> nextRight, ToDoubleFunction<? super K> transformer, double basis, DoubleBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
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; } } } }
MapReduceValuesToDoubleTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceValuesToDoubleTask<K,V> nextRight, ToDoubleFunction<? super V> transformer, double basis, DoubleBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
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; } } } }
MapReduceEntriesToDoubleTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceEntriesToDoubleTask<K,V> nextRight, ToDoubleFunction<Map.Entry<K,V>> transformer, double basis, DoubleBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
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; } } } }
MapReduceMappingsToDoubleTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceMappingsToDoubleTask<K,V> nextRight, ToDoubleBiFunction<? super K, ? super V> transformer, double basis, DoubleBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
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; } } } }
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"); }
private static BinaryOperator<Vec3D> getBinaryOperator(DoubleBinaryOperator op) { return (a, b) -> { double x = op.applyAsDouble(a.x, b.x); double y = op.applyAsDouble(a.y, b.y); double z = op.applyAsDouble(a.z, b.z); return new Vec3D(x, y, z); }; }
@DataProvider public static Object[][] doubleSet(){ return genericData(size -> IntStream.range(0, size).mapToDouble(i -> (double)i).toArray(), new DoubleBinaryOperator[]{ Double::sum, Double::min}); }
@Test(dataProvider="doubleSet") public void testParallelPrefixForDouble(double[] data, int fromIndex, int toIndex, DoubleBinaryOperator op) { double[] sequentialResult = data.clone(); for (int index = fromIndex + 1; index < toIndex; index++) { sequentialResult[index ] = op.applyAsDouble(sequentialResult[index - 1], sequentialResult[index]); } double[] parallelResult = data.clone(); Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op); assertEquals(parallelResult, sequentialResult); double[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex); Arrays.parallelPrefix(parallelRangeResult, op); assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex)); }
private static double meetBounds(double a, double b, DoubleBinaryOperator op) { if (Double.isNaN(a)) { return b; } else if (Double.isNaN(b)) { return a; } else { return op.applyAsDouble(a, b); } }
SerializationProxy(double value, DoubleBinaryOperator function, long identity) { this.value = value; this.function = function; this.identity = identity; }