gpt4 book ai didi

Java - 使用 Apache Commons 数学库计算导数

转载 作者:行者123 更新时间:2023-12-01 12:45:45 25 4
gpt4 key购买 nike

我在使用 apache commons 数学库时遇到问题。
我只想创建像 f(x) = 4x^2 + 2x 这样的函数,我想计算这个函数的导数 --> f'(x) = 8x + 2

我阅读了关于分化的文章( http://commons.apache.org/proper/commons-math/userguide/analysis.html ,第 4.7 节)。
有一个我不明白的例子:

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 数学库的微分/推导有任何经验,或者有没有人知道另一个可以帮助我的库/框架?

谢谢

最佳答案

在该示例下面的段落中,作者描述了创建 DerivativeStructure 的方法。 s。这不是魔法。在您引用的示例中,应该有人编写函数 f .嗯,那不是很清楚。

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 数学库计算导数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16781563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com