apache commons math 示例代码

     apache commons Math是一组偏向科学计算为主的函数,主要是针对线性代数,数学分析,概率和统计等方面。

     我虽然是数学专业毕业,当年也是抱着《数学分析》啃,但是好久不用,这些概念都开始生疏,写一点例子,仅作参考。

 

  1 packagetest.ffm83.commons.math;
  2 
  3 importorg.apache.commons.math3.linear.Array2DRowRealMatrix; 
  4 
  5 import org.apache.commons.math3.linear.LUDecomposition; 
  6 
  7 importorg.apache.commons.math3.linear.RealMatrix; 
  8 
  9 importorg.apache.commons.math3.stat.descriptive.moment.GeometricMean; 
 10 
 11 importorg.apache.commons.math3.stat.descriptive.moment.Kurtosis; 
 12 
 13 importorg.apache.commons.math3.stat.descriptive.moment.Mean; 
 14 
 15 importorg.apache.commons.math3.stat.descriptive.moment.Skewness; 
 16 
 17 importorg.apache.commons.math3.stat.descriptive.moment.StandardDeviation; 
 18 
 19 importorg.apache.commons.math3.stat.descriptive.moment.Variance; 
 20 
 21 import org.apache.commons.math3.stat.descriptive.rank.Max; 
 22 
 23 importorg.apache.commons.math3.stat.descriptive.rank.Min; 
 24 
 25 importorg.apache.commons.math3.stat.descriptive.rank.Percentile; 
 26 
 27 importorg.apache.commons.math3.stat.descriptive.summary.Product; 
 28 
 29 importorg.apache.commons.math3.stat.descriptive.summary.Sum; 
 30 
 31 importorg.apache.commons.math3.stat.descriptive.summary.SumOfSquares;
 32 
 33  
 34 
 35 /**
 36 
 37  * 简单使用commons Math方法
 38 
 39  * @author 范芳铭
 40 
 41  */
 42 
 43 public class MathUsage {
 44 
 45      public static void main(String[] args) { 
 46 
 47         double[] values = new double[] { 0.33, 1.33,0.27333, 0.3, 0.501, 
 48 
 49                 0.444, 0.44, 0.34496, 0.33,0.3, 0.292, 0.667 }; 
 50 
 51         Min min = new Min(); 
 52 
 53         Max max = new Max(); 
 54 
 55          
 56 
 57         Mean mean = new Mean(); // 算术平均值 
 58 
 59         Product product = new Product();//乘积 
 60 
 61         Sum sum = new Sum(); 
 62 
 63         Variance variance = new Variance();//方差 
 64 
 65         System.out.println("min: " +min.evaluate(values)); 
 66 
 67         System.out.println("max: " +max.evaluate(values)); 
 68 
 69         System.out.println("mean: " +mean.evaluate(values)); 
 70 
 71         System.out.println("product:" + product.evaluate(values)); 
 72 
 73         System.out.println("sum: " +sum.evaluate(values)); 
 74 
 75         System.out.println("variance:" + variance.evaluate(values)); 
 76 
 77  
 78 
 79         Percentile percentile = newPercentile(); // 百分位数 
 80 
 81         GeometricMean geoMean = newGeometricMean(); // 几何平均数,n个正数的连乘积的n次算术根叫做这n个数的几何平均数 
 82 
 83         Skewness skewness = new Skewness(); //Skewness(); 
 84 
 85         Kurtosis kurtosis = new Kurtosis(); //Kurtosis,峰度 
 86 
 87         SumOfSquares sumOfSquares = newSumOfSquares(); // 平方和 
 88 
 89         StandardDeviation StandardDeviation =new StandardDeviation();//标准差 
 90 
 91         System.out.println("80 percentilevalue: " 
 92 
 93                 + percentile.evaluate(values,80.0)); 
 94 
 95         System.out.println("geometricmean: " + geoMean.evaluate(values)); 
 96 
 97         System.out.println("skewness:" + skewness.evaluate(values)); 
 98 
 99         System.out.println("kurtosis:" + kurtosis.evaluate(values)); 
100 
101         System.out.println("sumOfSquares:" + sumOfSquares.evaluate(values)); 
102 
103        System.out.println("StandardDeviation: " +StandardDeviation.evaluate(values)); 
104 
105          
106 
107        System.out.println("-------------------------------------"); 
108 
109         // Create a real matrix with two rowsand three columns 
110 
111         double[][] matrixData = { {1d,2d,3d},{2d,5d,3d}}; 
112 
113         RealMatrix m = newArray2DRowRealMatrix(matrixData); 
114 
115         System.out.println(m); 
116 
117         // One more with three rows, twocolumns 
118 
119         double[][] matrixData2 = { {1d,2d},{2d,5d}, {1d, 7d}}; 
120 
121         RealMatrix n = newArray2DRowRealMatrix(matrixData2);         
122 
123         // Note: The constructor copies  the input double[][] array.          
124 
125         // Now multiply m by n 
126 
127         RealMatrix p = m.multiply(n); 
128 
129        System.out.println("p:"+p); 
130 
131        System.out.println(p.getRowDimension());    // 2 
132 
133        System.out.println(p.getColumnDimension()); // 2          
134 
135         // Invert p, using LUdecomposition 
136 
137         RealMatrix pInverse = newLUDecomposition(p).getSolver().getInverse(); 
138 
139         System.out.println(pInverse); 
140 
141     } 
142 
143 }
144 
145 运行结果如下:
146 
147 min: 0.27333
148 
149 max: 1.33
150 
151 mean: 0.46269083333333333
152 
153 product: 2.3429343978460972E-5
154 
155 sum: 5.552289999999999
156 
157 variance: 0.08757300031742428
158 
159 80 percentile value: 0.5674000000000001
160 
161 geometric mean: 0.4112886050879374
162 
163 skewness: 2.670095445623868
164 
165 kurtosis: 7.718241303328169
166 
167 sumOfSquares: 3.5322966905000004
168 
169 StandardDeviation: 0.2959273564870681
170 
171 -------------------------------------
172 
173 Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0}}
174 
175 p:Array2DRowRealMatrix{{8.0,33.0},{15.0,50.0}}
176 
177 2
178 
179 2
180 
181 Array2DRowRealMatrix{{-0.5263157895,0.3473684211},{0.1578947368,-0.0842105263}}

 

posted @ 2017-01-04 17:12  LT-blogs  阅读(4615)  评论(0编辑  收藏  举报