apache commons Math库简单实用

Posted songfu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache commons Math库简单实用相关的知识,希望对你有一定的参考价值。

packagetest.ffm83.commons.math;
importorg.apache.commons.math3.linear.Array2DRowRealMatrix; 
import org.apache.commons.math3.linear.LUDecomposition; 
importorg.apache.commons.math3.linear.RealMatrix; 
importorg.apache.commons.math3.stat.descriptive.moment.GeometricMean; 
importorg.apache.commons.math3.stat.descriptive.moment.Kurtosis; 
importorg.apache.commons.math3.stat.descriptive.moment.Mean; 
importorg.apache.commons.math3.stat.descriptive.moment.Skewness; 
importorg.apache.commons.math3.stat.descriptive.moment.StandardDeviation; 
importorg.apache.commons.math3.stat.descriptive.moment.Variance; 
import org.apache.commons.math3.stat.descriptive.rank.Max; 
importorg.apache.commons.math3.stat.descriptive.rank.Min; 
importorg.apache.commons.math3.stat.descriptive.rank.Percentile; 
importorg.apache.commons.math3.stat.descriptive.summary.Product; 
importorg.apache.commons.math3.stat.descriptive.summary.Sum; 
importorg.apache.commons.math3.stat.descriptive.summary.SumOfSquares;
 
/**
 * 简单使用commons Math方法
 * @author 范芳铭
 */
public class MathUsage {
     public static void main(String[] args) { 
        double[] values = new double[] { 0.33, 1.33,0.27333, 0.3, 0.501, 
                0.444, 0.44, 0.34496, 0.33,0.3, 0.292, 0.667 }; 
        Min min = new Min(); 
        Max max = new Max(); 
         
        Mean mean = new Mean(); // 算术平均值 
        Product product = new Product();//乘积 
        Sum sum = new Sum(); 
        Variance variance = new Variance();//方差 
        System.out.println("min: " +min.evaluate(values)); 
        System.out.println("max: " +max.evaluate(values)); 
        System.out.println("mean: " +mean.evaluate(values)); 
        System.out.println("product:" + product.evaluate(values)); 
        System.out.println("sum: " +sum.evaluate(values)); 
        System.out.println("variance:" + variance.evaluate(values)); 
 
        Percentile percentile = newPercentile(); // 百分位数 
        GeometricMean geoMean = newGeometricMean(); // 几何平均数,n个正数的连乘积的n次算术根叫做这n个数的几何平均数 
        Skewness skewness = new Skewness(); //Skewness(); 
        Kurtosis kurtosis = new Kurtosis(); //Kurtosis,峰度 
        SumOfSquares sumOfSquares = newSumOfSquares(); // 平方和 
        StandardDeviation StandardDeviation =new StandardDeviation();//标准差 
        System.out.println("80 percentilevalue: " 
                + percentile.evaluate(values,80.0)); 
        System.out.println("geometricmean: " + geoMean.evaluate(values)); 
        System.out.println("skewness:" + skewness.evaluate(values)); 
        System.out.println("kurtosis:" + kurtosis.evaluate(values)); 
        System.out.println("sumOfSquares:" + sumOfSquares.evaluate(values)); 
       System.out.println("StandardDeviation: " +StandardDeviation.evaluate(values)); 
         
       System.out.println("-------------------------------------"); 
        // Create a real matrix with two rowsand three columns 
        double[][] matrixData = { {1d,2d,3d},{2d,5d,3d}}; 
        RealMatrix m = newArray2DRowRealMatrix(matrixData); 
        System.out.println(m); 
        // One more with three rows, twocolumns 
        double[][] matrixData2 = { {1d,2d},{2d,5d}, {1d, 7d}}; 
        RealMatrix n = newArray2DRowRealMatrix(matrixData2);         
        // Note: The constructor copies  the input double[][] array.          
        // Now multiply m by n 
        RealMatrix p = m.multiply(n); 
       System.out.println("p:"+p); 
       System.out.println(p.getRowDimension());    // 2 
       System.out.println(p.getColumnDimension()); // 2          
        // Invert p, using LUdecomposition 
        RealMatrix pInverse = newLUDecomposition(p).getSolver().getInverse(); 
        System.out.println(pInverse); 
    } 
}
运行结果如下:
min: 0.27333
max: 1.33
mean: 0.46269083333333333
product: 2.3429343978460972E-5
sum: 5.552289999999999
variance: 0.08757300031742428
80 percentile value: 0.5674000000000001
geometric mean: 0.4112886050879374
skewness: 2.670095445623868
kurtosis: 7.718241303328169
sumOfSquares: 3.5322966905000004
StandardDeviation: 0.2959273564870681
-------------------------------------
Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0}}
p:Array2DRowRealMatrix{{8.0,33.0},{15.0,50.0}}
2
2
Array2DRowRealMatrix{{-0.5263157895,0.3473684211},{0.1578947368,-0.0842105263}}
 

 

以上是关于apache commons Math库简单实用的主要内容,如果未能解决你的问题,请参考以下文章

Apache Commons Math3学习笔记 - 多项式曲线拟合(转)

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig(示例代

POI操作报错-- java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils 解决

使用 Apache Commons Math 的 Weibull 参数估计

漏洞分析Apache Commons Text远程代码执行漏洞(CVE-2022-42889)

Java实用类库之StringUtils