亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼

 更新時間:2017年10月19日 15:09:54   作者:狐帝  
這篇文章主要介紹了Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼,小編覺得挺不錯的,這里分享給大家,供需要的朋友參考。

上一篇文章我們介紹了Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實例代碼,這里給大家分享math3多項式曲線擬合的相關(guān)內(nèi)容,具體如下。

多項式曲線擬合:org.apache.commons.math3.fitting.PolynomialCurveFitter類。

用法示例代碼:

// ... 創(chuàng)建并初始化輸入數(shù)據(jù): 
double[] x = new double[...]; 
double[] y = new double[...]; 
將原始的x-y數(shù)據(jù)序列合成帶權(quán)重的觀察點數(shù)據(jù)序列: 
WeightedObservedPoints points = new WeightedObservedPoints(); 
// 將x-y數(shù)據(jù)元素調(diào)用points.add(x[i], y[i])加入到觀察點序列中 
// ... 
PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree);  // degree 指定多項式階數(shù) 
double[] result = fitter.fit(points.toList());  // 曲線擬合,結(jié)果保存于雙精度數(shù)組中,由常數(shù)項至最高次冪系數(shù)排列 

首先要準(zhǔn)備好待擬合的曲線數(shù)據(jù)x和y,這是兩個double數(shù)組,然后把這兩個數(shù)組合并到WeightedObservedPoints對象實例中,可以調(diào)用WeightedObservedPoints.add(x[i], y[i])將x和y序列中的數(shù)據(jù)逐個添加到觀察點序列對象中。隨后創(chuàng)建PolynomialCurveFitter對象,創(chuàng)建時要指定擬合多項式的階數(shù),注意階數(shù)要選擇適當(dāng),不是越高越好,否則擬合誤差會很大。最后調(diào)用PolynomialCurveFitter的fit方法即可完成多項式曲線擬合,fit方法的參數(shù)通過WeightedObservedPoints.toList()獲得。擬合結(jié)果通過一個double數(shù)組返回,按元素順序依次是常數(shù)項、一次項、二次項、……。

完整的演示代碼如下:

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("本算例用于計算多項式曲線擬合。正在初始化 計算數(shù)據(jù)(" + 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個系數(shù),其中之一為常數(shù)項 
   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;  // 階數(shù) 
} 
public class TimeCostCalculator 
{ 
  public TimeCostCalculator() 
  { 
  } 
  /** 
  * 計算指定對象的運(yùn)行時間開銷。 
  * 
  * @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("--------------------------------------------------------------------------"); 
  } 
} 

總結(jié)

以上就是本文關(guān)于Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實例代碼、apache zookeeper使用方法實例詳解等,有什么問題可以隨時留言,小編會及時回復(fù)大家。下面推薦幾本Java方面的書籍,供大家學(xué)習(xí),免費(fèi)的哦。

7天學(xué)會spring+cloud教程 pdf格式

chabaoo.cn/books/575161.html

Java核心技術(shù):基礎(chǔ)知識 卷I(原書第10版) (凱S.霍斯特曼) 中文pdf完整版

http://chabaoo.cn/books/569792.html

更多精彩內(nèi)容,盡在http://chabaoo.cn/

相關(guān)文章

  • 詳解Java使用JMH進(jìn)行基準(zhǔn)性能測試

    詳解Java使用JMH進(jìn)行基準(zhǔn)性能測試

    本文主要介紹了Java使用JMH進(jìn)行基準(zhǔn)性能測試,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 淺談Hibernate n+1問題

    淺談Hibernate n+1問題

    這篇文章主要介紹了淺談Hibernate n+1問題,怎么解決n+1問題,文中也作了簡要分析,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Mybatis-Plus實現(xiàn)只更新部分字段的數(shù)據(jù)

    Mybatis-Plus實現(xiàn)只更新部分字段的數(shù)據(jù)

    這篇文章主要介紹了Mybatis-Plus實現(xiàn)只更新部分字段的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 詳解Java目錄操作與文件操作教程

    詳解Java目錄操作與文件操作教程

    本章具體介紹了目錄操作、文件操作的基本使用方法和常用函數(shù),圖解穿插代碼實現(xiàn),感興趣的朋友來看看吧
    2022-03-03
  • Springboot集成Protobuf的流程步驟

    Springboot集成Protobuf的流程步驟

    在以往的項目中進(jìn)行網(wǎng)絡(luò)通信和數(shù)據(jù)交換的應(yīng)用場景中,最經(jīng)常使用的技術(shù)便是json或xml,但是今天在介紹一個Google的力作protobuf作為數(shù)據(jù)交換格式,文中給大家介紹了Springboot集成Protobuf的流程步驟,需要的朋友可以參考下
    2024-03-03
  • java讀取簡單excel通用工具類

    java讀取簡單excel通用工具類

    這篇文章主要為大家詳細(xì)介紹了java讀取簡單excel通用工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Springboot如何連接遠(yuǎn)程服務(wù)器上的數(shù)據(jù)庫實踐

    Springboot如何連接遠(yuǎn)程服務(wù)器上的數(shù)據(jù)庫實踐

    本文主要介紹了Springboot如何連接遠(yuǎn)程服務(wù)器上的數(shù)據(jù)庫實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Spring中的refreshContext源碼分析

    Spring中的refreshContext源碼分析

    這篇文章主要介紹了Spring中的refreshContext源碼分析,在SpringBoot啟動流程中,主要的兩個階段是初始化SpringApplication對象以及SpringApplication.run方法執(zhí)行的內(nèi)容,今天主要細(xì)講的是SpringApplication.run中的刷新容器refreshContext方法,需要的朋友可以參考下
    2023-12-12
  • SpringMVC 數(shù)據(jù)校驗實例解析

    SpringMVC 數(shù)據(jù)校驗實例解析

    這篇文章主要介紹了SpringMVC 數(shù)據(jù)校驗實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • RocketMq同組消費(fèi)者如何自動設(shè)置InstanceName

    RocketMq同組消費(fèi)者如何自動設(shè)置InstanceName

    這篇文章主要介紹了RocketMq同組消費(fèi)者如何自動設(shè)置InstanceName問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評論