2025-11-03 16:47:35 +08:00
|
|
|
|
package com.gunshi;
|
|
|
|
|
|
|
2025-11-18 10:53:17 +08:00
|
|
|
|
import com.gunshi.project.hsz.common.model.vo.OsmoticPressDetailVo;
|
|
|
|
|
|
import com.gunshi.project.hsz.model.RegressionEquation;
|
|
|
|
|
|
import com.gunshi.project.hsz.util.ProjectCalculateUtil;
|
|
|
|
|
|
import com.gunshi.project.hsz.util.RegressionAnalysis;
|
|
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
import java.math.RoundingMode;
|
|
|
|
|
|
import java.text.Format;
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
import java.util.*;
|
2025-11-03 16:47:35 +08:00
|
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
|
|
|
|
|
|
|
|
public class Tests extends Thread {
|
|
|
|
|
|
|
2025-11-18 10:53:17 +08:00
|
|
|
|
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
|
|
public static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
2025-11-03 16:47:35 +08:00
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2025-11-18 10:53:17 +08:00
|
|
|
|
List<OsmoticPressDetailVo> res = new ArrayList<>();
|
|
|
|
|
|
OsmoticPressDetailVo o1 = new OsmoticPressDetailVo();
|
|
|
|
|
|
o1.setRz(new BigDecimal("119.19"));
|
|
|
|
|
|
o1.setValue(new BigDecimal("99.65"));
|
|
|
|
|
|
|
|
|
|
|
|
OsmoticPressDetailVo o2 = new OsmoticPressDetailVo();
|
|
|
|
|
|
o2.setRz(new BigDecimal("119.19"));
|
|
|
|
|
|
o2.setValue(new BigDecimal("99.655"));
|
|
|
|
|
|
|
|
|
|
|
|
OsmoticPressDetailVo o3 = new OsmoticPressDetailVo();
|
|
|
|
|
|
o3.setRz(new BigDecimal("119.2"));
|
|
|
|
|
|
o3.setValue(new BigDecimal("99.665"));
|
|
|
|
|
|
|
|
|
|
|
|
OsmoticPressDetailVo o4 = new OsmoticPressDetailVo();
|
|
|
|
|
|
o4.setRz(new BigDecimal("119.22"));
|
|
|
|
|
|
o4.setValue(new BigDecimal("99.675"));
|
|
|
|
|
|
|
|
|
|
|
|
OsmoticPressDetailVo o5 = new OsmoticPressDetailVo();
|
|
|
|
|
|
o5.setRz(new BigDecimal("119.23"));
|
|
|
|
|
|
o5.setValue(new BigDecimal("99.645"));
|
|
|
|
|
|
res.add(o1);
|
|
|
|
|
|
res.add(o2);
|
|
|
|
|
|
res.add(o5);
|
|
|
|
|
|
res.add(o3);
|
|
|
|
|
|
res.add(o4);
|
|
|
|
|
|
RegressionEquation first = RegressionAnalysis.calculateLinear(res);
|
|
|
|
|
|
System.out.println(first.toString());
|
|
|
|
|
|
BigDecimal decimal = calculateRegressionValue(first.toString(), new BigDecimal("119.19"));
|
|
|
|
|
|
System.out.println(decimal.toString());
|
|
|
|
|
|
RegressionEquation second = RegressionAnalysis.calculateQuadratic(res);
|
|
|
|
|
|
BigDecimal decimal1 = calculateRegressionValue(second.toString(), new BigDecimal("119.19"));
|
|
|
|
|
|
System.out.println(decimal1.toString());
|
|
|
|
|
|
System.out.println(second.toString());
|
|
|
|
|
|
RegressionEquation three = RegressionAnalysis.calculateCubic(res);
|
|
|
|
|
|
System.out.println(calculateRegressionValue(three.toString(), new BigDecimal("119.19")));
|
|
|
|
|
|
System.out.println(three.toString());
|
|
|
|
|
|
RegressionEquation four = RegressionAnalysis.calculateQuartic(res);
|
|
|
|
|
|
// System.out.println(calculateRegressionValue(four.toString(), new BigDecimal("119.19")));
|
|
|
|
|
|
System.out.println(four.toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据回归方程计算y值
|
|
|
|
|
|
* @param equation 回归方程字符串
|
|
|
|
|
|
* @param x 自变量x的值
|
|
|
|
|
|
* @return 计算得到的y值
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static BigDecimal calculateRegressionValue(String equation, BigDecimal x) {
|
|
|
|
|
|
if (equation == null || equation.trim().isEmpty()) {
|
|
|
|
|
|
return BigDecimal.ZERO;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清理方程字符串,移除空格和y=
|
|
|
|
|
|
String cleanEquation = equation.replace("y =", "").replace(" ", "").toLowerCase();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
BigDecimal result;
|
|
|
|
|
|
// 根据方程的形式判断阶数并计算
|
|
|
|
|
|
if (cleanEquation.contains("x^4")) {
|
|
|
|
|
|
result = calculateFourthOrder(cleanEquation, x);
|
|
|
|
|
|
} else if (cleanEquation.contains("x^3")) {
|
|
|
|
|
|
result = calculateThirdOrder(cleanEquation, x);
|
|
|
|
|
|
} else if (cleanEquation.contains("x^2")) {
|
|
|
|
|
|
result = calculateSecondOrder(cleanEquation, x);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
result = calculateFirstOrder(cleanEquation, x);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 四舍五入保留两位小数
|
|
|
|
|
|
return result.setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
// 计算异常时返回0
|
|
|
|
|
|
return BigDecimal.ZERO;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 计算一阶线性方程 y = ax + b
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static BigDecimal calculateFirstOrder(String equation, BigDecimal x) {
|
|
|
|
|
|
// 解析方程系数,格式如:0.0455x+94.2395
|
|
|
|
|
|
String[] parts = equation.split("x");
|
|
|
|
|
|
if (parts.length < 2) {
|
|
|
|
|
|
return BigDecimal.ZERO;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BigDecimal a = parseCoefficient(parts[0]);
|
|
|
|
|
|
BigDecimal b = parseCoefficient(parts[1]);
|
|
|
|
|
|
|
|
|
|
|
|
return a.multiply(x).add(b);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 计算二阶方程 y = ax^2 + bx + c
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static BigDecimal calculateSecondOrder(String equation, BigDecimal x) {
|
|
|
|
|
|
// 解析方程系数,格式如:-68.4211x^2+16312.8684x-972224.1397
|
|
|
|
|
|
String[] parts = equation.split("x");
|
|
|
|
|
|
if (parts.length < 3) {
|
|
|
|
|
|
return BigDecimal.ZERO;
|
2025-11-03 16:47:35 +08:00
|
|
|
|
}
|
2025-11-18 10:53:17 +08:00
|
|
|
|
|
|
|
|
|
|
BigDecimal a = parseCoefficient(parts[0]);
|
|
|
|
|
|
BigDecimal b = parseCoefficient(parts[1].replace("^2", ""));
|
|
|
|
|
|
BigDecimal c = parseCoefficient(parts[2]);
|
|
|
|
|
|
|
|
|
|
|
|
BigDecimal x2 = x.multiply(x); // x^2
|
|
|
|
|
|
return a.multiply(x2).add(b.multiply(x)).add(c);
|
2025-11-03 16:47:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 10:53:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 计算三阶方程 y = ax^3 + bx^2 + cx + d
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static BigDecimal calculateThirdOrder(String equation, BigDecimal x) {
|
|
|
|
|
|
// 解析方程系数,格式如:-2291.6667x^3+819497.9167x^2-97683901.8750x+3881297151.1650
|
|
|
|
|
|
String[] parts = equation.split("x");
|
|
|
|
|
|
if (parts.length < 4) {
|
|
|
|
|
|
return BigDecimal.ZERO;
|
2025-11-03 16:47:35 +08:00
|
|
|
|
}
|
2025-11-18 10:53:17 +08:00
|
|
|
|
|
|
|
|
|
|
BigDecimal a = parseCoefficient(parts[0]);
|
|
|
|
|
|
BigDecimal b = parseCoefficient(parts[1].replace("^3", "").replace("^2", ""));
|
|
|
|
|
|
BigDecimal c = parseCoefficient(parts[2].replace("^2", ""));
|
|
|
|
|
|
BigDecimal d = parseCoefficient(parts[3]);
|
|
|
|
|
|
|
|
|
|
|
|
BigDecimal x2 = x.multiply(x); // x^2
|
|
|
|
|
|
BigDecimal x3 = x2.multiply(x); // x^3
|
|
|
|
|
|
|
|
|
|
|
|
return a.multiply(x3).add(b.multiply(x2)).add(c.multiply(x)).add(d);
|
2025-11-03 16:47:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 10:53:17 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 计算四阶方程 y = ax^4 + bx^3 + cx^2 + dx + e
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static BigDecimal calculateFourthOrder(String equation, BigDecimal x) {
|
|
|
|
|
|
// 解析方程系数,格式如:-5.9039x^4+523.5482x^3+316095.2736x^2-57676816.2672x+2688986002.6804
|
|
|
|
|
|
String[] parts = equation.split("x");
|
|
|
|
|
|
if (parts.length < 5) {
|
|
|
|
|
|
return BigDecimal.ZERO;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BigDecimal a = parseCoefficient(parts[0]);
|
|
|
|
|
|
BigDecimal b = parseCoefficient(parts[1].replace("^4", "").replace("^3", ""));
|
|
|
|
|
|
BigDecimal c = parseCoefficient(parts[2].replace("^3", "").replace("^2", ""));
|
|
|
|
|
|
BigDecimal d = parseCoefficient(parts[3].replace("^2", ""));
|
|
|
|
|
|
BigDecimal e = parseCoefficient(parts[4]);
|
|
|
|
|
|
|
|
|
|
|
|
BigDecimal x2 = x.multiply(x); // x^2
|
|
|
|
|
|
BigDecimal x3 = x2.multiply(x); // x^3
|
|
|
|
|
|
BigDecimal x4 = x3.multiply(x); // x^4
|
2025-11-03 16:47:35 +08:00
|
|
|
|
|
2025-11-18 10:53:17 +08:00
|
|
|
|
return a.multiply(x4).add(b.multiply(x3)).add(c.multiply(x2)).add(d.multiply(x)).add(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 解析系数,处理正负号和数字格式
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static BigDecimal parseCoefficient(String coeffStr) {
|
|
|
|
|
|
if (coeffStr == null || coeffStr.isEmpty()) {
|
|
|
|
|
|
return BigDecimal.ZERO;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理空字符串情况
|
|
|
|
|
|
if (coeffStr.equals("+") || coeffStr.equals("-")) {
|
|
|
|
|
|
coeffStr += "1";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理没有显式数字的情况,如:x^2+x+1 中的第一个x系数为1
|
|
|
|
|
|
if (coeffStr.equals("")) {
|
|
|
|
|
|
return BigDecimal.ONE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
return new BigDecimal(coeffStr);
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
// 如果解析失败,尝试处理特殊情况
|
|
|
|
|
|
if (coeffStr.equals("+")) {
|
|
|
|
|
|
return BigDecimal.ONE;
|
|
|
|
|
|
} else if (coeffStr.equals("-")) {
|
|
|
|
|
|
return BigDecimal.ONE.negate();
|
|
|
|
|
|
}
|
|
|
|
|
|
return BigDecimal.ZERO;
|
2025-11-03 16:47:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-18 10:53:17 +08:00
|
|
|
|
|
2025-11-03 16:47:35 +08:00
|
|
|
|
}
|