4x4 線形方程式の解を与えるこのコードがあります。一次方程式に解がないか、複数の解がある場合、どのように出力できますか? エラーを出力する代わりに?
public class OvaWork
{
void fourthEquationSolver()
{
//Creating Arrays Representing Equations
double[][] lhsArray = {{8,1,10,1}, {2,1,5,4}, {1,5,3,2}, {9,8,4,6}};
double[] rhsArray = {14,22,38,44};
//Creating Matrix Objects with arrays
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 4);
//Calculate Solved Matrix
Matrix ans = lhs.solve(rhs);
//Printing Answers
System.out.println("x1 = " + (ans.get(0, 0)));
System.out.println("x2 = " + (ans.get(1, 0)));
System.out.println("X3 = " + (ans.get(2, 0)));
System.out.println("X4 = " + (ans.get(3, 0)));
}
public static void main(String[] args)
{
OvaWork equation = new OvaWork();
}
}
このコードに次のようなマトリックスを書き込むと:
1,1,1,1=14
2,2,2,2=22
3,3,3,3=38
4,4,4,4=44
このコードは次を出力します:
Exception in thread "main" java.lang.RuntimeException: Matrix is singular.
at Jama.LUDecomposition.solve(LUDecomposition.java:282)
at Jama.Matrix.solve(Matrix.java:815)
at OvaWork.fourthEquationSolver(OvaWork.java:20)
at OvaWork.main(OvaWork.java:106)
上記の行列には複数の解があるか、解がないため