CAPD DynSys Library  5.2.0
Non-rigorous computations of eigenvalues and eigenvectors

For computation of eigenvalues and eigenvectors we use alglib package (version 1.3) which is now part of the CAPD.

Header file

In file

#include "capd/matrixAlgorithms/capd2alglib.h"

there are functions which translate data types between the CAPD and the alglib.

To use alglib with CAPD you need to include only this file .

How to compute eigenvalues?

To compute eigenvalues of given matrix A

DMatrix A(2,2);
A[0][0] = 2; A[0][1] = 10;
A[1][0] = -10; A[1][1] = 2;

first you need to prepare place for output

DVector rV(2), iV(2); // real and imaginary parts of eigenvalues

Remark: Dimensions of rV and iV must be set properly!

Then simply call

and real and imaginary part of eigenvalues will be placed in rV and iV correspondingly.

For convenient output you can use

std::cout << "\neigenValues = " << capd::alglib::eigenvaluesToString(rV, iV, ", ");

How to compute eigenvectors?

To compute eigenvectors along with eigenvalues first prepare place for output

DVector rV(2), iV(2); // real and imaginary parts of eigenvalues
DMatrix rVec(2,2), iVec(2,2); // real and imaginary parts of eigenvectors

and then call

Example

The following example can be found in the capd/examples/alglibdemo directory.

#include "capd/vectalg/lib.h"
#include "capd/matrixAlgorithms/capd2alglib.h"
#include <iostream>
using namespace capd;
int main(){
{ // 2x2 matrix with complex eigenvalues
DMatrix A(2,2);
A[0][0] = 2; A[0][1] = 10;
A[1][0] = -10; A[1][1] = 2;
// place for an output:
DVector rV(2), iV(2); // real and imaginary parts of eigenvalues
std::cout << "\nmatrix A = \n" << A;
std::cout << "\neigenValues = " << capd::alglib::eigenvaluesToString(rV, iV, ", ");
}
{ //##############################################################
// 2x2 matrix with real eigenvalues
DMatrix A(2,2);
A[0][0] = 5; A[0][1] = 1;
A[1][0] = 3; A[1][1] = 6;
// place for an output:
DVector rV(2), iV(2); // real and imaginary parts of eigenvalues
DMatrix rVec(2,2), iVec(2,2); // real and imaginary parts of eigenvectors
std::cout << "\n======================="
<< "\nmatrix A = \n " << A
<< "\neigenValues = " << alglib::eigenvaluesToString(rV, iV, ", ");
std::cout << "\neigenVectors : " << alglib::eigenvectorsToString(rVec, iVec);
std::cout << "\neigenVectors (i-th column contains vector corresponing to i-th eigenvalue)= \n"
<< rVec << "+ i* " << iVec;
std::cout << "\nCHECK (matrix A in the eigenvectors base - should be diagonal up to rounding errors):\n "
}
//##############################################################
// 3x3 diagonal matrix
double data[] = {1, 0, 0,
0, 3, 0,
0, 0, 7};
// new coordinates base
double base[] = {3, 1, 4,
2, -5, 3,
1, 4, 1};
DMatrix B(3,3, data), P(3,3, base);
// C is a matrix B in new coordinates
// Place for output:
DVector eigReal(3), eigIm(3); // real and imaginary parts of eigenvalues
DMatrix Vreal(3,3), Vim(3,3); // real and imaginary parts of eigenvectors (stored in corresponding columns)
capd::alglib::computeEigenvaluesAndEigenvectors(C, eigReal, eigIm, Vreal, Vim);
std::cout << "\n======================="
<< "\nmatrix C = \n" << C << "\neigenvalues of C = " << alglib::eigenvaluesToString(eigReal, eigIm)
<< "\neigenvectors of C\n" << alglib::eigenvectorsToString(Vreal,Vim)
<< std::endl;
return 0;
}
capd::alglib::eigenvaluesToString
std::string eigenvaluesToString(const VectorType &re, const VectorType &im, std::string separator=", ")
Converts vector of eigenvalues to text (string)
Definition: capd2alglib.h:158
capd::alglib::computeEigenvalues
void computeEigenvalues(const MatrixT &A, VectorT &eigenRealPart, VectorT &eigenImPart)
Function computes Eigenvalues of a general matrix.
Definition: capd2alglib.h:32
capd::matrixAlgorithms::gaussInverseMatrix
MatrixType gaussInverseMatrix(const MatrixType &A)
Definition: floatMatrixAlgorithms.hpp:748
capd::vectalg::Vector
Definition: ColumnVector.h:177
capd::alglib::computeEigenvaluesAndEigenvectors
void computeEigenvaluesAndEigenvectors(const MatrixT &A, VectorT &eigenRealPart, VectorT &eigenImPart, MatrixT &vectorRealPart, MatrixT &vectorImPart)
Computes Eigenvalues and corresponding right Eigenvectors.
Definition: capd2alglib.h:79
capd
Definition: atom.h:31
floatMatrixAlgorithms.hpp
main
#define main()
Definition: krak-lib.h:382
capd::vectalg::Matrix
Definition: ColumnVector.h:174
capd::alglib::eigenvectorsToString
std::string eigenvectorsToString(const MatrixType &re, const MatrixType &im, std::string separator=", ", std::string vectorSeparator=",\n")
Converts eigenvectors stored as columns of matrices to text (string)
Definition: capd2alglib.h:183