// Hyperbolic Rogue -- Field Quotient geometry // Copyright (C) 2011-2018 Zeno Rogue, see 'hyper.cpp' for details /** \file fieldpattern.cpp * \brief Field Quotient geometry */ #include "hyper.h" #if CAP_FIELD namespace hr { EX namespace fieldpattern { int limitsq = 10; int limitp = 10000; int limitv = 100000; #if HDR #define currfp fieldpattern::getcurrfp() struct primeinfo { int p; int cells; bool squared; }; struct fgeomextra { eGeometry base; vector primes; vector dualval; int current_prime_id; fgeomextra(eGeometry b, int i) : base(b), current_prime_id(i) {} }; #endif EX bool isprime(int n) { for(int k=2; k, MAXMDIM> { bool operator == (const matrix& B) const { for(int i=0; i= Prime && tx % Prime) neasy++; if(ty >= Prime && ty % Prime) neasy++; int x[2], y[2], z[3]; for(int i=0; i<3; i++) z[i] = 0; for(int i=0; i<2; i++) x[i] = tx%Prime, tx /= Prime; for(int i=0; i<2; i++) y[i] = ty%Prime, ty /= Prime; for(int i=0; i<2; i++) for(int j=0; j<2; j++) z[i+j] = (z[i+j] + x[i] * y[j]) % Prime; z[0] += z[2] * wsquare; return m(z[0]) + Prime * m(z[1]); #endif } int sqr(int x) { return mul(x,x); } matrix mmul(const matrix& A, const matrix& B) { matrix res; for(int i=0; i matcode; vector matrices; vector qpaths; vector qcoords; // S7 in 2D, but e.g. 4 for a 3D cube int rotations; // S7 in 2D, but e.g. 24 for a 3D cube int local_group; // Id: Identity // R : rotate by 1/rotations of the full circle // P : make a step and turn backwards // X : in 3-dim, turn by 90 degrees matrix Id, R, P, X; matrix strtomatrix(string s) { matrix res = Id; matrix m = Id; for(int i=isize(s)-1; i>=0; i--) if(s[i] == 'R') res = mmul(R, res); else if (s[i] == 'P') res = mmul(P, res); else if (s[i] == 'x') { m[0][0] = -1; res = mmul(m, res); m[0][0] = +1; } else if (s[i] == 'y') { m[1][1] = -1; res = mmul(m, res); m[1][1] = +1; } else if (s[i] == 'z') { m[2][2] = -1; res = mmul(m, res); m[2][2] = +1; } return res; } void addas(const matrix& M, int i) { if(!matcode.count(M)) { matcode[M] = i; for(int j=0; j connections; vector inverses; // NYI in 3D // 2D only vector rrf; // rrf[i] equals gmul(i, rotations-1) vector rpf; // rpf[i] equals gmul(i, rotations) matrix mpow(matrix M, int N) { while((N&1) == 0) N >>= 1, M = mmul(M, M); matrix res = M; N >>= 1; while(N) { M = mmul(M,M); if(N&1) res = mmul(res, M); N >>= 1; } return res; } int gmul(int a, int b) { return matcode[mmul(matrices[a], matrices[b])]; } int gpow(int a, int N) { return matcode[mpow(matrices[a], N)]; } pair gmul(pair a, int b) { return make_pair(gmul(a.first,b), a.second); } int order(const matrix& M); string decodepath(int i) { string s; while(i) { if(i % S7) i--, s += 'R'; else i = connections[i], s += 'P'; } return s; } int orderstats(); int cs, sn, ch, sh; int solve(); void build(); static const int MAXDIST = 120; vector disthep; vector disthex; vector distwall, distriver, distwall2, distriverleft, distriverright, distflower; int distflower0; vector markers; int getdist(pair a, vector& dists); int getdist(pair a, pair b); int dijkstra(vector& dists, vector indist[MAXDIST]); void analyze(); int maxdist, otherpole, circrad, wallid, wallorder, riverid; bool easy(int i) { return i < Prime || !(i % Prime); } // 11 * 25 // (1+z+z^3) * (1+z^3+z^4) == // 1+z+z^7 == 1+z+z^2(z^5) == 1+z+z^2(1+z^2) = 1+z+z^2+z^4 void init(int p) { Prime = p; if(solve()) { printf("error: could not solve the fieldpattern\n"); exit(1); } build(); } fpattern(int p) { force_hash = 0; #if CAP_THREAD dis = nullptr; #endif if(!p) return; init(p); } void findsubpath(); vector generate_isometries(); bool check_order(matrix M, int req); unsigned compute_hash(); void set_field(int p, int sq); #if MAXMDIM >= 4 // general 4D vector fullv; void add1(const matrix& M); void add1(const matrix& M, const transmatrix& Full); vector generate_isometries3(); int solve3(); bool generate_all3(); #if CAP_THREAD struct discovery *dis; #endif #endif }; #if CAP_THREAD struct discovery { fpattern experiment; std::shared_ptr discoverer; std::mutex lock; std::condition_variable cv; bool is_suspended; bool stop_it; map > hashes_found; discovery() : experiment(0) { is_suspended = false; stop_it = false; experiment.dis = this; experiment.Prime = experiment.Field = experiment.wsquare = 0; } void activate(); void suspend(); void check_suspend(); void schedule_destruction(); void discovered(); ~discovery(); }; #endif #endif bool fpattern::check_order(matrix M, int req) { matrix P = M; for(int i=1; i fpattern::generate_isometries() { matrix T = Id; int low = wsquare ? 1-Prime : 0; vector res; auto colprod = [&] (int a, int b) { return add(add(mul(T[0][a], T[0][b]), mul(T[1][a], T[1][b])), mul(T[2][a], T[2][b])); }; for(T[0][0]=low; T[0][0]= 4 vector fpattern::generate_isometries3() { matrix T = Id; int low = wsquare ? 1-Prime : 0; vector res; auto colprod = [&] (int a, int b) { return add(add(mul(T[0][a], T[0][b]), mul(T[1][a], T[1][b])), sub(mul(T[2][a], T[2][b]), mul(T[3][a], T[3][b]))); }; auto rowcol = [&] (int a, int b) { return add(add(mul(T[a][0], T[0][b]), mul(T[a][1], T[1][b])), add(mul(T[a][2], T[2][b]), mul(T[a][3], T[3][b]))); }; for(T[0][0]=low; T[0][0]check_suspend(); if(dis && dis->stop_it) return res; #endif for(T[0][2]=low; T[0][2] limitp) return res; } return res; } void fpattern::add1(const matrix& M) { if(!matcode.count(M)) { int i = matrices.size(); matcode[M] = i, matrices.push_back(M); } } void fpattern::add1(const matrix& M, const transmatrix& Full) { if(!matcode.count(M)) { int i = matrices.size(); matcode[M] = i, matrices.push_back(M), fullv.push_back(Full); } } #endif map hash_found; unsigned fpattern::compute_hash() { unsigned hashv = 0; int iR = matcode[R]; int iP = matcode[P]; int iX = matcode[X]; for(int i=0; i= 4 bool fpattern::generate_all3() { reg3::generate_fulls(); matrices.clear(); matcode.clear(); add1(Id); fullv = {hr::Id}; for(int i=0; i= limitv) { println(hlog, "limitv exceeded"); return false; } } unsigned hashv = compute_hash(); DEBB(DF_FIELD, ("all = ", isize(matrices), "/", local_group, " = ", isize(matrices) / local_group, " hash = ", hashv, " count = ", ++hash_found[hashv])); return true; } int fpattern::solve3() { reg3::construct_relations(); DEBB(DF_FIELD, ("generating isometries for ", Field)); auto iso3 = generate_isometries(); auto iso4 = generate_isometries3(); int cmb = 0; int N = isize(cgi.rels); vector fails(N); vector possible_P, possible_X, possible_R; for(auto& M: iso3) { if(check_order(M, 2)) possible_X.push_back(M); if(check_order(M, cgi.r_order)) possible_R.push_back(M); } for(auto& M: iso4) if(check_order(M, 2)) possible_P.push_back(M); DEBB(DF_FIELD, ("field = ", Field, " #P = ", isize(possible_P), " #X = ", isize(possible_X), " #R = ", isize(possible_R), " r_order = ", cgi.r_order, " xp_order = ", cgi.xp_order)); for(auto& xX: possible_X) for(auto& xP: possible_P) if(check_order(mmul(xP, xX), cgi.xp_order)) for(auto& xR: possible_R) if(check_order(mmul(xR, xX), cgi.rx_order)) { // if(xR[0][0] == 1 && xR[0][1] == 0) #if CAP_THREAD if(dis) dis->check_suspend(); if(dis && dis->stop_it) return 0; #endif auto by = [&] (char ch) -> matrix& { return ch == 'X' ? xX : ch == 'R' ? xR : xP; }; for(int i=0; idiscovered(); continue; } #endif if(force_hash && compute_hash() != force_hash) continue; cmb++; goto ok; bad: ; } ok: DEBB(DF_FIELD, ("cmb = ", cmb, " for field = ", Field)); for(int i=0; i a, vector& dists) { if(!a.second) return dists[a.first]; int m = MAXDIST; int ma = dists[a.first]; int mb = dists[connections[btspin(a.first, 3)]]; int mc = dists[connections[btspin(a.first, 4)]]; m = min(m, 1 + ma); m = min(m, 1 + mb); m = min(m, 1 + mc); if(m <= 2 && ma+mb+mc <= m*3-2) return m-1; // special case m = min(m, 2 + dists[connections[btspin(a.first, 2)]]); m = min(m, 2 + dists[connections[btspin(a.first, 5)]]); m = min(m, 2 + dists[connections[btspin(connections[btspin(a.first, 3)], 5)]]); return m; } int fpattern::getdist(pair a, pair b) { if(a.first == b.first) return a.second == b.second ? 0 : 1; if(b.first) a.first = gmul(a.first, inverses[b.first]), b.first = 0; return getdist(a, b.second ? disthex : disthep); } int fpattern::dijkstra(vector& dists, vector indist[MAXDIST]) { int N = isize(matrices); dists.resize(N); for(int i=0; i indist[MAXDIST]; indist[0].push_back(0); int md0 = dijkstra(disthep, indist); if(MWDIM == 4) return; indist[1].push_back(0); indist[1].push_back(connections[3]); indist[1].push_back(connections[4]); indist[2].push_back(connections[btspin(connections[3], 5)]); indist[2].push_back(connections[2]); indist[2].push_back(connections[5]); int md1 = dijkstra(disthex, indist); maxdist = max(md0, md1); otherpole = 0; for(int i=0; i disthep[otherpole]) otherpole = i; // for(int r=0; r fgeomextras = { fgeomextra(gNormal, 4), fgeomextra(gOctagon, 1), fgeomextra(g45, 1), fgeomextra(g46, 5), fgeomextra(g47, 1), fgeomextra(gSchmutzM3, 0), /* fgeomextra(gSphere, 0), fgeomextra(gSmallSphere, 0), -> does not find the prime fgeomextra(gEuclid, 0), fgeomextra(gEuclidSquare, 0), fgeomextra(gTinySphere, 0) */ }; EX int current_extra = 0; EX void nextPrime(fgeomextra& ex) { dynamicval g(geometry, ex.base); int nextprime; if(isize(ex.primes)) nextprime = ex.primes.back().p + 1; else nextprime = 2; while(true) { fieldpattern::fpattern fp(0); fp.Prime = nextprime; if(fp.solve() == 0) { fp.build(); int cells = fp.matrices.size() / S7; ex.primes.emplace_back(primeinfo{nextprime, cells, (bool) fp.wsquare}); ex.dualval.emplace_back(fp.dual); break; } nextprime++; } } EX void nextPrimes(fgeomextra& ex) { while(isize(ex.primes) < 6) nextPrime(ex); } EX void enableFieldChange() { fgeomextra& gxcur = fgeomextras[current_extra]; fieldpattern::quotient_field_changed = true; nextPrimes(gxcur); dynamicval g(geometry, gFieldQuotient); ginf[geometry].sides = ginf[gxcur.base].sides; ginf[geometry].vertex = ginf[gxcur.base].vertex; ginf[geometry].distlimit = ginf[gxcur.base].distlimit; ginf[geometry].tiling_name = ginf[gxcur.base].tiling_name; fieldpattern::current_quotient_field.init(gxcur.primes[gxcur.current_prime_id].p); } EX void field_from_current() { auto& go = ginf[geometry]; dynamicval g(geometry, gFieldQuotient); auto& gg = ginf[geometry]; gg.sides = go.sides; gg.vertex = go.vertex; gg.distlimit = go.distlimit; gg.tiling_name = go.tiling_name; gg.flags = go.flags | qANYQ | qFIELD | qBOUNDED; gg.g = go.g; gg.default_variation = go.default_variation; fieldpattern::quotient_field_changed = true; } #if CAP_THREAD EX map discoveries; void discovery::activate() { if(!discoverer) { discoverer = std::make_shared ( [this] { for(int p=2; p<100; p++) { experiment.Prime = p; experiment.solve(); if(stop_it) break; } }); } if(is_suspended) { if(1) { std::unique_lock lk(lock); is_suspended = false; } cv.notify_one(); } } void discovery::discovered() { std::unique_lock lk(lock); auto& e = experiment; hashes_found[e.compute_hash()] = make_tuple(e.Prime, e.wsquare, e.R, e.P, e.X, isize(e.matrices) / e.local_group); } void discovery::suspend() { is_suspended = true; } void discovery::check_suspend() { std::unique_lock lk(lock); if(is_suspended) cv.wait(lk, [this] { return !is_suspended; }); } void discovery::schedule_destruction() { stop_it = true; } discovery::~discovery() { schedule_destruction(); if(discoverer) discoverer->join(); } #endif int hk = #if CAP_THREAD + addHook(on_geometry_change, 100, [] { for(auto& d:discoveries) if(!d.second.is_suspended) d.second.suspend(); }) + addHook(final_cleanup, 100, [] { for(auto& d:discoveries) { d.second.schedule_destruction(); if(d.second.is_suspended) d.second.activate(); } discoveries.clear(); }) #endif #if CAP_COMMANDLINE + addHook(hooks_args, 0, [] { using namespace arg; if(0) ; else if(argis("-q3-limitsq")) { shift(); limitsq = argi(); } else if(argis("-q3-limitp")) { shift(); limitp = argi(); } else if(argis("-q3-limitv")) { shift(); limitv = argi(); } else return 1; return 0; }) #endif + 0; EX purehookset on_geometry_change; EX int field_celldistance(cell *c1, cell *c2) { if(geometry != gFieldQuotient) return DISTANCE_UNKNOWN; if(GOLDBERG) return DISTANCE_UNKNOWN; auto v1 =fieldpattern::fieldval(c1); auto v2 =fieldpattern::fieldval(c2); int d = currfp.getdist(v1, v2); return d; } EX } #define currfp fieldpattern::getcurrfp() EX int currfp_gmul(int a, int b) { return currfp.gmul(a,b); } EX int currfp_inverses(int i) { return currfp.inverses[i]; } EX int currfp_distwall(int i) { return currfp.distwall[i]; } EX int currfp_n() { return isize(currfp.matrices); } EX int currfp_get_R() { return currfp.matcode[currfp.R]; } EX int currfp_get_P() { return currfp.matcode[currfp.P]; } EX int currfp_get_X() { return currfp.matcode[currfp.X]; } EX void hread_fpattern(hstream& hs, fieldpattern::fpattern& fp) { hread(hs, fp.Prime); hread(hs, fp.wsquare); hread(hs, fp.P); hread(hs, fp.R); hread(hs, fp.X); fp.set_field(fp.Prime, fp.wsquare); #if MAXMDIM >= 4 fp.generate_all3(); #endif } EX void hwrite_fpattern(hstream& hs, fieldpattern::fpattern& fp) { hwrite(hs, fp.Prime); hwrite(hs, fp.wsquare); hwrite(hs, fp.P); hwrite(hs, fp.R); hwrite(hs, fp.X); } } #endif