java.util.function.DoubleFunction Java Examples

The following examples show how to use java.util.function.DoubleFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DoublePipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #2
Source File: DoublePipeline.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
    return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
            return new Sink.ChainedDouble<Double>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example #3
Source File: DoublePipeline.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
    return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
            return new Sink.ChainedDouble<Double>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example #4
Source File: DoublePipeline.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #5
Source File: HistogramPanel.java    From mzmine3 with GNU General Public License v2.0 6 votes vote down vote up
protected JFreeChart doInBackground(double binShift, double binwidth) {
  // create histogram
  double[] dat = data.getData();
  if (cbExcludeSmallerNoise.isSelected()) {
    double noise = data.getRange().getLowerBound();
    // get processed data from original image
    dat = DoubleStream.of(dat).filter(d -> d > noise).toArray();
  }

  Range r = HistogramChartFactory.getBounds(dat);

  DoubleFunction<Double> f =
      cbThirdSQRT.isSelected() ? val -> Math.cbrt(val) : val -> val;

  JFreeChart chart = HistogramChartFactory.createHistogram(dat, xLabel, binwidth,
      r.getLowerBound() - binShift, r.getUpperBound(), f);
  // add gaussian?
  if (cbGaussianFit.isSelected()) {
    addGaussianCurve(chart.getXYPlot());
  }
  return chart;
}
 
Example #6
Source File: DoublePipeline.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
    Objects.requireNonNull(mapper);
    return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
            return new Sink.ChainedDouble<Double>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example #7
Source File: DoublePipeline.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
    return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
            return new Sink.ChainedDouble<Double>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example #8
Source File: DoublePipeline.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #9
Source File: DoublePipeline.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #10
Source File: OrcBatchReader.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <T> void readNonNullDoubleColumn(Object[] vals, int fieldIdx, DoubleColumnVector vector,
												int childCount, DoubleFunction<T> reader) {

	if (vector.isRepeating) { // fill complete column with first value
		T repeatingValue = reader.apply(vector.vector[0]);
		fillColumnWithRepeatingValue(vals, fieldIdx, repeatingValue, childCount);
	} else {
		if (fieldIdx == -1) { // set as an object
			for (int i = 0; i < childCount; i++) {
				vals[i] = reader.apply(vector.vector[i]);
			}
		} else { // set as a field of Row
			Row[] rows = (Row[]) vals;
			for (int i = 0; i < childCount; i++) {
				rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
			}
		}
	}
}
 
Example #11
Source File: DoublePipeline.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
    return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
            return new Sink.ChainedDouble<Double>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example #12
Source File: DoublePipeline.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #13
Source File: DoublePipeline.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #14
Source File: BinarySearch.java    From Algorithms with MIT License 6 votes vote down vote up
public static double binarySearch(
    double lo, double hi, double target, DoubleFunction<Double> function) {

  if (hi <= lo) throw new IllegalArgumentException("hi should be greater than lo");

  double mid;
  do {

    // Find the middle point
    mid = (hi + lo) / 2.0;

    // Compute the value of our function for the middle point
    // Note that f can be any function not just the square root function
    double value = function.apply(mid);

    if (value > target) {
      hi = mid;
    } else {
      lo = mid;
    }

  } while ((hi - lo) > EPS);

  return mid;
}
 
Example #15
Source File: DoublePipeline.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #16
Source File: DoublePipeline.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
    return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
            return new Sink.ChainedDouble<Double>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example #17
Source File: QuaternionRotationTest.java    From commons-geometry with Apache License 2.0 6 votes vote down vote up
@Test
public void testSlerp_tOutsideOfZeroToOne_apply() {
    // arrange
    Vector3D vec = Vector3D.Unit.PLUS_X;

    QuaternionRotation q1 = QuaternionRotation.fromAxisAngle(Vector3D.Unit.PLUS_Z, 0.25 * PlaneAngleRadians.PI);
    QuaternionRotation q2 = QuaternionRotation.fromAxisAngle(Vector3D.Unit.PLUS_Z, 0.75 * PlaneAngleRadians.PI);

    // act/assert
    DoubleFunction<QuaternionRotation> slerp12 = q1.slerp(q2);
    EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_X, slerp12.apply(-4.5).apply(vec), EPS);
    EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_X, slerp12.apply(-0.5).apply(vec), EPS);
    EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_X, slerp12.apply(1.5).apply(vec), EPS);
    EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_X, slerp12.apply(5.5).apply(vec), EPS);

    DoubleFunction<QuaternionRotation> slerp21 = q2.slerp(q1);
    EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_X, slerp21.apply(-4.5).apply(vec), EPS);
    EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_X, slerp21.apply(-0.5).apply(vec), EPS);
    EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_X, slerp21.apply(1.5).apply(vec), EPS);
    EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_X, slerp21.apply(5.5).apply(vec), EPS);
}
 
Example #18
Source File: DoublePipeline.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE,
                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #19
Source File: DoublePipeline.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
@Override
public final DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper) {
    return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
                                    StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
            return new Sink.ChainedDouble<Double>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(double t) {
                    try (DoubleStream result = mapper.apply(t)) {
                        // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
                        if (result != null)
                            result.sequential().forEach(i -> downstream.accept(i));
                    }
                }
            };
        }
    };
}
 
Example #20
Source File: OrcBatchReader.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static <T> void readNonNullDoubleColumn(Object[] vals, int fieldIdx, DoubleColumnVector vector,
												int childCount, DoubleFunction<T> reader) {

	if (vector.isRepeating) { // fill complete column with first value
		T repeatingValue = reader.apply(vector.vector[0]);
		fillColumnWithRepeatingValue(vals, fieldIdx, repeatingValue, childCount);
	} else {
		if (fieldIdx == -1) { // set as an object
			for (int i = 0; i < childCount; i++) {
				vals[i] = reader.apply(vector.vector[i]);
			}
		} else { // set as a field of Row
			Row[] rows = (Row[]) vals;
			for (int i = 0; i < childCount; i++) {
				rows[i].setField(fieldIdx, reader.apply(vector.vector[i]));
			}
		}
	}
}
 
Example #21
Source File: QuaternionRotationTest.java    From commons-geometry with Apache License 2.0 6 votes vote down vote up
@Test
public void testSlerp_simple() {
    // arrange
    QuaternionRotation q0 = QuaternionRotation.fromAxisAngle(Vector3D.Unit.PLUS_Z, 0.0);
    QuaternionRotation q1 = QuaternionRotation.fromAxisAngle(Vector3D.Unit.PLUS_Z, PlaneAngleRadians.PI);
    DoubleFunction<QuaternionRotation> fn = q0.slerp(q1);
    Vector3D v = Vector3D.of(2, 0, 1);

    double sqrt2 = Math.sqrt(2);

    // act
    checkVector(fn.apply(0).apply(v), 2, 0, 1);
    checkVector(fn.apply(0.25).apply(v), sqrt2, sqrt2, 1);
    checkVector(fn.apply(0.5).apply(v), 0, 2, 1);
    checkVector(fn.apply(0.75).apply(v), -sqrt2, sqrt2, 1);
    checkVector(fn.apply(1).apply(v), -2, 0, 1);
}
 
Example #22
Source File: DoublePipeline.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper, int opFlags) {
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE, opFlags) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #23
Source File: Matrix.java    From MachineLearning with MIT License 5 votes vote down vote up
/**
 * Applies the given function on every element of the matrix.
 * B[i, j] = f(A[i, j])
 * @param function Function that gets applied on every element
 * @return Resulting matrix
 */
public Matrix apply(DoubleFunction<Double> function) {
  final Matrix result = new Matrix(getHeight(), getWidth());
  
  for(int j=0; j<result.getHeight(); j++) {
    for(int i=0; i<result.getWidth(); i++) {
      result.set(j, i, function.apply(get(j, i)));
    }
  }
  
  return result;
}
 
Example #24
Source File: DoublePipeline.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper, int opFlags) {
    return new ReferencePipeline.StatelessOp<Double, U>(this, StreamShape.DOUBLE_VALUE, opFlags) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedDouble<U>(sink) {
                @Override
                public void accept(double t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #25
Source File: QuaternionRotationTest.java    From commons-geometry with Apache License 2.0 5 votes vote down vote up
private void checkSlerpCombination(QuaternionRotation start, QuaternionRotation end) {
    DoubleFunction<QuaternionRotation> slerp = start.slerp(end);
    Vector3D vec = Vector3D.of(1, 1, 1).normalize();

    Vector3D startVec = start.apply(vec);
    Vector3D endVec = end.apply(vec);

    // check start and end values
    EuclideanTestUtils.assertCoordinatesEqual(startVec, slerp.apply(0).apply(vec), EPS);
    EuclideanTestUtils.assertCoordinatesEqual(endVec, slerp.apply(1).apply(vec), EPS);

    // check intermediate values
    double prevAngle = -1;
    final int numSteps = 100;
    final double delta = 1d / numSteps;
    for (int step = 0; step <= numSteps; step++) {
        final double t = step * delta;
        QuaternionRotation result = slerp.apply(t);

        Vector3D slerpVec = result.apply(vec);
        Assert.assertEquals(1, slerpVec.norm(), EPS);

        // make sure that we're steadily progressing to the end angle
        double angle = slerpVec.angle(startVec);
        Assert.assertTrue("Expected slerp angle to continuously increase; previous angle was " +
                          prevAngle + " and new angle is " + angle,
                          Precision.compareTo(angle, prevAngle, EPS) >= 0);

        prevAngle = angle;
    }
}
 
Example #26
Source File: Rotation2DTest.java    From commons-geometry with Apache License 2.0 5 votes vote down vote up
/** Check a rotation transform for consistency against a variety of points and rotation angles.
 * @param factory function used to create a rotation transform from an input angle
 * @param transformFn function that accepts the transform and a point and returns
 *      the transformed point
 */
private static <T extends EuclideanTransform<Vector2D>> void checkRotate(
        DoubleFunction<T> factory, BiFunction<T, Vector2D, Vector2D> transformFn) {

    // check zero
    T transform = factory.apply(0);
    EuclideanTestUtils.assertCoordinatesEqual(Vector2D.ZERO, transformFn.apply(transform, Vector2D.ZERO), TEST_EPS);

    // check a variety of non-zero points
    EuclideanTestUtils.permuteSkipZero(-2, -2, 1, (x, y) -> {
        checkRotatePoint(Vector2D.of(x, y), factory, transformFn);
    });
}
 
Example #27
Source File: Function2.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an LONG function that wraps to function provided
 * @param function  the function to wrap
 * @param <O>       the output type
 * @return          the newly created function wrapper
 */
public static <O> Function2<Double,O> fromDouble(DoubleFunction<O> function) {
    return new Function2<Double,O>(FunctionStyle.DOUBLE) {
        @Override
        public final O apply(double input) {
            return function.apply(input);
        }
    };
}
 
Example #28
Source File: TernarySearch.java    From Algorithms with MIT License 5 votes vote down vote up
public static double ternarySearch(double low, double high, DoubleFunction<Double> function) {
  Double best = null;
  while (true) {
    double mid1 = (2 * low + high) / 3, mid2 = (low + 2 * high) / 3;
    double res1 = function.apply(mid1), res2 = function.apply(mid2);
    if (res1 > res2) low = mid1;
    else high = mid2;
    if (best != null && Math.abs(best - mid1) < EPS) break;
    best = mid1;
  }
  return best;
}
 
Example #29
Source File: TernarySearch.java    From Algorithms with MIT License 5 votes vote down vote up
public static void main(String[] args) {

    // Search for the lowest point on the function x^2 + 3x + 5
    // using a ternary search on the interval [-100, +100]
    DoubleFunction<Double> function = (x) -> (x * x + 3 * x + 5);
    double root = ternarySearch(-100.0, +100.0, function);
    System.out.printf("%.4f\n", root);
  }
 
Example #30
Source File: Rotation2DTest.java    From commons-geometry with Apache License 2.0 5 votes vote down vote up
/** Check a rotation transform for consistency when transforming a single point against a
 * variety of rotation angles.
 * @param pt point to transform
 * @param factory function used to create a rotation transform from an input angle
 * @param transformFn function that accepts the transform and a point and returns
 *      the transformed point
 */
private static <T extends EuclideanTransform<Vector2D>> void checkRotatePoint(
        Vector2D pt, DoubleFunction<T> factory, BiFunction<T, Vector2D, Vector2D> transformFn) {

    // arrange
    double limit = 4 * Math.PI;
    double inc = 0.25;

    Line line = Lines.fromPointAndDirection(Vector2D.ZERO, pt, TEST_PRECISION);

    T transform;
    Vector2D resultPt;
    Line resultLine;
    for (double angle = -limit; angle < limit; angle += inc) {
        transform = factory.apply(angle);

        // act
        resultPt = transformFn.apply(transform, pt);

        // assert
        // check that the norm is unchanged
        Assert.assertEquals(pt.norm(), resultPt.norm(), TEST_EPS);

        resultLine = Lines.fromPointAndDirection(Vector2D.ZERO, resultPt, TEST_PRECISION);
        double lineAngle = line.angle(resultLine);

        // check that the angle is what we expect
        Assert.assertEquals(PlaneAngleRadians.normalizeBetweenMinusPiAndPi(angle), lineAngle, TEST_EPS);
    }
}