Java计算方程式

时间:2016-04-29 10:02:42

标签: java math

任何人都可以告诉我如何计算这个方程中的X和Y值

3X -3 = 6

和这个

X + 5Y = something <-- This is just an example 
2X - 4Y = something <-- This is just an example 

使用Java?

1 个答案:

答案 0 :(得分:1)

您可以使用库Commons Math

public static void main (final String args[])
{
    //Your equation (x and y)
    RealMatrix coefficients = new Array2DRowRealMatrix(new double[][] { { 1,5 }, {2,-4} }, false);
    DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();

    //Result of equation
    RealVector constants = new ArrayRealVector(new double[] { 27,-21 }, false);
    RealVector solution = solver.solve(constants);

    System.out.println("X = " + solution.getEntry(0));
    System.out.println("Y = " + solution.getEntry(1));
}