FunctionUtils { @Deprecated public static DifferentiableMultivariateFunction toDifferentiableMultivariateFunction(final MultivariateDifferentiableFunction f) { return new DifferentiableMultivariateFunction() { public double value(final double[] x) { return f.value(x); } public MultivariateFunction partialDerivative(final int k) { return new MultivariateFunction() { public double value(final double[] x) { final int n = x.length; final DerivativeStructure[] dsX = new DerivativeStructure[n]; for (int i = 0; i < n; ++i) { if (i == k) { dsX[i] = new DerivativeStructure(1, 1, 0, x[i]); } else { dsX[i] = new DerivativeStructure(1, 1, x[i]); } } final DerivativeStructure y = f.value(dsX); return y.getPartialDerivative(1); } }; } public MultivariateVectorFunction gradient() { return new MultivariateVectorFunction() { public double[] value(final double[] x) { final int n = x.length; final DerivativeStructure[] dsX = new DerivativeStructure[n]; for (int i = 0; i < n; ++i) { dsX[i] = new DerivativeStructure(n, 1, i, x[i]); } final DerivativeStructure y = f.value(dsX); final double[] gradient = new double[n]; final int[] orders = new int[n]; for (int i = 0; i < n; ++i) { orders[i] = 1; gradient[i] = y.getPartialDerivative(orders); orders[i] = 0; } return gradient; } }; } }; } 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 testToDifferentiableMultivariateFunction() { MultivariateDifferentiableFunction hypot = new MultivariateDifferentiableFunction() { public double value(double[] point) { return FastMath.hypot(point[0], point[1]); } public DerivativeStructure value(DerivativeStructure[] point) { return DerivativeStructure.hypot(point[0], point[1]); } }; DifferentiableMultivariateFunction converted = FunctionUtils.toDifferentiableMultivariateFunction(hypot); for (double x = 0.1; x < 0.5; x += 0.01) { for (double y = 0.1; y < 0.5; y += 0.01) { double[] point = new double[] { x, y }; Assert.assertEquals(hypot.value(point), converted.value(point), 1.0e-10); Assert.assertEquals(x / hypot.value(point), converted.gradient().value(point)[0], 1.0e-10); Assert.assertEquals(y / hypot.value(point), converted.gradient().value(point)[1], 1.0e-10); } } }