javamatrixeigenvectoreigenvaluejama

Java JAMA Incompatible Cannot be Converted Error


I'm trying to write a code that allows me to create a 2x2 matrix, then use the JAMA library (http://math.nist.gov/javanumerics/jama/) to calculate the eigenvalues and eigenvectors of the matrix I just created. Then, I'll compare eigenvalues with the analytical method using the trace-determinate form.

My code is below. The first block is to generate the 2x2 matrix, then the second block of code to compute the eigenvalues and eigenvectors

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);
    }

}

That is for the creation of the matrix. And then I want to use that matrix in the next code. When I use ''return A; '' I get another error saying "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
    }

When I create a matrix, populate with with values, then try to use that in the next bit of code, I get an error about incompatible file types and that Matrix_For_Eval_Calc cannot be converted to a Matrix. I imagine that this is because there is no return matrix, but not sure how to remedy that.

Any advice is greatly appreciated.

EDIT:

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;
    }

}

The second part

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
    }
}

I create a matrix, populate it. Then use the input you suggested of

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

Then I get as an output V = Jama.Matrix@1b213c5

Any advice on how to make it output a matrix properly?


Solution

  • Note the return type of PopulateMatrix():

    public void PopulateMatrix() { ... }
    

    You say it returns nothing, by making it void so when you try to return a Matrix you get an error message saying this is an unexpected return type.

    If you want to return a Matrix from PopulateMatrix(), you should change its return type to 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;
    }
    

    But perhaps this is not exactly what you want. You have already declared an instance field Matrix A. When you do Matrix A = new Matrix(array) you are creating a local variable with the same name, and not assigning a value to the instance field. If you want to do the later, you could keep the return type as void:

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

    And access the field directly (since you made it public):

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

    As a side note, you should try to follow the Java conventions when naming your variables, methods and classes:

    1. Variable names should be camel case starting with lowercase: InputMatrix should be InputMatrix.
    2. Method names should be camel case starting with lowercase: PopulateMatrix() should be populateMatrix() and EigenvalueCalc() should be eigenvalueCalc().
    3. Class names should be camel case starting with uppercase: Matrix_For_Eval_Calc should be MatrixForEvalCalc.