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

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

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

问题

  1. 我写了这个简单的类和测试用于我的培训以使用`org.apache.commons.math3.analysis.polynomials`中的`PolynomialFunction`
  2. ```java
  3. public class Derivee {
  4. /**
  5. * 获取导数值。
  6. * @param f 函数。
  7. * @param x 要获取导数的x值。
  8. * @param h 用于计算极限的差距。
  9. * @return 导数值。
  10. * @throws IllegalArgumentException 如果h为零。
  11. */
  12. public double getNombreDerive(PolynomialFunction f, double x, double h) {
  13. Objects.requireNonNull(f, "函数不能为空");
  14. if (h == 0) {
  15. throw new IllegalArgumentException("计算多项式导数时差距不能为零。");
  16. }
  17. double nombreDerive = (f.value(x+h) - f.value(x)) / h;
  18. if (LOGGER.isDebugEnabled()) {
  19. LOGGER.debug("在 {} 处的导数值为 {},对于 p(x) = {},使用 h = {} 测量。", x, nombreDerive, f, h);
  20. }
  21. return nombreDerive;
  22. }
  23. }
  1. class DeriveeTest {
  2. @Test @DisplayName("导数值")
  3. void nombresDerives() {
  4. PolynomialFunction f = new PolynomialFunction(new double[] {-2.0, 5.0});
  5. Derivee derivee = new Derivee();
  6. assertEquals(5.0, derivee.getNombreDerive(f, 3.0, 0.5), 0.01, "导数值不符合预期");
  7. assertThrows(IllegalArgumentException.class, () -> derivee.getNombreDerive(f, 3.0, 0), "接受了 h = 0:它应该被拒绝");
  8. }
  9. }
  1. 07:57:51.863 [main] DEBUG fr.ecoemploi.domain.utils.math.derivees.Derivee -
  2. 3.0 处的导数值为 5.0,对于 p(x) = -2 + 5 x,使用 h = 0.5 测量。

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

  1. <details>
  2. <summary>英文:</summary>
  3. I wrote this simple class and test for my training in using `PolynomialFunction` of `org.apache.commons.math3.analysis.polynomials`.
  4. ```java
  5. public class Derivee {
  6. /**
  7. * Obtenir un nombre d&#233;riv&#233;.
  8. * @param f Fonction.
  9. * @param x Valeur x0 o&#249; l&#39;on veut le nombre d&#233;riv&#233;.
  10. * @param h &#233;cart pour calculer la limite.
  11. * @return Nombre d&#233;riv&#233;.
  12. * @throws IllegalArgumentException si h vaut z&#233;ro.
  13. */
  14. public double getNombreDerive(PolynomialFunction f, double x, double h) {
  15. Objects.requireNonNull(f, &quot;La fonction ne peut pas valoir null&quot;);
  16. if (h == 0) {
  17. 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;);
  18. }
  19. double nombreDerive = (f.value(x+h) - f.value(x)) / h;
  20. if (LOGGER.isDebugEnabled()) {
  21. LOGGER.debug(&quot;Le nombre d&#233;riv&#233; en {} est {} pour p(x) = {}, mesur&#233; avec h = {}.&quot;, x, nombreDerive, f, h);
  22. }
  23. return nombreDerive;
  24. }
  25. }
  1. class DeriveeTest {
  2. @Test @DisplayName(&quot;nombres d&#233;riv&#233;s&quot;)
  3. void nombresDerives() {
  4. PolynomialFunction f = new PolynomialFunction(new double[] {-2.0, 5.0});
  5. Derivee derivee = new Derivee();
  6. 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;);
  7. 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;);
  8. }
  9. }
  1. 07:57:51.863 [main] DEBUG fr.ecoemploi.domain.utils.math.derivees.Derivee -
  2. 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 表达式:

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

  1. var f = new UnivariateDifferentiableFunction() {
  2. @Override
  3. public double value(double x) {
  4. return (5 * x - 2)/(1 - 3 * x);
  5. }
  6. @Override
  7. public DerivativeStructure value(DerivativeStructure x) throws DimensionMismatchException {
  8. var numerator = x.multiply(5).subtract(2);
  9. var denominator = x.multiply(-3).add(1);
  10. return numerator.divide(denominator);
  11. }
  12. };

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

  1. var point = 2; // 假设你想在 x = 2 处获取导数。
  2. var x = new DerivativeStructure(1, 1, 0, point);
  3. 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:

  1. 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:

  1. var f = new UnivariateDifferentiableFunction() {
  2. @Override
  3. public double value(double x) {
  4. return (5 * x - 2)/(1 - 3 * x);
  5. }
  6. @Override
  7. public DerivativeStructure value(DerivativeStructure x) throws DimensionMismatchException {
  8. var numerator = x.multiply(5).subtract(2);
  9. var denominator = x.multiply(-3).add(1);
  10. return numerator.divide(denominator);
  11. }
  12. };

And get the derivative at a certain point like this:

  1. var point = 2; // suppose you want the derivative at x = 2.
  2. var x = new DerivativeStructure(1, 1, 0, point);
  3. 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:

确定