@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
@@ -0,0 +1,695 @@
1
+ /**
2
+ * Prime Resonance Network Components
3
+ *
4
+ * Implementation of:
5
+ * - Prime Resonance Identity (PRI) = (P_G, P_E, P_Q)
6
+ * - Entanglement and Coherence
7
+ * - Phase-Locked Prime Rings
8
+ * - Holographic Memory Fields
9
+ *
10
+ * From "Prime Resonance Network Specification (PRNS)"
11
+ */
12
+
13
+ const { GaussianInteger, EisensteinInteger, isPrime, firstNPrimes } = require('./prime');
14
+ const { Complex, PrimeState } = require('./hilbert');
15
+
16
+ // Golden ratio and its conjugate (for irrational phase locks)
17
+ const PHI = (1 + Math.sqrt(5)) / 2; // 1.618...
18
+ const PHI_CONJ = (1 - Math.sqrt(5)) / 2; // -0.618...
19
+ const DELTA_S = Math.sqrt(2); // Another irrational for phase locks
20
+
21
+ /**
22
+ * Quaternionic Prime representation
23
+ * Using Hamilton quaternions: q = a + bi + cj + dk
24
+ */
25
+ class QuaternionPrime {
26
+ constructor(a, b, c, d) {
27
+ this.a = a;
28
+ this.b = b;
29
+ this.c = c;
30
+ this.d = d;
31
+ }
32
+
33
+ static fromPrime(p) {
34
+ // Embed a prime in quaternion space using a specific encoding
35
+ // p → (p, 0, 0, 0) for simplicity, or a more interesting embedding
36
+ const sqrt = Math.sqrt(p);
37
+ return new QuaternionPrime(p, 0, 0, 0);
38
+ }
39
+
40
+ static fromGaussian(g) {
41
+ return new QuaternionPrime(g.real, g.imag, 0, 0);
42
+ }
43
+
44
+ norm() {
45
+ return Math.sqrt(this.a*this.a + this.b*this.b + this.c*this.c + this.d*this.d);
46
+ }
47
+
48
+ add(other) {
49
+ return new QuaternionPrime(
50
+ this.a + other.a,
51
+ this.b + other.b,
52
+ this.c + other.c,
53
+ this.d + other.d
54
+ );
55
+ }
56
+
57
+ mul(other) {
58
+ // Hamilton product
59
+ return new QuaternionPrime(
60
+ this.a*other.a - this.b*other.b - this.c*other.c - this.d*other.d,
61
+ this.a*other.b + this.b*other.a + this.c*other.d - this.d*other.c,
62
+ this.a*other.c - this.b*other.d + this.c*other.a + this.d*other.b,
63
+ this.a*other.d + this.b*other.c - this.c*other.b + this.d*other.a
64
+ );
65
+ }
66
+
67
+ conjugate() {
68
+ return new QuaternionPrime(this.a, -this.b, -this.c, -this.d);
69
+ }
70
+
71
+ isHurwitzPrime() {
72
+ // A Hurwitz quaternion is prime if its norm is a rational prime
73
+ const n = this.norm();
74
+ return isPrime(Math.round(n * n));
75
+ }
76
+
77
+ toArray() {
78
+ return [this.a, this.b, this.c, this.d];
79
+ }
80
+
81
+ toString() {
82
+ return `${this.a} + ${this.b}i + ${this.c}j + ${this.d}k`;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Prime Resonance Identity (PRI)
88
+ * A triadic identity composed of:
89
+ * - P_G: Gaussian prime
90
+ * - P_E: Eisenstein prime
91
+ * - P_Q: Quaternionic prime
92
+ */
93
+ class PrimeResonanceIdentity {
94
+ constructor(gaussianPrime, eisensteinPrime, quaternionPrime) {
95
+ this.gaussian = gaussianPrime; // GaussianInteger
96
+ this.eisenstein = eisensteinPrime; // EisensteinInteger
97
+ this.quaternion = quaternionPrime; // QuaternionPrime
98
+
99
+ // Compute combined signature
100
+ this._computeSignature();
101
+ }
102
+
103
+ _computeSignature() {
104
+ const gNorm = this.gaussian.norm();
105
+ const eNorm = this.eisenstein.norm();
106
+ const qNorm = this.quaternion.norm();
107
+
108
+ // Signature is a triple of norms
109
+ this.signature = [gNorm, eNorm, qNorm];
110
+
111
+ // Hash from signature
112
+ this.hash = (gNorm * 997 + eNorm * 991 + Math.round(qNorm) * 983) % 1000000007;
113
+ }
114
+
115
+ /**
116
+ * Generate a PRI from a seed integer
117
+ */
118
+ static fromSeed(seed) {
119
+ // Use seed to generate three primes
120
+ const p1 = firstNPrimes(seed + 10)[seed % 10 + 5];
121
+ const p2 = firstNPrimes(seed + 15)[seed % 10 + 8];
122
+ const p3 = firstNPrimes(seed + 20)[seed % 10 + 12];
123
+
124
+ // Create Gaussian prime: find a + bi where a² + b² is prime
125
+ const g = new GaussianInteger(p1 % 100, (seed * 7) % 50);
126
+
127
+ // Create Eisenstein prime: find a + bω where a² - ab + b² ≡ 2 (mod 3)
128
+ const e = new EisensteinInteger(p2 % 100, (seed * 11) % 50);
129
+
130
+ // Create Quaternion prime
131
+ const q = new QuaternionPrime(p3, seed % 10, (seed * 3) % 10, (seed * 7) % 10);
132
+
133
+ return new PrimeResonanceIdentity(g, e, q);
134
+ }
135
+
136
+ /**
137
+ * Generate a random PRI
138
+ */
139
+ static random() {
140
+ const primes = firstNPrimes(50);
141
+
142
+ const idx1 = Math.floor(Math.random() * primes.length);
143
+ const idx2 = Math.floor(Math.random() * primes.length);
144
+ const idx3 = Math.floor(Math.random() * primes.length);
145
+
146
+ const p1 = primes[idx1];
147
+ const p2 = primes[idx2];
148
+ const p3 = primes[idx3];
149
+
150
+ const g = new GaussianInteger(p1, Math.floor(Math.random() * 10));
151
+ const e = new EisensteinInteger(p2, Math.floor(Math.random() * 10));
152
+ const q = new QuaternionPrime(p3,
153
+ Math.floor(Math.random() * 5),
154
+ Math.floor(Math.random() * 5),
155
+ Math.floor(Math.random() * 5)
156
+ );
157
+
158
+ return new PrimeResonanceIdentity(g, e, q);
159
+ }
160
+
161
+ /**
162
+ * Compute entanglement strength with another PRI
163
+ */
164
+ entanglementStrength(other) {
165
+ // Based on phase alignment and norm similarity
166
+ const gPhase = Math.atan2(this.gaussian.imag, this.gaussian.real);
167
+ const gPhaseOther = Math.atan2(other.gaussian.imag, other.gaussian.real);
168
+
169
+ const ePhase = Math.atan2(this.eisenstein.b * Math.sqrt(3)/2,
170
+ this.eisenstein.a - this.eisenstein.b/2);
171
+ const ePhaseOther = Math.atan2(other.eisenstein.b * Math.sqrt(3)/2,
172
+ other.eisenstein.a - other.eisenstein.b/2);
173
+
174
+ const gAlignment = Math.cos(gPhase - gPhaseOther);
175
+ const eAlignment = Math.cos(ePhase - ePhaseOther);
176
+
177
+ // Quaternion alignment via normalized dot product
178
+ const qNorm = this.quaternion.norm() * other.quaternion.norm();
179
+ const qDot = (this.quaternion.a * other.quaternion.a +
180
+ this.quaternion.b * other.quaternion.b +
181
+ this.quaternion.c * other.quaternion.c +
182
+ this.quaternion.d * other.quaternion.d);
183
+ const qAlignment = qNorm > 0 ? qDot / qNorm : 0;
184
+
185
+ // Combined entanglement strength
186
+ return (gAlignment + eAlignment + qAlignment) / 3;
187
+ }
188
+
189
+ toJSON() {
190
+ return {
191
+ gaussian: { real: this.gaussian.real, imag: this.gaussian.imag },
192
+ eisenstein: { a: this.eisenstein.a, b: this.eisenstein.b },
193
+ quaternion: this.quaternion.toArray(),
194
+ signature: this.signature,
195
+ hash: this.hash
196
+ };
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Phase-Locked Prime Ring
202
+ * Implements stable communication via irrational phase locks
203
+ */
204
+ class PhaseLockedRing {
205
+ constructor(primes, phaseType = 'phi') {
206
+ this.primes = primes;
207
+ this.n = primes.length;
208
+ this.phases = new Float64Array(this.n);
209
+
210
+ // Select irrational phase lock
211
+ this.phaseMultiplier = phaseType === 'phi' ? PHI :
212
+ phaseType === 'deltaS' ? DELTA_S :
213
+ 2 * Math.PI / PHI; // 2π/φ
214
+
215
+ // Initialize phases using irrational multiples
216
+ for (let i = 0; i < this.n; i++) {
217
+ this.phases[i] = (i * this.phaseMultiplier) % (2 * Math.PI);
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Advance all phases by one step
223
+ */
224
+ tick(dt = 0.01) {
225
+ for (let i = 0; i < this.n; i++) {
226
+ // Each prime oscillates at frequency proportional to log(p)
227
+ const freq = Math.log(this.primes[i]);
228
+ this.phases[i] = (this.phases[i] + freq * dt * this.phaseMultiplier) % (2 * Math.PI);
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Compute order parameter (Kuramoto-style)
234
+ * r = |1/N Σ e^(iθ_j)|
235
+ */
236
+ orderParameter() {
237
+ let sumReal = 0, sumImag = 0;
238
+ for (let i = 0; i < this.n; i++) {
239
+ sumReal += Math.cos(this.phases[i]);
240
+ sumImag += Math.sin(this.phases[i]);
241
+ }
242
+ return Math.sqrt(sumReal*sumReal + sumImag*sumImag) / this.n;
243
+ }
244
+
245
+ /**
246
+ * Mean phase
247
+ */
248
+ meanPhase() {
249
+ let sumReal = 0, sumImag = 0;
250
+ for (let i = 0; i < this.n; i++) {
251
+ sumReal += Math.cos(this.phases[i]);
252
+ sumImag += Math.sin(this.phases[i]);
253
+ }
254
+ return Math.atan2(sumImag, sumReal);
255
+ }
256
+
257
+ /**
258
+ * Synchronization measure
259
+ * How well are phases aligned?
260
+ */
261
+ synchronization() {
262
+ const order = this.orderParameter();
263
+ return order; // 0 = no sync, 1 = perfect sync
264
+ }
265
+
266
+ /**
267
+ * Apply phase correction toward target
268
+ */
269
+ correctPhase(targetPhase, strength = 0.1) {
270
+ for (let i = 0; i < this.n; i++) {
271
+ const diff = targetPhase - this.phases[i];
272
+ const correction = Math.sin(diff) * strength;
273
+ this.phases[i] = (this.phases[i] + correction) % (2 * Math.PI);
274
+ }
275
+ }
276
+
277
+ /**
278
+ * Get phase vector as complex amplitudes
279
+ */
280
+ toPrimeState(primesList = null) {
281
+ const state = new PrimeState(primesList || this.primes);
282
+ for (let i = 0; i < this.n && i < state.primes.length; i++) {
283
+ const p = this.primes[i];
284
+ if (state.amplitudes.has(p)) {
285
+ state.set(p, Complex.fromPolar(1, this.phases[i]));
286
+ }
287
+ }
288
+ return state.normalize();
289
+ }
290
+
291
+ getPhases() {
292
+ return [...this.phases];
293
+ }
294
+ }
295
+
296
+ /**
297
+ * Holographic Memory Field
298
+ * 2D spatial representation of prime-encoded information
299
+ * I(x,y) = Σ A_p e^(-S(x,y)) e^(ipθ)
300
+ */
301
+ class HolographicField {
302
+ constructor(width = 64, height = 64, primes = null) {
303
+ this.width = width;
304
+ this.height = height;
305
+ this.primes = primes || firstNPrimes(25);
306
+
307
+ // Complex amplitude field
308
+ this.field = new Array(height);
309
+ for (let y = 0; y < height; y++) {
310
+ this.field[y] = new Array(width);
311
+ for (let x = 0; x < width; x++) {
312
+ this.field[y][x] = Complex.zero();
313
+ }
314
+ }
315
+
316
+ // Entropy surface S(x,y)
317
+ this.entropyField = new Array(height);
318
+ for (let y = 0; y < height; y++) {
319
+ this.entropyField[y] = new Float64Array(width);
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Encode a PrimeState into the holographic field
325
+ */
326
+ encodeState(state, centerX = null, centerY = null) {
327
+ centerX = centerX ?? this.width / 2;
328
+ centerY = centerY ?? this.height / 2;
329
+
330
+ const sigma = Math.min(this.width, this.height) / 4; // Spread
331
+
332
+ for (let y = 0; y < this.height; y++) {
333
+ for (let x = 0; x < this.width; x++) {
334
+ const dx = x - centerX;
335
+ const dy = y - centerY;
336
+ const r = Math.sqrt(dx*dx + dy*dy);
337
+ const theta = Math.atan2(dy, dx);
338
+
339
+ // Gaussian envelope
340
+ const envelope = Math.exp(-r*r / (2*sigma*sigma));
341
+
342
+ // Sum over primes
343
+ let sum = Complex.zero();
344
+ for (const p of state.primes) {
345
+ const amp = state.get(p);
346
+ const phase = p * theta; // e^(ipθ)
347
+ const contribution = amp.mul(Complex.fromPolar(envelope, phase));
348
+ sum = sum.add(contribution);
349
+ }
350
+
351
+ this.field[y][x] = this.field[y][x].add(sum);
352
+
353
+ // Update entropy field
354
+ const intensity = sum.norm2();
355
+ if (intensity > 1e-10) {
356
+ this.entropyField[y][x] += -intensity * Math.log2(intensity);
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ /**
363
+ * Decode field at a point back to PrimeState
364
+ */
365
+ decodeAt(x, y, radius = 5) {
366
+ const state = new PrimeState(this.primes);
367
+
368
+ // Sample in a small neighborhood
369
+ for (let dy = -radius; dy <= radius; dy++) {
370
+ for (let dx = -radius; dx <= radius; dx++) {
371
+ const px = Math.floor(x + dx);
372
+ const py = Math.floor(y + dy);
373
+
374
+ if (px >= 0 && px < this.width && py >= 0 && py < this.height) {
375
+ const val = this.field[py][px];
376
+ const theta = Math.atan2(dy, dx);
377
+
378
+ // Recover prime contributions
379
+ for (const p of this.primes) {
380
+ const phase = -p * theta; // Inverse of encoding phase
381
+ const contribution = val.mul(Complex.fromPolar(1, phase));
382
+ state.set(p, state.get(p).add(contribution.scale(1 / (4 * radius * radius))));
383
+ }
384
+ }
385
+ }
386
+ }
387
+
388
+ return state.normalize();
389
+ }
390
+
391
+ /**
392
+ * Get total intensity at a point
393
+ */
394
+ intensity(x, y) {
395
+ if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
396
+ return this.field[Math.floor(y)][Math.floor(x)].norm2();
397
+ }
398
+ return 0;
399
+ }
400
+
401
+ /**
402
+ * Get local entropy at a point
403
+ */
404
+ localEntropy(x, y) {
405
+ if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
406
+ return this.entropyField[Math.floor(y)][Math.floor(x)];
407
+ }
408
+ return 0;
409
+ }
410
+
411
+ /**
412
+ * Find regions of high intensity (potential memory fragments)
413
+ */
414
+ findPeaks(threshold = 0.1) {
415
+ const peaks = [];
416
+ const maxIntensity = this.maxIntensity();
417
+
418
+ for (let y = 1; y < this.height - 1; y++) {
419
+ for (let x = 1; x < this.width - 1; x++) {
420
+ const intensity = this.field[y][x].norm2();
421
+
422
+ if (intensity > threshold * maxIntensity) {
423
+ // Check if local maximum
424
+ let isMax = true;
425
+ for (let dy = -1; dy <= 1 && isMax; dy++) {
426
+ for (let dx = -1; dx <= 1 && isMax; dx++) {
427
+ if (dx !== 0 || dy !== 0) {
428
+ if (this.field[y+dy][x+dx].norm2() > intensity) {
429
+ isMax = false;
430
+ }
431
+ }
432
+ }
433
+ }
434
+
435
+ if (isMax) {
436
+ peaks.push({ x, y, intensity, phase: this.field[y][x].phase() });
437
+ }
438
+ }
439
+ }
440
+ }
441
+
442
+ return peaks.sort((a, b) => b.intensity - a.intensity);
443
+ }
444
+
445
+ maxIntensity() {
446
+ let max = 0;
447
+ for (let y = 0; y < this.height; y++) {
448
+ for (let x = 0; x < this.width; x++) {
449
+ const i = this.field[y][x].norm2();
450
+ if (i > max) max = i;
451
+ }
452
+ }
453
+ return max;
454
+ }
455
+
456
+ /**
457
+ * Clear the field
458
+ */
459
+ clear() {
460
+ for (let y = 0; y < this.height; y++) {
461
+ for (let x = 0; x < this.width; x++) {
462
+ this.field[y][x] = Complex.zero();
463
+ this.entropyField[y][x] = 0;
464
+ }
465
+ }
466
+ }
467
+
468
+ /**
469
+ * Export as grayscale image data (intensity map)
470
+ */
471
+ toImageData() {
472
+ const data = new Uint8ClampedArray(this.width * this.height * 4);
473
+ const max = this.maxIntensity();
474
+
475
+ for (let y = 0; y < this.height; y++) {
476
+ for (let x = 0; x < this.width; x++) {
477
+ const idx = (y * this.width + x) * 4;
478
+ const intensity = max > 0 ? this.field[y][x].norm2() / max : 0;
479
+ const phase = (this.field[y][x].phase() + Math.PI) / (2 * Math.PI);
480
+
481
+ // HSV to RGB (hue = phase, value = intensity)
482
+ const h = phase * 360;
483
+ const s = 1;
484
+ const v = Math.sqrt(intensity); // sqrt for gamma correction
485
+
486
+ const [r, g, b] = hsvToRgb(h, s, v);
487
+ data[idx] = r;
488
+ data[idx + 1] = g;
489
+ data[idx + 2] = b;
490
+ data[idx + 3] = 255;
491
+ }
492
+ }
493
+
494
+ return { width: this.width, height: this.height, data };
495
+ }
496
+ }
497
+
498
+ // Helper: HSV to RGB conversion
499
+ function hsvToRgb(h, s, v) {
500
+ const c = v * s;
501
+ const x = c * (1 - Math.abs((h / 60) % 2 - 1));
502
+ const m = v - c;
503
+
504
+ let r = 0, g = 0, b = 0;
505
+ if (h < 60) { r = c; g = x; }
506
+ else if (h < 120) { r = x; g = c; }
507
+ else if (h < 180) { g = c; b = x; }
508
+ else if (h < 240) { g = x; b = c; }
509
+ else if (h < 300) { r = x; b = c; }
510
+ else { r = c; b = x; }
511
+
512
+ return [
513
+ Math.round((r + m) * 255),
514
+ Math.round((g + m) * 255),
515
+ Math.round((b + m) * 255)
516
+ ];
517
+ }
518
+
519
+ /**
520
+ * Entangled Node (from ResoLang spec)
521
+ * A network node with PRI, phase ring, and holographic memory
522
+ */
523
+ class EntangledNode {
524
+ constructor(id, pri = null) {
525
+ this.id = id;
526
+ this.pri = pri || PrimeResonanceIdentity.random();
527
+ this.phaseRing = new PhaseLockedRing(firstNPrimes(16));
528
+ this.holographicMemory = new HolographicField(32, 32);
529
+ this.entanglementMap = new Map(); // nodeId -> strength
530
+ this.coherence = 1.0;
531
+ }
532
+
533
+ /**
534
+ * Establish entanglement with another node
535
+ */
536
+ entangleWith(other) {
537
+ const strength = this.pri.entanglementStrength(other.pri);
538
+ this.entanglementMap.set(other.id, strength);
539
+ other.entanglementMap.set(this.id, strength);
540
+ return strength;
541
+ }
542
+
543
+ /**
544
+ * Store a memory fragment
545
+ */
546
+ storeMemory(state, x = null, y = null) {
547
+ this.holographicMemory.encodeState(state, x, y);
548
+ }
549
+
550
+ /**
551
+ * Retrieve memory at position
552
+ */
553
+ retrieveMemory(x, y) {
554
+ return this.holographicMemory.decodeAt(x, y);
555
+ }
556
+
557
+ /**
558
+ * Advance node state
559
+ */
560
+ tick(dt = 0.01) {
561
+ this.phaseRing.tick(dt);
562
+
563
+ // Coherence decays slightly over time
564
+ this.coherence *= (1 - 0.001 * dt);
565
+
566
+ // But increases with synchronization
567
+ this.coherence = Math.min(1, this.coherence + this.phaseRing.synchronization() * 0.002 * dt);
568
+ }
569
+
570
+ /**
571
+ * Get current state as PrimeState
572
+ */
573
+ getState() {
574
+ return this.phaseRing.toPrimeState();
575
+ }
576
+
577
+ /**
578
+ * Check if stable (coherent and synchronized)
579
+ */
580
+ isStable() {
581
+ return this.coherence > 0.85 && this.phaseRing.synchronization() > 0.7;
582
+ }
583
+
584
+ toJSON() {
585
+ return {
586
+ id: this.id,
587
+ pri: this.pri.toJSON(),
588
+ coherence: this.coherence,
589
+ synchronization: this.phaseRing.synchronization(),
590
+ entanglements: Array.from(this.entanglementMap.entries())
591
+ };
592
+ }
593
+ }
594
+
595
+ /**
596
+ * Resonant Fragment (from ResoLang spec)
597
+ * A portable memory fragment that can be teleported
598
+ */
599
+ class ResonantFragment {
600
+ constructor(state, centerX = 0, centerY = 0) {
601
+ this.state = state;
602
+ this.centerX = centerX;
603
+ this.centerY = centerY;
604
+ this.entropy = state.entropy();
605
+ this.createdAt = Date.now();
606
+ }
607
+
608
+ /**
609
+ * Create from text
610
+ */
611
+ static fromText(text) {
612
+ const { encodeMemory } = require('./hilbert');
613
+ const state = encodeMemory(text);
614
+ return new ResonantFragment(state);
615
+ }
616
+
617
+ /**
618
+ * Create from prime list
619
+ */
620
+ static fromPrimes(primes, weights = null) {
621
+ const state = new PrimeState();
622
+ for (let i = 0; i < primes.length; i++) {
623
+ const p = primes[i];
624
+ const w = weights ? weights[i] : 1 / primes.length;
625
+ if (state.amplitudes.has(p)) {
626
+ state.set(p, new Complex(w, 0));
627
+ }
628
+ }
629
+ return new ResonantFragment(state.normalize());
630
+ }
631
+
632
+ /**
633
+ * Tensor product with another fragment
634
+ */
635
+ tensorWith(other) {
636
+ // Combine states via multiplication (non-commutative in quaternionic extension)
637
+ const combined = new PrimeState(this.state.primes);
638
+ for (const p of this.state.primes) {
639
+ const a = this.state.get(p);
640
+ const b = other.state.get(p);
641
+ combined.set(p, a.mul(b));
642
+ }
643
+ return new ResonantFragment(combined.normalize());
644
+ }
645
+
646
+ /**
647
+ * Rotate phase of fragment
648
+ */
649
+ rotatePhase(angle) {
650
+ const rotation = Complex.fromPolar(1, angle);
651
+ const rotated = this.state.scale(rotation);
652
+ return new ResonantFragment(rotated);
653
+ }
654
+
655
+ /**
656
+ * Check coherence with another fragment
657
+ */
658
+ coherenceWith(other) {
659
+ return this.state.coherence(other.state);
660
+ }
661
+
662
+ /**
663
+ * Get dominant primes
664
+ */
665
+ dominant(n = 5) {
666
+ return this.state.dominant(n);
667
+ }
668
+
669
+ toJSON() {
670
+ return {
671
+ amplitudes: this.state.toArray(),
672
+ center: [this.centerX, this.centerY],
673
+ entropy: this.entropy,
674
+ dominant: this.dominant(3)
675
+ };
676
+ }
677
+ }
678
+
679
+ module.exports = {
680
+ // Constants
681
+ PHI,
682
+ PHI_CONJ,
683
+ DELTA_S,
684
+
685
+ // Classes
686
+ QuaternionPrime,
687
+ PrimeResonanceIdentity,
688
+ PhaseLockedRing,
689
+ HolographicField,
690
+ EntangledNode,
691
+ ResonantFragment,
692
+
693
+ // Helpers
694
+ hsvToRgb
695
+ };