Java常用库总结(程序更新)
来源:互联网 发布:淘宝的收货地址怎么删 编辑:程序博客网 时间:2024/06/06 14:07
由于目前在学习统计机器学习这本书,首先学习研究学习了常用的相似性度量 及 java和python实现计算,详见http://blog.csdn.net/qq_36603091/article/details/78216124
在学习第一感受就是python真是太好了!!!!
而java太繁琐,但是没办法公司项目还是要java 没办法 还是要用java!!!!
在求解马氏距离的时候需要用到矩阵的计算,那刚开始接触了jama
一、jama简介
Jama是一个基本的线性代数java包。包括一个基本的Matrix类和5个矩阵分解类。
Matrix类提供了基本的线性代数数值运算的功能,不同的构造函数可以构造双精度和浮点精度的二维数组,而不同的gets和sets方法可以返回子矩阵和矩阵元素。
官方文档:http://math.nist.gov/javanumerics/jama/doc/
math3简介
math3是Apache下的一款进行数学计算的一款java开源工具。jar包名称为:commons-math3.jar。大家可以通过maven或者其他网站进行下载。
math3是一款非常好用的工具,里面提供了各种运算的方法及类,方便大家调用。其API文档为:http://commons.apache.org/proper/commons-math/javadocs/api-3.2/index.html。可以参照API文档进行学习,也可以在实战中,进行学习。
数组转化为矩阵,矩阵的逆,矩阵的转置,矩阵的乘法,矩阵转为数组等等 非常方便 貌似没发现jama有矩阵转数组的 功能。
package aa;import org.apache.commons.math3.linear.Array2DRowRealMatrix;import org.apache.commons.math3.linear.DecompositionSolver;import org.apache.commons.math3.linear.LUDecomposition;import org.apache.commons.math3.linear.MatrixUtils;import org.apache.commons.math3.linear.RealMatrix;import org.apache.commons.math3.stat.descriptive.moment.Mean;public class Math {@SuppressWarnings("unused")public static void main(String[] args) {//double[]数组转换为矩阵double[] x0={10,2,3};double[] x1={-1,30,1.0};double[] x2={200,200,5000};double[] x3={3000,1000,1000};double[][] x = {{10,2,3},{-1,30,1.0},{200,200,5000}};RealMatrix matrix0 = new Array2DRowRealMatrix(x0);RealMatrix matrix1 = new Array2DRowRealMatrix(x1);RealMatrix matrix2 = new Array2DRowRealMatrix(x2); RealMatrix matrix3= new Array2DRowRealMatrix(x3);RealMatrix matrix= new Array2DRowRealMatrix(x);RealMatrix inverseMatrix =inverseMatrix(matrix);//矩阵求逆System.out.println(inverseMatrix);RealMatrix combinedCol =combinedCol(matrix1,matrix2);System.out.println(combinedCol);RealMatrix combinedRow =combinedRow(matrix1,matrix2);System.out.println(combinedRow);RealMatrix meanRow = meanRow(matrix);RealMatrix mean = mean(matrix);System.out.println(meanRow);System.out.println(mean);//矩阵相乘、获取返回的第一列数据RealMatrix pMatrix = matrix;//matrix和matrix2相乘System.out.println(pMatrix);double[] y = pMatrix.scalarMultiply(1/1).getColumn(0);//获取到第一列 for (int i = 0; i < y.length; i++) { System.out.println(y[i]); }}//矩阵求逆public static RealMatrix inverseMatrix(RealMatrix matrix) { LUDecomposition LUDe = new LUDecomposition(matrix); DecompositionSolver solver = LUDe.getSolver(); RealMatrix result = solver.getInverse(); return result; } //按列合并矩阵[a/b]public static RealMatrix combinedCol(RealMatrix a, RealMatrix b) { int col = a.getColumnDimension() + b.getColumnDimension(); int row = a.getRowDimension(); RealMatrix result = MatrixUtils.createRealMatrix(row, col); int temp = a.getColumnDimension(); for (int i = 0; i < col; i++) { if (i < a.getColumnDimension()) { for (int j = 0; j < row; j++) { result.setEntry(j, i, a.getEntry(j, i)); } } else { for (int j = 0; j < row; j++) { result.setEntry(j, i, b.getEntry(j, i - temp)); } } } return result; } //按行合并矩阵[a,b]public static RealMatrix combinedRow(RealMatrix a, RealMatrix b) { int col = a.getColumnDimension(); int row = a.getRowDimension() + b.getRowDimension(); int temp = a.getRowDimension(); RealMatrix result = MatrixUtils.createRealMatrix(row, col); for (int i = 0; i < row; i++) { if (i < a.getRowDimension()) { for (int j = 0; j < col; j++) { result.setEntry(i, j, a.getEntry(i, j)); } } else { for (int j = 0; j < col; j++) { result.setEntry(i, j, b.getEntry(i - temp, j)); } } } return result; } //返回各列平均值1*colpublic static RealMatrix mean(RealMatrix a) { int col = a.getColumnDimension(); double[] data = new double[col]; for (int i = 0; i < col; i++) { data[i] = new Mean().evaluate(a.getColumn(i)); } RealMatrix result = MatrixUtils.createRowRealMatrix(data); return result; } //返回各行平均值row*1public static RealMatrix meanRow(RealMatrix a) { int row = a.getRowDimension(); double[] data = new double[row]; for (int i = 0; i < row; i++) { data[i] = new Mean().evaluate(a.getRow(i)); } RealMatrix result = MatrixUtils.createRowRealMatrix(data); return result; } //获取指定列的元素public static RealMatrix getColMatrix(RealMatrix absMatrix) { RealMatrix a = absMatrix.getColumnMatrix(1);//此处的1指第二列 return a; } }
阅读全文
0 0
- Java常用库总结(程序更新)
- java常用acm的util总结(面向竞赛)【暂停更新】
- mysql常用总结《持续更新......》
- 常用Linux程序总结
- 黑马程序程序员——java常用类总结
- 【Java常用类库】_国际化程序
- Java常用类库--国际化程序
- 【Java常用类库】_国际化程序
- java常用类库---国际化程序
- 常用Java程序片段
- Java常用程序片段
- 小程序常用功能总结
- Excel 操作常用总结(更新中!!)
- PHP 常用 函数总结 持续更新中...
- ol3常用api总结(持续更新)
- 常用sql技巧总结_持续更新
- 常用排序算法总结。【力求持续更新】
- 常用模板总结(持续更新)
- poj 3264 Balanced Lineup 线段树
- fir滤波器的设计和实现
- 以***结尾
- Linux启动/停止/重启Mysql数据库的方法
- 通过ajax发请求的页面转发问题
- Java常用库总结(程序更新)
- 锦江国际的一道面试题(很简单)
- EXCEL vba在哪里填加控件,控件的事件在哪里,打开页面就赋值
- 基于R-CNN的物体检测(详细版)
- 38、C#:app.config详述
- 简单模拟 Spring 的 ioc
- myeclipse 装 javadoc
- php sftp上传下载
- prototype提交form的一个例子