Define f(x) = (5x – 2) / (1 – 3x) with PolynomialFunction (or another class?) from org.apache.commons.math3.analysis.polynomials

huangapple go评论53阅读模式
英文:

Define f(x) = (5x - 2) / (1 - 3x) with PolynomialFunction (or another class?) from org.apache.commons.math3.analysis.polynomials

问题

我写了这个简单的类和测试用于我的培训以使用`org.apache.commons.math3.analysis.polynomials`中的`PolynomialFunction`。

```java
public class Derivee {
   /**
    * 获取导数值。
    * @param f 函数。
    * @param x 要获取导数的x值。
    * @param h 用于计算极限的差距。
    * @return 导数值。
    * @throws IllegalArgumentException 如果h为零。
    */
   public double getNombreDerive(PolynomialFunction f, double x, double h) {
      Objects.requireNonNull(f, "函数不能为空");

      if (h == 0) {
         throw new IllegalArgumentException("计算多项式导数时差距不能为零。");
      }

      double nombreDerive = (f.value(x+h) - f.value(x)) / h;

      if (LOGGER.isDebugEnabled()) {
         LOGGER.debug("在 {} 处的导数值为 {},对于 p(x) = {},使用 h = {} 测量。", x, nombreDerive, f, h);
      }

      return nombreDerive;
   }
}
class DeriveeTest {
   @Test @DisplayName("导数值")
   void nombresDerives() {
      PolynomialFunction f = new PolynomialFunction(new double[] {-2.0, 5.0});
      Derivee derivee = new Derivee();

      assertEquals(5.0, derivee.getNombreDerive(f, 3.0, 0.5), 0.01, "导数值不符合预期");
      assertThrows(IllegalArgumentException.class, () -> derivee.getNombreDerive(f, 3.0, 0), "接受了 h = 0:它应该被拒绝");
   }
}
07:57:51.863 [main] DEBUG fr.ecoemploi.domain.utils.math.derivees.Derivee - 
   在 3.0 处的导数值为 5.0,对于 p(x) = -2 + 5 x,使用 h = 0.5 测量。

它确实有效。但是如何输入多项式的组合:
f(x) = (5x - 2) / (1 - 3x)


<details>
<summary>英文:</summary>

I wrote this simple class and test for my training in using `PolynomialFunction` of `org.apache.commons.math3.analysis.polynomials`.

```java
public class Derivee {
   /**
    * Obtenir un nombre d&#233;riv&#233;.
    * @param f Fonction.
    * @param x Valeur x0 o&#249; l&#39;on veut le nombre d&#233;riv&#233;.
    * @param h &#233;cart pour calculer la limite.
    * @return Nombre d&#233;riv&#233;.
    * @throws IllegalArgumentException si h vaut z&#233;ro.
    */
   public double getNombreDerive(PolynomialFunction f, double x, double h) {
      Objects.requireNonNull(f, &quot;La fonction ne peut pas valoir null&quot;);

      if (h == 0) {
         throw new IllegalArgumentException(&quot;L&#39;&#233;cart pour calculer le nombre d&#233;riv&#233; d&#39;un polyn&#244;me ne peut pas valoir z&#233;ro.&quot;);
      }

      double nombreDerive = (f.value(x+h) - f.value(x)) / h;

      if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(&quot;Le nombre d&#233;riv&#233; en {} est {} pour p(x) = {}, mesur&#233; avec h = {}.&quot;, x, nombreDerive, f, h);
      }

      return nombreDerive;
   }
}
class DeriveeTest {
   @Test @DisplayName(&quot;nombres d&#233;riv&#233;s&quot;)
   void nombresDerives() {
      PolynomialFunction f = new PolynomialFunction(new double[] {-2.0, 5.0});
      Derivee derivee = new Derivee();

      assertEquals(5.0, derivee.getNombreDerive(f, 3.0, 0.5), 0.01, &quot;Le nombre d&#233;riv&#233; n&#39;est pas celui attendu&quot;);
      assertThrows(IllegalArgumentException.class, () -&gt; derivee.getNombreDerive(f, 3.0, 0), &quot;Un h = 0 a &#233;t&#233; accept&#233; : il aurait d&#251; &#234;tre refus&#233;&quot;);
   }
}
07:57:51.863 [main] DEBUG fr.ecoemploi.domain.utils.math.derivees.Derivee - 
   Le nombre d&#233;riv&#233; en 3.0 est 5.0 pour p(x) = -2 + 5 x, mesur&#233; avec h = 0.5.

It does work well. But how may I enter, for polynomial, the combination:
f(x) = (5x - 2) / (1 - 3x) ?

答案1

得分: 2

(5x - 2) / (1 - 3x) 不是一个多项式,所以在这里不应该使用 PolynomialFunction

你可以使用更一般的 UnivariantFunction 类型来表示它。你可以直接为它分配一个 lambda 表达式:

UnivariateFunction f = x -> (5 * x - 2)/(1 - 3 * x);

在你的代码中替换所有的 PolynomialFunction 后,你可以将 f 传递给 derivee.getNombreDerive,而不会出现编译错误。你没有使用与 PolynomialFunction 专有的内容。

请注意,除了使用 (f.value(x+h) - f.value(x)) / h 进行自己的微分之外,你还可以使用 Apache 的 DerivativeStructure

你可以像这样创建一个 UnivariantDifferentiableFunction

var f = new UnivariateDifferentiableFunction() {
    @Override
    public double value(double x) {
        return (5 * x - 2)/(1 - 3 * x);
    }

    @Override
    public DerivativeStructure value(DerivativeStructure x) throws DimensionMismatchException {
        var numerator = x.multiply(5).subtract(2);
        var denominator = x.multiply(-3).add(1);
        return numerator.divide(denominator);
    }
};

然后像这样在某个点获取导数:

var point = 2; // 假设你想在 x = 2 处获取导数。
var x = new DerivativeStructure(1, 1, 0, point);
System.out.println(f.value(x).getPartialDerivative(1));
英文:

(5x - 2) / (1 - 3x) is not a polynomial, so you should not use PolynomialFunction here.

You can use the more general UnivariantFunction type to represent it. You can directly assign a lambda to it:

UnivariateFunction f = x -&gt; (5 * x - 2)/(1 - 3 * x);

After replacing all the PolynomialFunctions in your code, you can pass f to derivee.getNombreDerive without compiler errors. You are not using anything exclusive to PolynomialFunctions.

Note that other than doing your own differentiation using (f.value(x+h) - f.value(x)) / h, you can also use DerivativeStructure from Apache.

You can make a UnivariantDifferentiableFunction like this:

var f = new UnivariateDifferentiableFunction() {
    @Override
    public double value(double x) {
        return (5 * x - 2)/(1 - 3 * x);
    }

    @Override
    public DerivativeStructure value(DerivativeStructure x) throws DimensionMismatchException {
        var numerator = x.multiply(5).subtract(2);
        var denominator = x.multiply(-3).add(1);
        return numerator.divide(denominator);
    }
};

And get the derivative at a certain point like this:

var point = 2; // suppose you want the derivative at x = 2.
var x = new DerivativeStructure(1, 1, 0, point);
System.out.println(f.value(x).getPartialDerivative(1));

huangapple
  • 本文由 发表于 2023年7月23日 14:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定