样条插值法(Java)
标签:img func test col ted sed ack ble 字符
该程序包含:样条插值法、读取文件,写入文件,字符型转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 source = splineInterpolatorImpl.getFileContent(args[0]);
34 File file = new File(args[1]);
35 // List 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 getFileContent(String filepath) {
50 List 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 ) {
104 // System.out.println(arr[i]);
105 d[i] = Double.valueOf(arr[i].trim());
106 }
107 }
108 return d;
109 }
110 }
样条插值法(Java)
标签:img func test col ted sed ack ble 字符
原文地址:https://www.cnblogs.com/xiao02fang/p/9705755.html
评论