JAVA 解三或二元一次方程组commons-math包 麻瓜教程

JAVA 解三元一次方程组commons-math包

  1. 始末,最近接到一个租金测算的功能,很是头疼,方程如下:
    0.08x + 0.12y + 0.25z = 1000;
    x - 8y = 0
    x- 8z = 0
  2. 最开始打算使用Math.randm(),然而解的时间超长
  3. 咨询度娘,得出可以使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值