JAVA 解三元一次方程组commons-math包
- 始末,最近接到一个租金测算的功能,很是头疼,方程如下:
0.08x + 0.12y + 0.25z = 1000;
x - 8y = 0
x- 8z = 0 - 最开始打算使用Math.randm(),然而解的时间超长
- 咨询度娘,得出可以使用commons-math进行解析
//0.08x + 0.12y + 0.25z = 1000;
// x - 8y -0z = 0
// x- 0y -8z = 0
//建立一个矩阵 传入系数
RealMatrix coefficients =
new Array2DRowRealMatrix(new double[][] { { 0.8, 0.25, 0.24 }, { 1, -8, 0 }, { 1, 0, -8 } },
false);
//创建求解器
DecompositionSolver solver = new LUDecompositionImpl(coefficients).getSolver();
//传入等号后面的值
RealVector constants = new ArrayRealVector(new double[] { 1000,0, 0 }, false);
//计算
RealVector solution = solver.solve(constants);
//参数1 x
System.out.println(solution .getEntry(0));
//参数2 y
System.out.println(solution .getEntry(1));
//参数3 z
System.out.println(solution .getEntry(2));
输出结果
参考链接:http://commons.apache.org/proper/commons-math/userguide/linear.html#a3.2_Real_matrices