All MicroEvals
// Exact sparse polynomial ring Q[x,y]. Monomials are keyed ...
Create MicroEval

// Exact sparse polynomial ring Q[x,y]. Monomials are keyed ...

Prompt

// Exact sparse polynomial ring Q[x,y]. Monomials are keyed by i*64 + j for // x^i y^j; coefficients are exact BigInt rationals. import { type Frac, Q0, q, qadd, qdiv, qeq, qfmt, qisZero, qmul, qneg, qnum, qsub, } from "./rat"; export type Key = number; export const key = (i: number, j: number): Key => i * 64 + j; export const ki = (k: Key): number => Math.floor(k / 64); export const kj = (k: Key): number => k % 64; export interface Poly { m: Map<Key, Frac>; } export function pzero(): Poly { return { m: new Map() }; } export function pconst(c: Frac): Poly { const p = pzero(); if (!qisZero(c)) p.m.set(key(0, 0), c); return p; } export function pmono(i: number, j: number, c: Frac): Poly { const p = pzero(); if (!qisZero(c)) p.m.set(key(i, j), c); return p; } export const px: Poly = pmono(1, 0, q(1)); export const py: Poly = pmono(0, 1, q(1)); export const pone: Poly = pconst(q(1)); export function pclone(a: Poly): Poly { return { m: new Map(a.m) }; } export function padd(a: Poly, b: Poly): Poly { const out = pclone(a); for (const [k, v] of b.m) { const cur = out.m.get(k); const s = cur === undefined ? v : qadd(cur, v); if (qisZero(s)) out.m.delete(k); else out.m.set(k, s); } return out; } export function psub(a: Poly, b: Poly): Poly { const out = pclone(a); for (const [k, v] of b.m) { const cur = out.m.get(k); const s = cur === undefined ? qneg(v) : qsub(cur, v); if (qisZero(s)) out.m.delete(k); else out.m.set(k, s); } return out; } export function pscale(a: Poly, c: Frac): Poly { if (qisZero(c)) return pzero(); const out = pzero(); for (const [k, v] of a.m) out.m.set(k, qmul(v, c)); return out; } export function pmul(a: Poly, b: Poly): Poly { const out = pzero(); for (const [ka, va] of a.m) { const ia = ki(ka); const ja = kj(ka); for (const [kb, vb] of b.m) { const k = key(ia + ki(kb), ja + kj(kb)); const cur = out.m.get(k); const prod = qmul(va, vb); const s = cur === undefined ? prod : qadd(cur, prod); if (qisZero(s)) out.m.delete(k); else out.m.set(k, s); } } return out; } export function ppow(a: Poly, n: number): Poly { let out = pone; let base = a; let e = n; while (e > 0) { if (e & 1) out = pmul(out, base); base = pmul(base, base); e >>= 1; } return out; } export function pdx(a: Poly): Poly { const out = pzero(); for (const [k, v] of a.m) { const i = ki(k); if (i === 0) continue; out.m.set(key(i - 1, kj(k)), qmul(v, q(i))); } return out; } export function pdy(a: Poly): Poly { const out = pzero(); for (const [k, v] of a.m) { const j = kj(k); if (j === 0) continue; out.m.set(key(ki(k), j - 1), qmul(v, q(j))); } return out; } export const pisZero = (a: Poly): boolean => a.m.size === 0; export function peq(a: Poly, b: Poly): boolean { if (a.m.size !== b.m.size) return false; for (const [k, v] of a.m) { const w = b.m.get(k); if (w === undefined || !qeq(v, w)) return false; } return true; } export function pdeg(a: Poly): number { let d = -1; for (const k of a.m.keys()) d = Math.max(d, ki(k) + kj(k)); return d; } export interface Term { i: number; j: number; c: Frac; k: Key; } /** Terms in graded-lex order (highest total degree first). */ export function pterms(a: Poly): Term[] { const out: Term[] = []; for (const [k, c] of a.m) out.push({ i: ki(k), j: kj(k), c, k }); out.sort((u, v) => { const du = u.i + u.j; const dv = v.i + v.j; if (du !== dv) return dv - du; if (v.i !== u.i) return v.i - u.i; return u.j - v.j; }); return out; } export function peval(a: Poly, x: Frac, y: Frac): Frac { let s = Q0; for (const t of pterms(a)) { let term = t.c; for (let e = 0; e < t.i; e++) term = qmul(term, x); for (let e = 0; e < t.j; e++) term = qmul(term, y); s = qadd(s, term); } return s; } export function pevalNum(a: Poly, x: number, y: number): number { let s = 0; for (const [k, v] of a.m) { s += qnum(v) * Math.pow(x, ki(k)) * Math.pow(y, kj(k)); } return s; } const SUP = ["\u2070", "\u00b9", "\u00b2", "\u00b3", "\u2074", "\u2075", "\u2076", "\u2077", "\u2078", "\u2079"]; function sup(n: number): string { return String(n) .split("") .map((d) => SUP[Number(d)]) .join(""); } /** Human-readable plain-text rendering, e.g. "3x\u00b2y \u2212 (7/2)xy\u00b3 + 1". */ export function pfmt(a: Poly, opts: { forceSign?: boolean } = {}): string { const ts = pterms(a); if (ts.length === 0) return "0"; let out = ""; ts.forEach((t, idx) => { const neg = t.c.n < 0n; const abs = qfmt({ n: -t.c.n, d: t.c.d }); const isOne = t.c.d === 1n && (t.c.n === 1n || t.c.n === -1n); const frac = t.c.d !== 1n; let body: string; if (t.i === 0 && t.j === 0) body = abs; else { const num = isOne && !frac ? "" : frac ? `(${abs})` : abs; const xv = t.i === 0 ? "" : t.i === 1 ? "x" : `x${sup(t.i)}`; const yv = t.j === 0 ? "" : t.j === 1 ? "y" : `y${sup(t.j)}`; body = num + xv + yv; } if (idx === 0) out += (neg ? "\u2212" : opts.forceSign ? "+" : "") + body; else out += (neg ? " \u2212 " : " + ") + body; }); return out; } /** Parse a small polynomial spec like "x + y^2 - (3/2)*x^2*y". */ export function pparse(src: string): Poly | null { const clean = src.replace(/\s+/g, "").replace(/\u2212/g, "-").replace(/\^/g, "**"); if (clean === "") return pzero(); const tokens = clean.match(/[+-]?[^+-]+/g); if (!tokens) return null; let out = pzero(); for (const raw of tokens) { const t = raw.trim(); if (t === "") continue; const sign = t.startsWith("-") ? -1 : t.startsWith("+") ? 1 : 1; const body = t.replace(/^[+-]/, ""); // split coefficient from variables const m = /^(\(([^)]*)\)|(\d+\/\d+|\d*\.?\d+))?\*?(.*)$/.exec(body); if (!m) return null; const coefStr = m[2] ?? m[3] ?? "1"; const varStr = m[4] ?? ""; let coef: Frac; if (/^-?\d+\/\d+$/.test(coefStr)) { const [n, d] = coefStr.split("/"); coef = q(BigInt(n), BigInt(d)); } else if (/^-?\d+$/.test(coefStr)) coef = q(BigInt(coefStr)); else { const f = Number(coefStr); if (!Number.isFinite(f)) return null; coef = q(BigInt(Math.round(f * 1e6)), BigInt(1e6)); } if (sign < 0) coef = qneg(coef); // variables let ei = 0; let ej = 0; const vm = varStr.match(/([xy])(?:\*\*(\d+))?/g) ?? []; if (vm.join("").replace(/\*\*\d+/g, "") !== varStr) return null; for (const piece of vm) { const pm2 = /^([xy])(?:\*\*(\d+))?$/.exec(piece); if (!pm2) return null; const e = pm2[2] ? Number(pm2[2]) : 1; if (pm2[1] === "x") ei += e; else ej += e; } out = padd(out, pmono(ei, ej, coef)); } return out; } /** det J(F1,F2) - 1, the Keller defect. */ export function kellerDefect(f1: Poly, f2: Poly): Poly { const det = psub(pmul(pdx(f1), pdy(f2)), pmul(pdy(f1), pdx(f2))); return psub(det, pone); } export function pjacobian(f1: Poly, f2: Poly): Poly { return psub(pmul(pdx(f1), pdy(f2)), pmul(pdy(f1), pdx(f2))); } /** Divide by a rational, used for nullspace pivoting. */ export function prdiv(a: Poly, b: Frac): Poly { return pscale(a, qdiv(q(1), b)); } Send back the complete code with all the fixes. Fix each of the listed errors one by one, making sure to actually correct them so that there are 0 errors remaining. Keep the original imports, since the files exist. Write out every single character; do not abbreviate anything. Fix every error. There must be exactly one file. Do not write anything else; just output the complete code, and it must not contain any comments. Never, under any circumstances, use simplified, substitute, dummy, simulated, or fake code. Write the entire file as complete, unabridged, production-ready code in a single code block. It must be 100% error-free, a complete, error-free file, and must be submitted as a downloadable file. These requirements are mandatory and must be strictly adhered to. If no list of errors is provided, you must find all the errors and fix them. If there were comments in the original code, delete them. And most importantly: YOU MUST NEVER SIMPLIFY!

Drag to resize

Response not available

Drag to resize
Drag to resize
Drag to resize