# Calculate Eigenvalues & Vectors using matrices

Page 1 of 1

## 2 Replies - 20860 Views - Last Post: 18 December 2006 - 07:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=21877&amp;s=5b0aec2756ed6e1e1d6e0b0440084874&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TJP

Reputation: 0
• Posts: 2
• Joined: 10-December 06

# Calculate Eigenvalues & Vectors using matrices

Posted 10 December 2006 - 04:02 AM

/*Hi all, I been having a few problems with this next part. The idea is to make two extra functions. one, that works out the dominant eigenvalue
and the corresponding eigenvector with a prescribed numerical error. I understand how, but i just cant do it.

I have made an algorithm for the prescribed numerical error that may be helpful.

Power Method(A 2 <n×n)
Select x 2 <n(6= 0)
 0.0001
errorInLambda 1
k 1
while errorInLamda >= 
do
calc
1 Ak+1x·Akx // top / bottom
Akx·Akx
errorInLambda sqr root Ak+1x·Ak+1x
Akx·Akx − (calc1 )2
k k + 1
output (1,Akx)as the eigenpair

And finally , two, write a function deflate() that computes all of the eigenvalues and eigenvectors of a matrix. Usethis function to compute the eigenvalues and eigenvectors of the covariance matrix i have already done.

any help will be greatly appreciated, I have done a bit but I am confessed and lost now.

Many Thanks

TJP
*/
```#include <iostream>
#include <vector>

// a class which holds matrices of an arbitrary size
class matrix {
private:
int sizex,sizey;
std::vector< std::vector<double> > thenumbers;
public:
matrix(); // default constructor
matrix(int y_size, int x_size); // more useful constructor
matrix(matrix& a_matrix); // copy constuctor
inline double& operator() (int i, int j) { return thenumbers[i][j]; } // indexing functionality
void print(); // printing the matrix
friend matrix transpose(matrix& op1); // transposition
friend bool operator== (matrix& op1, matrix& op2); // equality
inline friend bool operator!= (matrix& op1, matrix& op2) { return (!(op1==op2)); } // inequality
friend matrix operator+ (matrix& op1, matrix& op2); // addition
friend matrix operator- (matrix& op1, matrix& op2); // subtraction
friend matrix operator* (matrix& op1, matrix& op2); // multiplication of matrices
friend matrix operator* (double op1, matrix& op2); // multiplication by a number on the left
friend matrix operator* (matrix& op1, double op2); // multiplication by a number on the right

void set(int i, int j, double value) { thenumbers[i][j] = value; };
//double average (double r[], double n);
//double convar ( double x[], double y[], double meanx, double meany, double n);

};

matrix::matrix() {
sizex=0; sizey=0;
}

matrix::matrix(int y_size, int x_size)
: thenumbers(y_size,std::vector<double>(x_size,0.0)) { // calling constructors for the vectors
sizex=x_size; sizey=y_size;
}

matrix::matrix(matrix& a_matrix) {
sizex=a_matrix.sizex; sizey=a_matrix.sizey;
thenumbers=a_matrix.thenumbers;
}

void matrix::print() {
for (int i=0; i<sizey; i++) {
for (int j=0; j<sizex; j++) {
std::cout << ' ' << thenumbers[i][j];

}
std::cout << std::endl;
}
}

matrix transpose(matrix& op1) {
matrix result=matrix(op1.sizex,op1.sizey);
for (int i=0; i<op1.sizey; i++) {
for (int j=0; j<op1.sizex; j++) {
result(j,i)=op1(i,j);
}
}
return result;
}

bool operator== (matrix& op1, matrix& op2) {
if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) {
return false;
} else {
for (int i=0; i<op1.sizey; i++) {
for (int j=0; j<op1.sizex; j++) {
if (op1(i,j)!=op2(i,j)) return false;
}
}
return true;
}
}

matrix operator+ (matrix& op1, matrix& op2) {
if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) {
throw 100; // cannot add matrices of different size
} else {
matrix result=matrix(op1.sizex,op1.sizey);
for (int i=0; i<op1.sizey; i++) {
for (int j=0; j<op1.sizex; j++) {
result(i,j)=op1(i,j)+op2(i,j);
}
}
return result;
}
}

matrix operator- (matrix& op1, matrix& op2) {
if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) {
throw 100; // cannot subtract matrices of different size
} else {
matrix result=matrix(op1.sizex,op1.sizey);
for (int i=0; i<op1.sizey; i++) {
for (int j=0; j<op1.sizex; j++) {
result(i,j)=op1(i,j)-op2(i,j);
}
}
return result;
}
}

matrix operator* (matrix& op1, matrix& op2) {
if (op1.sizex!=op2.sizey) {
throw 100; // cannot multiply matrices of inappropriate sizes
} else {
matrix result=matrix(op1.sizey,op2.sizex);
for (int i=0; i<op1.sizey; i++) {
for (int j=0; j<op2.sizex; j++) {
result(i,j)=0.0;
for (int k=0; k<op1.sizex; k++)
result(i,j)+=op1(i,k)*op2(k,j);
}
}
return result;
}
}

matrix operator* (double op1, matrix& op2) {
matrix result=matrix(op2.sizex,op2.sizey);
for (int i=0; i<op2.sizey; i++) {
for (int j=0; j<op2.sizex; j++) {
result(i,j)=op1*op2(i,j);
}
}
return result;
}

matrix operator* (matrix& op1, double op2) {
matrix result=matrix(op1.sizex,op1.sizey);
for (int i=0; i<op1.sizey; i++) {
for (int j=0; j<op1.sizex; j++) {
result(i,j)=op1(i,j)*op2;
}
}
return result;
}

double average (double r[], double n)
{
int i;
double sum = 0;
for (i=0; i<n; i++)
sum += r[i];
return sum /n;
}

double covmatrix ( double x[], double y[], double meanx, double meany, double n)
{
int i;
double covar = 0;
for (i=0; i<n; i++);
covar += (x[i]-meanx) * (y[i] - meany);
return covar/(n-1);
}

double powermethod ()
{
}

{
Power Method(A 2 <n×n)
Select x 2 <n(6= 0)
   0.0001
errorInLambda   1
k   1
while errorInLamda >= 
do
calc
1   Ak+1x·Akx // top divide  bottom
Akx·Akx
errorInLambda   sqr root Ak+1x·Ak+1x
Akx·Akx − (calc1 )2
k   k + 1
output (1,Akx)as the eigenpair
}
```

Is This A Good Question/Topic? 0

## Replies To: Calculate Eigenvalues & Vectors using matrices

### #2 Jayman

• Student of Life

Reputation: 419
• Posts: 9,532
• Joined: 26-December 05

## Re: Calculate Eigenvalues & Vectors using matrices

Posted 10 December 2006 - 11:34 AM

### #3 lplover2k

Reputation: 0
• Posts: 1
• Joined: 18-December 06

## Re: Calculate Eigenvalues & Vectors using matrices

Posted 18 December 2006 - 07:47 AM

is there a way to easily find eigenvectors and eigenvalues plz??