-
Notifications
You must be signed in to change notification settings - Fork 0
/
betti.cpp
233 lines (224 loc) · 7.22 KB
/
betti.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef __BETTI_CPP_
#define __BETTI_CPP_
/*****************************************************************************\
* This file is part of DynGB. *
* *
* DynGB 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 2 of the License, or *
* (at your option) any later version. *
* *
* DynGB 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 DynGB. If not, see <http://www.gnu.org/licenses/>. *
\*****************************************************************************/
#include <set>
using std::set;
#include <utility>
using std::pair; using std::tuple; using std::get;
#include <algorithm>
using std::any_of; using std::min; using std::max; using std::sort;
#include "betti.hpp"
WT_TYPE lcm_degree(const Monomial &t, const Monomial &u, const WT_TYPE * grading = nullptr) {
auto a = t.packed_log(), b = u.packed_log();
NVAR_TYPE i = 0, j = 0;
WT_TYPE result = 0;
if (grading != nullptr) {
for (/* */; i < t.packed_size() and j < u.packed_size(); /* */) {
if (a[i] == b[j]) {
result += max(a[i+1], b[j+1]) * grading[a[i]];
i += 2; j += 2;
} else if (a[i] < b[j]) {
result += a[i+1] * grading[a[i]];
i += 2;
} else {
result += b[j+1] * grading[b[j]];
j += 2;
}
}
for (/* */; i < t.packed_size(); i += 2)
result += a[i+1] * grading[a[i]];
for (/* */; j < u.packed_size(); j += 2)
result += b[j+1] * grading[b[j]];
} else {
for (/* */; i < t.packed_size() and j < u.packed_size(); /* */) {
if (a[i] == b[j]) {
result += max(a[i+1], b[j+1]);
i += 2; j += 2;
} else if (a[i] < b[j]) {
result += a[i+1];
i += 2;
} else {
result += b[j+1];
j += 2;
}
}
for (/* */; i < t.packed_size(); i += 2)
result += a[i+1];
for (/* */; j < u.packed_size(); j += 2)
result += b[j+1];
}
return result;
}
struct Comparer {
const vector<const Monomial *> * T;
bool operator()(const pair<int, int> &p, const pair<int, int> &q) {
return lcm_degree(*((*T)[p.first]), *((*T)[p.second])) <
lcm_degree(*((*T)[q.first]), *((*T)[q.second])) ;
}
};
map<DEG_TYPE, unsigned long> incremental_betti(
const vector<Monomial> & T,
const Monomial & u,
set< pair<int, int> > & R,
const WT_TYPE * grading
) {
map<DEG_TYPE, unsigned long> result;
// cancel old triples
auto ri = R.begin();
while (ri != R.end()) {
auto & r = *ri;
auto rlcm{T[r.first].lcm(r.second)};
if (rlcm.like_lcm(rlcm, u) and rlcm.like_lcm(T[r.first], u) and rlcm.like_lcm(T[r.second], u)) {
auto tmp {ri};
++ri;
R.erase(tmp);
} else ++ri;
}
// cancel new pairs subject to proper divisibility of lcms
set<tuple<int, int, const Monomial> > A;
for (int i = 0; i < T.size(); ++i) { A.emplace(i, T.size(), T[i].lcm(u)); }
auto ai = A.begin();
while (ai != A.end()) {
if (any_of(
A.begin(), A.end(),
[&ai](auto bi){
return *ai != bi and get<2>(bi) != get<2>(*ai) and get<2>(bi) | get<2>(*ai);
}
)) {
auto tmp{ai}; ++ai;
A.erase(tmp);
} else ++ai;
}
// retain only one pair at each lcm
ai = A.begin();
while (ai != A.end()) {
auto ai2 = ai; ++ai2;
auto bi { ai2 };
while (bi != A.end()) {
if (get<2>(*ai) == get<2>(*bi)) {
if (T[get<0>(*bi)].is_coprime(u)) {
A.erase(ai);
ai = bi;
++bi;
} else {
auto tmp { bi };
++tmp;
if (ai2 == bi) ai2 = tmp;
A.erase(bi);
bi = tmp;
}
} else ++bi;
}
if (T[get<0>(*ai)].is_coprime(u))
A.erase(ai);
else
R.emplace(get<0>(*ai), T.size());
ai = ai2;
}
for (auto & r : R) {
DEG_TYPE d = (r.second == T.size()) ?
lcm_degree(T[r.first], u, grading) :
lcm_degree(T[r.first], T[r.second], grading);
if (result.find(d) != result.end())
++result[d];
else
result[d] = 1;
}
return result;
}
map<DEG_TYPE, unsigned long> full_betti(
const list<Monomial> & U, const WT_TYPE * grading
) {
map<DEG_TYPE, unsigned long> result;
vector<const Monomial *> T(U.size());
int i = 0;
for (auto & u : U) { T[i] = &u; ++i; }
// first add all pairs
size_t T_size = T.size();
vector< pair<int, int> > P(T.size() * (T.size() - 1) / 2);
unsigned k = 0;
for (int i = 0; i < T.size(); ++i) {
for (int j = i + 1; j < T.size(); ++j) {
P[k].first = i; P[k].second = j;
++k;
}
}
// now we remove relatively prime pairs,
// as well as those that they divide
vector< pair<int, int> > Q(P.size());
k = 0;
auto pi = P.begin();
while (pi != P.end()) {
if (pi->first != -1) {
const Monomial & t = *T[pi->first];
if (not t.is_coprime(*T[pi->second])) {
Q[k] = *pi;
++k;
} else {
//cout << "dropping " << *T[pi->first] << ", " << *T[pi->second] << endl;
const Monomial & u = *T[pi->second];
auto tu { t.lcm(u) };
auto pi2 = pi; ++pi2;
while (pi2 != P.end()) {
if (pi2->first != -1 and (not tu.divides_lcm(*T[pi2->first], *T[pi2->second])))
++pi2;
else {
pi2->first = -1;
++pi2;
}
}
}
}
++pi;
}
// we are now left only with non-relatively prime pairs
// remove pairs that will be computed after others whose lcm divides theirs
set< pair<int, int> > R;
pair<int, int> first, second;
Comparer compare;
compare.T = &T;
sort(Q.begin(), Q.begin() + k, compare);
for (auto qi = Q.begin(); qi < Q.begin() + k; ++qi) {
auto & q = *qi;
bool any_divides = false;
for (int i = 0; (not any_divides) and i < T.size(); ++i) {
first.first = min(i, q.first); first.second = max(i, q.first);
second.first = min(i, q.second); second.second = max(i, q.second);
if (
T[i]->divides_lcm(*T[q.first], *T[q.second]) and
R.find(first) != R.end() and R.find(second) != R.end()
) {
any_divides = true;
//cout << "erasing " << *T[q.first] << ", " << *T[q.second] << " because of " << *T[i] << endl;
}
}
if (not any_divides)
R.emplace(q);
}
for (auto & r : R) {
//cout << *T[r.first] << ", " << *T[r.second] << "; ";
DEG_TYPE d = lcm_degree(*T[r.first], *T[r.second], grading);
if (result.find(d) != result.end())
++result[d];
else
result[d] = 1;
}
//cout << result.size() << "; ";
return result;
}
#endif