# 3.9.5.4 Eigenvalues and Eigenvectors

## Real Symmetric Matrix Eigenproblem(Origin8 SR1)

#include <Origin.h>
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
// Include your own header files here.
#define NMAX1 4
#define TDA1 NMAX1
#define TDV1 NMAX1

void real_symmetric_matrix_eigenproblem()
{
Integer  i, j, n;
double a[NMAX1*TDA1], r[NMAX1], v[NMAX1*TDV1];

n = 4;  // assume that NMAX1 is larger than n
matrix mx = {{0.5,0.0,2.3,-2.6},{0.0,0.5,-1.4,-0.7},{2.3,-1.4,0.5,0.0},{ -2.6,-0.7,0.0,0.5}};

//f02abc(n, (double *)a, (Integer)TDA1, r, (double *)v, (Integer)TDV1,
//NAGERR_DEFAULT);

f02abc(n, (double *)mx, n, r, (double *)v, n,
NAGERR_DEFAULT);

printf("Eigenvalues\n");
for (i=0; i<n; i++)
printf("%9.4f ", r[i]);
printf("\n");

printf("Eigenvectors\n");
for ( i=0; i<n; i++)
{
for (j=0; j<n; j++)
printf("%9.4f ", v[i*n+j]);
printf("\n");
}

}

#include <Origin.h>
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
// Include your own header files here.
#include <OC_nag.h>

#define NMAX2 10
#define TDA2 NMAX2
#define TDV2 NMAX2

void real_symmetric_matrix_eigenproblem_ex2()
{
Integer  i, j, n;
double r[NMAX2], v[NMAX2*TDV2];

// get input matrix data from a matrix layer
MatrixLayer ml = Project.ActiveLayer();
Matrix mData(ml);// Get the active Matrix from a MatrixLayer
n = mData.GetNumRows();  // assume that NMAX is larger than n
ASSERT(mData.GetNumRows()==GetNumCols());

f02abc(n, (double *)mData, n, r, (double *)v, n,
NAGERR_DEFAULT);

printf("Eigenvalues\n");
for (i=0; i<n; i++)
printf("%9.4f ", r[i]);
printf("\n");

printf("Eigenvectors\n");
for ( i=0; i<n; i++)
{
for (j=0; j<n; j++)
printf("%9.4f ", v[i*n+j]);
printf("\n");
}

}

## Real Matrix Eigenproblem(Origin8 SR1)

#include <Origin.h>
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
// Include your own header files here.
#include <OC_nag.h>

void general_real_matrix_eigenproblem()
{
Integer  i, j, n;

// for a general real matrix, its eigenvalues and eigenvector may be complex
// so use complex matrix to store the result
matrix<complex> r, v;
r.SetSize(1,4);
v.SetSize(4,4);
Integer iter[4];
matrix a = {{1.5,0.1,4.5,-1.5},{-22.5,3.5,12.5,-2.5},{-2.5,0.3,4.5,-2.5},{-2.5,0.1,4.5,2.5}};
n = 4;

f02agc(n, a, n, r, v,
n, iter, NAGERR_DEFAULT);

printf("Eigenvalues\n");
for (i=0; i<n; i++)
{
complex temp = r[0][i];
printf("(%7.3f, %7.3f) \n", temp.m_re, temp.m_im);
}

printf("\nEigenvectors\n");
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
complex temp = v[i][j];
printf("(%7.3f, %7.3f)", temp.m_re, temp.m_im);
if(j%4==3 || j==n-1)
printf("\n");
}
}