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
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
//dummy function
int power(int x, int y)
{
return (int) (pow(x, y)+0.5);
}
//dummy function
void get_rk_from_ni(int* r, int* k, int n, int i)
{
for (int j= 1, m= 0; m<i; ++j) {
*r= j; // j: depth level
*k= i-1-m; // k: rank, 0 based index in the level
m+= power(n, j); // m: i max of the previous level
}
}
//dummy function
void fill_p_from_rnk(int p[], int r, int n, int k)
{
for (int j= r; j>0; --j) {
p[j-1]= k%n; // filling (zero based) p[] (vector?) in reverse order with a polynomial (decomposition?) algorithm,
k/= n; // it is the equivalence of the number (k)10 in base n expansion,
} // the difference is that P0(3,n) output is 000 when (0)10->n conversion output is 0.
}
//ben dummy aussi... très très dummy...
int main()
{
//permutation unranking part
// input:
enum {n= 26}; // number of elements in a set of distinct elements.
int i= 703; // lexicographic depth index, starts from one.
// computing intermediate values
int r; // permutation size, r elements from n, aka P(r,n) where r>0 and generally r<=n, it is also the depth level of the tree (graph)
int k; // zero based permutation rank, it is also the zero based index from the depth level of the tree graph
get_rk_from_ni(&r, &k, n, i);
// allocating and filling the output array:
int* p= malloc(sizeof *p * (unsigned)r); // <=> int p[r], permutation container for the unranking operations Pk(r,n).
if (p == 0)
return 1;
fill_p_from_rnk(&p[0], r, n, k);
// printing data:
printf("\nPermutation Pk(r,n) with k=%d (0x%x) (o%o), r=%d, n=%d: {", k, k, k, r, n);
for (int j= 0; j<r; ++j)
printf("%d,", p[j]);
puts("\b}\n");
//bijection part (optional), (mapping?)
// input:
char S[][n]= {
{"ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789,:!_"} // Set of n distinct elements from a list, ordered arbitrarily.
, {"0123456789ABCDEF"} // another example with base 16 (hex) alphabet when n=16, decimal when n=10, octal for n=8, binary for n=2
};
// printing data (lexicographic sequence? string?) (bitstring?):
for (size_t s= 0; s<(sizeof S / sizeof S[0]); ++s) {
printf("Selected Set: %.*s\n", n, S[s]);
printf("Bijection f of the Permutation P through the Set, aka f(P), +/- <=> to (k)10=(fP)n: ");
char c;
for (int j= 0; j<r; ++j)
printf("%c", ((c= S[s][p[j]]) ? c: '?'));
puts("\n");
}
free(p);
return 0;
} |
Partager