File size: 4,641 Bytes
b6f1c7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Independently implemented exact finite audit. No floating point or Python.
// Build: g++ -std=c++20 -O2 cpp/exact_atlas.cpp -o exact_atlas
// Run: ./exact_atlas results/exact_atlas.csv > results/cpp_report.json
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <numeric>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
using Row=std::array<int,2>;
using Mat=std::array<Row,3>;
long long checks=0,states=0,energy_states=0;
void check(bool b){++checks;if(!b)throw std::runtime_error("exact certificate failed at check "+std::to_string(checks));}
int det(Row a,Row b){return a[0]*b[1]-a[1]*b[0];}
int mod(int a,int q){int b=a%q;return b<0?b+q:b;}
std::vector<int> supports(const Mat&a){
 std::set<int>s;
 for(const auto&r:a){int bits=0;for(int i=0;i<3;++i)if(det(r,a[i]))bits|=1<<i;if(bits)s.insert(bits);}
 return {s.begin(),s.end()};
}
std::pair<int,int> cogirth(const std::vector<int>&s,std::array<int,3>w){
 int v=100000000,count=0;
 for(int mask:s){int sum=0;for(int i=0;i<3;++i)if(mask&(1<<i))sum+=w[i];
 if(sum<v){v=sum;count=1;}else if(sum==v)++count;}
 return {v,count};
}
int sine_energy(int q,int r){
 if(q==2){int a[]={0,4};return a[r];}
 if(q==3){int a[]={0,3,3};return a[r];}
 if(q==4){int a[]={0,2,4,2};return a[r];}
 if(q==6){int a[]={0,1,3,4,3,1};return a[r];}
 throw std::runtime_error("unsupported exact alphabet");
}
std::pair<int,int> minimum(const Mat&a,std::array<int,3>w,int q){
 int low=100000000,count=0;
 for(int x=0;x<q;++x)for(int y=0;y<q;++y){if(x==0&&y==0)continue;
 int v=0;for(int i=0;i<3;++i)v+=w[i]*sine_energy(q,mod(a[i][0]*x+a[i][1]*y,q));++energy_states;
 if(v<low){low=v;count=1;}else if(v==low)++count;}
 return {low,count};
}
int main(int argc,char**argv){try{
 std::ofstream csv;if(argc>1)csv.open(argv[1]);
 if(argc>1&&!csv)throw std::runtime_error("cannot open CSV output");
 if(csv)csv<<"a00,a01,a10,a11,a20,a21,Q,safe,unimodular,cogirth_unit,exact_squared_distance_unit\n";
 std::vector<Row>rows;for(int x=-2;x<=2;++x)for(int y=-2;y<=2;++y)if(x||y)rows.push_back({x,y});
 int candidates=0,faithful=0,uni=0,safe_count=0,unsafe_count=0,unsafe_exact=0;
 for(int i=0;i<int(rows.size());++i)for(int j=i+1;j<int(rows.size());++j)for(int k=j+1;k<int(rows.size());++k){
 ++candidates;Mat a={rows[i],rows[j],rows[k]};
 std::array<int,3>dets={det(a[0],a[1]),det(a[0],a[2]),det(a[1],a[2])};
 int g=0;bool u=true;for(int z:dets){g=std::gcd(g,std::abs(z));if(std::abs(z)>1)u=false;}
 if(g!=1) { continue; }
 ++faithful; if(u) { ++uni; }
 auto supp=supports(a); check(!supp.empty());
 for(int q=2;q<=11;++q){
  bool safe=true;for(int z:dets)if(z&&std::gcd(std::abs(z),q)!=1)safe=false;
  if(safe)++safe_count;else ++unsafe_count;
  bool alias=false;
  for(int x=0;x<q;++x)for(int y=0;y<q;++y){if(x==0&&y==0)continue;
   ++states;int zero=0;for(int r=0;r<3;++r)if(mod(a[r][0]*x+a[r][1]*y,q)==0)zero|=1<<r;
   check(zero!=7); // Faithfulness on every composite alphabet too.
   for(int r=0;r<3;++r)for(int s=r+1;s<3;++s)if((zero&(1<<r))&&(zero&(1<<s))&&det(a[r],a[s]))alias=true;
  }
  check(safe==!alias);
  int exact=-1;
  if(q==2||q==3||q==4||q==6){
   int c=sine_energy(q,1);
   for(std::array<int,3>w:std::vector<std::array<int,3>>{{1,1,1},{1,2,3},{3,1,2}}){
    auto lam=cogirth(supp,w),answer=minimum(a,w,q);
    if(w==std::array<int,3>{1,1,1})exact=answer.first;
    if(safe)check(answer.first>=c*lam.first);
    if(u){check(answer.first==c*lam.first);check(answer.second==(q==2?1:2)*lam.second);}
   }
   if(!safe){
    bool done=false;
    for(int r=0;r<3&&!done;++r)for(int s=r+1;s<3&&!done;++s){
     int z=det(a[r],a[s]);if(!z||std::gcd(std::abs(z),q)==1)continue;
     std::array<int,3>w={1,1,1};w[r]=w[s]=4*q*q;
     auto lam=cogirth(supp,w),ans=minimum(a,w,q);
     check(ans.first<c*lam.first);done=true;++unsafe_exact;
    }check(done);
   }
  }
  if(csv){for(auto row:a)csv<<row[0]<<','<<row[1]<<',';csv<<q<<','<<safe<<','<<u<<','<<cogirth(supp,{1,1,1}).first<<','<<exact<<'\n';}
 }
 }
 check(candidates==2024);
 std::cout<<"{\n  \"status\": \"passed\",\n  \"arithmetic\": \"integer only\",\n  \"candidate_matrices\": "<<candidates<<",\n  \"primitive_matrices\": "<<faithful<<",\n  \"unimodular_matrices\": "<<uni<<",\n  \"matrix_modulus_cases\": "<<safe_count+unsafe_count<<",\n  \"safe_cases\": "<<safe_count<<",\n  \"unsafe_cases\": "<<unsafe_count<<",\n  \"modular_configurations\": "<<states<<",\n  \"exact_energy_configurations\": "<<energy_states<<",\n  \"constructive_failure_checks\": "<<unsafe_exact<<",\n  \"assertions\": "<<checks<<"\n}\n";
 }catch(const std::exception&e){std::cerr<<e.what()<<'\n';return 1;}return 0;}