关于 Commons-math 中的矩阵算法小结
2017-04-27 11:21
399 查看
对于大的数据量,转换成矩阵或者行列式进行计算会将运算效率提高。
所用jar包:Commons-math3-3.5.jar
相关API:http://commons.apache.org/proper/commons-math/javadocs/api-3.3/
1.double[]数组转换为矩阵
double[] martxData = {1d,2d,3d};
RealMatrix matrix = new Array2DRowRealMatrix(martxData);2.矩阵求逆
public static RealMatrix inverseMatrix(RealMatrix matrix) {
LUDecomposition LUDe = new LUDecomposition(matrix);
DecompositionSolver solver = LUDe.getSolver();
RealMatrix result = solver.getInverse();
return result;
}3.按列合并矩阵[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;
}4.按行合并矩阵[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;
}5.返回各列平均值1*col
public 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;
}6.返回各行平均值row*1
public static RealMatrix getColMatrix(RealMatrix absMatrix) {
RealMatrix a = absMatrix.getColumnMatrix(1);//此处的1指第二列
return a;
}8.矩阵相乘、获取返回的第一列数据
RealMatrix pMatrix = matrix.multiply(matrix2);//matrix和matrix2相乘
double[] y = sMatrix.scalarMultiply(1/1).getColumn(0);//获取到第一列
for (int i = 0; i < y.length; i++) {
System.out.println(y[i]);;
}
所用jar包:Commons-math3-3.5.jar
相关API:http://commons.apache.org/proper/commons-math/javadocs/api-3.3/
1.double[]数组转换为矩阵
double[] martxData = {1d,2d,3d};
RealMatrix matrix = new Array2DRowRealMatrix(martxData);2.矩阵求逆
public static RealMatrix inverseMatrix(RealMatrix matrix) {
LUDecomposition LUDe = new LUDecomposition(matrix);
DecompositionSolver solver = LUDe.getSolver();
RealMatrix result = solver.getInverse();
return result;
}3.按列合并矩阵[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;
}4.按行合并矩阵[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;
}5.返回各列平均值1*col
public 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;
}6.返回各行平均值row*1
public 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; }7.获取指定列的元素
public static RealMatrix getColMatrix(RealMatrix absMatrix) {
RealMatrix a = absMatrix.getColumnMatrix(1);//此处的1指第二列
return a;
}8.矩阵相乘、获取返回的第一列数据
RealMatrix pMatrix = matrix.multiply(matrix2);//matrix和matrix2相乘
double[] y = sMatrix.scalarMultiply(1/1).getColumn(0);//获取到第一列
for (int i = 0; i < y.length; i++) {
System.out.println(y[i]);;
}
相关文章推荐
- 关于Floyd-Warshall算法由前趋矩阵计算出的最短路径反映出了算法的执行过程特性的证明
- 稀疏矩阵的三元组顺序表存储及矩阵相乘算法小结
- 关于矩阵旋转算法的实现
- 面试小结三:关于算法数据结构的面试题整理(待续)
- 稀疏矩阵的三元组顺序表存储及矩阵相乘算法小结
- 17.7.3小结、【矩阵快速幂】2017武大校赛I题:A simple math problem即nyoj2333、 bzoj2326
- 稀疏矩阵的三元组顺序表存储及矩阵相乘算法小结
- 算法小结(Math)
- 有序矩阵查找 -- 算法小结
- 关于矩阵求和的常见算法//题目:n阶幻方矩阵验证
- 关于矩阵N次方的一个比较快速算法
- 关于有序二维矩阵查找和字符串替换的两道算法题
- 关于印象笔记的扫描宝——简单的图像背景去除(去底色)算法小结
- 关于JavaScript闭包/匿名函数,以及垃圾回收算法的小结
- 关于“拜占庭将军算法”byzantine generals problem
- 简单常识——关于 STL 算法
- 关于使用commons-betwixt组件实现xml信息-->Java Bean的转化的一些感受
- 关于使用ODBC+VC操作DBF文件问题小结
- 关于快速排序和插入排序最坏时间复杂度为O(nlogn)的算法
- 关于分解质因数的一个算法