@aleph-ai/tinyaleph 1.5.7 → 1.6.1

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 (122) hide show
  1. package/core/alexander-module.js +1469 -0
  2. package/core/arithmetic-link-kernel.js +1338 -0
  3. package/core/emotion.js +565 -0
  4. package/core/gravity.js +714 -0
  5. package/core/hilbert.js +506 -3
  6. package/core/index.js +132 -4
  7. package/core/nonlocal.js +744 -0
  8. package/core/oracle.js +662 -0
  9. package/examples/01-hello-world.js +69 -0
  10. package/examples/02-basic-hash.js +90 -0
  11. package/examples/02-observer-stack.js +385 -0
  12. package/examples/03-quantum-coin.js +136 -0
  13. package/examples/05-symbolic-resonance.js +146 -0
  14. package/examples/06-symbol-database.js +150 -0
  15. package/examples/07-semantic-inference.js +223 -0
  16. package/examples/08-compound-symbols.js +219 -0
  17. package/examples/README.md +170 -0
  18. package/examples/ai/01-embeddings.js +155 -0
  19. package/examples/ai/02-semantic-memory.js +243 -0
  20. package/examples/ai/03-reasoning.js +243 -0
  21. package/examples/ai/04-knowledge-graph.js +279 -0
  22. package/examples/ai/05-llm-integration.js +333 -0
  23. package/examples/ai/06-agent.js +294 -0
  24. package/examples/ai/07-hybrid-ai.js +223 -0
  25. package/examples/ai/08-entropy-reasoning.js +259 -0
  26. package/examples/ai/09-concept-learning.js +271 -0
  27. package/examples/ai/10-prompt-primes.js +312 -0
  28. package/examples/ai/11-rag.js +332 -0
  29. package/examples/ai/12-neuro-symbolic.js +321 -0
  30. package/examples/ai/README.md +80 -0
  31. package/examples/arithmetic-topology/01-legendre-symbol.js +78 -0
  32. package/examples/arithmetic-topology/02-redei-symbol.js +126 -0
  33. package/examples/arithmetic-topology/03-alk-kuramoto.js +138 -0
  34. package/examples/arithmetic-topology/04-alexander-module.js +117 -0
  35. package/examples/arithmetic-topology/05-signature-memory.js +118 -0
  36. package/examples/arithmetic-topology/README.md +291 -0
  37. package/examples/bioinformatics/01-dna-encoding.js +108 -0
  38. package/examples/bioinformatics/02-central-dogma.js +162 -0
  39. package/examples/bioinformatics/03-protein-folding.js +206 -0
  40. package/examples/bioinformatics/04-dna-computing.js +192 -0
  41. package/examples/bioinformatics/05-molecular-binding.js +209 -0
  42. package/examples/book-operators-demo.js +155 -0
  43. package/examples/chat.js +105 -0
  44. package/examples/crt-homology/01-residue-encoding.js +87 -0
  45. package/examples/crt-homology/02-birkhoff-attention.js +100 -0
  46. package/examples/crt-homology/03-homology-loss.js +132 -0
  47. package/examples/crt-homology/04-crt-resoformer.js +132 -0
  48. package/examples/crt-homology/README.md +67 -0
  49. package/examples/crypto/01-password-hash.js +210 -0
  50. package/examples/crypto/02-key-derivation.js +210 -0
  51. package/examples/crypto/03-hmac.js +229 -0
  52. package/examples/crypto/04-file-integrity.js +263 -0
  53. package/examples/crypto/05-content-hash.js +263 -0
  54. package/examples/crypto/README.md +99 -0
  55. package/examples/demo-modular.js +223 -0
  56. package/examples/demo-two-layer.js +196 -0
  57. package/examples/discrete/01-integer-sine-table.js +120 -0
  58. package/examples/discrete/02-codebook-tunneling.js +118 -0
  59. package/examples/discrete/03-canonical-fusion.js +135 -0
  60. package/examples/discrete/04-tick-gate.js +139 -0
  61. package/examples/discrete/README.md +142 -0
  62. package/examples/emotion-demo.js +200 -0
  63. package/examples/formal-semantics/01-typed-terms.js +156 -0
  64. package/examples/formal-semantics/02-reduction.js +202 -0
  65. package/examples/formal-semantics/03-lambda-translation.js +206 -0
  66. package/examples/formal-semantics/04-enochian-language.js +257 -0
  67. package/examples/formal-semantics/README.md +98 -0
  68. package/examples/gravity-demo.js +190 -0
  69. package/examples/math/01-quaternions.js +237 -0
  70. package/examples/math/02-octonions.js +192 -0
  71. package/examples/math/03-prime-factorization.js +215 -0
  72. package/examples/math/04-vector-spaces.js +210 -0
  73. package/examples/math/05-gaussian-primes.js +234 -0
  74. package/examples/math/README.md +93 -0
  75. package/examples/nonlocal-demo.js +237 -0
  76. package/examples/oracle-demo.js +204 -0
  77. package/examples/physics/01-oscillator.js +177 -0
  78. package/examples/physics/02-lyapunov.js +201 -0
  79. package/examples/physics/03-collapse.js +183 -0
  80. package/examples/physics/04-kuramoto.js +212 -0
  81. package/examples/physics/05-entropy.js +226 -0
  82. package/examples/physics/05-sync-models.js +298 -0
  83. package/examples/physics/06-primeon-ladder.js +233 -0
  84. package/examples/physics/07-kuramoto-coupled-ladder.js +298 -0
  85. package/examples/physics/README.md +126 -0
  86. package/examples/quantum/01-prime-hunter.js +79 -0
  87. package/examples/quantum/02-entanglement-demo.js +79 -0
  88. package/examples/quantum/03-wave-analysis.js +63 -0
  89. package/examples/resonance/01-prime-hilbert-space.js +140 -0
  90. package/examples/resonance/02-prime-resonance-network.js +221 -0
  91. package/examples/resonance/03-resoformer.js +349 -0
  92. package/examples/resonance/04-resoformer-training.js +329 -0
  93. package/examples/resonance/05-language-model.js +484 -0
  94. package/examples/resonance/README.md +238 -0
  95. package/examples/run-examples.js +427 -0
  96. package/examples/scientific/01-single-qubit.js +185 -0
  97. package/examples/scientific/02-two-qubit.js +209 -0
  98. package/examples/scientific/03-quantum-circuits.js +270 -0
  99. package/examples/scientific/04-measurement.js +229 -0
  100. package/examples/scientific/05-algorithms.js +245 -0
  101. package/examples/scientific/06-random.js +225 -0
  102. package/examples/scientific/07-wavefunction.js +192 -0
  103. package/examples/scientific/README.md +118 -0
  104. package/examples/semantic/01-vocabulary.js +186 -0
  105. package/examples/semantic/02-similarity.js +263 -0
  106. package/examples/semantic/03-word-algebra.js +295 -0
  107. package/examples/semantic/04-clustering.js +348 -0
  108. package/examples/semantic/05-classification.js +386 -0
  109. package/examples/semantic/06-dna-encoding.js +228 -0
  110. package/examples/semantic/07-search.js +304 -0
  111. package/examples/semantic/08-qa-system.js +278 -0
  112. package/examples/semantic/README.md +116 -0
  113. package/examples/topology/01-108-invariant.js +81 -0
  114. package/examples/topology/02-trefoil-constants.js +112 -0
  115. package/examples/topology/03-gauge-symmetry.js +112 -0
  116. package/examples/topology/04-free-energy-dynamics.js +124 -0
  117. package/examples/topology/README.md +129 -0
  118. package/index.js +32 -0
  119. package/modular.js +63 -2
  120. package/package.json +8 -3
  121. package/physics/alk-kuramoto.js +817 -0
  122. package/physics/index.js +23 -2
@@ -0,0 +1,183 @@
1
+ /**
2
+ * @example State Collapse
3
+ * @description Explore quantum-inspired state collapse dynamics
4
+ *
5
+ * TinyAleph models meaning as quantum-like state collapse:
6
+ * - Superposition of possible meanings
7
+ * - Context provides measurement
8
+ * - Collapse to definite interpretation
9
+ */
10
+
11
+ const { SemanticBackend, Hypercomplex } = require('../../modular');
12
+ const Collapse = require('../../physics/collapse');
13
+
14
+ console.log('TinyAleph State Collapse Example');
15
+ console.log('=================================\n');
16
+
17
+ const backend = new SemanticBackend({ dimension: 16 });
18
+
19
+ // ===========================================
20
+ // COLLAPSE BASICS
21
+ // ===========================================
22
+
23
+ console.log('State Collapse Basics:');
24
+ console.log('-'.repeat(50) + '\n');
25
+
26
+ console.log('In TinyAleph, meaning collapse is analogous to quantum measurement:');
27
+ console.log(' - Input text exists in superposition of interpretations');
28
+ console.log(' - Context acts as measurement operator');
29
+ console.log(' - Collapse produces definite meaning\n');
30
+
31
+ // Create a superposition state
32
+ var ambiguousWord = 'bank'; // Could mean financial or river
33
+ var state = backend.textToOrderedState(ambiguousWord);
34
+
35
+ console.log('Ambiguous word: "' + ambiguousWord + '"');
36
+ console.log('State norm: ' + state.norm().toFixed(4));
37
+ console.log('Components: [' + state.c.slice(0, 4).map(function(v) { return v.toFixed(3); }).join(', ') + '...]\n');
38
+
39
+ // ===========================================
40
+ // CONTEXT-DRIVEN COLLAPSE
41
+ // ===========================================
42
+
43
+ console.log('='.repeat(50));
44
+ console.log('Context-Driven Collapse:');
45
+ console.log('='.repeat(50) + '\n');
46
+
47
+ // Different contexts for "bank"
48
+ var contexts = [
49
+ { text: 'money deposit account financial', meaning: 'financial institution' },
50
+ { text: 'river water fish stream', meaning: 'river bank' },
51
+ { text: 'airplane turn angle aircraft', meaning: 'banking turn' }
52
+ ];
53
+
54
+ function similarity(s1, s2) {
55
+ var dot = 0, m1 = 0, m2 = 0;
56
+ for (var i = 0; i < s1.c.length; i++) {
57
+ dot += s1.c[i] * s2.c[i];
58
+ m1 += s1.c[i] * s1.c[i];
59
+ m2 += s2.c[i] * s2.c[i];
60
+ }
61
+ return dot / (Math.sqrt(m1) * Math.sqrt(m2) || 1);
62
+ }
63
+
64
+ console.log('Context determines meaning collapse:\n');
65
+
66
+ for (var i = 0; i < contexts.length; i++) {
67
+ var ctx = contexts[i];
68
+ var contextState = backend.textToOrderedState(ctx.text);
69
+ var combinedState = backend.textToOrderedState(ambiguousWord + ' ' + ctx.text);
70
+
71
+ var sim = similarity(state, contextState);
72
+
73
+ console.log('Context: "' + ctx.text + '"');
74
+ console.log(' Collapsed meaning: ' + ctx.meaning);
75
+ console.log(' Context-word similarity: ' + (sim * 100).toFixed(1) + '%\n');
76
+ }
77
+
78
+ // ===========================================
79
+ // COLLAPSE DYNAMICS
80
+ // ===========================================
81
+
82
+ console.log('='.repeat(50));
83
+ console.log('Collapse Dynamics:');
84
+ console.log('='.repeat(50) + '\n');
85
+
86
+ console.log('Collapse can be gradual or sudden depending on context strength.\n');
87
+
88
+ // Simulate gradual collapse
89
+ function interpolateStates(s1, s2, t) {
90
+ var result = Hypercomplex.zero(s1.c.length);
91
+ for (var i = 0; i < result.c.length; i++) {
92
+ result.c[i] = (1 - t) * s1.c[i] + t * s2.c[i];
93
+ }
94
+ return result.normalize();
95
+ }
96
+
97
+ var initialState = backend.textToOrderedState('unclear ambiguous vague');
98
+ var finalState = backend.textToOrderedState('clear definite precise');
99
+
100
+ console.log('Collapse trajectory from unclear to clear:\n');
101
+
102
+ var ts = [0, 0.25, 0.5, 0.75, 1.0];
103
+ for (var i = 0; i < ts.length; i++) {
104
+ var t = ts[i];
105
+ var interpolated = interpolateStates(initialState, finalState, t);
106
+ console.log(' t=' + t.toFixed(2) + ': norm=' + interpolated.norm().toFixed(4) +
107
+ ', similarity to final=' + (similarity(interpolated, finalState) * 100).toFixed(1) + '%');
108
+ }
109
+
110
+ // ===========================================
111
+ // ENTROPY AND COLLAPSE
112
+ // ===========================================
113
+
114
+ console.log('\n' + '='.repeat(50));
115
+ console.log('Entropy and Collapse:');
116
+ console.log('='.repeat(50) + '\n');
117
+
118
+ console.log('Entropy measures uncertainty in the state:');
119
+ console.log(' - High entropy: many possible meanings');
120
+ console.log(' - Low entropy: definite meaning');
121
+ console.log(' - Collapse reduces entropy\n');
122
+
123
+ function computeEntropy(state) {
124
+ // Treat squared components as probabilities
125
+ var probs = [];
126
+ var sum = 0;
127
+
128
+ for (var i = 0; i < state.c.length; i++) {
129
+ var p = state.c[i] * state.c[i];
130
+ probs.push(p);
131
+ sum += p;
132
+ }
133
+
134
+ // Normalize and compute entropy
135
+ var entropy = 0;
136
+ for (var i = 0; i < probs.length; i++) {
137
+ var p = probs[i] / sum;
138
+ if (p > 0) {
139
+ entropy -= p * Math.log2(p);
140
+ }
141
+ }
142
+
143
+ return entropy;
144
+ }
145
+
146
+ var vague = backend.textToOrderedState('something somewhere somehow');
147
+ var specific = backend.textToOrderedState('the cat sat on the mat');
148
+
149
+ console.log('Entropy comparison:');
150
+ console.log(' "something somewhere somehow": H=' + computeEntropy(vague).toFixed(3) + ' bits');
151
+ console.log(' "the cat sat on the mat": H=' + computeEntropy(specific).toFixed(3) + ' bits');
152
+
153
+ // ===========================================
154
+ // COLLAPSE REVERSIBILITY
155
+ // ===========================================
156
+
157
+ console.log('\n' + '='.repeat(50));
158
+ console.log('Collapse (Ir)reversibility:');
159
+ console.log('='.repeat(50) + '\n');
160
+
161
+ console.log('Unlike quantum measurement, semantic collapse can be partially reversed:');
162
+ console.log(' - New context can shift interpretation');
163
+ console.log(' - But original superposition is lost');
164
+ console.log(' - "Garden path" sentences demonstrate this\n');
165
+
166
+ var gardenPath = 'The horse raced past the barn fell';
167
+ console.log('Garden path: "' + gardenPath + '"');
168
+ console.log('Initial parse: The horse raced... (active)');
169
+ console.log('Reanalysis: The horse [that was] raced past the barn fell (passive)');
170
+ console.log('Context forces re-collapse of syntactic interpretation.');
171
+
172
+ // ===========================================
173
+ // KEY TAKEAWAYS
174
+ // ===========================================
175
+
176
+ console.log('\n' + '='.repeat(50));
177
+ console.log('KEY TAKEAWAYS:');
178
+ console.log('1. Meaning exists in superposition before context');
179
+ console.log('2. Context acts as measurement operator');
180
+ console.log('3. Collapse produces definite interpretation');
181
+ console.log('4. Entropy measures uncertainty in meaning');
182
+ console.log('5. Collapse can be gradual or sudden');
183
+ console.log('6. Reanalysis allows partial reversal');
@@ -0,0 +1,212 @@
1
+ /**
2
+ * @example Kuramoto Synchronization
3
+ * @description Deep dive into Kuramoto model dynamics
4
+ *
5
+ * The Kuramoto model describes synchronization of coupled oscillators:
6
+ * - Phase dynamics with coupling
7
+ * - Critical coupling threshold
8
+ * - Order parameter emergence
9
+ */
10
+
11
+ console.log('TinyAleph Kuramoto Synchronization Example');
12
+ console.log('==========================================\n');
13
+
14
+ // ===========================================
15
+ // KURAMOTO MODEL
16
+ // ===========================================
17
+
18
+ console.log('Kuramoto Model:');
19
+ console.log('-'.repeat(50) + '\n');
20
+
21
+ console.log('Model equation: dθᵢ/dt = ωᵢ + (K/N) Σⱼ sin(θⱼ - θᵢ)');
22
+ console.log('');
23
+ console.log('Parameters:');
24
+ console.log(' θᵢ: Phase of oscillator i');
25
+ console.log(' ωᵢ: Natural frequency of oscillator i');
26
+ console.log(' K: Coupling strength');
27
+ console.log(' N: Number of oscillators\n');
28
+
29
+ // ===========================================
30
+ // SIMULATION SETUP
31
+ // ===========================================
32
+
33
+ console.log('='.repeat(50));
34
+ console.log('Simulation Setup:');
35
+ console.log('='.repeat(50) + '\n');
36
+
37
+ function createOscillatorSystem(N, freqSpread) {
38
+ var system = {
39
+ N: N,
40
+ phases: [],
41
+ frequencies: [],
42
+ K: 0
43
+ };
44
+
45
+ // Initialize with random phases and Lorentzian frequency distribution
46
+ for (var i = 0; i < N; i++) {
47
+ system.phases.push(2 * Math.PI * Math.random());
48
+ // Lorentzian distribution centered at 1.0
49
+ var u = Math.random();
50
+ system.frequencies.push(1.0 + freqSpread * Math.tan(Math.PI * (u - 0.5)));
51
+ }
52
+
53
+ return system;
54
+ }
55
+
56
+ var system = createOscillatorSystem(50, 0.5);
57
+ console.log('Created system with ' + system.N + ' oscillators');
58
+ console.log('Frequency range: [' +
59
+ Math.min.apply(null, system.frequencies).toFixed(2) + ', ' +
60
+ Math.max.apply(null, system.frequencies).toFixed(2) + ']\n');
61
+
62
+ // ===========================================
63
+ // EVOLUTION AND ORDER PARAMETER
64
+ // ===========================================
65
+
66
+ console.log('='.repeat(50));
67
+ console.log('Evolution and Order Parameter:');
68
+ console.log('='.repeat(50) + '\n');
69
+
70
+ function evolveKuramoto(system, dt) {
71
+ var N = system.N;
72
+ var newPhases = [];
73
+
74
+ for (var i = 0; i < N; i++) {
75
+ var coupling = 0;
76
+ for (var j = 0; j < N; j++) {
77
+ coupling += Math.sin(system.phases[j] - system.phases[i]);
78
+ }
79
+ coupling *= system.K / N;
80
+
81
+ var dphase = system.frequencies[i] + coupling;
82
+ newPhases.push(system.phases[i] + dphase * dt);
83
+ }
84
+
85
+ system.phases = newPhases;
86
+ }
87
+
88
+ function orderParameter(phases) {
89
+ var sumCos = 0, sumSin = 0;
90
+ for (var i = 0; i < phases.length; i++) {
91
+ sumCos += Math.cos(phases[i]);
92
+ sumSin += Math.sin(phases[i]);
93
+ }
94
+ var N = phases.length;
95
+ var r = Math.sqrt(sumCos*sumCos + sumSin*sumSin) / N;
96
+ var psi = Math.atan2(sumSin, sumCos);
97
+ return { r: r, psi: psi };
98
+ }
99
+
100
+ console.log('Order parameter r ∈ [0, 1]:');
101
+ console.log(' r = 0: Incoherent (random phases)');
102
+ console.log(' r = 1: Fully synchronized (identical phases)\n');
103
+
104
+ // Test different coupling strengths
105
+ console.log('Coupling Initial r Final r Status');
106
+ console.log('-'.repeat(50));
107
+
108
+ var couplings = [0, 1, 2, 3, 4, 5];
109
+
110
+ for (var c = 0; c < couplings.length; c++) {
111
+ var K = couplings[c];
112
+
113
+ // Reset system
114
+ var testSystem = createOscillatorSystem(50, 0.5);
115
+ testSystem.K = K;
116
+
117
+ var initialR = orderParameter(testSystem.phases).r;
118
+
119
+ // Evolve for 1000 steps
120
+ for (var step = 0; step < 1000; step++) {
121
+ evolveKuramoto(testSystem, 0.05);
122
+ }
123
+
124
+ var finalR = orderParameter(testSystem.phases).r;
125
+ var status = finalR > 0.8 ? 'Synchronized' : finalR > 0.3 ? 'Partial' : 'Incoherent';
126
+
127
+ console.log(K.toFixed(1).padStart(5) + ' ' +
128
+ initialR.toFixed(3) + ' ' +
129
+ finalR.toFixed(3) + ' ' + status);
130
+ }
131
+
132
+ // ===========================================
133
+ // CRITICAL COUPLING
134
+ // ===========================================
135
+
136
+ console.log('\n' + '='.repeat(50));
137
+ console.log('Critical Coupling:');
138
+ console.log('='.repeat(50) + '\n');
139
+
140
+ console.log('Critical coupling Kc marks the synchronization transition:');
141
+ console.log(' K < Kc: Incoherent state');
142
+ console.log(' K > Kc: Partial synchronization');
143
+ console.log(' K >> Kc: Full synchronization\n');
144
+
145
+ // Estimate critical coupling
146
+ console.log('Estimating critical coupling...\n');
147
+
148
+ var Kc_estimate = 2.5; // Approximate for our parameters
149
+ console.log('For Lorentzian frequency distribution:');
150
+ console.log(' Kc = 2γ (where γ is width parameter)');
151
+ console.log(' Estimated Kc ≈ ' + Kc_estimate.toFixed(1));
152
+
153
+ // ===========================================
154
+ // PHASE TRANSITION
155
+ // ===========================================
156
+
157
+ console.log('\n' + '='.repeat(50));
158
+ console.log('Phase Transition:');
159
+ console.log('='.repeat(50) + '\n');
160
+
161
+ console.log('Order parameter r vs coupling K:\n');
162
+
163
+ var Krange = [];
164
+ var rValues = [];
165
+
166
+ for (var K = 0; K <= 6; K += 0.5) {
167
+ var testSystem = createOscillatorSystem(50, 0.5);
168
+ testSystem.K = K;
169
+
170
+ // Evolve to steady state
171
+ for (var step = 0; step < 2000; step++) {
172
+ evolveKuramoto(testSystem, 0.02);
173
+ }
174
+
175
+ var r = orderParameter(testSystem.phases).r;
176
+ Krange.push(K);
177
+ rValues.push(r);
178
+
179
+ // ASCII plot
180
+ var bar = '';
181
+ for (var b = 0; b < Math.floor(r * 30); b++) bar += '█';
182
+ console.log('K=' + K.toFixed(1).padStart(3) + ': |' + bar.padEnd(30) + '| r=' + r.toFixed(2));
183
+ }
184
+
185
+ // ===========================================
186
+ // APPLICATIONS
187
+ // ===========================================
188
+
189
+ console.log('\n' + '='.repeat(50));
190
+ console.log('Applications:');
191
+ console.log('='.repeat(50) + '\n');
192
+
193
+ console.log('Kuramoto model applies to:');
194
+ console.log(' - Firefly synchronization');
195
+ console.log(' - Circadian rhythms');
196
+ console.log(' - Power grid stability');
197
+ console.log(' - Neural oscillations');
198
+ console.log(' - Coupled lasers');
199
+ console.log(' - Semantic concept synchronization\n');
200
+
201
+ // ===========================================
202
+ // KEY TAKEAWAYS
203
+ // ===========================================
204
+
205
+ console.log('='.repeat(50));
206
+ console.log('KEY TAKEAWAYS:');
207
+ console.log('1. Kuramoto model describes coupled oscillator sync');
208
+ console.log('2. Order parameter r measures collective coherence');
209
+ console.log('3. Critical coupling Kc triggers phase transition');
210
+ console.log('4. Above Kc, oscillators spontaneously synchronize');
211
+ console.log('5. Models many natural synchronization phenomena');
212
+ console.log('6. TinyAleph uses for semantic field coherence');
@@ -0,0 +1,226 @@
1
+ /**
2
+ * @example Entropy Analysis
3
+ * @description Analyze information entropy in semantic states
4
+ *
5
+ * Entropy measures uncertainty and information content:
6
+ * - Shannon entropy for discrete distributions
7
+ * - Differential entropy for continuous
8
+ * - Connects to thermodynamics and information theory
9
+ */
10
+
11
+ const { SemanticBackend, Hypercomplex } = require('../../modular');
12
+ const Entropy = require('../../physics/entropy');
13
+
14
+ console.log('TinyAleph Entropy Analysis Example');
15
+ console.log('===================================\n');
16
+
17
+ const backend = new SemanticBackend({ dimension: 16 });
18
+
19
+ // ===========================================
20
+ // ENTROPY BASICS
21
+ // ===========================================
22
+
23
+ console.log('Entropy Basics:');
24
+ console.log('-'.repeat(50) + '\n');
25
+
26
+ console.log('Shannon entropy: H(X) = -Σ p(x) log₂ p(x)');
27
+ console.log('');
28
+ console.log('Properties:');
29
+ console.log(' - H ≥ 0 (always non-negative)');
30
+ console.log(' - H = 0 for certainty (single outcome)');
31
+ console.log(' - H = log₂(n) for uniform distribution over n outcomes');
32
+ console.log('');
33
+
34
+ // ===========================================
35
+ // COMPUTING ENTROPY
36
+ // ===========================================
37
+
38
+ console.log('='.repeat(50));
39
+ console.log('Computing Entropy:');
40
+ console.log('='.repeat(50) + '\n');
41
+
42
+ function shannonEntropy(probs) {
43
+ var H = 0;
44
+ for (var i = 0; i < probs.length; i++) {
45
+ if (probs[i] > 0) {
46
+ H -= probs[i] * Math.log2(probs[i]);
47
+ }
48
+ }
49
+ return H;
50
+ }
51
+
52
+ // Examples
53
+ var certain = [1, 0, 0, 0];
54
+ var uniform = [0.25, 0.25, 0.25, 0.25];
55
+ var biased = [0.7, 0.2, 0.07, 0.03];
56
+ var binary = [0.5, 0.5];
57
+
58
+ console.log('Distribution Entropy');
59
+ console.log('-'.repeat(40));
60
+ console.log('Certain [1,0,0,0]: ' + shannonEntropy(certain).toFixed(3) + ' bits');
61
+ console.log('Uniform [.25,.25,.25,.25]: ' + shannonEntropy(uniform).toFixed(3) + ' bits');
62
+ console.log('Biased [.7,.2,.07,.03]: ' + shannonEntropy(biased).toFixed(3) + ' bits');
63
+ console.log('Binary [.5,.5]: ' + shannonEntropy(binary).toFixed(3) + ' bits');
64
+
65
+ // ===========================================
66
+ // SEMANTIC STATE ENTROPY
67
+ // ===========================================
68
+
69
+ console.log('\n' + '='.repeat(50));
70
+ console.log('Semantic State Entropy:');
71
+ console.log('='.repeat(50) + '\n');
72
+
73
+ function stateEntropy(state) {
74
+ // Treat squared normalized components as probabilities
75
+ var probs = [];
76
+ var sum = 0;
77
+
78
+ for (var i = 0; i < state.c.length; i++) {
79
+ var p = state.c[i] * state.c[i];
80
+ probs.push(p);
81
+ sum += p;
82
+ }
83
+
84
+ // Normalize
85
+ for (var i = 0; i < probs.length; i++) {
86
+ probs[i] /= sum;
87
+ }
88
+
89
+ return shannonEntropy(probs);
90
+ }
91
+
92
+ var texts = [
93
+ 'a',
94
+ 'the cat',
95
+ 'the quick brown fox jumps over the lazy dog',
96
+ 'asjdkfh qwerty zxcvbn random nonsense words gibberish'
97
+ ];
98
+
99
+ console.log('Text entropy (16-dimensional state):\n');
100
+
101
+ for (var i = 0; i < texts.length; i++) {
102
+ var text = texts[i];
103
+ var state = backend.textToOrderedState(text);
104
+ var H = stateEntropy(state);
105
+ console.log(' "' + text.substring(0, 35) + (text.length > 35 ? '...' : '') + '"');
106
+ console.log(' Entropy: ' + H.toFixed(3) + ' bits\n');
107
+ }
108
+
109
+ // ===========================================
110
+ // ENTROPY AND MEANING
111
+ // ===========================================
112
+
113
+ console.log('='.repeat(50));
114
+ console.log('Entropy and Meaning:');
115
+ console.log('='.repeat(50) + '\n');
116
+
117
+ console.log('Low entropy states:');
118
+ console.log(' - Concentrated, specific meaning');
119
+ console.log(' - Few active dimensions');
120
+ console.log(' - Clear, unambiguous\n');
121
+
122
+ console.log('High entropy states:');
123
+ console.log(' - Distributed, general meaning');
124
+ console.log(' - Many active dimensions');
125
+ console.log(' - Vague, ambiguous\n');
126
+
127
+ // Compare specific vs general texts
128
+ var specific = backend.textToOrderedState('hydrogen atom electron orbital');
129
+ var general = backend.textToOrderedState('thing stuff entity concept');
130
+
131
+ console.log('Comparison:');
132
+ console.log(' "hydrogen atom electron orbital": H=' + stateEntropy(specific).toFixed(3) + ' bits');
133
+ console.log(' "thing stuff entity concept": H=' + stateEntropy(general).toFixed(3) + ' bits');
134
+
135
+ // ===========================================
136
+ // CONDITIONAL ENTROPY
137
+ // ===========================================
138
+
139
+ console.log('\n' + '='.repeat(50));
140
+ console.log('Conditional Entropy:');
141
+ console.log('='.repeat(50) + '\n');
142
+
143
+ console.log('H(Y|X): Uncertainty in Y given knowledge of X');
144
+ console.log('H(Y|X) ≤ H(Y): Conditioning reduces entropy\n');
145
+
146
+ function conditionalEntropy(stateY, stateX) {
147
+ // Simplified: measure how much Y's entropy decreases given X
148
+ var Hy = stateEntropy(stateY);
149
+
150
+ // Combine states
151
+ var combined = Hypercomplex.zero(stateY.c.length);
152
+ for (var i = 0; i < combined.c.length; i++) {
153
+ combined.c[i] = (stateY.c[i] + stateX.c[i]) / 2;
154
+ }
155
+ var Hxy = stateEntropy(combined.normalize());
156
+
157
+ // H(Y|X) ≈ H(X,Y) - H(X)
158
+ return Math.max(0, Hxy);
159
+ }
160
+
161
+ var y = backend.textToOrderedState('animal');
162
+ var x1 = backend.textToOrderedState('cat'); // Specific context
163
+ var x2 = backend.textToOrderedState('thing'); // Vague context
164
+
165
+ console.log('Uncertainty about "animal" given context:\n');
166
+ console.log(' H(animal): ' + stateEntropy(y).toFixed(3) + ' bits');
167
+ console.log(' H(animal|cat): ' + conditionalEntropy(y, x1).toFixed(3) + ' bits');
168
+ console.log(' H(animal|thing): ' + conditionalEntropy(y, x2).toFixed(3) + ' bits');
169
+
170
+ // ===========================================
171
+ // RELATIVE ENTROPY (KL DIVERGENCE)
172
+ // ===========================================
173
+
174
+ console.log('\n' + '='.repeat(50));
175
+ console.log('Relative Entropy (KL Divergence):');
176
+ console.log('='.repeat(50) + '\n');
177
+
178
+ console.log('D_KL(P||Q) = Σ P(x) log(P(x)/Q(x))');
179
+ console.log('Measures "distance" between distributions.\n');
180
+
181
+ function klDivergence(state1, state2) {
182
+ var probs1 = [], probs2 = [];
183
+ var sum1 = 0, sum2 = 0;
184
+
185
+ for (var i = 0; i < state1.c.length; i++) {
186
+ var p1 = state1.c[i] * state1.c[i];
187
+ var p2 = state2.c[i] * state2.c[i];
188
+ probs1.push(p1);
189
+ probs2.push(p2);
190
+ sum1 += p1;
191
+ sum2 += p2;
192
+ }
193
+
194
+ var kl = 0;
195
+ for (var i = 0; i < probs1.length; i++) {
196
+ var p = probs1[i] / sum1;
197
+ var q = probs2[i] / sum2 + 1e-10; // Avoid log(0)
198
+ if (p > 0) {
199
+ kl += p * Math.log2(p / q);
200
+ }
201
+ }
202
+
203
+ return kl;
204
+ }
205
+
206
+ var cat = backend.textToOrderedState('cat');
207
+ var dog = backend.textToOrderedState('dog');
208
+ var car = backend.textToOrderedState('car');
209
+
210
+ console.log('KL divergence between concepts:\n');
211
+ console.log(' D(cat||dog) = ' + klDivergence(cat, dog).toFixed(3) + ' bits');
212
+ console.log(' D(cat||car) = ' + klDivergence(cat, car).toFixed(3) + ' bits');
213
+ console.log(' D(dog||car) = ' + klDivergence(dog, car).toFixed(3) + ' bits');
214
+
215
+ // ===========================================
216
+ // KEY TAKEAWAYS
217
+ // ===========================================
218
+
219
+ console.log('\n' + '='.repeat(50));
220
+ console.log('KEY TAKEAWAYS:');
221
+ console.log('1. Entropy measures uncertainty in a state');
222
+ console.log('2. Low entropy = specific, high entropy = vague');
223
+ console.log('3. Context reduces conditional entropy');
224
+ console.log('4. KL divergence measures state divergence');
225
+ console.log('5. Entropy connects information and thermodynamics');
226
+ console.log('6. TinyAleph uses entropy for semantic analysis');