-
Notifications
You must be signed in to change notification settings - Fork 2
/
EigenSolver.cpp
89 lines (72 loc) · 2.46 KB
/
EigenSolver.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
/*===========================================================================
This file is part of AC4DC.
AC4DC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AC4DC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AC4DC. If not, see <https://www.gnu.org/licenses/>.
===========================================================================*/
#include "EigenSolver.h"
#include <vector>
#include "stdafx.h"
EigenSolver::EigenSolver()
{
//this class would test sever different pakages for matrix diagonalization. It uses just "Eigen" for now. Reminder: "Armadillo" in future
}
void EigenSolver::SolveSystem(std::vector<std::vector<double>> &A, std::vector<double> &B, int Length)
{
Eigen::MatrixXd M(Length, Length);
Eigen::VectorXd Y(Length);
Eigen::VectorXd X(Length);
for (int i = 0; i < Length; i++)
{
Y(i) = B[i];
for (int j = 0; j < Length; j++)
{
M(i, j) = A[i][j];
}
}
X = M.fullPivLu().solve(Y);
for (int i = 0; i < Length; i++)
{
B[i] = X(i);
}
}
void EigenSolver::SolveGenEig(std::vector<std::vector<double>> &F, std::vector<std::vector<double>> &S)
{
int Length = F.size();
Eigen::MatrixXd M(Length, Length);
Eigen::MatrixXd Q(Length, Length);
for (int i = 0; i < Length; i++) {
for (int j = 0; j < Length; j++) {
M(i, j) = F[i][j];
Q(i, j) = S[i][j];
}
}
Eigen::GeneralizedSelfAdjointEigenSolver<Eigen::MatrixXd> Magic(M, Q);
EigenValues = Magic.eigenvalues();
EigenVectors = Magic.eigenvectors();
}
vector<double> EigenSolver::EigenVals()
{
vector<double> Result(EigenValues.size(), 0);
for (int i = 0; i < Result.size(); i++) {
Result[i] = EigenValues(i);
}
return move(Result);
}
vector<vector<double>> EigenSolver::EigenVecs()
{
vector<vector<double>> Result(EigenValues.size(), vector<double>(EigenValues.size(), 0));
for (int i = 0; i < Result.size(); i++) {
for (int j = 0; j < Result.size(); j++) {
Result[i][j] = EigenVectors(j, i);
}
}
return move(Result);
}