FunctionUtils { @Deprecated public static MultivariateDifferentiableFunction toMultivariateDifferentiableFunction(final DifferentiableMultivariateFunction f) { return new MultivariateDifferentiableFunction() { public double value(final double[] x) { return f.value(x); } public DerivativeStructure value(final DerivativeStructure[] t) throws DimensionMismatchException, NumberIsTooLargeException { final int parameters = t[0].getFreeParameters(); final int order = t[0].getOrder(); final int n = t.length; if (order > 1) { throw new NumberIsTooLargeException(order, 1, true); } for (int i = 0; i < n; ++i) { if (t[i].getFreeParameters() != parameters) { throw new DimensionMismatchException(t[i].getFreeParameters(), parameters); } if (t[i].getOrder() != order) { throw new DimensionMismatchException(t[i].getOrder(), order); } } final double[] point = new double[n]; for (int i = 0; i < n; ++i) { point[i] = t[i].getValue(); } final double value = f.value(point); final double[] gradient = f.gradient().value(point); final double[] derivatives = new double[parameters + 1]; derivatives[0] = value; final int[] orders = new int[parameters]; for (int i = 0; i < parameters; ++i) { orders[i] = 1; for (int j = 0; j < n; ++j) { derivatives[i + 1] += gradient[j] * t[j].getPartialDerivative(orders); } orders[i] = 0; } return new DerivativeStructure(parameters, order, derivatives); } }; } 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 @Deprecated public void testToMultivariateDifferentiableFunction() { DifferentiableMultivariateFunction hypot = new DifferentiableMultivariateFunction() { public double value(double[] point) { return FastMath.hypot(point[0], point[1]); } public MultivariateFunction partialDerivative(final int k) { return new MultivariateFunction() { public double value(double[] point) { return point[k] / FastMath.hypot(point[0], point[1]); } }; } public MultivariateVectorFunction gradient() { return new MultivariateVectorFunction() { public double[] value(double[] point) { final double h = FastMath.hypot(point[0], point[1]); return new double[] { point[0] / h, point[1] / h }; } }; } }; MultivariateDifferentiableFunction converted = FunctionUtils.toMultivariateDifferentiableFunction(hypot); for (double x = 0.1; x < 0.5; x += 0.01) { for (double y = 0.1; y < 0.5; y += 0.01) { DerivativeStructure[] t = new DerivativeStructure[] { new DerivativeStructure(3, 1, x, 1.0, 2.0, 3.0 ), new DerivativeStructure(3, 1, y, 4.0, 5.0, 6.0 ) }; DerivativeStructure h = DerivativeStructure.hypot(t[0], t[1]); Assert.assertEquals(h.getValue(), converted.value(t).getValue(), 1.0e-10); Assert.assertEquals(h.getPartialDerivative(1, 0, 0), converted.value(t).getPartialDerivative(1, 0, 0), 1.0e-10); Assert.assertEquals(h.getPartialDerivative(0, 1, 0), converted.value(t).getPartialDerivative(0, 1, 0), 1.0e-10); Assert.assertEquals(h.getPartialDerivative(0, 0, 1), converted.value(t).getPartialDerivative(0, 0, 1), 1.0e-10); } } }