关于 Commons-math 中的矩阵算法小结

对于大的数据量,转换成矩阵或者行列式进行计算会将运算效率提高。

所用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]);;
}












  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值