@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.
- package/LICENSE +21 -0
- package/README.md +278 -0
- package/backends/cryptographic/index.js +196 -0
- package/backends/index.js +15 -0
- package/backends/interface.js +89 -0
- package/backends/scientific/index.js +272 -0
- package/backends/semantic/index.js +527 -0
- package/backends/semantic/surface.js +393 -0
- package/backends/semantic/two-layer.js +375 -0
- package/core/fano.js +127 -0
- package/core/hilbert.js +564 -0
- package/core/hypercomplex.js +141 -0
- package/core/index.js +133 -0
- package/core/llm.js +132 -0
- package/core/prime.js +184 -0
- package/core/resonance.js +695 -0
- package/core/rformer-tf.js +1086 -0
- package/core/rformer.js +806 -0
- package/core/sieve.js +350 -0
- package/data.json +8163 -0
- package/docs/EXAMPLES_PLAN.md +293 -0
- package/docs/README.md +159 -0
- package/docs/design/ALEPH_CHAT_ARCHITECTURE.md +499 -0
- package/docs/guide/01-quickstart.md +298 -0
- package/docs/guide/02-semantic-computing.md +409 -0
- package/docs/guide/03-cryptographic.md +420 -0
- package/docs/guide/04-scientific.md +494 -0
- package/docs/guide/05-llm-integration.md +568 -0
- package/docs/guide/06-advanced.md +996 -0
- package/docs/guide/README.md +188 -0
- package/docs/reference/01-core.md +695 -0
- package/docs/reference/02-physics.md +601 -0
- package/docs/reference/03-backends.md +892 -0
- package/docs/reference/04-engine.md +632 -0
- package/docs/reference/README.md +252 -0
- package/docs/theory/01-prime-semantics.md +327 -0
- package/docs/theory/02-hypercomplex-algebra.md +421 -0
- package/docs/theory/03-phase-synchronization.md +364 -0
- package/docs/theory/04-entropy-reasoning.md +348 -0
- package/docs/theory/05-non-commutativity.md +402 -0
- package/docs/theory/06-two-layer-meaning.md +414 -0
- package/docs/theory/07-resonant-field-interface.md +419 -0
- package/docs/theory/08-semantic-sieve.md +520 -0
- package/docs/theory/09-temporal-emergence.md +298 -0
- package/docs/theory/10-quaternionic-memory.md +415 -0
- package/docs/theory/README.md +162 -0
- package/engine/aleph.js +418 -0
- package/engine/index.js +7 -0
- package/index.js +23 -0
- package/modular.js +254 -0
- package/package.json +99 -0
- package/physics/collapse.js +95 -0
- package/physics/entropy.js +88 -0
- package/physics/index.js +65 -0
- package/physics/kuramoto.js +91 -0
- package/physics/lyapunov.js +80 -0
- package/physics/oscillator.js +95 -0
- package/types/index.d.ts +575 -0
package/core/hilbert.js
ADDED
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prime Hilbert Space (HP)
|
|
3
|
+
*
|
|
4
|
+
* Implementation of the formal Prime Hilbert space from the paper:
|
|
5
|
+
* "Programming Reality: Prime Resonance Systems for Memory, Computation, and Probability Control"
|
|
6
|
+
*
|
|
7
|
+
* HP = {|ψ⟩ = Σ αp|p⟩ : Σ|αp|² = 1, αp ∈ ℂ}
|
|
8
|
+
*
|
|
9
|
+
* Key features:
|
|
10
|
+
* - Complex amplitudes (not just real)
|
|
11
|
+
* - Prime basis states |p⟩
|
|
12
|
+
* - Composite states as prime products
|
|
13
|
+
* - Resonance operators (P̂, F̂, R̂, Ĉ)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const { isPrime, firstNPrimes, factorize } = require('./prime');
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Complex number class for amplitudes
|
|
20
|
+
*/
|
|
21
|
+
class Complex {
|
|
22
|
+
constructor(re = 0, im = 0) {
|
|
23
|
+
this.re = re;
|
|
24
|
+
this.im = im;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static fromPolar(r, theta) {
|
|
28
|
+
return new Complex(r * Math.cos(theta), r * Math.sin(theta));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static zero() { return new Complex(0, 0); }
|
|
32
|
+
static one() { return new Complex(1, 0); }
|
|
33
|
+
static i() { return new Complex(0, 1); }
|
|
34
|
+
|
|
35
|
+
add(other) {
|
|
36
|
+
return new Complex(this.re + other.re, this.im + other.im);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
sub(other) {
|
|
40
|
+
return new Complex(this.re - other.re, this.im - other.im);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
mul(other) {
|
|
44
|
+
return new Complex(
|
|
45
|
+
this.re * other.re - this.im * other.im,
|
|
46
|
+
this.re * other.im + this.im * other.re
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
scale(k) {
|
|
51
|
+
return new Complex(this.re * k, this.im * k);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
conj() {
|
|
55
|
+
return new Complex(this.re, -this.im);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
norm2() {
|
|
59
|
+
return this.re * this.re + this.im * this.im;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
norm() {
|
|
63
|
+
return Math.sqrt(this.norm2());
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
phase() {
|
|
67
|
+
return Math.atan2(this.im, this.re);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
normalize() {
|
|
71
|
+
const n = this.norm();
|
|
72
|
+
return n > 1e-10 ? this.scale(1/n) : Complex.zero();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exp() {
|
|
76
|
+
// e^(a+bi) = e^a * (cos(b) + i*sin(b))
|
|
77
|
+
const ea = Math.exp(this.re);
|
|
78
|
+
return new Complex(ea * Math.cos(this.im), ea * Math.sin(this.im));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
toString() {
|
|
82
|
+
if (Math.abs(this.im) < 1e-10) return `${this.re.toFixed(4)}`;
|
|
83
|
+
if (Math.abs(this.re) < 1e-10) return `${this.im.toFixed(4)}i`;
|
|
84
|
+
const sign = this.im >= 0 ? '+' : '';
|
|
85
|
+
return `${this.re.toFixed(4)}${sign}${this.im.toFixed(4)}i`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Prime Hilbert Space State
|
|
91
|
+
* |ψ⟩ = Σ αp|p⟩ where p ∈ P (primes)
|
|
92
|
+
*/
|
|
93
|
+
class PrimeState {
|
|
94
|
+
constructor(primes = null, maxPrime = 100) {
|
|
95
|
+
// Use provided primes or generate first N
|
|
96
|
+
this.primes = primes || firstNPrimes(25);
|
|
97
|
+
this.maxPrime = maxPrime;
|
|
98
|
+
|
|
99
|
+
// Map prime → index and index → prime
|
|
100
|
+
this.primeToIndex = new Map();
|
|
101
|
+
this.indexToPrime = new Map();
|
|
102
|
+
this.primes.forEach((p, i) => {
|
|
103
|
+
this.primeToIndex.set(p, i);
|
|
104
|
+
this.indexToPrime.set(i, p);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Complex amplitudes αp for each prime
|
|
108
|
+
this.amplitudes = new Map();
|
|
109
|
+
for (const p of this.primes) {
|
|
110
|
+
this.amplitudes.set(p, Complex.zero());
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Create a basis state |p⟩
|
|
116
|
+
*/
|
|
117
|
+
static basis(p, primes = null) {
|
|
118
|
+
const state = new PrimeState(primes);
|
|
119
|
+
if (state.amplitudes.has(p)) {
|
|
120
|
+
state.amplitudes.set(p, Complex.one());
|
|
121
|
+
}
|
|
122
|
+
return state;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Create a uniform superposition over primes
|
|
127
|
+
*/
|
|
128
|
+
static uniform(primes = null) {
|
|
129
|
+
const state = new PrimeState(primes);
|
|
130
|
+
const n = state.primes.length;
|
|
131
|
+
const amp = new Complex(1 / Math.sqrt(n), 0);
|
|
132
|
+
for (const p of state.primes) {
|
|
133
|
+
state.amplitudes.set(p, amp);
|
|
134
|
+
}
|
|
135
|
+
return state;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Create a composite state from number n = Π p_i^a_i
|
|
140
|
+
* |n⟩ = Π|p_i⟩^a_i (tensor product abstraction)
|
|
141
|
+
*/
|
|
142
|
+
static composite(n, primes = null) {
|
|
143
|
+
const state = new PrimeState(primes);
|
|
144
|
+
const factors = factorize(n);
|
|
145
|
+
|
|
146
|
+
// Amplitude weighted by multiplicity
|
|
147
|
+
let totalWeight = 0;
|
|
148
|
+
for (const [p, exp] of Object.entries(factors)) {
|
|
149
|
+
totalWeight += exp;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (totalWeight === 0) return state;
|
|
153
|
+
|
|
154
|
+
for (const [p, exp] of Object.entries(factors)) {
|
|
155
|
+
const prime = parseInt(p);
|
|
156
|
+
if (state.amplitudes.has(prime)) {
|
|
157
|
+
state.amplitudes.set(prime, new Complex(exp / totalWeight, 0));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return state.normalize();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Get amplitude for prime p
|
|
165
|
+
*/
|
|
166
|
+
get(p) {
|
|
167
|
+
return this.amplitudes.get(p) || Complex.zero();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Set amplitude for prime p
|
|
172
|
+
*/
|
|
173
|
+
set(p, amplitude) {
|
|
174
|
+
if (this.amplitudes.has(p)) {
|
|
175
|
+
this.amplitudes.set(p, amplitude);
|
|
176
|
+
}
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Add states: |ψ⟩ + |φ⟩
|
|
182
|
+
*/
|
|
183
|
+
add(other) {
|
|
184
|
+
const result = new PrimeState(this.primes);
|
|
185
|
+
for (const p of this.primes) {
|
|
186
|
+
result.amplitudes.set(p, this.get(p).add(other.get(p)));
|
|
187
|
+
}
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Scale state by complex number: c|ψ⟩
|
|
193
|
+
*/
|
|
194
|
+
scale(c) {
|
|
195
|
+
const result = new PrimeState(this.primes);
|
|
196
|
+
const coeff = c instanceof Complex ? c : new Complex(c, 0);
|
|
197
|
+
for (const p of this.primes) {
|
|
198
|
+
result.amplitudes.set(p, this.get(p).mul(coeff));
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Inner product ⟨φ|ψ⟩
|
|
205
|
+
*/
|
|
206
|
+
inner(other) {
|
|
207
|
+
let sum = Complex.zero();
|
|
208
|
+
for (const p of this.primes) {
|
|
209
|
+
sum = sum.add(this.get(p).conj().mul(other.get(p)));
|
|
210
|
+
}
|
|
211
|
+
return sum;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Norm ||ψ||
|
|
216
|
+
*/
|
|
217
|
+
norm() {
|
|
218
|
+
let sum = 0;
|
|
219
|
+
for (const p of this.primes) {
|
|
220
|
+
sum += this.get(p).norm2();
|
|
221
|
+
}
|
|
222
|
+
return Math.sqrt(sum);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Normalize to unit vector
|
|
227
|
+
*/
|
|
228
|
+
normalize() {
|
|
229
|
+
const n = this.norm();
|
|
230
|
+
if (n < 1e-10) return this;
|
|
231
|
+
return this.scale(new Complex(1/n, 0));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Entropy S(ψ) = -Σ |αp|² log |αp|²
|
|
236
|
+
*/
|
|
237
|
+
entropy() {
|
|
238
|
+
let h = 0;
|
|
239
|
+
const n2 = this.norm() ** 2;
|
|
240
|
+
if (n2 < 1e-10) return 0;
|
|
241
|
+
|
|
242
|
+
for (const p of this.primes) {
|
|
243
|
+
const prob = this.get(p).norm2() / n2;
|
|
244
|
+
if (prob > 1e-10) {
|
|
245
|
+
h -= prob * Math.log2(prob);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return h;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Coherence with another state: |⟨φ|ψ⟩|²
|
|
253
|
+
*/
|
|
254
|
+
coherence(other) {
|
|
255
|
+
return this.inner(other).norm2();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Get dominant primes (highest amplitude)
|
|
260
|
+
*/
|
|
261
|
+
dominant(n = 3) {
|
|
262
|
+
return this.primes
|
|
263
|
+
.map(p => ({ p, amp: this.get(p).norm() }))
|
|
264
|
+
.sort((a, b) => b.amp - a.amp)
|
|
265
|
+
.slice(0, n);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Born measurement: probabilistic collapse
|
|
270
|
+
*/
|
|
271
|
+
measure() {
|
|
272
|
+
const n2 = this.norm() ** 2;
|
|
273
|
+
if (n2 < 1e-10) return { prime: this.primes[0], probability: 1 };
|
|
274
|
+
|
|
275
|
+
const r = Math.random() * n2;
|
|
276
|
+
let cumulative = 0;
|
|
277
|
+
|
|
278
|
+
for (const p of this.primes) {
|
|
279
|
+
cumulative += this.get(p).norm2();
|
|
280
|
+
if (r < cumulative) {
|
|
281
|
+
return {
|
|
282
|
+
prime: p,
|
|
283
|
+
probability: this.get(p).norm2() / n2
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
prime: this.primes[this.primes.length - 1],
|
|
290
|
+
probability: this.get(this.primes[this.primes.length - 1]).norm2() / n2
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Convert to array representation
|
|
296
|
+
*/
|
|
297
|
+
toArray() {
|
|
298
|
+
return this.primes.map(p => ({
|
|
299
|
+
prime: p,
|
|
300
|
+
amplitude: this.get(p),
|
|
301
|
+
probability: this.get(p).norm2()
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
clone() {
|
|
306
|
+
const copy = new PrimeState(this.primes);
|
|
307
|
+
for (const p of this.primes) {
|
|
308
|
+
copy.amplitudes.set(p, new Complex(this.get(p).re, this.get(p).im));
|
|
309
|
+
}
|
|
310
|
+
return copy;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Resonance Operators from the paper
|
|
316
|
+
*/
|
|
317
|
+
const ResonanceOperators = {
|
|
318
|
+
/**
|
|
319
|
+
* Prime operator P̂|p⟩ = p|p⟩
|
|
320
|
+
* Eigenvalue is the prime itself
|
|
321
|
+
*/
|
|
322
|
+
P(state) {
|
|
323
|
+
const result = new PrimeState(state.primes);
|
|
324
|
+
for (const p of state.primes) {
|
|
325
|
+
result.amplitudes.set(p, state.get(p).scale(p));
|
|
326
|
+
}
|
|
327
|
+
return result;
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Factorization operator F̂|n⟩ = Σ|p_i⟩
|
|
332
|
+
* Decomposes composite state into prime components
|
|
333
|
+
*/
|
|
334
|
+
F(state) {
|
|
335
|
+
const result = new PrimeState(state.primes);
|
|
336
|
+
// Already in prime basis - identity for basis states
|
|
337
|
+
// For composite: distribute amplitude to factors
|
|
338
|
+
for (const p of state.primes) {
|
|
339
|
+
const amp = state.get(p);
|
|
340
|
+
if (amp.norm() > 1e-10) {
|
|
341
|
+
result.amplitudes.set(p, amp);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return result.normalize();
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Resonance operator R̂(n)|p⟩ = e^(2πi log_p(n))|p⟩
|
|
349
|
+
* Creates phase rotation based on logarithmic relationship
|
|
350
|
+
*/
|
|
351
|
+
R(n) {
|
|
352
|
+
return (state) => {
|
|
353
|
+
const result = new PrimeState(state.primes);
|
|
354
|
+
const logN = Math.log(n);
|
|
355
|
+
|
|
356
|
+
for (const p of state.primes) {
|
|
357
|
+
const logP = Math.log(p);
|
|
358
|
+
const phase = 2 * Math.PI * logN / logP;
|
|
359
|
+
const rotation = Complex.fromPolar(1, phase);
|
|
360
|
+
result.amplitudes.set(p, state.get(p).mul(rotation));
|
|
361
|
+
}
|
|
362
|
+
return result;
|
|
363
|
+
};
|
|
364
|
+
},
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Coupling operator Ĉ|ψ⟩ = Σ e^(iφ_pq) ⟨q|ψ⟩|p⟩
|
|
368
|
+
* Phase coupling based on prime relationships
|
|
369
|
+
*/
|
|
370
|
+
C(n) {
|
|
371
|
+
return (state) => {
|
|
372
|
+
const result = new PrimeState(state.primes);
|
|
373
|
+
const logN = Math.log(n);
|
|
374
|
+
|
|
375
|
+
for (const p of state.primes) {
|
|
376
|
+
let sum = Complex.zero();
|
|
377
|
+
for (const q of state.primes) {
|
|
378
|
+
// φ_pq = 2π(log_p(n) - log_q(n))
|
|
379
|
+
const phase = 2 * Math.PI * (logN / Math.log(p) - logN / Math.log(q));
|
|
380
|
+
const rotation = Complex.fromPolar(1, phase);
|
|
381
|
+
sum = sum.add(rotation.mul(state.get(q)));
|
|
382
|
+
}
|
|
383
|
+
result.amplitudes.set(p, sum.scale(1 / state.primes.length));
|
|
384
|
+
}
|
|
385
|
+
return result.normalize();
|
|
386
|
+
};
|
|
387
|
+
},
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Hadamard-like superposition operator
|
|
391
|
+
*/
|
|
392
|
+
H(state) {
|
|
393
|
+
const result = new PrimeState(state.primes);
|
|
394
|
+
const n = state.primes.length;
|
|
395
|
+
const norm = 1 / Math.sqrt(n);
|
|
396
|
+
|
|
397
|
+
for (const p of state.primes) {
|
|
398
|
+
let sum = Complex.zero();
|
|
399
|
+
for (const [i, q] of state.primes.entries()) {
|
|
400
|
+
const phase = 2 * Math.PI * i * state.primeToIndex.get(p) / n;
|
|
401
|
+
sum = sum.add(state.get(q).mul(Complex.fromPolar(1, phase)));
|
|
402
|
+
}
|
|
403
|
+
result.amplitudes.set(p, sum.scale(norm));
|
|
404
|
+
}
|
|
405
|
+
return result;
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Entropy-driven evolution from equation (7)
|
|
411
|
+
* d|Ψ(t)⟩/dt = iĤ|Ψ(t)⟩ - λ(R̂ - r_stable)|Ψ(t)⟩
|
|
412
|
+
*/
|
|
413
|
+
class EntropyDrivenEvolution {
|
|
414
|
+
constructor(state, options = {}) {
|
|
415
|
+
this.state = state.clone();
|
|
416
|
+
this.lambda = options.lambda || 0.1; // Decay rate
|
|
417
|
+
this.rStable = options.rStable || 0.5; // Stable resonance target
|
|
418
|
+
this.dt = options.dt || 0.01; // Time step
|
|
419
|
+
this.time = 0;
|
|
420
|
+
this.entropyIntegral = 0;
|
|
421
|
+
this.history = [];
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Single time step evolution
|
|
426
|
+
*/
|
|
427
|
+
step() {
|
|
428
|
+
const s0 = this.state.entropy();
|
|
429
|
+
|
|
430
|
+
// Hamiltonian evolution (rotation in Hilbert space)
|
|
431
|
+
const rotatedState = ResonanceOperators.R(Math.exp(this.time))(this.state);
|
|
432
|
+
|
|
433
|
+
// Entropy-driven damping
|
|
434
|
+
const currentR = this.state.norm();
|
|
435
|
+
const dampingFactor = 1 - this.lambda * (currentR - this.rStable) * this.dt;
|
|
436
|
+
|
|
437
|
+
// Update state
|
|
438
|
+
this.state = rotatedState.scale(new Complex(dampingFactor, 0)).normalize();
|
|
439
|
+
|
|
440
|
+
// Track entropy
|
|
441
|
+
const s1 = this.state.entropy();
|
|
442
|
+
this.entropyIntegral += s1 * this.dt;
|
|
443
|
+
this.time += this.dt;
|
|
444
|
+
|
|
445
|
+
this.history.push({
|
|
446
|
+
time: this.time,
|
|
447
|
+
entropy: s1,
|
|
448
|
+
entropyIntegral: this.entropyIntegral,
|
|
449
|
+
dominant: this.state.dominant(3)
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
return this.state;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Evolve until collapse condition met
|
|
457
|
+
* Equation (8): P_collapse = 1 - e^(-∫S(t)dt)
|
|
458
|
+
*/
|
|
459
|
+
evolveUntilCollapse(maxSteps = 1000) {
|
|
460
|
+
for (let i = 0; i < maxSteps; i++) {
|
|
461
|
+
this.step();
|
|
462
|
+
|
|
463
|
+
const pCollapse = 1 - Math.exp(-this.entropyIntegral);
|
|
464
|
+
if (Math.random() < pCollapse * this.dt) {
|
|
465
|
+
return {
|
|
466
|
+
collapsed: true,
|
|
467
|
+
steps: i + 1,
|
|
468
|
+
probability: pCollapse,
|
|
469
|
+
finalState: this.state.measure()
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return {
|
|
475
|
+
collapsed: false,
|
|
476
|
+
steps: maxSteps,
|
|
477
|
+
probability: 1 - Math.exp(-this.entropyIntegral),
|
|
478
|
+
finalState: this.state.dominant(1)[0]
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Get evolution history
|
|
484
|
+
*/
|
|
485
|
+
getHistory() {
|
|
486
|
+
return this.history;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Memory encoding as in equation (9)
|
|
492
|
+
* |M⟩ = Σ αp|p⟩
|
|
493
|
+
*/
|
|
494
|
+
function encodeMemory(text, primes = null) {
|
|
495
|
+
const state = new PrimeState(primes);
|
|
496
|
+
|
|
497
|
+
// Map characters to primes with phase encoding
|
|
498
|
+
const chars = text.toLowerCase().split('');
|
|
499
|
+
const n = chars.length;
|
|
500
|
+
|
|
501
|
+
for (let i = 0; i < chars.length; i++) {
|
|
502
|
+
const charCode = chars[i].charCodeAt(0);
|
|
503
|
+
// Use prime at index charCode % numPrimes
|
|
504
|
+
const primeIdx = charCode % state.primes.length;
|
|
505
|
+
const p = state.primes[primeIdx];
|
|
506
|
+
|
|
507
|
+
// Phase encodes position, amplitude encodes frequency
|
|
508
|
+
const phase = 2 * Math.PI * i / n;
|
|
509
|
+
const currentAmp = state.get(p);
|
|
510
|
+
const newAmp = currentAmp.add(Complex.fromPolar(1/n, phase));
|
|
511
|
+
state.set(p, newAmp);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
return state.normalize();
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Symbolic computation via iterative entropy minimization
|
|
519
|
+
* Equation (10): |Ψ₀⟩ = Σ c_i|R_i⟩ → |r_stable⟩
|
|
520
|
+
*/
|
|
521
|
+
function symbolicCompute(inputStates, maxIterations = 100, coherenceThreshold = 0.9) {
|
|
522
|
+
if (inputStates.length === 0) return null;
|
|
523
|
+
|
|
524
|
+
// Superposition of input states
|
|
525
|
+
let state = inputStates[0].clone();
|
|
526
|
+
for (let i = 1; i < inputStates.length; i++) {
|
|
527
|
+
state = state.add(inputStates[i]);
|
|
528
|
+
}
|
|
529
|
+
state = state.normalize();
|
|
530
|
+
|
|
531
|
+
const evolution = new EntropyDrivenEvolution(state, {
|
|
532
|
+
lambda: 0.15,
|
|
533
|
+
rStable: coherenceThreshold
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
// Evolve toward stable resonance
|
|
537
|
+
let prevEntropy = state.entropy();
|
|
538
|
+
for (let i = 0; i < maxIterations; i++) {
|
|
539
|
+
evolution.step();
|
|
540
|
+
const currentEntropy = evolution.state.entropy();
|
|
541
|
+
|
|
542
|
+
// Check for stable state (entropy no longer decreasing)
|
|
543
|
+
if (prevEntropy - currentEntropy < 0.001) {
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
prevEntropy = currentEntropy;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return {
|
|
550
|
+
result: evolution.state,
|
|
551
|
+
iterations: evolution.history.length,
|
|
552
|
+
finalEntropy: evolution.state.entropy(),
|
|
553
|
+
dominant: evolution.state.dominant(5)
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
module.exports = {
|
|
558
|
+
Complex,
|
|
559
|
+
PrimeState,
|
|
560
|
+
ResonanceOperators,
|
|
561
|
+
EntropyDrivenEvolution,
|
|
562
|
+
encodeMemory,
|
|
563
|
+
symbolicCompute
|
|
564
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @class Hypercomplex
|
|
3
|
+
* @description Generic Cayley-Dickson algebra of dimension 2^n
|
|
4
|
+
*
|
|
5
|
+
* Dimension 2: Complex numbers
|
|
6
|
+
* Dimension 4: Quaternions (non-commutative)
|
|
7
|
+
* Dimension 8: Octonions (non-associative)
|
|
8
|
+
* Dimension 16: Sedenions (zero-divisors exist)
|
|
9
|
+
* Dimension 32: Pathions (further structure loss)
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { multiplyIndices } = require('./fano');
|
|
13
|
+
|
|
14
|
+
class Hypercomplex {
|
|
15
|
+
constructor(dim, components = null) {
|
|
16
|
+
if (!Number.isInteger(Math.log2(dim))) {
|
|
17
|
+
throw new Error('Dimension must be power of 2');
|
|
18
|
+
}
|
|
19
|
+
this.dim = dim;
|
|
20
|
+
this.c = components instanceof Float64Array
|
|
21
|
+
? components
|
|
22
|
+
: new Float64Array(dim);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Factory methods
|
|
26
|
+
static zero(dim) { return new Hypercomplex(dim); }
|
|
27
|
+
|
|
28
|
+
static basis(dim, index, value = 1) {
|
|
29
|
+
const h = new Hypercomplex(dim);
|
|
30
|
+
h.c[index] = value;
|
|
31
|
+
return h;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static fromReal(dim, real) {
|
|
35
|
+
const h = new Hypercomplex(dim);
|
|
36
|
+
h.c[0] = real;
|
|
37
|
+
return h;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static fromArray(arr) {
|
|
41
|
+
return new Hypercomplex(arr.length, Float64Array.from(arr));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Arithmetic operations
|
|
45
|
+
add(other) {
|
|
46
|
+
const r = new Hypercomplex(this.dim);
|
|
47
|
+
for (let i = 0; i < this.dim; i++) r.c[i] = this.c[i] + other.c[i];
|
|
48
|
+
return r;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
sub(other) {
|
|
52
|
+
const r = new Hypercomplex(this.dim);
|
|
53
|
+
for (let i = 0; i < this.dim; i++) r.c[i] = this.c[i] - other.c[i];
|
|
54
|
+
return r;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
scale(k) {
|
|
58
|
+
const r = new Hypercomplex(this.dim);
|
|
59
|
+
for (let i = 0; i < this.dim; i++) r.c[i] = this.c[i] * k;
|
|
60
|
+
return r;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
mul(other) {
|
|
64
|
+
// Cayley-Dickson multiplication via table lookup
|
|
65
|
+
const r = new Hypercomplex(this.dim);
|
|
66
|
+
for (let i = 0; i < this.dim; i++) {
|
|
67
|
+
for (let j = 0; j < this.dim; j++) {
|
|
68
|
+
const [k, s] = multiplyIndices(this.dim, i, j);
|
|
69
|
+
r.c[k] += s * this.c[i] * other.c[j];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return r;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
conjugate() {
|
|
76
|
+
const r = new Hypercomplex(this.dim);
|
|
77
|
+
r.c[0] = this.c[0];
|
|
78
|
+
for (let i = 1; i < this.dim; i++) r.c[i] = -this.c[i];
|
|
79
|
+
return r;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
inverse() {
|
|
83
|
+
const n2 = this.dot(this);
|
|
84
|
+
if (n2 < 1e-10) return Hypercomplex.zero(this.dim);
|
|
85
|
+
return this.conjugate().scale(1 / n2);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Metrics
|
|
89
|
+
norm() { return Math.sqrt(this.dot(this)); }
|
|
90
|
+
|
|
91
|
+
normalize() {
|
|
92
|
+
const n = this.norm();
|
|
93
|
+
return n > 1e-10 ? this.scale(1 / n) : Hypercomplex.zero(this.dim);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
dot(other) {
|
|
97
|
+
let s = 0;
|
|
98
|
+
for (let i = 0; i < this.dim; i++) s += this.c[i] * other.c[i];
|
|
99
|
+
return s;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Information theory
|
|
103
|
+
entropy() {
|
|
104
|
+
const n = this.norm();
|
|
105
|
+
if (n < 1e-10) return 0;
|
|
106
|
+
let h = 0;
|
|
107
|
+
for (let i = 0; i < this.dim; i++) {
|
|
108
|
+
const p = (this.c[i] / n) ** 2;
|
|
109
|
+
if (p > 1e-10) h -= p * Math.log2(p);
|
|
110
|
+
}
|
|
111
|
+
return h;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
coherence(other) {
|
|
115
|
+
const n1 = this.norm(), n2 = other.norm();
|
|
116
|
+
return (n1 > 1e-10 && n2 > 1e-10) ? Math.abs(this.dot(other)) / (n1 * n2) : 0;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Zero-divisor detection (for dim >= 16)
|
|
120
|
+
isZeroDivisorWith(other) {
|
|
121
|
+
const prod = this.mul(other);
|
|
122
|
+
return this.norm() > 0.1 && other.norm() > 0.1 && prod.norm() < 0.01;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Dominant axes
|
|
126
|
+
dominantAxes(n = 3) {
|
|
127
|
+
return [...this.c]
|
|
128
|
+
.map((v, i) => ({ i, v: Math.abs(v) }))
|
|
129
|
+
.sort((a, b) => b.v - a.v)
|
|
130
|
+
.slice(0, n);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Serialization
|
|
134
|
+
toArray() { return [...this.c]; }
|
|
135
|
+
|
|
136
|
+
clone() {
|
|
137
|
+
return new Hypercomplex(this.dim, Float64Array.from(this.c));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = { Hypercomplex };
|