如何用java解决方程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1431885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Solve Equations with java?
提问by Cong De Peng
I have three equations like the following ones:
我有三个等式,如下所示:
- x + y + z = 100;
- x + y - z = 50;
- x - y - z = 10;
- x + y + z = 100;
- x + y - z = 50;
- x - y - z = 10;
How can I find the values of x, y, and z with Java?
如何使用 Java 找到 x、y 和 z 的值?
String equation1="x+y+z=100;";
String equation2="x+y-z=50;";
String equation3="x-y-z=10;";
int[] SolveEquations(equation1,equation2,equation3) {
// to do
// how to do?
}
Do you have any possible solutions or other common frameworks?
你有任何可能的解决方案或其他通用框架吗?
采纳答案by Xinus
You can use determinant to calculate values of x y and z. Logic can be found out here http://www.intmath.com/Matrices-determinants/1_Determinants.php
您可以使用行列式来计算 xy 和 z 的值。逻辑可以在这里找到http://www.intmath.com/Matrices-determinants/1_Determinants.php
And then you need to implement it in java using 3 dimensional arrays.
然后你需要使用 3 维数组在 java 中实现它。
回答by erikkallen
回答by Liran Orevi
Use Gaussian_eliminationit's incredibly easy, but there are some valuesyou may have hard life calculating.
使用Gaussian_elimination它非常容易,但是有些值您可能很难计算。
回答by duffymo
Since you're writing Java, you can use the JAMApackage to solve this. I'd recommend a good LU decomposition method.
由于您正在编写 Java,您可以使用JAMA包来解决这个问题。我会推荐一个很好的 LU 分解方法。
It's a simple linear algebra problem. You should be able to solve it by hand or using something like Excel pretty easily. Once you have that you can use the solution to test your program.
这是一个简单的线性代数问题。您应该能够很容易地手动或使用 Excel 之类的东西来解决它。一旦你有了它,你就可以使用该解决方案来测试你的程序。
There's no guarantee, of course, that there is a solution. If your matrix is singular, that means there is no intersection of those three lines in 3D space.
当然,不能保证有解决方案。如果您的矩阵是奇异矩阵,则意味着这三条线在 3D 空间中没有交集。
回答by Valentin Rocher
You can also use Commons Math. They have a section of this in their userguide(see 3.4)
您还可以使用Commons Math。他们对此有部分在其userguide(见3.4)
回答by sandi vorace
you can use the java matrix package JAMA. See the full page of this example below here
您可以使用 java 矩阵包JAMA。在此处查看此示例的完整页面
/*
*Solving three variable linear equation system
* 3x + 2y - z = 1 ---> Eqn(1)
* 2x - 2y + 4z = -2 ---> Eqn(2)
* -x + y/2- z = 0 ---> Eqn(3)
*/
import Jama.Matrix;
import java.lang.Math.*;
public class Main {
public Main() {
//Creating Arrays Representing Equations
double[][] lhsArray = {{3, 2, -1}, {2, -2, 4}, {-1, 0.5, -1}};
double[] rhsArray = {1, -2, 0};
//Creating Matrix Objects with arrays
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 3);
//Calculate Solved Matrix
Matrix ans = lhs.solve(rhs);
//Printing Answers
System.out.println("x = " + Math.round(ans.get(0, 0)));
System.out.println("y = " + Math.round(ans.get(1, 0)));
System.out.println("z = " + Math.round(ans.get(2, 0)));
}
public static void main(String[] args) {
new Main();
}
}
/*
*Solving three variable linear equation system
* 3x + 2y - z = 1 ---> Eqn(1)
* 2x - 2y + 4z = -2 ---> Eqn(2)
* -x + y/2- z = 0 ---> Eqn(3)
*/
import Jama.Matrix;
import java.lang.Math.*;
public class Main {
public Main() {
//Creating Arrays Representing Equations
double[][] lhsArray = {{3, 2, -1}, {2, -2, 4}, {-1, 0.5, -1}};
double[] rhsArray = {1, -2, 0};
//Creating Matrix Objects with arrays
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 3);
//Calculate Solved Matrix
Matrix ans = lhs.solve(rhs);
//Printing Answers
System.out.println("x = " + Math.round(ans.get(0, 0)));
System.out.println("y = " + Math.round(ans.get(1, 0)));
System.out.println("z = " + Math.round(ans.get(2, 0)));
}
public static void main(String[] args) {
new Main();
}
}
回答by Keating Lopez
There are many ways to solve linear system equations. There is a simplest way to perform this. In the example the java code solve for two variables USING Matrix method but you can modify to perform 3 variables calculations.
求解线性系统方程的方法有很多种。有一种最简单的方法来执行此操作。在示例中,java 代码使用矩阵方法求解两个变量,但您可以修改以执行 3 个变量计算。
import java.util.Scanner; //OBJETO SCANNER
public class SYS2 {
public static void main (String args[]) {
//VARIABLE DECLARATION SPACE
int i=0,j = 0;
float x,y;
Scanner S = new Scanner (System.in);
int EC3[][]= new int [2][3]; //ARRAY TO STORE EQUATION 2X3
float DET1=0;
float DET2 =0;
float DETA=0;
float DETB=0;
//END VARIABLE DECLARATIONS
System.out.println("Enter Equation System : ");
for (i=0; i< 2; i++) {
for (j=0; j< 3; j++)
EC3[i][j] = S.nextInt();
}
System.out.println("SISTEMA DE ECUACION LINEAL: "); //THIS SENTENCE ONLY PRINT THE CATCHED VALUES OF EQUATION
for (i=0; i< 2; i++) {
for (j=0; j< 3; j++)
System.out.print(EC3[i][j] + " ");
System.out.println();
}
// System.out.print("Determinante A de la Matriz: ");
// System.out.print((EC3[0][2] * EC3[1][1]) - (EC3[0][1]*EC3[1][2]) );
for (i=0;i<2;i++) {
for (j=0; j<2;j++)
DET1= ((EC3[0][2] * EC3[1][1]) -( EC3[0][1]*EC3[1][2]));
}
// System.out.print(DET1 );
// System.out.println();
for (i=0;i<2;i++) {
for (j=0; j<2;j++)
DET2= ((EC3[0][0] * EC3[1][1]) - (EC3[0][1]*EC3[1][0]));
}
// System.out.print("Determinante B de la Matriz: ");
// System.out.println(DET2 );
x = (DET1 / DET2);
System.out.println();
System.out.println("X = " + x);
System.out.print("=======================");
//FIN PARA VALOR DE X
//COMIENZO DE VALOR DE Y
// System.out.print("Determinante A de la Matriz Y: ");
for (i=0;i<2;i++) {
for (j=0; j<2;j++)
DETA= EC3[0][0] * EC3[1][2] - EC3[0][2]*EC3[1][0];
// System.out.print(DETA );
// System.out.println();
}
for (i=0;i<2;i++) {
for (j=0; j<2;j++)
DETB= EC3[0][0] * EC3[1][1] - EC3[0][1]*EC3[1][0];
}
// System.out.print("Determinante B de la Matriz Y: ");
// System.out.println(DETB );
y = DETA / DETB;
System.out.print("=======================");
System.out.println();
System.out.println("Y = " + y);
System.out.print("=======================");
}
}