@aleph-ai/tinyaleph 1.5.6 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/core/alexander-module.js +1469 -0
  2. package/core/arithmetic-link-kernel.js +1338 -0
  3. package/core/index.js +95 -2
  4. package/examples/01-hello-world.js +69 -0
  5. package/examples/02-basic-hash.js +90 -0
  6. package/examples/02-observer-stack.js +385 -0
  7. package/examples/03-quantum-coin.js +136 -0
  8. package/examples/05-symbolic-resonance.js +146 -0
  9. package/examples/06-symbol-database.js +150 -0
  10. package/examples/07-semantic-inference.js +223 -0
  11. package/examples/08-compound-symbols.js +219 -0
  12. package/examples/README.md +170 -0
  13. package/examples/ai/01-embeddings.js +155 -0
  14. package/examples/ai/02-semantic-memory.js +243 -0
  15. package/examples/ai/03-reasoning.js +243 -0
  16. package/examples/ai/04-knowledge-graph.js +279 -0
  17. package/examples/ai/05-llm-integration.js +333 -0
  18. package/examples/ai/06-agent.js +294 -0
  19. package/examples/ai/07-hybrid-ai.js +223 -0
  20. package/examples/ai/08-entropy-reasoning.js +259 -0
  21. package/examples/ai/09-concept-learning.js +271 -0
  22. package/examples/ai/10-prompt-primes.js +312 -0
  23. package/examples/ai/11-rag.js +332 -0
  24. package/examples/ai/12-neuro-symbolic.js +321 -0
  25. package/examples/ai/README.md +80 -0
  26. package/examples/arithmetic-topology/01-legendre-symbol.js +78 -0
  27. package/examples/arithmetic-topology/02-redei-symbol.js +126 -0
  28. package/examples/arithmetic-topology/03-alk-kuramoto.js +138 -0
  29. package/examples/arithmetic-topology/04-alexander-module.js +117 -0
  30. package/examples/arithmetic-topology/05-signature-memory.js +118 -0
  31. package/examples/arithmetic-topology/README.md +291 -0
  32. package/examples/bioinformatics/01-dna-encoding.js +108 -0
  33. package/examples/bioinformatics/02-central-dogma.js +162 -0
  34. package/examples/bioinformatics/03-protein-folding.js +206 -0
  35. package/examples/bioinformatics/04-dna-computing.js +192 -0
  36. package/examples/bioinformatics/05-molecular-binding.js +209 -0
  37. package/examples/chat.js +105 -0
  38. package/examples/crt-homology/01-residue-encoding.js +87 -0
  39. package/examples/crt-homology/02-birkhoff-attention.js +100 -0
  40. package/examples/crt-homology/03-homology-loss.js +132 -0
  41. package/examples/crt-homology/04-crt-resoformer.js +132 -0
  42. package/examples/crt-homology/README.md +67 -0
  43. package/examples/crypto/01-password-hash.js +210 -0
  44. package/examples/crypto/02-key-derivation.js +210 -0
  45. package/examples/crypto/03-hmac.js +229 -0
  46. package/examples/crypto/04-file-integrity.js +263 -0
  47. package/examples/crypto/05-content-hash.js +263 -0
  48. package/examples/crypto/README.md +99 -0
  49. package/examples/demo-modular.js +223 -0
  50. package/examples/demo-two-layer.js +196 -0
  51. package/examples/discrete/01-integer-sine-table.js +120 -0
  52. package/examples/discrete/02-codebook-tunneling.js +118 -0
  53. package/examples/discrete/03-canonical-fusion.js +135 -0
  54. package/examples/discrete/04-tick-gate.js +139 -0
  55. package/examples/discrete/README.md +142 -0
  56. package/examples/formal-semantics/01-typed-terms.js +156 -0
  57. package/examples/formal-semantics/02-reduction.js +202 -0
  58. package/examples/formal-semantics/03-lambda-translation.js +206 -0
  59. package/examples/formal-semantics/04-enochian-language.js +257 -0
  60. package/examples/formal-semantics/README.md +98 -0
  61. package/examples/math/01-quaternions.js +237 -0
  62. package/examples/math/02-octonions.js +192 -0
  63. package/examples/math/03-prime-factorization.js +215 -0
  64. package/examples/math/04-vector-spaces.js +210 -0
  65. package/examples/math/05-gaussian-primes.js +234 -0
  66. package/examples/math/README.md +93 -0
  67. package/examples/physics/01-oscillator.js +177 -0
  68. package/examples/physics/02-lyapunov.js +201 -0
  69. package/examples/physics/03-collapse.js +183 -0
  70. package/examples/physics/04-kuramoto.js +212 -0
  71. package/examples/physics/05-entropy.js +226 -0
  72. package/examples/physics/05-sync-models.js +298 -0
  73. package/examples/physics/06-primeon-ladder.js +233 -0
  74. package/examples/physics/07-kuramoto-coupled-ladder.js +298 -0
  75. package/examples/physics/README.md +126 -0
  76. package/examples/resonance/01-prime-hilbert-space.js +140 -0
  77. package/examples/resonance/02-prime-resonance-network.js +221 -0
  78. package/examples/resonance/03-resoformer.js +349 -0
  79. package/examples/resonance/04-resoformer-training.js +329 -0
  80. package/examples/resonance/05-language-model.js +484 -0
  81. package/examples/resonance/README.md +238 -0
  82. package/examples/run-examples.js +417 -0
  83. package/examples/scientific/01-single-qubit.js +185 -0
  84. package/examples/scientific/02-two-qubit.js +209 -0
  85. package/examples/scientific/03-quantum-circuits.js +270 -0
  86. package/examples/scientific/04-measurement.js +229 -0
  87. package/examples/scientific/05-algorithms.js +245 -0
  88. package/examples/scientific/06-random.js +225 -0
  89. package/examples/scientific/07-wavefunction.js +192 -0
  90. package/examples/scientific/README.md +118 -0
  91. package/examples/semantic/01-vocabulary.js +186 -0
  92. package/examples/semantic/02-similarity.js +263 -0
  93. package/examples/semantic/03-word-algebra.js +295 -0
  94. package/examples/semantic/04-clustering.js +348 -0
  95. package/examples/semantic/05-classification.js +386 -0
  96. package/examples/semantic/06-dna-encoding.js +228 -0
  97. package/examples/semantic/07-search.js +304 -0
  98. package/examples/semantic/08-qa-system.js +278 -0
  99. package/examples/semantic/README.md +116 -0
  100. package/examples/topology/01-108-invariant.js +81 -0
  101. package/examples/topology/02-trefoil-constants.js +112 -0
  102. package/examples/topology/03-gauge-symmetry.js +112 -0
  103. package/examples/topology/04-free-energy-dynamics.js +124 -0
  104. package/examples/topology/README.md +129 -0
  105. package/index.js +32 -0
  106. package/modular.js +63 -2
  107. package/observer/agency.js +885 -0
  108. package/observer/assays.js +973 -0
  109. package/observer/boundary.js +1155 -0
  110. package/observer/entanglement.js +673 -0
  111. package/observer/hqe.js +1465 -0
  112. package/observer/index.js +158 -0
  113. package/observer/prsc.js +1289 -0
  114. package/observer/safety.js +815 -0
  115. package/observer/smf.js +1015 -0
  116. package/observer/symbolic-smf.js +726 -0
  117. package/observer/symbolic-temporal.js +790 -0
  118. package/observer/temporal.js +669 -0
  119. package/package.json +9 -3
  120. package/physics/alk-kuramoto.js +817 -0
  121. package/physics/index.js +23 -2
@@ -0,0 +1,237 @@
1
+ /**
2
+ * @example Quaternions
3
+ * @description Explore quaternion algebra using hypercomplex numbers
4
+ *
5
+ * Quaternions are 4-dimensional hypercomplex numbers:
6
+ * - q = a + bi + cj + dk
7
+ * - Used for 3D rotations without gimbal lock
8
+ * - Non-commutative multiplication
9
+ */
10
+
11
+ const { Hypercomplex } = require('../../modular');
12
+
13
+ console.log('TinyAleph Quaternion Example');
14
+ console.log('============================\n');
15
+
16
+ // ===========================================
17
+ // QUATERNION BASICS
18
+ // ===========================================
19
+
20
+ console.log('Quaternion Basics:');
21
+ console.log('-'.repeat(50) + '\n');
22
+
23
+ console.log('Quaternions: q = a + bi + cj + dk');
24
+ console.log('Where: i² = j² = k² = ijk = -1\n');
25
+
26
+ // Create quaternions using Hypercomplex with dimension 4
27
+ function createQuaternion(a, b, c, d) {
28
+ var q = Hypercomplex.zero(4);
29
+ q.c[0] = a; // Real part
30
+ q.c[1] = b; // i component
31
+ q.c[2] = c; // j component
32
+ q.c[3] = d; // k component
33
+ return q;
34
+ }
35
+
36
+ function quaternionToString(q) {
37
+ var parts = [];
38
+ if (q.c[0] !== 0) parts.push(q.c[0].toFixed(3));
39
+ if (q.c[1] !== 0) parts.push(q.c[1].toFixed(3) + 'i');
40
+ if (q.c[2] !== 0) parts.push(q.c[2].toFixed(3) + 'j');
41
+ if (q.c[3] !== 0) parts.push(q.c[3].toFixed(3) + 'k');
42
+ return parts.length > 0 ? parts.join(' + ').replace(/\+ -/g, '- ') : '0';
43
+ }
44
+
45
+ var q1 = createQuaternion(1, 2, 3, 4);
46
+ var q2 = createQuaternion(2, -1, 1, 3);
47
+
48
+ console.log('q1 = ' + quaternionToString(q1));
49
+ console.log('q2 = ' + quaternionToString(q2));
50
+
51
+ // ===========================================
52
+ // QUATERNION OPERATIONS
53
+ // ===========================================
54
+
55
+ console.log('\n' + '='.repeat(50));
56
+ console.log('Quaternion Operations:');
57
+ console.log('='.repeat(50) + '\n');
58
+
59
+ // Addition
60
+ function quaternionAdd(q1, q2) {
61
+ var result = Hypercomplex.zero(4);
62
+ for (var i = 0; i < 4; i++) {
63
+ result.c[i] = q1.c[i] + q2.c[i];
64
+ }
65
+ return result;
66
+ }
67
+
68
+ var sum = quaternionAdd(q1, q2);
69
+ console.log('Addition:');
70
+ console.log(' q1 + q2 = ' + quaternionToString(sum));
71
+
72
+ // Quaternion multiplication (Hamilton product)
73
+ function quaternionMul(q1, q2) {
74
+ var a1 = q1.c[0], b1 = q1.c[1], c1 = q1.c[2], d1 = q1.c[3];
75
+ var a2 = q2.c[0], b2 = q2.c[1], c2 = q2.c[2], d2 = q2.c[3];
76
+
77
+ var result = Hypercomplex.zero(4);
78
+ result.c[0] = a1*a2 - b1*b2 - c1*c2 - d1*d2;
79
+ result.c[1] = a1*b2 + b1*a2 + c1*d2 - d1*c2;
80
+ result.c[2] = a1*c2 - b1*d2 + c1*a2 + d1*b2;
81
+ result.c[3] = a1*d2 + b1*c2 - c1*b2 + d1*a2;
82
+
83
+ return result;
84
+ }
85
+
86
+ var prod = quaternionMul(q1, q2);
87
+ console.log('\nMultiplication:');
88
+ console.log(' q1 * q2 = ' + quaternionToString(prod));
89
+
90
+ // Non-commutativity
91
+ var prod2 = quaternionMul(q2, q1);
92
+ console.log(' q2 * q1 = ' + quaternionToString(prod2));
93
+ console.log(' Non-commutative: q1*q2 ≠ q2*q1');
94
+
95
+ // Conjugate
96
+ function quaternionConjugate(q) {
97
+ var result = Hypercomplex.zero(4);
98
+ result.c[0] = q.c[0];
99
+ result.c[1] = -q.c[1];
100
+ result.c[2] = -q.c[2];
101
+ result.c[3] = -q.c[3];
102
+ return result;
103
+ }
104
+
105
+ var conj = quaternionConjugate(q1);
106
+ console.log('\nConjugate:');
107
+ console.log(' q1* = ' + quaternionToString(conj));
108
+
109
+ // Norm
110
+ function quaternionNorm(q) {
111
+ return Math.sqrt(q.c[0]*q.c[0] + q.c[1]*q.c[1] + q.c[2]*q.c[2] + q.c[3]*q.c[3]);
112
+ }
113
+
114
+ console.log('\nNorm:');
115
+ console.log(' |q1| = ' + quaternionNorm(q1).toFixed(4));
116
+
117
+ // ===========================================
118
+ // UNIT QUATERNIONS AND ROTATIONS
119
+ // ===========================================
120
+
121
+ console.log('\n' + '='.repeat(50));
122
+ console.log('Unit Quaternions and 3D Rotations:');
123
+ console.log('='.repeat(50) + '\n');
124
+
125
+ console.log('Unit quaternions (|q| = 1) represent 3D rotations.');
126
+ console.log('Rotation by angle θ around axis (x, y, z):');
127
+ console.log(' q = cos(θ/2) + sin(θ/2)(xi + yj + zk)\n');
128
+
129
+ function rotationQuaternion(axis, angle) {
130
+ // Normalize axis
131
+ var mag = Math.sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
132
+ var x = axis[0] / mag;
133
+ var y = axis[1] / mag;
134
+ var z = axis[2] / mag;
135
+
136
+ var halfAngle = angle / 2;
137
+ var s = Math.sin(halfAngle);
138
+
139
+ return createQuaternion(
140
+ Math.cos(halfAngle),
141
+ x * s,
142
+ y * s,
143
+ z * s
144
+ );
145
+ }
146
+
147
+ // 90 degree rotation around Z axis
148
+ var rotation = rotationQuaternion([0, 0, 1], Math.PI / 2);
149
+ console.log('90° rotation around Z axis:');
150
+ console.log(' q = ' + quaternionToString(rotation));
151
+ console.log(' |q| = ' + quaternionNorm(rotation).toFixed(4) + ' (unit quaternion)');
152
+
153
+ // Rotate a vector
154
+ function rotateVector(v, q) {
155
+ // v' = q * v * q*
156
+ var vQuat = createQuaternion(0, v[0], v[1], v[2]);
157
+ var qConj = quaternionConjugate(q);
158
+
159
+ var result = quaternionMul(quaternionMul(q, vQuat), qConj);
160
+ return [result.c[1], result.c[2], result.c[3]];
161
+ }
162
+
163
+ var original = [1, 0, 0];
164
+ var rotated = rotateVector(original, rotation);
165
+
166
+ console.log('\nRotating vector [1, 0, 0]:');
167
+ console.log(' Original: [' + original.join(', ') + ']');
168
+ console.log(' Rotated: [' + rotated.map(function(v) { return v.toFixed(4); }).join(', ') + ']');
169
+ console.log(' Expected: [0, 1, 0] (90° around Z)');
170
+
171
+ // ===========================================
172
+ // QUATERNION INTERPOLATION (SLERP)
173
+ // ===========================================
174
+
175
+ console.log('\n' + '='.repeat(50));
176
+ console.log('Quaternion Interpolation (SLERP):');
177
+ console.log('='.repeat(50) + '\n');
178
+
179
+ console.log('SLERP smoothly interpolates between rotations.\n');
180
+
181
+ function slerp(q1, q2, t) {
182
+ // Compute dot product
183
+ var dot = q1.c[0]*q2.c[0] + q1.c[1]*q2.c[1] + q1.c[2]*q2.c[2] + q1.c[3]*q2.c[3];
184
+
185
+ // If negative dot, negate one quaternion
186
+ if (dot < 0) {
187
+ q2 = createQuaternion(-q2.c[0], -q2.c[1], -q2.c[2], -q2.c[3]);
188
+ dot = -dot;
189
+ }
190
+
191
+ // Clamp
192
+ dot = Math.min(dot, 1);
193
+
194
+ var theta = Math.acos(dot);
195
+ var sinTheta = Math.sin(theta);
196
+
197
+ var result = Hypercomplex.zero(4);
198
+
199
+ if (sinTheta < 0.001) {
200
+ // Linear interpolation for small angles
201
+ for (var i = 0; i < 4; i++) {
202
+ result.c[i] = (1 - t) * q1.c[i] + t * q2.c[i];
203
+ }
204
+ } else {
205
+ var a = Math.sin((1 - t) * theta) / sinTheta;
206
+ var b = Math.sin(t * theta) / sinTheta;
207
+ for (var i = 0; i < 4; i++) {
208
+ result.c[i] = a * q1.c[i] + b * q2.c[i];
209
+ }
210
+ }
211
+
212
+ return result;
213
+ }
214
+
215
+ var q_start = rotationQuaternion([0, 0, 1], 0);
216
+ var q_end = rotationQuaternion([0, 0, 1], Math.PI);
217
+
218
+ console.log('Interpolating 0° to 180° rotation around Z:');
219
+ var ts = [0, 0.25, 0.5, 0.75, 1.0];
220
+ for (var i = 0; i < ts.length; i++) {
221
+ var t = ts[i];
222
+ var interp = slerp(q_start, q_end, t);
223
+ console.log(' t=' + t.toFixed(2) + ': ' + quaternionToString(interp));
224
+ }
225
+
226
+ // ===========================================
227
+ // KEY TAKEAWAYS
228
+ // ===========================================
229
+
230
+ console.log('\n' + '='.repeat(50));
231
+ console.log('KEY TAKEAWAYS:');
232
+ console.log('1. Quaternions are 4D hypercomplex numbers');
233
+ console.log('2. Multiplication is non-commutative');
234
+ console.log('3. Unit quaternions represent 3D rotations');
235
+ console.log('4. No gimbal lock unlike Euler angles');
236
+ console.log('5. SLERP provides smooth rotation interpolation');
237
+ console.log('6. Used in 3D graphics, robotics, aerospace');
@@ -0,0 +1,192 @@
1
+ /**
2
+ * @example Octonions
3
+ * @description Explore 8-dimensional hypercomplex algebra
4
+ *
5
+ * Octonions are 8-dimensional hypercomplex numbers:
6
+ * - Extension of quaternions
7
+ * - Non-associative multiplication
8
+ * - Related to exceptional Lie groups
9
+ */
10
+
11
+ const { Hypercomplex } = require('../../modular');
12
+
13
+ console.log('TinyAleph Octonion Example');
14
+ console.log('==========================\n');
15
+
16
+ // ===========================================
17
+ // OCTONION BASICS
18
+ // ===========================================
19
+
20
+ console.log('Octonion Basics:');
21
+ console.log('-'.repeat(50) + '\n');
22
+
23
+ console.log('Octonions: o = a₀ + a₁e₁ + a₂e₂ + a₃e₃ + a₄e₄ + a₅e₅ + a₆e₆ + a₇e₇');
24
+ console.log('8-dimensional hypercomplex numbers with 7 imaginary units.\n');
25
+
26
+ console.log('Hierarchy of hypercomplex numbers:');
27
+ console.log(' Real (1D) → Complex (2D) → Quaternions (4D) → Octonions (8D)\n');
28
+
29
+ // Create octonion using Hypercomplex with dimension 8
30
+ function createOctonion(components) {
31
+ var o = Hypercomplex.zero(8);
32
+ for (var i = 0; i < Math.min(components.length, 8); i++) {
33
+ o.c[i] = components[i];
34
+ }
35
+ return o;
36
+ }
37
+
38
+ function octonionToString(o) {
39
+ var parts = [];
40
+ var labels = ['', 'e₁', 'e₂', 'e₃', 'e₄', 'e₅', 'e₆', 'e₇'];
41
+
42
+ for (var i = 0; i < 8; i++) {
43
+ if (Math.abs(o.c[i]) > 0.0001) {
44
+ if (i === 0) {
45
+ parts.push(o.c[i].toFixed(2));
46
+ } else {
47
+ parts.push(o.c[i].toFixed(2) + labels[i]);
48
+ }
49
+ }
50
+ }
51
+ return parts.length > 0 ? parts.join(' + ').replace(/\+ -/g, '- ') : '0';
52
+ }
53
+
54
+ var o1 = createOctonion([1, 2, 0, 0, 1, 0, 0, 0]);
55
+ var o2 = createOctonion([0, 1, 1, 0, 0, 1, 0, 1]);
56
+
57
+ console.log('o1 = ' + octonionToString(o1));
58
+ console.log('o2 = ' + octonionToString(o2));
59
+
60
+ // ===========================================
61
+ // OCTONION OPERATIONS
62
+ // ===========================================
63
+
64
+ console.log('\n' + '='.repeat(50));
65
+ console.log('Octonion Operations:');
66
+ console.log('='.repeat(50) + '\n');
67
+
68
+ // Addition
69
+ function octonionAdd(o1, o2) {
70
+ var result = Hypercomplex.zero(8);
71
+ for (var i = 0; i < 8; i++) {
72
+ result.c[i] = o1.c[i] + o2.c[i];
73
+ }
74
+ return result;
75
+ }
76
+
77
+ var sum = octonionAdd(o1, o2);
78
+ console.log('Addition:');
79
+ console.log(' o1 + o2 = ' + octonionToString(sum));
80
+
81
+ // Conjugate
82
+ function octonionConjugate(o) {
83
+ var result = Hypercomplex.zero(8);
84
+ result.c[0] = o.c[0];
85
+ for (var i = 1; i < 8; i++) {
86
+ result.c[i] = -o.c[i];
87
+ }
88
+ return result;
89
+ }
90
+
91
+ var conj = octonionConjugate(o1);
92
+ console.log('\nConjugate:');
93
+ console.log(' o1* = ' + octonionToString(conj));
94
+
95
+ // Norm
96
+ function octonionNorm(o) {
97
+ var sum = 0;
98
+ for (var i = 0; i < 8; i++) {
99
+ sum += o.c[i] * o.c[i];
100
+ }
101
+ return Math.sqrt(sum);
102
+ }
103
+
104
+ console.log('\nNorm:');
105
+ console.log(' |o1| = ' + octonionNorm(o1).toFixed(4));
106
+ console.log(' |o2| = ' + octonionNorm(o2).toFixed(4));
107
+
108
+ // ===========================================
109
+ // OCTONION MULTIPLICATION
110
+ // ===========================================
111
+
112
+ console.log('\n' + '='.repeat(50));
113
+ console.log('Octonion Multiplication:');
114
+ console.log('='.repeat(50) + '\n');
115
+
116
+ console.log('Octonion multiplication is:');
117
+ console.log(' - Non-commutative: o1 * o2 ≠ o2 * o1');
118
+ console.log(' - Non-associative: (o1 * o2) * o3 ≠ o1 * (o2 * o3)');
119
+ console.log(' - Alternative: o * (o * x) = o² * x\n');
120
+
121
+ // Fano plane multiplication table for octonions
122
+ // This encodes the multiplication rules for e1...e7
123
+ var fanoMul = [
124
+ [1, 2, 3, 4, 5, 6, 7], // e1 * en
125
+ [2, -1, 4, -3, 6, -5, -8],
126
+ [3, -4, -1, 2, 7, 8, -5],
127
+ [4, 3, -2, -1, 8, -7, 6],
128
+ [5, -6, -7, -8, -1, 2, 3],
129
+ [6, 5, -8, 7, -2, -1, -4],
130
+ [7, 8, 5, -6, -3, 4, -1]
131
+ ];
132
+
133
+ console.log('Octonion multiplication uses the Fano plane structure.');
134
+ console.log('This defines the relationships between the 7 imaginary units.\n');
135
+
136
+ // Simplified multiplication (demonstrating non-associativity)
137
+ var e1 = createOctonion([0, 1, 0, 0, 0, 0, 0, 0]);
138
+ var e2 = createOctonion([0, 0, 1, 0, 0, 0, 0, 0]);
139
+ var e4 = createOctonion([0, 0, 0, 0, 1, 0, 0, 0]);
140
+
141
+ console.log('Basis elements:');
142
+ console.log(' e1 = ' + octonionToString(e1));
143
+ console.log(' e2 = ' + octonionToString(e2));
144
+ console.log(' e4 = ' + octonionToString(e4));
145
+
146
+ // ===========================================
147
+ // NORMED DIVISION ALGEBRAS
148
+ // ===========================================
149
+
150
+ console.log('\n' + '='.repeat(50));
151
+ console.log('Normed Division Algebras:');
152
+ console.log('='.repeat(50) + '\n');
153
+
154
+ console.log('There are only 4 normed division algebras:');
155
+ console.log(' 1. Real numbers (R) - 1D');
156
+ console.log(' 2. Complex numbers (C) - 2D');
157
+ console.log(' 3. Quaternions (H) - 4D');
158
+ console.log(' 4. Octonions (O) - 8D\n');
159
+
160
+ console.log('As dimensions increase, we lose properties:');
161
+ console.log(' R: commutative, associative, ordered');
162
+ console.log(' C: commutative, associative, not ordered');
163
+ console.log(' H: not commutative, associative');
164
+ console.log(' O: not commutative, not associative');
165
+
166
+ // ===========================================
167
+ // APPLICATIONS
168
+ // ===========================================
169
+
170
+ console.log('\n' + '='.repeat(50));
171
+ console.log('Octonion Applications:');
172
+ console.log('='.repeat(50) + '\n');
173
+
174
+ console.log('Despite being exotic, octonions appear in:');
175
+ console.log(' - String theory and M-theory');
176
+ console.log(' - Exceptional Lie groups (G2, F4, E6, E7, E8)');
177
+ console.log(' - Bott periodicity theorem');
178
+ console.log(' - Supersymmetry');
179
+ console.log(' - Some approaches to quantum gravity\n');
180
+
181
+ // ===========================================
182
+ // KEY TAKEAWAYS
183
+ // ===========================================
184
+
185
+ console.log('='.repeat(50));
186
+ console.log('KEY TAKEAWAYS:');
187
+ console.log('1. Octonions are 8D hypercomplex numbers');
188
+ console.log('2. Multiplication is non-associative');
189
+ console.log('3. Last of the normed division algebras');
190
+ console.log('4. Related to exceptional mathematical structures');
191
+ console.log('5. Applications in theoretical physics');
192
+ console.log('6. TinyAleph dimension=8 represents octonions');
@@ -0,0 +1,215 @@
1
+ /**
2
+ * @example Prime Factorization
3
+ * @description Explore prime numbers and factorization
4
+ *
5
+ * Prime numbers are fundamental to TinyAleph:
6
+ * - Words map to primes
7
+ * - Products encode ordered sequences
8
+ * - Unique factorization enables decoding
9
+ */
10
+
11
+ const { SemanticBackend } = require('../../modular');
12
+
13
+ console.log('TinyAleph Prime Factorization Example');
14
+ console.log('=====================================\n');
15
+
16
+ const backend = new SemanticBackend({ dimension: 16 });
17
+
18
+ // ===========================================
19
+ // PRIME BASICS
20
+ // ===========================================
21
+
22
+ console.log('Prime Number Basics:');
23
+ console.log('-'.repeat(50) + '\n');
24
+
25
+ console.log('A prime number is divisible only by 1 and itself.');
26
+ console.log('Fundamental Theorem: Every integer > 1 has unique prime factorization.\n');
27
+
28
+ // Generate primes using sieve
29
+ function sieveOfEratosthenes(limit) {
30
+ var sieve = new Array(limit + 1).fill(true);
31
+ sieve[0] = sieve[1] = false;
32
+
33
+ for (var i = 2; i * i <= limit; i++) {
34
+ if (sieve[i]) {
35
+ for (var j = i * i; j <= limit; j += i) {
36
+ sieve[j] = false;
37
+ }
38
+ }
39
+ }
40
+
41
+ var primes = [];
42
+ for (var i = 2; i <= limit; i++) {
43
+ if (sieve[i]) primes.push(i);
44
+ }
45
+ return primes;
46
+ }
47
+
48
+ var primes = sieveOfEratosthenes(100);
49
+ console.log('Primes up to 100:');
50
+ console.log(' ' + primes.join(', '));
51
+ console.log(' Count: ' + primes.length + ' primes\n');
52
+
53
+ // ===========================================
54
+ // PRIME FACTORIZATION
55
+ // ===========================================
56
+
57
+ console.log('='.repeat(50));
58
+ console.log('Prime Factorization:');
59
+ console.log('='.repeat(50) + '\n');
60
+
61
+ function factorize(n) {
62
+ var factors = [];
63
+ var d = 2;
64
+
65
+ while (n > 1) {
66
+ while (n % d === 0) {
67
+ factors.push(d);
68
+ n = n / d;
69
+ }
70
+ d++;
71
+ if (d * d > n && n > 1) {
72
+ factors.push(n);
73
+ break;
74
+ }
75
+ }
76
+
77
+ return factors;
78
+ }
79
+
80
+ function factorizationToString(factors) {
81
+ var counts = {};
82
+ for (var i = 0; i < factors.length; i++) {
83
+ counts[factors[i]] = (counts[factors[i]] || 0) + 1;
84
+ }
85
+
86
+ var parts = [];
87
+ for (var prime in counts) {
88
+ if (counts.hasOwnProperty(prime)) {
89
+ if (counts[prime] === 1) {
90
+ parts.push(prime);
91
+ } else {
92
+ parts.push(prime + '^' + counts[prime]);
93
+ }
94
+ }
95
+ }
96
+ return parts.join(' × ');
97
+ }
98
+
99
+ var numbers = [12, 60, 100, 1001, 9999];
100
+
101
+ console.log('Prime factorizations:');
102
+ for (var i = 0; i < numbers.length; i++) {
103
+ var n = numbers[i];
104
+ var factors = factorize(n);
105
+ console.log(' ' + n + ' = ' + factorizationToString(factors));
106
+ }
107
+
108
+ // ===========================================
109
+ // PRIMES IN TEXT ENCODING
110
+ // ===========================================
111
+
112
+ console.log('\n' + '='.repeat(50));
113
+ console.log('Primes in Text Encoding:');
114
+ console.log('='.repeat(50) + '\n');
115
+
116
+ console.log('TinyAleph assigns primes to words:');
117
+ console.log(' - Each word token gets a unique prime');
118
+ console.log(' - Position encoded by prime powers');
119
+ console.log(' - Product uniquely identifies sequence\n');
120
+
121
+ var words = ['the', 'quick', 'brown', 'fox'];
122
+
123
+ console.log('Word to prime mapping:');
124
+ for (var i = 0; i < words.length; i++) {
125
+ var primeArr = backend.encode(words[i]);
126
+ console.log(' "' + words[i] + '" → prime ' + primeArr[0]);
127
+ }
128
+
129
+ // Show ordered encoding
130
+ var sentence = 'the quick brown fox';
131
+ var ordered = backend.encodeOrdered(sentence);
132
+ console.log('\nOrdered encoding of "' + sentence + '":');
133
+ console.log(' Primes: [' + ordered.join(', ') + ']');
134
+
135
+ // ===========================================
136
+ // PRIME PROPERTIES
137
+ // ===========================================
138
+
139
+ console.log('\n' + '='.repeat(50));
140
+ console.log('Prime Properties:');
141
+ console.log('='.repeat(50) + '\n');
142
+
143
+ // Check if prime
144
+ function isPrime(n) {
145
+ if (n < 2) return false;
146
+ if (n === 2) return true;
147
+ if (n % 2 === 0) return false;
148
+
149
+ for (var i = 3; i * i <= n; i += 2) {
150
+ if (n % i === 0) return false;
151
+ }
152
+ return true;
153
+ }
154
+
155
+ console.log('Primality testing:');
156
+ var testNumbers = [17, 91, 97, 100, 101];
157
+ for (var i = 0; i < testNumbers.length; i++) {
158
+ var n = testNumbers[i];
159
+ console.log(' ' + n + ': ' + (isPrime(n) ? 'prime' : 'composite'));
160
+ }
161
+
162
+ // Twin primes
163
+ console.log('\nTwin primes (differ by 2):');
164
+ var twins = [];
165
+ for (var i = 0; i < primes.length - 1 && twins.length < 5; i++) {
166
+ if (primes[i + 1] - primes[i] === 2) {
167
+ twins.push('(' + primes[i] + ', ' + primes[i + 1] + ')');
168
+ }
169
+ }
170
+ console.log(' ' + twins.join(', '));
171
+
172
+ // ===========================================
173
+ // GCD AND LCM
174
+ // ===========================================
175
+
176
+ console.log('\n' + '='.repeat(50));
177
+ console.log('GCD and LCM:');
178
+ console.log('='.repeat(50) + '\n');
179
+
180
+ function gcd(a, b) {
181
+ while (b !== 0) {
182
+ var t = b;
183
+ b = a % b;
184
+ a = t;
185
+ }
186
+ return a;
187
+ }
188
+
189
+ function lcm(a, b) {
190
+ return Math.abs(a * b) / gcd(a, b);
191
+ }
192
+
193
+ var pairs = [[12, 18], [35, 49], [100, 60]];
194
+
195
+ console.log('GCD and LCM examples:');
196
+ for (var i = 0; i < pairs.length; i++) {
197
+ var a = pairs[i][0];
198
+ var b = pairs[i][1];
199
+ console.log(' GCD(' + a + ', ' + b + ') = ' + gcd(a, b));
200
+ console.log(' LCM(' + a + ', ' + b + ') = ' + lcm(a, b));
201
+ console.log('');
202
+ }
203
+
204
+ // ===========================================
205
+ // KEY TAKEAWAYS
206
+ // ===========================================
207
+
208
+ console.log('='.repeat(50));
209
+ console.log('KEY TAKEAWAYS:');
210
+ console.log('1. Primes are building blocks of integers');
211
+ console.log('2. Every integer has unique prime factorization');
212
+ console.log('3. TinyAleph maps words to primes');
213
+ console.log('4. Products encode ordered sequences');
214
+ console.log('5. Factorization enables structure analysis');
215
+ console.log('6. Prime properties underpin cryptography');