Orbiter 2022
Combinatorial Objects
number_partition.cpp
Go to the documentation of this file.
1// number_partition.cpp
2//
3// Anton Betten
4// 23.03.2000
5// moved from D2 to ORBI Nov 15, 2007
6
8#include "discreta.h"
9
10
11using namespace std;
12
13
14#undef PARTITION_CHANGE_KIND_VERBOSE
15#undef PARTITION_COPY_VERBOSE
16
17
18namespace orbiter {
19namespace layer2_discreta {
20
22{
24 self.vector_pointer = NULL;
25}
26
28{
30 self.vector_pointer = NULL;
32 first(n);
33}
34
36{
37 // cout << "number_partition::allocate_number_partition()\n";
38 // c_kind(VECTOR);
39 Vector::m_l(2);
43}
44
46 // copy constructor: this := x
47{
48 // cout << "number_partition::copy constructor for object: " << const_cast<discreta_base &>(x) << "\n";
49 const_cast<discreta_base &>(x).copyobject_to(*this);
50}
51
53 // copy assignment
54{
55 // cout << "number_partition::operator = (copy assignment)" << endl;
56 copyobject(const_cast<discreta_base &>(x));
57 return *this;
58}
59
61{
62 OBJECTSELF s;
63
64 s = self;
65 new(this) number_partition;
66 self = s;
68}
69
71{
73}
74
76{
77 // cout << "partition::freeself_partition()\n";
79}
80
82{
83 return NUMBER_PARTITION;
84}
85
87{
88#ifdef PARTITION_COPY_VERBOSE
89 cout << "number_partition::copyobject_to()\n";
90 print_as_vector(cout);
91#endif
94#ifdef PARTITION_COPY_VERBOSE
95 x.as_number_partition().print_as_vector(cout);
96#endif
97}
98
99ostream& number_partition::print(ostream& ost)
100{
101 int i, l, a;
102
103 l = s_self().s_l();
104 if (s_type() == PARTITION_TYPE_VECTOR) {
105 ost << "[";
106 for (i = 0; i < l; i++) {
107 a = s_i(i);
108 ost << a;
109 if (i < l)
110 ost << ", ";
111 }
112 ost << "]";
113 }
114 else {
115 ost << "(";
116 int f_first = TRUE;
117 for (i = 0; i < l; i++) {
118 a = s_i(i);
119 if (a == 0)
120 continue;
121 if (!f_first)
122 ost << ", ";
123 ost << (i + 1);
124 if (a > 1) {
126 ost << "^" << a;
127 }
128 else {
129 ost << "^{" << a << "}";
130 }
131 }
132 f_first = FALSE;
133 }
134 ost << ")";
135 }
136 return ost;
137}
138
140{
141 // cout << "number_partition::first()\n";
144 m_l(n);
145 s_i(n - 1) = 1;
146}
147
149{
150 // cout << "number_partition::next()\n";
152 return next_exponent();
153 else
154 return next_vector();
155}
156
158{
159 int i, j, n, a, s;
160 // cout << "number_partition::next_exponent()\n";
161 n = s_self().s_l();
162 s = s_i(0);
163 for (i = 1; i < n; i++) {
164 a = s_i(i);
165 if (a > 0) {
166 a--;
167 s += (i + 1);
168 s_i(i) = a;
169 for (j = i - 1; j >= 0; j--) {
170 a = s / (j + 1);
171 s -= a * (j + 1);
172 s_i(j) = a;
173 }
174 return TRUE;
175 }
176 }
177 return FALSE;
178}
179
181{
182 // cout << "number_partition::next_vector()\n";
183 return FALSE;
184}
185
187{
188 int s;
189
190 first(n);
191 do {
192 s = nb_parts();
193 if (s == k)
194 return TRUE;
195 } while (next());
196 return FALSE;
197}
198
200{
201 int s;
202
203 while (next()) {
204 s = nb_parts();
205 if (s == k)
206 return TRUE;
207 }
208 return FALSE;
209}
210
212{
213 int s;
214
215 first(n);
216 do {
217 s = nb_parts();
218 if (s <= k)
219 return TRUE;
220 } while (next());
221 return FALSE;
222}
223
225{
226 int s;
227
228 while (next()) {
229 s = nb_parts();
230 if (s <= k)
231 return TRUE;
232 }
233 return FALSE;
234}
235
237{
238 int i, n, s = 0;
239
240 n = s_l();
241 for (i = 0; i < n; i++) {
242 // cout << "number_partition::nb_parts() i=" << i << ", s_i=" << s_i(i) << endl;
243 s += s_i(i);
244 }
245 return s;
246}
247
249{
250 Vector q;
251 int i, ii = 0, n, s, a;
252
253 n = s_l();
254 q.m_l_n(n);
255 s = nb_parts();
256 // cout << "number_partition::conjugate() s=" << s << endl;
257 for (i = 1; i <= n; i++) {
258 a = s_i(i - 1);
259 if (a) {
260 q.m_ii(s - 1, i - ii);
261 // cout << "partition::conjugate() q[" << s - 1 << "] = " << i - ii << endl;
262 ii = i;
263 s -= a;
264 }
265 }
266 q.swap(s_self());
267}
268
270{
271 int s, i, n, a;
272
273 s = nb_parts();
275 q.m_l(s);
276 n = s_l();
277 for (i = 0; i < n; i++) {
278 a = s_i(i);
279 if (a)
280 q.s_i(a - 1)++;
281 }
282}
283
285{
286 discreta_base a, b, c;
287 int i, n, m;
288
289 n = s_l();
290 a.factorial(n);
291#if 0
292 if (f_v) {
293 cout << "multinomial() factorial(" << n << ")=" << a << endl;
294 }
295#endif
296 b.m_i_i(1);
297 for (i = 1; i <= n; i++) {
298 m = s_i(i - 1);
299 if (m == 0)
300 continue;
301 c.factorial(i);
302 c.power_int(m);
303 b *= c;
304 }
305 a.integral_division_exact(b, res);
306 if (f_v) {
307 cout << "multinomial(" << *this << ") = " << res << endl;
308 }
309}
310
312{
315
316 type(q);
317
318 multinomial(res, f_v);
319 q.multinomial(a, f_v);
320 res *= a;
321 if (f_v) {
322 cout << "multinomial_ordered(" << *this << ") = " << res << endl;
323 }
324}
325
327{
328 int i, n, s;
329
330 s = 0;
332 cout << "number_partition::sum_of_decreased_parts() not of type exponent\n";
333 exit(1);
334 }
335 n = s_l();
336 for (i = 2; i <= n; i++) {
337 s += s_i(i - 1) * (i - 1);
338 }
339 return s;
340}
341
342
343}}
DISCRETA vector class for vectors of DISCRETA objects.
Definition: discreta.h:797
discreta_base & s_i(int i)
Definition: vector.cpp:202
void m_ii(int i, int a)
Definition: discreta.h:824
void copyobject_to(discreta_base &x)
Definition: vector.cpp:72
DISCRETA base class. All DISCRETA classes are derived from this class.
Definition: discreta.h:382
void integral_division_exact(discreta_base &x, discreta_base &q)
Definition: base.cpp:907
discreta_base & factorial(int z)
Definition: base.cpp:859
void swap(discreta_base &a)
Definition: base.cpp:179
number_partition & as_number_partition()
Definition: discreta.h:407
void copyobject(discreta_base &x)
Definition: base.cpp:194
discreta_base & power_int(int l)
Definition: base.cpp:447
DISCRETA class for partitions of an integer.
Definition: discreta.h:1310
number_partition & operator=(const discreta_base &x)
std::ostream & print(std::ostream &)
void multinomial_ordered(discreta_base &res, int f_v)
void multinomial(discreta_base &res, int f_v)
#define PARTITION_TYPE_EXPONENT
Definition: discreta.h:1303
#define PARTITION_TYPE_VECTOR
Definition: discreta.h:1302
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
enum printing_mode_enum current_printing_mode()
Definition: global.cpp:1573
@ NUMBER_PARTITION
NUMBER_PARTITION.
Definition: discreta.h:55
the orbiter library for the classification of combinatorial objects
DISCRETA internal class.
Definition: discreta.h:353