UNPKG

@uor-foundation/operators

Version:

Layer 3: Arithmetic operators - operations as chemical reactions between numbers

128 lines 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CarryOperator = void 0; const field_substrate_1 = require("@uor-foundation/field-substrate"); /** * Carry operator 𝒞(m,n) - encodes all interference effects during multiplication * This is the quantum mechanism where the universe creates/destroys information */ class CarryOperator { constructor(substrate) { this.substrate = substrate; } /** * Compute the carry operator 𝒞(m,n) for multiplication m × n * This encodes which fields vanish and which emerge during the operation */ compute(m, n) { // Get field patterns for the operands const patternM = this.substrate.getFieldPattern(m); const patternN = this.substrate.getFieldPattern(n); // Get the product's pattern const product = m * n; const patternProduct = this.substrate.getFieldPattern(product); // The carry operator is the XOR difference between: // 1. What we'd expect from simple XOR (patternM ⊕ patternN) // 2. What we actually get (patternProduct) const carry = new Array(field_substrate_1.FIELD_COUNT); for (let i = 0; i < field_substrate_1.FIELD_COUNT; i++) { // Expected from simple XOR const expected = patternM[i] !== patternN[i]; // Actual result const actual = patternProduct[i]; // Carry bit encodes the difference carry[i] = expected !== actual; } return carry; } /** * Compute partial products for binary multiplication * This is used in the full carry computation algorithm */ computePartialProducts(m, n) { // Convert to binary representations const bitsM = this.toBinary(m); const bitsN = this.toBinary(n); // Calculate maximum possible bits in result const maxBits = bitsM.length + bitsN.length; const partialProducts = new Array(maxBits).fill(0); // Compute s_k = Σ_{i+j=k} a_i * c_j for (let i = 0; i < bitsM.length; i++) { for (let j = 0; j < bitsN.length; j++) { if (bitsM[i] && bitsN[j]) { partialProducts[i + j]++; } } } return partialProducts; } /** * Propagate carries through the partial products */ propagateCarries(partialProducts) { const result = new Array(partialProducts.length).fill(0); let carry = 0; for (let k = 0; k < partialProducts.length; k++) { result[k] = partialProducts[k] + carry; carry = Math.floor(result[k] / 2); result[k] = result[k] % 2; } return result; } /** * Convert bigint to binary array (LSB first) */ toBinary(n) { if (n === 0n) return [false]; const bits = []; let num = n < 0n ? -n : n; while (num > 0n) { bits.push((num & 1n) === 1n); num >>= 1n; } return bits; } /** * Analyze denormalization artifacts from multiplication */ analyzeArtifacts(m, n) { const patternM = this.substrate.getFieldPattern(m); const patternN = this.substrate.getFieldPattern(n); const product = m * n; const patternProduct = this.substrate.getFieldPattern(product); const carry = this.compute(m, n); const artifacts = []; for (let i = 0; i < field_substrate_1.FIELD_COUNT; i++) { if (carry[i]) { // Determine if this is a vanishing or emergent field const activeInM = patternM[i]; const activeInN = patternN[i]; const activeInProduct = patternProduct[i]; if ((activeInM || activeInN) && !activeInProduct) { // Vanishing field artifacts.push({ type: 'vanishing', field: i, factors: [Number(m), Number(n)], product: Number(product), interferencePattern: { real: 0, imag: 0 }, // Simplified for now }); } else if (!activeInM && !activeInN && activeInProduct) { // Emergent field artifacts.push({ type: 'emergent', field: i, factors: [Number(m), Number(n)], product: Number(product), interferencePattern: { real: 1, imag: 0 }, // Simplified for now }); } } } return artifacts; } } exports.CarryOperator = CarryOperator; //# sourceMappingURL=carry.js.map