Skip to content

Instantly share code, notes, and snippets.

@Yaffle
Last active November 29, 2024 19:29
Show Gist options
  • Save Yaffle/4726e0eddd069ac9c0d95ef1af1c910d to your computer and use it in GitHub Desktop.
Save Yaffle/4726e0eddd069ac9c0d95ef1af1c910d to your computer and use it in GitHub Desktop.
function add(a, b) {
const c = new Array(Math.max(a.length, b.length));
const min = Math.min(a.length, b.length);
for (let i = 0; i < min; i += 1) {
c[i] = a[i] + b[i];
}
for (let i = min; i < a.length; i += 1) {
c[i] = a[i];
}
for (let i = min; i < b.length; i += 1) {
c[i] = b[i];
}
return c;
}
function reversalN(A, n) {
return new Array(n - A.length).fill(0n).concat(A.slice(0).reverse());
}
LaurentSeries.prototype.multiplyMod1 = function (polynomial, precision, p) {
const e = this.lowestDegree;
const a = this.coefficients;
const b = polynomial;
if (degree(b) > degree(a)) {
throw new RangeError();
}
if (degree(b) < 0) {
return b;
}
// x**(e + k) >= x**-precision
const k = 0 - precision - e;
if (k < degree(b)) {
throw new RangeError('inacurracy');
}
const a1 = reversalN(low(reversalN(a, degree(a) + 1), degree(a) + 1 - k), degree(a) + 1 - k);
const a0 = low(a, Math.max(k, 0));
const m1 = multiplyLow(low(a1, precision), low(b, precision), precision);
const m2 = low(reversalN(multiplyLow(
low(reversalN(a0, Math.max(k, degree(a0) + 1)), degree(b)),
low(reversalN(b, degree(b) + 1), degree(b)),
degree(b)), degree(b)), precision);
let m = add(m1, m2);
m = mod(m, p);
return new LaurentSeries(m, 0 - precision);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment