nx_theorems3_test.nx source
↩ module page · 141 lines · 5669 B
1// nx_theorems3_test.nx -- batch 3 verification.
2
3import "syscalls.nx"
4import "nx_qed_freek.nx"
5
6func main() -> i64 {
7 // #26 Category composition associativity on {0,1,2}.
8 let f: *i64 = (sys_mmap(24)) as *i64
9 let g: *i64 = (sys_mmap(24)) as *i64
10 let h: *i64 = (sys_mmap(24)) as *i64
11 f[0] = 1; f[1] = 2; f[2] = 0
12 g[0] = 2; g[1] = 0; g[2] = 1
13 h[0] = 0; h[1] = 1; h[2] = 2
14 if nx_th_cat_assoc_check(h, g, f, 3) != 1 { return 26 }
15
16 // #27 Mobius mu(1)=1, mu(2)=-1, mu(4)=0 (4 has 2^2), mu(6)=1 (2*3), mu(30)=-1 (2*3*5).
17 if nx_th_mobius(1) != 1 { return 27 }
18 if nx_th_mobius(2) != -1 { return 28 }
19 if nx_th_mobius(4) != 0 { return 29 }
20 if nx_th_mobius(6) != 1 { return 30 }
21 if nx_th_mobius(30) != -1 { return 31 }
22
23 // #28 Stirling S(4,2)=7, S(5,3)=25, S(0,0)=1.
24 if nx_th_stirling2(0, 0) != 1 { return 32 }
25 if nx_th_stirling2(4, 2) != 7 { return 33 }
26 if nx_th_stirling2(5, 3) != 25 { return 34 }
27
28 // #29 Catalan C_0=1, C_3=5, C_5=42.
29 if nx_th_catalan(0) != 1 { return 35 }
30 if nx_th_catalan(3) != 5 { return 36 }
31 if nx_th_catalan(5) != 42 { return 37 }
32
33 // #30 Bell B_0=1, B_3=5, B_4=15, B_5=52.
34 if nx_th_bell(0) != 1 { return 38 }
35 if nx_th_bell(3) != 5 { return 39 }
36 if nx_th_bell(4) != 15 { return 40 }
37 if nx_th_bell(5) != 52 { return 41 }
38
39 // #31 Fibonacci F_0=0, F_1=1, F_10=55, F_20=6765.
40 if nx_th_fibonacci(0) != 0 { return 42 }
41 if nx_th_fibonacci(10) != 55 { return 43 }
42 if nx_th_fibonacci(20) != 6765 { return 44 }
43
44 // #32 Lucas L_0=2, L_1=1, L_10=123.
45 if nx_th_lucas(0) != 2 { return 45 }
46 if nx_th_lucas(1) != 1 { return 46 }
47 if nx_th_lucas(10) != 123 { return 47 }
48
49 // #33 Bernoulli B_0=1 (PPB=1e9), B_2 ~= 166666667.
50 if nx_th_bernoulli_ppb(0) != 1000000000 { return 48 }
51 if nx_th_bernoulli_ppb(2) != 166666667 { return 49 }
52
53 // #34 p-adic v_2(8)=3, v_3(81)=4, v_5(75)=2, v_7(98)=1.
54 if nx_th_p_adic_valuation(2, 8) != 3 { return 50 }
55 if nx_th_p_adic_valuation(3, 81) != 4 { return 51 }
56 if nx_th_p_adic_valuation(5, 75) != 2 { return 52 }
57 if nx_th_p_adic_valuation(7, 98) != 1 { return 53 }
58
59 // #35 Continued fraction 415 / 93 = [4; 2, 6, 7] (Aryabhata).
60 let out: *i64 = (sys_mmap(40)) as *i64
61 let cnt: i64 = nx_th_cf_expand(415, 93, out, 5)
62 if cnt != 4 { return 54 }
63 if out[0] != 4 { return 55 }
64 if out[1] != 2 { return 56 }
65 if out[2] != 6 { return 57 }
66 if out[3] != 7 { return 58 }
67
68 // #38 Quaternion -- i * j = k.
69 let qi: *Quaternion = nx_th_quat_alloc()
70 let qj: *Quaternion = nx_th_quat_alloc()
71 let qres: *Quaternion = nx_th_quat_alloc()
72 qi.x = 1
73 qj.y = 1
74 nx_th_quat_mul(qi, qj, qres)
75 if qres.z != 1 { return 60 }
76 if qres.w != 0 { return 61 }
77 if qres.x != 0 { return 62 }
78 if qres.y != 0 { return 63 }
79 // i^2 = -1
80 let qi2: *Quaternion = nx_th_quat_alloc()
81 nx_th_quat_mul(qi, qi, qi2)
82 if qi2.w != -1 { return 64 }
83 if qi2.x != 0 { return 65 }
84
85 // #39 Tropical -- min(3, 7) = 3 ; tropical_mul(3, 7) = 10.
86 if nx_th_tropical_add(3, 7) != 3 { return 70 }
87 if nx_th_tropical_mul(3, 7) != 10 { return 71 }
88 if nx_th_tropical_add(NX_TH_TROPICAL_INF, 5) != 5 { return 72 }
89
90 // #40 Cayley-Hamilton 2x2. [[1,2],[3,4]] -- should satisfy.
91 if nx_th_cayley_hamilton_2x2_check(1, 2, 3, 4) != 1 { return 73 }
92 if nx_th_cayley_hamilton_2x2_check(7, -3, 2, 5) != 1 { return 74 }
93
94 // #42 Heisenberg -- product 0.3 fails; 0.5 passes (>=0.25).
95 if nx_th_heisenberg_check_ppb(300000000, 1000000000) != 1 { return 80 }
96 if nx_th_heisenberg_check_ppb(100000000, 1000000000) != 0 { return 81 }
97
98 // #43 CF convergent of [3; 7, 15, 1] (start of pi).
99 let cf_pi: *i64 = (sys_mmap(32)) as *i64
100 cf_pi[0] = 3; cf_pi[1] = 7; cf_pi[2] = 15; cf_pi[3] = 1
101 let p_out: *i64 = (sys_mmap(8)) as *i64
102 let q_out: *i64 = (sys_mmap(8)) as *i64
103 // 0th convergent: p/q = 3/1
104 nx_th_cf_convergent(cf_pi, 0, p_out, q_out)
105 if p_out[0] != 3 { return 90 }
106 if q_out[0] != 1 { return 91 }
107 // 1st convergent: 22/7
108 nx_th_cf_convergent(cf_pi, 1, p_out, q_out)
109 if p_out[0] != 22 { return 92 }
110 if q_out[0] != 7 { return 93 }
111 // 3rd convergent: 355/113 (famous pi approximation)
112 nx_th_cf_convergent(cf_pi, 3, p_out, q_out)
113 if p_out[0] != 355 { return 94 }
114 if q_out[0] != 113 { return 95 }
115
116 // #45 Spectral discriminant 2x2: [[2,1],[1,2]] tr=4 det=3 disc=4 (lambdas 1,3).
117 if nx_th_spectral_disc_2x2(2, 1, 1, 2) != 4 { return 100 }
118
119 // #46 Pell: 2^2 - 3 * 1^2 = 1. Wait that's 4 - 3 = 1. Yes 1 -- pell with D=3.
120 if nx_th_pell_check(2, 1, 3) != 1 { return 110 }
121 // Fundamental solution for D=2 is (3, 2): 9 - 8 = 1.
122 if nx_th_pell_check(3, 2, 2) != 1 { return 111 }
123 // (5, 2) doesn't solve any standard Pell.
124 if nx_th_pell_check(5, 2, 3) != 0 { return 112 }
125
126 // #48 Hamming distance.
127 if nx_th_hamming_distance(0xFF, 0x00) != 8 { return 120 }
128 if nx_th_hamming_distance(0xAA, 0x55) != 8 { return 121 }
129 if nx_th_hamming_distance(0x01, 0x03) != 1 { return 122 }
130
131 // #49 Catalan via Segner: C_5 = 42 (same as direct).
132 if nx_th_catalan_segner(5) != 42 { return 130 }
133 if nx_th_catalan_segner(7) != 429 { return 131 }
134
135 // #50 Modular inverse: 3 * 7 = 21 = 20 + 1 mod 20. So inv(3 mod 20) = 7.
136 if nx_th_mod_inverse(3, 20) != 7 { return 140 }
137 // inv(2 mod 11) = 6 since 12 mod 11 = 1.
138 if nx_th_mod_inverse(2, 11) != 6 { return 141 }
139
140 return 0
141}