0

I'm trying to compute eigenvalue for matrix[7][7], and my code is:

for (i = 0; i<M; i++)               //matrix + identity
      {
        for (j = 0; j<M; j++)
        {

            MI[i][j] = MX[i][j] + a[i][j];
            cout << setw(7) << MI[i][j] << " ";

            MatrixXd W;
            W = MI[i][j];
            SelfAdjointEigenSolver <MatrixXd> eigensolver (W);
            cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << endl;
        }
        cout << endl;

    }

My question is am I doing it right calling my matrix by define as matrixXd W?

I end up with an error in line W = MI[i][j]:

no operand "=" matches this operand & oeprand types are :MatrixXd=double

Cristik
  • 30,989
  • 25
  • 91
  • 127
h26
  • 1
  • 1

1 Answers1

0

I guess, you want to use Eigen library to compute the eigenvalues of the matrix. It is not quite clear from your code, but there are certain things that should be fixed right away.

  1. MatrixXd W is ok. Though you do not specify the dimensions. I guess, you can resize it to and M x M.

    MatrixXd W
    W.resize(M,M)
    
  2. Your line W=MI[i][J] is very strange. I guess, you mean

    W(i,j)=MI[i][j]
    
  3. It is unclear, if you really want to calculate eigenvalues for M^2 different matrices. With egiensolver and matrix declaration inside of both two for-loops - that's exactly you what you are doing. If that's not your intentions (which is very probable), consider moving declaration of the matrix before the for-loops. Fill the matrix using your loops. And call the eigensolver after both for loops. That will calculate the eigenvalues for one matrix.

  4. Also, you could have started converting all of your code to use Eigen library based matrices. (if the codebase is not too large).

Eigen website has plenty of examples showing how to do basic operations. These two, should cover your needs.

http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html http://eigen.tuxfamily.org/dox/classEigen_1_1EigenSolver.html

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
  • Thank you Anton for your quick reply, no 3 give sense because i,m looking for eigenvalue,if i have matrix 3x3, then my eigenvalue should be 3 and no need for loop,am i right? – h26 Jun 15 '16 at 09:11
  • I think you either misinterpret my statements or the definition of the eigenvalue. Please, take a look at what eigenvalue of a matrix mean. A 3 x 3 matrix has 3 eigenvalues (counting multiplicities). But using Eigen library, you can find them without doing 3*3=9 eigenvalue decompositions. – Anton Menshov Jun 15 '16 at 14:53