Eigen matrix operations example

Example 1: matrix eigen c++ example

#include <iostream>
#include <Eigen/Dense>
 
using namespace Eigen;
 
int main()
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << "Here is the matrix m:\n" << m << std::endl;
  VectorXd v(2);
  v(0) = 4;
  v(1) = v(0) - 1;
  std::cout << "Here is the vector v:\n" << v << std::endl;
}

Example 2: eigenvalue of matrix c++ using Eigen

#include <eigen3/Eigen/Eigenvalues> // header file
  #include <iostream>

  int main(){
  Eigen::Matrix<double, 2, 2> A; // declare a real (double) 2x2 matrix
  A << 0, 2, 1, 0; // defined the matrix A

  Eigen::EigenSolver<Eigen::Matrix<double, 2,2> > s(A); // the instance s(A) includes the eigensystem
  std::cout << A << std::endl;
  std::cout << "eigenvalues:" << std::endl;
  std::cout << s.eigenvalues()(0) << std::endl;
  std::cout << "eigenvectors=" << std::endl;
  std::cout << s.eigenvectors() << std::endl;

  return(0);
}

Tags:

Cpp Example