List of usage examples for java.util.function DoubleBinaryOperator applyAsDouble
double applyAsDouble(double left, double right);
From source file:Main.java
public static void main(String[] args) { DoubleBinaryOperator d = (x, y) -> x * y; System.out.println(d.applyAsDouble(0.23, 0.45)); }
From source file:at.gridtec.lambda4j.operator.binary.DoubleBinaryOperator2.java
/** * Calls the given {@link DoubleBinaryOperator} with the given arguments and returns its result. * * @param operator The operator to be called * @param value1 The first argument to the operator * @param value2 The second argument to the operator * @return The result from the given {@code DoubleBinaryOperator2}. * @throws NullPointerException If given argument is {@code null} *//*from w w w . j av a 2s .co m*/ static double call(@Nonnull final DoubleBinaryOperator operator, double value1, double value2) { Objects.requireNonNull(operator); return operator.applyAsDouble(value1, value2); }
From source file:com.opengamma.strata.math.impl.regression.NamedVariableLeastSquaresRegressionResultTest.java
@Test public void test() { final int n = 100; final double beta0 = 0.3; final double beta1 = 2.5; final double beta2 = -0.3; final DoubleBinaryOperator f1 = (x1, x2) -> beta1 * x1 + beta2 * x2; final DoubleBinaryOperator f2 = (x1, x2) -> beta0 + beta1 * x1 + beta2 * x2; final double[][] x = new double[n][2]; final double[] y1 = new double[n]; final double[] y2 = new double[n]; for (int i = 0; i < n; i++) { x[i][0] = RANDOM.nextDouble();/*from w ww . ja v a2 s . c o m*/ x[i][1] = RANDOM.nextDouble(); y1[i] = f1.applyAsDouble(x[i][0], x[i][1]); y2[i] = f2.applyAsDouble(x[i][0], x[i][1]); } final LeastSquaresRegression ols = new OrdinaryLeastSquaresRegression(); final List<String> names = Arrays.asList("1", "2"); final NamedVariableLeastSquaresRegressionResult result1 = new NamedVariableLeastSquaresRegressionResult( names, ols.regress(x, null, y1, false)); final NamedVariableLeastSquaresRegressionResult result2 = new NamedVariableLeastSquaresRegressionResult( names, ols.regress(x, null, y2, true)); try { result1.getPredictedValue((Map<String, Double>) null); Assert.fail(); } catch (final IllegalArgumentException e) { // Expected } assertEquals(result1.getPredictedValue(Collections.<String, Double>emptyMap()), 0., 1e-16); try { final Map<String, Double> map = new HashMap<>(); map.put("1", 0.); result1.getPredictedValue(map); Assert.fail(); } catch (final IllegalArgumentException e) { // Expected } double x1, x2, x3; final Map<String, Double> var = new HashMap<>(); for (int i = 0; i < 10; i++) { x1 = RANDOM.nextDouble(); x2 = RANDOM.nextDouble(); x3 = RANDOM.nextDouble(); var.put("1", x1); var.put("2", x2); assertEquals(result1.getPredictedValue(var), f1.applyAsDouble(x1, x2), EPS); assertEquals(result2.getPredictedValue(var), f2.applyAsDouble(x1, x2), EPS); var.put("3", x3); assertEquals(result1.getPredictedValue(var), f1.applyAsDouble(x1, x2), EPS); assertEquals(result2.getPredictedValue(var), f2.applyAsDouble(x1, x2), EPS); } }
From source file:at.gridtec.lambda4j.operator.binary.ThrowableDoubleBinaryOperator.java
/** * Returns a composed {@link DoubleBinaryOperator2} that first applies this operator to its input, and then applies * the {@code recover} operation if a {@link Throwable} is thrown from this one. The {@code recover} operation is * represented by a curried operation which is called with throwable information and same arguments of this * operator./*from w w w .j a v a 2s . c o m*/ * * @param recover The operation to apply if this operator throws a {@code Throwable} * @return A composed {@link DoubleBinaryOperator2} that first applies this operator to its input, and then applies * the {@code recover} operation if a {@code Throwable} is thrown from this one. * @throws NullPointerException If given argument or the returned enclosing operator is {@code null} * @implSpec The implementation checks that the returned enclosing operator from {@code recover} operation is not * {@code null}. If it is, then a {@link NullPointerException} with appropriate message is thrown. * @implNote If thrown {@code Throwable} is of type {@link Error}, it is thrown as-is and thus not passed to {@code * recover} operation. */ @Nonnull default DoubleBinaryOperator2 recover( @Nonnull final Function<? super Throwable, ? extends DoubleBinaryOperator> recover) { Objects.requireNonNull(recover); return (value1, value2) -> { try { return this.applyAsDoubleThrows(value1, value2); } catch (Error e) { throw e; } catch (Throwable throwable) { final DoubleBinaryOperator operator = recover.apply(throwable); Objects.requireNonNull(operator, () -> "recover returned null for " + throwable.getClass() + ": " + throwable.getMessage()); return operator.applyAsDouble(value1, value2); } }; }
From source file:com.simiacryptus.mindseye.lang.Tensor.java
/** * Reduce parallel tensor.//from w w w.j ava2 s . c o m * * @param right the right * @param f the f * @return the tensor */ @Nullable public Tensor reduceParallel(@Nonnull final Tensor right, @Nonnull final DoubleBinaryOperator f) { if (!Arrays.equals(right.getDimensions(), getDimensions())) { throw new IllegalArgumentException( Arrays.toString(right.getDimensions()) + " != " + Arrays.toString(getDimensions())); } @Nullable final double[] dataL = getData(); @Nullable final double[] dataR = right.getData(); return new Tensor(Tensor.getDoubles( IntStream.range(0, length()).mapToDouble(i -> f.applyAsDouble(dataL[i], dataR[i])), length()), dimensions); }
From source file:gedi.util.ArrayUtils.java
public static double[] componentWise(double[] a, double[] b, DoubleBinaryOperator op) { double[] re = new double[a.length]; for (int i = 0; i < re.length; i++) re[i] = op.applyAsDouble(a[i], b[i]); return re;/*from www. j a v a2 s . c o m*/ }
From source file:org.briljantframework.array.AbstractDoubleArray.java
@Override public void combineAssign(DoubleArray array, DoubleBinaryOperator combine) { array = ShapeUtils.broadcastIfSensible(this, array); Check.size(this, array); for (int i = 0; i < size(); i++) { set(i, combine.applyAsDouble(get(i), array.get(i))); }//from ww w .ja v a 2s . com }
From source file:org.briljantframework.array.AbstractDoubleArray.java
@Override public double reduce(double identity, DoubleBinaryOperator reduce) { for (int i = 0; i < size(); i++) { identity = reduce.applyAsDouble(identity, get(i)); }// w w w . ja va2 s.c o m return identity; }
From source file:org.nmrfx.processor.gui.spectra.DrawSpectrum.java
private static int drawVectoreCore(Vec vec, int dataOffset, boolean drawReal, double ph0, double ph1, double[][] xyValues, Path bcPath, DoubleBinaryOperator xFunction, DoubleBinaryOperator yFunction, boolean offsetVec, int start, int end, int size, double dValue, double dDelta, double delta) { if ((start - dataOffset) < 0) { start = dataOffset;//from w ww.j a v a 2 s .co m } if (end > ((size + dataOffset) - 1)) { end = (size + dataOffset) - 1; } int incr = (end - start) / 2048; if (incr < 1) { incr = 1; } if ((start - dataOffset) < 0) { start = dataOffset; } if (end > ((size + dataOffset) - 1)) { end = (size + dataOffset) - 1; } //System.out.println((start - dataOffset) + " " + (end - dataOffset) + " " + dValue + " " + (dValue + delta * (end - start))); if (((Math.abs(ph0) > 1.0e-6) || (Math.abs(ph1) > 1.0e-6)) && !vec.isComplex()) { vec.hft(); } double dValueHold = dValue; int nPoints = 0; if (incr != 1) { double[] ve = new double[end - start + 1]; for (int i = start; i <= end; i++) { double p = ph0 + i * dDelta; if (vec.isComplex()) { Complex cmpPhas = new Complex(Math.cos(p * degtorad), -Math.sin(p * degtorad)); Complex phasedValue = vec.getComplex(i - dataOffset).multiply(cmpPhas); if (drawReal) { ve[i - start] = phasedValue.getReal(); } else { ve[i - start] = phasedValue.getImaginary(); } } else { ve[i - start] = vec.getReal(i - dataOffset); } } nPoints = speedSpectrum(ve, start, start, end, dValue, delta, incr, xyValues, xFunction, yFunction); } else { nPoints = end - start + 1; if ((xyValues[0] == null) || (xyValues[0].length < nPoints)) { xyValues[0] = new double[nPoints]; xyValues[1] = new double[nPoints]; } int iLine = 0; for (int i = start; i <= end; i++) { double intensity; if (vec.isComplex()) { double p = ph0 + i * dDelta; Complex cmpPhas = new Complex(Math.cos(p * degtorad), -Math.sin(p * degtorad)); Complex phasedValue = vec.getComplex(i - dataOffset).multiply(cmpPhas); if (drawReal) { intensity = phasedValue.getReal(); } else { intensity = phasedValue.getImaginary(); } } else { intensity = vec.getReal(i - dataOffset); } xyValues[0][iLine] = xFunction.applyAsDouble(dValue, intensity); xyValues[1][iLine++] = yFunction.applyAsDouble(dValue, intensity); dValue += delta; } } if (bcPath != null) { boolean[] signalPoints = vec.getSignalRegion(); dValue = dValueHold; if (signalPoints != null) { boolean inBase = !signalPoints[start]; int last = 0; for (int i = start; i < end; i++) { double intensity; if (vec.isComplex()) { double p = ph0 + i * dDelta; Complex cmpPhas = new Complex(Math.cos(p * degtorad), -Math.sin(p * degtorad)); Complex phasedValue = vec.getComplex(i - dataOffset).multiply(cmpPhas); if (drawReal) { intensity = phasedValue.getReal(); } else { intensity = phasedValue.getImaginary(); } } else { intensity = vec.getReal(i - dataOffset); } double xValue = xFunction.applyAsDouble(dValue, intensity); double yValue = yFunction.applyAsDouble(dValue, intensity); if (i == start) { if (inBase) { bcPath.getElements().add(new MoveTo(xValue, yValue)); } } if (i == (end - 1)) { if (inBase) { bcPath.getElements().add(new LineTo(xValue, yValue)); } } if (!signalPoints[i] != inBase) { if (inBase) { bcPath.getElements().add(new LineTo(xValue, yValue)); last = i; } else { bcPath.getElements().add(new MoveTo(xValue, yValue)); } } if ((i - last) > 10) { if (inBase) { bcPath.getElements().add(new LineTo(xValue, yValue)); last = i; } } inBase = !signalPoints[i]; dValue += delta; } } } return nPoints; }
From source file:org.nmrfx.processor.gui.spectra.DrawSpectrum.java
static int speedSpectrum(double[] ve, int vStart, int start, int end, double dValue, double delta, int nIncr, double[][] xy, DoubleBinaryOperator xFunction, DoubleBinaryOperator yFunction) { double minValue = Double.MAX_VALUE; double maxValue = Double.NEGATIVE_INFINITY; double pxmin; double pxmax; double iMin = 0; double iMax = 0; int k = 0;/*from w ww .ja v a 2 s. c o m*/ int n = ((end - start + 1) / nIncr) * 2 + 8; // fixme approximate if ((xy[0] == null) || (xy[0].length < n)) { xy[0] = new double[n]; xy[1] = new double[n]; } xy[0][0] = xFunction.applyAsDouble(dValue, ve[start - vStart]); xy[1][0] = yFunction.applyAsDouble(dValue, ve[start - vStart]); dValue += delta; int iLine = 0; for (int i = (start + 1); i < end; i++) { double py1 = ve[i - vStart]; if (py1 < minValue) { minValue = py1; iMin = dValue; } if (py1 > maxValue) { maxValue = py1; iMax = dValue; } pxmin = iMin; pxmax = iMax; k++; dValue += delta; if (k < nIncr) { continue; } k = 0; if (pxmin > pxmax) { xy[0][iLine] = (xFunction.applyAsDouble(pxmin, minValue)); xy[1][iLine++] = (yFunction.applyAsDouble(pxmin, minValue)); xy[0][iLine] = (xFunction.applyAsDouble(pxmax, maxValue)); xy[1][iLine++] = (yFunction.applyAsDouble(pxmax, maxValue)); } else { xy[0][iLine] = (xFunction.applyAsDouble(pxmax, maxValue)); xy[1][iLine++] = (yFunction.applyAsDouble(pxmax, maxValue)); xy[0][iLine] = (xFunction.applyAsDouble(pxmin, minValue)); xy[1][iLine++] = (yFunction.applyAsDouble(pxmin, minValue)); } minValue = Double.MAX_VALUE; maxValue = Double.NEGATIVE_INFINITY; } xy[0][iLine] = (xFunction.applyAsDouble(dValue, ve[end - vStart])); xy[1][iLine++] = (yFunction.applyAsDouble(dValue, ve[end - vStart])); return iLine; }