FunctionUtils { public static MultivariateFunction collector(final BivariateFunction combiner, final UnivariateFunction f, final double initialValue) { return new MultivariateFunction() { public double value(double[] point) { double result = combiner.value(initialValue, f.value(point[0])); for (int i = 1; i < point.length; i++) { result = combiner.value(result, f.value(point[i])); } return result; } }; } private  FunctionUtils(); static UnivariateFunction compose(final UnivariateFunction ... f); static UnivariateDifferentiableFunction compose(final UnivariateDifferentiableFunction ... f); @Deprecated static DifferentiableUnivariateFunction compose(final DifferentiableUnivariateFunction ... f); static UnivariateFunction add(final UnivariateFunction ... f); static UnivariateDifferentiableFunction add(final UnivariateDifferentiableFunction ... f); @Deprecated static DifferentiableUnivariateFunction add(final DifferentiableUnivariateFunction ... f); static UnivariateFunction multiply(final UnivariateFunction ... f); static UnivariateDifferentiableFunction multiply(final UnivariateDifferentiableFunction ... f); @Deprecated static DifferentiableUnivariateFunction multiply(final DifferentiableUnivariateFunction ... f); static UnivariateFunction combine(final BivariateFunction combiner,
                                             final UnivariateFunction f,
                                             final UnivariateFunction g); static MultivariateFunction collector(final BivariateFunction combiner,
                                                 final UnivariateFunction f,
                                                 final double initialValue); static MultivariateFunction collector(final BivariateFunction combiner,
                                                 final double initialValue); static UnivariateFunction fix1stArgument(final BivariateFunction f,
                                                    final double fixed); static UnivariateFunction fix2ndArgument(final BivariateFunction f,
                                                    final double fixed); static double[] sample(UnivariateFunction f, double min, double max, int n); @Deprecated static DifferentiableUnivariateFunction toDifferentiableUnivariateFunction(final UnivariateDifferentiableFunction f); @Deprecated static UnivariateDifferentiableFunction toUnivariateDifferential(final DifferentiableUnivariateFunction f); @Deprecated static DifferentiableMultivariateFunction toDifferentiableMultivariateFunction(final MultivariateDifferentiableFunction f); @Deprecated static MultivariateDifferentiableFunction toMultivariateDifferentiableFunction(final DifferentiableMultivariateFunction f); @Deprecated static DifferentiableMultivariateVectorFunction toDifferentiableMultivariateVectorFunction(final MultivariateDifferentiableVectorFunction f); @Deprecated static MultivariateDifferentiableVectorFunction toMultivariateDifferentiableVectorFunction(final DifferentiableMultivariateVectorFunction f);  }
@Test public void testCollector() { BivariateFunction bi = new Add(); MultivariateFunction coll = FunctionUtils.collector(bi, 0); Assert.assertEquals(10, coll.value(new double[] {1, 2, 3, 4}), EPS); bi = new Multiply(); coll = FunctionUtils.collector(bi, 1); Assert.assertEquals(24, coll.value(new double[] {1, 2, 3, 4}), EPS); bi = new Max(); coll = FunctionUtils.collector(bi, Double.NEGATIVE_INFINITY); Assert.assertEquals(10, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0); bi = new Min(); coll = FunctionUtils.collector(bi, Double.POSITIVE_INFINITY); Assert.assertEquals(-24, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0); }