This commit is contained in:
caixiang
2022-07-25 16:58:55 +08:00
parent 1477d05a69
commit d26600892f
16 changed files with 767 additions and 178 deletions

View File

@@ -2,6 +2,7 @@ package com.cnbm.common.spc.math;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
public class Math {
@@ -17,7 +18,7 @@ public class Math {
// }
//8.1 10.6 9.8 9.0 9.4 9.3 10.2 11.7
double[] d = new double[8];
Double[] d = new Double[8];
d[0] = new Double(8.1) ;
d[1] = new Double(10.6);
d[2] = new Double(9.8);
@@ -32,12 +33,12 @@ public class Math {
}
//平均值
public static double getMean(double...numbers) {
public static double getMean(Double...numbers) {
return format(getSum(numbers) / numbers.length);
}
//极差
public static double range(double[] in) {
public static double range(Double[] in) {
if (in == null) {
throw new NumberFormatException();
}
@@ -51,14 +52,14 @@ public class Math {
//return Mutil.subtract(max, min);
}
public static double format(double value) {
public static double format(Double value) {
DecimalFormat df = new DecimalFormat("0.00");//创建一个df对象传入0.00表示构造一个保留小数点后两位的df对象
df.setRoundingMode(RoundingMode.HALF_UP);//设置规则,这里采用的也是四舍五入规则
return Double.parseDouble(df.format(value));//返回value在返回之前使用df对象的格式化方法将数据格式化
}
//方差s^2=[(x1-x)^2 +...(xn-x)^2]/n 或者s^2=[(x1-x)^2 +...(xn-x)^2]/(n-1)
public static double Variance(double[] x) {
public static double Variance(Double[] x) {
int m=x.length;
double sum=0;
for(int i=0;i<m;i++){//求和
@@ -72,8 +73,10 @@ public class Math {
return format(dVar/m);
}
//标准差σ=sqrt(s^2)
public static double StandardDiviation(double[] x) {
public static StandardDiviation StandardDiviation(Double[] x) {
int m=x.length;
double sum=0;
for(int i=0;i<m;i++){//求和
@@ -84,13 +87,12 @@ public class Math {
for(int i=0;i<m;i++){//求方差
dVar+=(x[i]-dAve)*(x[i]-dAve);
}
return format(java.lang.Math.sqrt(dVar/(m-1)));
return new StandardDiviation(format(java.lang.Math.sqrt(dVar/(m-1))),format(java.lang.Math.sqrt(dVar/(m))));
//return Math.sqrt(dVar/m);
}
//前n项和
public static double getSum(double...numbers) {
public static double getSum(Double...numbers) {
double sum = 0.0;
for (double i : numbers) {

View File

@@ -0,0 +1,21 @@
package com.cnbm.common.spc.math;
import lombok.Data;
/**
* @Desc: ""
* @Author: caixiang
* @DATE: 2022/7/25 15:23
*/
@Data
public class StandardDiviation {
//标准差
private Double normal;
//总体标准差
private Double totality;
public StandardDiviation(Double normal, Double totality) {
this.normal = normal;
this.totality = totality;
}
}