Permutation matrices research.
1#include "external.hpp"
2#include "lib/lib.hpp"
3#include "lib/fractal.hpp"
4#include "test/test.hpp"
5#include <algorithm>
6#include <cassert>
7#include <format>
8#include <fstream>
9#include <iomanip>
10#include <iostream>
11#include <ranges>
12
13void static foo() noexcept {
14 for (auto p : vi::iota(5, 100) | vi::filter(ext::is_prime)) {
15 let roots = primitive_roots((UInt(p)));
16 let first_root = roots.front();
17 std::cout << std::format("${}$, ${}, ", p, first_root);
18
19 var space = "";
20 for (auto root : roots | vi::drop(1)) {
21 let i = find_index(seq(p, root), first_root) + 1;
22 std::cout << std::format("{}{}^{}", space, first_root, i);
23 space = ", ";
24 }
25
26 std::cout << "$\n";
27 }
28}
29
30struct Node {
31 Vec<U8> indices;
32 Size x;
33 Size y;
34 Size left_wall = 0;
35
36 fn children() const -> Vec<Node> {
37 let level = indices.size();
38 Vec<Node> v;
39 v.reserve(level + 1);
40 for (Size i = 1; i <= level + 1; ++i) {
41 var next_indices = indices;
42 next_indices.push_back(i - 1);
43 std::cout << "current_indices: ";
44 for (let e : indices) std::cout << e << ' ';
45 std::cout << std::endl;
46 std::cout << "next_indices: ";
47 for (let e : next_indices) std::cout << e << ' ';
48 std::cout << std::endl;
49
50 let tmp_x = 2 * (x - left_wall);
51 let next_y = y + 10;
52 let next_x = (2 * i - 1) * tmp_x / ((level + 1) * 2) + left_wall;
53 let next_wall = (2 * (i - 1)) * tmp_x / ((level + 1) * 2) + left_wall;
54 v.push_back(Node{next_indices, next_x, next_y, next_wall});
55 }
56 return v;
57 }
58};
59
60fn main() noexcept -> int {
61#ifndef TEST
62 let WIDTH = 800;
63 let HEIGHT = 800;
64 static var canvas = Canvas<WIDTH, HEIGHT>();
65 canvas.fill(RGB(0xEE, 0xEE, 0xEE).to_hsv());
66 let r = RGB(0xFF, 0x00, 0x00);
67 let g = RGB(0x00, 0xFF, 0x00);
68 let b = RGB(0x00, 0x00, 0xFF);
69 let gray = RGB(0x22, 0x22, 0x22);
70
71 let a = PixelLocation(WIDTH / 2, 20);
72 var v = Vec<U8>();
73 v.push_back(0);
74 let c1 = Node{v, WIDTH / 2, 20};
75 var counter = 0;
76 var color = r;
77 let radius = 2;
78 canvas.circle(a, radius, g.to_hsv());
79 for (let& c2 : c1.children()) {
80 // for (let& c3 : c2.children()) {
81 // for (let& c4 : c3.children()) {
82 // for (let& c5 : c4.children()) {
83 /*
84 for (let& c6 : c5.children()) {
85 for (let& c7 : c6.children()) {
86 let loc = PixelLocation{c7.x, c7.y};
87 if (is_costas(from_indices(c7.indices)))
88 canvas.circle(loc, radius, g.to_hsv());
89 else canvas.circle(loc, radius, r.to_hsv());
90 }
91 let loc = PixelLocation{c6.x, c6.y};
92 canvas.circle(loc, radius, color.to_hsv());
93 }
94 */
95 // let loc = PixelLocation{c5.x, c5.y};
96 // if (is_costas(from_indices(c5.indices)))
97 // canvas.circle(loc, radius, g.to_hsv());
98 // else canvas.circle(loc, radius, r.to_hsv());
99 // }
100 // let loc = PixelLocation{c4.x, c4.y};
101 // if (is_costas(from_indices(c4.indices)))
102 // canvas.circle(loc, radius, g.to_hsv());
103 // else canvas.circle(loc, radius, r.to_hsv());
104 // }
105 // let loc = PixelLocation{c3.x, c3.y};
106 // if (is_costas(from_indices(c3.indices)))
107 // canvas.circle(loc, radius, g.to_hsv());
108 // else canvas.circle(loc, radius, r.to_hsv());
109 // }
110 let loc = PixelLocation{c2.x, c2.y};
111 let lst = from_indices(c2.indices);
112 std::cout << "indices: ";
113 for (let e : c2.indices) std::cout << e << ' ';
114 std::cout << std::endl;
115 for (let e : lst) std::cout << e << ' ';
116 std::cout << std::endl;
117 if (is_costas(lst))
118 canvas.circle(loc, radius, g.to_hsv());
119 else canvas.circle(loc, radius, r.to_hsv());
120 }
121 canvas.save_to_ppm("test.ppm");
122#else
123 let s1 = seq(11, 2);
124 Vec<UInt> s2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
125 "2 is a primitive root of 11"_test = is_primitive_root(11, 2);
126 "test permutation matrix for 11 for alpha 2"_test =
127 is_permutation_matrix(s1);
128 "test permutation matrix for 11 with increasing numbers"_test =
129 not is_permutation_matrix(s2);
130#endif
131
132 return 0;
133}