Java - Computation of Derivations with Apache Commons Mathematic Library

≡放荡痞女 提交于 2019-11-30 10:33:53
Eric Jablow

In the paragraph below that example, the author describes ways to create DerivativeStructures. It isn't magic. In the example you quoted, someone was supposed to write the function f. Well, that wasn't very clear.

There are several ways a user can create an implementation of the UnivariateDifferentiableFunction interface. The first method is to simply write it directly using the appropriate methods from DerivativeStructure to compute addition, subtraction, sine, cosine... This is often quite straigthforward and there is no need to remember the rules for differentiation: the user code only represent the function itself, the differentials will be computed automatically under the hood. The second method is to write a classical UnivariateFunction and to pass it to an existing implementation of the UnivariateFunctionDifferentiator interface to retrieve a differentiated version of the same function. The first method is more suited to small functions for which user already control all the underlying code. The second method is more suited to either large functions that would be cumbersome to write using the DerivativeStructure API, or functions for which user does not have control to the full underlying code (for example functions that call external libraries).

Use the first idea.

// Function of 1 variable, keep track of 3 derivatives with respect to that variable,
// use 2.5 as the current value.  Basically, the identity function.
DerivativeStructure x = new DerivativeStructure(1, 3, 0, 2.5);
// Basically, x --> x^2.
DerivativeStructure x2 = x.pow(2);
//Linear combination: y = 4x^2 + 2x
DerivativeStructure y = new DerivativeStructure(4.0, x2, 2.0, x);
System.out.println("y    = " + y.getValue());
System.out.println("y'   = " + y.getPartialDerivative(1));
System.out.println("y''  = " + y.getPartialDerivative(2));
System.out.println("y''' = " + y.getPartialDerivative(3));

The following thread from the Apache mailing list seems to illustrate the two possible ways of how the derivative of a UnivariateDifferentiableFunction can be defined. I am adding a new answer as I'm unable to comment on the previous one (insufficient reputation).

The used sample specification of the function is f(x) = x^2.

(1) Using a DerivativeStructure:

public DerivativeStructure value(DerivativeStructure t) {
     return t.multiply(t);
}

(2) By writing a classical UnivariateFunction:

public UnivariateRealFunction derivative() {
    return new UnivariateRealFunction() {
          public double value(double x) {
                // example derivative
                return 2.*x;
          }
     }
}

If I understand well, the advantage of the first case is that the derivative does not need to be obtained manually, as in the second case. In case the derivative is known, there should thus be no advantage of defining a DerivativeStructure, right? The application I have in mind is that of a Newton-Raphson solver, for which generally the function value and its derivative need to be known.

The full example is provided on the aforementioned web site (authors are Thomas Neidhart and Franz Simons). Any further comments are most welcome!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!