180 lines
6.0 KiB
Java
180 lines
6.0 KiB
Java
package com.gunshi;
|
||
|
||
import com.gunshi.project.ss.common.model.vo.OsmoticPressDetailVo;
|
||
import com.gunshi.project.ss.model.RegressionEquation;
|
||
import com.gunshi.project.ss.util.RegressionAnalysis;
|
||
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
|
||
|
||
import java.math.BigDecimal;
|
||
import java.math.RoundingMode;
|
||
import java.text.SimpleDateFormat;
|
||
import java.time.format.DateTimeFormatter;
|
||
import java.util.*;
|
||
|
||
public class Tests extends Thread {
|
||
|
||
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
|
||
public static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||
|
||
public static void main(String[] args) throws Exception {
|
||
|
||
}
|
||
|
||
class Myclass implements Cloneable{
|
||
private String name;
|
||
|
||
@Override
|
||
public Object clone() throws CloneNotSupportedException {
|
||
Myclass clone = (Myclass) super.clone();
|
||
return clone;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 根据回归方程计算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;
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
/**
|
||
* 计算三阶方程 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;
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
/**
|
||
* 计算四阶方程 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
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
}
|