aboutsummaryrefslogtreecommitdiff
path: root/src/utils/math.h
blob: 3ecd313daec1dc72c1f9f0d2c37f1556a86b7c1a (plain)
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
#define SWAP(x, y) do { x ^= y; y ^= x; x ^= y; } while (0)
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))

STATIC int64_t factorial(int64_t);
STATIC bool isperm(size_t n, const uint8_t [n]);
STATIC int64_t permtoindex(size_t n, const uint8_t [n]);
STATIC void indextoperm(int64_t, size_t n, uint8_t [n]);
STATIC int permsign(size_t n, const uint8_t [n]);
STATIC int64_t digitstosumzero(size_t n, const uint8_t [n], uint8_t);
STATIC void sumzerotodigits(int64_t, size_t n, uint8_t, uint8_t [n]);
STATIC double intpow(double, uint64_t);

STATIC int64_t
factorial(int64_t n)
{
	int64_t i, ret;

	if (n > FACTORIAL_MAX) {
		LOG("Error: won't compute factorial for n=%" PRId64 " because"
		    " it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
		return -1;
	}

	if (n < 0)
		return 0;

	for (i = 1, ret = 1; i <= n; i++)
		ret *= i;

	return ret;
}

STATIC bool
isperm(size_t n, const uint8_t a[n])
{
	size_t i;
	bool aux[FACTORIAL_MAX+1];

	if (n > (size_t)FACTORIAL_MAX) {
		LOG("Error: won't compute 'isperm()' for n=%zu because"
		    " it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
		return false;
	}

	memset(aux, false, n);
	
	for (i = 0; i < n; i++) {
		if (a[i] >= n)
			return false;
		else
			aux[a[i]] = true;
	}

	for (i = 0; i < n; i++)
		if (!aux[i])
			return false;

	return true;
}

STATIC int64_t
permtoindex(size_t n, const uint8_t a[n])
{
	size_t i, j;
	int64_t c, ret;

	if (n > (size_t)FACTORIAL_MAX) {
		LOG("Error: won't compute 'permtoindex()' for n=%zu because "
		    "it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
		return -1;
	}

	if (!isperm(n, a))
		return -1;

	for (i = 0, ret = 0; i < n; i++) {
		for (j = i+1, c = 0; j < n; j++)
			c += (a[i] > a[j]) ? 1 : 0;
		ret += factorial(n-i-1) * c;
	}

	return ret;
}

STATIC void
indextoperm(int64_t p, size_t n, uint8_t r[n])
{
	int64_t c;
	size_t i, j;
	uint8_t a[FACTORIAL_MAX+1];

	if (n > FACTORIAL_MAX) {
		LOG("Error: won't compute 'indextoperm()' for n=%zu because "
		    "it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
		goto indextoperm_error;
	}

	memset(a, 0, n);

	if (p < 0 || p >= factorial(n))
		goto indextoperm_error;

	for (i = 0; i < n; i++) {
		for (j = 0, c = 0; c <= p / factorial(n-i-1); j++)
			c += a[j] ? 0 : 1;
		r[i] = j-1;
		a[j-1] = 1;
		p %= factorial(n-i-1);
	}

	if (!isperm(n, r))
		goto indextoperm_error;

	return;

indextoperm_error:
	memset(r, UINT8_ERROR, n);
}

STATIC int
permsign(size_t n, const uint8_t a[n])
{
	size_t i, j, ret;

	for (i = 0, ret = 0; i < n; i++)
		for (j = i+1; j < n; j++)
			ret += a[i] > a[j] ? 1 : 0;

	return ret % 2;
}

STATIC int64_t
digitstosumzero(size_t n, const uint8_t a[n], uint8_t b)
{
	int64_t ret, p;
	size_t i, sum;

	if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) {
		LOG("Error: won't compute 'sumzero' for n=%zu and b=%" PRIu8
		    " (use n=8 b=3 or n=12 b=2)\n", n, b);
		return -1;
	}

	for (i = 1, ret = 0, p = 1, sum = 0; i < n; i++, p *= (int64_t)b) {
		if (a[i] >= b) {
			LOG("Error: digit %" PRIu8 " larger than maximum"
			    " (b=%" PRIu8 "\n", a[i], b);
			return -1;
		}
		sum += a[i];
		ret += p * (int64_t)a[i];
	}

	if ((sum + a[0]) % b != 0) {
		LOG("Error: digits do not have sum zero modulo b\n");
		return -1;
	}

	return ret;
}

STATIC void
sumzerotodigits(int64_t d, size_t n, uint8_t b, uint8_t a[n])
{
	uint8_t sum;
	size_t i;

	if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) {
		LOG("Error: won't compute 'digits' for n=%zu and b=%" PRIu8
		    " (use n=8 b=3 or n=12 b=2)\n", n, b);
		goto sumzerotodigits_error;
	}

	for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) {
		a[i] = (uint8_t)(d % (int64_t)b);
		sum += a[i];
	}
	a[0] = (b - (sum % b)) % b;

	return;

sumzerotodigits_error:
	memset(a, UINT8_ERROR, n);
}

STATIC double
intpow(double b, uint64_t e)
{
	double r;

	if (e == 0)
		return 1;

	r = intpow(b, e/2);

	return e % 2 == 0 ? r * r : b * r * r;
}

Generated with cgit - Back to sebastiano.tronto.net