Apache Commons Math3学习笔记(2) - 多项式曲线拟合

多项式曲线拟合:org.apache.commons.math3.fitting.PolynomialCurveFitter类。

用法示例代码:

// ... 创建并初始化输入数据:
double[] x = new double[...];
double[] y = new double[...];
将原始的x-y数据序列合成带权重的观察点数据序列:
WeightedObservedPoints points = new WeightedObservedPoints();
// 将x-y数据元素调用points.add(x[i], y[i])加入到观察点序列中
// ...
PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);   // degree 指定多项式阶数
double[] result = fitter.fit(points.toList());   // 曲线拟合,结果保存于双精度数组中,由常数项至最高次幂系数排列

首先要准备好待拟合的曲线数据x和y,这是两个double数组,然后把这两个数组合并到WeightedObservedPoints对象实例中,可以调用WeightedObservedPoints.add(x[i], y[i])将x和y序列中的数据逐个添加到观察点序列对象中。随后创建PolynomialCurveFitter对象,创建时要指定拟合多项式的阶数,注意阶数要选择适当,不是越高越好,否则拟合误差会很大。最后调用PolynomialCurveFitter的fit方法即可完成多项式曲线拟合,fit方法的参数通过WeightedObservedPoints.toList()获得。拟合结果通过一个double数组返回,按元素顺序依次是常数项、一次项、二次项、……。

完整的演示代码如下:

interface TestCase
{
   public Object run(List<Object> params) throws Exception;
   public List<Object> getParams();
   public void printResult(Object result);
}

class CalcCurveFitting implements TestCase
{
   public CalcCurveFitting()
   {
      System.out.print("本算例用于计算多项式曲线拟合。正在初始化 计算数据(" + arrayLength + "点, " + degree + "阶)... ...");
      inputDataX = new double[arrayLength];
      //      inputDataX = new double[] {1, 2, 3, 4, 5, 6, 7};
      inputDataY = new double[inputDataX.length];
      double[] factor = new double[degree + 1];    // N阶多项式会有N+1个系数,其中之一为常数项
      for(int index = 0; index < factor.length; index ++)
      {
         factor[index] = index + 1;
      }
      for(int index = 0; index < inputDataY.length; index ++)
      {
         inputDataX[index] = index * 0.00001;
         inputDataY[index] = calcPoly(inputDataX[index], factor);    // y = sum(x[n) * fact[n])
         // System.out.print(inputDataY[index] + ", ");
      }
      points = new WeightedObservedPoints();
      for(int index = 0; index < inputDataX.length; index ++)
      {
         points.add(inputDataX[index], inputDataY[index]);
      }
      System.out.println("初始化完成");
   }

   @Override
   public List<Object> getParams()
   {
      List<Object> params = new ArrayList<Object>();
      params.add(points);
      return params;
   }

   @Override
   public Object run(List<Object> params) throws Exception
   {
      PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);
      WeightedObservedPoints points = (WeightedObservedPoints)params.get(0);
      double[] result = fitter.fit(points.toList());
      return result;
   }

   @Override
   public void printResult(Object result)
   {
      for(double data : (double[])result)
      {
         System.out.println(data);
      }
   }

   private double calcPoly(double x, double[] factor)
   {
      double y = 0;
      for(int deg = 0; deg < factor.length; deg ++)
      {
         y += Math.pow(x, deg) * factor[deg];
      }

      return y;
   }

   private double[] inputDataX = null;
   private double[] inputDataY = null;
   private WeightedObservedPoints points = null;

   private final int arrayLength = 200000;
   private final int degree = 5;    // 阶数

}

public class TimeCostCalculator
{
   public TimeCostCalculator()
   {
   }

   /**
    * 计算指定对象的运行时间开销。
    * 
    * @param testCase 指定被测对象。
    * @return 返回sub.run的时间开销,单位为s。
    * @throws Exception
    */
   public double calcTimeCost(TestCase testCase) throws Exception
   {
      List<Object> params = testCase.getParams();
      long startTime = System.nanoTime();
      Object result = testCase.run(params);
      long stopTime = System.nanoTime();
      testCase.printResult(result);
      System.out.println("start: " + startTime + " / stop: " + stopTime);
      double timeCost = (stopTime - startTime) * 1.0e-9;
      return timeCost;
   }

   public static void main(String[] args) throws Exception
   {
      TimeCostCalculator tcc = new TimeCostCalculator();
      double timeCost;

      System.out.println("--------------------------------------------------------------------------");
      timeCost = tcc.calcTimeCost(new CalcCurveFitting());
      System.out.println("time cost is: " + timeCost + "s");
      System.out.println("--------------------------------------------------------------------------");
   }

}


  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: 好的,以下是使用Apache Commons Math库实现多项式乘法的示例代码: ```java import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionLagrangeForm; public class PolynomialMultiplication { public static void main(String[] args) { // 定义两个多项式 double[] poly1Coeffs = {2.0, -3.0, 1.0}; // 2x^2 - 3x + 1 double[] poly2Coeffs = {1.0, 4.0}; // x + 4 PolynomialFunction poly1 = new PolynomialFunction(poly1Coeffs); PolynomialFunction poly2 = new PolynomialFunction(poly2Coeffs); // 计算两个多项式的乘积 PolynomialFunctionLagrangeForm product = PolynomialFunctionLagrangeForm.multiply(poly1, poly2); // 输出结果 System.out.println("The product of " + poly1 + " and " + poly2 + " is:"); System.out.println(product); } } ``` 在上面的代码中,我们先定义了两个多项式,然后使用`PolynomialFunctionLagrangeForm.multiply()`方法计算它们的乘积,最后输出结果。 注意,这里使用的是Lagrange形式的多项式,它是一种用于表示多项式的标准形式之一。如果需要使用另一种形式的多项式,可以参考Apache Commons Math库的文档。 ### 回答2: Apache Commons Math是一个用于数学计算的开源库,提供了众多数学函数和算法的实现。要实现多项式乘法,可以使用其中的Polynomial类。 首先,需要将多项式表示成一个double数组,数组的每个元素表示该项的系数。例如,多项式2x^2 + 3x + 1可以表示成数组[1, 3, 2],数组元素的索引表示该项的次数。 接下来,可以使用Polynomial类提供的multiply方法进行多项式的乘法。该方法接受两个多项式的数组形式作为参数,并返回乘积结果的数组。 以下是一个示例代码: ```java import org.apache.commons.math3.analysis.polynomials.Polynomial; public class PolynomialMultiplication { public static void main(String[] args) { double[] polynomial1 = {1, 3, 2}; // 2x^2 + 3x + 1 double[] polynomial2 = {2, -1}; // -x + 2 Polynomial p1 = new Polynomial(polynomial1); Polynomial p2 = new Polynomial(polynomial2); double[] product = p1.multiply(p2).getCoefficients(); // 输出乘积多项式的系数数组 for (double coefficient : product) { System.out.print(coefficient + " "); } } } ``` 该代码将输出结果为:2.0 -5.0 4.0。 这样,我们就使用Apache Commons MathPolynomial类实现了多项式的乘法。 ### 回答3: Apache Commons Math是一个用于数学和统计的Java库,其中包含了许多数学操作的实现。在Apache Commons Math库中,没有直接提供多项式乘法的实现方法,但我们可以利用其中的类和方法来实现多项式乘法。 首先,我们需要定义多项式的表示方式,可以使用数组或者ArrayList来存储多项式的系数。例如,对于多项式P(x) = 2x^2 + 3x + 1,可以表示为数组{1, 3, 2},其中第一个元素表示常数项,第二个元素表示一次项的系数,第三个元素表示二次项的系数。 然后,我们可以使用Apache Commons Math库中的类来实现多项式乘法。可以使用PolynomialFunction类来表示多项式对象,使用PolynomialFunctionUtils类中的multiply方法来进行多项式乘法操作。下面是一个使用Apache Commons Math库实现多项式乘法的示例代码: ```java import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionUtils; public class PolynomialMultiplicationExample { public static void main(String[] args) { // 定义多项式P(x) = 2x^2 + 3x + 1 PolynomialFunction polynomial1 = new PolynomialFunction(new double[] {1, 3, 2}); // 定义多项式Q(x) = x + 1 PolynomialFunction polynomial2 = new PolynomialFunction(new double[] {1, 1}); // 计算多项式P(x)和Q(x)的乘积 PolynomialFunction product = PolynomialFunctionUtils.multiply(polynomial1, polynomial2); // 打印乘积多项式的系数 double[] coefficients = product.getCoefficients(); for (int i = 0; i < coefficients.length; i++) { System.out.println("系数" + i + ": " + coefficients[i]); } } } ``` 以上示例代码定义了两个多项式P(x)和Q(x),使用PolynomialFunctionUtils类中的multiply方法计算它们的乘积,最后将乘积多项式的系数打印出来。 通过以上方式,我们可以利用Apache Commons Math库来实现多项式乘法操作。当然,也可以根据具体的需求和多项式的特征来自行定义对应的算法实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值