Java - 使用Apache Commons Mathematic Library计算派生 [英] Java - Computation of Derivations with Apache Commons Mathematic Library

查看:181
本文介绍了Java - 使用Apache Commons Mathematic Library计算派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用apache commons数学库时遇到问题。

我只想创建像f(x)= 4x ^ 2 + 2x这样的函数,我想计算这个函数的导数< br> - > f'(x)= 8x + 2

I have a problem in using the apache commons math library.
I just want to create functions like f(x) = 4x^2 + 2x and I want to compute the derivative of this function
--> f'(x) = 8x + 2

我读过有关差异化的文章( http://commons.apache.org/proper/commons-math/userguide/analysis.html ,第4.7节)。

有一个我不明白的例子:

I read the article about Differentiation (http://commons.apache.org/proper/commons-math/userguide/analysis.html, section 4.7).
There is an example which I don't understand:

int params = 1;
int order = 3;
double xRealValue = 2.5;
DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue);
DerivativeStructure y = f(x);                    //COMPILE ERROR
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);

在第5行中,当然会发生编译错误。函数 f(x)被调用但未定义。我错了什么?

有没有人有任何关于使用apache commons数学库进行区分/派生的经验,或者是否有人知道另一个可以帮助的库/框架我?

In Line 5 a compile error occurs of course. The function f(x) is called and not defined. What I am getting wrong?
Has anyone any experience with the differentiation/derivation with the apache commons math library or does anyone know another library/framework which can help me?

谢谢

推荐答案

在下面的例子中,作者描述了创建 DerivativeStructure 的方法。这不是魔术。在你引用的例子中,有人应该编写函数 f 。嗯,这不是很清楚。

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.


用户有几种方法可以eate实现UnivariateDifferentiableFunction接口。第一种方法是直接使用DerivativeStructure中的适当方法直接编写它来计算加法,减法,正弦,余弦......这通常非常简单,并且不需要记住区分规则:用户代码仅代表功能本身,差异将在引擎盖下自动计算。第二种方法是编写经典的UnivariateFunction并将其传递给UnivariateFunctionDifferentiator接口的现有实现,以检索相同函数的差异化版本。第一种方法更适合用户已经控制所有底层代码的小函数。第二种方法更适合于使用DerivativeStructure API编写繁琐的大型函数,或者用户无法控制完整底层代码的函数(例如调用外部库的函数)。

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).

使用第一个想法。

// 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));

这篇关于Java - 使用Apache Commons Mathematic Library计算派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆