-
Notifications
You must be signed in to change notification settings - Fork 0
/
EigenProject.cpp
161 lines (156 loc) · 5.53 KB
/
EigenProject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<iostream>
#include<iterator>
#include<Eigen/Dense>
#include<Eigen/Eigenvalues>
#include<random>
#include<cmath>
#include<math.h>
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
using namespace std;
using namespace Eigen;
#define MAX_SIZE 10000
int n;
void setValueOfn(){
cout<<"Enter order of matrix:";
cin>>n;
if (n > MAX_SIZE) {
cout << "Error: Order exceeds maximum size. Setting order to maximum size.\n";
n = MAX_SIZE;
}
}
MatrixXf makeSymmetric(const MatrixXf& A) {
return 0.5 * (A + A.transpose());
}
MatrixXf normalizeVector(const MatrixXf& vect) {
MatrixXf normalizedVector = vect;
float maxElement = vect.cwiseAbs().maxCoeff();
for (int i = 0; i < vect.size(); ++i) {
normalizedVector(i) /= maxElement;
}
return normalizedVector;
}
void insertColumnMatrix(MatrixXf& originalMatrix, const MatrixXf& columnMatrix) {
if (columnMatrix.rows() != originalMatrix.rows()) {
cerr << "Dimention of Column & Rows not match" <<endl;
return;
}
originalMatrix.conservativeResize(originalMatrix.rows(), originalMatrix.cols() + columnMatrix.cols());
originalMatrix.rightCols(columnMatrix.cols()) = columnMatrix;
}
void deleteColumn(MatrixXf& matrix, int columnIndex) {
if (columnIndex < 0 || columnIndex >= matrix.cols()) {
cerr << "Error: Column index out of bounds." << endl;
return;
}
for (int i = columnIndex; i < matrix.cols() - 1; ++i) {
matrix.col(i) = matrix.col(i + 1);
}
matrix.conservativeResize(matrix.rows(), matrix.cols() - 1);
}
MatrixXf eigenValues(const MatrixXf& eigenvectors) {
SelfAdjointEigenSolver<MatrixXf> solver(eigenvectors.transpose() * eigenvectors);
return solver.eigenvalues();
}
int main(){
setValueOfn();
srand(time(0));
MatrixXf finalEigVect(n,1);
MatrixXf randomMatrixA , randomMatrixB;
randomMatrixA.setRandom(n,n);
randomMatrixA = randomMatrixA.array().abs(); // Ensure positive values
randomMatrixB.setRandom(n,n);
randomMatrixB = randomMatrixB.array().abs(); // Ensure positive values
MatrixXf A = makeSymmetric(randomMatrixA);
MatrixXf B = makeSymmetric(randomMatrixB);
// Eigenvalue and Eigenvector Computation: The GeneralizedEigenSolver class in Eigen is used
// to solve the generalized eigenvalue problem Ax=λBx. The .compute(A, B) method computes the
// eigenvalues and eigenvectors.
GeneralizedSelfAdjointEigenSolver<MatrixXf> ges(A , B);
MatrixXf eigenvalues = ges.eigenvalues();
MatrixXf eigenvectors = ges.eigenvectors();
for (int i = 0; i < eigenvectors.rows(); ++i) {
for (int j = 0; j < eigenvectors.cols(); ++j) {
eigenvectors(i, j) *= -1;
}
}
cout << "Eigenvalues: \n" << eigenvalues << endl;
cout << "Eigenvectors: \n" << eigenvectors << endl;
MatrixXf EigenVector(n,1);
MatrixXf temp;
for(int i=0; i<eigenvectors.cols(); i++){
temp = normalizeVector(eigenvectors.col(i));
insertColumnMatrix(EigenVector , temp);
deleteColumn(temp , 0);
}
deleteColumn(EigenVector , 0);
cout<<"Normalized Eigen Vector\n"<<EigenVector<<endl;
int T=3; //just for checking otherwise set vale to 10001
MatrixXf Reward;
MatrixXf operation(n, n);
MatrixXf result;
MatrixXf rewCala , rewCalaR , rewResl, rewCalb , rewCalbR , rewResR;
float rewa , rewb;
int colNum;
MatrixXf vi , vj;
for (int j = 0; j < T; j++) { //T
for (int i = 0; i < n; i++) { //n or k
colNum = rand()%EigenVector.cols();
vi = EigenVector.col(colNum);
colNum = rand()%EigenVector.cols();
vj = EigenVector.col(colNum);
float result = (vj.transpose() * B * vj)(0, 0);
if(result < 0) //to fin nan result
result = result * (-1);
float sqrtResult = sqrt(result);
sqrtResult = 1.0 /sqrtResult;
MatrixXf yj;
yj = vj / sqrtResult;
rewCala= vi.transpose() * B * vi;
rewa = rewCala(0,0);
rewCalaR = A * vi;
rewCalb = vi.transpose() * A * vi;
rewb=rewCalb(0,0);
rewCalbR = B * vi;
rewResl=rewCalaR * rewa;
rewResR=rewCalbR * rewb;
Reward=rewResl - rewResR;
int tempj = j;
MatrixXf PenARes , PenBRes , diffRes;
MatrixXf Penalties(n,1) ;
for(int row=0; row<n; row++){
Penalties(row , 0) = 0;
}
float PenAScal, PenBScal , PenCScal;
MatrixXf PenA , PenB , PenC , PenVecA , PenVecB;
while(tempj < i){
PenA = vi.transpose() * A * yj;
PenAScal=PenA(0,0);
PenB = vi.transpose() * B * vi ;
PenBScal = PenB(0,0);
PenC = vi.transpose() * B * yj ;
PenCScal = PenC(0,0);
PenVecA = B * yj;
PenVecB = B * vi;
PenARes = PenBScal * PenVecA;
PenBRes = PenCScal * PenVecB;
diffRes = PenARes - PenBRes;
Penalties = Penalties + (PenAScal * diffRes);
MatrixXf delta(n,1);
delta = Reward - Penalties;
MatrixXf itadelta(n,1);
itadelta = -2 * delta;
MatrixXf wi(n,1);
wi = vi + itadelta;
vi = normalizeVector(wi);
insertColumnMatrix(finalEigVect , vi);
tempj++;
}
}
}
deleteColumn(finalEigVect , 0);
cout<<"Final Eigen Vector:\n"<<finalEigVect<<endl;
MatrixXf EigenValues = eigenValues(finalEigVect);
cout<<"Eigen Values Matrix : \n"<<EigenValues<<endl;
return 0;
}