@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.
- package/core/alexander-module.js +1469 -0
- package/core/arithmetic-link-kernel.js +1338 -0
- package/core/emotion.js +565 -0
- package/core/gravity.js +714 -0
- package/core/hilbert.js +506 -3
- package/core/index.js +132 -4
- package/core/nonlocal.js +744 -0
- package/core/oracle.js +662 -0
- package/examples/01-hello-world.js +69 -0
- package/examples/02-basic-hash.js +90 -0
- package/examples/02-observer-stack.js +385 -0
- package/examples/03-quantum-coin.js +136 -0
- package/examples/05-symbolic-resonance.js +146 -0
- package/examples/06-symbol-database.js +150 -0
- package/examples/07-semantic-inference.js +223 -0
- package/examples/08-compound-symbols.js +219 -0
- package/examples/README.md +170 -0
- package/examples/ai/01-embeddings.js +155 -0
- package/examples/ai/02-semantic-memory.js +243 -0
- package/examples/ai/03-reasoning.js +243 -0
- package/examples/ai/04-knowledge-graph.js +279 -0
- package/examples/ai/05-llm-integration.js +333 -0
- package/examples/ai/06-agent.js +294 -0
- package/examples/ai/07-hybrid-ai.js +223 -0
- package/examples/ai/08-entropy-reasoning.js +259 -0
- package/examples/ai/09-concept-learning.js +271 -0
- package/examples/ai/10-prompt-primes.js +312 -0
- package/examples/ai/11-rag.js +332 -0
- package/examples/ai/12-neuro-symbolic.js +321 -0
- package/examples/ai/README.md +80 -0
- package/examples/arithmetic-topology/01-legendre-symbol.js +78 -0
- package/examples/arithmetic-topology/02-redei-symbol.js +126 -0
- package/examples/arithmetic-topology/03-alk-kuramoto.js +138 -0
- package/examples/arithmetic-topology/04-alexander-module.js +117 -0
- package/examples/arithmetic-topology/05-signature-memory.js +118 -0
- package/examples/arithmetic-topology/README.md +291 -0
- package/examples/bioinformatics/01-dna-encoding.js +108 -0
- package/examples/bioinformatics/02-central-dogma.js +162 -0
- package/examples/bioinformatics/03-protein-folding.js +206 -0
- package/examples/bioinformatics/04-dna-computing.js +192 -0
- package/examples/bioinformatics/05-molecular-binding.js +209 -0
- package/examples/book-operators-demo.js +155 -0
- package/examples/chat.js +105 -0
- package/examples/crt-homology/01-residue-encoding.js +87 -0
- package/examples/crt-homology/02-birkhoff-attention.js +100 -0
- package/examples/crt-homology/03-homology-loss.js +132 -0
- package/examples/crt-homology/04-crt-resoformer.js +132 -0
- package/examples/crt-homology/README.md +67 -0
- package/examples/crypto/01-password-hash.js +210 -0
- package/examples/crypto/02-key-derivation.js +210 -0
- package/examples/crypto/03-hmac.js +229 -0
- package/examples/crypto/04-file-integrity.js +263 -0
- package/examples/crypto/05-content-hash.js +263 -0
- package/examples/crypto/README.md +99 -0
- package/examples/demo-modular.js +223 -0
- package/examples/demo-two-layer.js +196 -0
- package/examples/discrete/01-integer-sine-table.js +120 -0
- package/examples/discrete/02-codebook-tunneling.js +118 -0
- package/examples/discrete/03-canonical-fusion.js +135 -0
- package/examples/discrete/04-tick-gate.js +139 -0
- package/examples/discrete/README.md +142 -0
- package/examples/emotion-demo.js +200 -0
- package/examples/formal-semantics/01-typed-terms.js +156 -0
- package/examples/formal-semantics/02-reduction.js +202 -0
- package/examples/formal-semantics/03-lambda-translation.js +206 -0
- package/examples/formal-semantics/04-enochian-language.js +257 -0
- package/examples/formal-semantics/README.md +98 -0
- package/examples/gravity-demo.js +190 -0
- package/examples/math/01-quaternions.js +237 -0
- package/examples/math/02-octonions.js +192 -0
- package/examples/math/03-prime-factorization.js +215 -0
- package/examples/math/04-vector-spaces.js +210 -0
- package/examples/math/05-gaussian-primes.js +234 -0
- package/examples/math/README.md +93 -0
- package/examples/nonlocal-demo.js +237 -0
- package/examples/oracle-demo.js +204 -0
- package/examples/physics/01-oscillator.js +177 -0
- package/examples/physics/02-lyapunov.js +201 -0
- package/examples/physics/03-collapse.js +183 -0
- package/examples/physics/04-kuramoto.js +212 -0
- package/examples/physics/05-entropy.js +226 -0
- package/examples/physics/05-sync-models.js +298 -0
- package/examples/physics/06-primeon-ladder.js +233 -0
- package/examples/physics/07-kuramoto-coupled-ladder.js +298 -0
- package/examples/physics/README.md +126 -0
- package/examples/quantum/01-prime-hunter.js +79 -0
- package/examples/quantum/02-entanglement-demo.js +79 -0
- package/examples/quantum/03-wave-analysis.js +63 -0
- package/examples/resonance/01-prime-hilbert-space.js +140 -0
- package/examples/resonance/02-prime-resonance-network.js +221 -0
- package/examples/resonance/03-resoformer.js +349 -0
- package/examples/resonance/04-resoformer-training.js +329 -0
- package/examples/resonance/05-language-model.js +484 -0
- package/examples/resonance/README.md +238 -0
- package/examples/run-examples.js +427 -0
- package/examples/scientific/01-single-qubit.js +185 -0
- package/examples/scientific/02-two-qubit.js +209 -0
- package/examples/scientific/03-quantum-circuits.js +270 -0
- package/examples/scientific/04-measurement.js +229 -0
- package/examples/scientific/05-algorithms.js +245 -0
- package/examples/scientific/06-random.js +225 -0
- package/examples/scientific/07-wavefunction.js +192 -0
- package/examples/scientific/README.md +118 -0
- package/examples/semantic/01-vocabulary.js +186 -0
- package/examples/semantic/02-similarity.js +263 -0
- package/examples/semantic/03-word-algebra.js +295 -0
- package/examples/semantic/04-clustering.js +348 -0
- package/examples/semantic/05-classification.js +386 -0
- package/examples/semantic/06-dna-encoding.js +228 -0
- package/examples/semantic/07-search.js +304 -0
- package/examples/semantic/08-qa-system.js +278 -0
- package/examples/semantic/README.md +116 -0
- package/examples/topology/01-108-invariant.js +81 -0
- package/examples/topology/02-trefoil-constants.js +112 -0
- package/examples/topology/03-gauge-symmetry.js +112 -0
- package/examples/topology/04-free-energy-dynamics.js +124 -0
- package/examples/topology/README.md +129 -0
- package/index.js +32 -0
- package/modular.js +63 -2
- package/package.json +8 -3
- package/physics/alk-kuramoto.js +817 -0
- package/physics/index.js +23 -2
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* book.pdf Chapters 10 & 11: Oracle Systems & NP Resonance Demo
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates:
|
|
5
|
+
* - Oracle Q: (S, ε) → R_stable with I-Ching attractors
|
|
6
|
+
* - Divination queries
|
|
7
|
+
* - SAT solving via resonance collapse
|
|
8
|
+
* - Semantic compression
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { PrimeState } from '../core/hilbert.js';
|
|
12
|
+
import {
|
|
13
|
+
OracleSystem,
|
|
14
|
+
HexagramAttractor,
|
|
15
|
+
NPResonanceEncoder,
|
|
16
|
+
SemanticCompressor,
|
|
17
|
+
HEXAGRAMS
|
|
18
|
+
} from '../core/oracle.js';
|
|
19
|
+
|
|
20
|
+
console.log('═══════════════════════════════════════════════════════════════');
|
|
21
|
+
console.log(' book.pdf Chapters 10 & 11: Oracle & NP Systems Demo');
|
|
22
|
+
console.log('═══════════════════════════════════════════════════════════════\n');
|
|
23
|
+
|
|
24
|
+
// ==========================================================================
|
|
25
|
+
// 1. I-Ching Hexagram Attractors
|
|
26
|
+
// ==========================================================================
|
|
27
|
+
console.log('1. I-Ching Hexagram Attractors');
|
|
28
|
+
console.log(' 64 hexagrams as stable resonance basins\n');
|
|
29
|
+
|
|
30
|
+
console.log(' Sample hexagrams:');
|
|
31
|
+
[1, 2, 11, 12, 63, 64].forEach(num => {
|
|
32
|
+
const hex = HEXAGRAMS[num];
|
|
33
|
+
if (hex) {
|
|
34
|
+
console.log(` ${num}. ${hex.name} (${hex.meaning})`);
|
|
35
|
+
console.log(` Binary: ${hex.binary.toString(2).padStart(6, '0')}, Primes: [${hex.primes.join(', ')}]`);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Create attractor and show its state
|
|
40
|
+
const attractor = new HexagramAttractor(11); // Tài - Peace
|
|
41
|
+
console.log(`\n Attractor for Hexagram 11 (Tài - Peace):`);
|
|
42
|
+
console.log(` Dominant primes: ${attractor.state.dominant(3).map(d => `${d.p}: ${d.amp.toFixed(3)}`).join(', ')}`);
|
|
43
|
+
|
|
44
|
+
// ==========================================================================
|
|
45
|
+
// 2. Oracle System Q: (S, ε) → R_stable
|
|
46
|
+
// ==========================================================================
|
|
47
|
+
console.log('\n2. Oracle System Q: (S, ε) → R_stable');
|
|
48
|
+
console.log(' Entropy-modulated evolution to stable attractors\n');
|
|
49
|
+
|
|
50
|
+
const oracle = new OracleSystem({
|
|
51
|
+
maxIterations: 50,
|
|
52
|
+
convergenceThreshold: 0.01
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Query with a semantic state
|
|
56
|
+
const queries = [
|
|
57
|
+
'love and harmony',
|
|
58
|
+
'conflict and struggle',
|
|
59
|
+
'new beginnings',
|
|
60
|
+
'patience and waiting'
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
console.log(' Oracle queries:');
|
|
64
|
+
for (const query of queries) {
|
|
65
|
+
const result = oracle.query(query);
|
|
66
|
+
console.log(`\n "${query}"`);
|
|
67
|
+
console.log(` → Attractor: ${result.attractor.number}. ${result.attractor.name}`);
|
|
68
|
+
console.log(` Meaning: ${result.attractor.meaning}`);
|
|
69
|
+
console.log(` Converged: ${result.converged} in ${result.iterations} iterations`);
|
|
70
|
+
console.log(` Final entropy: ${result.finalEntropy.toFixed(4)}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ==========================================================================
|
|
74
|
+
// 3. Divination Query
|
|
75
|
+
// ==========================================================================
|
|
76
|
+
console.log('\n\n3. Divination Query with I-Ching Interpretation');
|
|
77
|
+
console.log(' oracle.divine(question) → hexagram + interpretation\n');
|
|
78
|
+
|
|
79
|
+
const divination = oracle.divine('Should I take a new path?');
|
|
80
|
+
console.log(` Question: "${divination.question}"`);
|
|
81
|
+
console.log(` Answer: Hexagram ${divination.attractor.number} - ${divination.attractor.name}`);
|
|
82
|
+
console.log(` Meaning: ${divination.attractor.meaning}`);
|
|
83
|
+
console.log(` Primes: [${divination.attractor.primes.join(', ')}]`);
|
|
84
|
+
console.log(` Iterations: ${divination.iterations}, Converged: ${divination.converged}`);
|
|
85
|
+
|
|
86
|
+
// ==========================================================================
|
|
87
|
+
// 4. NP Resonance Encoder - SAT Solving
|
|
88
|
+
// ==========================================================================
|
|
89
|
+
console.log('\n\n4. NP Resonance Encoder: V̂_NP = Π_j Ĉ_j');
|
|
90
|
+
console.log(' SAT solving via clause-resonance projection and collapse\n');
|
|
91
|
+
|
|
92
|
+
// Create a simple SAT problem: (x1 OR x2) AND (NOT x1 OR x3) AND (NOT x2 OR NOT x3)
|
|
93
|
+
const sat = new NPResonanceEncoder(['x1', 'x2', 'x3']);
|
|
94
|
+
|
|
95
|
+
sat.addClause([{ var: 'x1', negated: false }, { var: 'x2', negated: false }]); // x1 OR x2
|
|
96
|
+
sat.addClause([{ var: 'x1', negated: true }, { var: 'x3', negated: false }]); // NOT x1 OR x3
|
|
97
|
+
sat.addClause([{ var: 'x2', negated: true }, { var: 'x3', negated: true }]); // NOT x2 OR NOT x3
|
|
98
|
+
|
|
99
|
+
console.log(' SAT Problem:');
|
|
100
|
+
console.log(' (x1 OR x2) AND (NOT x1 OR x3) AND (NOT x2 OR NOT x3)\n');
|
|
101
|
+
|
|
102
|
+
// Solve
|
|
103
|
+
const solution = sat.solve(50);
|
|
104
|
+
console.log(' Solution:');
|
|
105
|
+
console.log(` Satisfiable: ${solution.satisfiable}`);
|
|
106
|
+
if (solution.assignment) {
|
|
107
|
+
console.log(` Assignment: ${JSON.stringify(solution.assignment)}`);
|
|
108
|
+
}
|
|
109
|
+
console.log(` Iterations: ${solution.iterations}`);
|
|
110
|
+
console.log(` Confidence: ${(solution.confidence * 100).toFixed(1)}%`);
|
|
111
|
+
|
|
112
|
+
// Verify solution manually
|
|
113
|
+
if (solution.assignment) {
|
|
114
|
+
const { x1, x2, x3 } = solution.assignment;
|
|
115
|
+
const c1 = x1 || x2;
|
|
116
|
+
const c2 = !x1 || x3;
|
|
117
|
+
const c3 = !x2 || !x3;
|
|
118
|
+
console.log(` Verification: c1=${c1}, c2=${c2}, c3=${c3}, all=${c1 && c2 && c3}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ==========================================================================
|
|
122
|
+
// 5. SAT Problem from CNF String
|
|
123
|
+
// ==========================================================================
|
|
124
|
+
console.log('\n\n5. SAT from CNF String');
|
|
125
|
+
console.log(' Parsing and solving CNF notation\n');
|
|
126
|
+
|
|
127
|
+
const sat2 = new NPResonanceEncoder([]);
|
|
128
|
+
sat2.fromCNF('(a OR b) AND (NOT a OR c) AND (b OR c)');
|
|
129
|
+
|
|
130
|
+
console.log(' CNF: (a OR b) AND (NOT a OR c) AND (b OR c)');
|
|
131
|
+
console.log(` Variables: ${sat2.variables.join(', ')}`);
|
|
132
|
+
console.log(` Clauses: ${sat2.clauses.length}`);
|
|
133
|
+
|
|
134
|
+
const solution2 = sat2.solve(50);
|
|
135
|
+
console.log(` Satisfiable: ${solution2.satisfiable}`);
|
|
136
|
+
if (solution2.assignment) {
|
|
137
|
+
console.log(` Assignment: ${JSON.stringify(solution2.assignment)}`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ==========================================================================
|
|
141
|
+
// 6. Semantic Compression
|
|
142
|
+
// ==========================================================================
|
|
143
|
+
console.log('\n\n6. Semantic Compression via Attractor Convergence');
|
|
144
|
+
console.log(' Map semantic content to hexagram codes\n');
|
|
145
|
+
|
|
146
|
+
const compressor = new SemanticCompressor({ maxIterations: 30 });
|
|
147
|
+
|
|
148
|
+
const texts = [
|
|
149
|
+
'The creative force initiates action',
|
|
150
|
+
'Creative energy drives innovation',
|
|
151
|
+
'Patience brings peace',
|
|
152
|
+
'Peaceful harmony prevails',
|
|
153
|
+
'Conflict arises from misunderstanding'
|
|
154
|
+
];
|
|
155
|
+
|
|
156
|
+
console.log(' Compressing texts:');
|
|
157
|
+
const compressed = texts.map(text => {
|
|
158
|
+
const result = compressor.compress(text);
|
|
159
|
+
console.log(` "${text.substring(0, 35)}..."`);
|
|
160
|
+
console.log(` → Code: ${result.code} (${result.attractor})`);
|
|
161
|
+
return result;
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Find similar
|
|
165
|
+
console.log('\n Finding similar to "Creative power"');
|
|
166
|
+
const similar = compressor.findSimilar('Creative power');
|
|
167
|
+
console.log(` Attractor: ${similar.attractor.number}. ${similar.attractor.name}`);
|
|
168
|
+
console.log(` Exact matches: ${similar.exact.length}`);
|
|
169
|
+
console.log(` Related: ${similar.related.length}`);
|
|
170
|
+
|
|
171
|
+
// ==========================================================================
|
|
172
|
+
// 7. Attractor Basin Analysis
|
|
173
|
+
// ==========================================================================
|
|
174
|
+
console.log('\n\n7. Attractor Basin Analysis');
|
|
175
|
+
console.log(' Measuring convergence patterns\n');
|
|
176
|
+
|
|
177
|
+
// Create random states and see which attractors they converge to
|
|
178
|
+
const basinCounts = new Map();
|
|
179
|
+
|
|
180
|
+
for (let i = 0; i < 20; i++) {
|
|
181
|
+
const state = PrimeState.uniform();
|
|
182
|
+
// Add some random perturbation
|
|
183
|
+
for (const p of state.primes) {
|
|
184
|
+
const phase = Math.random() * 2 * Math.PI;
|
|
185
|
+
state.set(p, state.get(p).mul({ re: Math.cos(phase), im: Math.sin(phase) }));
|
|
186
|
+
}
|
|
187
|
+
state.normalize();
|
|
188
|
+
|
|
189
|
+
const result = oracle.query(state);
|
|
190
|
+
const key = result.attractor.number;
|
|
191
|
+
basinCounts.set(key, (basinCounts.get(key) || 0) + 1);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
console.log(' Distribution of 20 random states across attractors:');
|
|
195
|
+
const sorted = [...basinCounts.entries()].sort((a, b) => b[1] - a[1]);
|
|
196
|
+
sorted.slice(0, 5).forEach(([num, count]) => {
|
|
197
|
+
const hex = HEXAGRAMS[num];
|
|
198
|
+
const name = hex ? hex.name : `Hexagram ${num}`;
|
|
199
|
+
console.log(` ${num}. ${name}: ${count} states (${(count/20*100).toFixed(0)}%)`);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
console.log('\n═══════════════════════════════════════════════════════════════');
|
|
203
|
+
console.log(' ✅ book.pdf Chapters 10 & 11 Oracle Demo Complete!');
|
|
204
|
+
console.log('═══════════════════════════════════════════════════════════════\n');
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example Coupled Oscillators
|
|
3
|
+
* @description Simulate coupled oscillator dynamics
|
|
4
|
+
*
|
|
5
|
+
* TinyAleph's physics module includes oscillator simulations:
|
|
6
|
+
* - Kuramoto model for synchronization
|
|
7
|
+
* - Coupled phase dynamics
|
|
8
|
+
* - Emergence of collective behavior
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const { Oscillator } = require('../../physics/oscillator');
|
|
12
|
+
const { KuramotoModel } = require('../../physics/kuramoto');
|
|
13
|
+
|
|
14
|
+
console.log('TinyAleph Coupled Oscillators Example');
|
|
15
|
+
console.log('=====================================\n');
|
|
16
|
+
|
|
17
|
+
// ===========================================
|
|
18
|
+
// SINGLE OSCILLATOR
|
|
19
|
+
// ===========================================
|
|
20
|
+
|
|
21
|
+
console.log('Single Oscillator:');
|
|
22
|
+
console.log('-'.repeat(50) + '\n');
|
|
23
|
+
|
|
24
|
+
var oscillator = new Oscillator(1.0, 0, 1.0);
|
|
25
|
+
|
|
26
|
+
console.log('Initial state:');
|
|
27
|
+
console.log(' Frequency: ' + oscillator.freq + ' Hz');
|
|
28
|
+
console.log(' Phase: ' + oscillator.phase.toFixed(4) + ' rad');
|
|
29
|
+
console.log(' Amplitude: ' + oscillator.amplitude);
|
|
30
|
+
|
|
31
|
+
// Evolve over time
|
|
32
|
+
console.log('\nTime evolution:');
|
|
33
|
+
var times = [0, 0.25, 0.5, 0.75, 1.0];
|
|
34
|
+
for (var i = 0; i < times.length; i++) {
|
|
35
|
+
var t = times[i];
|
|
36
|
+
var phase = oscillator.phase + 2 * Math.PI * oscillator.freq * t;
|
|
37
|
+
var value = oscillator.amplitude * Math.sin(phase);
|
|
38
|
+
console.log(' t=' + t.toFixed(2) + 's: phase=' + (phase % (2*Math.PI)).toFixed(3) + ', sin=' + value.toFixed(3));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ===========================================
|
|
42
|
+
// KURAMOTO MODEL
|
|
43
|
+
// ===========================================
|
|
44
|
+
|
|
45
|
+
console.log('\n' + '='.repeat(50));
|
|
46
|
+
console.log('Kuramoto Model:');
|
|
47
|
+
console.log('='.repeat(50) + '\n');
|
|
48
|
+
|
|
49
|
+
console.log('Kuramoto model describes synchronization of coupled oscillators:');
|
|
50
|
+
console.log(' dθᵢ/dt = ωᵢ + (K/N) Σⱼ sin(θⱼ - θᵢ)\n');
|
|
51
|
+
|
|
52
|
+
var numOscillators = 5;
|
|
53
|
+
var naturalFreqs = [];
|
|
54
|
+
var phases = [];
|
|
55
|
+
|
|
56
|
+
// Initialize with random frequencies and phases
|
|
57
|
+
for (var i = 0; i < numOscillators; i++) {
|
|
58
|
+
naturalFreqs.push(0.8 + 0.4 * Math.random());
|
|
59
|
+
phases.push(2 * Math.PI * Math.random());
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
console.log('Initial state (5 oscillators):');
|
|
63
|
+
for (var i = 0; i < numOscillators; i++) {
|
|
64
|
+
console.log(' Osc ' + i + ': ω=' + naturalFreqs[i].toFixed(3) + ', θ=' + phases[i].toFixed(3));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Kuramoto evolution step
|
|
68
|
+
function kuramotoStep(phases, freqs, coupling, dt) {
|
|
69
|
+
var N = phases.length;
|
|
70
|
+
var newPhases = [];
|
|
71
|
+
|
|
72
|
+
for (var i = 0; i < N; i++) {
|
|
73
|
+
var interaction = 0;
|
|
74
|
+
for (var j = 0; j < N; j++) {
|
|
75
|
+
interaction += Math.sin(phases[j] - phases[i]);
|
|
76
|
+
}
|
|
77
|
+
interaction *= coupling / N;
|
|
78
|
+
|
|
79
|
+
var dtheta = freqs[i] + interaction;
|
|
80
|
+
newPhases.push(phases[i] + dtheta * dt);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return newPhases;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Order parameter (measures synchronization)
|
|
87
|
+
function orderParameter(phases) {
|
|
88
|
+
var sumCos = 0, sumSin = 0;
|
|
89
|
+
for (var i = 0; i < phases.length; i++) {
|
|
90
|
+
sumCos += Math.cos(phases[i]);
|
|
91
|
+
sumSin += Math.sin(phases[i]);
|
|
92
|
+
}
|
|
93
|
+
return Math.sqrt(sumCos*sumCos + sumSin*sumSin) / phases.length;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Simulate with different coupling strengths
|
|
97
|
+
console.log('\nSynchronization with coupling strength K:\n');
|
|
98
|
+
|
|
99
|
+
var couplings = [0, 0.5, 1.0, 2.0, 5.0];
|
|
100
|
+
|
|
101
|
+
for (var k = 0; k < couplings.length; k++) {
|
|
102
|
+
var K = couplings[k];
|
|
103
|
+
var currentPhases = phases.slice();
|
|
104
|
+
|
|
105
|
+
// Evolve for 100 steps
|
|
106
|
+
for (var step = 0; step < 100; step++) {
|
|
107
|
+
currentPhases = kuramotoStep(currentPhases, naturalFreqs, K, 0.1);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
var r = orderParameter(currentPhases);
|
|
111
|
+
console.log(' K=' + K.toFixed(1) + ': order parameter r=' + r.toFixed(3) + ' (' + (r > 0.8 ? 'synchronized' : r > 0.5 ? 'partial' : 'desynchronized') + ')');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ===========================================
|
|
115
|
+
// SYNCHRONIZATION DYNAMICS
|
|
116
|
+
// ===========================================
|
|
117
|
+
|
|
118
|
+
console.log('\n' + '='.repeat(50));
|
|
119
|
+
console.log('Synchronization Dynamics:');
|
|
120
|
+
console.log('='.repeat(50) + '\n');
|
|
121
|
+
|
|
122
|
+
console.log('Evolution of order parameter over time (K=2.0):\n');
|
|
123
|
+
|
|
124
|
+
var K = 2.0;
|
|
125
|
+
var evolvePhases = phases.slice();
|
|
126
|
+
|
|
127
|
+
for (var step = 0; step <= 50; step += 10) {
|
|
128
|
+
var r = orderParameter(evolvePhases);
|
|
129
|
+
var bar = '';
|
|
130
|
+
for (var b = 0; b < Math.floor(r * 20); b++) bar += '█';
|
|
131
|
+
console.log(' Step ' + String(step).padStart(3) + ': r=' + r.toFixed(3) + ' |' + bar);
|
|
132
|
+
|
|
133
|
+
// Evolve 10 steps
|
|
134
|
+
for (var s = 0; s < 10; s++) {
|
|
135
|
+
evolvePhases = kuramotoStep(evolvePhases, naturalFreqs, K, 0.1);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ===========================================
|
|
140
|
+
// PHASE DISTRIBUTION
|
|
141
|
+
// ===========================================
|
|
142
|
+
|
|
143
|
+
console.log('\n' + '='.repeat(50));
|
|
144
|
+
console.log('Phase Distribution:');
|
|
145
|
+
console.log('='.repeat(50) + '\n');
|
|
146
|
+
|
|
147
|
+
// Show phase distribution at final state
|
|
148
|
+
console.log('Final phase distribution (mod 2π):');
|
|
149
|
+
var bins = new Array(8).fill(0);
|
|
150
|
+
|
|
151
|
+
for (var i = 0; i < evolvePhases.length; i++) {
|
|
152
|
+
var normalizedPhase = ((evolvePhases[i] % (2*Math.PI)) + 2*Math.PI) % (2*Math.PI);
|
|
153
|
+
var bin = Math.floor(normalizedPhase / (2*Math.PI) * 8);
|
|
154
|
+
if (bin >= 8) bin = 7;
|
|
155
|
+
bins[bin]++;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
for (var i = 0; i < 8; i++) {
|
|
159
|
+
var start = (i * 45).toString().padStart(3);
|
|
160
|
+
var end = ((i+1) * 45).toString().padStart(3);
|
|
161
|
+
var bar = '';
|
|
162
|
+
for (var b = 0; b < bins[i]; b++) bar += '█';
|
|
163
|
+
console.log(' ' + start + '°-' + end + '°: ' + bar);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ===========================================
|
|
167
|
+
// KEY TAKEAWAYS
|
|
168
|
+
// ===========================================
|
|
169
|
+
|
|
170
|
+
console.log('\n' + '='.repeat(50));
|
|
171
|
+
console.log('KEY TAKEAWAYS:');
|
|
172
|
+
console.log('1. Oscillators evolve with characteristic frequency');
|
|
173
|
+
console.log('2. Kuramoto model couples oscillators via sine interaction');
|
|
174
|
+
console.log('3. Order parameter r measures synchronization level');
|
|
175
|
+
console.log('4. Above critical coupling, synchronization emerges');
|
|
176
|
+
console.log('5. Phase distribution narrows as system synchronizes');
|
|
177
|
+
console.log('6. Models fireflies, neurons, power grids, etc.');
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example Lyapunov Exponents
|
|
3
|
+
* @description Analyze system stability using Lyapunov exponents
|
|
4
|
+
*
|
|
5
|
+
* Lyapunov exponents measure sensitivity to initial conditions:
|
|
6
|
+
* - Positive: chaotic, trajectories diverge
|
|
7
|
+
* - Zero: marginally stable
|
|
8
|
+
* - Negative: stable, trajectories converge
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const Lyapunov = require('../../physics/lyapunov');
|
|
12
|
+
|
|
13
|
+
console.log('TinyAleph Lyapunov Exponents Example');
|
|
14
|
+
console.log('====================================\n');
|
|
15
|
+
|
|
16
|
+
// ===========================================
|
|
17
|
+
// LYAPUNOV BASICS
|
|
18
|
+
// ===========================================
|
|
19
|
+
|
|
20
|
+
console.log('Lyapunov Exponent Basics:');
|
|
21
|
+
console.log('-'.repeat(50) + '\n');
|
|
22
|
+
|
|
23
|
+
console.log('Lyapunov exponent λ measures exponential separation:');
|
|
24
|
+
console.log(' |δx(t)| ≈ |δx(0)| e^(λt)');
|
|
25
|
+
console.log('');
|
|
26
|
+
console.log(' λ > 0: Chaotic (butterfly effect)');
|
|
27
|
+
console.log(' λ = 0: Marginally stable');
|
|
28
|
+
console.log(' λ < 0: Stable (perturbations decay)\n');
|
|
29
|
+
|
|
30
|
+
// ===========================================
|
|
31
|
+
// COMPUTING LYAPUNOV EXPONENTS
|
|
32
|
+
// ===========================================
|
|
33
|
+
|
|
34
|
+
console.log('='.repeat(50));
|
|
35
|
+
console.log('Computing Lyapunov Exponents:');
|
|
36
|
+
console.log('='.repeat(50) + '\n');
|
|
37
|
+
|
|
38
|
+
// Simple 1D map: f(x) = rx(1-x) (logistic map)
|
|
39
|
+
function logisticMap(x, r) {
|
|
40
|
+
return r * x * (1 - x);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function logisticMapDerivative(x, r) {
|
|
44
|
+
return r * (1 - 2 * x);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function computeLyapunov(r, x0, iterations, transient) {
|
|
48
|
+
transient = transient || 100;
|
|
49
|
+
|
|
50
|
+
var x = x0;
|
|
51
|
+
|
|
52
|
+
// Skip transient
|
|
53
|
+
for (var i = 0; i < transient; i++) {
|
|
54
|
+
x = logisticMap(x, r);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Compute Lyapunov exponent
|
|
58
|
+
var sumLog = 0;
|
|
59
|
+
for (var i = 0; i < iterations; i++) {
|
|
60
|
+
var derivative = Math.abs(logisticMapDerivative(x, r));
|
|
61
|
+
if (derivative > 0) {
|
|
62
|
+
sumLog += Math.log(derivative);
|
|
63
|
+
}
|
|
64
|
+
x = logisticMap(x, r);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return sumLog / iterations;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Test different r values for logistic map
|
|
71
|
+
var rValues = [2.5, 3.0, 3.5, 3.8, 4.0];
|
|
72
|
+
|
|
73
|
+
console.log('Logistic map: x_{n+1} = r * x_n * (1 - x_n)\n');
|
|
74
|
+
console.log('r λ Behavior');
|
|
75
|
+
console.log('-'.repeat(40));
|
|
76
|
+
|
|
77
|
+
for (var i = 0; i < rValues.length; i++) {
|
|
78
|
+
var r = rValues[i];
|
|
79
|
+
var lambda = computeLyapunov(r, 0.5, 1000);
|
|
80
|
+
var behavior = lambda > 0 ? 'Chaotic' : lambda < -0.1 ? 'Stable fixed point' : 'Period doubling';
|
|
81
|
+
console.log(r.toFixed(1) + ' ' + lambda.toFixed(4).padStart(7) + ' ' + behavior);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ===========================================
|
|
85
|
+
// CHAOS IN LOGISTIC MAP
|
|
86
|
+
// ===========================================
|
|
87
|
+
|
|
88
|
+
console.log('\n' + '='.repeat(50));
|
|
89
|
+
console.log('Chaos in Logistic Map:');
|
|
90
|
+
console.log('='.repeat(50) + '\n');
|
|
91
|
+
|
|
92
|
+
console.log('At r=4.0, the logistic map is fully chaotic.\n');
|
|
93
|
+
|
|
94
|
+
// Show sensitive dependence
|
|
95
|
+
var x1 = 0.5;
|
|
96
|
+
var x2 = 0.5 + 1e-10; // Tiny perturbation
|
|
97
|
+
|
|
98
|
+
console.log('Sensitivity to initial conditions (r=4.0):');
|
|
99
|
+
console.log(' x1(0) = 0.5');
|
|
100
|
+
console.log(' x2(0) = 0.5 + 10^(-10)\n');
|
|
101
|
+
|
|
102
|
+
console.log('Step x1 x2 |x1-x2|');
|
|
103
|
+
console.log('-'.repeat(50));
|
|
104
|
+
|
|
105
|
+
for (var step = 0; step <= 50; step += 10) {
|
|
106
|
+
var diff = Math.abs(x1 - x2);
|
|
107
|
+
console.log(String(step).padStart(4) + ' ' + x1.toFixed(6).padStart(10) + ' ' + x2.toFixed(6).padStart(10) + ' ' + diff.toExponential(2));
|
|
108
|
+
|
|
109
|
+
// Evolve 10 steps
|
|
110
|
+
for (var s = 0; s < 10; s++) {
|
|
111
|
+
x1 = logisticMap(x1, 4.0);
|
|
112
|
+
x2 = logisticMap(x2, 4.0);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
console.log('\nNote: Tiny initial difference grows exponentially!');
|
|
117
|
+
|
|
118
|
+
// ===========================================
|
|
119
|
+
// STABILITY ANALYSIS
|
|
120
|
+
// ===========================================
|
|
121
|
+
|
|
122
|
+
console.log('\n' + '='.repeat(50));
|
|
123
|
+
console.log('Stability Analysis:');
|
|
124
|
+
console.log('='.repeat(50) + '\n');
|
|
125
|
+
|
|
126
|
+
// Linear system stability
|
|
127
|
+
function linearSystemLyapunov(A) {
|
|
128
|
+
// For 2x2 matrix, find eigenvalues
|
|
129
|
+
var a = A[0][0], b = A[0][1], c = A[1][0], d = A[1][1];
|
|
130
|
+
var trace = a + d;
|
|
131
|
+
var det = a * d - b * c;
|
|
132
|
+
|
|
133
|
+
var discriminant = trace * trace - 4 * det;
|
|
134
|
+
|
|
135
|
+
if (discriminant >= 0) {
|
|
136
|
+
var lambda1 = (trace + Math.sqrt(discriminant)) / 2;
|
|
137
|
+
var lambda2 = (trace - Math.sqrt(discriminant)) / 2;
|
|
138
|
+
return Math.max(Math.log(Math.abs(lambda1)), Math.log(Math.abs(lambda2)));
|
|
139
|
+
} else {
|
|
140
|
+
// Complex eigenvalues, magnitude is sqrt(det)
|
|
141
|
+
return Math.log(Math.sqrt(det));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
var systems = [
|
|
146
|
+
{ name: 'Stable node', matrix: [[0.5, 0], [0, 0.3]] },
|
|
147
|
+
{ name: 'Saddle point', matrix: [[2, 0], [0, 0.25]] },
|
|
148
|
+
{ name: 'Stable spiral', matrix: [[0.9, 0.4], [-0.4, 0.9]] },
|
|
149
|
+
{ name: 'Unstable', matrix: [[1.1, 0], [0, 1.2]] }
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
console.log('Linear systems dx/dt = Ax:\n');
|
|
153
|
+
console.log('System λ_max Stability');
|
|
154
|
+
console.log('-'.repeat(45));
|
|
155
|
+
|
|
156
|
+
for (var i = 0; i < systems.length; i++) {
|
|
157
|
+
var sys = systems[i];
|
|
158
|
+
var lambda = linearSystemLyapunov(sys.matrix);
|
|
159
|
+
var stability = lambda < 0 ? 'Stable' : lambda > 0 ? 'Unstable' : 'Marginal';
|
|
160
|
+
console.log(sys.name.padEnd(16) + lambda.toFixed(4).padStart(7) + ' ' + stability);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ===========================================
|
|
164
|
+
// PRACTICAL APPLICATIONS
|
|
165
|
+
// ===========================================
|
|
166
|
+
|
|
167
|
+
console.log('\n' + '='.repeat(50));
|
|
168
|
+
console.log('Practical Applications:');
|
|
169
|
+
console.log('='.repeat(50) + '\n');
|
|
170
|
+
|
|
171
|
+
console.log('Lyapunov exponents are used in:');
|
|
172
|
+
console.log(' - Weather prediction (limits of predictability)');
|
|
173
|
+
console.log(' - Cardiac dynamics (detecting arrhythmias)');
|
|
174
|
+
console.log(' - Financial markets (risk assessment)');
|
|
175
|
+
console.log(' - Neural networks (edge of chaos)');
|
|
176
|
+
console.log(' - Encryption (chaos-based cryptography)\n');
|
|
177
|
+
|
|
178
|
+
// Predictability horizon
|
|
179
|
+
console.log('Predictability horizon:');
|
|
180
|
+
console.log(' For λ > 0, prediction error doubles every T = ln(2)/λ time units.\n');
|
|
181
|
+
|
|
182
|
+
var lambda_weather = 1.0; // Approximate for atmosphere
|
|
183
|
+
var initial_error = 1e-6;
|
|
184
|
+
var acceptable_error = 0.1;
|
|
185
|
+
|
|
186
|
+
var predictability = Math.log(acceptable_error / initial_error) / lambda_weather;
|
|
187
|
+
console.log(' Example: λ=' + lambda_weather + ', initial error=10^-6');
|
|
188
|
+
console.log(' Time until error reaches 0.1: ' + predictability.toFixed(1) + ' time units');
|
|
189
|
+
|
|
190
|
+
// ===========================================
|
|
191
|
+
// KEY TAKEAWAYS
|
|
192
|
+
// ===========================================
|
|
193
|
+
|
|
194
|
+
console.log('\n' + '='.repeat(50));
|
|
195
|
+
console.log('KEY TAKEAWAYS:');
|
|
196
|
+
console.log('1. Lyapunov exponents measure chaos');
|
|
197
|
+
console.log('2. λ > 0 indicates sensitive dependence');
|
|
198
|
+
console.log('3. Computed from derivative along trajectory');
|
|
199
|
+
console.log('4. Determines predictability horizon');
|
|
200
|
+
console.log('5. Used in stability analysis of dynamical systems');
|
|
201
|
+
console.log('6. TinyAleph uses λ to characterize semantic states');
|