-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.h
85 lines (74 loc) · 2.42 KB
/
matrix.h
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
#pragma once
#include "stb.h"
#include <Eigen/Dense>
#include <repr/repr.h>
#include <algorithm>
#include <vector>
struct StbData {
std::string path;
stb::Equil equilibria;
std::vector<std::string> basis;
};
struct MainData {
std::vector<std::string> basis;
Eigen::VectorXd lgk;
Eigen::MatrixXd matrix;
void insert_basis(const std::string& el) {
if(std::ranges::find(basis, el) == basis.end()) {
basis.push_back(el);
}
}
void insert_data(const StbData& data) {
if(matrix.rows() == 0 || matrix.cols() == 0) {
matrix = Eigen::MatrixXd::Zero(data.equilibria.matrix.rows(), basis.size() + 1);
matrix(Eigen::all, insert_indexes(data.basis)) = data.equilibria.matrix;
lgk = data.equilibria.lgk;
} else {
std::vector<unsigned> unmatched;
std::vector<unsigned> row_indexes;
unsigned count = matrix.rows();
auto indexes = insert_indexes(data.basis);
for(unsigned i = 0; i < data.equilibria.matrix.rows(); ++i) {
Eigen::VectorXd vec = Eigen::VectorXd::Zero(basis.size() + 1);
vec(indexes) = data.equilibria.matrix.row(i);
bool found = false;
for(unsigned j = 0; j < matrix.rows(); ++j) {
if(matrix.row(j).transpose() == vec) {
found = true;
break;
}
}
if (!found) {
unmatched.push_back(i);
row_indexes.push_back(count++);
}
}
if(unmatched.size() > 0) {
std::cout << unmatched.size() << std::endl;
matrix.conservativeResize(matrix.rows() + unmatched.size(), Eigen::NoChange);
matrix(row_indexes, Eigen::all).setZero();
matrix(row_indexes, indexes) = data.equilibria.matrix(unmatched, Eigen::all);
lgk.conservativeResize(lgk.size() + unmatched.size());
lgk(row_indexes) = data.equilibria.lgk(unmatched);
}
}
std::cout << repr(insert_indexes(data.basis)) << std::endl;
}
unsigned index(const std::string& b) {
auto it = std::ranges::find(basis, b);
return std::distance(basis.begin(), it);
}
std::vector<unsigned> insert_indexes(const std::vector<std::string>& b) {
std::vector<unsigned> res;
for(const auto& el : b) {
res.push_back(index(el));
}
res.push_back(basis.size());
return res;
}
void display() {
for(unsigned i = 0; i < matrix.rows(); ++i) {
std::cout << i << ".\t" << matrix.row(i) << "\t" << lgk(i) << std::endl;
}
}
};