@Override public final DoubleStream map(DoubleUnaryOperator mapper) { Objects.requireNonNull(mapper); return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Double> opWrapSink(int flags, Sink<Double> sink) { return new Sink.ChainedDouble<Double>(sink) { @Override public void accept(double t) { downstream.accept(mapper.applyAsDouble(t)); } }; } }; }
/** * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code DoubleStream} * will be the provided {@code seed}. For {@code n > 0}, the element at * position {@code n}, will be the result of applying the function {@code f} * to the element at position {@code n - 1}. * * @param seed the initial element * @param f a function to be applied to to the previous element to produce * a new element * @return a new sequential {@code DoubleStream} */ public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) { Objects.requireNonNull(f); final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() { double t = seed; @Override public boolean hasNext() { return true; } @Override public double nextDouble() { double v = t; t = f.applyAsDouble(t); return v; } }; return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize( iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); }
/** * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code DoubleStream} * will be the provided {@code seed}. For {@code n > 0}, the element at * position {@code n}, will be the result of applying the function {@code f} * to the element at position {@code n - 1}. * * <p>The action of applying {@code f} for one element * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a> * the action of applying {@code f} for subsequent elements. For any given * element the action may be performed in whatever thread the library * chooses. * * @param seed the initial element * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code DoubleStream} */ public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) { Objects.requireNonNull(f); Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { double prev; boolean started; @Override public boolean tryAdvance(DoubleConsumer action) { Objects.requireNonNull(action); double t; if (started) t = f.applyAsDouble(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.doubleStream(spliterator, false); }
/** * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code DoubleStream} * will be the provided {@code seed}. For {@code n > 0}, the element at * position {@code n}, will be the result of applying the function {@code f} * to the element at position {@code n - 1}. * * @param seed the initial element * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code DoubleStream} */ public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) { Objects.requireNonNull(f); Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { double prev; boolean started; @Override public boolean tryAdvance(DoubleConsumer action) { Objects.requireNonNull(action); double t; if (started) t = f.applyAsDouble(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.doubleStream(spliterator, false); }
static double integrate(DoubleUnaryOperator f, double a, double b) { double dx = (b - a); double sum0; double sum1 = f.applyAsDouble(a) * (b - a); int it = 1; do { dx /= 2.; it *= 2; sum0 = sum1; sum1 = .0; for (double x = a, eps = dx / 2.; x + eps < b; x += dx) { sum1 += dx * f.applyAsDouble(x); } } while (Math.abs(sum0 - sum1) > 1e-7 && it < 100000000); return sum1; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Double, Double> cache = null; final DoubleUnaryOperator operator = input -> input; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>(cache, keyFunction, operator); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullOperator() { // given final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = null; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL DoubleUnaryOperator - provide an actual DoubleUnaryOperator to fix this."); // then new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>(cache, keyFunction, operator); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = input -> input; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsDouble(123D); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123D, memoizer.viewCacheForTest().keySet().iterator().next().doubleValue(), 0.0D); Assert.assertEquals("Memoization value does not match expectations", 123D, memoizer.viewCacheForTest().values().iterator().next().doubleValue(), 0.0D); }
/** * */ @Test public void shouldUseCallWrappedOperator() { // given final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = Mockito.mock(DoubleUnaryOperator.class); final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsDouble(123D); Mockito.verify(operator).applyAsDouble(123D); }
/** * Construct an instance of the NewtonRaphson method for masochists who * do not want to use {@link #builder()}. * @param func the function * @param derivative the derivative of the function * @param tolerance the tolerance * @param iterations maximum number of iterations */ public NewtonRaphson( DoubleUnaryOperator func, DoubleUnaryOperator derivative, double tolerance, long iterations) { this.func = func; this.derivative = derivative; this.tolerance = tolerance; this.iterations = iterations; }
@NonNull @Override public MuVector2d map(@NonNull final DoubleUnaryOperator operator) { this.x = operator.applyAsDouble(this.x); this.y = operator.applyAsDouble(this.y); return this; }
@NonNull @Override public MuVector3d map(@NonNull final DoubleUnaryOperator operator) { this.x = operator.applyAsDouble(this.x); this.y = operator.applyAsDouble(this.y); this.z = operator.applyAsDouble(this.z); return this; }
@NonNull @Override public MuVector4d map(@NonNull final DoubleUnaryOperator operator) { this.x = operator.applyAsDouble(this.x); this.y = operator.applyAsDouble(this.y); this.z = operator.applyAsDouble(this.z); this.w = operator.applyAsDouble(this.w); return this; }
@NonNull @Override public MuVector4f map(@NonNull final DoubleUnaryOperator operator) { this.x = (float) operator.applyAsDouble(this.x); this.y = (float) operator.applyAsDouble(this.y); this.z = (float) operator.applyAsDouble(this.z); this.w = (float) operator.applyAsDouble(this.w); return this; }
@NonNull @Override public MuVector3f map(@NonNull final DoubleUnaryOperator operator) { this.x = (float) operator.applyAsDouble(this.x); this.y = (float) operator.applyAsDouble(this.y); this.z = (float) operator.applyAsDouble(this.z); return this; }
@NonNull @Override public MuVector2f map(@NonNull final DoubleUnaryOperator operator) { this.x = (float) operator.applyAsDouble(this.x); this.y = (float) operator.applyAsDouble(this.y); return this; }
@Override public void teleport( Location l, boolean onGround ) { Location previous = this.location; if( l.getY() < 0 ) { damage( 2D ); return; } if( previous.getY() > l.getY() ) { double tempFallDistance = Math.abs( l.getY() - previous.getY() ); BlockState state = getWorld().getBlockAt( new Vector3i( l.getBlockX(), ((int) l.getY()) - 1, l.getBlockZ() ) ); if( state.getTypeId() != 0 || getWorld().getBlockAt( new Vector3i( l.getBlockX(), l.getBlockY(), l.getBlockZ() ) ).getTypeId() != 0 ) { fallDistance.updateAndGet( new DoubleUnaryOperator() { @Override public double applyAsDouble( double operand ) { double finalFallDistance = operand + tempFallDistance; if( finalFallDistance > 3 ) { damage( finalFallDistance * 0.8 ); } return 0; } } ); } else { fallDistance.addAndGet( tempFallDistance ); } } else { fallDistance.set( 0D ); } super.teleport( l, onGround ); }
public final double updateAndGet( DoubleUnaryOperator updateFunction ) { double prev, next; do { prev = get(); next = updateFunction.applyAsDouble( prev ); } while ( !compareAndSet( prev, next ) ); return next; }
public final double getAndUpdate( DoubleUnaryOperator updateFunction ) { double prev, next; do { prev = get(); next = updateFunction.applyAsDouble( prev ); } while ( !compareAndSet( prev, next ) ); return prev; }
public static Name makeOperator(String name, DoubleUnaryOperator duo) { return new Name(name, toTableConsumer(stack -> { stack.replaceAll(c -> { return new Column.OfDoubles(inputs -> inputs[0].toString() + " " + name, inputs -> range -> ((Column.OfDoubles)inputs[0]).rows().map(duo), c); }); }),"[:]", "Unary double operator " + name); }
public static double[] applyToArray(final double[] array, final DoubleUnaryOperator func) { Utils.nonNull(func, "function may not be null"); Utils.nonNull(array, "array may not be null"); final double[] result = new double[array.length]; for (int m = 0; m < result.length; m++) { result[m] = func.applyAsDouble(array[m]); } return result; }
public static double[] applyToArrayInPlace(final double[] array, final DoubleUnaryOperator func) { Utils.nonNull(array, "array may not be null"); Utils.nonNull(func, "function may not be null"); for (int m = 0; m < array.length; m++) { array[m] = func.applyAsDouble(array[m]); } return array; }
public static double[] applyToArray(final double[] array, final DoubleUnaryOperator func) { JointCallingUtils.nonNull(func, "function may not be null"); JointCallingUtils.nonNull(array, "array may not be null"); final double[] result = new double[array.length]; for (int m = 0; m < result.length; m++) { result[m] = func.applyAsDouble(array[m]); } return result; }
public static double[] applyToArrayInPlace(final double[] array, final DoubleUnaryOperator func) { JointCallingUtils.nonNull(array, "array may not be null"); JointCallingUtils.nonNull(func, "function may not be null"); for (int m = 0; m < array.length; m++) { array[m] = func.applyAsDouble(array[m]); } return array; }
public InterpolatingIntDoubleSampler(DoubleUnaryOperator icdSource, int resolution, boolean hash) { this.f = icdSource; this.resolution = resolution; if (hash) { this.hash = new ThreadSafeHash(); } this.lut = precompute(); }
public InterpolatingLongDoubleSampler(DoubleUnaryOperator icdSource, int resolution, boolean hash) { this.f = icdSource; this.resolution = resolution; if (hash) { this.hash = new ThreadSafeHash(); } this.lut = precompute(); }
/** * Takes a set of rules and a conflict-free set of blocks * * @param assignmentGraph * assignmentGraph to consider * @param blockSet * blocks to consider * @return a list of PathRule that the randomizer is content with */ public static Collection<PathRule> randomizeSA(final AssignmentGraph assignmentGraph, final ECBlockSet blockSet) { final Randomizer randomizer = newRandomizer(assignmentGraph, blockSet); final DoubleUnaryOperator cooldown = t -> 0.95 * t; /* Gator uses: number of edges in the condition graph */ /* we just use the number of nodes here !? */ final long innerLoopOpimizations = 0; // TBD number of fact variables final double initialTemp = 2 * randomizer.getBestState().rate(); return resetPaths(new SimulatedAnnealing(randomizer, cooldown, innerLoopOpimizations, initialTemp).optimize()); }
/** * Takes a set of rules and a conflict-free set of blocks * * @param assignmentGraph * assignmentGraph to consider * @param blockSet * blocks to consider * @return a list of PathRule that the randomizer is content with */ public static Collection<PathRule> randomizeTPO(final AssignmentGraph assignmentGraph, final ECBlockSet blockSet) { final Randomizer randomizer = newRandomizer(assignmentGraph, blockSet); // TBD number of fact variables final long iiLocalOptimizations = 0; final long iiRLocalMinimum = 20; final DoubleUnaryOperator saCooldown = t -> 0.95 * t; /* Gator uses: number of edges in the condition graph */ /* we just use the number of nodes here !? */ // TBD number of fact variables final long saInnerLoopOpimizations = 0; return resetPaths(new TwoPhaseOptimization(randomizer, iiLocalOptimizations, iiRLocalMinimum, saCooldown, saInnerLoopOpimizations).optimize()); }
@BeforeClass public static void beforeClass() { mainClass = TestUtils.getUserClass("Main"); integrate = TestUtils.getMethod(mainClass, "integrate", new int[]{Modifier.PUBLIC | Modifier.STATIC}, Double.TYPE, DoubleUnaryOperator.class, Double.TYPE, Double.TYPE); }
public DivHKVFactorizer(double lambdaP, double lambdaQ, double lambdaD, ItemDistanceModel<I> distanceModel, DoubleUnaryOperator confidence, int numIter, boolean useSimilarity) { super(numIter); this.lambdaP = lambdaP; this.lambdaQ = lambdaQ; this.lambdaD = lambdaD; this.distanceModel = distanceModel; this.confidence = confidence; this.useSimilarity = useSimilarity; }