summaryrefslogtreecommitdiff
path: root/test/math/berlekampMassey.cpp
blob: a9d57092779416e902e1c5a819f3beb261b74874 (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
#include "../util.h"
#include <math/modPowIterativ.cpp>
#include <math/berlekampMassey.cpp>

struct RandomRecurence {
	vector<ll> f, c, cache;
	RandomRecurence(int n) : f(Random::integers<ll>(n, 0, mod)), c(Random::integers<ll>(n, 0, mod)), cache(f) {}
	RandomRecurence(const vector<ll>& f_, const vector<ll>& c_) : c(c_), cache(f_) {
		if (cache.size() < c.size()) cerr << "wrong size" << FAIL;
		cache.resize(c.size());
		f = cache;
	}

	ll operator()(ll k){
		while (ssize(cache) <= k) {
			ll cur = 0;
			for (ll i = 0; i < ssize(c); i++) {
				cur += (c[i] * cache[ssize(cache) - i - 1]) % mod;
			}
			cur %= mod;
			cache.push_back(cur);
		}
		return cache[k];
	}
};

void stress_test() {
	int queries = 0;
	for (int i = 0; i < 50'000; i++) {
		int n = Random::integer<int>(1, 10);
		RandomRecurence expected(n);

		ll k = Random::integer<ll>(2*n, 100);
		vector<ll> s(k);
		for (ll j = 0; j < k; j++) s[j] = expected(j);

		auto res = BerlekampMassey(s);
		RandomRecurence got(s, res);

		for (ll j = 0; j < 3*k; j++) {
			if (got(j) != expected(j)) cerr << "got: " << got(j) << ", expected: " << expected(j) << FAIL;
		}

		queries += k;
	}
	cerr << "tested random queries: " << queries << endl;
}

constexpr int N = 5'000;
void performance_test() {
	timer t;
	RandomRecurence f(N);
	f(2*N);
	t.start();
	auto res = BerlekampMassey(f.cache);
	t.stop();
	hash_t hash = 0;
	for (ll x : res) hash += x;
	if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}


int main() {
	stress_test();
	performance_test();
}