Apache Commons Math3學習筆記(2)
阿新 • • 發佈:2019-01-04
多項式曲線擬合: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("--------------------------------------------------------------------------"); } }