Java—使用Apache Commons数学库计算派生

Java—使用Apache Commons数学库计算派生,java,derivative,differentiation,apache-commons-math,automatic-differentiation,Java,Derivative,Differentiation,Apache Commons Math,Automatic Differentiation,我在使用apache commons数学库时遇到问题。 我只想创建像f(x)=4x^2+2x这样的函数,我想计算这个函数的导数-->f'(x)=8x+2 我读了关于差异化的文章(,第4.7节) 有一个我不明白的例子: int params = 1; int order = 3; double xRealValue = 2.5; DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue); Deriva

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

我读了关于差异化的文章(,第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)
被调用但未定义。我做错了什么
有没有人有过ApacheCommons数学库的微分/派生经验,或者有没有人知道另一个库/框架可以帮助我


感谢

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

用户可以通过多种方式创建单变量可微分函数接口的实现。第一种方法是直接编写它,使用派生结构中的适当方法来计算加法、减法、正弦、余弦。。。这通常非常简单,不需要记住区分规则:用户代码只表示函数本身,区分将在引擎盖下自动计算。第二种方法是编写一个经典的单变量函数,并将其传递给单变量函数微分器接口的现有实现,以检索同一函数的微分版本。第一种方法更适合于用户已经控制所有底层代码的小函数。第二种方法更适用于使用DerivativeStructure API编写起来很麻烦的大型函数,或者用户无法控制完整底层代码的函数(例如调用外部库的函数)

使用第一个想法

// 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));
下面似乎说明了如何定义一元可微函数导数的两种可能方法。我正在添加一个新答案,因为我无法对前一个答案(声誉不足)发表评论

函数使用的示例规范是f(x)=x^2

(1) 使用派生结构:

public DerivativeStructure value(DerivativeStructure t) {
     return t.multiply(t);
}
(2) 通过编写一个经典的单变量函数:

public UnivariateRealFunction derivative() {
    return new UnivariateRealFunction() {
          public double value(double x) {
                // example derivative
                return 2.*x;
          }
     }
}
如果我理解清楚的话,第一种情况的优点是,不需要像第二种情况那样手动获取导数。如果导数是已知的,那么定义导数结构应该没有什么好处,对吗?我想到的应用是牛顿-拉斐逊解算器,通常需要知道函数值及其导数


上述网站提供了完整的示例(作者是Thomas Neidhart和Franz Simons)。欢迎任何进一步的评论

这可能是因为方法
f
和变量
x
不存在。是的,该方法不存在,但这是apache commons数学网站上的一个示例。他们的目的是什么?要显示未定义已用函数的示例,Commons math有更简单的方法来创建数学函数,但
DerivativeStructure
是获取导数的方法。是的,我知道文档非常不透明。我想知道是谁想出了这种区分的方法?不是数学家或物理学家。第二种解决方案可能有两个问题:(i)在当前版本的math3中,“单变量函数”类似乎已被“单变量函数”取代。(ii)第二对括号结尾处可能缺少分号。