@aleph-ai/tinyaleph 1.0.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 (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +278 -0
  3. package/backends/cryptographic/index.js +196 -0
  4. package/backends/index.js +15 -0
  5. package/backends/interface.js +89 -0
  6. package/backends/scientific/index.js +272 -0
  7. package/backends/semantic/index.js +527 -0
  8. package/backends/semantic/surface.js +393 -0
  9. package/backends/semantic/two-layer.js +375 -0
  10. package/core/fano.js +127 -0
  11. package/core/hilbert.js +564 -0
  12. package/core/hypercomplex.js +141 -0
  13. package/core/index.js +133 -0
  14. package/core/llm.js +132 -0
  15. package/core/prime.js +184 -0
  16. package/core/resonance.js +695 -0
  17. package/core/rformer-tf.js +1086 -0
  18. package/core/rformer.js +806 -0
  19. package/core/sieve.js +350 -0
  20. package/data.json +8163 -0
  21. package/docs/EXAMPLES_PLAN.md +293 -0
  22. package/docs/README.md +159 -0
  23. package/docs/design/ALEPH_CHAT_ARCHITECTURE.md +499 -0
  24. package/docs/guide/01-quickstart.md +298 -0
  25. package/docs/guide/02-semantic-computing.md +409 -0
  26. package/docs/guide/03-cryptographic.md +420 -0
  27. package/docs/guide/04-scientific.md +494 -0
  28. package/docs/guide/05-llm-integration.md +568 -0
  29. package/docs/guide/06-advanced.md +996 -0
  30. package/docs/guide/README.md +188 -0
  31. package/docs/reference/01-core.md +695 -0
  32. package/docs/reference/02-physics.md +601 -0
  33. package/docs/reference/03-backends.md +892 -0
  34. package/docs/reference/04-engine.md +632 -0
  35. package/docs/reference/README.md +252 -0
  36. package/docs/theory/01-prime-semantics.md +327 -0
  37. package/docs/theory/02-hypercomplex-algebra.md +421 -0
  38. package/docs/theory/03-phase-synchronization.md +364 -0
  39. package/docs/theory/04-entropy-reasoning.md +348 -0
  40. package/docs/theory/05-non-commutativity.md +402 -0
  41. package/docs/theory/06-two-layer-meaning.md +414 -0
  42. package/docs/theory/07-resonant-field-interface.md +419 -0
  43. package/docs/theory/08-semantic-sieve.md +520 -0
  44. package/docs/theory/09-temporal-emergence.md +298 -0
  45. package/docs/theory/10-quaternionic-memory.md +415 -0
  46. package/docs/theory/README.md +162 -0
  47. package/engine/aleph.js +418 -0
  48. package/engine/index.js +7 -0
  49. package/index.js +23 -0
  50. package/modular.js +254 -0
  51. package/package.json +99 -0
  52. package/physics/collapse.js +95 -0
  53. package/physics/entropy.js +88 -0
  54. package/physics/index.js +65 -0
  55. package/physics/kuramoto.js +91 -0
  56. package/physics/lyapunov.js +80 -0
  57. package/physics/oscillator.js +95 -0
  58. package/types/index.d.ts +575 -0
package/modular.js ADDED
@@ -0,0 +1,254 @@
1
+ /**
2
+ * TinyAleph Modular - Main entry point
3
+ *
4
+ * A backend-agnostic prime-based computing framework supporting:
5
+ * - Semantic Computing (NLP, concept mapping)
6
+ * - Cryptographic Applications (hashing, key derivation)
7
+ * - Scientific Computing (quantum simulation, particle physics)
8
+ */
9
+
10
+ // Core mathematical foundation
11
+ const core = require('./core');
12
+ const {
13
+ Hypercomplex,
14
+ FANO_LINES,
15
+ octonionMultiplyIndex,
16
+ sedenionMultiplyIndex,
17
+ multiplyIndices,
18
+ buildMultiplicationTable,
19
+ primeGenerator,
20
+ nthPrime,
21
+ primesUpTo,
22
+ isPrime,
23
+ factorize,
24
+ primeSignature,
25
+ firstNPrimes,
26
+ GaussianInteger,
27
+ EisensteinInteger,
28
+ primeToFrequency,
29
+ primeToAngle,
30
+ sumOfTwoSquares,
31
+ DEFAULT_PRIMES,
32
+ LLM,
33
+ // Prime Hilbert Space (HP)
34
+ Complex,
35
+ PrimeState,
36
+ ResonanceOperators,
37
+ EntropyDrivenEvolution,
38
+ encodeMemory,
39
+ symbolicCompute,
40
+ // Prime Resonance Network
41
+ PHI,
42
+ PHI_CONJ,
43
+ DELTA_S,
44
+ QuaternionPrime,
45
+ PrimeResonanceIdentity,
46
+ PhaseLockedRing,
47
+ HolographicField,
48
+ EntangledNode,
49
+ ResonantFragment,
50
+ // ResoFormer ML primitives
51
+ Quaternion,
52
+ SparsePrimeState,
53
+ resonanceScore,
54
+ resonantAttention,
55
+ hamiltonCompose,
56
+ measureNonCommutativity,
57
+ computeCoherence,
58
+ haltingDecision,
59
+ coherenceGatedCompute,
60
+ EntropyCollapseHead,
61
+ generateAttractorCodebook,
62
+ PRGraphMemory,
63
+ applyResonanceOperator
64
+ } = core;
65
+
66
+ // Physics engine
67
+ const physics = require('./physics');
68
+ const {
69
+ Oscillator,
70
+ OscillatorBank,
71
+ KuramotoModel,
72
+ shannonEntropy,
73
+ stateEntropy,
74
+ coherence,
75
+ mutualInformation,
76
+ relativeEntropy,
77
+ jointEntropy,
78
+ oscillatorEntropy,
79
+ estimateLyapunov,
80
+ classifyStability,
81
+ adaptiveCoupling,
82
+ localLyapunov,
83
+ delayEmbedding,
84
+ stabilityMargin,
85
+ collapseProbability,
86
+ shouldCollapse,
87
+ measureState,
88
+ collapseToIndex,
89
+ bornMeasurement,
90
+ partialCollapse,
91
+ applyDecoherence
92
+ } = physics;
93
+
94
+ // Domain backends
95
+ const backends = require('./backends');
96
+ const {
97
+ Backend,
98
+ SemanticBackend,
99
+ CryptographicBackend,
100
+ ScientificBackend
101
+ } = backends;
102
+
103
+ // Unified engine
104
+ const engine = require('./engine');
105
+ const { AlephEngine } = engine;
106
+
107
+ /**
108
+ * Factory function to create an engine with a specific backend
109
+ */
110
+ function createEngine(backendType, config = {}) {
111
+ let backend;
112
+
113
+ switch (backendType.toLowerCase()) {
114
+ case 'semantic':
115
+ backend = new SemanticBackend(config);
116
+ break;
117
+ case 'cryptographic':
118
+ case 'crypto':
119
+ backend = new CryptographicBackend(config);
120
+ break;
121
+ case 'scientific':
122
+ case 'science':
123
+ case 'quantum':
124
+ backend = new ScientificBackend(config);
125
+ break;
126
+ default:
127
+ throw new Error(`Unknown backend type: ${backendType}`);
128
+ }
129
+
130
+ return new AlephEngine(backend, config.engineOptions || {});
131
+ }
132
+
133
+ /**
134
+ * Quick hash function using cryptographic backend
135
+ */
136
+ function hash(input, length = 32) {
137
+ const backend = new CryptographicBackend({ dimension: 32 });
138
+ return backend.hash(input, length);
139
+ }
140
+
141
+ /**
142
+ * Quick key derivation
143
+ */
144
+ function deriveKey(password, salt, length = 32, iterations = 10000) {
145
+ const backend = new CryptographicBackend({ dimension: 32 });
146
+ return backend.deriveKey(password, salt, length, iterations);
147
+ }
148
+
149
+ module.exports = {
150
+ // Main engine
151
+ AlephEngine,
152
+ createEngine,
153
+
154
+ // Backends
155
+ Backend,
156
+ SemanticBackend,
157
+ CryptographicBackend,
158
+ ScientificBackend,
159
+
160
+ // Core math
161
+ Hypercomplex,
162
+ FANO_LINES,
163
+ octonionMultiplyIndex,
164
+ sedenionMultiplyIndex,
165
+ multiplyIndices,
166
+ buildMultiplicationTable,
167
+
168
+ // Prime utilities
169
+ primeGenerator,
170
+ nthPrime,
171
+ primesUpTo,
172
+ isPrime,
173
+ factorize,
174
+ primeSignature,
175
+ firstNPrimes,
176
+ GaussianInteger,
177
+ EisensteinInteger,
178
+ primeToFrequency,
179
+ primeToAngle,
180
+ sumOfTwoSquares,
181
+ DEFAULT_PRIMES,
182
+
183
+ // Physics
184
+ Oscillator,
185
+ OscillatorBank,
186
+ KuramotoModel,
187
+ shannonEntropy,
188
+ stateEntropy,
189
+ coherence,
190
+ mutualInformation,
191
+ relativeEntropy,
192
+ jointEntropy,
193
+ oscillatorEntropy,
194
+ estimateLyapunov,
195
+ classifyStability,
196
+ adaptiveCoupling,
197
+ localLyapunov,
198
+ delayEmbedding,
199
+ stabilityMargin,
200
+ collapseProbability,
201
+ shouldCollapse,
202
+ measureState,
203
+ collapseToIndex,
204
+ bornMeasurement,
205
+ partialCollapse,
206
+ applyDecoherence,
207
+
208
+ // Convenience functions
209
+ hash,
210
+ deriveKey,
211
+
212
+ // LLM client
213
+ LLM,
214
+
215
+ // Prime Hilbert Space (HP) - Quantum-like prime states
216
+ Complex,
217
+ PrimeState,
218
+ ResonanceOperators,
219
+ EntropyDrivenEvolution,
220
+ encodeMemory,
221
+ symbolicCompute,
222
+
223
+ // Prime Resonance Network - Non-local symbolic computing
224
+ PHI,
225
+ PHI_CONJ,
226
+ DELTA_S,
227
+ QuaternionPrime,
228
+ PrimeResonanceIdentity,
229
+ PhaseLockedRing,
230
+ HolographicField,
231
+ EntangledNode,
232
+ ResonantFragment,
233
+
234
+ // ResoFormer ML Primitives (H_Q = H_P ⊗ ℍ)
235
+ Quaternion,
236
+ SparsePrimeState,
237
+ resonanceScore,
238
+ resonantAttention,
239
+ hamiltonCompose,
240
+ measureNonCommutativity,
241
+ computeCoherence,
242
+ haltingDecision,
243
+ coherenceGatedCompute,
244
+ EntropyCollapseHead,
245
+ generateAttractorCodebook,
246
+ PRGraphMemory,
247
+ applyResonanceOperator,
248
+
249
+ // Sub-modules
250
+ core,
251
+ physics,
252
+ backends,
253
+ engine
254
+ };
package/package.json ADDED
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "@aleph-ai/tinyaleph",
3
+ "version": "1.0.0",
4
+ "description": "Prime-resonant semantic computing framework - hypercomplex algebra, oscillator dynamics, and entropy-minimizing reasoning",
5
+ "main": "index.js",
6
+ "types": "types/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./index.js",
10
+ "import": "./index.js",
11
+ "types": "./types/index.d.ts"
12
+ },
13
+ "./core": {
14
+ "require": "./core/index.js",
15
+ "import": "./core/index.js"
16
+ },
17
+ "./physics": {
18
+ "require": "./physics/index.js",
19
+ "import": "./physics/index.js"
20
+ },
21
+ "./backends": {
22
+ "require": "./backends/index.js",
23
+ "import": "./backends/index.js"
24
+ },
25
+ "./engine": {
26
+ "require": "./engine/index.js",
27
+ "import": "./engine/index.js"
28
+ }
29
+ },
30
+ "scripts": {
31
+ "test": "node --test test/*.test.js",
32
+ "test:legacy": "node test/test-modular.js",
33
+ "test:queries": "node test/test-queries.js",
34
+ "test:dna": "node test/test-dna-queries.js",
35
+ "test:watch": "node --test --watch test/*.test.js",
36
+ "test:coverage": "node --test --experimental-test-coverage test/*.test.js",
37
+ "demo": "node examples/demo-modular.js",
38
+ "demo:two-layer": "node examples/demo-two-layer.js",
39
+ "benchmark": "node scripts/benchmark.js",
40
+ "chat": "node examples/chat.js",
41
+ "train": "node scripts/trainer.js",
42
+ "examples": "node examples/run-examples.js",
43
+ "aleph-chat": "node apps/aleph-chat/index.js",
44
+ "ecdsa-learn": "node apps/ecdsa/index.js"
45
+ },
46
+ "keywords": [
47
+ "semantic-computing",
48
+ "hypercomplex",
49
+ "sedenion",
50
+ "quaternion",
51
+ "octonion",
52
+ "prime-numbers",
53
+ "nlp",
54
+ "natural-language",
55
+ "cryptography",
56
+ "hash",
57
+ "oscillator",
58
+ "kuramoto",
59
+ "entropy",
60
+ "ai",
61
+ "machine-learning",
62
+ "reasoning",
63
+ "symbolic-ai",
64
+ "quantum-inspired"
65
+ ],
66
+ "author": "Sebastian Schepis",
67
+ "license": "MIT",
68
+ "repository": {
69
+ "type": "git",
70
+ "url": "git+https://github.com/aleph-ai/tinyaleph.git"
71
+ },
72
+ "bugs": {
73
+ "url": "https://github.com/aleph-ai/tinyaleph/issues"
74
+ },
75
+ "homepage": "https://github.com/aleph-ai/tinyaleph#readme",
76
+ "engines": {
77
+ "node": ">=14.0.0"
78
+ },
79
+ "files": [
80
+ "index.js",
81
+ "modular.js",
82
+ "data.json",
83
+ "core/",
84
+ "physics/",
85
+ "backends/",
86
+ "engine/",
87
+ "types/",
88
+ "docs/",
89
+ "LICENSE",
90
+ "README.md"
91
+ ],
92
+ "publishConfig": {
93
+ "access": "public"
94
+ },
95
+ "dependencies": {
96
+ "@noble/curves": "^1.9.7",
97
+ "@tensorflow/tfjs-node": "^4.22.0"
98
+ }
99
+ }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Quantum-inspired state collapse mechanics
3
+ */
4
+
5
+ function collapseProbability(entropyIntegral, lyapunovFactor = 1) {
6
+ const factor = lyapunovFactor < 0 ? 1.5 : 0.5;
7
+ return (1 - Math.exp(-entropyIntegral)) * factor;
8
+ }
9
+
10
+ function shouldCollapse(coherence, entropy, probability, thresholds) {
11
+ const { minCoherence = 0.7, minEntropy = 1.8 } = thresholds;
12
+ return coherence > minCoherence && entropy > minEntropy && Math.random() < probability;
13
+ }
14
+
15
+ function measureState(hypercomplex, basis = null) {
16
+ if (!basis) {
17
+ let maxIdx = 0, maxVal = 0;
18
+ for (let i = 0; i < hypercomplex.dim; i++) {
19
+ const v = Math.abs(hypercomplex.c[i]);
20
+ if (v > maxVal) { maxVal = v; maxIdx = i; }
21
+ }
22
+ return { index: maxIdx, value: hypercomplex.c[maxIdx] };
23
+ }
24
+ return hypercomplex.dot(basis);
25
+ }
26
+
27
+ /**
28
+ * Collapse state to a specific basis vector
29
+ */
30
+ function collapseToIndex(hypercomplex, index) {
31
+ const { Hypercomplex } = require('../core/hypercomplex');
32
+ const collapsed = Hypercomplex.zero(hypercomplex.dim);
33
+ collapsed.c[index] = hypercomplex.c[index] >= 0 ? 1 : -1;
34
+ return collapsed;
35
+ }
36
+
37
+ /**
38
+ * Probabilistic measurement with Born rule
39
+ */
40
+ function bornMeasurement(hypercomplex) {
41
+ const n = hypercomplex.norm();
42
+ if (n < 1e-10) return { index: 0, probability: 1 };
43
+
44
+ const probabilities = hypercomplex.c.map(v => (v / n) ** 2);
45
+ const r = Math.random();
46
+ let cumulative = 0;
47
+
48
+ for (let i = 0; i < probabilities.length; i++) {
49
+ cumulative += probabilities[i];
50
+ if (r < cumulative) {
51
+ return { index: i, probability: probabilities[i] };
52
+ }
53
+ }
54
+ return { index: probabilities.length - 1, probability: probabilities[probabilities.length - 1] };
55
+ }
56
+
57
+ /**
58
+ * Partial collapse: mix between current state and collapsed state
59
+ */
60
+ function partialCollapse(hypercomplex, targetIndex, strength = 0.5) {
61
+ const { Hypercomplex } = require('../core/hypercomplex');
62
+ const collapsed = Hypercomplex.zero(hypercomplex.dim);
63
+ const sign = hypercomplex.c[targetIndex] >= 0 ? 1 : -1;
64
+ collapsed.c[targetIndex] = sign;
65
+
66
+ // Linear interpolation between current and collapsed
67
+ const result = Hypercomplex.zero(hypercomplex.dim);
68
+ for (let i = 0; i < hypercomplex.dim; i++) {
69
+ result.c[i] = (1 - strength) * hypercomplex.c[i] + strength * collapsed.c[i];
70
+ }
71
+ return result.normalize();
72
+ }
73
+
74
+ /**
75
+ * Decoherence: gradual loss of quantum coherence
76
+ */
77
+ function applyDecoherence(hypercomplex, rate = 0.1) {
78
+ const { Hypercomplex } = require('../core/hypercomplex');
79
+ const result = Hypercomplex.zero(hypercomplex.dim);
80
+ for (let i = 0; i < hypercomplex.dim; i++) {
81
+ // Add small random noise proportional to rate
82
+ result.c[i] = hypercomplex.c[i] * (1 - rate) + (Math.random() - 0.5) * rate;
83
+ }
84
+ return result.normalize();
85
+ }
86
+
87
+ module.exports = {
88
+ collapseProbability,
89
+ shouldCollapse,
90
+ measureState,
91
+ collapseToIndex,
92
+ bornMeasurement,
93
+ partialCollapse,
94
+ applyDecoherence
95
+ };
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Information-theoretic measures for hypercomplex states
3
+ */
4
+
5
+ function shannonEntropy(probabilities) {
6
+ let H = 0;
7
+ for (const p of probabilities) {
8
+ if (p > 1e-10) H -= p * Math.log2(p);
9
+ }
10
+ return H;
11
+ }
12
+
13
+ function stateEntropy(hypercomplex) {
14
+ const n = hypercomplex.norm();
15
+ if (n < 1e-10) return 0;
16
+ const probs = hypercomplex.c.map(v => (v / n) ** 2);
17
+ return shannonEntropy(probs);
18
+ }
19
+
20
+ function coherence(state1, state2) {
21
+ const n1 = state1.norm();
22
+ const n2 = state2.norm();
23
+ if (n1 < 1e-10 || n2 < 1e-10) return 0;
24
+ return Math.abs(state1.dot(state2)) / (n1 * n2);
25
+ }
26
+
27
+ function mutualInformation(bank1, bank2) {
28
+ let corr = 0;
29
+ const n = Math.min(bank1.oscillators.length, bank2.oscillators.length);
30
+ for (let i = 0; i < n; i++) {
31
+ corr += Math.cos(bank1.oscillators[i].phase - bank2.oscillators[i].phase);
32
+ }
33
+ return Math.max(0, corr / n);
34
+ }
35
+
36
+ /**
37
+ * Relative entropy (KL divergence) between two probability distributions
38
+ */
39
+ function relativeEntropy(p, q) {
40
+ let kl = 0;
41
+ for (let i = 0; i < p.length; i++) {
42
+ if (p[i] > 1e-10 && q[i] > 1e-10) {
43
+ kl += p[i] * Math.log2(p[i] / q[i]);
44
+ }
45
+ }
46
+ return kl;
47
+ }
48
+
49
+ /**
50
+ * Joint entropy of two hypercomplex states
51
+ */
52
+ function jointEntropy(state1, state2) {
53
+ const n1 = state1.norm();
54
+ const n2 = state2.norm();
55
+ if (n1 < 1e-10 || n2 < 1e-10) return 0;
56
+
57
+ // Create joint probability distribution
58
+ const probs = [];
59
+ for (let i = 0; i < state1.dim; i++) {
60
+ for (let j = 0; j < state2.dim; j++) {
61
+ const p1 = (state1.c[i] / n1) ** 2;
62
+ const p2 = (state2.c[j] / n2) ** 2;
63
+ probs.push(p1 * p2);
64
+ }
65
+ }
66
+ return shannonEntropy(probs);
67
+ }
68
+
69
+ /**
70
+ * Entropy rate from oscillator bank
71
+ */
72
+ function oscillatorEntropy(bank) {
73
+ const amplitudes = bank.getAmplitudes();
74
+ const total = amplitudes.reduce((a, b) => a + b, 0);
75
+ if (total < 1e-10) return 0;
76
+ const probs = amplitudes.map(a => a / total);
77
+ return shannonEntropy(probs);
78
+ }
79
+
80
+ module.exports = {
81
+ shannonEntropy,
82
+ stateEntropy,
83
+ coherence,
84
+ mutualInformation,
85
+ relativeEntropy,
86
+ jointEntropy,
87
+ oscillatorEntropy
88
+ };
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Physics engine - exports all physics modules
3
+ */
4
+
5
+ const { Oscillator, OscillatorBank } = require('./oscillator');
6
+ const { KuramotoModel } = require('./kuramoto');
7
+ const {
8
+ shannonEntropy,
9
+ stateEntropy,
10
+ coherence,
11
+ mutualInformation,
12
+ relativeEntropy,
13
+ jointEntropy,
14
+ oscillatorEntropy
15
+ } = require('./entropy');
16
+ const {
17
+ estimateLyapunov,
18
+ classifyStability,
19
+ adaptiveCoupling,
20
+ localLyapunov,
21
+ delayEmbedding,
22
+ stabilityMargin
23
+ } = require('./lyapunov');
24
+ const {
25
+ collapseProbability,
26
+ shouldCollapse,
27
+ measureState,
28
+ collapseToIndex,
29
+ bornMeasurement,
30
+ partialCollapse,
31
+ applyDecoherence
32
+ } = require('./collapse');
33
+
34
+ module.exports = {
35
+ // Oscillators
36
+ Oscillator,
37
+ OscillatorBank,
38
+ KuramotoModel,
39
+
40
+ // Entropy & Information
41
+ shannonEntropy,
42
+ stateEntropy,
43
+ coherence,
44
+ mutualInformation,
45
+ relativeEntropy,
46
+ jointEntropy,
47
+ oscillatorEntropy,
48
+
49
+ // Lyapunov stability
50
+ estimateLyapunov,
51
+ classifyStability,
52
+ adaptiveCoupling,
53
+ localLyapunov,
54
+ delayEmbedding,
55
+ stabilityMargin,
56
+
57
+ // State collapse
58
+ collapseProbability,
59
+ shouldCollapse,
60
+ measureState,
61
+ collapseToIndex,
62
+ bornMeasurement,
63
+ partialCollapse,
64
+ applyDecoherence
65
+ };
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Kuramoto synchronization dynamics
3
+ */
4
+ const { OscillatorBank } = require('./oscillator');
5
+
6
+ class KuramotoModel extends OscillatorBank {
7
+ constructor(frequencies, couplingStrength = 0.3) {
8
+ super(frequencies);
9
+ this.K = couplingStrength;
10
+ }
11
+
12
+ orderParameter() {
13
+ let sx = 0, sy = 0;
14
+ for (const osc of this.oscillators) {
15
+ sx += osc.amplitude * Math.cos(osc.phase);
16
+ sy += osc.amplitude * Math.sin(osc.phase);
17
+ }
18
+ const N = this.oscillators.length;
19
+ return Math.sqrt((sx/N)**2 + (sy/N)**2);
20
+ }
21
+
22
+ meanPhase() {
23
+ let sx = 0, sy = 0, ta = 0;
24
+ for (const osc of this.oscillators) {
25
+ sx += osc.amplitude * Math.cos(osc.phase);
26
+ sy += osc.amplitude * Math.sin(osc.phase);
27
+ ta += osc.amplitude;
28
+ }
29
+ return ta > 0 ? Math.atan2(sy/ta, sx/ta) : 0;
30
+ }
31
+
32
+ kuramotoCoupling(osc) {
33
+ let coupling = 0;
34
+ for (const other of this.oscillators) {
35
+ if (other !== osc) {
36
+ coupling += Math.sin(other.phase - osc.phase);
37
+ }
38
+ }
39
+ return this.K * coupling / this.oscillators.length;
40
+ }
41
+
42
+ tick(dt) {
43
+ super.tick(dt, (osc) => this.kuramotoCoupling(osc) * dt);
44
+ for (const osc of this.oscillators) {
45
+ osc.decay(0.02, dt);
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Excite oscillators that correspond to given prime indices
51
+ */
52
+ exciteByPrimes(primes, primeList, amount = 0.5) {
53
+ const primeSet = new Set(primes);
54
+ for (let i = 0; i < this.oscillators.length && i < primeList.length; i++) {
55
+ if (primeSet.has(primeList[i])) {
56
+ this.oscillators[i].excite(amount);
57
+ }
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Get current state as amplitudes weighted by phase
63
+ */
64
+ getWeightedAmplitudes() {
65
+ return this.oscillators.map(o => o.amplitude * Math.sin(o.phase));
66
+ }
67
+
68
+ /**
69
+ * Synchronization measure (0 = desync, 1 = full sync)
70
+ */
71
+ synchronization() {
72
+ return this.orderParameter();
73
+ }
74
+
75
+ /**
76
+ * Phase coherence between pairs
77
+ */
78
+ pairwiseCoherence() {
79
+ const N = this.oscillators.length;
80
+ let total = 0, count = 0;
81
+ for (let i = 0; i < N; i++) {
82
+ for (let j = i + 1; j < N; j++) {
83
+ total += Math.cos(this.oscillators[i].phase - this.oscillators[j].phase);
84
+ count++;
85
+ }
86
+ }
87
+ return count > 0 ? total / count : 0;
88
+ }
89
+ }
90
+
91
+ module.exports = { KuramotoModel };