2016-11-18 97 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

欣賞它,但它仍然給我一個錯誤「不兼容的類型:矩陣不能轉換爲雙倍」,你在你的答案中描述。但我只需要一個驅動程序類的方法作爲我的任務的一部分 –