-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartition.cpp
227 lines (186 loc) · 6.61 KB
/
Partition.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
#include <vector>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <map>
#include <iostream>
#include "Partition.h"
using namespace std;
int_partition::int_partition() : theLayer(NULL) {}
int_partition::int_partition(const int_partition::vector_of& layer) : theLayer(&layer) {}
bool int_partition::lexico_next()
{
nat num_units = count(1);
// First case: there are no non-unit parts
if(num_units == size())
return false;
erase(1);
nat smallest_nonunit = *begin();
erase(begin());
//(num_units + smallest_nonunit) = Q*(smallest_nonunit - 1) + R
nat Q = (num_units + smallest_nonunit)/(smallest_nonunit - 1);
nat R = (num_units + smallest_nonunit) % (smallest_nonunit - 1);
for(nat q = 0; q < Q; ++q)
insert(smallest_nonunit - 1);
if(R != 0)
insert(R);
return true;
}
// Is this a covering refinement of mu?
void int_partition::BuildListOfCoveringCoarsenings(int_partition::set_of& out) const
{
for(const_iterator i1 = begin(); i1 != end(); ++i1)
{
for(const_iterator i2 = next(i1,1); i2 != end(); ++i2)
{
int_partition temp(*this);
temp.erase(temp.find(*i1));
temp.erase(temp.find(*i2));
temp.insert(*i1 + *i2);
out.insert(temp);
}
}
}
void int_partition::BuildListOfCoveringDominators(int_partition::set_of& out) const
{
for(const_reverse_iterator i1 = rbegin(); i1 != rend(); ++i1)
{
for(const_reverse_iterator i2 = next(i1,1); i2 != rend(); ++i2)
{
int_partition temp(*this);
temp.erase(temp.find(*i1));
temp.erase(temp.find(*i2));
temp.insert(*i1 + 1);
temp.insert(*i2 - 1);
temp.erase(0);
out.insert(temp);
}
}
}
int int_partition::GetQuadraticSum() const {
return inner_product(begin(),end(),begin(),0);
}
nat int_partition::GetWeight() const {
return accumulate(begin(),end(),0);
}
nat int_partition::GetFirstPart() const { return empty() ? 0:*rbegin(); }
int_partition::const_iterator int_partition::GetFirstPartIterator() const { return empty() ? end():--end(); }
bool int_partition::IsContainedWithin(const int_partition& mu) const {
for(const_iterator i = begin(); i != end(); ++i)
if(mu.count(*i) < count(*i))
return false;
return true;
}
void int_partition::PrintInline(ostream& cout) const {
for(auto i = rbegin(); i != rend(); ++i)
cout << *i << " ";
}
void int_partition::Print(nat spaces) const
{
for(nat s = 0; s < spaces;++s) cout << " ";
cout << "( ";
PrintInline(cout);
cout << ")" << endl;
}
int_partition_layer::int_partition_layer() : theBank(NULL) {}
int_partition_layer::int_partition_layer(const int_partition_layer::vector_of& bank, nat n) : theBank(&bank), N(n) {}
void int_partition_layer::Build()
{
int_partition temp(*this);
temp.insert(N);
push_back(temp);
while(temp.lexico_next())
push_back(temp);
//sort(begin(), end(), [](const int_partition& a, const int_partition& b){ if(a.size() < b.size())return true; if(b.size() < a.size())return false; return a<b; } );
BuildBins();
}
void int_partition_layer::BuildBins()
{
ptrByLength.resize(N + 1);
ptrByMaxPart.resize(N + 1);
BeginByFirstPart.resize(N + 1,end());
for(const_iterator i = begin(); i != end(); ++i)
{
if(BeginByFirstPart[i->GetFirstPart()] == end())
BeginByFirstPart[i->GetFirstPart()] = i;
ptrByLength[i->size()].push_back(i);
ptrByMaxPart[i->GetFirstPart()].push_back(i);
}
}
void int_partition_layer::BuildProperties()
{
theProperties.clear();
for(const_iterator i = begin(); i != end(); ++i)
theProperties.push_back(properties());
for(const_iterator i = begin(); i != end(); ++i)
{
int_partition::set_of temp;
theProperties[i - begin()].QuadraticSum = i->GetQuadraticSum();
/*i->BuildListOfCoveringCoarsenings(temp);
for(const int_partition& p : temp)
{
const_iterator itr = std::find(begin(),end(),p);
if(itr == end())
cout << "Error" << std::endl;
theProperties[i].theCoveringCoarsenings.push_back(itr);
theProperties[itr].theCoveringRefinements.push_back(i);
}
temp.clear();*/
i->BuildListOfCoveringDominators(temp);
for(const int_partition& p : temp)
{
const_iterator itr = std::find(begin(),end(),p);
if(itr == end())
cout << "Error" << std::endl;
theProperties[i - begin()].theCoveringDominators.push_back(itr);
theProperties[itr - begin()].theCoveringDominees.push_back(i);
}
}
}
const int_partition_layer::properties&
int_partition_layer::GetProperties(int_partition_layer::const_iterator i) const { return theProperties.at(i - begin()); }
int_partition_layer::const_iterator int_partition_layer::FindFromRaw(const int_partition& raw) const
{
return find(BeginByFirstPart[raw.GetFirstPart()], end(), raw);
}
const vector<int_partition_layer::const_iterator>& int_partition_layer::GetHighestCommonRefinements(int_partition_layer::const_iterator i, int_partition_layer::const_iterator j) const {
return thePairProperties.at(BuildPair(i,j)).theHighestCommonRefinements;
}
const vector<int_partition_layer::const_iterator>& int_partition_layer::GetLowestCommonCoarsenings(int_partition_layer::const_iterator i, int_partition_layer::const_iterator j) const {
return thePairProperties.at(BuildPair(i,j)).theLowestCommonCoarsenings;
}
nat int_partition_layer::GetWeight() const { return N; }
int_partition_layer::operator nat() const { return N; }
const int_partition_layer& int_partition_layer::operator()(nat n) const { return theBank->at(n); }
int_partition_bank::int_partition_bank(nat n) : max_n(n) {
for(nat n = 0; n <= max_n; ++n)
push_back(int_partition_layer(*this, n));
}
void int_partition_bank::Build()
{
at(0).push_back(int_partition(at(0)));
for(nat n = 1; n <= max_n; ++n)
at(n).Build();
}
void int_partition_bank::BuildProperties()
{
for(iterator i = begin(); i != end(); ++i)
i->BuildProperties();
}
void int_partition_bank::BuildPairBounds()
{
// for(nat n = 0; n <= max_n; ++n)
// at(n).BuildPairBounds();
}
nat int_partition_bank::GetMaxWeight() const { return max_n; }
void int_partition_bank::Print() const {
for(const_iterator i = begin(); i != end(); ++i)
{
cout << (nat)*i << endl;
for(auto j = i->begin(); j != i->end(); ++j)
{
j->Print(0);
}
}
}