Java JAMA 不兼容无法转换错误

Java JAMA Incompatible Cannot be Converted Error

我正在尝试编写允许我创建 2x2 矩阵的代码,然后使用 JAMA 库 (http://math.nist.gov/javanumerics/jama/) 计算我刚创建的矩阵的特征值和特征向量。然后,我将使用迹定形式将特征值与​​分析方法进行比较。

我的代码如下。第一个块是生成 2x2 矩阵,然后是第二个代码块来计算特征值和特征向量

import Jama.Matrix;
import Jama.EigenvalueDecomposition;
import Jama.*;
import java.util.Scanner;

/**
 * Code to generate a 2x2 matrix then find its eigenvalues and eigenvectors
 * Check eigenvalue computation using trick for 2x2 case 
 * ^(only possible for 2x2, not in general possible for general nxn)
 */
public class Matrix_For_Eval_Calc
{
    // instance variables - replace the example below with your own
    public Matrix A;
    // Create empty 2x2 array

    /**
     * Constructor for objects of class EigenvalueProblem
     * Input elements in array
     * Fill in elements of 2x2 matrix
     */
     public void PopulateMatrix()
    {
        // initialise instance variables
        // Prompt User Input for a_1,1 a_1,2 a_2,1 and a_2,2
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the element a_{1,1}: ");
        double a_11 = in.nextInt();
        System.out.println("a_{1,1} = " + a_11 );

        System.out.println("Enter the element a_{1,2}: ");
        double a_12 = in.nextInt();
        System.out.println("a_{1,2} = " + a_12 );

        System.out.println("Enter the element a_{2,1}: ");
        double a_21 = in.nextInt();
        System.out.println("a_{2,1} = " + a_21 );

        System.out.println("Enter the element a_{2,2}: ");
        double a_22 = in.nextInt();
        System.out.println("a_{2,2} = " + a_22 );

        double[][] array = { {a_11 , a_12} , {a_21 , a_22} };
        Matrix A = new Matrix(array); 
        // System.out.println(A);
        // System.out.println(a_11 + "," + a_12);
        // System.out.println(a_21 + "," + a_22);
    }

}

那是为了创建矩阵。然后我想在下一个代码中使用该矩阵。当我使用“'return A; '”时,我收到另一个错误提示 "incompatible types: unexpected return value"

    import Jama.Matrix;
import Jama.EigenvalueDecomposition;
import Jama.*;
import java.util.Scanner;

/**
 * Write a description of class EvalCalculation here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class EvalCalculation
{
    // instance variables - replace the example below with your own
    //private int x;

    public void EigenvalueCalc(Matrix InputMatrix) 
    {
        EigenvalueDecomposition somematrix = new EigenvalueDecomposition(InputMatrix);
        Matrix S = somematrix.getV();
        System.out.println("V = " + S);
        // Compute Evals and e-vecs
        // Print out
    }

当我创建一个矩阵,用值填充,然后尝试在下一段代码中使用它时,我收到关于不兼容文件类型的错误,并且 Matrix_For_Eval_Calc 无法转换为矩阵。我想这是因为没有 return 矩阵,但不确定如何补救。

非常感谢任何建议。

编辑:

import Jama.Matrix;
import Jama.EigenvalueDecomposition;
import Jama.*;
import java.util.Scanner;

/**
 * Code to generate a 2x2 matrix then find its eigenvalues and eigenvectors
 * Check eigenvalue computation using trick for 2x2 case 
 * ^(only possible for 2x2, not in general possible for general nxn)
 */
public class MatrixForEvalCalc
{
    // instance variables - replace the example below with your own
    public Matrix A;
    // Create empty 2x2 array

    /**
     * Constructor for objects of class EigenvalueProblem
     * Input elements in array
     * Fill in elements of 2x2 matrix
     */
     public void populateMatrix()
    {
        // initialise instance variables
        // Prompt User Input for a_1,1 a_1,2 a_2,1 and a_2,2
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the element a_{1,1}: ");
        double a_11 = in.nextInt();
        System.out.println("a_{1,1} = " + a_11 );

        System.out.println("Enter the element a_{1,2}: ");
        double a_12 = in.nextInt();
        System.out.println("a_{1,2} = " + a_12 );

        System.out.println("Enter the element a_{2,1}: ");
        double a_21 = in.nextInt();
        System.out.println("a_{2,1} = " + a_21 );

        System.out.println("Enter the element a_{2,2}: ");
        double a_22 = in.nextInt();
        System.out.println("a_{2,2} = " + a_22 );

        double[][] array = { {a_11 , a_12} , {a_21 , a_22} };
        this.A = new Matrix(array); 
        // return A;
    }

}

第二部分

import Jama.Matrix;
import Jama.EigenvalueDecomposition;
import Jama.*;
import java.util.Scanner;

/**
 * Write a description of class EvalCalculation here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class EvalCalculation
{
    // instance variables - replace the example below with your own
    //private int x;

    public void eigenvalueCalc(Matrix inputMatrix) 
    {
        EigenvalueDecomposition someMatrix = new EigenvalueDecomposition(inputMatrix);
        Matrix S = someMatrix.getV();
        System.out.println("V = " + S);
        // Compute Evals and e-vecs
        // Print out
    }
}

我创建一个矩阵,填充它。然后使用您建议的

输入
MatrixForEvalCalc matrixWrapper = new MatrixForEvalCalc();
matrixWrapper.PopulateMatrix();
EigenvalueCalc(matrixWrapper.A);

然后我得到一个输出 V = Jama.Matrix@1b213c5

关于如何使其正确输出矩阵有什么建议吗?

注意 PopulateMatrix() 的 return 类型:

public void PopulateMatrix() { ... }

你说它 return 什么都不是,通过使它成为 void 所以当你尝试 return 一个 Matrix 你会收到一条错误消息,说这是一个意外return类型。

如果你想从PopulateMatrix()return一个Matrix,你应该将其return类型更改为Matrix:

public Matrix PopulateMatrix() {
    // rest of the code
    double[][] array = { {a_11 , a_12} , {a_21 , a_22} };
    Matrix A = new Matrix(array);
    return A;
}

但这也许不是您想要的。您已经声明了一个实例字段 Matrix A。当您执行 Matrix A = new Matrix(array) 时,您正在创建一个具有相同名称的局部变量,而不是为实例字段赋值。如果你想稍后做,你可以将 return 类型保留为 void:

public void PopulateMatrix() {
    // rest of the code
    double[][] array = { {a_11 , a_12} , {a_21 , a_22} };
    this.A = new Matrix(array);
}

并直接访问该字段(因为你创建了它 public):

Matrix_For_Eval_Calc matrixWrapper = new Matrix_For_Eval_Calc();
matrixWrapper.PopulateMatrix();
EigenvalueCalc(matrixWrapper.A);

附带说明一下,在命名变量、方法和 类:

时,您应该尽量遵循 Java 约定
  1. 变量名应以小写字母开头的驼峰式命名:InputMatrix 应为 InputMatrix.
  2. 方法名称应为小写字母开头的驼峰式命名法:PopulateMatrix() 应为 populateMatrix()EigenvalueCalc() 应为 eigenvalueCalc().
  3. Class 名称应为大写字母开头的驼峰式:Matrix_For_Eval_Calc 应为 MatrixForEvalCalc.