我试图用Jama来求解一个4x4线性方程组(4个变量,4个方程)。我尝试过以下代码,但它不起作用。如果有人能帮助我,使用Jama或其他任何方法,我将不胜感激。
import Jama.Matrix;
public class OvaWork {
public OvaWork()
{
//Creating Arrays Representing Equations
double[][] lhsArray = {{-3, 1, -1}, {5, -2, 1}, {-1, 1, 3}, {2, 5, 7}};
double[] rhsArray = {-4, 6, 0, 8};
//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("w = " + Math.round(ans.get(0, 0)));
System.out.println("x = " + Math.round(ans.get(1, 0)));
System.out.println("y = " + Math.round(ans.get(2, 0)));
System.out.println("z = " + Math.round(ans.get(3, 0)));
}
public static void main(String[] args)
{
OvaWork o = new OvaWork();
}
}发布于 2016-04-28 01:23:24
您必须尝试使用更简单的例子,如2x2方程。
double[][] lhsArray = {{1,1},{2, 0}};
double[] rhsArray = {10,2};
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 2);
Matrix ans = lhs.solve(rhs);它工作,输出是一个矩阵{1,9}
代码的问题是矩阵不是正方形的,而是3x4。
double[][] lhsArray = {{-3, 1, -1}, {5, -2, 1}, {-1, 1, 3}, {2, 5, 7}};把你的矩阵转换成正方形。
测试这个琐碎的等式:
double[][] lhsArray = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
double[] rhsArray = {1, 2, 3, 4};
Matrix lhs = new Matrix(lhsArray);
Matrix rhs = new Matrix(rhsArray, 4);
Matrix ans = lhs.solve(rhs);ans为{1,2,3,4}。
https://stackoverflow.com/questions/36898630
复制相似问题