我正在尝试使用solve方法,但它给了我一个错误,有人可以帮我解决如何编写求解方法吗?我尝试运行此代码,解决方法之前的代码工作正常,但在那之后它给了我一个错误。非常感谢你
import java.lang.Math;
public class MatrixDriver {
public static void main(String[] args) {
double[][] d = { { 1, 2, 3 }, { 4, 5, 6 }, { 9, 1, 3} };
System.out.println("Matrix D - Testing Constructor that take double integer array as a parameter");
Matrix D = new Matrix(d);
System.out.println(D);
System.out.println();
System.out.println("Matrix A - Testing Named Constructor -- random\nTakes two parameters (row, col) and returns a \"new\" Matrix");
Matrix A = Matrix.random(5, 5);
System.out.println(A);
System.out.println();
System.out.println("Matrix A - Testing the swap rows method");
A.swapRows(1,2);
System.out.println(A);
System.out.println();
System.out.println("Matrix B - Testing the transpose method on Matrix A");
Matrix B = A.transpose();
System.out.println(B);
System.out.println();
System.out.println("Matrix C - Testing named constructor - identity - which takes a single parameter (i) and returns a 'new' (i x i) Identity Matrx");
Matrix C = Matrix.identity(5);
System.out.println(C);
System.out.println();
System.out.println("Matrix E - Testing matrix addition (A + B)");
Matrix E = A.plus(B);
System.out.println(E);
System.out.println();
System.out.println("Matrix F - Testing matrix subtraction (A - B)");
Matrix F = A.minus(B);
System.out.println(F);
System.out.println();
System.out.println("Matrix G - Testing matrix scalar multiplication (c * A), c = 1.2");
double c = 1.2;
Matrix G = A.times(c);
System.out.println(G);
System.out.println();
System.out.println("Matrix AB - Testing matrix multiplication (A X B)");
Matrix AB = A.times(B);
System.out.println(AB);
System.out.println();
System.out.println("Matrix BA - Testing matrix multiplication (B X A)");
Matrix BA = B.times(A);
System.out.println(BA);
System.out.println();
System.out.println("Matrices AB and BA - Testing Matrix Equality (AB equals BA)");
try
{
System.out.println(" " + AB.equals(BA));
System.out.println();
}
catch(RuntimeException e)
{
System.out.println(e.getMessage());
System.out.println();
}
System.out.println("Matrix b - Creating a random (5 X 1)");
Matrix b = Matrix.random(5,1);
System.out.println(b);
System.out.println();
System.out.println("Matrix X - Testing the solve method (X = A^-1 * b)");
Matrix L= Matrix.solve(X);
double X = Math.pow(A,-1) * b;
//b.solve(b);
System.out.println(b);
System.out.println();
System.out.println("Matrix d - Testing the sove again (A x X) = b ... is it?");
Matrix b = Matrix.solve();
System.out.println(b);
System.out.println();
}
}
答案 0 :(得分:0)
你没有使用JAMA,你正在使用:http://introcs.cs.princeton.edu/java/95linear/Matrix.java.html你呢?这将是下次有用的。
回答你的问题虽然是什么' x' Matrix.solve的方法并不知道' X'是。
Matrix L= Matrix.solve(X);
double X = Math.pow(A,-1) * b;
应该是:
double X = Math.pow(A,-1) * b;
Matrix L= Matrix.solve(X);
但是即使这样,你仍然会有错误,因为Math.pow不适用于Matrix,如果你想要^ 2整个矩阵,你需要使用另一种方法。这是:https://stackoverflow.com/a/22901024/4329778
答案 1 :(得分:0)
我可以建议这个post。我认为JAMA不再更新,org.apache.commons.math3.linear似乎是当前最先进的矩阵库。在Android Studio中,您可以添加Gradle:
实现'org.apache.commons:commons-math3:3.6.1'