1

2x2 行列を作成できるコードを作成し、JAMA ライブラリ ( http://math.nist.gov/javanumerics/jama/ ) を使用して、作成したばかりの行列の固有値と固有ベクトルを計算しようとしています。 . 次に、トレース決定形式を使用して、固有値を解析手法と比較します。

私のコードは以下です。最初のブロックは 2x2 行列を生成するためのもので、次にコードの 2 番目のブロックは固有値と固有ベクトルを計算するためのものです。

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; を使用すると、「互換性のない型: 予期しない戻り値」という別のエラーが表示されます

    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 をマトリックスに変換できないというエラーが表示されます。これは、リターンマトリックスがないためだと思いますが、それを修正する方法がわかりません。

どんなアドバイスでも大歓迎です。

編集:

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 を取得します

マトリックスを適切に出力する方法に関するアドバイスはありますか?

4

1 に答える 1

0

の戻り値の型に注意してくださいPopulateMatrix():

public void PopulateMatrix() { ... }

何も返さないと言いますが、voidこれを返そうとするMatrixと、これは予期しない戻り値の型であるというエラー メッセージが表示されます。

Matrixfromを返したい場合は、戻り値の型を次PopulateMatrix()のように変更する必要があります。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)、同じ名前のローカル変数を作成していて、インスタンス フィールドに値を割り当てていません。後でやりたい場合は、戻り値の型をvoid次のように保つことができます。

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

フィールドに直接アクセスします(公開したため):

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

補足として、変数、メソッド、およびクラスに名前を付けるときは、Java の規則に従うようにしてください。

  1. 変数名は小文字で始まるキャメルケースにする必要があります:InputMatrixにする必要がありますInputMatrix
  2. メソッド名は小文字で始まるキャメルケースにする必要があります: PopulateMatrix()should bepopulateMatrix()およびEigenvalueCalc()should be eigenvalueCalc().
  3. クラス名は大文字で始まるキャメルケースにするMatrix_For_Eval_Calc必要がありますMatrixForEvalCalc
于 2015-04-29T00:52:27.487 に答える