@aleph-ai/tinyaleph 1.6.0 → 1.6.2

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/hilbert.js CHANGED
@@ -1148,12 +1148,302 @@ function eulerTotient(n) {
1148
1148
  return Math.round(result);
1149
1149
  }
1150
1150
 
1151
+ // ============================================================================
1152
+ // VON MANGOLDT FUNCTION (from book.pdf Chapter 4)
1153
+ // ============================================================================
1154
+
1155
+ /**
1156
+ * Von Mangoldt function Λ(n)
1157
+ *
1158
+ * Λ(n) = log p if n = p^k for some prime p and k ≥ 1
1159
+ * Λ(n) = 0 otherwise
1160
+ *
1161
+ * From book.pdf Chapter 4: "Number-Theoretic Operators"
1162
+ * The Von Mangoldt function detects prime powers and weights them by log(p).
1163
+ *
1164
+ * @param {number} n - Positive integer
1165
+ * @returns {number} Von Mangoldt function value
1166
+ */
1167
+ function vonMangoldt(n) {
1168
+ if (n <= 1) return 0;
1169
+
1170
+ const factors = factorize(n);
1171
+ const primeFactors = Object.keys(factors);
1172
+
1173
+ // Λ(n) is non-zero only if n = p^k (single prime factor)
1174
+ if (primeFactors.length !== 1) return 0;
1175
+
1176
+ const p = parseInt(primeFactors[0]);
1177
+ return Math.log(p);
1178
+ }
1179
+
1180
+ /**
1181
+ * Liouville function λ(n)
1182
+ *
1183
+ * λ(n) = (-1)^Ω(n) where Ω(n) is total number of prime factors with multiplicity
1184
+ *
1185
+ * @param {number} n - Positive integer
1186
+ * @returns {number} Liouville function value (-1 or 1)
1187
+ */
1188
+ function liouvilleFunction(n) {
1189
+ if (n === 1) return 1;
1190
+
1191
+ const factors = factorize(n);
1192
+
1193
+ // Count total prime factors with multiplicity
1194
+ let omega = 0;
1195
+ for (const exp of Object.values(factors)) {
1196
+ omega += exp;
1197
+ }
1198
+
1199
+ return Math.pow(-1, omega);
1200
+ }
1201
+
1202
+ /**
1203
+ * Divisor count function d(n) = σ₀(n)
1204
+ *
1205
+ * Counts the number of positive divisors of n.
1206
+ * d(n) = Π (a_i + 1) where n = Π p_i^a_i
1207
+ *
1208
+ * @param {number} n - Positive integer
1209
+ * @returns {number} Number of divisors
1210
+ */
1211
+ function divisorCount(n) {
1212
+ if (n === 1) return 1;
1213
+
1214
+ const factors = factorize(n);
1215
+
1216
+ let count = 1;
1217
+ for (const exp of Object.values(factors)) {
1218
+ count *= (exp + 1);
1219
+ }
1220
+
1221
+ return count;
1222
+ }
1223
+
1224
+ // ============================================================================
1225
+ // DIRICHLET CHARACTERS (from book.pdf Chapter 4)
1226
+ // ============================================================================
1227
+
1228
+ /**
1229
+ * Dirichlet Character χ_d(n) (Legendre symbol extension)
1230
+ *
1231
+ * For quadratic characters modulo d:
1232
+ * χ_d(n) = (n/d) Legendre/Jacobi symbol
1233
+ *
1234
+ * From book.pdf Chapter 4: Dirichlet characters encode modular arithmetic symmetries
1235
+ *
1236
+ * @param {number} n - Integer
1237
+ * @param {number} d - Modulus (conductor)
1238
+ * @returns {number} Character value: -1, 0, or 1
1239
+ */
1240
+ function dirichletCharacter(n, d) {
1241
+ // Use Jacobi symbol as quadratic Dirichlet character
1242
+ return jacobiSymbol(n, d);
1243
+ }
1244
+
1245
+ /**
1246
+ * Jacobi symbol (n/m) - generalization of Legendre symbol
1247
+ *
1248
+ * Computed using quadratic reciprocity
1249
+ *
1250
+ * @param {number} n - Numerator
1251
+ * @param {number} m - Denominator (positive odd integer)
1252
+ * @returns {number} -1, 0, or 1
1253
+ */
1254
+ function jacobiSymbol(n, m) {
1255
+ if (m <= 0 || m % 2 === 0) {
1256
+ throw new Error('Jacobi symbol denominator must be positive and odd');
1257
+ }
1258
+
1259
+ n = ((n % m) + m) % m; // Reduce n mod m
1260
+
1261
+ if (n === 0) return 0;
1262
+ if (n === 1) return 1;
1263
+ if (m === 1) return 1;
1264
+
1265
+ // Factor out powers of 2 from n
1266
+ let twos = 0;
1267
+ while (n % 2 === 0) {
1268
+ n /= 2;
1269
+ twos++;
1270
+ }
1271
+
1272
+ // (2/m) = (-1)^((m²-1)/8)
1273
+ let result = 1;
1274
+ if (twos % 2 === 1) {
1275
+ const mMod8 = m % 8;
1276
+ if (mMod8 === 3 || mMod8 === 5) {
1277
+ result = -result;
1278
+ }
1279
+ }
1280
+
1281
+ if (n === 1) return result;
1282
+
1283
+ // Quadratic reciprocity: (n/m)(m/n) = (-1)^((n-1)(m-1)/4)
1284
+ if (n % 4 === 3 && m % 4 === 3) {
1285
+ result = -result;
1286
+ }
1287
+
1288
+ return result * jacobiSymbol(m % n, n);
1289
+ }
1290
+
1291
+ /**
1292
+ * Principal Dirichlet character χ₀ mod d
1293
+ *
1294
+ * χ₀(n) = 1 if gcd(n, d) = 1, else 0
1295
+ *
1296
+ * @param {number} n - Integer
1297
+ * @param {number} d - Modulus
1298
+ * @returns {number} 0 or 1
1299
+ */
1300
+ function principalCharacter(n, d) {
1301
+ return gcd(n, d) === 1 ? 1 : 0;
1302
+ }
1303
+
1304
+ /**
1305
+ * Greatest common divisor
1306
+ */
1307
+ function gcd(a, b) {
1308
+ a = Math.abs(Math.floor(a));
1309
+ b = Math.abs(Math.floor(b));
1310
+ while (b > 0) {
1311
+ [a, b] = [b, a % b];
1312
+ }
1313
+ return a;
1314
+ }
1315
+
1316
+ /**
1317
+ * Generate all Dirichlet characters modulo d
1318
+ *
1319
+ * For small d, enumerate character table
1320
+ *
1321
+ * @param {number} d - Modulus
1322
+ * @returns {Array<Function>} Array of character functions
1323
+ */
1324
+ function generateDirichletCharacters(d) {
1325
+ const phi_d = eulerTotient(d);
1326
+ const characters = [];
1327
+
1328
+ // Principal character always exists
1329
+ characters.push((n) => principalCharacter(n, d));
1330
+
1331
+ // For prime modulus, we can generate all characters
1332
+ if (isPrime(d)) {
1333
+ // Find primitive root
1334
+ const g = findPrimitiveRoot(d);
1335
+ if (g) {
1336
+ // Generate characters via powers of primitive root
1337
+ for (let k = 1; k < phi_d; k++) {
1338
+ const exponent = k;
1339
+ characters.push((n) => {
1340
+ if (gcd(n, d) !== 1) return 0;
1341
+ const idx = discreteLog(n % d, g, d);
1342
+ if (idx === -1) return 0;
1343
+ return Complex.fromPolar(1, 2 * Math.PI * exponent * idx / phi_d);
1344
+ });
1345
+ }
1346
+ }
1347
+ }
1348
+
1349
+ return characters;
1350
+ }
1351
+
1352
+ /**
1353
+ * Find a primitive root modulo p (for prime p)
1354
+ */
1355
+ function findPrimitiveRoot(p) {
1356
+ if (!isPrime(p)) return null;
1357
+ if (p === 2) return 1;
1358
+
1359
+ const phi = p - 1;
1360
+ const factors = factorize(phi);
1361
+
1362
+ for (let g = 2; g < p; g++) {
1363
+ let isPrimitive = true;
1364
+ for (const q of Object.keys(factors)) {
1365
+ if (modPow(g, phi / parseInt(q), p) === 1) {
1366
+ isPrimitive = false;
1367
+ break;
1368
+ }
1369
+ }
1370
+ if (isPrimitive) return g;
1371
+ }
1372
+ return null;
1373
+ }
1374
+
1375
+ /**
1376
+ * Modular exponentiation: a^b mod m
1377
+ */
1378
+ function modPow(a, b, m) {
1379
+ let result = 1;
1380
+ a = a % m;
1381
+ while (b > 0) {
1382
+ if (b % 2 === 1) {
1383
+ result = (result * a) % m;
1384
+ }
1385
+ b = Math.floor(b / 2);
1386
+ a = (a * a) % m;
1387
+ }
1388
+ return result;
1389
+ }
1390
+
1391
+ /**
1392
+ * Discrete logarithm: find x such that g^x ≡ n (mod p)
1393
+ * Uses baby-step giant-step for small p
1394
+ */
1395
+ function discreteLog(n, g, p) {
1396
+ n = ((n % p) + p) % p;
1397
+ if (n === 1) return 0;
1398
+
1399
+ const m = Math.ceil(Math.sqrt(p));
1400
+
1401
+ // Baby step: compute g^j for j = 0..m-1
1402
+ const table = new Map();
1403
+ let val = 1;
1404
+ for (let j = 0; j < m; j++) {
1405
+ table.set(val, j);
1406
+ val = (val * g) % p;
1407
+ }
1408
+
1409
+ // Giant step: compute n * (g^-m)^i for i = 0..m-1
1410
+ const gInvM = modPow(modInverse(g, p), m, p);
1411
+ val = n;
1412
+ for (let i = 0; i < m; i++) {
1413
+ if (table.has(val)) {
1414
+ return i * m + table.get(val);
1415
+ }
1416
+ val = (val * gInvM) % p;
1417
+ }
1418
+
1419
+ return -1; // Not found
1420
+ }
1421
+
1422
+ /**
1423
+ * Modular inverse using extended Euclidean algorithm
1424
+ */
1425
+ function modInverse(a, m) {
1426
+ let [old_r, r] = [a, m];
1427
+ let [old_s, s] = [1, 0];
1428
+
1429
+ while (r !== 0) {
1430
+ const q = Math.floor(old_r / r);
1431
+ [old_r, r] = [r, old_r - q * r];
1432
+ [old_s, s] = [s, old_s - q * s];
1433
+ }
1434
+
1435
+ return ((old_s % m) + m) % m;
1436
+ }
1437
+
1151
1438
  /**
1152
1439
  * Extended Resonance Operators including Möbius and Euler Totient
1153
1440
  *
1154
- * From qis.pdf:
1441
+ * From book.pdf Chapters 2 and 4:
1155
1442
  * - M̂|n⟩ = μ(n)|n⟩ (Möbius operator)
1156
1443
  * - Ê|n⟩ = exp(2πiφ(n)/n)|n⟩ (Euler phase operator)
1444
+ * - ζ̂(s)|n⟩ = n^(-s)|n⟩ (Zeta operator)
1445
+ * - Λ̂|n⟩ = Λ(n)|n⟩ (Von Mangoldt operator)
1446
+ * - χ̂_d(n)|n⟩ = χ_d(n)|n⟩ (Dirichlet character operator)
1157
1447
  */
1158
1448
  const ExtendedOperators = {
1159
1449
  /**
@@ -1213,6 +1503,205 @@ const ExtendedOperators = {
1213
1503
  result.amplitudes.set(p, state.get(p).scale(-1));
1214
1504
  }
1215
1505
  return result;
1506
+ },
1507
+
1508
+ // ==========================================================================
1509
+ // NEW OPERATORS FROM book.pdf Chapter 4
1510
+ // ==========================================================================
1511
+
1512
+ /**
1513
+ * Zeta Operator: ζ̂(s)|n⟩ = n^(-s)|n⟩
1514
+ *
1515
+ * From book.pdf Chapter 4: "Number-Theoretic Operators"
1516
+ *
1517
+ * The Zeta operator scales each prime basis state by p^(-s).
1518
+ * This connects to the Riemann zeta function ζ(s) = Σ n^(-s).
1519
+ *
1520
+ * For complex s = σ + it:
1521
+ * - Real part σ controls amplitude decay
1522
+ * - Imaginary part t controls phase rotation
1523
+ *
1524
+ * @param {number|Complex} s - The zeta parameter (real or complex)
1525
+ * @returns {Function} Operator function that acts on PrimeState
1526
+ */
1527
+ Zeta(s) {
1528
+ const sComplex = s instanceof Complex ? s : new Complex(s, 0);
1529
+
1530
+ return (state) => {
1531
+ const result = new PrimeState(state.primes);
1532
+
1533
+ for (const p of state.primes) {
1534
+ // p^(-s) = exp(-s * log(p))
1535
+ const logP = Math.log(p);
1536
+ const exponent = new Complex(-sComplex.re * logP, -sComplex.im * logP);
1537
+ const scaling = exponent.exp();
1538
+
1539
+ result.amplitudes.set(p, state.get(p).mul(scaling));
1540
+ }
1541
+
1542
+ return result;
1543
+ };
1544
+ },
1545
+
1546
+ /**
1547
+ * Von Mangoldt Operator: Λ̂|n⟩ = Λ(n)|n⟩
1548
+ *
1549
+ * From book.pdf Chapter 4: "Number-Theoretic Operators"
1550
+ *
1551
+ * For prime basis states |p⟩: Λ̂|p⟩ = log(p)|p⟩
1552
+ * For non-prime-power: Λ̂|n⟩ = 0
1553
+ *
1554
+ * This operator detects and weights prime powers by their logarithm.
1555
+ * It's the "derivative" of the prime counting function.
1556
+ */
1557
+ Lambda(state) {
1558
+ const result = new PrimeState(state.primes);
1559
+
1560
+ for (const p of state.primes) {
1561
+ // For prime basis states, Λ(p) = log(p)
1562
+ const lambda = vonMangoldt(p);
1563
+ result.amplitudes.set(p, state.get(p).scale(lambda));
1564
+ }
1565
+
1566
+ return result;
1567
+ },
1568
+
1569
+ /**
1570
+ * Dirichlet Character Operator: χ̂_d|n⟩ = χ_d(n)|n⟩
1571
+ *
1572
+ * From book.pdf Chapter 4: "Number-Theoretic Operators"
1573
+ *
1574
+ * Applies Dirichlet character modulo d to scale amplitudes.
1575
+ * For quadratic characters, this uses the Jacobi symbol.
1576
+ *
1577
+ * @param {number} d - The modulus (conductor) of the character
1578
+ * @returns {Function} Operator function
1579
+ */
1580
+ Chi(d) {
1581
+ return (state) => {
1582
+ const result = new PrimeState(state.primes);
1583
+
1584
+ for (const p of state.primes) {
1585
+ try {
1586
+ // Use quadratic character (Jacobi symbol)
1587
+ const chi = d % 2 === 1 ? jacobiSymbol(p, d) : principalCharacter(p, d);
1588
+ result.amplitudes.set(p, state.get(p).scale(chi));
1589
+ } catch (e) {
1590
+ // For even d, fall back to principal character
1591
+ result.amplitudes.set(p, state.get(p).scale(principalCharacter(p, d)));
1592
+ }
1593
+ }
1594
+
1595
+ return result;
1596
+ };
1597
+ },
1598
+
1599
+ /**
1600
+ * Principal Character Operator: χ̂₀|n⟩ = χ₀(n)|n⟩
1601
+ *
1602
+ * χ₀(n) = 1 if gcd(n, d) = 1, else 0
1603
+ * Projects onto states coprime to d.
1604
+ *
1605
+ * @param {number} d - Modulus
1606
+ * @returns {Function} Operator function
1607
+ */
1608
+ Chi0(d) {
1609
+ return (state) => {
1610
+ const result = new PrimeState(state.primes);
1611
+
1612
+ for (const p of state.primes) {
1613
+ const chi0 = principalCharacter(p, d);
1614
+ result.amplitudes.set(p, state.get(p).scale(chi0));
1615
+ }
1616
+
1617
+ return result;
1618
+ };
1619
+ },
1620
+
1621
+ /**
1622
+ * General Von Mangoldt Operator with scaling
1623
+ * Λ̂_α|n⟩ = Λ(n)^α |n⟩
1624
+ *
1625
+ * Allows fractional powers of the Von Mangoldt function.
1626
+ *
1627
+ * @param {number} alpha - Power to raise Λ(n)
1628
+ * @returns {Function} Operator function
1629
+ */
1630
+ LambdaAlpha(alpha) {
1631
+ return (state) => {
1632
+ const result = new PrimeState(state.primes);
1633
+
1634
+ for (const p of state.primes) {
1635
+ const lambda = vonMangoldt(p);
1636
+ const scaled = lambda > 0 ? Math.pow(lambda, alpha) : 0;
1637
+ result.amplitudes.set(p, state.get(p).scale(scaled));
1638
+ }
1639
+
1640
+ return result;
1641
+ };
1642
+ },
1643
+
1644
+ /**
1645
+ * Spectral Zeta Analysis: Compute ζ(s) expectation
1646
+ *
1647
+ * ⟨ψ|ζ̂(s)|ψ⟩ = Σ |αp|² p^(-s)
1648
+ *
1649
+ * Returns complex value representing weighted prime sum.
1650
+ *
1651
+ * @param {PrimeState} state - Input state
1652
+ * @param {number|Complex} s - Zeta parameter
1653
+ * @returns {Complex} Expectation value
1654
+ */
1655
+ zetaExpectation(state, s) {
1656
+ const sComplex = s instanceof Complex ? s : new Complex(s, 0);
1657
+ let sum = Complex.zero();
1658
+
1659
+ for (const p of state.primes) {
1660
+ const prob = state.get(p).norm2();
1661
+ if (prob > 1e-10) {
1662
+ const logP = Math.log(p);
1663
+ const exponent = new Complex(-sComplex.re * logP, -sComplex.im * logP);
1664
+ const scaling = exponent.exp();
1665
+ sum = sum.add(scaling.scale(prob));
1666
+ }
1667
+ }
1668
+
1669
+ return sum;
1670
+ },
1671
+
1672
+ /**
1673
+ * Symbolic Zeta Function approximation
1674
+ *
1675
+ * Computes ζ(s) ≈ Σ_{p≤N} 1/(1 - p^(-s)) (Euler product over primes)
1676
+ *
1677
+ * @param {number|Complex} s - Zeta parameter (Re(s) > 1 for convergence)
1678
+ * @param {Array<number>} primes - Primes to sum over
1679
+ * @returns {Complex} Approximate zeta value
1680
+ */
1681
+ zetaEulerProduct(s, primes = null) {
1682
+ const ps = primes || [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47];
1683
+ const sComplex = s instanceof Complex ? s : new Complex(s, 0);
1684
+
1685
+ let product = Complex.one();
1686
+
1687
+ for (const p of ps) {
1688
+ const logP = Math.log(p);
1689
+ const exponent = new Complex(-sComplex.re * logP, -sComplex.im * logP);
1690
+ const pMinusS = exponent.exp(); // p^(-s)
1691
+
1692
+ // (1 - p^(-s))^(-1)
1693
+ const denominator = Complex.one().sub(pMinusS);
1694
+ const denom2 = denominator.norm2();
1695
+ if (denom2 > 1e-10) {
1696
+ const inverse = new Complex(
1697
+ denominator.re / denom2,
1698
+ -denominator.im / denom2
1699
+ );
1700
+ product = product.mul(inverse);
1701
+ }
1702
+ }
1703
+
1704
+ return product;
1216
1705
  }
1217
1706
  };
1218
1707
 
@@ -1661,13 +2150,27 @@ export {
1661
2150
  pAdicValuation,
1662
2151
  pAdicCoherence,
1663
2152
 
1664
- // Number theory functions
2153
+ // Number theory functions (book.pdf Chapter 4)
1665
2154
  mobiusFunction,
1666
2155
  eulerTotient,
2156
+ vonMangoldt,
2157
+ liouvilleFunction,
2158
+ divisorCount,
2159
+
2160
+ // Dirichlet characters and number theory helpers
2161
+ dirichletCharacter,
2162
+ jacobiSymbol,
2163
+ principalCharacter,
2164
+ generateDirichletCharacters,
2165
+ findPrimitiveRoot,
2166
+ modPow,
2167
+ discreteLog,
2168
+ modInverse,
2169
+ gcd,
1667
2170
 
1668
2171
  // Extended operators and calculators
1669
2172
  ExtendedOperators,
1670
2173
  KnowledgeResonanceCalculator,
1671
2174
  FibonacciWaveletBank,
1672
2175
  SemanticPatternDetector
1673
- };
2176
+ };
package/core/index.js CHANGED
@@ -101,7 +101,21 @@ import { Complex,
101
101
  HolographicField,
102
102
  EntangledNode,
103
103
  ResonantFragment,
104
- DELTA_S } from './hilbert.js';
104
+ DELTA_S,
105
+ // Number theory functions (book.pdf Chapter 4)
106
+ vonMangoldt,
107
+ liouvilleFunction,
108
+ divisorCount,
109
+ // Dirichlet characters
110
+ dirichletCharacter,
111
+ jacobiSymbol,
112
+ principalCharacter,
113
+ generateDirichletCharacters,
114
+ findPrimitiveRoot,
115
+ modPow,
116
+ discreteLog,
117
+ modInverse,
118
+ gcd } from './hilbert.js';
105
119
 
106
120
  // Golden Ratio Resonance (from symprime symbolic AI)
107
121
  import { ResonanceCalculator,
@@ -430,7 +444,28 @@ export {
430
444
  createSignatureExtractor
431
445
  };
432
446
 
447
+ // Export gravity module (book.pdf Chapter 6/16)
448
+ export * from './gravity.js';
449
+
450
+ // Export oracle module (book.pdf Chapter 10/11)
451
+ export * from './oracle.js';
452
+
453
+ // Export emotion module (book.pdf Chapter 9)
454
+ export * from './emotion.js';
455
+
456
+ // Export non-local communication module (book.pdf Chapter 8)
457
+ export * from './nonlocal.js';
458
+
459
+ import * as gravity from './gravity.js';
460
+ import * as oracle from './oracle.js';
461
+ import * as emotion from './emotion.js';
462
+ import * as nonlocal from './nonlocal.js';
463
+
433
464
  export default {
465
+ ...gravity,
466
+ ...oracle,
467
+ ...emotion,
468
+ ...nonlocal,
434
469
  // Hypercomplex algebra
435
470
  Hypercomplex,
436
471
 
@@ -702,4 +737,4 @@ export default {
702
737
  extractSignature,
703
738
  createSignatureMemory,
704
739
  createSignatureExtractor
705
- };
740
+ };