1. 程式人生 > >樣條插值法(Java)

樣條插值法(Java)

Coding trace 算法 top writer 檢查 block 技術分享 iter

該程序包含:樣條插值法、讀取文件,寫入文件,字符型轉double型方法等;

適合初學Java的人學習;

在cmd中執行,

技術分享圖片

在Linux中執行

技術分享圖片

完整代碼如下:

樣條插值法:

  1 import java.io.BufferedReader;
  2 import java.io.File;
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileWriter;
  6 import java.io.IOException;
  7 import
java.io.InputStreamReader; 8 import java.io.UnsupportedEncodingException; 9 import java.util.ArrayList; 10 import java.util.List; 11 12 import org.apache.commons.lang3.StringUtils; 13 import org.apache.commons.math3.analysis.interpolation.SplineInterpolator; 14 import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
15 16 17 /** 18 * 樣條插值法 19 * @author 91911 20 */ 21 public class SplineInterpolatorImpl { 22 public static void main(String[] args){ 23 // String[] source = new String[]{ 24 // "0,1,2,3 0,1,1,0 2.5", 25 // "0,1,2,3 0,1,1,0 1.5" 26 // }; 27
// 判斷傳入參數的長度,必須輸入兩個參數(輸入文件和輸出文件),否則報錯 28 if (args.length != 2) { 29 System.out.println("請輸入原文件和輸出文件的路徑!!"); 30 System.exit(0); 31 } 32 SplineInterpolatorImpl splineInterpolatorImpl = new SplineInterpolatorImpl(); 33 List<String> source = splineInterpolatorImpl.getFileContent(args[0]); 34 File file = new File(args[1]); 35 // List<String> source = splineInterpolatorImpl.getFileContent("C:/Users/91911/Desktop/test.txt"); 36 // File file = new File("C:/Users/91911/Desktop/result.txt"); 37 for(String s1:source) { 38 String splited[] = s1.split("\t"); 39 double[] x = splineInterpolatorImpl.String2Double(splited[0]); 40 double[] y = splineInterpolatorImpl.String2Double(splited[1]); 41 double z = Double.parseDouble(splited[2]); 42 double result = splineInterpolatorImpl.caculate(x, y, z); 43 exportFile(s1+"\t"+result,file); 44 // System.out.println(splineInterpolatorImpl.caculate(x, y, z)); 45 } 46 } 47 48 // 讀取配置文檔 49 public static List<String> getFileContent(String filepath) { 50 List<String> list = new ArrayList<>(); 51 BufferedReader br; 52 String rec; 53 try { 54 br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filepath)), "GBK")); 55 while ((rec = br.readLine()) != null) { 56 if (StringUtils.isNotEmpty(rec.trim())) { 57 list.add(rec); 58 } 59 } 60 br.close(); 61 } catch (UnsupportedEncodingException e) { 62 // TODO Auto-generated catch block 63 System.out.println("轉碼出錯!"); 64 e.printStackTrace(); 65 return null; 66 } catch (FileNotFoundException e) { 67 // TODO Auto-generated catch block 68 System.out.println("未找到配置文件 " + filepath + " ,請檢查該路徑是否正確!"); 69 e.printStackTrace(); 70 return null; 71 } catch (IOException e) { 72 // TODO Auto-generated catch block 73 e.printStackTrace(); 74 return null; 75 } 76 return list; 77 } 78 79 //寫文件 80 public static void exportFile(String content,File file){ 81 try { 82 FileWriter out = new FileWriter(file,true); 83 out.write(content + "\r\n"); 84 out.flush(); 85 out.close(); 86 }catch (IOException e){ 87 System.out.println("!IO異常,寫文件異常"); 88 } 89 } 90 //樣條計算法 91 public double caculate(double[] x,double[] y, double z){ 92 SplineInterpolator sp = new SplineInterpolator(); 93 PolynomialSplineFunction f = sp.interpolate(x, y); 94 return f.value(z); 95 } 96 97 //將字符型轉換為double型 98 public static double[] String2Double(String str) { 99 double[] d = { 1 }; 100 if (str.contains(",")) { 101 String[] arr = str.split(","); 102 d = new double[arr.length]; 103 for (int i = 0; i < arr.length; i++) { 104 // System.out.println(arr[i]); 105 d[i] = Double.valueOf(arr[i].trim()); 106 } 107 } 108 return d; 109 } 110 }

樣條插值法(Java)