2016-11-18 78 views
0

我想用解决方法,但它给了我一个错误,任何人都可以请帮我一下如何编写解决方法?我试着运行这个代码,解决方法之前的代码工作正常,但之后,它给了我一个错误。谢谢你这么多矩阵求解方法java

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

什么是错误,什么是解决方法?你在哪里得到矩阵什么=新的矩阵()?你有没有建立自己的矩阵课程?或者你在使用JAMA吗? –

+0

用于Java矩阵类的JAMA包。可以为Java矩阵添加库。 –

+0

非静态方法求解(矩阵)不能从静态上下文中引用 –

回答

0

您没有使用JAMA你使用:http://introcs.cs.princeton.edu/java/95linear/Matrix.java.html arn't吗?这将有助于下次。

要回答你的问题,虽然什么是'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不与矩阵工作,你需要,如果你想使用另一种方法^ 2是一个完整的矩阵。这里是:https://stackoverflow.com/a/22901024/4329778

+0

欣赏它,但它仍然给我一个错误“不兼容的类型:矩阵不能转换为双倍”,你在你的答案中描述。但我只需要一个驱动程序类的方法作为我的任务的一部分 –