-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinadic.c
614 lines (531 loc) · 12.7 KB
/
combinadic.c
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int polynomial_exponents( int, int, int, int * );
long int polynomial_exponents_long( long int, long int, long int, long int * );
int truncfact( int, int );
long int truncfact_long( long int, long int );
/**
* This is the standard factorial function. Improvement can certainly be made
* by rewriting it more intelligently.
* @param num_in The number whose factorial you want
* @return The factorial of num_in
*/
int factorial( int num_in )
{
int fct = 1;
int i;
if( num_in <= 0 )
return 1;
for(i=num_in;i>1;i--)
fct *= i;
return fct;
}
long int factorial_long( long int num_in )
{
long int fct = 1;
long int i;
if( num_in <= 0 )
return 1;
for(i=num_in;i>1;i--)
fct *= i;
return fct;
}
int binomial_slow( int n_in, int k_in )
{
if( n_in < k_in )
return 0;
else
return factorial( n_in ) / factorial( k_in ) / factorial( n_in - k_in );
}
long int binomial_slow_long( long int n_in, long int k_in )
{
if( n_in < k_in )
return 0;
else
return factorial( n_in ) / factorial( k_in ) / factorial( n_in - k_in );
}
int binomial( int n_in, int k_in )
{
if( n_in < k_in )
return 0;
else
return truncfact( n_in, k_in ) / factorial( k_in );
}
long int binomial_long( long int n_in, long int k_in )
{
if( n_in < k_in )
return 0;
else
return truncfact_long( n_in, k_in ) / factorial_long( k_in );
}
/**
* This function returns the number of r_in-permutations of n_in objects.
*/
int truncfact( int n_in, int r_in )
{
int i,prd = 1;
for(i=0;i<r_in;i++)
prd *= n_in - i;
return prd;
}
long int truncfact_long( long int n_in, long int r_in )
{
long int i,prd = 1;
for(i=0;i<r_in;i++)
prd *= n_in - i;
return prd;
}
/**
* Given a combinadic index, its corresponding vector is
* the strictly increasing non-negative integer sequence,
* {m_k}, of length dim_in and whose entries are less than
* or equal to lim_in, and such that
* C(m_k,k+1) + C(m_{k-1},k) + ... + C(m_0,1) = idx_in
*/
int combinadic_vector( int idx_in, int lim_in, int dim_in, int *vec_out )
{
int i,j,idx,tmp;
if( idx_in < 0 )
return -1;
idx = idx_in;
for(i=dim_in-1;i>=0;i--)
{
for(j=i+1;j<=lim_in;j++)
{
tmp = binomial( j, i + 1 );
if( tmp > idx )
break;
}
idx -= binomial( j - 1, i + 1 );
vec_out[i] = j - 1;
}
return 0;
}
long int combinadic_vector_long( long int idx_in, long int lim_in, long int dim_in, long int *vec_out )
{
long int i,j,idx,tmp;
if( idx_in < 0 )
return -1;
idx = idx_in;
for(i=dim_in-1;i>=0;i--)
{
for(j=i+1;j<=lim_in;j++)
{
tmp = binomial( j, i + 1 );
if( tmp > idx )
break;
}
idx -= binomial( j - 1, i + 1 );
vec_out[i] = j - 1;
}
return 0;
}
int combinadic_vector_length( int idx_in, int dim_in, int lim_in )
{
int i,sum;
sum = 0;
for(i=1;i<=dim_in;i++)
{
sum += binomial( lim_in, i );
if( sum > idx_in )
break;
}
return i;
}
long int combinadic_vector_length_long( long int idx_in, long int dim_in, long int lim_in )
{
long int i,sum;
sum = 0;
for(i=1;i<=dim_in;i++)
{
sum += binomial( lim_in, i );
if( sum > idx_in )
break;
}
return i;
}
int combinadic_next( int lim_in, int dim_in, int *vec_in )
{
int i,j;
for(i=0;i<dim_in;i++)
{
if( ( vec_in[i] < lim_in )
&& ( ( i < dim_in - 1 && ( vec_in[i+1] - vec_in[i] ) > 1 ) || i == dim_in - 1 ) )
{
++vec_in[i];
break;
}
}
for(j=0;j<i;j++)
vec_in[j] = j;
}
long int combinadic_next_long( long int lim_in, long int dim_in, long int *vec_in )
{
long int i,j;
for(i=0;i<dim_in;i++)
{
if( ( vec_in[i] < lim_in )
&& ( ( i < dim_in - 1 && ( vec_in[i+1] - vec_in[i] ) > 1 ) || i == dim_in - 1 ) )
{
++vec_in[i];
break;
}
}
for(j=0;j<i;j++)
vec_in[j] = j;
}
int combinadic_init( int lim_in, int dim_in, int *ptr_in )
{
int i;
if(lim_in<dim_in)
return -1; // can't make dim_in-combinations of lim_in (< dim_in) objects
for(i=0;i<dim_in;i++)
ptr_in[i] = i;
return 0;
}
long int combinadic_init_long( long int lim_in, long int dim_in, long int *ptr_in )
{
long int i;
if(lim_in<dim_in)
return -1; // can't make dim_in-combinations of lim_in (< dim_in) objects
for(i=0;i<dim_in;i++)
ptr_in[i] = i;
return 0;
}
int combinadic_index( int dim_in, int *vec_in )
{
int i;
int idx = 0;
for(i=0;i<dim_in;i++)
idx += binomial( vec_in[i], i + 1 );
return idx;
}
long int combinadic_index_long( long int dim_in, long int *vec_in )
{
long int i;
long int idx = 0;
for(i=0;i<dim_in;i++)
idx += binomial( vec_in[i], i + 1 );
return idx;
}
int rcombinadic_init( int lim_in, int dim_in, int *ptr_in )
{
return combinadic_init( lim_in + dim_in - 1, dim_in - 1, ptr_in );
}
long int rcombinadic_init_long( long int lim_in, long int dim_in, long int *ptr_in )
{
return combinadic_init_long( lim_in + dim_in - 1, dim_in - 1, ptr_in );
}
int rcombinadic_index( int dim_in, int *vec_in )
{
return combinadic_index( dim_in - 1, vec_in );
}
long int rcombinadic_index_long( long int dim_in, long int *vec_in )
{
return combinadic_index_long( dim_in - 1, vec_in );
}
int rcombinadic_next( int lim_in, int dim_in, int *vec_in )
{
return combinadic_next( lim_in + dim_in - 1, dim_in - 1, vec_in );
}
long int rcombinadic_next_long( long int lim_in, long int dim_in, long int *vec_in )
{
return combinadic_next_long( lim_in + dim_in - 1, dim_in - 1, vec_in );
}
int rcombinadic_vector( int idx_in, int lim_in, int dim_in, int *vec_out )
{
return combinadic_vector( idx_in, lim_in + dim_in - 1, dim_in - 1, vec_out );
}
long int rcombinadic_vector_long( long int idx_in, long int lim_in, long int dim_in, long int *vec_out )
{
return combinadic_vector_long( idx_in, lim_in + dim_in - 1, dim_in - 1, vec_out );
}
/* takes a combinadic vector of length dim_in and outputs
an occupancy vector of length dim_in + 1 */
int rcombinadic_occupancy( int lim_in, int dim_in, int *vec_in, int *occ_out )
{
int i;
if( dim_in < 1 )
return -1; /* Failure: Parameter out of acceptable domain */
if( dim_in > 1 )
{
occ_out[0] = vec_in[0];
for(i=1;i<dim_in-1;i++)
occ_out[i] = vec_in[i] - vec_in[i-1] - 1;
occ_out[dim_in-1] = lim_in + dim_in - 1 - vec_in[dim_in-2] - 1; /* minus addtl 'one' adjust difference of 1 to mean zero occupancy */
}
else
occ_out[0] = lim_in;
return 0;
}
long int rcombinadic_occupancy_long( long int lim_in, long int dim_in, long int *vec_in, long int *occ_out )
{
long int i;
if( dim_in < 1 )
return -1; /* Failure: Parameter out of acceptable domain */
if( dim_in > 1 )
{
occ_out[0] = vec_in[0];
for(i=1;i<dim_in;i++)
occ_out[i] = vec_in[i] - vec_in[i-1] - 1;
occ_out[dim_in-1] = lim_in + dim_in - 1 - vec_in[dim_in-2] - 1; /* minus addtl 'one' adjust difference of 1 to mean zero occupancy */
}
else
occ_out[0] = lim_in;
return 0;
}
/* takes an occupancy vector of length dim_in and outputs
a vector of dim dim_in - 1 */
int rcombinadic_invoccup( int dim_in, int *occ_in, int *vec_out )
{
int i;
vec_out[0] = occ_in[0];
for(i=1;i<dim_in;i++)
vec_out[i] = vec_out[i-1] + occ_in[i] + 1;
return 0;
}
long int rcombinadic_invoccup_long( long int dim_in, long int *occ_in, long int *vec_out )
{
long int i;
vec_out[0] = occ_in[0];
for(i=1;i<dim_in;i++)
vec_out[i] = vec_out[i-1] + occ_in[i] + 1;
return 0;
}
int polynomial_index( int dim_in, int *exp_in )
{
int cmb[dim_in-1];
rcombinadic_invoccup( dim_in, exp_in, cmb );
return rcombinadic_index( dim_in, cmb );
}
long int polynomial_index_long( long int dim_in, long int *exp_in )
{
long int cmb[dim_in-1];
rcombinadic_invoccup_long( dim_in, exp_in, cmb );
return rcombinadic_index_long( dim_in, cmb );
}
int global_poly_index( int ord_in, int dim_in, int *exp_in )
{
return polynomial_index( dim_in, exp_in ) + binomial( ord_in + dim_in - 1, dim_in );
}
long int global_poly_index_long( long int ord_in, long int dim_in, long int *exp_in )
{
return polynomial_index_long( dim_in, exp_in ) + binomial_long( ord_in + dim_in - 1, dim_in );
}
/**
* Generate the global polynomial vector idx_in of max order ord_in
* and of dimension dim_in and put the exponent in exp_out
*/
int global_polynomial_vector( int idx_in, int dim_in, int *exp_out )
{
int n,m,r,cmb[dim_in-1];
for(n=0,r=0;;r++)
{
m = binomial( r + dim_in - 1, dim_in - 1 );
if( n + m > idx_in )
break;
else
n += m;
}
polynomial_exponents( idx_in - n, r, dim_in, exp_out );
return 0;
}
long int global_polynomial_vector_long( long int idx_in, long int dim_in, long int *exp_out )
{
long int n,m,r,cmb[dim_in-1];
for(n=0,r=0;;r++)
{
m = binomial( r + dim_in - 1, dim_in - 1 );
if( n + m > idx_in )
break;
else
n += m;
}
polynomial_exponents_long( idx_in - n, r, dim_in, exp_out );
return 0;
}
/* NOTE: it's probably best to stick to a two-index system: (term_order,local_index) */
/* takes an index of a specified order only */
int polynomial_exponents( int idx_in, int ord_in, int dim_in, int *occ_out )
{
int vec[dim_in-1];
rcombinadic_vector( idx_in, ord_in, dim_in, vec );
rcombinadic_occupancy( ord_in, dim_in, vec, occ_out );
return 0;
}
long int polynomial_exponents_long( long int idx_in, long int ord_in, long int dim_in, long int *occ_out )
{
long int vec[dim_in-1];
rcombinadic_vector_long( idx_in, ord_in, dim_in, vec );
rcombinadic_occupancy_long( ord_in, dim_in, vec, occ_out );
return 0;
}
/*
* FACTORADIC OPERATIONS
*/
int factoradic_radix_index( int dim_in, int idx_in, int *rad_out )
{
int i,fct,div,rem;
rem = idx_in;
for(i=dim_in-1;i>=0;i--)
{
fct = factorial( i );
div = rem / fct;
rem = rem % fct;
rad_out[dim_in-1-i] = div;
}
}
int factoradic_radix_vector( int dim_in, int *vec_in, int *rad_out )
{
int c,i,j;
for(i=0;i<dim_in;i++)
rad_out[i] = 0;
for(i=0;i<dim_in;i++)
{
for(j=i+1;j<dim_in;j++)
{
if( vec_in[j] < vec_in[i] )
++rad_out[i];
}
}
return 0l;
}
int factoradic_index( int dim_in, int *vec_in )
{
int i,rad[dim_in],idx = 0;
factoradic_radix_vector( dim_in, vec_in, rad );
for(i=0;i<dim_in;i++)
idx += rad[dim_in-1-i] * factorial( i );
return idx;
}
int factoradic_init( int dim_in, int *vec_in )
{
int i;
for(i=0;i<dim_in;i++)
vec_in[i] = i;
return 0l;
}
int factoradic_next( int dim_in, int *vec_in )
{
}
int factoradic_vector( int idx_in, int dim_in, int *vec_out )
{
int i,j,k,rad[dim_in];
int fnd[dim_in];
for(i=0;i<dim_in;i++)
fnd[i] = 0;
factoradic_radix_index( dim_in, idx_in, rad );
for(i=0;i<dim_in;i++)
{
for(j=0,k=0;j<dim_in;j++)
{
if( fnd[j] == 0 )
++k;
if( k - 1 >= rad[i] )
break;
}
fnd[j] = 1;
vec_out[i] = j;
}
return 0;
}
/**
* Given a factoradic index, this function computes the radix form of the
* index. That is, it converts the base 10 input index into a non-constant
* factorial base number.
*/
long int factoradic_radix_index_long( long int dim_in, long int idx_in, long int *rad_out )
{
long int i,fct,div,rem;
rem = idx_in;
for(i=dim_in-1;i>=0;i--)
{
fct = factorial( i );
div = rem / fct;
rem = rem % fct;
rad_out[dim_in-1-i] = div;
}
}
long int factoradic_radix_vector_long( long int dim_in, long int *vec_in, long int *rad_out )
{
long int c,i,j;
for(i=0;i<dim_in;i++)
rad_out[i] = 0;
for(i=0;i<dim_in;i++)
{
for(j=i+1;j<dim_in;j++)
{
if( vec_in[j] < vec_in[i] )
++rad_out[i];
}
}
return 0l;
}
long int factoradic_index_long( long int dim_in, long int *vec_in )
{
long int i,rad[dim_in],idx = 0;
factoradic_radix_vector_long( dim_in, vec_in, rad );
for(i=0;i<dim_in;i++)
idx += rad[dim_in-1-i] * factorial( i );
return idx;
}
long int factoradic_init_long( long int dim_in, long int *vec_in )
{
long int i;
for(i=0;i<dim_in;i++)
vec_in[i] = i;
return 0l;
}
long int factoradic_next_long( long int dim_in, long int *vec_in )
{
}
long int factoradic_vector_long( long int idx_in, long int dim_in, long int *vec_out )
{
long int i,j,k,rad[dim_in];
int fnd[dim_in];
for(i=0;i<dim_in;i++)
fnd[i] = 0;
factoradic_radix_index_long( dim_in, idx_in, rad );
for(i=0;i<dim_in;i++)
{
for(j=0,k=0;j<dim_in;j++)
{
if( fnd[j] == 0 )
++k;
if( k - 1 >= rad[i] )
break;
}
fnd[j] = 1;
vec_out[i] = j;
}
return 0;
}
void partition_init( int *s, int *m, int n )
{
int i;
for(i=0;i<n;i++)
m[i] = 1, s[i] = 1;
}
int partition_next( int *s, int *m, int n )
{
/* Update s: 1 1 1 1 -> 2 1 1 1 -> 1 2 1 1 -> 2 2 1 1 -> 3 2 1 1 -> 1 1 2 1 ... */
int i = 0;
++s[i];
while ((i < n - 1) && (s[i] > m[i] + 1))
{
s[i] = 1;
++i;
++s[i];
}
/* If i is has reached n-1 th element, then the last unique partition has been found*/
if (i == n - 1)
return 0;
/* Because all the first i elements are now 1, s[i] (i + 1 th element)
is the largest. So we update max by copying it to all the first i
positions in m.*/
int max = s[i];
for (i = i - 1; i >= 0; --i)
m[i] = max;
return 1;
}