tla-ts 0.0.10-dev.5ffc55ce5 → 0.0.10-dev.6232a1403
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/dist/deciders.cjs +35 -35
- package/dist/deciders.js +30 -30
- package/dist/run.cjs +1 -1
- package/dist/run.js +1 -1
- package/package.json +1 -1
- package/src/prover/smt/solver/ast.ts +1 -1
- package/src/prover/smt/solver/expr2polynomial.ts +79 -204
- package/src/prover/smt/solver/factor.ts +14 -36
- package/src/prover/smt/solver/goal2nlsat.ts +30 -57
- package/src/prover/smt/solver/lp/nla-core.ts +1 -1
- package/src/prover/smt/solver/lp/nra-solver.ts +39 -45
- package/src/prover/smt/solver/nlsat/algebraic.ts +89 -194
- package/src/prover/smt/solver/nlsat/atoms.ts +33 -52
- package/src/prover/smt/solver/nlsat/explain.ts +217 -375
- package/src/prover/smt/solver/nlsat/levelwise.ts +240 -464
- package/src/prover/smt/solver/nlsat/polynomial.ts +680 -2400
- package/src/prover/smt/solver/nlsat/solver.ts +64 -74
- package/src/prover/smt/solver/nlsat/transcendentals.ts +17 -41
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +4 -8
- package/src/prover/smt/solver/nlsat/upolynomial.ts +17 -40
- package/src/prover/smt/solver/opt-nlsat.ts +18 -29
- package/src/prover/smt/solver/opt-solver.ts +3 -2
- package/src/prover/smt/solver/smtlib-parser.ts +10 -10
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { SmtInvariantError } from "../../../../errors";
|
|
2
|
-
import { RandomGen } from "../random-gen";
|
|
3
2
|
import { getGcd, Rational } from "../rational";
|
|
4
|
-
import
|
|
5
|
-
import { BIG_PRIMES, getExtendedGcd, UpolynomialManager } from "./upolynomial";
|
|
3
|
+
import { floorDiv, UpolynomialManager } from "./upolynomial";
|
|
6
4
|
import {
|
|
7
5
|
DEFAULT_FACTOR_PARAMS,
|
|
8
6
|
factorSquareFree,
|
|
@@ -32,24 +30,6 @@ const RATIONAL_OPS: ValueOps<Rational> = {
|
|
|
32
30
|
const getMonomialKey = (vars: readonly number[], degrees: readonly number[]) =>
|
|
33
31
|
vars.map((variable, index) => `${variable}^${degrees[index]}`).join(",");
|
|
34
32
|
|
|
35
|
-
const normalizeZp = (value: bigint, modulus: bigint) => {
|
|
36
|
-
const upper = modulus / 2n;
|
|
37
|
-
const lower = modulus % 2n === 0n ? -upper + 1n : -upper;
|
|
38
|
-
const rest = value % modulus;
|
|
39
|
-
return rest > upper ? rest - modulus : rest < lower ? rest + modulus : rest;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const divMpz = (dividend: bigint, divisor: bigint) => {
|
|
43
|
-
const quotient = dividend / divisor;
|
|
44
|
-
if (dividend >= 0n || dividend % divisor === 0n) return quotient;
|
|
45
|
-
return divisor < 0n ? quotient + 1n : quotient - 1n;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const modMpz = (value: bigint, modulus: bigint) => {
|
|
49
|
-
const rest = value % modulus;
|
|
50
|
-
return rest < 0n ? rest + (modulus < 0n ? -modulus : modulus) : rest;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
33
|
class Monomial {
|
|
54
34
|
readonly totalDegree: number;
|
|
55
35
|
|
|
@@ -85,11 +65,6 @@ class Monomial {
|
|
|
85
65
|
for (const current of this.vars) if (current < variable) result = current;
|
|
86
66
|
return result;
|
|
87
67
|
}
|
|
88
|
-
|
|
89
|
-
isSquare() {
|
|
90
|
-
for (const degree of this.degrees) if (degree % 2 === 1) return false;
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
68
|
}
|
|
94
69
|
|
|
95
70
|
const lexCompare = (first: Monomial, second: Monomial) => {
|
|
@@ -114,45 +89,6 @@ const lexCompare = (first: Monomial, second: Monomial) => {
|
|
|
114
89
|
return firstIndex < 0 ? -1 : 1;
|
|
115
90
|
};
|
|
116
91
|
|
|
117
|
-
const lexCompare2 = (first: Monomial, second: Monomial, minVar: number) => {
|
|
118
|
-
if (first === second) return 0;
|
|
119
|
-
let firstIndex = first.size - 1;
|
|
120
|
-
let secondIndex = second.size - 1;
|
|
121
|
-
let firstMinVarDegree = 0;
|
|
122
|
-
let secondMinVarDegree = 0;
|
|
123
|
-
while (firstIndex >= 0 && secondIndex >= 0) {
|
|
124
|
-
const firstVar = first.vars[firstIndex];
|
|
125
|
-
const secondVar = second.vars[secondIndex];
|
|
126
|
-
if (firstVar === minVar) {
|
|
127
|
-
firstMinVarDegree = first.degrees[firstIndex];
|
|
128
|
-
firstIndex--;
|
|
129
|
-
if (secondVar === minVar) {
|
|
130
|
-
secondMinVarDegree = second.degrees[secondIndex];
|
|
131
|
-
secondIndex--;
|
|
132
|
-
}
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
if (secondVar === minVar) {
|
|
136
|
-
secondMinVarDegree = second.degrees[secondIndex];
|
|
137
|
-
secondIndex--;
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
if (firstVar === secondVar) {
|
|
141
|
-
const firstDegree = first.degrees[firstIndex];
|
|
142
|
-
const secondDegree = second.degrees[secondIndex];
|
|
143
|
-
if (firstDegree === secondDegree) {
|
|
144
|
-
firstIndex--;
|
|
145
|
-
secondIndex--;
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
return firstDegree < secondDegree ? -1 : 1;
|
|
149
|
-
}
|
|
150
|
-
return firstVar > secondVar ? 1 : -1;
|
|
151
|
-
}
|
|
152
|
-
if (firstIndex === secondIndex) return firstMinVarDegree < secondMinVarDegree ? -1 : 1;
|
|
153
|
-
return firstIndex < 0 ? -1 : 1;
|
|
154
|
-
};
|
|
155
|
-
|
|
156
92
|
const gradedLexCompare = (first: Monomial, second: Monomial) =>
|
|
157
93
|
first.totalDegree === second.totalDegree
|
|
158
94
|
? lexCompare(first, second)
|
|
@@ -169,7 +105,6 @@ const isPoGreater = (first: Monomial, second: Monomial) => {
|
|
|
169
105
|
|
|
170
106
|
export class Polynomial {
|
|
171
107
|
isLexSorted = false;
|
|
172
|
-
refCount = 0;
|
|
173
108
|
|
|
174
109
|
constructor(
|
|
175
110
|
readonly id: number,
|
|
@@ -225,13 +160,6 @@ export class Polynomial {
|
|
|
225
160
|
return maxPos;
|
|
226
161
|
}
|
|
227
162
|
|
|
228
|
-
getGradedLexMinPos() {
|
|
229
|
-
let minPos = 0;
|
|
230
|
-
for (let index = 1; index < this.ms.length; index++)
|
|
231
|
-
if (gradedLexCompare(this.ms[index], this.ms[minPos]) < 0) minPos = index;
|
|
232
|
-
return minPos;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
163
|
toString() {
|
|
236
164
|
if (this.ms.length === 0) return "0";
|
|
237
165
|
return this.ms
|
|
@@ -247,112 +175,6 @@ export class Polynomial {
|
|
|
247
175
|
}
|
|
248
176
|
}
|
|
249
177
|
|
|
250
|
-
export class PolynomialRef {
|
|
251
|
-
private value: Polynomial | null = null;
|
|
252
|
-
|
|
253
|
-
constructor(private readonly pm: PolynomialManager) {}
|
|
254
|
-
|
|
255
|
-
get() {
|
|
256
|
-
const value = this.value;
|
|
257
|
-
if (value === null) throw new SmtInvariantError("polynomial reference is null");
|
|
258
|
-
return value;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
getOrNull() {
|
|
262
|
-
return this.value;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
set(polynomial: Polynomial) {
|
|
266
|
-
this.pm.incRef(polynomial);
|
|
267
|
-
if (this.value !== null) this.pm.decRef(this.value);
|
|
268
|
-
this.value = polynomial;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
copy(other: PolynomialRef) {
|
|
272
|
-
if (this.value === other.value) return;
|
|
273
|
-
if (this.value !== null) this.pm.decRef(this.value);
|
|
274
|
-
this.value = other.value;
|
|
275
|
-
if (this.value !== null) this.pm.incRef(this.value);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
move(other: PolynomialRef) {
|
|
279
|
-
if (this === other) return;
|
|
280
|
-
if (this.value !== null) this.pm.decRef(this.value);
|
|
281
|
-
this.value = other.value;
|
|
282
|
-
other.value = null;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
swap(other: PolynomialRef) {
|
|
286
|
-
const value = this.value;
|
|
287
|
-
this.value = other.value;
|
|
288
|
-
other.value = value;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
reset() {
|
|
292
|
-
if (this.value !== null) this.pm.decRef(this.value);
|
|
293
|
-
this.value = null;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
export class PolynomialRefVector {
|
|
298
|
-
readonly items: Polynomial[] = [];
|
|
299
|
-
|
|
300
|
-
constructor(private readonly pm: PolynomialManager) {}
|
|
301
|
-
|
|
302
|
-
get size() {
|
|
303
|
-
return this.items.length;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
get(index: number) {
|
|
307
|
-
return this.items[index];
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
push(polynomial: Polynomial) {
|
|
311
|
-
this.pm.incRef(polynomial);
|
|
312
|
-
this.items.push(polynomial);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
set(index: number, polynomial: Polynomial) {
|
|
316
|
-
this.pm.incRef(polynomial);
|
|
317
|
-
this.pm.decRef(this.items[index]);
|
|
318
|
-
this.items[index] = polynomial;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
append(other: PolynomialRefVector) {
|
|
322
|
-
for (const polynomial of other.items) this.push(polynomial);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
shrink(size: number) {
|
|
326
|
-
for (let index = size; index < this.items.length; index++) this.pm.decRef(this.items[index]);
|
|
327
|
-
this.items.length = size;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
reset() {
|
|
331
|
-
this.shrink(0);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
export class Factors {
|
|
336
|
-
constant = 1n;
|
|
337
|
-
readonly factors: Polynomial[] = [];
|
|
338
|
-
readonly degrees: number[] = [];
|
|
339
|
-
|
|
340
|
-
constructor(private readonly pm: PolynomialManager) {}
|
|
341
|
-
|
|
342
|
-
push(polynomial: Polynomial, degree: number) {
|
|
343
|
-
this.factors.push(polynomial);
|
|
344
|
-
this.degrees.push(degree);
|
|
345
|
-
this.pm.incRef(polynomial);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
reset() {
|
|
349
|
-
for (const factor of this.factors) this.pm.decRef(factor);
|
|
350
|
-
this.factors.length = 0;
|
|
351
|
-
this.degrees.length = 0;
|
|
352
|
-
this.constant = 1n;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
178
|
class SomBuffer {
|
|
357
179
|
as: bigint[] = [];
|
|
358
180
|
ms: Monomial[] = [];
|
|
@@ -368,69 +190,52 @@ class SomBuffer {
|
|
|
368
190
|
|
|
369
191
|
addTerm(coeff: bigint, monomial: Monomial) {
|
|
370
192
|
if (coeff === 0n) return;
|
|
371
|
-
const modulus = this.manager.modulus;
|
|
372
193
|
const position = this.positions.get(monomial.id);
|
|
373
194
|
if (position === undefined) {
|
|
374
195
|
this.positions.set(monomial.id, this.ms.length);
|
|
375
196
|
this.ms.push(monomial);
|
|
376
|
-
this.as.push(
|
|
197
|
+
this.as.push(coeff);
|
|
377
198
|
return;
|
|
378
199
|
}
|
|
379
|
-
|
|
380
|
-
this.as[position] = modulus === null ? sum : normalizeZp(sum, modulus);
|
|
200
|
+
this.as[position] += coeff;
|
|
381
201
|
}
|
|
382
202
|
|
|
383
203
|
add(polynomial: Polynomial) {
|
|
384
|
-
const modulus = this.manager.modulus;
|
|
385
204
|
for (let index = 0; index < polynomial.size; index++) {
|
|
386
205
|
const monomial = polynomial.ms[index];
|
|
387
206
|
const position = this.positions.get(monomial.id);
|
|
388
207
|
if (position === undefined) {
|
|
389
208
|
this.positions.set(monomial.id, this.ms.length);
|
|
390
209
|
this.ms.push(monomial);
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
continue;
|
|
394
|
-
}
|
|
395
|
-
const sum = this.as[position] + polynomial.as[index];
|
|
396
|
-
this.as[position] = modulus === null ? sum : normalizeZp(sum, modulus);
|
|
210
|
+
this.as.push(polynomial.as[index]);
|
|
211
|
+
} else this.as[position] += polynomial.as[index];
|
|
397
212
|
}
|
|
398
213
|
}
|
|
399
214
|
|
|
400
215
|
addmul(coeff: bigint, monomial: Monomial, polynomial: Polynomial) {
|
|
401
216
|
if (coeff === 0n) return;
|
|
402
|
-
const modulus = this.manager.modulus;
|
|
403
217
|
for (let index = 0; index < polynomial.size; index++) {
|
|
404
218
|
const product = this.manager.mulMonomials(monomial, polynomial.ms[index]);
|
|
405
219
|
const position = this.positions.get(product.id);
|
|
406
220
|
if (position === undefined) {
|
|
407
221
|
this.positions.set(product.id, this.ms.length);
|
|
408
222
|
this.ms.push(product);
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
continue;
|
|
412
|
-
}
|
|
413
|
-
const sum = this.as[position] + coeff * polynomial.as[index];
|
|
414
|
-
this.as[position] = modulus === null ? sum : normalizeZp(sum, modulus);
|
|
223
|
+
this.as.push(coeff * polynomial.as[index]);
|
|
224
|
+
} else this.as[position] += coeff * polynomial.as[index];
|
|
415
225
|
}
|
|
416
226
|
}
|
|
417
227
|
|
|
418
228
|
// Like Z3, zero entries of the other buffer are added too, which keeps their monomials' positions.
|
|
419
229
|
addmulBuffer(coeff: bigint, monomial: Monomial, buffer: SomBuffer) {
|
|
420
230
|
if (coeff === 0n) return;
|
|
421
|
-
const modulus = this.manager.modulus;
|
|
422
231
|
for (let index = 0; index < buffer.ms.length; index++) {
|
|
423
232
|
const product = this.manager.mulMonomials(monomial, buffer.ms[index]);
|
|
424
233
|
const position = this.positions.get(product.id);
|
|
425
234
|
if (position === undefined) {
|
|
426
235
|
this.positions.set(product.id, this.ms.length);
|
|
427
236
|
this.ms.push(product);
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
continue;
|
|
431
|
-
}
|
|
432
|
-
const sum = this.as[position] + coeff * buffer.as[index];
|
|
433
|
-
this.as[position] = modulus === null ? sum : normalizeZp(sum, modulus);
|
|
237
|
+
this.as.push(coeff * buffer.as[index]);
|
|
238
|
+
} else this.as[position] += coeff * buffer.as[index];
|
|
434
239
|
}
|
|
435
240
|
}
|
|
436
241
|
|
|
@@ -452,8 +257,8 @@ class SomBuffer {
|
|
|
452
257
|
ms.push(this.ms[index]);
|
|
453
258
|
}
|
|
454
259
|
this.reset();
|
|
455
|
-
|
|
456
|
-
return this.manager.mkPolynomialCore(as, ms);
|
|
260
|
+
const gcd = isNormalized ? as.reduce((current, coeff) => getGcd(current, coeff), 0n) : 1n;
|
|
261
|
+
return this.manager.mkPolynomialCore(gcd > 1n ? as.map((coeff) => coeff / gcd) : as, ms);
|
|
457
262
|
}
|
|
458
263
|
}
|
|
459
264
|
|
|
@@ -463,38 +268,20 @@ class CheapSomBuffer {
|
|
|
463
268
|
|
|
464
269
|
constructor(private manager: PolynomialManager) {}
|
|
465
270
|
|
|
466
|
-
addReset(coeff: bigint, monomial: Monomial) {
|
|
467
|
-
if (coeff === 0n) return;
|
|
468
|
-
this.as.push(coeff);
|
|
469
|
-
this.ms.push(monomial);
|
|
470
|
-
}
|
|
471
|
-
|
|
472
271
|
add(coeff: bigint, monomial: Monomial) {
|
|
473
272
|
if (coeff === 0n) return;
|
|
474
|
-
|
|
475
|
-
this.as.push(modulus === null ? coeff : normalizeZp(coeff, modulus));
|
|
273
|
+
this.as.push(coeff);
|
|
476
274
|
this.ms.push(monomial);
|
|
477
275
|
}
|
|
478
276
|
|
|
479
277
|
addmul(coeff: bigint, monomial: Monomial, polynomial: Polynomial) {
|
|
480
278
|
if (coeff === 0n) return;
|
|
481
|
-
const modulus = this.manager.modulus;
|
|
482
279
|
for (let index = 0; index < polynomial.size; index++) {
|
|
483
|
-
|
|
484
|
-
this.as.push(modulus === null ? value : normalizeZp(value, modulus));
|
|
280
|
+
this.as.push(coeff * polynomial.as[index]);
|
|
485
281
|
this.ms.push(this.manager.mulMonomials(monomial, polynomial.ms[index]));
|
|
486
282
|
}
|
|
487
283
|
}
|
|
488
284
|
|
|
489
|
-
normalize() {
|
|
490
|
-
this.manager.normalizeNumerals(this.as);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
reset() {
|
|
494
|
-
this.as = [];
|
|
495
|
-
this.ms = [];
|
|
496
|
-
}
|
|
497
|
-
|
|
498
285
|
mk() {
|
|
499
286
|
const result = this.manager.mkPolynomialCore(this.as, this.ms);
|
|
500
287
|
this.as = [];
|
|
@@ -503,238 +290,27 @@ class CheapSomBuffer {
|
|
|
503
290
|
}
|
|
504
291
|
}
|
|
505
292
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
constructor(private readonly pm: PolynomialManager) {
|
|
512
|
-
this.vs = new PolynomialRefVector(pm);
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
reset() {
|
|
516
|
-
this.inputs.length = 0;
|
|
517
|
-
this.invs.length = 1;
|
|
518
|
-
this.vs.reset();
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
add(input: bigint, output: Polynomial) {
|
|
522
|
-
const pm = this.pm;
|
|
523
|
-
const size = this.inputs.length;
|
|
524
|
-
if (size === 0) {
|
|
525
|
-
this.inputs.push(input);
|
|
526
|
-
this.vs.push(output);
|
|
527
|
-
return;
|
|
528
|
-
}
|
|
529
|
-
const k = size;
|
|
530
|
-
let product = pm.subNumeral(input, this.inputs[0]);
|
|
531
|
-
for (let index = 1; index <= k - 1; index++)
|
|
532
|
-
product = pm.mulNumeral(product, pm.subNumeral(input, this.inputs[index]));
|
|
533
|
-
product = pm.invNumeral(product);
|
|
534
|
-
this.inputs.push(input);
|
|
535
|
-
this.invs.push(product);
|
|
536
|
-
const temp = new PolynomialRef(pm);
|
|
537
|
-
const auxPoly = new PolynomialRef(pm);
|
|
538
|
-
temp.set(this.vs.get(k - 1));
|
|
539
|
-
for (let j = k - 2; j >= 0; j--) {
|
|
540
|
-
const aux = pm.subNumeral(input, this.inputs[j]);
|
|
541
|
-
auxPoly.set(pm.mulConst(aux, temp.get()));
|
|
542
|
-
temp.set(pm.add(auxPoly.get(), this.vs.get(j)));
|
|
543
|
-
}
|
|
544
|
-
auxPoly.set(pm.sub(output, temp.get()));
|
|
545
|
-
auxPoly.set(pm.mulConst(this.invs[size], auxPoly.get()));
|
|
546
|
-
this.vs.push(auxPoly.get());
|
|
547
|
-
auxPoly.reset();
|
|
548
|
-
temp.reset();
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
mk(x: number, r: PolynomialRef) {
|
|
552
|
-
const pm = this.pm;
|
|
553
|
-
const u = new PolynomialRef(pm);
|
|
554
|
-
const auxPoly = new PolynomialRef(pm);
|
|
555
|
-
const d = this.inputs.length - 1;
|
|
556
|
-
u.set(this.vs.get(d));
|
|
557
|
-
for (let k = d - 1; k >= 0; k--) {
|
|
558
|
-
const c = pm.negNumeral(this.inputs[k]);
|
|
559
|
-
auxPoly.set(pm.mkLinear([1n], [x], c));
|
|
560
|
-
auxPoly.set(pm.mul(u.get(), auxPoly.get()));
|
|
561
|
-
u.set(pm.add(auxPoly.get(), this.vs.get(k)));
|
|
562
|
-
}
|
|
563
|
-
r.copy(u);
|
|
564
|
-
auxPoly.reset();
|
|
565
|
-
u.reset();
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
interface SkeletonEntry {
|
|
570
|
-
monomial: Monomial;
|
|
571
|
-
firstPowerIndex: number;
|
|
572
|
-
numPowers: number;
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
class Skeleton {
|
|
576
|
-
readonly entries: SkeletonEntry[] = [];
|
|
577
|
-
readonly powers: number[] = [];
|
|
578
|
-
readonly origMonomials: Monomial[] = [];
|
|
579
|
-
maxPowers = 0;
|
|
580
|
-
|
|
581
|
-
constructor(pm: PolynomialManager, polynomial: Polynomial, x: number) {
|
|
582
|
-
const ms = [...polynomial.ms].sort((first, second) => lexCompare2(first, second, x));
|
|
583
|
-
let previous: Monomial | null = null;
|
|
584
|
-
for (const origMonomial of ms) {
|
|
585
|
-
const k = origMonomial.degreeOf(x);
|
|
586
|
-
const monomial = k > 0 ? pm.divX(origMonomial, x) : origMonomial;
|
|
587
|
-
if (monomial === previous) {
|
|
588
|
-
const entry = this.entries[this.entries.length - 1];
|
|
589
|
-
entry.numPowers++;
|
|
590
|
-
if (entry.numPowers > this.maxPowers) this.maxPowers = entry.numPowers;
|
|
591
|
-
} else {
|
|
592
|
-
previous = monomial;
|
|
593
|
-
this.entries.push({ monomial, firstPowerIndex: this.powers.length, numPowers: 1 });
|
|
594
|
-
if (this.maxPowers === 0) this.maxPowers = 1;
|
|
595
|
-
}
|
|
596
|
-
this.origMonomials.push(origMonomial);
|
|
597
|
-
this.powers.push(k);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
getEntryIndex(monomial: Monomial) {
|
|
602
|
-
for (let index = 0; index < this.entries.length; index++)
|
|
603
|
-
if (this.entries[index].monomial === monomial) return index;
|
|
604
|
-
return -1;
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
const solveLinearEquations = (
|
|
609
|
-
pm: PolynomialManager,
|
|
610
|
-
rows: bigint[][],
|
|
611
|
-
b: bigint[],
|
|
612
|
-
xs: bigint[],
|
|
613
|
-
) => {
|
|
614
|
-
const n = rows.length;
|
|
615
|
-
for (let k = 0; k < n; k++) {
|
|
616
|
-
let i = k;
|
|
617
|
-
for (; i < n; i++) if (rows[i][k] !== 0n) break;
|
|
618
|
-
if (i === n) return false;
|
|
619
|
-
const row = rows[k];
|
|
620
|
-
rows[k] = rows[i];
|
|
621
|
-
rows[i] = row;
|
|
622
|
-
const rowK = rows[k];
|
|
623
|
-
const pivot = rowK[k];
|
|
624
|
-
for (let column = k + 1; column < n; column++)
|
|
625
|
-
rowK[column] = pm.divNumeral(rowK[column], pivot);
|
|
626
|
-
b[k] = pm.divNumeral(b[k], pivot);
|
|
627
|
-
rowK[k] = pm.normalizeNumeral(1n);
|
|
628
|
-
for (let other = k + 1; other < n; other++) {
|
|
629
|
-
const rowI = rows[other];
|
|
630
|
-
const factor = rowI[k];
|
|
631
|
-
for (let column = k + 1; column < n; column++)
|
|
632
|
-
rowI[column] = pm.normalizeNumeral(rowI[column] - factor * rowK[column]);
|
|
633
|
-
b[other] = pm.normalizeNumeral(b[other] - factor * b[k]);
|
|
634
|
-
rowI[k] = 0n;
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
for (let k = n; k-- > 0;) {
|
|
638
|
-
xs[k] = b[k];
|
|
639
|
-
for (let other = k; other-- > 0;) {
|
|
640
|
-
b[other] = pm.normalizeNumeral(b[other] - rows[other][k] * b[k]);
|
|
641
|
-
rows[other][k] = 0n;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
return true;
|
|
645
|
-
};
|
|
646
|
-
|
|
647
|
-
class SparseInterpolator {
|
|
648
|
-
private inputs: bigint[] = [];
|
|
649
|
-
private outputs: bigint[] = [];
|
|
650
|
-
|
|
651
|
-
constructor(
|
|
652
|
-
private readonly pm: PolynomialManager,
|
|
653
|
-
skeleton: Skeleton | null,
|
|
654
|
-
) {
|
|
655
|
-
if (skeleton !== null)
|
|
656
|
-
for (const entry of skeleton.entries)
|
|
657
|
-
for (let index = 0; index < entry.numPowers; index++) this.outputs.push(0n);
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
reset() {
|
|
661
|
-
this.inputs.length = 0;
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
isReady(skeleton: Skeleton) {
|
|
665
|
-
return this.inputs.length === skeleton.maxPowers;
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
add(skeleton: Skeleton, input: bigint, q: Polynomial) {
|
|
669
|
-
const inputIndex = this.inputs.length;
|
|
670
|
-
this.inputs.push(this.pm.normalizeNumeral(input));
|
|
671
|
-
for (let index = 0; index < q.size; index++) {
|
|
672
|
-
const entryIndex = skeleton.getEntryIndex(q.ms[index]);
|
|
673
|
-
if (entryIndex === -1) return false;
|
|
674
|
-
const entry = skeleton.entries[entryIndex];
|
|
675
|
-
if (inputIndex >= entry.numPowers) continue;
|
|
676
|
-
this.outputs[entry.firstPowerIndex + inputIndex] = this.pm.normalizeNumeral(q.as[index]);
|
|
677
|
-
}
|
|
678
|
-
return true;
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
mk(skeleton: Skeleton, r: PolynomialRef) {
|
|
682
|
-
const pm = this.pm;
|
|
683
|
-
const as: bigint[] = [];
|
|
684
|
-
const ms: Monomial[] = [];
|
|
685
|
-
let rows: bigint[][] = [];
|
|
686
|
-
let b: bigint[] = [];
|
|
687
|
-
for (const entry of skeleton.entries) {
|
|
688
|
-
const numPowers = entry.numPowers;
|
|
689
|
-
if (rows.length !== numPowers) {
|
|
690
|
-
rows = Array.from({ length: numPowers }, () => Array.from({ length: numPowers }, () => 0n));
|
|
691
|
-
b = Array.from({ length: numPowers }, () => 0n);
|
|
692
|
-
}
|
|
693
|
-
const newAs = Array.from({ length: numPowers }, () => 0n);
|
|
694
|
-
for (let index = 0; index < numPowers; index++) {
|
|
695
|
-
const input = this.inputs[index];
|
|
696
|
-
for (let j = 0; j < numPowers; j++)
|
|
697
|
-
rows[index][j] = pm.powerNumeral(input, skeleton.powers[entry.firstPowerIndex + j]);
|
|
698
|
-
b[index] = this.outputs[entry.firstPowerIndex + index];
|
|
699
|
-
}
|
|
700
|
-
if (!solveLinearEquations(pm, rows, b, newAs)) return false;
|
|
701
|
-
for (let index = 0; index < numPowers; index++) {
|
|
702
|
-
if (newAs[index] === 0n) continue;
|
|
703
|
-
as.push(newAs[index]);
|
|
704
|
-
ms.push(skeleton.origMonomials[entry.firstPowerIndex + index]);
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
r.set(pm.mkPolynomial(as, ms));
|
|
708
|
-
return true;
|
|
709
|
-
}
|
|
293
|
+
interface Factors {
|
|
294
|
+
constant: bigint;
|
|
295
|
+
factors: Polynomial[];
|
|
296
|
+
degrees: number[];
|
|
710
297
|
}
|
|
711
298
|
|
|
712
299
|
export class PolynomialManager {
|
|
713
300
|
private monomials = new Map<string, Monomial>();
|
|
714
301
|
private nextPolynomialId = 0;
|
|
715
|
-
private freePolynomialIds: number[] = [];
|
|
716
|
-
private polynomials: (Polynomial | null)[] = [];
|
|
717
|
-
modulus: bigint | null = null;
|
|
718
302
|
readonly unit: Monomial;
|
|
719
303
|
readonly zero: Polynomial;
|
|
720
304
|
readonly one: Polynomial;
|
|
721
|
-
private um
|
|
305
|
+
private um = new UpolynomialManager();
|
|
722
306
|
private som = new SomBuffer(this);
|
|
723
307
|
private som2 = new SomBuffer(this);
|
|
724
308
|
private cheap = new CheapSomBuffer(this);
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
private mgcdInterpolators: NewtonInterpolator[] = [];
|
|
728
|
-
private mgcdSkeletons: (Skeleton | null)[] = [];
|
|
729
|
-
private iccpZpXBuffers: SomBuffer[] = [];
|
|
730
|
-
|
|
731
|
-
constructor(readonly limit: ResourceLimit) {
|
|
732
|
-
this.um = new UpolynomialManager(limit);
|
|
309
|
+
|
|
310
|
+
constructor() {
|
|
733
311
|
this.unit = this.mkMonomial([], []);
|
|
734
312
|
this.zero = this.mkPolynomialCore([], []);
|
|
735
|
-
this.
|
|
736
|
-
this.one = this.mkConstCore(1n);
|
|
737
|
-
this.incRef(this.one);
|
|
313
|
+
this.one = this.mkPolynomialCore([1n], [this.unit]);
|
|
738
314
|
}
|
|
739
315
|
|
|
740
316
|
mkMonomial(vars: readonly number[], degrees: readonly number[]) {
|
|
@@ -746,7 +322,7 @@ export class PolynomialManager {
|
|
|
746
322
|
return monomial;
|
|
747
323
|
}
|
|
748
324
|
|
|
749
|
-
rename(permutation: readonly number[]) {
|
|
325
|
+
rename(permutation: readonly number[], polynomials: Iterable<Polynomial>) {
|
|
750
326
|
const monomials = new Map<string, Monomial>();
|
|
751
327
|
for (const monomial of this.monomials.values()) {
|
|
752
328
|
const order = monomial.vars
|
|
@@ -760,8 +336,7 @@ export class PolynomialManager {
|
|
|
760
336
|
monomials.set(getMonomialKey(monomial.vars, monomial.degrees), monomial);
|
|
761
337
|
}
|
|
762
338
|
this.monomials = monomials;
|
|
763
|
-
for (const polynomial of
|
|
764
|
-
if (polynomial !== null) polynomial.makeFirstMaximal();
|
|
339
|
+
for (const polynomial of polynomials) polynomial.makeFirstMaximal();
|
|
765
340
|
}
|
|
766
341
|
|
|
767
342
|
mkPower(variable: number, power: number) {
|
|
@@ -826,118 +401,22 @@ export class PolynomialManager {
|
|
|
826
401
|
return this.mkMonomial(vars, degrees);
|
|
827
402
|
}
|
|
828
403
|
|
|
829
|
-
divX(monomial: Monomial, variable: number) {
|
|
404
|
+
private divX(monomial: Monomial, variable: number) {
|
|
830
405
|
return this.divXk(monomial, variable, monomial.degreeOf(variable));
|
|
831
406
|
}
|
|
832
407
|
|
|
833
|
-
private sqrtMonomial(monomial: Monomial) {
|
|
834
|
-
return this.mkMonomial(
|
|
835
|
-
monomial.vars,
|
|
836
|
-
monomial.degrees.map((degree) => degree / 2),
|
|
837
|
-
);
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
normalizeNumeral(value: bigint) {
|
|
841
|
-
const modulus = this.modulus;
|
|
842
|
-
return modulus === null ? value : normalizeZp(value, modulus);
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
subNumeral(first: bigint, second: bigint) {
|
|
846
|
-
return this.normalizeNumeral(first - second);
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
mulNumeral(first: bigint, second: bigint) {
|
|
850
|
-
return this.normalizeNumeral(first * second);
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
negNumeral(value: bigint) {
|
|
854
|
-
return this.normalizeNumeral(-value);
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
invNumeral(value: bigint) {
|
|
858
|
-
const modulus = this.modulus;
|
|
859
|
-
if (modulus === null) throw new SmtInvariantError("inverse over Z");
|
|
860
|
-
return normalizeZp(getExtendedGcd(normalizeZp(value, modulus), modulus).x, modulus);
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
divNumeral(dividend: bigint, divisor: bigint) {
|
|
864
|
-
return this.modulus === null
|
|
865
|
-
? divMpz(dividend, divisor)
|
|
866
|
-
: this.mulNumeral(dividend, this.invNumeral(divisor));
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
powerNumeral(base: bigint, exponent: number) {
|
|
870
|
-
if (exponent === 1) return this.normalizeNumeral(base);
|
|
871
|
-
if (exponent === 0) return this.normalizeNumeral(1n);
|
|
872
|
-
let mask = 1;
|
|
873
|
-
let power = this.normalizeNumeral(base);
|
|
874
|
-
let result = this.normalizeNumeral(1n);
|
|
875
|
-
while (mask <= exponent) {
|
|
876
|
-
if ((mask & exponent) !== 0) result = this.mulNumeral(result, power);
|
|
877
|
-
power = this.mulNumeral(power, power);
|
|
878
|
-
mask <<= 1;
|
|
879
|
-
}
|
|
880
|
-
return result;
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
private dividesNumeral(divisor: bigint, dividend: bigint) {
|
|
884
|
-
if (this.modulus !== null && divisor !== 0n) return true;
|
|
885
|
-
return divisor === 0n ? dividend === 0n : dividend % divisor === 0n;
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
private gcdNumerals(as: readonly bigint[]) {
|
|
889
|
-
let result = 0n;
|
|
890
|
-
for (const coeff of as) result = getGcd(result, coeff);
|
|
891
|
-
return result;
|
|
892
|
-
}
|
|
893
|
-
|
|
894
|
-
normalizeNumerals(as: bigint[]) {
|
|
895
|
-
if (as.length === 0) return;
|
|
896
|
-
const g = this.gcdNumerals(as);
|
|
897
|
-
if (g === 1n) return;
|
|
898
|
-
for (let index = 0; index < as.length; index++) as[index] = this.divNumeral(as[index], g);
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
setZp(prime: bigint) {
|
|
902
|
-
this.modulus = prime;
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
setZ() {
|
|
906
|
-
this.modulus = null;
|
|
907
|
-
}
|
|
908
|
-
|
|
909
408
|
mkPolynomialCore(as: bigint[], ms: Monomial[]) {
|
|
910
|
-
|
|
911
|
-
const polynomial = new Polynomial(id, as, ms);
|
|
912
|
-
this.polynomials[id] = polynomial;
|
|
913
|
-
return polynomial;
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
incRef(polynomial: Polynomial) {
|
|
917
|
-
polynomial.refCount++;
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
decRef(polynomial: Polynomial) {
|
|
921
|
-
if (--polynomial.refCount === 0) this.del(polynomial);
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
private del(polynomial: Polynomial) {
|
|
925
|
-
this.polynomials[polynomial.id] = null;
|
|
926
|
-
this.freePolynomialIds.push(polynomial.id);
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
private mkConstCore(value: bigint) {
|
|
930
|
-
return this.mkPolynomialCore([value], [this.unit]);
|
|
409
|
+
return new Polynomial(this.nextPolynomialId++, as, ms);
|
|
931
410
|
}
|
|
932
411
|
|
|
933
412
|
mkConst(value: bigint) {
|
|
934
413
|
if (value === 0n) return this.zero;
|
|
935
414
|
if (value === 1n) return this.one;
|
|
936
|
-
return this.
|
|
415
|
+
return this.mkPolynomialCore([value], [this.unit]);
|
|
937
416
|
}
|
|
938
417
|
|
|
939
418
|
mkVar(variable: number, degree = 1) {
|
|
940
|
-
return this.mkPolynomialCore([1n], [this.
|
|
419
|
+
return this.mkPolynomialCore([1n], [this.mkMonomial([variable], [degree])]);
|
|
941
420
|
}
|
|
942
421
|
|
|
943
422
|
mkPolynomial(as: readonly bigint[], ms: readonly Monomial[]) {
|
|
@@ -946,17 +425,6 @@ export class PolynomialManager {
|
|
|
946
425
|
return this.som.mk();
|
|
947
426
|
}
|
|
948
427
|
|
|
949
|
-
private mkUnivariate(variable: number, coeffs: readonly bigint[]) {
|
|
950
|
-
for (let power = coeffs.length - 1; power >= 0; power--)
|
|
951
|
-
this.cheap.addReset(coeffs[power], this.mkPower(variable, power));
|
|
952
|
-
return this.cheap.mk();
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
toPolynomial(coeffs: readonly bigint[], variable: number) {
|
|
956
|
-
if (coeffs.length === 0) return this.zero;
|
|
957
|
-
return this.mkUnivariate(variable, coeffs);
|
|
958
|
-
}
|
|
959
|
-
|
|
960
428
|
mkLinear(as: readonly bigint[], vars: readonly number[], constant: bigint) {
|
|
961
429
|
const coeffs: bigint[] = [];
|
|
962
430
|
const ms: Monomial[] = [];
|
|
@@ -1034,47 +502,29 @@ export class PolynomialManager {
|
|
|
1034
502
|
return key;
|
|
1035
503
|
}
|
|
1036
504
|
|
|
1037
|
-
|
|
1038
|
-
firstCoeff: bigint,
|
|
1039
|
-
firstMonomial: Monomial,
|
|
1040
|
-
first: Polynomial,
|
|
1041
|
-
secondCoeff: bigint,
|
|
1042
|
-
secondMonomial: Monomial,
|
|
1043
|
-
second: Polynomial,
|
|
1044
|
-
) {
|
|
505
|
+
addmul(firstCoeff: bigint, first: Polynomial, secondCoeff: bigint, second: Polynomial) {
|
|
1045
506
|
this.som.reset();
|
|
1046
|
-
this.som.addmul(firstCoeff,
|
|
1047
|
-
this.som.addmul(secondCoeff,
|
|
507
|
+
this.som.addmul(firstCoeff, this.unit, first);
|
|
508
|
+
this.som.addmul(secondCoeff, this.unit, second);
|
|
1048
509
|
return this.som.mk();
|
|
1049
510
|
}
|
|
1050
511
|
|
|
1051
|
-
addmul(first: Polynomial, secondCoeff: bigint, second: Polynomial) {
|
|
1052
|
-
return this.addmulTerms(1n, this.unit, first, secondCoeff, this.unit, second);
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
512
|
add(first: Polynomial, second: Polynomial) {
|
|
1056
|
-
return this.
|
|
513
|
+
return this.addmul(1n, first, 1n, second);
|
|
1057
514
|
}
|
|
1058
515
|
|
|
1059
516
|
sub(first: Polynomial, second: Polynomial) {
|
|
1060
|
-
return this.
|
|
517
|
+
return this.addmul(1n, first, -1n, second);
|
|
1061
518
|
}
|
|
1062
519
|
|
|
1063
|
-
|
|
1064
|
-
if (first.size === 0 || second.size === 0) return this.
|
|
520
|
+
mul(first: Polynomial, second: Polynomial) {
|
|
521
|
+
if (first.size === 0 || second.size === 0) return this.zero;
|
|
1065
522
|
this.som.reset();
|
|
1066
|
-
for (let index = 0; index < first.size; index++)
|
|
1067
|
-
this.limit.checkpoint();
|
|
523
|
+
for (let index = 0; index < first.size; index++)
|
|
1068
524
|
this.som.addmul(first.as[index], first.ms[index], second);
|
|
1069
|
-
}
|
|
1070
|
-
this.som.addTerm(constant, this.unit);
|
|
1071
525
|
return this.som.mk();
|
|
1072
526
|
}
|
|
1073
527
|
|
|
1074
|
-
mul(first: Polynomial, second: Polynomial) {
|
|
1075
|
-
return this.muladd(first, second, 0n);
|
|
1076
|
-
}
|
|
1077
|
-
|
|
1078
528
|
mulTerm(coeff: bigint, monomial: Monomial, polynomial: Polynomial) {
|
|
1079
529
|
if (coeff === 0n) return this.zero;
|
|
1080
530
|
if (coeff === 1n && monomial === this.unit) return polynomial;
|
|
@@ -1086,45 +536,25 @@ export class PolynomialManager {
|
|
|
1086
536
|
return this.mulTerm(coeff, this.unit, polynomial);
|
|
1087
537
|
}
|
|
1088
538
|
|
|
1089
|
-
private mulScaled(coeff: bigint, first: Polynomial, second: Polynomial) {
|
|
1090
|
-
if (coeff === 0n || first.size === 0 || second.size === 0) return this.zero;
|
|
1091
|
-
this.som.reset();
|
|
1092
|
-
for (let index = 0; index < first.size; index++) {
|
|
1093
|
-
this.limit.checkpoint();
|
|
1094
|
-
this.som.addmul(this.mulNumeral(coeff, first.as[index]), first.ms[index], second);
|
|
1095
|
-
}
|
|
1096
|
-
return this.som.mk();
|
|
1097
|
-
}
|
|
1098
|
-
|
|
1099
|
-
private divConst(polynomial: Polynomial, divisor: bigint) {
|
|
1100
|
-
for (let index = 0; index < polynomial.size; index++)
|
|
1101
|
-
this.cheap.add(this.divNumeral(polynomial.as[index], divisor), polynomial.ms[index]);
|
|
1102
|
-
return this.cheap.mk();
|
|
1103
|
-
}
|
|
1104
|
-
|
|
1105
539
|
neg(polynomial: Polynomial) {
|
|
1106
540
|
for (let index = 0; index < polynomial.size; index++)
|
|
1107
|
-
this.cheap.add(
|
|
541
|
+
this.cheap.add(-polynomial.as[index], polynomial.ms[index]);
|
|
1108
542
|
return this.cheap.mk();
|
|
1109
543
|
}
|
|
1110
544
|
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
}
|
|
1117
|
-
return this.cheap.mk();
|
|
545
|
+
pw(polynomial: Polynomial, power: number) {
|
|
546
|
+
if (power === 0) return this.one;
|
|
547
|
+
let result = polynomial;
|
|
548
|
+
for (let index = 1; index < power; index++) result = this.mul(result, polynomial);
|
|
549
|
+
return result;
|
|
1118
550
|
}
|
|
1119
551
|
|
|
1120
|
-
|
|
552
|
+
coeff(polynomial: Polynomial, variable: number, power: number) {
|
|
1121
553
|
for (let index = 0; index < polynomial.size; index++) {
|
|
1122
554
|
const monomial = polynomial.ms[index];
|
|
1123
555
|
if (monomial.degreeOf(variable) === power)
|
|
1124
556
|
this.cheap.add(polynomial.as[index], this.divX(monomial, variable));
|
|
1125
|
-
else this.cheap2.add(polynomial.as[index], monomial);
|
|
1126
557
|
}
|
|
1127
|
-
reduct.set(this.cheap2.mk());
|
|
1128
558
|
return this.cheap.mk();
|
|
1129
559
|
}
|
|
1130
560
|
|
|
@@ -1139,17 +569,6 @@ export class PolynomialManager {
|
|
|
1139
569
|
return result;
|
|
1140
570
|
}
|
|
1141
571
|
|
|
1142
|
-
isNonzeroConstCoeff(polynomial: Polynomial, variable: number, power: number) {
|
|
1143
|
-
const coeff = this.getConstCoeff(polynomial, variable, power);
|
|
1144
|
-
return coeff !== null && coeff !== 0n;
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
private getUnivCoeff(polynomial: Polynomial, power: number) {
|
|
1148
|
-
for (let index = 0; index < polynomial.size; index++)
|
|
1149
|
-
if (polynomial.ms[index].totalDegree === power) return polynomial.as[index];
|
|
1150
|
-
return 0n;
|
|
1151
|
-
}
|
|
1152
|
-
|
|
1153
572
|
lc(polynomial: Polynomial, variable: number) {
|
|
1154
573
|
return this.coeff(polynomial, variable, this.degree(polynomial, variable));
|
|
1155
574
|
}
|
|
@@ -1159,10 +578,7 @@ export class PolynomialManager {
|
|
|
1159
578
|
const monomial = polynomial.ms[index];
|
|
1160
579
|
const degree = monomial.degreeOf(variable);
|
|
1161
580
|
if (degree > 0)
|
|
1162
|
-
this.cheap.
|
|
1163
|
-
this.mulNumeral(polynomial.as[index], BigInt(degree)),
|
|
1164
|
-
this.divXk(monomial, variable, 1),
|
|
1165
|
-
);
|
|
581
|
+
this.cheap.add(polynomial.as[index] * BigInt(degree), this.divXk(monomial, variable, 1));
|
|
1166
582
|
}
|
|
1167
583
|
return this.cheap.mk();
|
|
1168
584
|
}
|
|
@@ -1183,43 +599,24 @@ export class PolynomialManager {
|
|
|
1183
599
|
return result;
|
|
1184
600
|
}
|
|
1185
601
|
|
|
1186
|
-
private icWithContent(polynomial: Polynomial,
|
|
1187
|
-
if (polynomial.size === 0)
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
if (
|
|
1192
|
-
c.set(this.one);
|
|
1193
|
-
return polynomial.as[0];
|
|
1194
|
-
}
|
|
1195
|
-
const content = this.gcdNumerals(polynomial.as);
|
|
1196
|
-
if (content === 1n) {
|
|
1197
|
-
c.set(polynomial);
|
|
1198
|
-
return content;
|
|
1199
|
-
}
|
|
1200
|
-
this.cheap.reset();
|
|
602
|
+
private icWithContent(polynomial: Polynomial): [bigint, Polynomial] {
|
|
603
|
+
if (polynomial.size === 0) return [0n, polynomial];
|
|
604
|
+
if (this.isConst(polynomial)) return [polynomial.as[0], this.one];
|
|
605
|
+
let content = 0n;
|
|
606
|
+
for (const coeff of polynomial.as) content = getGcd(content, coeff);
|
|
607
|
+
if (content === 1n) return [1n, polynomial];
|
|
1201
608
|
for (let index = 0; index < polynomial.size; index++)
|
|
1202
|
-
this.cheap.
|
|
1203
|
-
|
|
1204
|
-
return content;
|
|
609
|
+
this.cheap.add(polynomial.as[index] / content, polynomial.ms[index]);
|
|
610
|
+
return [content, this.cheap.mk()];
|
|
1205
611
|
}
|
|
1206
612
|
|
|
1207
|
-
iccp(polynomial: Polynomial, variable: number
|
|
1208
|
-
if (polynomial.size === 0)
|
|
1209
|
-
|
|
1210
|
-
pp.set(polynomial);
|
|
1211
|
-
return 0n;
|
|
1212
|
-
}
|
|
1213
|
-
if (this.isConst(polynomial)) {
|
|
1214
|
-
c.set(this.one);
|
|
1215
|
-
pp.set(this.one);
|
|
1216
|
-
return polynomial.as[0];
|
|
1217
|
-
}
|
|
613
|
+
iccp(polynomial: Polynomial, variable: number): [bigint, Polynomial, Polynomial] {
|
|
614
|
+
if (polynomial.size === 0) return [0n, this.one, polynomial];
|
|
615
|
+
if (this.isConst(polynomial)) return [polynomial.as[0], this.one, this.one];
|
|
1218
616
|
const degree = this.degree(polynomial, variable);
|
|
1219
617
|
if (degree === 0) {
|
|
1220
|
-
const integer = this.icWithContent(polynomial
|
|
1221
|
-
|
|
1222
|
-
return integer;
|
|
618
|
+
const [integer, content] = this.icWithContent(polynomial);
|
|
619
|
+
return [integer, content, this.one];
|
|
1223
620
|
}
|
|
1224
621
|
const filter = Array.from({ length: degree + 1 }, () => 0);
|
|
1225
622
|
const powers: number[] = [];
|
|
@@ -1228,598 +625,160 @@ export class PolynomialManager {
|
|
|
1228
625
|
if (filter[power] === 0) powers.push(power);
|
|
1229
626
|
filter[power] += monomial.size === (power > 0 ? 1 : 0) ? 1 : 2;
|
|
1230
627
|
}
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
c.set(this.one);
|
|
1235
|
-
return integer;
|
|
1236
|
-
}
|
|
1237
|
-
const integer = this.icWithContent(polynomial, pp);
|
|
1238
|
-
const ci = new PolynomialRef(this);
|
|
1239
|
-
c.set(this.coeff(pp.get(), variable, powers[0]));
|
|
628
|
+
const [integer, primitive] = this.icWithContent(polynomial);
|
|
629
|
+
for (const power of powers) if (filter[power] === 1) return [integer, this.one, primitive];
|
|
630
|
+
let content = this.coeff(primitive, variable, powers[0]);
|
|
1240
631
|
for (let index = 1; index < powers.length; index++) {
|
|
1241
|
-
|
|
1242
|
-
this.
|
|
1243
|
-
if (this.isConst(c.get())) {
|
|
1244
|
-
c.set(this.one);
|
|
1245
|
-
ci.reset();
|
|
1246
|
-
return integer;
|
|
1247
|
-
}
|
|
632
|
+
content = this.gcd(content, this.coeff(primitive, variable, powers[index]));
|
|
633
|
+
if (this.isConst(content)) return [integer, this.one, primitive];
|
|
1248
634
|
}
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
ci.reset();
|
|
1252
|
-
return integer;
|
|
635
|
+
content = this.flipSignIfLmNeg(content);
|
|
636
|
+
return [integer, content, this.exactDiv(primitive, content)];
|
|
1253
637
|
}
|
|
1254
638
|
|
|
1255
639
|
pp(polynomial: Polynomial, variable: number) {
|
|
1256
|
-
|
|
1257
|
-
const result = new PolynomialRef(this);
|
|
1258
|
-
this.iccp(polynomial, variable, c, result);
|
|
1259
|
-
c.reset();
|
|
1260
|
-
return result;
|
|
640
|
+
return this.iccp(polynomial, variable)[2];
|
|
1261
641
|
}
|
|
1262
642
|
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
const
|
|
1266
|
-
this.iccp(polynomial, variable
|
|
1267
|
-
const
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
643
|
+
squareFree(polynomial: Polynomial): Polynomial {
|
|
644
|
+
if (this.isConst(polynomial)) return polynomial;
|
|
645
|
+
const variable = this.maxVar(polynomial);
|
|
646
|
+
const [integer, content, primitive] = this.iccp(polynomial, variable);
|
|
647
|
+
const squareFreeContent = this.squareFree(content);
|
|
648
|
+
const g = this.gcd(primitive, this.derivative(primitive, variable));
|
|
649
|
+
if (this.isConst(g) && this.eq(squareFreeContent, content)) return polynomial;
|
|
650
|
+
const squareFreePrimitive = this.isConst(g) ? primitive : this.exactDiv(primitive, g);
|
|
651
|
+
return this.mul(this.mulConst(integer, squareFreeContent), squareFreePrimitive);
|
|
1271
652
|
}
|
|
1272
653
|
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
const
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
const dR = new PolynomialRef(this);
|
|
1282
|
-
const g = new PolynomialRef(this);
|
|
1283
|
-
const h = new PolynomialRef(this);
|
|
1284
|
-
const rem = new PolynomialRef(this);
|
|
1285
|
-
const newH = new PolynomialRef(this);
|
|
1286
|
-
const release = () => {
|
|
1287
|
-
newH.reset();
|
|
1288
|
-
rem.reset();
|
|
1289
|
-
h.reset();
|
|
1290
|
-
g.reset();
|
|
1291
|
-
dR.reset();
|
|
1292
|
-
ppV.reset();
|
|
1293
|
-
ppU.reset();
|
|
1294
|
-
cV.reset();
|
|
1295
|
-
cU.reset();
|
|
1296
|
-
};
|
|
1297
|
-
const iU = this.iccp(u, variable, cU, ppU);
|
|
1298
|
-
const iV = this.iccp(v, variable, cV, ppV);
|
|
1299
|
-
this.gcd(cU.get(), cV.get(), dR);
|
|
1300
|
-
const dA = getGcd(iU, iV);
|
|
1301
|
-
g.set(this.one);
|
|
1302
|
-
h.set(this.one);
|
|
1303
|
-
while (true) {
|
|
1304
|
-
this.limit.checkpoint();
|
|
1305
|
-
const delta = this.degree(ppU.get(), variable) - this.degree(ppV.get(), variable);
|
|
1306
|
-
this.exactPseudoRemainder(ppU.get(), ppV.get(), variable, rem);
|
|
1307
|
-
if (this.isZero(rem.get())) {
|
|
1308
|
-
ppV.set(this.flipSignIfLmNeg(ppV.get()));
|
|
1309
|
-
r.move(this.pp(ppV.get(), variable));
|
|
1310
|
-
r.set(this.mulScaled(dA, dR.get(), r.get()));
|
|
1311
|
-
release();
|
|
1312
|
-
return;
|
|
1313
|
-
}
|
|
1314
|
-
if (this.isConst(rem.get())) {
|
|
1315
|
-
r.set(this.mulConst(dA, dR.get()));
|
|
1316
|
-
release();
|
|
1317
|
-
return;
|
|
1318
|
-
}
|
|
1319
|
-
ppU.copy(ppV);
|
|
1320
|
-
ppV.set(this.exactDiv(rem.get(), g.get()));
|
|
1321
|
-
for (let index = 0; index < delta; index++) ppV.set(this.exactDiv(ppV.get(), h.get()));
|
|
1322
|
-
g.set(this.lc(ppU.get(), variable));
|
|
1323
|
-
newH.set(this.one);
|
|
1324
|
-
for (let index = 0; index < delta; index++) newH.set(this.mul(newH.get(), g.get()));
|
|
1325
|
-
if (delta > 1)
|
|
1326
|
-
for (let index = 0; index < delta - 1; index++)
|
|
1327
|
-
newH.set(this.exactDiv(newH.get(), h.get()));
|
|
1328
|
-
h.copy(newH);
|
|
1329
|
-
}
|
|
654
|
+
normalize(polynomial: Polynomial) {
|
|
655
|
+
if (polynomial.size === 0) return polynomial;
|
|
656
|
+
let g = 0n;
|
|
657
|
+
for (const coeff of polynomial.as) g = getGcd(g, coeff);
|
|
658
|
+
if (g === 1n) return polynomial;
|
|
659
|
+
for (let index = 0; index < polynomial.size; index++)
|
|
660
|
+
this.cheap.add(polynomial.as[index] / g, polynomial.ms[index]);
|
|
661
|
+
return this.cheap.mk();
|
|
1330
662
|
}
|
|
1331
663
|
|
|
1332
|
-
|
|
1333
|
-
const
|
|
1334
|
-
|
|
1335
|
-
const iU = this.iccp(u, variable, cU, ppU);
|
|
1336
|
-
cU.set(this.mulConst(iU, cU.get()));
|
|
1337
|
-
this.gcd(cU.get(), v, r);
|
|
1338
|
-
ppU.reset();
|
|
1339
|
-
cU.reset();
|
|
664
|
+
isNonzeroConstCoeff(polynomial: Polynomial, variable: number, power: number) {
|
|
665
|
+
const coeff = this.getConstCoeff(polynomial, variable, power);
|
|
666
|
+
return coeff !== null && coeff !== 0n;
|
|
1340
667
|
}
|
|
1341
668
|
|
|
1342
|
-
|
|
1343
|
-
if (
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
const
|
|
1361
|
-
|
|
1362
|
-
|
|
669
|
+
exactDiv(polynomial: Polynomial, divisor: Polynomial) {
|
|
670
|
+
if (polynomial.size === 0) return polynomial;
|
|
671
|
+
const remainder = this.som;
|
|
672
|
+
const quotient = this.som2;
|
|
673
|
+
remainder.reset();
|
|
674
|
+
quotient.reset();
|
|
675
|
+
remainder.add(polynomial);
|
|
676
|
+
const maxPos = divisor.getGradedLexMaxPos();
|
|
677
|
+
const divisorMonomial = divisor.ms[maxPos];
|
|
678
|
+
const divisorCoeff = divisor.as[maxPos];
|
|
679
|
+
while (true) {
|
|
680
|
+
const remainderPos = remainder.getGradedLexMaxPos();
|
|
681
|
+
if (remainderPos === -1) {
|
|
682
|
+
remainder.reset();
|
|
683
|
+
return quotient.mk();
|
|
684
|
+
}
|
|
685
|
+
const monomial = this.divMonomials(remainder.ms[remainderPos], divisorMonomial);
|
|
686
|
+
if (monomial === null) throw new SmtInvariantError("exact_div: the divisor does not divide");
|
|
687
|
+
const coeff = remainder.as[remainderPos] / divisorCoeff;
|
|
688
|
+
quotient.addTerm(coeff, monomial);
|
|
689
|
+
remainder.addmul(-coeff, monomial, divisor);
|
|
1363
690
|
}
|
|
1364
|
-
this.gcdPrs(u, v, this.maxVar(u), r);
|
|
1365
691
|
}
|
|
1366
692
|
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
r: PolynomialRef,
|
|
693
|
+
exactPseudoDivision(
|
|
694
|
+
polynomial: Polynomial,
|
|
695
|
+
divisor: Polynomial,
|
|
696
|
+
variable: number,
|
|
697
|
+
isQuotient = true,
|
|
1373
698
|
) {
|
|
1374
|
-
|
|
1375
|
-
c2.lexSort();
|
|
1376
|
-
const inverses = getExtendedGcd(b1, b2);
|
|
1377
|
-
const inv1 = modMpz(inverses.x, b2);
|
|
1378
|
-
const inv2 = modMpz(inverses.y, b1);
|
|
1379
|
-
const a1 = this.mulNumeral(b2, inv2);
|
|
1380
|
-
const a2 = this.mulNumeral(b1, inv1);
|
|
1381
|
-
const newBound = this.mulNumeral(b1, b2);
|
|
1382
|
-
const upper = divMpz(newBound, 2n);
|
|
1383
|
-
const add = (first: bigint, second: bigint, monomial: Monomial) => {
|
|
1384
|
-
const tmp3 = this.normalizeNumeral(this.mulNumeral(first, a1) + this.mulNumeral(second, a2));
|
|
1385
|
-
const value = modMpz(tmp3, newBound);
|
|
1386
|
-
this.cheap.add(value > upper ? this.subNumeral(value, newBound) : value, monomial);
|
|
1387
|
-
};
|
|
1388
|
-
let i1 = 0;
|
|
1389
|
-
let i2 = 0;
|
|
1390
|
-
while (true) {
|
|
1391
|
-
if (i1 === c1.size) {
|
|
1392
|
-
for (; i2 < c2.size; i2++) add(0n, c2.as[i2], c2.ms[i2]);
|
|
1393
|
-
break;
|
|
1394
|
-
}
|
|
1395
|
-
if (i2 === c2.size) {
|
|
1396
|
-
for (; i1 < c1.size; i1++) add(c1.as[i1], 0n, c1.ms[i1]);
|
|
1397
|
-
break;
|
|
1398
|
-
}
|
|
1399
|
-
const m1 = c1.ms[i1];
|
|
1400
|
-
const m2 = c2.ms[i2];
|
|
1401
|
-
const s = lexCompare(m1, m2);
|
|
1402
|
-
if (s === 0) {
|
|
1403
|
-
add(c1.as[i1], c2.as[i2], m1);
|
|
1404
|
-
i1++;
|
|
1405
|
-
i2++;
|
|
1406
|
-
} else if (s > 0) {
|
|
1407
|
-
add(c1.as[i1], 0n, m1);
|
|
1408
|
-
i1++;
|
|
1409
|
-
} else {
|
|
1410
|
-
add(0n, c2.as[i2], m2);
|
|
1411
|
-
i2++;
|
|
1412
|
-
}
|
|
1413
|
-
}
|
|
1414
|
-
r.set(this.cheap.mk());
|
|
1415
|
-
return newBound;
|
|
1416
|
-
}
|
|
1417
|
-
|
|
1418
|
-
private uniModGcd(u: Polynomial, v: Polynomial, r: PolynomialRef) {
|
|
1419
|
-
const x = this.maxVar(u);
|
|
1420
|
-
const ppU = new PolynomialRef(this);
|
|
1421
|
-
const ppV = new PolynomialRef(this);
|
|
1422
|
-
const cU = this.icWithContent(u, ppU);
|
|
1423
|
-
const cV = this.icWithContent(v, ppV);
|
|
1424
|
-
const dA = getGcd(cU, cV);
|
|
1425
|
-
const dU = this.degree(ppU.get(), x);
|
|
1426
|
-
const dV = this.degree(ppV.get(), x);
|
|
1427
|
-
const lcU = this.getUnivCoeff(ppU.get(), dU);
|
|
1428
|
-
const lcV = this.getUnivCoeff(ppV.get(), dV);
|
|
1429
|
-
const lcG = getGcd(lcU, lcV);
|
|
1430
|
-
const uZp = new PolynomialRef(this);
|
|
1431
|
-
const vZp = new PolynomialRef(this);
|
|
1432
|
-
const cStar = new PolynomialRef(this);
|
|
1433
|
-
let bound = 0n;
|
|
1434
|
-
const q = new PolynomialRef(this);
|
|
1435
|
-
const candidate = new PolynomialRef(this);
|
|
1436
|
-
const release = () => {
|
|
1437
|
-
candidate.reset();
|
|
1438
|
-
q.reset();
|
|
1439
|
-
cStar.reset();
|
|
1440
|
-
vZp.reset();
|
|
1441
|
-
uZp.reset();
|
|
1442
|
-
ppV.reset();
|
|
1443
|
-
ppU.reset();
|
|
1444
|
-
};
|
|
1445
|
-
for (const prime of BIG_PRIMES) {
|
|
1446
|
-
const savedModulus = this.modulus;
|
|
1447
|
-
this.setZp(prime);
|
|
1448
|
-
uZp.set(this.normalize(ppU.get()));
|
|
1449
|
-
vZp.set(this.normalize(ppV.get()));
|
|
1450
|
-
const isBadPrime = this.degree(uZp.get(), x) < dU || this.degree(vZp.get(), x) < dV;
|
|
1451
|
-
if (!isBadPrime) {
|
|
1452
|
-
this.euclidGcd(uZp.get(), vZp.get(), q);
|
|
1453
|
-
q.set(this.mkGlexMonic(q.get()));
|
|
1454
|
-
q.set(this.mulConst(this.normalizeNumeral(lcG), q.get()));
|
|
1455
|
-
}
|
|
1456
|
-
this.modulus = savedModulus;
|
|
1457
|
-
if (isBadPrime) continue;
|
|
1458
|
-
if (this.isConst(q.get())) {
|
|
1459
|
-
// Deviation from Z3: when d_a is 1 and q is a constant other than 1, uni_mod_gcd returns q
|
|
1460
|
-
// (polynomial.cpp:4170), so gcd(2x - 3, 2x - 1) is 2 there. This returns 1 and frees q.
|
|
1461
|
-
r.set(dA === 1n && q.get().as[0] === 1n ? q.get() : this.mkConst(dA));
|
|
1462
|
-
release();
|
|
1463
|
-
return;
|
|
1464
|
-
}
|
|
1465
|
-
if (cStar.getOrNull() === null) {
|
|
1466
|
-
cStar.copy(q);
|
|
1467
|
-
bound = prime;
|
|
1468
|
-
} else if (this.degree(q.get(), x) < this.degree(cStar.get(), x)) {
|
|
1469
|
-
cStar.copy(q);
|
|
1470
|
-
bound = prime;
|
|
1471
|
-
} else bound = this.craCombineImages(q.get(), prime, cStar.get(), bound, cStar);
|
|
1472
|
-
candidate.move(this.pp(cStar.get(), x));
|
|
1473
|
-
const lcCandidate = this.getUnivCoeff(candidate.get(), this.degree(candidate.get(), x));
|
|
1474
|
-
if (
|
|
1475
|
-
this.dividesNumeral(lcCandidate, lcG) &&
|
|
1476
|
-
this.divides(candidate.get(), ppU.get()) &&
|
|
1477
|
-
this.divides(candidate.get(), ppV.get())
|
|
1478
|
-
) {
|
|
1479
|
-
r.set(this.mulConst(dA, candidate.get()));
|
|
1480
|
-
r.set(this.flipSignIfLmNeg(r.get()));
|
|
1481
|
-
release();
|
|
1482
|
-
return;
|
|
1483
|
-
}
|
|
1484
|
-
}
|
|
1485
|
-
this.gcdPrs(u, v, x, r);
|
|
1486
|
-
release();
|
|
699
|
+
return this.pseudoDivisionCore(polynomial, divisor, variable, true, isQuotient);
|
|
1487
700
|
}
|
|
1488
701
|
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
const
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
702
|
+
pseudoRemainder(polynomial: Polynomial, divisor: Polynomial, variable: number) {
|
|
703
|
+
return this.pseudoDivisionCore(polynomial, divisor, variable, false, false);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
private pseudoDivisionCore(
|
|
707
|
+
polynomial: Polynomial,
|
|
708
|
+
divisor: Polynomial,
|
|
709
|
+
variable: number,
|
|
710
|
+
isExact: boolean,
|
|
711
|
+
isQuotient: boolean,
|
|
712
|
+
) {
|
|
713
|
+
const degA = this.degree(polynomial, variable);
|
|
714
|
+
const degB = this.degree(divisor, variable);
|
|
715
|
+
if (degB === 0) {
|
|
716
|
+
const steps = isExact ? degA + 1 : 1;
|
|
717
|
+
if (!isQuotient || !isExact || degA === 0)
|
|
718
|
+
return { quotient: polynomial, remainder: this.zero, steps };
|
|
719
|
+
return {
|
|
720
|
+
quotient: this.mul(polynomial, this.pw(divisor, degA)),
|
|
721
|
+
remainder: this.zero,
|
|
722
|
+
steps,
|
|
723
|
+
};
|
|
1505
724
|
}
|
|
1506
|
-
const
|
|
1507
|
-
const
|
|
1508
|
-
let
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
725
|
+
const lcB = this.coeff(divisor, variable, degB);
|
|
726
|
+
const reductB = this.reduct(divisor, variable, degB);
|
|
727
|
+
let remainder = polynomial;
|
|
728
|
+
let quotient = this.zero;
|
|
729
|
+
let steps = 0;
|
|
730
|
+
while (true) {
|
|
731
|
+
const degR = this.degree(remainder, variable);
|
|
732
|
+
if (degB > degR) {
|
|
733
|
+
const exactSteps = degA - degB + 1;
|
|
734
|
+
if (isExact && steps < exactSteps) {
|
|
735
|
+
const power = this.pw(lcB, exactSteps - steps);
|
|
736
|
+
if (isQuotient) quotient = this.mul(power, quotient);
|
|
737
|
+
remainder = this.mul(power, remainder);
|
|
738
|
+
}
|
|
739
|
+
return { quotient, remainder, steps };
|
|
1514
740
|
}
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
xmin.reset();
|
|
1526
|
-
return ci;
|
|
1527
|
-
}
|
|
1528
|
-
const numMarked = noXMonomials.length;
|
|
1529
|
-
let numUnmarked = 0;
|
|
1530
|
-
for (const monomial of polynomial.ms) {
|
|
1531
|
-
if (monomial.degreeOf(x) === 0) continue;
|
|
1532
|
-
const reduced = this.divX(monomial, x);
|
|
1533
|
-
if (marks.get(reduced.id) === 1) {
|
|
1534
|
-
numUnmarked++;
|
|
1535
|
-
marks.delete(reduced.id);
|
|
741
|
+
this.som.reset();
|
|
742
|
+
this.som2.reset();
|
|
743
|
+
for (let index = 0; index < remainder.size; index++) {
|
|
744
|
+
const monomial = remainder.ms[index];
|
|
745
|
+
const coeff = remainder.as[index];
|
|
746
|
+
if (monomial.degreeOf(variable) === degR) {
|
|
747
|
+
const shifted = this.divXk(monomial, variable, degB);
|
|
748
|
+
if (isQuotient) this.som2.addTerm(coeff, shifted);
|
|
749
|
+
this.som.addmul(-coeff, shifted, reductB);
|
|
750
|
+
} else this.som.addmul(coeff, monomial, lcB);
|
|
1536
751
|
}
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
}
|
|
1543
|
-
const ci = this.icWithContent(polynomial, pp);
|
|
1544
|
-
marks.clear();
|
|
1545
|
-
noXMonomials.length = 0;
|
|
1546
|
-
const buffers = this.iccpZpXBuffers;
|
|
1547
|
-
const ppValue = pp.get();
|
|
1548
|
-
for (let index = 0; index < polynomial.size; index++) {
|
|
1549
|
-
let monomial = ppValue.ms[index];
|
|
1550
|
-
const k = monomial.degreeOf(x);
|
|
1551
|
-
if (k !== 0) monomial = this.divX(monomial, x);
|
|
1552
|
-
let position = marks.get(monomial.id);
|
|
1553
|
-
if (position === undefined) {
|
|
1554
|
-
position = noXMonomials.length;
|
|
1555
|
-
noXMonomials.push(monomial);
|
|
1556
|
-
marks.set(monomial.id, position);
|
|
752
|
+
remainder = this.som.mk();
|
|
753
|
+
if (isQuotient) {
|
|
754
|
+
for (let index = 0; index < quotient.size; index++)
|
|
755
|
+
this.som2.addmul(quotient.as[index], quotient.ms[index], lcB);
|
|
756
|
+
quotient = this.som2.mk();
|
|
1557
757
|
}
|
|
1558
|
-
|
|
1559
|
-
buffers[position].addTerm(ppValue.as[index], this.mkPower(x, k));
|
|
1560
|
-
}
|
|
1561
|
-
const numMonomials = noXMonomials.length;
|
|
1562
|
-
const g = new PolynomialRef(this);
|
|
1563
|
-
const newG = new PolynomialRef(this);
|
|
1564
|
-
g.set(buffers[0].mk());
|
|
1565
|
-
for (let index = 1; index < numMonomials; index++) {
|
|
1566
|
-
const a = new PolynomialRef(this);
|
|
1567
|
-
a.set(buffers[index].mk());
|
|
1568
|
-
this.euclidGcd(g.get(), a.get(), newG);
|
|
1569
|
-
g.copy(newG);
|
|
1570
|
-
const isDone = this.isConst(g.get());
|
|
1571
|
-
a.reset();
|
|
1572
|
-
if (isDone) break;
|
|
1573
|
-
}
|
|
1574
|
-
if (!this.isConst(g.get())) {
|
|
1575
|
-
c.copy(g);
|
|
1576
|
-
pp.set(this.exactDiv(pp.get(), c.get()));
|
|
1577
|
-
} else c.set(this.one);
|
|
1578
|
-
for (let index = 0; index < numMonomials; index++) buffers[index].reset();
|
|
1579
|
-
newG.reset();
|
|
1580
|
-
g.reset();
|
|
1581
|
-
return ci;
|
|
1582
|
-
}
|
|
1583
|
-
|
|
1584
|
-
private lcGlexZpX(polynomial: Polynomial, x: number) {
|
|
1585
|
-
this.som.reset();
|
|
1586
|
-
let maxMonomial: Monomial | null = null;
|
|
1587
|
-
for (let index = 0; index < polynomial.size; index++) {
|
|
1588
|
-
let monomial = polynomial.ms[index];
|
|
1589
|
-
const k = monomial.degreeOf(x);
|
|
1590
|
-
if (k !== 0) monomial = this.divX(monomial, x);
|
|
1591
|
-
if (maxMonomial === null || gradedLexCompare(monomial, maxMonomial) > 0) {
|
|
1592
|
-
this.som.reset();
|
|
1593
|
-
maxMonomial = monomial;
|
|
1594
|
-
this.som.addTerm(polynomial.as[index], this.mkPower(x, k));
|
|
1595
|
-
} else if (maxMonomial === monomial)
|
|
1596
|
-
this.som.addTerm(polynomial.as[index], this.mkPower(x, k));
|
|
758
|
+
steps++;
|
|
1597
759
|
}
|
|
1598
|
-
return this.som.mk();
|
|
1599
|
-
}
|
|
1600
|
-
|
|
1601
|
-
private primitiveZpX(polynomial: Polynomial, x: number, pp: PolynomialRef) {
|
|
1602
|
-
const c = new PolynomialRef(this);
|
|
1603
|
-
this.iccpZpX(polynomial, x, c, pp);
|
|
1604
|
-
c.reset();
|
|
1605
760
|
}
|
|
1606
761
|
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
const hi = this.random.next();
|
|
1610
|
-
const lo = this.random.next();
|
|
1611
|
-
const value = this.normalizeNumeral(BigInt(((hi << 15) | lo) % prime));
|
|
1612
|
-
if (!values.includes(value)) return value;
|
|
1613
|
-
}
|
|
762
|
+
exactPseudoRemainder(polynomial: Polynomial, divisor: Polynomial, variable: number) {
|
|
763
|
+
return this.exactPseudoDivision(polynomial, divisor, variable, false).remainder;
|
|
1614
764
|
}
|
|
1615
765
|
|
|
1616
|
-
|
|
1617
|
-
if (polynomial.size === 0) return 0n;
|
|
1618
|
-
if (this.isConst(polynomial)) return this.normalizeNumeral(polynomial.as[0]);
|
|
1619
|
-
polynomial.lexSort();
|
|
1620
|
-
let result = 0n;
|
|
766
|
+
reduct(polynomial: Polynomial, variable: number, power: number) {
|
|
1621
767
|
for (let index = 0; index < polynomial.size; index++)
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
polynomial.as[index],
|
|
1626
|
-
this.powerNumeral(value, polynomial.ms[index].totalDegree),
|
|
1627
|
-
),
|
|
1628
|
-
);
|
|
1629
|
-
return result;
|
|
1630
|
-
}
|
|
1631
|
-
|
|
1632
|
-
private modGcdRec(
|
|
1633
|
-
u: Polynomial,
|
|
1634
|
-
v: Polynomial,
|
|
1635
|
-
prime: number,
|
|
1636
|
-
idx: number,
|
|
1637
|
-
vars: readonly number[],
|
|
1638
|
-
r: PolynomialRef,
|
|
1639
|
-
): boolean {
|
|
1640
|
-
if (idx === vars.length - 1) {
|
|
1641
|
-
this.euclidGcd(u, v, r);
|
|
1642
|
-
return true;
|
|
1643
|
-
}
|
|
1644
|
-
const x = vars[idx];
|
|
1645
|
-
const cU = new PolynomialRef(this);
|
|
1646
|
-
const ppU = new PolynomialRef(this);
|
|
1647
|
-
const lcU = new PolynomialRef(this);
|
|
1648
|
-
const cV = new PolynomialRef(this);
|
|
1649
|
-
const ppV = new PolynomialRef(this);
|
|
1650
|
-
const lcV = new PolynomialRef(this);
|
|
1651
|
-
const cG = new PolynomialRef(this);
|
|
1652
|
-
const lcG = new PolynomialRef(this);
|
|
1653
|
-
const u1 = new PolynomialRef(this);
|
|
1654
|
-
const v1 = new PolynomialRef(this);
|
|
1655
|
-
const q = new PolynomialRef(this);
|
|
1656
|
-
const h = new PolynomialRef(this);
|
|
1657
|
-
const c = new PolynomialRef(this);
|
|
1658
|
-
const lcH = new PolynomialRef(this);
|
|
1659
|
-
try {
|
|
1660
|
-
const ciU = this.iccpZpX(u, x, cU, ppU);
|
|
1661
|
-
const ciV = this.iccpZpX(v, x, cV, ppV);
|
|
1662
|
-
lcU.set(this.lcGlexZpX(ppU.get(), x));
|
|
1663
|
-
lcV.set(this.lcGlexZpX(ppV.get(), x));
|
|
1664
|
-
const ciG = getGcd(ciU, ciV);
|
|
1665
|
-
this.euclidGcd(cU.get(), cV.get(), cG);
|
|
1666
|
-
this.euclidGcd(lcU.get(), lcV.get(), lcG);
|
|
1667
|
-
const skeleton = this.mgcdSkeletons[idx];
|
|
1668
|
-
while (this.mgcdInterpolators.length <= idx)
|
|
1669
|
-
this.mgcdInterpolators.push(new NewtonInterpolator(this));
|
|
1670
|
-
const interpolator = this.mgcdInterpolators[idx];
|
|
1671
|
-
const sparseInterpolator = new SparseInterpolator(this, skeleton);
|
|
1672
|
-
let minDegQ = Infinity;
|
|
1673
|
-
while (true) {
|
|
1674
|
-
let value = 0n;
|
|
1675
|
-
let lcGValue = 0n;
|
|
1676
|
-
while (true) {
|
|
1677
|
-
value = this.peekFresh(interpolator.inputs, prime);
|
|
1678
|
-
lcGValue = this.univEval(lcG.get(), value);
|
|
1679
|
-
if (lcGValue !== 0n) break;
|
|
1680
|
-
}
|
|
1681
|
-
u1.set(this.substitute(ppU.get(), [x], [value]));
|
|
1682
|
-
v1.set(this.substitute(ppV.get(), [x], [value]));
|
|
1683
|
-
if (!this.modGcdRec(u1.get(), v1.get(), prime, idx + 1, vars, q)) return false;
|
|
1684
|
-
q.set(this.mkGlexMonic(q.get()));
|
|
1685
|
-
q.set(this.mulConst(lcGValue, q.get()));
|
|
1686
|
-
const qVar = this.maxVar(q.get());
|
|
1687
|
-
const degQ = qVar === NULL_VAR ? 0 : this.degree(q.get(), qVar);
|
|
1688
|
-
if (degQ < minDegQ) {
|
|
1689
|
-
minDegQ = degQ;
|
|
1690
|
-
if (skeleton === null) {
|
|
1691
|
-
interpolator.reset();
|
|
1692
|
-
interpolator.add(value, q.get());
|
|
1693
|
-
} else {
|
|
1694
|
-
sparseInterpolator.reset();
|
|
1695
|
-
if (!sparseInterpolator.add(skeleton, value, q.get())) return false;
|
|
1696
|
-
}
|
|
1697
|
-
} else if (degQ === minDegQ) {
|
|
1698
|
-
if (skeleton === null) interpolator.add(value, q.get());
|
|
1699
|
-
else if (!sparseInterpolator.add(skeleton, value, q.get())) return false;
|
|
1700
|
-
} else continue;
|
|
1701
|
-
let isCandidate = false;
|
|
1702
|
-
if (skeleton === null) {
|
|
1703
|
-
interpolator.mk(x, h);
|
|
1704
|
-
lcH.set(this.lcGlexZpX(h.get(), x));
|
|
1705
|
-
if (this.eq(lcH.get(), lcG.get())) isCandidate = true;
|
|
1706
|
-
} else if (sparseInterpolator.isReady(skeleton)) {
|
|
1707
|
-
if (!sparseInterpolator.mk(skeleton, h)) return false;
|
|
1708
|
-
isCandidate = true;
|
|
1709
|
-
}
|
|
1710
|
-
if (!isCandidate) continue;
|
|
1711
|
-
if (this.degree(h.get(), x) > 0) this.primitiveZpX(h.get(), x, c);
|
|
1712
|
-
else c.set(this.normalize(h.get()));
|
|
1713
|
-
if (this.divides(c.get(), ppU.get()) && this.divides(c.get(), ppV.get())) {
|
|
1714
|
-
r.set(this.mul(cG.get(), c.get()));
|
|
1715
|
-
r.set(this.mulConst(ciG, r.get()));
|
|
1716
|
-
} else if (minDegQ === 0) {
|
|
1717
|
-
r.copy(cG);
|
|
1718
|
-
r.set(this.mulConst(ciG, r.get()));
|
|
1719
|
-
} else if (skeleton !== null) return false;
|
|
1720
|
-
else continue;
|
|
1721
|
-
if (skeleton === null) this.mgcdSkeletons[idx] = new Skeleton(this, h.get(), x);
|
|
1722
|
-
return true;
|
|
1723
|
-
}
|
|
1724
|
-
} finally {
|
|
1725
|
-
lcH.reset();
|
|
1726
|
-
c.reset();
|
|
1727
|
-
h.reset();
|
|
1728
|
-
q.reset();
|
|
1729
|
-
v1.reset();
|
|
1730
|
-
u1.reset();
|
|
1731
|
-
lcG.reset();
|
|
1732
|
-
cG.reset();
|
|
1733
|
-
lcV.reset();
|
|
1734
|
-
ppV.reset();
|
|
1735
|
-
cV.reset();
|
|
1736
|
-
lcU.reset();
|
|
1737
|
-
ppU.reset();
|
|
1738
|
-
cU.reset();
|
|
1739
|
-
}
|
|
768
|
+
if (polynomial.ms[index].degreeOf(variable) !== power)
|
|
769
|
+
this.cheap.add(polynomial.as[index], polynomial.ms[index]);
|
|
770
|
+
return this.cheap.mk();
|
|
1740
771
|
}
|
|
1741
772
|
|
|
1742
|
-
private
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
const minDegrees = uDegrees.map((entry, index): [number, number] => [
|
|
1750
|
-
entry[0],
|
|
1751
|
-
Math.min(entry[1], vDegrees[index][1]),
|
|
1752
|
-
]);
|
|
1753
|
-
minDegrees.sort((first, second) => first[1] - second[1] || first[0] - second[0]);
|
|
1754
|
-
this.mgcdSkeletons = minDegrees.map(() => null);
|
|
1755
|
-
const vars = minDegrees.map((entry) => entry[0]);
|
|
1756
|
-
const ppU = new PolynomialRef(this);
|
|
1757
|
-
const ppV = new PolynomialRef(this);
|
|
1758
|
-
const uZp = new PolynomialRef(this);
|
|
1759
|
-
const vZp = new PolynomialRef(this);
|
|
1760
|
-
const cStar = new PolynomialRef(this);
|
|
1761
|
-
const q = new PolynomialRef(this);
|
|
1762
|
-
const candidate = new PolynomialRef(this);
|
|
1763
|
-
try {
|
|
1764
|
-
const cU = this.icWithContent(u, ppU);
|
|
1765
|
-
const cV = this.icWithContent(v, ppV);
|
|
1766
|
-
const dA = getGcd(cU, cV);
|
|
1767
|
-
const lcU = ppU.get().as[ppU.get().getGradedLexMaxPos()];
|
|
1768
|
-
const lcV = ppV.get().as[ppV.get().getGradedLexMaxPos()];
|
|
1769
|
-
const lcG = getGcd(lcU, lcV);
|
|
1770
|
-
let bound = 0n;
|
|
1771
|
-
for (const prime of BIG_PRIMES) {
|
|
1772
|
-
const savedModulus = this.modulus;
|
|
1773
|
-
this.setZp(prime);
|
|
1774
|
-
try {
|
|
1775
|
-
uZp.set(this.normalize(ppU.get()));
|
|
1776
|
-
if (uZp.get().size !== ppU.get().size) continue;
|
|
1777
|
-
vZp.set(this.normalize(ppV.get()));
|
|
1778
|
-
if (vZp.get().size !== ppV.get().size) continue;
|
|
1779
|
-
if (!this.modGcdRec(uZp.get(), vZp.get(), Number(prime), 0, vars, q)) return false;
|
|
1780
|
-
q.set(this.mkGlexMonic(q.get()));
|
|
1781
|
-
q.set(this.mulConst(this.normalizeNumeral(lcG), q.get()));
|
|
1782
|
-
} finally {
|
|
1783
|
-
this.modulus = savedModulus;
|
|
1784
|
-
}
|
|
1785
|
-
if (this.isConst(q.get())) {
|
|
1786
|
-
r.set(this.mkConst(dA));
|
|
1787
|
-
return true;
|
|
1788
|
-
}
|
|
1789
|
-
if (cStar.getOrNull() === null) {
|
|
1790
|
-
cStar.copy(q);
|
|
1791
|
-
bound = prime;
|
|
1792
|
-
} else {
|
|
1793
|
-
const maxCStar = cStar.get().ms[cStar.get().getGradedLexMaxPos()];
|
|
1794
|
-
const maxQ = q.get().ms[q.get().getGradedLexMaxPos()];
|
|
1795
|
-
if (gradedLexCompare(maxQ, maxCStar) < 0) {
|
|
1796
|
-
cStar.copy(q);
|
|
1797
|
-
bound = prime;
|
|
1798
|
-
} else bound = this.craCombineImages(q.get(), prime, cStar.get(), bound, cStar);
|
|
1799
|
-
}
|
|
1800
|
-
candidate.set(this.normalize(cStar.get()));
|
|
1801
|
-
const lcCandidate = candidate.get().as[candidate.get().getGradedLexMaxPos()];
|
|
1802
|
-
if (
|
|
1803
|
-
this.dividesNumeral(lcCandidate, lcG) &&
|
|
1804
|
-
this.divides(candidate.get(), ppU.get()) &&
|
|
1805
|
-
this.divides(candidate.get(), ppV.get())
|
|
1806
|
-
) {
|
|
1807
|
-
r.set(this.mulConst(dA, candidate.get()));
|
|
1808
|
-
r.set(this.flipSignIfLmNeg(r.get()));
|
|
1809
|
-
return true;
|
|
1810
|
-
}
|
|
773
|
+
private getMaxDegrees(polynomial: Polynomial) {
|
|
774
|
+
const degrees = new Map<number, number>();
|
|
775
|
+
for (const monomial of polynomial.ms)
|
|
776
|
+
for (let index = 0; index < monomial.size; index++) {
|
|
777
|
+
const variable = monomial.vars[index];
|
|
778
|
+
if (monomial.degrees[index] > (degrees.get(variable) ?? 0))
|
|
779
|
+
degrees.set(variable, monomial.degrees[index]);
|
|
1811
780
|
}
|
|
1812
|
-
|
|
1813
|
-
return true;
|
|
1814
|
-
} finally {
|
|
1815
|
-
candidate.reset();
|
|
1816
|
-
q.reset();
|
|
1817
|
-
cStar.reset();
|
|
1818
|
-
vZp.reset();
|
|
1819
|
-
uZp.reset();
|
|
1820
|
-
ppV.reset();
|
|
1821
|
-
ppU.reset();
|
|
1822
|
-
}
|
|
781
|
+
return degrees;
|
|
1823
782
|
}
|
|
1824
783
|
|
|
1825
784
|
private getVarMaxDegrees(polynomial: Polynomial) {
|
|
@@ -1828,689 +787,207 @@ export class PolynomialManager {
|
|
|
1828
787
|
);
|
|
1829
788
|
}
|
|
1830
789
|
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
}
|
|
1842
|
-
if (u === v) {
|
|
1843
|
-
r.set(u);
|
|
1844
|
-
r.set(this.flipSignIfLmNeg(r.get()));
|
|
1845
|
-
return;
|
|
1846
|
-
}
|
|
1847
|
-
if (this.isConst(u) || this.isConst(v)) {
|
|
1848
|
-
const iV = this.getIc(v);
|
|
1849
|
-
const iU = this.getIc(u);
|
|
1850
|
-
r.set(this.mkConst(getGcd(iV, iU)));
|
|
1851
|
-
return;
|
|
1852
|
-
}
|
|
1853
|
-
const uDegrees = this.getVarMaxDegrees(u);
|
|
1854
|
-
const vDegrees = this.getVarMaxDegrees(v);
|
|
1855
|
-
const size = Math.min(uDegrees.length, vDegrees.length);
|
|
790
|
+
// Deviation from Z3: subresultant PRS (gcd_prs), not mod_gcd, which can return 2 for coprime inputs (polynomial.cpp:4815).
|
|
791
|
+
gcd(first: Polynomial, second: Polynomial): Polynomial {
|
|
792
|
+
if (first.size === 0) return this.flipSignIfLmNeg(second);
|
|
793
|
+
if (second.size === 0) return this.flipSignIfLmNeg(first);
|
|
794
|
+
if (first === second) return this.flipSignIfLmNeg(first);
|
|
795
|
+
if (this.isConst(first) || this.isConst(second))
|
|
796
|
+
return this.mkConst(getGcd(this.getIc(second), this.getIc(first)));
|
|
797
|
+
const firstDegrees = this.getVarMaxDegrees(first);
|
|
798
|
+
const secondDegrees = this.getVarMaxDegrees(second);
|
|
799
|
+
const size = Math.min(firstDegrees.length, secondDegrees.length);
|
|
1856
800
|
for (let index = 0; index < size; index++) {
|
|
1857
|
-
const
|
|
1858
|
-
const
|
|
1859
|
-
if (
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
801
|
+
const firstVar = firstDegrees[index][0];
|
|
802
|
+
const secondVar = secondDegrees[index][0];
|
|
803
|
+
if (firstVar < secondVar) return this.gcdContent(first, firstVar, second);
|
|
804
|
+
if (firstVar > secondVar) return this.gcdContent(second, secondVar, first);
|
|
805
|
+
}
|
|
806
|
+
if (size < firstDegrees.length) return this.gcdContent(first, firstDegrees[size][0], second);
|
|
807
|
+
if (size < secondDegrees.length) return this.gcdContent(second, secondDegrees[size][0], first);
|
|
808
|
+
return this.gcdPrs(first, second, firstDegrees[size - 1][0]);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
private gcdContent(polynomial: Polynomial, variable: number, other: Polynomial) {
|
|
812
|
+
const [integer, content] = this.iccp(polynomial, variable);
|
|
813
|
+
return this.gcd(this.mulConst(integer, content), other);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
private gcdPrs(first: Polynomial, second: Polynomial, variable: number) {
|
|
817
|
+
const [u, v] =
|
|
818
|
+
this.degree(first, variable) < this.degree(second, variable)
|
|
819
|
+
? [second, first]
|
|
820
|
+
: [first, second];
|
|
821
|
+
const [integerU, contentU, primitiveU] = this.iccp(u, variable);
|
|
822
|
+
const [integerV, contentV, primitiveV] = this.iccp(v, variable);
|
|
823
|
+
const contentGcd = this.gcd(contentU, contentV);
|
|
824
|
+
const integerGcd = getGcd(integerU, integerV);
|
|
825
|
+
let ppU = primitiveU;
|
|
826
|
+
let ppV = primitiveV;
|
|
827
|
+
let g = this.one;
|
|
828
|
+
let h = this.one;
|
|
829
|
+
while (true) {
|
|
830
|
+
const delta = this.degree(ppU, variable) - this.degree(ppV, variable);
|
|
831
|
+
const remainder = this.exactPseudoRemainder(ppU, ppV, variable);
|
|
832
|
+
if (remainder.size === 0) {
|
|
833
|
+
const result = this.pp(this.flipSignIfLmNeg(ppV), variable);
|
|
834
|
+
return this.mulConst(integerGcd, this.mul(contentGcd, result));
|
|
1866
835
|
}
|
|
836
|
+
if (this.isConst(remainder)) return this.mulConst(integerGcd, contentGcd);
|
|
837
|
+
ppU = ppV;
|
|
838
|
+
ppV = this.exactDiv(remainder, g);
|
|
839
|
+
for (let index = 0; index < delta; index++) ppV = this.exactDiv(ppV, h);
|
|
840
|
+
g = this.lc(ppU, variable);
|
|
841
|
+
let newH = this.one;
|
|
842
|
+
for (let index = 0; index < delta; index++) newH = this.mul(newH, g);
|
|
843
|
+
for (let index = 1; index < delta; index++) newH = this.exactDiv(newH, h);
|
|
844
|
+
h = newH;
|
|
1867
845
|
}
|
|
1868
|
-
if (size < uDegrees.length) {
|
|
1869
|
-
this.gcdContent(u, uDegrees[size][0], v, r);
|
|
1870
|
-
return;
|
|
1871
|
-
}
|
|
1872
|
-
if (size < vDegrees.length) {
|
|
1873
|
-
this.gcdContent(v, vDegrees[size][0], u, r);
|
|
1874
|
-
return;
|
|
1875
|
-
}
|
|
1876
|
-
const x = uDegrees[size - 1][0];
|
|
1877
|
-
if (this.modulus !== null) {
|
|
1878
|
-
this.gcdPrs(u, v, x, r);
|
|
1879
|
-
return;
|
|
1880
|
-
}
|
|
1881
|
-
if (this.isUnivariate(u)) {
|
|
1882
|
-
this.uniModGcd(u, v, r);
|
|
1883
|
-
return;
|
|
1884
|
-
}
|
|
1885
|
-
if (!this.modGcd(u, v, uDegrees, vDegrees, r)) this.gcdPrs(u, v, x, r);
|
|
1886
846
|
}
|
|
1887
847
|
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
return;
|
|
1896
|
-
}
|
|
1897
|
-
const x = this.maxVar(polynomial);
|
|
1898
|
-
const c = new PolynomialRef(this);
|
|
1899
|
-
const pp = new PolynomialRef(this);
|
|
1900
|
-
const integer = this.iccp(polynomial, x, c, pp);
|
|
1901
|
-
const sqfC = new PolynomialRef(this);
|
|
1902
|
-
this.squareFree(c.get(), sqfC);
|
|
1903
|
-
const ppPrime = new PolynomialRef(this);
|
|
1904
|
-
ppPrime.set(this.derivative(pp.get(), x));
|
|
1905
|
-
const g = new PolynomialRef(this);
|
|
1906
|
-
this.gcd(pp.get(), ppPrime.get(), g);
|
|
1907
|
-
const release = () => {
|
|
1908
|
-
g.reset();
|
|
1909
|
-
ppPrime.reset();
|
|
1910
|
-
sqfC.reset();
|
|
1911
|
-
pp.reset();
|
|
1912
|
-
c.reset();
|
|
1913
|
-
};
|
|
1914
|
-
if (this.isConst(g.get())) {
|
|
1915
|
-
if (this.eq(sqfC.get(), c.get())) {
|
|
1916
|
-
r.set(polynomial);
|
|
1917
|
-
release();
|
|
1918
|
-
return;
|
|
848
|
+
private getMinDegreeVar(polynomial: Polynomial) {
|
|
849
|
+
let result = NULL_VAR;
|
|
850
|
+
let minDegree = Infinity;
|
|
851
|
+
for (const [variable, degree] of this.getMaxDegrees(polynomial))
|
|
852
|
+
if (degree < minDegree) {
|
|
853
|
+
result = variable;
|
|
854
|
+
minDegree = degree;
|
|
1919
855
|
}
|
|
1920
|
-
} else pp.set(this.exactDiv(pp.get(), g.get()));
|
|
1921
|
-
r.set(this.mulConst(integer, sqfC.get()));
|
|
1922
|
-
r.set(this.mul(r.get(), pp.get()));
|
|
1923
|
-
release();
|
|
1924
|
-
}
|
|
1925
|
-
|
|
1926
|
-
private squareFreeIn(polynomial: Polynomial, x: number, r: PolynomialRef) {
|
|
1927
|
-
if (this.isZero(polynomial)) {
|
|
1928
|
-
r.set(this.zero);
|
|
1929
|
-
return;
|
|
1930
|
-
}
|
|
1931
|
-
if (this.isConst(polynomial)) {
|
|
1932
|
-
r.set(polynomial);
|
|
1933
|
-
return;
|
|
1934
|
-
}
|
|
1935
|
-
const pPrime = new PolynomialRef(this);
|
|
1936
|
-
pPrime.set(this.derivative(polynomial, x));
|
|
1937
|
-
const g = new PolynomialRef(this);
|
|
1938
|
-
this.gcd(polynomial, pPrime.get(), g);
|
|
1939
|
-
if (this.isConst(g.get())) r.set(polynomial);
|
|
1940
|
-
else r.set(this.exactDiv(polynomial, g.get()));
|
|
1941
|
-
g.reset();
|
|
1942
|
-
pPrime.reset();
|
|
1943
|
-
}
|
|
1944
|
-
|
|
1945
|
-
private isSquareFreeIn(polynomial: Polynomial, x: number) {
|
|
1946
|
-
const r = new PolynomialRef(this);
|
|
1947
|
-
this.squareFreeIn(polynomial, x, r);
|
|
1948
|
-
const result = polynomial === r.get();
|
|
1949
|
-
r.reset();
|
|
1950
856
|
return result;
|
|
1951
857
|
}
|
|
1952
858
|
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
if (k === 1) {
|
|
1959
|
-
r.set(polynomial);
|
|
1960
|
-
return;
|
|
1961
|
-
}
|
|
1962
|
-
const result = new PolynomialRef(this);
|
|
1963
|
-
result.set(polynomial);
|
|
1964
|
-
for (let index = 1; index < k; index++) result.set(this.mul(result.get(), polynomial));
|
|
1965
|
-
r.copy(result);
|
|
1966
|
-
result.reset();
|
|
1967
|
-
}
|
|
1968
|
-
|
|
1969
|
-
composeXDivY(polynomial: Polynomial, y: number) {
|
|
1970
|
-
if (this.isConst(polynomial)) return polynomial;
|
|
1971
|
-
const x = this.maxVar(polynomial);
|
|
1972
|
-
const n = this.degree(polynomial, x);
|
|
1973
|
-
for (let index = 0; index < polynomial.size; index++) {
|
|
1974
|
-
const k = polynomial.ms[index].degreeOf(x);
|
|
1975
|
-
const vars: number[] = [];
|
|
1976
|
-
const degrees: number[] = [];
|
|
1977
|
-
const push = (variable: number, degree: number) => {
|
|
1978
|
-
if (degree === 0) return;
|
|
1979
|
-
vars.push(variable);
|
|
1980
|
-
degrees.push(degree);
|
|
1981
|
-
};
|
|
1982
|
-
if (x < y) {
|
|
1983
|
-
push(x, k);
|
|
1984
|
-
push(y, n - k);
|
|
1985
|
-
} else {
|
|
1986
|
-
push(y, n - k);
|
|
1987
|
-
push(x, k);
|
|
1988
|
-
}
|
|
1989
|
-
this.cheap.add(polynomial.as[index], this.mkMonomial(vars, degrees));
|
|
1990
|
-
}
|
|
1991
|
-
return this.cheap.mk();
|
|
859
|
+
factor(polynomial: Polynomial): Factors {
|
|
860
|
+
const result: Factors = { constant: 1n, factors: [], degrees: [] };
|
|
861
|
+
if (polynomial.size === 0) result.constant = 0n;
|
|
862
|
+
else this.factorCore(polynomial, result);
|
|
863
|
+
return result;
|
|
1992
864
|
}
|
|
1993
865
|
|
|
1994
|
-
private
|
|
866
|
+
private factorCore(polynomial: Polynomial, result: Factors) {
|
|
1995
867
|
if (this.isConst(polynomial)) {
|
|
1996
|
-
|
|
868
|
+
result.constant *= polynomial.as[0];
|
|
1997
869
|
return;
|
|
1998
870
|
}
|
|
1999
|
-
const
|
|
2000
|
-
const
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
const position = degree2pos[d - index];
|
|
2007
|
-
r.set(this.muladd(q, r.get(), position === -1 ? 0n : polynomial.as[position]));
|
|
2008
|
-
}
|
|
2009
|
-
}
|
|
2010
|
-
|
|
2011
|
-
private mkXMinusY(x: number, y: number) {
|
|
2012
|
-
return this.mkLinear([1n, this.normalizeNumeral(-1n)], [x, y], 0n);
|
|
2013
|
-
}
|
|
2014
|
-
|
|
2015
|
-
composeXMinusY(polynomial: Polynomial, y: number, r: PolynomialRef) {
|
|
2016
|
-
const x = this.maxVar(polynomial);
|
|
2017
|
-
if (y === x) {
|
|
2018
|
-
r.set(this.coeff(polynomial, x, 0));
|
|
871
|
+
const variable = this.getMinDegreeVar(polynomial);
|
|
872
|
+
const [integer, content, primitive] = this.iccp(polynomial, variable);
|
|
873
|
+
result.constant *= integer;
|
|
874
|
+
this.factorCore(content, result);
|
|
875
|
+
let square = this.gcd(primitive, this.derivative(primitive, variable));
|
|
876
|
+
if (this.isConst(square)) {
|
|
877
|
+
this.factorSqfPp(primitive, variable, 1, result);
|
|
2019
878
|
return;
|
|
2020
879
|
}
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
q: Polynomial,
|
|
2030
|
-
x: number,
|
|
2031
|
-
isExact: boolean,
|
|
2032
|
-
isQuotient: boolean,
|
|
2033
|
-
quotient: PolynomialRef,
|
|
2034
|
-
remainder: PolynomialRef,
|
|
2035
|
-
) {
|
|
2036
|
-
const a = p;
|
|
2037
|
-
const b = q;
|
|
2038
|
-
const degA = this.degree(a, x);
|
|
2039
|
-
const degB = this.degree(b, x);
|
|
2040
|
-
if (degB === 0) {
|
|
2041
|
-
remainder.set(this.zero);
|
|
2042
|
-
if (!isQuotient) return 0;
|
|
2043
|
-
if (!isExact) {
|
|
2044
|
-
quotient.set(a);
|
|
2045
|
-
return 1;
|
|
2046
|
-
}
|
|
2047
|
-
const d = degA + 1;
|
|
2048
|
-
if (d === 1) {
|
|
2049
|
-
quotient.set(a);
|
|
2050
|
-
return d;
|
|
2051
|
-
}
|
|
2052
|
-
const bdm1 = new PolynomialRef(this);
|
|
2053
|
-
this.pw(b, d - 1, bdm1);
|
|
2054
|
-
quotient.set(this.mul(a, bdm1.get()));
|
|
2055
|
-
bdm1.reset();
|
|
2056
|
-
return d;
|
|
2057
|
-
}
|
|
2058
|
-
if (degB > degA) {
|
|
2059
|
-
quotient.set(this.zero);
|
|
2060
|
-
remainder.set(a);
|
|
2061
|
-
}
|
|
2062
|
-
const lB = new PolynomialRef(this);
|
|
2063
|
-
const rB = new PolynomialRef(this);
|
|
2064
|
-
lB.set(this.coeffWithReduct(b, x, degB, rB));
|
|
2065
|
-
let d = 0;
|
|
2066
|
-
remainder.set(a);
|
|
2067
|
-
quotient.set(this.zero);
|
|
2068
|
-
while (true) {
|
|
2069
|
-
this.limit.checkpoint();
|
|
2070
|
-
const r = remainder.get();
|
|
2071
|
-
const degR = this.degree(r, x);
|
|
2072
|
-
if (degB > degR) {
|
|
2073
|
-
if (isExact) {
|
|
2074
|
-
const exactD = degA - degB + 1;
|
|
2075
|
-
if (d < exactD) {
|
|
2076
|
-
const lBE = new PolynomialRef(this);
|
|
2077
|
-
this.pw(lB.get(), exactD - d, lBE);
|
|
2078
|
-
if (isQuotient) quotient.set(this.mul(lBE.get(), quotient.get()));
|
|
2079
|
-
remainder.set(this.mul(lBE.get(), remainder.get()));
|
|
2080
|
-
lBE.reset();
|
|
2081
|
-
}
|
|
2082
|
-
}
|
|
2083
|
-
rB.reset();
|
|
2084
|
-
lB.reset();
|
|
2085
|
-
return d;
|
|
2086
|
-
}
|
|
2087
|
-
this.som.reset();
|
|
2088
|
-
this.som2.reset();
|
|
2089
|
-
for (let index = 0; index < r.size; index++) {
|
|
2090
|
-
if (r.size > 100 && index % 100 === 0) this.limit.checkpoint();
|
|
2091
|
-
const monomial = r.ms[index];
|
|
2092
|
-
const coeff = r.as[index];
|
|
2093
|
-
if (monomial.degreeOf(x) === degR) {
|
|
2094
|
-
const shifted = this.divXk(monomial, x, degB);
|
|
2095
|
-
if (isQuotient) this.som2.addTerm(coeff, shifted);
|
|
2096
|
-
this.som.addmul(this.negNumeral(coeff), shifted, rB.get());
|
|
2097
|
-
} else this.som.addmul(coeff, monomial, lB.get());
|
|
2098
|
-
}
|
|
2099
|
-
remainder.set(this.som.mk());
|
|
2100
|
-
if (isQuotient) {
|
|
2101
|
-
const current = quotient.get();
|
|
2102
|
-
for (let index = 0; index < current.size; index++)
|
|
2103
|
-
this.som2.addmul(current.as[index], current.ms[index], lB.get());
|
|
2104
|
-
quotient.set(this.som2.mk());
|
|
2105
|
-
}
|
|
2106
|
-
d++;
|
|
880
|
+
let product = this.exactDiv(primitive, square);
|
|
881
|
+
for (let multiplicity = 1; !this.isConst(product); multiplicity++) {
|
|
882
|
+
const next = this.gcd(product, square);
|
|
883
|
+
const factor = this.exactDiv(product, next);
|
|
884
|
+
if (!this.isConst(factor)) this.factorSqfPp(factor, variable, multiplicity, result);
|
|
885
|
+
else if (factor.as[0] === -1n && multiplicity % 2 === 1) result.constant = -result.constant;
|
|
886
|
+
square = this.exactDiv(square, next);
|
|
887
|
+
product = next;
|
|
2107
888
|
}
|
|
2108
889
|
}
|
|
2109
890
|
|
|
2110
|
-
|
|
2111
|
-
const
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
const quotient = new PolynomialRef(this);
|
|
2118
|
-
const d = this.pseudoDivisionCore(p, q, x, false, false, quotient, remainder);
|
|
2119
|
-
quotient.reset();
|
|
2120
|
-
return d;
|
|
891
|
+
private factorSqfPp(polynomial: Polynomial, variable: number, k: number, result: Factors) {
|
|
892
|
+
const degree = this.degree(polynomial, variable);
|
|
893
|
+
if (degree === 1) this.pushFactor(result, polynomial, k);
|
|
894
|
+
else if (this.isUnivariate(polynomial)) this.factorSqfPpUniv(polynomial, k, result);
|
|
895
|
+
else if (degree === 2) this.factorQuadratic(polynomial, variable, k, result);
|
|
896
|
+
else if (!this.factorMultivariate(polynomial, variable, k, result))
|
|
897
|
+
this.pushFactor(result, polynomial, k);
|
|
2121
898
|
}
|
|
2122
899
|
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
x: number,
|
|
2127
|
-
quotient: PolynomialRef,
|
|
2128
|
-
remainder: PolynomialRef,
|
|
2129
|
-
) {
|
|
2130
|
-
this.pseudoDivisionCore(p, q, x, true, true, quotient, remainder);
|
|
900
|
+
private pushFactor(result: Factors, polynomial: Polynomial, k: number) {
|
|
901
|
+
result.factors.push(polynomial);
|
|
902
|
+
result.degrees.push(k);
|
|
2131
903
|
}
|
|
2132
904
|
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
quotient.reset();
|
|
2139
|
-
remainder.add(polynomial);
|
|
2140
|
-
const maxPos = divisor.getGradedLexMaxPos();
|
|
2141
|
-
const divisorMonomial = divisor.ms[maxPos];
|
|
2142
|
-
const divisorCoeff = divisor.as[maxPos];
|
|
2143
|
-
while (true) {
|
|
2144
|
-
this.limit.checkpoint();
|
|
2145
|
-
const remainderPos = remainder.getGradedLexMaxPos();
|
|
2146
|
-
if (remainderPos === -1) {
|
|
2147
|
-
remainder.reset();
|
|
2148
|
-
return quotient.mk();
|
|
2149
|
-
}
|
|
2150
|
-
const monomial = this.divMonomials(remainder.ms[remainderPos], divisorMonomial);
|
|
2151
|
-
if (monomial === null) throw new SmtInvariantError("exact_div: the divisor does not divide");
|
|
2152
|
-
const coeff = this.divNumeral(remainder.as[remainderPos], divisorCoeff);
|
|
2153
|
-
quotient.addTerm(coeff, monomial);
|
|
2154
|
-
remainder.addmul(this.negNumeral(coeff), monomial, divisor);
|
|
2155
|
-
}
|
|
905
|
+
toCoeffs(polynomial: Polynomial) {
|
|
906
|
+
const coeffs = Array.from({ length: this.totalDegree(polynomial) + 1 }, () => 0n);
|
|
907
|
+
for (let index = 0; index < polynomial.size; index++)
|
|
908
|
+
coeffs[polynomial.ms[index].totalDegree] = polynomial.as[index];
|
|
909
|
+
return this.um.set(coeffs);
|
|
2156
910
|
}
|
|
2157
911
|
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
this.
|
|
2162
|
-
this.som2.reset();
|
|
2163
|
-
remainder.add(polynomial);
|
|
2164
|
-
const maxPos = divisor.getGradedLexMaxPos();
|
|
2165
|
-
const divisorMonomial = divisor.ms[maxPos];
|
|
2166
|
-
const divisorCoeff = divisor.as[maxPos];
|
|
2167
|
-
while (true) {
|
|
2168
|
-
this.limit.checkpoint();
|
|
2169
|
-
const remainderPos = remainder.getGradedLexMaxPos();
|
|
2170
|
-
if (remainderPos === -1) return true;
|
|
2171
|
-
const monomial = this.divMonomials(remainder.ms[remainderPos], divisorMonomial);
|
|
2172
|
-
if (monomial === null) return false;
|
|
2173
|
-
if (!this.dividesNumeral(divisorCoeff, remainder.as[remainderPos])) return false;
|
|
2174
|
-
const coeff = this.divNumeral(remainder.as[remainderPos], divisorCoeff);
|
|
2175
|
-
remainder.addmul(this.negNumeral(coeff), monomial, divisor);
|
|
2176
|
-
}
|
|
912
|
+
fromCoeffs(coeffs: readonly bigint[], variable: number) {
|
|
913
|
+
for (let power = coeffs.length - 1; power >= 0; power--)
|
|
914
|
+
this.cheap.add(coeffs[power], this.mkPower(variable, power));
|
|
915
|
+
return this.cheap.mk();
|
|
2177
916
|
}
|
|
2178
917
|
|
|
2179
|
-
|
|
2180
|
-
const
|
|
2181
|
-
const
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
result.set(this.zero);
|
|
2186
|
-
b.reset();
|
|
2187
|
-
a.reset();
|
|
2188
|
-
return;
|
|
2189
|
-
}
|
|
2190
|
-
if (this.isConst(a.get())) {
|
|
2191
|
-
if (this.isConst(b.get())) result.set(this.one);
|
|
2192
|
-
else this.pw(a.get(), this.degree(b.get(), x), result);
|
|
2193
|
-
b.reset();
|
|
2194
|
-
a.reset();
|
|
2195
|
-
return;
|
|
2196
|
-
}
|
|
2197
|
-
if (this.isConst(b.get())) {
|
|
2198
|
-
this.pw(b.get(), this.degree(a.get(), x), result);
|
|
2199
|
-
b.reset();
|
|
2200
|
-
a.reset();
|
|
918
|
+
private factorSqfPpUniv(polynomial: Polynomial, k: number, result: Factors) {
|
|
919
|
+
const variable = this.maxVar(polynomial);
|
|
920
|
+
const fs = mkFactors();
|
|
921
|
+
factorSquareFree(this.um, this.toCoeffs(polynomial), fs, 1, DEFAULT_FACTOR_PARAMS);
|
|
922
|
+
if (fs.factors.length === 1 && fs.degrees[0] === 1) {
|
|
923
|
+
this.pushFactor(result, polynomial, k);
|
|
2201
924
|
return;
|
|
2202
925
|
}
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
const ppB = new PolynomialRef(this);
|
|
2207
|
-
const iA = this.iccp(a.get(), x, cA, ppA);
|
|
2208
|
-
const iB = this.iccp(b.get(), x, cB, ppB);
|
|
2209
|
-
cA.set(this.mulConst(iA, cA.get()));
|
|
2210
|
-
cB.set(this.mulConst(iB, cB.get()));
|
|
2211
|
-
const t = new PolynomialRef(this);
|
|
2212
|
-
this.pw(cA.get(), this.degree(b.get(), x), cA);
|
|
2213
|
-
this.pw(cB.get(), this.degree(a.get(), x), cB);
|
|
2214
|
-
t.set(this.mul(cA.get(), cB.get()));
|
|
2215
|
-
a.copy(ppA);
|
|
2216
|
-
b.copy(ppB);
|
|
2217
|
-
let s = 1;
|
|
2218
|
-
let degA = this.degree(a.get(), x);
|
|
2219
|
-
let degB = this.degree(b.get(), x);
|
|
2220
|
-
if (degA < degB) {
|
|
2221
|
-
a.swap(b);
|
|
2222
|
-
if (degA % 2 === 1 && degB % 2 === 1) s = -1;
|
|
2223
|
-
}
|
|
2224
|
-
const remainder = new PolynomialRef(this);
|
|
2225
|
-
const g = new PolynomialRef(this);
|
|
2226
|
-
const h = new PolynomialRef(this);
|
|
2227
|
-
const newH = new PolynomialRef(this);
|
|
2228
|
-
g.set(this.one);
|
|
2229
|
-
h.set(this.one);
|
|
2230
|
-
while (true) {
|
|
2231
|
-
this.limit.checkpoint();
|
|
2232
|
-
degA = this.degree(a.get(), x);
|
|
2233
|
-
degB = this.degree(b.get(), x);
|
|
2234
|
-
const delta = degA - degB;
|
|
2235
|
-
if (degA % 2 === 1 && degB % 2 === 1) s = -s;
|
|
2236
|
-
this.exactPseudoRemainder(a.get(), b.get(), x, remainder);
|
|
2237
|
-
a.copy(b);
|
|
2238
|
-
b.set(this.exactDiv(remainder.get(), g.get()));
|
|
2239
|
-
for (let index = 0; index < delta; index++) b.set(this.exactDiv(b.get(), h.get()));
|
|
2240
|
-
g.set(this.lc(a.get(), x));
|
|
2241
|
-
newH.set(this.one);
|
|
2242
|
-
this.pw(g.get(), delta, newH);
|
|
2243
|
-
if (delta > 1)
|
|
2244
|
-
for (let index = 0; index < delta - 1; index++)
|
|
2245
|
-
newH.set(this.exactDiv(newH.get(), h.get()));
|
|
2246
|
-
h.copy(newH);
|
|
2247
|
-
if (this.degree(b.get(), x) === 0) {
|
|
2248
|
-
const lastDegA = this.degree(a.get(), x);
|
|
2249
|
-
newH.set(this.lc(b.get(), x));
|
|
2250
|
-
this.pw(newH.get(), lastDegA, newH);
|
|
2251
|
-
if (lastDegA > 1)
|
|
2252
|
-
for (let index = 0; index < lastDegA - 1; index++)
|
|
2253
|
-
newH.set(this.exactDiv(newH.get(), h.get()));
|
|
2254
|
-
h.copy(newH);
|
|
2255
|
-
result.set(this.mul(t.get(), h.get()));
|
|
2256
|
-
if (s < 0) result.set(this.neg(result.get()));
|
|
2257
|
-
newH.reset();
|
|
2258
|
-
h.reset();
|
|
2259
|
-
g.reset();
|
|
2260
|
-
remainder.reset();
|
|
2261
|
-
t.reset();
|
|
2262
|
-
ppB.reset();
|
|
2263
|
-
ppA.reset();
|
|
2264
|
-
cB.reset();
|
|
2265
|
-
cA.reset();
|
|
2266
|
-
b.reset();
|
|
2267
|
-
a.reset();
|
|
2268
|
-
return;
|
|
2269
|
-
}
|
|
2270
|
-
}
|
|
926
|
+
for (let index = 0; index < fs.factors.length; index++)
|
|
927
|
+
this.pushFactor(result, this.fromCoeffs(fs.factors[index], variable), k * fs.degrees[index]);
|
|
928
|
+
if (fs.constant === -1n && k % 2 === 1) result.constant = -result.constant;
|
|
2271
929
|
}
|
|
2272
930
|
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
931
|
+
private factorQuadratic(polynomial: Polynomial, variable: number, k: number, result: Factors) {
|
|
932
|
+
let a = this.coeff(polynomial, variable, 2);
|
|
933
|
+
let b = this.coeff(polynomial, variable, 1);
|
|
934
|
+
let c = this.coeff(polynomial, variable, 0);
|
|
935
|
+
const isFlipped = a.as[a.getGradedLexMaxPos()] < 0n;
|
|
936
|
+
if (isFlipped) {
|
|
937
|
+
a = this.neg(a);
|
|
938
|
+
b = this.neg(b);
|
|
939
|
+
c = this.neg(c);
|
|
2280
940
|
}
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
const lc = this.getConstCoeff(polynomial, x, m);
|
|
2285
|
-
if (lc !== null) r.set(this.divConst(r.get(), isNegated ? this.negNumeral(lc) : lc));
|
|
2286
|
-
else {
|
|
2287
|
-
if (isNegated) r.set(this.neg(r.get()));
|
|
2288
|
-
const c = new PolynomialRef(this);
|
|
2289
|
-
c.set(this.coeff(polynomial, x, m));
|
|
2290
|
-
r.set(this.exactDiv(r.get(), c.get()));
|
|
2291
|
-
c.reset();
|
|
2292
|
-
}
|
|
2293
|
-
pPrime.reset();
|
|
2294
|
-
}
|
|
2295
|
-
|
|
2296
|
-
private seLazard(d: number, lcSd: Polynomial, sd1: Polynomial, x: number, se: PolynomialRef) {
|
|
2297
|
-
let n = d - this.degree(sd1, x) - 1;
|
|
2298
|
-
if (n === 0) {
|
|
2299
|
-
se.set(sd1);
|
|
941
|
+
const root = this.sqrt(this.addmul(1n, this.mul(b, b), -4n, this.mul(a, c)));
|
|
942
|
+
if (root === null) {
|
|
943
|
+
this.pushFactor(result, polynomial, k);
|
|
2300
944
|
return;
|
|
2301
945
|
}
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
const c = new PolynomialRef(this);
|
|
2307
|
-
c.copy(xLc);
|
|
2308
|
-
n -= a;
|
|
2309
|
-
while (a !== 1) {
|
|
2310
|
-
a /= 2;
|
|
2311
|
-
c.set(this.mul(c.get(), c.get()));
|
|
2312
|
-
c.set(this.exactDiv(c.get(), y));
|
|
2313
|
-
if (n >= a) {
|
|
2314
|
-
c.set(this.mul(c.get(), xLc.get()));
|
|
2315
|
-
c.set(this.exactDiv(c.get(), y));
|
|
2316
|
-
n -= a;
|
|
2317
|
-
}
|
|
2318
|
-
}
|
|
2319
|
-
se.set(this.mul(c.get(), sd1));
|
|
2320
|
-
se.set(this.exactDiv(se.get(), y));
|
|
2321
|
-
c.reset();
|
|
2322
|
-
xLc.reset();
|
|
2323
|
-
}
|
|
2324
|
-
|
|
2325
|
-
private optimizedSe1(
|
|
2326
|
-
d: number,
|
|
2327
|
-
e: number,
|
|
2328
|
-
a: Polynomial,
|
|
2329
|
-
sd1: Polynomial,
|
|
2330
|
-
se: Polynomial,
|
|
2331
|
-
s: Polynomial,
|
|
2332
|
-
x: number,
|
|
2333
|
-
se1: PolynomialRef,
|
|
2334
|
-
) {
|
|
2335
|
-
const cD1 = new PolynomialRef(this);
|
|
2336
|
-
const sE = new PolynomialRef(this);
|
|
2337
|
-
const xJ = new PolynomialRef(this);
|
|
2338
|
-
const tmp = new PolynomialRef(this);
|
|
2339
|
-
cD1.set(this.lc(sd1, x));
|
|
2340
|
-
sE.set(this.lc(se, x));
|
|
2341
|
-
const h = new PolynomialRefVector(this);
|
|
2342
|
-
xJ.set(this.one);
|
|
2343
|
-
for (let j = 0; j <= e - 1; j++) {
|
|
2344
|
-
xJ.set(this.mkVar(x, j));
|
|
2345
|
-
h.push(this.mul(sE.get(), xJ.get()));
|
|
2346
|
-
}
|
|
2347
|
-
xJ.set(this.mkVar(x, e));
|
|
2348
|
-
xJ.set(this.mul(sE.get(), xJ.get()));
|
|
2349
|
-
h.push(this.sub(xJ.get(), se));
|
|
2350
|
-
const xPol = new PolynomialRef(this);
|
|
2351
|
-
const xH = new PolynomialRef(this);
|
|
2352
|
-
const xHe = new PolynomialRef(this);
|
|
2353
|
-
xPol.set(this.mkVar(x, 1));
|
|
2354
|
-
for (let j = e + 1; j <= d - 1; j++) {
|
|
2355
|
-
xH.set(this.mul(xPol.get(), h.get(j - 1)));
|
|
2356
|
-
xHe.set(this.coeff(xH.get(), x, e));
|
|
2357
|
-
tmp.set(this.mul(xHe.get(), sd1));
|
|
2358
|
-
tmp.set(this.exactDiv(tmp.get(), cD1.get()));
|
|
2359
|
-
h.push(this.sub(xH.get(), tmp.get()));
|
|
2360
|
-
}
|
|
2361
|
-
const dPolynomial = new PolynomialRef(this);
|
|
2362
|
-
dPolynomial.set(this.zero);
|
|
2363
|
-
for (let j = 0; j < d; j++) {
|
|
2364
|
-
tmp.set(this.coeff(a, x, j));
|
|
2365
|
-
tmp.set(this.mul(tmp.get(), h.get(j)));
|
|
2366
|
-
dPolynomial.set(this.add(dPolynomial.get(), tmp.get()));
|
|
2367
|
-
}
|
|
2368
|
-
const lcA = new PolynomialRef(this);
|
|
2369
|
-
lcA.set(this.lc(a, x));
|
|
2370
|
-
dPolynomial.set(this.exactDiv(dPolynomial.get(), lcA.get()));
|
|
2371
|
-
xH.set(this.mul(xPol.get(), h.get(d - 1)));
|
|
2372
|
-
xHe.set(this.coeff(xH.get(), x, e));
|
|
2373
|
-
xHe.set(this.mul(xHe.get(), sd1));
|
|
2374
|
-
se1.set(this.add(xH.get(), dPolynomial.get()));
|
|
2375
|
-
se1.set(this.mul(cD1.get(), se1.get()));
|
|
2376
|
-
se1.set(this.sub(se1.get(), xHe.get()));
|
|
2377
|
-
se1.set(this.exactDiv(se1.get(), s));
|
|
2378
|
-
if ((d - e + 1) % 2 === 1) se1.set(this.neg(se1.get()));
|
|
2379
|
-
lcA.reset();
|
|
2380
|
-
dPolynomial.reset();
|
|
2381
|
-
xHe.reset();
|
|
2382
|
-
xH.reset();
|
|
2383
|
-
xPol.reset();
|
|
2384
|
-
h.reset();
|
|
2385
|
-
tmp.reset();
|
|
2386
|
-
xJ.reset();
|
|
2387
|
-
sE.reset();
|
|
2388
|
-
cD1.reset();
|
|
2389
|
-
}
|
|
2390
|
-
|
|
2391
|
-
private pscChainOptimizedCore(
|
|
2392
|
-
p: Polynomial,
|
|
2393
|
-
q: Polynomial,
|
|
2394
|
-
x: number,
|
|
2395
|
-
chain: PolynomialRefVector,
|
|
2396
|
-
) {
|
|
2397
|
-
const degP = this.degree(p, x);
|
|
2398
|
-
const degQ = this.degree(q, x);
|
|
2399
|
-
const a = new PolynomialRef(this);
|
|
2400
|
-
const b = new PolynomialRef(this);
|
|
2401
|
-
const c = new PolynomialRef(this);
|
|
2402
|
-
const minusQ = new PolynomialRef(this);
|
|
2403
|
-
const lcQ = new PolynomialRef(this);
|
|
2404
|
-
const ps = new PolynomialRef(this);
|
|
2405
|
-
lcQ.set(this.lc(q, x));
|
|
2406
|
-
const s = new PolynomialRef(this);
|
|
2407
|
-
const release = () => {
|
|
2408
|
-
s.reset();
|
|
2409
|
-
ps.reset();
|
|
2410
|
-
lcQ.reset();
|
|
2411
|
-
minusQ.reset();
|
|
2412
|
-
c.reset();
|
|
2413
|
-
b.reset();
|
|
2414
|
-
a.reset();
|
|
2415
|
-
};
|
|
2416
|
-
this.pw(lcQ.get(), degP - degQ, s);
|
|
2417
|
-
minusQ.set(this.neg(q));
|
|
2418
|
-
a.set(q);
|
|
2419
|
-
this.exactPseudoRemainder(p, minusQ.get(), x, b);
|
|
2420
|
-
while (true) {
|
|
2421
|
-
const d = this.degree(a.get(), x);
|
|
2422
|
-
const e = this.degree(b.get(), x);
|
|
2423
|
-
if (this.isZero(b.get())) {
|
|
2424
|
-
release();
|
|
2425
|
-
return;
|
|
2426
|
-
}
|
|
2427
|
-
ps.set(this.coeff(b.get(), x, d - 1));
|
|
2428
|
-
if (!this.isZero(ps.get())) chain.push(ps.get());
|
|
2429
|
-
const delta = d - e;
|
|
2430
|
-
if (delta > 1) {
|
|
2431
|
-
this.seLazard(d, s.get(), b.get(), x, c);
|
|
2432
|
-
ps.set(this.coeff(c.get(), x, e));
|
|
2433
|
-
if (!this.isZero(ps.get())) chain.push(ps.get());
|
|
2434
|
-
} else c.copy(b);
|
|
2435
|
-
if (e === 0) {
|
|
2436
|
-
release();
|
|
2437
|
-
return;
|
|
2438
|
-
}
|
|
2439
|
-
this.optimizedSe1(d, e, a.get(), b.get(), c.get(), s.get(), x, b);
|
|
2440
|
-
a.copy(c);
|
|
2441
|
-
s.set(this.lc(a.get(), x));
|
|
2442
|
-
}
|
|
2443
|
-
}
|
|
2444
|
-
|
|
2445
|
-
pscChain(p: Polynomial, q: Polynomial, x: number, chain: PolynomialRefVector) {
|
|
2446
|
-
chain.reset();
|
|
2447
|
-
if (this.degree(p, x) >= this.degree(q, x)) this.pscChainOptimizedCore(p, q, x, chain);
|
|
2448
|
-
else this.pscChainOptimizedCore(q, p, x, chain);
|
|
2449
|
-
if (chain.size === 0) chain.push(this.zero);
|
|
2450
|
-
chain.items.reverse();
|
|
2451
|
-
}
|
|
2452
|
-
|
|
2453
|
-
normalize(polynomial: Polynomial) {
|
|
2454
|
-
if (polynomial.size === 0) return polynomial;
|
|
2455
|
-
const modulus = this.modulus;
|
|
2456
|
-
if (modulus !== null) {
|
|
2457
|
-
let index = 0;
|
|
2458
|
-
for (; index < polynomial.size; index++)
|
|
2459
|
-
if (normalizeZp(polynomial.as[index], modulus) !== polynomial.as[index]) break;
|
|
2460
|
-
if (index < polynomial.size) {
|
|
2461
|
-
this.cheap.reset();
|
|
2462
|
-
for (let other = 0; other < polynomial.size; other++)
|
|
2463
|
-
this.cheap.addReset(normalizeZp(polynomial.as[other], modulus), polynomial.ms[other]);
|
|
2464
|
-
this.cheap.normalize();
|
|
2465
|
-
return this.cheap.mk();
|
|
2466
|
-
}
|
|
2467
|
-
}
|
|
2468
|
-
const g = this.gcdNumerals(polynomial.as);
|
|
2469
|
-
if (g === 1n) return polynomial;
|
|
2470
|
-
this.cheap.reset();
|
|
2471
|
-
for (let index = 0; index < polynomial.size; index++)
|
|
2472
|
-
this.cheap.addReset(this.divNumeral(polynomial.as[index], g), polynomial.ms[index]);
|
|
2473
|
-
return this.cheap.mk();
|
|
946
|
+
if (isFlipped && k % 2 === 1) result.constant = -result.constant;
|
|
947
|
+
const linear = this.add(this.mulTerm(2n, this.mkPower(variable, 1), a), b);
|
|
948
|
+
this.pushFactor(result, this.pp(this.sub(linear, root), variable), k);
|
|
949
|
+
this.pushFactor(result, this.pp(this.add(linear, root), variable), k);
|
|
2474
950
|
}
|
|
2475
951
|
|
|
2476
952
|
private getTermSquareRoot(polynomial: Polynomial, position: number) {
|
|
2477
|
-
|
|
953
|
+
const monomial = polynomial.ms[position];
|
|
954
|
+
if (monomial.degrees.some((degree) => degree % 2 === 1)) return null;
|
|
2478
955
|
return getExactSquareRoot(polynomial.as[position]);
|
|
2479
956
|
}
|
|
2480
957
|
|
|
2481
|
-
sqrt(polynomial: Polynomial
|
|
2482
|
-
if (polynomial.size === 0)
|
|
2483
|
-
|
|
2484
|
-
return
|
|
2485
|
-
}
|
|
2486
|
-
if (this.getTermSquareRoot(polynomial, polynomial.getGradedLexMinPos()) === null) return false;
|
|
958
|
+
sqrt(polynomial: Polynomial) {
|
|
959
|
+
if (polynomial.size === 0) return polynomial;
|
|
960
|
+
if (this.getTermSquareRoot(polynomial, this.getGradedLexMinPos(polynomial)) === null)
|
|
961
|
+
return null;
|
|
2487
962
|
const maxPos = polynomial.getGradedLexMaxPos();
|
|
2488
963
|
const a = this.getTermSquareRoot(polynomial, maxPos);
|
|
2489
|
-
if (a === null) return
|
|
2490
|
-
const
|
|
964
|
+
if (a === null) return null;
|
|
965
|
+
const maxMonomial = polynomial.ms[maxPos];
|
|
966
|
+
const m1 = this.mkMonomial(
|
|
967
|
+
maxMonomial.vars,
|
|
968
|
+
maxMonomial.degrees.map((degree) => degree / 2),
|
|
969
|
+
);
|
|
2491
970
|
const root = this.som;
|
|
2492
971
|
const rest = this.som2;
|
|
2493
972
|
root.reset();
|
|
2494
973
|
rest.reset();
|
|
2495
|
-
const twoA =
|
|
974
|
+
const twoA = 2n * a;
|
|
2496
975
|
root.addTerm(a, m1);
|
|
2497
976
|
for (let index = 0; index < polynomial.size; index++)
|
|
2498
977
|
if (index !== maxPos) rest.addTerm(polynomial.as[index], polynomial.ms[index]);
|
|
2499
978
|
while (true) {
|
|
2500
|
-
this.limit.checkpoint();
|
|
2501
979
|
const restMax = rest.getGradedLexMaxPos();
|
|
2502
980
|
if (restMax === -1) {
|
|
2503
|
-
r.set(root.mk());
|
|
2504
981
|
rest.reset();
|
|
2505
|
-
return
|
|
982
|
+
return root.mk();
|
|
2506
983
|
}
|
|
2507
984
|
const monomial = this.divMonomials(rest.ms[restMax], m1);
|
|
2508
|
-
if (monomial === null ||
|
|
2509
|
-
rest.reset();
|
|
985
|
+
if (monomial === null || rest.as[restMax] % twoA !== 0n) {
|
|
2510
986
|
root.reset();
|
|
2511
|
-
|
|
987
|
+
rest.reset();
|
|
988
|
+
return null;
|
|
2512
989
|
}
|
|
2513
|
-
const coeff =
|
|
990
|
+
const coeff = floorDiv(rest.as[restMax], twoA);
|
|
2514
991
|
const rootSize = root.as.length;
|
|
2515
992
|
for (let index = 0; index < rootSize; index++) {
|
|
2516
993
|
if (root.as[index] === 0n) continue;
|
|
@@ -2521,6 +998,13 @@ export class PolynomialManager {
|
|
|
2521
998
|
}
|
|
2522
999
|
}
|
|
2523
1000
|
|
|
1001
|
+
private getGradedLexMinPos(polynomial: Polynomial) {
|
|
1002
|
+
let minPos = 0;
|
|
1003
|
+
for (let index = 1; index < polynomial.size; index++)
|
|
1004
|
+
if (gradedLexCompare(polynomial.ms[index], polynomial.ms[minPos]) < 0) minPos = index;
|
|
1005
|
+
return minPos;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
2524
1008
|
getVars(polynomial: Polynomial) {
|
|
2525
1009
|
const vars: number[] = [];
|
|
2526
1010
|
for (const monomial of polynomial.ms)
|
|
@@ -2528,22 +1012,11 @@ export class PolynomialManager {
|
|
|
2528
1012
|
return vars;
|
|
2529
1013
|
}
|
|
2530
1014
|
|
|
2531
|
-
private getMaxDegrees(polynomial: Polynomial) {
|
|
2532
|
-
const degrees = new Map<number, number>();
|
|
2533
|
-
for (const monomial of polynomial.ms)
|
|
2534
|
-
for (let index = 0; index < monomial.size; index++) {
|
|
2535
|
-
const variable = monomial.vars[index];
|
|
2536
|
-
if (monomial.degrees[index] > (degrees.get(variable) ?? 0))
|
|
2537
|
-
degrees.set(variable, monomial.degrees[index]);
|
|
2538
|
-
}
|
|
2539
|
-
return degrees;
|
|
2540
|
-
}
|
|
2541
|
-
|
|
2542
1015
|
substitute(polynomial: Polynomial, vars: readonly number[], values: readonly bigint[]) {
|
|
2543
1016
|
this.som.reset();
|
|
2544
1017
|
for (let index = 0; index < polynomial.size; index++) {
|
|
2545
1018
|
const monomial = polynomial.ms[index];
|
|
2546
|
-
let coeff =
|
|
1019
|
+
let coeff = polynomial.as[index];
|
|
2547
1020
|
const keptVars: number[] = [];
|
|
2548
1021
|
const keptDegrees: number[] = [];
|
|
2549
1022
|
for (let power = 0; power < monomial.size; power++) {
|
|
@@ -2551,11 +1024,7 @@ export class PolynomialManager {
|
|
|
2551
1024
|
if (position === -1) {
|
|
2552
1025
|
keptVars.push(monomial.vars[power]);
|
|
2553
1026
|
keptDegrees.push(monomial.degrees[power]);
|
|
2554
|
-
} else
|
|
2555
|
-
coeff = this.mulNumeral(
|
|
2556
|
-
this.powerNumeral(values[position], monomial.degrees[power]),
|
|
2557
|
-
coeff,
|
|
2558
|
-
);
|
|
1027
|
+
} else coeff *= values[position] ** BigInt(monomial.degrees[power]);
|
|
2559
1028
|
}
|
|
2560
1029
|
this.som.addTerm(coeff, this.mkMonomial(keptVars, keptDegrees));
|
|
2561
1030
|
}
|
|
@@ -2573,11 +1042,9 @@ export class PolynomialManager {
|
|
|
2573
1042
|
this.divX(monomial, variable),
|
|
2574
1043
|
);
|
|
2575
1044
|
}
|
|
2576
|
-
for (let step = 1; step <= degree; step++)
|
|
2577
|
-
this.limit.checkpoint();
|
|
1045
|
+
for (let step = 1; step <= degree; step++)
|
|
2578
1046
|
for (let power = degree - step; power <= degree - 1; power++)
|
|
2579
1047
|
coeffs[power].addmulBuffer(value, this.unit, coeffs[power + 1]);
|
|
2580
|
-
}
|
|
2581
1048
|
this.som.reset();
|
|
2582
1049
|
for (let power = 0; power <= degree; power++)
|
|
2583
1050
|
this.som.addmulBuffer(1n, this.mkPower(variable, power), coeffs[power]);
|
|
@@ -2612,539 +1079,377 @@ export class PolynomialManager {
|
|
|
2612
1079
|
if (value !== undefined && monomial.degreeOf(variable) === 0)
|
|
2613
1080
|
coeff *= value.den ** BigInt(maxDegree);
|
|
2614
1081
|
}
|
|
2615
|
-
|
|
1082
|
+
this.som.addTerm(coeff, this.mkMonomial(keptVars, keptDegrees));
|
|
2616
1083
|
}
|
|
2617
1084
|
return this.som.mk(true);
|
|
2618
1085
|
}
|
|
2619
1086
|
|
|
2620
|
-
private
|
|
2621
|
-
if (
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
this.
|
|
2626
|
-
|
|
2627
|
-
this.cheap.add(this.mulNumeral(polynomial.as[index], inverse), polynomial.ms[index]);
|
|
2628
|
-
return this.cheap.mk();
|
|
2629
|
-
}
|
|
2630
|
-
|
|
2631
|
-
private getMinDegreeVar(polynomial: Polynomial) {
|
|
2632
|
-
let result = NULL_VAR;
|
|
2633
|
-
let minDegree = Infinity;
|
|
2634
|
-
for (const [variable, degree] of this.getMaxDegrees(polynomial))
|
|
2635
|
-
if (degree < minDegree) {
|
|
2636
|
-
result = variable;
|
|
2637
|
-
minDegree = degree;
|
|
2638
|
-
}
|
|
2639
|
-
return result;
|
|
1087
|
+
private muladd(first: Polynomial, second: Polynomial, constant: bigint) {
|
|
1088
|
+
if (first.size === 0 || second.size === 0) return this.mkConst(constant);
|
|
1089
|
+
this.som.reset();
|
|
1090
|
+
for (let index = 0; index < first.size; index++)
|
|
1091
|
+
this.som.addmul(first.as[index], first.ms[index], second);
|
|
1092
|
+
this.som.addTerm(constant, this.unit);
|
|
1093
|
+
return this.som.mk();
|
|
2640
1094
|
}
|
|
2641
1095
|
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
const
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
this.decRef(normalized);
|
|
2648
|
-
}
|
|
2649
|
-
const coeffs = Array.from({ length: this.totalDegree(polynomial) + 1 }, () => 0n);
|
|
1096
|
+
private compose(polynomial: Polynomial, inner: Polynomial) {
|
|
1097
|
+
if (this.isConst(polynomial)) return polynomial;
|
|
1098
|
+
const variable = this.maxVar(polynomial);
|
|
1099
|
+
const degree = this.degree(polynomial, variable);
|
|
1100
|
+
const coeffs = Array.from({ length: degree + 1 }, () => 0n);
|
|
2650
1101
|
for (let index = 0; index < polynomial.size; index++)
|
|
2651
|
-
coeffs[polynomial.ms[index].
|
|
2652
|
-
|
|
1102
|
+
coeffs[polynomial.ms[index].degreeOf(variable)] = polynomial.as[index];
|
|
1103
|
+
let result = this.mkConst(coeffs[degree]);
|
|
1104
|
+
for (let power = degree - 1; power >= 0; power--)
|
|
1105
|
+
result = this.muladd(inner, result, coeffs[power]);
|
|
1106
|
+
return result;
|
|
2653
1107
|
}
|
|
2654
1108
|
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
}
|
|
2660
|
-
this.factorCore(polynomial, r);
|
|
1109
|
+
composeXMinusY(polynomial: Polynomial, variable: number) {
|
|
1110
|
+
const x = this.maxVar(polynomial);
|
|
1111
|
+
if (variable === x) return this.coeff(polynomial, x, 0);
|
|
1112
|
+
return this.compose(polynomial, this.mkLinear([1n, -1n], [x, variable], 0n));
|
|
2661
1113
|
}
|
|
2662
1114
|
|
|
2663
|
-
|
|
2664
|
-
if (this.isConst(polynomial))
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
this.gcd(a.get(), b.get(), d);
|
|
2688
|
-
cPart.set(this.exactDiv(a.get(), d.get()));
|
|
2689
|
-
if (!this.isConst(cPart.get())) this.factorSqfPp(cPart.get(), r, x, j);
|
|
2690
|
-
else if (cPart.get().as[0] === -1n && j % 2 === 1) r.constant = -r.constant;
|
|
2691
|
-
b.set(this.exactDiv(b.get(), d.get()));
|
|
2692
|
-
a.copy(d);
|
|
2693
|
-
}
|
|
1115
|
+
composeXDivY(polynomial: Polynomial, variable: number) {
|
|
1116
|
+
if (this.isConst(polynomial)) return polynomial;
|
|
1117
|
+
const x = this.maxVar(polynomial);
|
|
1118
|
+
const degree = this.degree(polynomial, x);
|
|
1119
|
+
for (let index = 0; index < polynomial.size; index++) {
|
|
1120
|
+
const power = polynomial.ms[index].degreeOf(x);
|
|
1121
|
+
const powers: [number, number][] =
|
|
1122
|
+
x < variable
|
|
1123
|
+
? [
|
|
1124
|
+
[x, power],
|
|
1125
|
+
[variable, degree - power],
|
|
1126
|
+
]
|
|
1127
|
+
: [
|
|
1128
|
+
[variable, degree - power],
|
|
1129
|
+
[x, power],
|
|
1130
|
+
];
|
|
1131
|
+
const kept = powers.filter((entry) => entry[1] > 0);
|
|
1132
|
+
this.cheap.add(
|
|
1133
|
+
polynomial.as[index],
|
|
1134
|
+
this.mkMonomial(
|
|
1135
|
+
kept.map((entry) => entry[0]),
|
|
1136
|
+
kept.map((entry) => entry[1]),
|
|
1137
|
+
),
|
|
1138
|
+
);
|
|
2694
1139
|
}
|
|
2695
|
-
|
|
2696
|
-
a.reset();
|
|
2697
|
-
b.reset();
|
|
2698
|
-
cPrime.reset();
|
|
2699
|
-
cPart.reset();
|
|
2700
|
-
pp.reset();
|
|
2701
|
-
c.reset();
|
|
1140
|
+
return this.cheap.mk();
|
|
2702
1141
|
}
|
|
2703
1142
|
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
if (
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
f2.set(this.add(f2.get(), discSqrt.get()));
|
|
2744
|
-
f1.move(this.pp(f1.get(), x));
|
|
2745
|
-
f2.move(this.pp(f2.get(), x));
|
|
2746
|
-
r.push(f1.get(), k);
|
|
2747
|
-
r.push(f2.get(), k);
|
|
2748
|
-
}
|
|
2749
|
-
twoAx.reset();
|
|
2750
|
-
f2.reset();
|
|
2751
|
-
f1.reset();
|
|
2752
|
-
discSqrt.reset();
|
|
2753
|
-
disc.reset();
|
|
2754
|
-
ac.reset();
|
|
2755
|
-
b2.reset();
|
|
2756
|
-
c.reset();
|
|
2757
|
-
b.reset();
|
|
2758
|
-
a.reset();
|
|
2759
|
-
}
|
|
2760
|
-
|
|
2761
|
-
private factorSqfPpUniv(polynomial: Polynomial, r: Factors, k: number) {
|
|
2762
|
-
const x = this.maxVar(polynomial);
|
|
2763
|
-
const pRef = new PolynomialRef(this);
|
|
2764
|
-
pRef.set(polynomial);
|
|
2765
|
-
const fs = mkFactors();
|
|
2766
|
-
factorSquareFree(this.um, this.toCoeffs(polynomial), fs, 1, DEFAULT_FACTOR_PARAMS);
|
|
2767
|
-
if (fs.factors.length === 1 && fs.degrees[0] === 1) r.push(polynomial, k);
|
|
2768
|
-
else {
|
|
2769
|
-
const f = new PolynomialRef(this);
|
|
2770
|
-
for (let index = 0; index < fs.factors.length; index++) {
|
|
2771
|
-
f.set(this.toPolynomial(fs.factors[index], x));
|
|
2772
|
-
r.push(f.get(), k * fs.degrees[index]);
|
|
1143
|
+
resultant(first: Polynomial, second: Polynomial, variable: number) {
|
|
1144
|
+
if (first.size === 0 || second.size === 0) return this.zero;
|
|
1145
|
+
if (this.isConst(first))
|
|
1146
|
+
return this.isConst(second) ? this.one : this.pw(first, this.degree(second, variable));
|
|
1147
|
+
if (this.isConst(second)) return this.pw(second, this.degree(first, variable));
|
|
1148
|
+
const [integerA, contentA, ppA] = this.iccp(first, variable);
|
|
1149
|
+
const [integerB, contentB, ppB] = this.iccp(second, variable);
|
|
1150
|
+
const scale = this.mul(
|
|
1151
|
+
this.pw(this.mulConst(integerA, contentA), this.degree(second, variable)),
|
|
1152
|
+
this.pw(this.mulConst(integerB, contentB), this.degree(first, variable)),
|
|
1153
|
+
);
|
|
1154
|
+
let a = ppA;
|
|
1155
|
+
let b = ppB;
|
|
1156
|
+
let sign = 1;
|
|
1157
|
+
if (this.degree(a, variable) < this.degree(b, variable)) {
|
|
1158
|
+
if (this.degree(a, variable) % 2 === 1 && this.degree(b, variable) % 2 === 1) sign = -1;
|
|
1159
|
+
[a, b] = [b, a];
|
|
1160
|
+
}
|
|
1161
|
+
let g = this.one;
|
|
1162
|
+
let h = this.one;
|
|
1163
|
+
while (true) {
|
|
1164
|
+
const degreeA = this.degree(a, variable);
|
|
1165
|
+
const degreeB = this.degree(b, variable);
|
|
1166
|
+
const delta = degreeA - degreeB;
|
|
1167
|
+
if (degreeA % 2 === 1 && degreeB % 2 === 1) sign = -sign;
|
|
1168
|
+
const remainder = this.exactPseudoRemainder(a, b, variable);
|
|
1169
|
+
a = b;
|
|
1170
|
+
b = this.exactDiv(remainder, g);
|
|
1171
|
+
for (let index = 0; index < delta; index++) b = this.exactDiv(b, h);
|
|
1172
|
+
g = this.lc(a, variable);
|
|
1173
|
+
let nextH = this.pw(g, delta);
|
|
1174
|
+
for (let index = 1; index < delta; index++) nextH = this.exactDiv(nextH, h);
|
|
1175
|
+
h = nextH;
|
|
1176
|
+
if (this.degree(b, variable) === 0) {
|
|
1177
|
+
const degree = this.degree(a, variable);
|
|
1178
|
+
let lastH = this.pw(this.lc(b, variable), degree);
|
|
1179
|
+
for (let index = 1; index < degree; index++) lastH = this.exactDiv(lastH, h);
|
|
1180
|
+
const result = this.mul(scale, lastH);
|
|
1181
|
+
return sign < 0 ? this.neg(result) : result;
|
|
2773
1182
|
}
|
|
2774
|
-
if (fs.constant === -1n && k % 2 === 1) r.constant = -r.constant;
|
|
2775
|
-
f.reset();
|
|
2776
1183
|
}
|
|
2777
|
-
pRef.reset();
|
|
2778
1184
|
}
|
|
2779
1185
|
|
|
2780
|
-
private
|
|
2781
|
-
if (this.
|
|
2782
|
-
|
|
2783
|
-
return;
|
|
2784
|
-
}
|
|
2785
|
-
if (this.tryMultivarFactor(polynomial, r, x, k)) return;
|
|
2786
|
-
r.push(polynomial, k);
|
|
1186
|
+
private isSquareFreeIn(polynomial: Polynomial, variable: number) {
|
|
1187
|
+
if (this.isConst(polynomial)) return polynomial.size > 0;
|
|
1188
|
+
return this.isConst(this.gcd(polynomial, this.derivative(polynomial, variable)));
|
|
2787
1189
|
}
|
|
2788
1190
|
|
|
2789
|
-
private
|
|
2790
|
-
|
|
2791
|
-
if (d === 1) r.push(part.get(), k);
|
|
2792
|
-
else if (d === 2 && this.isPrimitive(part.get(), x) && this.isSquareFreeIn(part.get(), x))
|
|
2793
|
-
this.factor2SqfPp(part.get(), r, x, k);
|
|
2794
|
-
else r.push(part.get(), k);
|
|
1191
|
+
private isPrimitive(polynomial: Polynomial, variable: number) {
|
|
1192
|
+
return this.eq(polynomial, this.pp(polynomial, variable));
|
|
2795
1193
|
}
|
|
2796
1194
|
|
|
2797
|
-
private
|
|
1195
|
+
private factorMultivariate(polynomial: Polynomial, x: number, k: number, result: Factors) {
|
|
2798
1196
|
const allVars = this.getVars(polynomial);
|
|
2799
|
-
const mainVars = [
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
1197
|
+
const mainVars = [
|
|
1198
|
+
x,
|
|
1199
|
+
...allVars
|
|
1200
|
+
.filter((variable) => variable !== x)
|
|
1201
|
+
.map((variable) => [this.degree(polynomial, variable), variable])
|
|
1202
|
+
.sort((first, second) => second[0] - first[0] || second[1] - first[1])
|
|
1203
|
+
.filter((entry) => entry[0] > 1)
|
|
1204
|
+
.map((entry) => entry[1]),
|
|
1205
|
+
];
|
|
2805
1206
|
for (const mainVar of mainVars) {
|
|
2806
1207
|
const degMain = this.degree(polynomial, mainVar);
|
|
2807
1208
|
if (degMain <= 1) continue;
|
|
2808
1209
|
for (const liftVar of allVars) {
|
|
2809
1210
|
if (liftVar === mainVar) continue;
|
|
2810
|
-
|
|
2811
|
-
const evalVars
|
|
2812
|
-
for (const variable of allVars)
|
|
2813
|
-
if (variable !== mainVar && variable !== liftVar) evalVars.push(variable);
|
|
2814
|
-
const nEval = evalVars.length;
|
|
2815
|
-
const maxCombos = nEval === 0 ? 1 : Math.min(FACTOR_EVAL_VALUES.length, 5);
|
|
1211
|
+
const evalVars = allVars.filter((variable) => variable !== mainVar && variable !== liftVar);
|
|
1212
|
+
const maxCombos = evalVars.length === 0 ? 1 : 5;
|
|
2816
1213
|
for (let combo = 0; combo < maxCombos; combo++) {
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
1214
|
+
const bivariate =
|
|
1215
|
+
evalVars.length === 0
|
|
1216
|
+
? polynomial
|
|
1217
|
+
: this.substitute(
|
|
1218
|
+
polynomial,
|
|
1219
|
+
evalVars,
|
|
1220
|
+
evalVars.map(
|
|
1221
|
+
(_variable, index) =>
|
|
1222
|
+
FACTOR_EVAL_VALUES[
|
|
1223
|
+
Math.floor(combo / FACTOR_EVAL_VALUES.length ** index) %
|
|
1224
|
+
FACTOR_EVAL_VALUES.length
|
|
1225
|
+
],
|
|
1226
|
+
),
|
|
1227
|
+
);
|
|
1228
|
+
if (this.degree(bivariate, mainVar) !== degMain) continue;
|
|
1229
|
+
const degLift = this.degree(bivariate, liftVar);
|
|
1230
|
+
if (degLift === 0) continue;
|
|
1231
|
+
for (const point of FACTOR_EVAL_VALUES) {
|
|
1232
|
+
const univariate = this.substitute(bivariate, [liftVar], [point]);
|
|
1233
|
+
if (!this.isUnivariate(univariate)) continue;
|
|
1234
|
+
if (this.degree(univariate, mainVar) !== degMain) continue;
|
|
1235
|
+
if (!this.isSquareFreeIn(univariate, mainVar)) continue;
|
|
1236
|
+
const univariateFs = mkFactors();
|
|
1237
|
+
factorSquareFree(
|
|
1238
|
+
this.um,
|
|
1239
|
+
this.um.getPrimitive(this.toCoeffs(univariate)),
|
|
1240
|
+
univariateFs,
|
|
1241
|
+
1,
|
|
1242
|
+
DEFAULT_FACTOR_PARAMS,
|
|
1243
|
+
);
|
|
1244
|
+
const factorCount = univariateFs.factors.length;
|
|
1245
|
+
if (factorCount <= 1) continue;
|
|
1246
|
+
const translated = this.translate(bivariate, liftVar, point);
|
|
1247
|
+
const lcPolynomial = this.coeff(translated, mainVar, degMain);
|
|
1248
|
+
const lcAtZero = this.isConst(lcPolynomial)
|
|
1249
|
+
? (lcPolynomial.as[0] ?? 0n)
|
|
1250
|
+
: this.getConstValue(this.substitute(lcPolynomial, [liftVar], [0n]));
|
|
1251
|
+
if (lcAtZero === null) continue;
|
|
1252
|
+
for (const prime of FACTOR_PRIMES) {
|
|
1253
|
+
if (getGcd(prime, lcAtZero) !== 1n) continue;
|
|
1254
|
+
for (let split = 1; split <= Math.floor(factorCount / 2); split++) {
|
|
1255
|
+
let h1 = univariateFs.factors[0];
|
|
1256
|
+
for (let index = 1; index < split; index++)
|
|
1257
|
+
h1 = this.um.mul(h1, univariateFs.factors[index]);
|
|
1258
|
+
let h2 = univariateFs.factors[split];
|
|
1259
|
+
for (let index = split + 1; index < factorCount; index++)
|
|
1260
|
+
h2 = this.um.mul(h2, univariateFs.factors[index]);
|
|
1261
|
+
const lifted = this.liftBivariate(
|
|
1262
|
+
translated,
|
|
1263
|
+
mainVar,
|
|
1264
|
+
liftVar,
|
|
1265
|
+
degLift,
|
|
1266
|
+
this.um.flipSignIfLmNeg(h1),
|
|
1267
|
+
this.um.flipSignIfLmNeg(h2),
|
|
1268
|
+
lcAtZero,
|
|
1269
|
+
prime,
|
|
1270
|
+
);
|
|
1271
|
+
if (lifted === null) continue;
|
|
1272
|
+
const candidates = lifted.map((factor) => this.translate(factor, liftVar, -point));
|
|
1273
|
+
if (evalVars.length === 0) {
|
|
1274
|
+
for (const candidate of candidates) {
|
|
1275
|
+
if (this.isConst(candidate) || this.degree(candidate, x) === 0) continue;
|
|
1276
|
+
this.pushSmallFactor(candidate, x, k, result);
|
|
1277
|
+
}
|
|
1278
|
+
return true;
|
|
1279
|
+
}
|
|
1280
|
+
for (const candidate of candidates) {
|
|
1281
|
+
if (this.isConst(candidate)) continue;
|
|
1282
|
+
const division = this.exactPseudoDivision(
|
|
1283
|
+
polynomial,
|
|
1284
|
+
candidate,
|
|
1285
|
+
this.maxVar(candidate),
|
|
1286
|
+
);
|
|
1287
|
+
if (division.remainder.size !== 0) continue;
|
|
1288
|
+
if (!this.eq(this.mul(candidate, division.quotient), polynomial)) continue;
|
|
1289
|
+
for (const part of [candidate, division.quotient]) {
|
|
1290
|
+
if (this.isConst(part)) result.constant *= part.as[0];
|
|
1291
|
+
else if (this.degree(part, x) !== 0) this.pushSmallFactor(part, x, k, result);
|
|
1292
|
+
}
|
|
1293
|
+
return true;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
2833
1298
|
}
|
|
2834
1299
|
}
|
|
2835
1300
|
}
|
|
2836
1301
|
return false;
|
|
2837
1302
|
}
|
|
2838
1303
|
|
|
2839
|
-
private
|
|
2840
|
-
polynomial
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
mainVar: number,
|
|
2845
|
-
degMain: number,
|
|
2846
|
-
liftVar: number,
|
|
2847
|
-
evalVars: readonly number[],
|
|
2848
|
-
combo: number,
|
|
2849
|
-
pBivar: PolynomialRef,
|
|
2850
|
-
) {
|
|
2851
|
-
const nEval = evalVars.length;
|
|
2852
|
-
if (nEval > 0) {
|
|
2853
|
-
const evalValues: bigint[] = [];
|
|
2854
|
-
for (let index = 0; index < nEval; index++) {
|
|
2855
|
-
let c = combo;
|
|
2856
|
-
for (let skip = 0; skip < index; skip++) c = Math.floor(c / FACTOR_EVAL_VALUES.length);
|
|
2857
|
-
evalValues.push(FACTOR_EVAL_VALUES[c % FACTOR_EVAL_VALUES.length]);
|
|
2858
|
-
}
|
|
2859
|
-
pBivar.set(this.substitute(polynomial, evalVars, evalValues));
|
|
2860
|
-
} else pBivar.set(polynomial);
|
|
2861
|
-
if (this.degree(pBivar.get(), mainVar) !== degMain) return false;
|
|
2862
|
-
const degLift = this.degree(pBivar.get(), liftVar);
|
|
2863
|
-
if (degLift === 0) return false;
|
|
2864
|
-
for (const point of FACTOR_EVAL_VALUES) {
|
|
2865
|
-
const pUniv = new PolynomialRef(this);
|
|
2866
|
-
const pUnivRef = new PolynomialRef(this);
|
|
2867
|
-
const q = new PolynomialRef(this);
|
|
2868
|
-
const lcPoly = new PolynomialRef(this);
|
|
2869
|
-
const isFound = this.tryMultivarFactorPoint(
|
|
2870
|
-
polynomial,
|
|
2871
|
-
r,
|
|
2872
|
-
x,
|
|
2873
|
-
k,
|
|
2874
|
-
mainVar,
|
|
2875
|
-
degMain,
|
|
2876
|
-
liftVar,
|
|
2877
|
-
nEval,
|
|
2878
|
-
degLift,
|
|
2879
|
-
point,
|
|
2880
|
-
pBivar,
|
|
2881
|
-
pUniv,
|
|
2882
|
-
pUnivRef,
|
|
2883
|
-
q,
|
|
2884
|
-
lcPoly,
|
|
2885
|
-
);
|
|
2886
|
-
lcPoly.reset();
|
|
2887
|
-
q.reset();
|
|
2888
|
-
pUnivRef.reset();
|
|
2889
|
-
pUniv.reset();
|
|
2890
|
-
if (isFound) return true;
|
|
2891
|
-
}
|
|
2892
|
-
return false;
|
|
2893
|
-
}
|
|
2894
|
-
|
|
2895
|
-
private tryMultivarFactorPoint(
|
|
2896
|
-
polynomial: Polynomial,
|
|
2897
|
-
r: Factors,
|
|
2898
|
-
x: number,
|
|
2899
|
-
k: number,
|
|
2900
|
-
mainVar: number,
|
|
2901
|
-
degMain: number,
|
|
2902
|
-
liftVar: number,
|
|
2903
|
-
nEval: number,
|
|
2904
|
-
degLift: number,
|
|
2905
|
-
point: bigint,
|
|
2906
|
-
pBivar: PolynomialRef,
|
|
2907
|
-
pUniv: PolynomialRef,
|
|
2908
|
-
pUnivRef: PolynomialRef,
|
|
2909
|
-
q: PolynomialRef,
|
|
2910
|
-
lcPoly: PolynomialRef,
|
|
2911
|
-
) {
|
|
2912
|
-
pUniv.set(this.substitute(pBivar.get(), [liftVar], [point]));
|
|
2913
|
-
if (!this.isUnivariate(pUniv.get())) return false;
|
|
2914
|
-
if (this.degree(pUniv.get(), mainVar) !== degMain) return false;
|
|
2915
|
-
if (!this.isSquareFreeIn(pUniv.get(), mainVar)) return false;
|
|
2916
|
-
pUnivRef.copy(pUniv);
|
|
2917
|
-
const univariateFs = mkFactors();
|
|
2918
|
-
factorSquareFree(
|
|
2919
|
-
this.um,
|
|
2920
|
-
this.um.getPrimitive(this.toCoeffs(pUnivRef.get())),
|
|
2921
|
-
univariateFs,
|
|
2922
|
-
1,
|
|
2923
|
-
DEFAULT_FACTOR_PARAMS,
|
|
2924
|
-
);
|
|
2925
|
-
const nf = univariateFs.factors.length;
|
|
2926
|
-
if (nf <= 1) return false;
|
|
2927
|
-
q.set(this.translate(pBivar.get(), liftVar, point));
|
|
2928
|
-
lcPoly.set(this.coeff(q.get(), mainVar, degMain));
|
|
2929
|
-
let lcAtZero = 0n;
|
|
2930
|
-
if (this.isConst(lcPoly.get())) lcAtZero = lcPoly.get().as[0] ?? 0n;
|
|
2931
|
-
else {
|
|
2932
|
-
const lcEval = new PolynomialRef(this);
|
|
2933
|
-
lcEval.set(this.substitute(lcPoly.get(), [liftVar], [0n]));
|
|
2934
|
-
const isConstant = this.isConst(lcEval.get());
|
|
2935
|
-
if (isConstant) lcAtZero = lcEval.get().as[0] ?? 0n;
|
|
2936
|
-
lcEval.reset();
|
|
2937
|
-
if (!isConstant) return false;
|
|
2938
|
-
}
|
|
2939
|
-
for (const prime of FACTOR_PRIMES) {
|
|
2940
|
-
if (getGcd(prime, lcAtZero) !== 1n) continue;
|
|
2941
|
-
for (let split = 1; split <= Math.floor(nf / 2); split++) {
|
|
2942
|
-
this.limit.checkpoint();
|
|
2943
|
-
let h1 = this.um.set(univariateFs.factors[0]);
|
|
2944
|
-
for (let index = 1; index < split; index++)
|
|
2945
|
-
h1 = this.um.mul(h1, univariateFs.factors[index]);
|
|
2946
|
-
let h2 = this.um.set(univariateFs.factors[split]);
|
|
2947
|
-
for (let index = split + 1; index < nf; index++)
|
|
2948
|
-
h2 = this.um.mul(h2, univariateFs.factors[index]);
|
|
2949
|
-
if (h1.length > 0 && h1[h1.length - 1] < 0n) h1 = h1.map((coeff) => -coeff);
|
|
2950
|
-
if (h2.length > 0 && h2[h2.length - 1] < 0n) h2 = h2.map((coeff) => -coeff);
|
|
2951
|
-
const f1 = new PolynomialRef(this);
|
|
2952
|
-
const f2 = new PolynomialRef(this);
|
|
2953
|
-
const isFound =
|
|
2954
|
-
this.tryBivarHenselLift(
|
|
2955
|
-
q.get(),
|
|
2956
|
-
mainVar,
|
|
2957
|
-
liftVar,
|
|
2958
|
-
degLift,
|
|
2959
|
-
h1,
|
|
2960
|
-
h2,
|
|
2961
|
-
lcAtZero,
|
|
2962
|
-
prime,
|
|
2963
|
-
f1,
|
|
2964
|
-
f2,
|
|
2965
|
-
) && this.pushLiftedFactors(polynomial, r, x, k, nEval, liftVar, point, f1, f2);
|
|
2966
|
-
f2.reset();
|
|
2967
|
-
f1.reset();
|
|
2968
|
-
if (isFound) return true;
|
|
2969
|
-
}
|
|
2970
|
-
}
|
|
2971
|
-
return false;
|
|
1304
|
+
private pushSmallFactor(polynomial: Polynomial, x: number, k: number, result: Factors) {
|
|
1305
|
+
const degree = this.degree(polynomial, x);
|
|
1306
|
+
if (degree === 2 && this.isPrimitive(polynomial, x) && this.isSquareFreeIn(polynomial, x))
|
|
1307
|
+
this.factorQuadratic(polynomial, x, k, result);
|
|
1308
|
+
else this.pushFactor(result, polynomial, k);
|
|
2972
1309
|
}
|
|
2973
1310
|
|
|
2974
|
-
private
|
|
2975
|
-
polynomial
|
|
2976
|
-
|
|
2977
|
-
x: number,
|
|
2978
|
-
k: number,
|
|
2979
|
-
nEval: number,
|
|
2980
|
-
liftVar: number,
|
|
2981
|
-
point: bigint,
|
|
2982
|
-
f1: PolynomialRef,
|
|
2983
|
-
f2: PolynomialRef,
|
|
2984
|
-
) {
|
|
2985
|
-
f1.set(this.translate(f1.get(), liftVar, -point));
|
|
2986
|
-
f2.set(this.translate(f2.get(), liftVar, -point));
|
|
2987
|
-
if (nEval === 0) {
|
|
2988
|
-
const bivarFs = [new PolynomialRef(this), new PolynomialRef(this)];
|
|
2989
|
-
bivarFs[0].copy(f1);
|
|
2990
|
-
bivarFs[1].copy(f2);
|
|
2991
|
-
for (const bf of bivarFs) {
|
|
2992
|
-
if (this.isConst(bf.get()) || this.degree(bf.get(), x) === 0) continue;
|
|
2993
|
-
this.pushSmallFactor(bf, r, x, k);
|
|
2994
|
-
}
|
|
2995
|
-
bivarFs[1].reset();
|
|
2996
|
-
bivarFs[0].reset();
|
|
2997
|
-
return true;
|
|
2998
|
-
}
|
|
2999
|
-
const cands = [new PolynomialRef(this), new PolynomialRef(this)];
|
|
3000
|
-
cands[0].copy(f1);
|
|
3001
|
-
cands[1].copy(f2);
|
|
3002
|
-
let isFound = false;
|
|
3003
|
-
for (const cand of cands) {
|
|
3004
|
-
if (this.isConst(cand.get())) continue;
|
|
3005
|
-
const qDiv = new PolynomialRef(this);
|
|
3006
|
-
const rDiv = new PolynomialRef(this);
|
|
3007
|
-
const divVar = this.maxVar(cand.get());
|
|
3008
|
-
this.exactPseudoDivision(polynomial, cand.get(), divVar, qDiv, rDiv);
|
|
3009
|
-
if (!this.isZero(rDiv.get())) {
|
|
3010
|
-
rDiv.reset();
|
|
3011
|
-
qDiv.reset();
|
|
3012
|
-
continue;
|
|
3013
|
-
}
|
|
3014
|
-
const check = new PolynomialRef(this);
|
|
3015
|
-
check.set(this.mul(cand.get(), qDiv.get()));
|
|
3016
|
-
if (this.eq(check.get(), polynomial)) {
|
|
3017
|
-
const parts = [new PolynomialRef(this), new PolynomialRef(this)];
|
|
3018
|
-
parts[0].copy(cand);
|
|
3019
|
-
parts[1].copy(qDiv);
|
|
3020
|
-
for (const part of parts) {
|
|
3021
|
-
if (this.isConst(part.get())) {
|
|
3022
|
-
r.constant *= part.get().as[0];
|
|
3023
|
-
continue;
|
|
3024
|
-
}
|
|
3025
|
-
if (this.degree(part.get(), x) === 0) continue;
|
|
3026
|
-
this.pushSmallFactor(part, r, x, k);
|
|
3027
|
-
}
|
|
3028
|
-
parts[1].reset();
|
|
3029
|
-
parts[0].reset();
|
|
3030
|
-
isFound = true;
|
|
3031
|
-
}
|
|
3032
|
-
check.reset();
|
|
3033
|
-
rDiv.reset();
|
|
3034
|
-
qDiv.reset();
|
|
3035
|
-
if (isFound) break;
|
|
3036
|
-
}
|
|
3037
|
-
cands[1].reset();
|
|
3038
|
-
cands[0].reset();
|
|
3039
|
-
return isFound;
|
|
1311
|
+
private getConstValue(polynomial: Polynomial) {
|
|
1312
|
+
if (!this.isConst(polynomial)) return null;
|
|
1313
|
+
return polynomial.as[0] ?? 0n;
|
|
3040
1314
|
}
|
|
3041
1315
|
|
|
3042
|
-
private
|
|
1316
|
+
private liftBivariate(
|
|
3043
1317
|
q: Polynomial,
|
|
3044
1318
|
x: number,
|
|
3045
1319
|
y: number,
|
|
3046
1320
|
degY: number,
|
|
3047
1321
|
h1: readonly bigint[],
|
|
3048
1322
|
h2: readonly bigint[],
|
|
3049
|
-
|
|
1323
|
+
lc: bigint,
|
|
3050
1324
|
prime: bigint,
|
|
3051
|
-
f1Out: PolynomialRef,
|
|
3052
|
-
f2Out: PolynomialRef,
|
|
3053
1325
|
) {
|
|
3054
|
-
const zp = new UpolynomialManager(
|
|
1326
|
+
const zp = new UpolynomialManager(prime);
|
|
3055
1327
|
const f1 = zp.mkMonic(zp.set(h1));
|
|
3056
1328
|
const f2 = zp.mkMonic(zp.set(h2));
|
|
3057
1329
|
const ext = zp.extGcd(f1, f2);
|
|
3058
|
-
if (ext.d.length !== 1 || ext.d[0] !== 1n) return
|
|
1330
|
+
if (ext.d.length !== 1 || ext.d[0] !== 1n) return null;
|
|
3059
1331
|
const qCoeffs: bigint[][] = [];
|
|
3060
|
-
for (let
|
|
3061
|
-
const
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
if (isBivariate) qCoeffs.push(zp.set(this.isZero(cj.get()) ? [] : this.toCoeffs(cj.get())));
|
|
3065
|
-
cj.reset();
|
|
3066
|
-
if (!isBivariate) return false;
|
|
1332
|
+
for (let power = 0; power <= degY; power++) {
|
|
1333
|
+
const coeff = this.coeff(q, y, power);
|
|
1334
|
+
if (coeff.size !== 0 && !this.isUnivariate(coeff) && !this.isConst(coeff)) return null;
|
|
1335
|
+
qCoeffs.push(zp.set(coeff.size === 0 ? [] : this.toCoeffs(coeff)));
|
|
3067
1336
|
}
|
|
3068
1337
|
const f1Coeffs: bigint[][] = Array.from({ length: degY + 1 }, () => []);
|
|
3069
1338
|
const f2Coeffs: bigint[][] = Array.from({ length: degY + 1 }, () => []);
|
|
3070
1339
|
f1Coeffs[0] = zp.set(f1);
|
|
3071
|
-
f2Coeffs[0] = zp.mulConst(
|
|
3072
|
-
const lcInv = zp.inv(
|
|
3073
|
-
for (let
|
|
3074
|
-
|
|
3075
|
-
let
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
if (second > degY) continue;
|
|
3079
|
-
if (first === j && second === 0) continue;
|
|
3080
|
-
if (first === 0 && second === j) continue;
|
|
1340
|
+
f2Coeffs[0] = zp.mulConst(f2, zp.normalizeNumeral(lc));
|
|
1341
|
+
const lcInv = zp.inv(lc);
|
|
1342
|
+
for (let power = 1; power <= degY; power++) {
|
|
1343
|
+
let error = zp.set(qCoeffs[power]);
|
|
1344
|
+
for (let first = 0; first <= power; first++) {
|
|
1345
|
+
const second = power - first;
|
|
1346
|
+
if (second > degY || second === 0 || first === 0) continue;
|
|
3081
1347
|
if (f1Coeffs[first].length === 0 || f2Coeffs[second].length === 0) continue;
|
|
3082
|
-
|
|
1348
|
+
error = zp.sub(error, zp.mul(f1Coeffs[first], f2Coeffs[second]));
|
|
3083
1349
|
}
|
|
3084
|
-
if (
|
|
3085
|
-
const a = zp.divRem(zp.mul(
|
|
3086
|
-
const b = zp.divRem(zp.mul(
|
|
3087
|
-
f1Coeffs[
|
|
3088
|
-
f2Coeffs[
|
|
1350
|
+
if (error.length === 0) continue;
|
|
1351
|
+
const a = zp.divRem(zp.mul(error, ext.v), f1).remainder;
|
|
1352
|
+
const b = zp.divRem(zp.mul(error, ext.u), f2).remainder;
|
|
1353
|
+
f1Coeffs[power] = zp.mulConst(a, lcInv);
|
|
1354
|
+
f2Coeffs[power] = zp.set(b);
|
|
1355
|
+
}
|
|
1356
|
+
const toPolynomial = (coeffs: readonly bigint[][]) => {
|
|
1357
|
+
let result = this.zero;
|
|
1358
|
+
for (let power = 0; power <= degY; power++) {
|
|
1359
|
+
if (coeffs[power].length === 0) continue;
|
|
1360
|
+
const univariate = this.fromCoeffs(coeffs[power], x);
|
|
1361
|
+
if (univariate.size === 0) continue;
|
|
1362
|
+
result = this.add(result, this.mulTerm(1n, this.mkPower(y, power), univariate));
|
|
1363
|
+
}
|
|
1364
|
+
return result;
|
|
1365
|
+
};
|
|
1366
|
+
const first = toPolynomial(f1Coeffs);
|
|
1367
|
+
const second = toPolynomial(f2Coeffs);
|
|
1368
|
+
return this.eq(this.mul(first, second), q) ? [first, second] : null;
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
pscChain(first: Polynomial, second: Polynomial, variable: number) {
|
|
1372
|
+
const chain =
|
|
1373
|
+
this.degree(first, variable) >= this.degree(second, variable)
|
|
1374
|
+
? this.pscChainCore(first, second, variable)
|
|
1375
|
+
: this.pscChainCore(second, first, variable);
|
|
1376
|
+
return chain.length === 0 ? [this.zero] : chain.reverse();
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
private pscChainCore(p: Polynomial, q: Polynomial, x: number) {
|
|
1380
|
+
const chain: Polynomial[] = [];
|
|
1381
|
+
let s = this.pw(this.lc(q, x), this.degree(p, x) - this.degree(q, x));
|
|
1382
|
+
let a = q;
|
|
1383
|
+
let b = this.exactPseudoRemainder(p, this.neg(q), x);
|
|
1384
|
+
while (b.size > 0) {
|
|
1385
|
+
const d = this.degree(a, x);
|
|
1386
|
+
const e = this.degree(b, x);
|
|
1387
|
+
const principal = this.coeff(b, x, d - 1);
|
|
1388
|
+
if (principal.size > 0) chain.push(principal);
|
|
1389
|
+
let c = b;
|
|
1390
|
+
if (d - e > 1) {
|
|
1391
|
+
c = this.getSeLazard(d, s, b, x);
|
|
1392
|
+
const principalE = this.coeff(c, x, e);
|
|
1393
|
+
if (principalE.size > 0) chain.push(principalE);
|
|
1394
|
+
}
|
|
1395
|
+
if (e === 0) break;
|
|
1396
|
+
b = this.getOptimizedSe1(d, e, a, b, c, s, x);
|
|
1397
|
+
a = c;
|
|
1398
|
+
s = this.lc(a, x);
|
|
3089
1399
|
}
|
|
3090
|
-
return
|
|
1400
|
+
return chain;
|
|
3091
1401
|
}
|
|
3092
1402
|
|
|
3093
|
-
private
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
f2Poly.set(this.zero);
|
|
3107
|
-
const addTerm = (coeffs: readonly bigint[], power: number, target: PolynomialRef) => {
|
|
3108
|
-
if (coeffs.length === 0) return;
|
|
3109
|
-
const univ = new PolynomialRef(this);
|
|
3110
|
-
univ.set(this.toPolynomial(coeffs, x));
|
|
3111
|
-
if (!this.isZero(univ.get())) {
|
|
3112
|
-
const term = new PolynomialRef(this);
|
|
3113
|
-
term.set(this.mulTerm(1n, this.mkPower(y, power), univ.get()));
|
|
3114
|
-
target.set(this.add(target.get(), term.get()));
|
|
3115
|
-
term.reset();
|
|
1403
|
+
private getSeLazard(d: number, lcSd: Polynomial, sd1: Polynomial, x: number) {
|
|
1404
|
+
let n = d - this.degree(sd1, x) - 1;
|
|
1405
|
+
if (n === 0) return sd1;
|
|
1406
|
+
const lcSd1 = this.lc(sd1, x);
|
|
1407
|
+
let a = 1 << (31 - Math.clz32(n));
|
|
1408
|
+
let c = lcSd1;
|
|
1409
|
+
n -= a;
|
|
1410
|
+
while (a !== 1) {
|
|
1411
|
+
a /= 2;
|
|
1412
|
+
c = this.exactDiv(this.mul(c, c), lcSd);
|
|
1413
|
+
if (n >= a) {
|
|
1414
|
+
c = this.exactDiv(this.mul(c, lcSd1), lcSd);
|
|
1415
|
+
n -= a;
|
|
3116
1416
|
}
|
|
3117
|
-
univ.reset();
|
|
3118
|
-
};
|
|
3119
|
-
for (let j = 0; j <= degY; j++) {
|
|
3120
|
-
addTerm(f1Coeffs[j], j, f1Poly);
|
|
3121
|
-
addTerm(f2Coeffs[j], j, f2Poly);
|
|
3122
|
-
}
|
|
3123
|
-
const product = new PolynomialRef(this);
|
|
3124
|
-
product.set(this.mul(f1Poly.get(), f2Poly.get()));
|
|
3125
|
-
const isEqual = this.eq(product.get(), q);
|
|
3126
|
-
if (isEqual) {
|
|
3127
|
-
f1Out.copy(f1Poly);
|
|
3128
|
-
f2Out.copy(f2Poly);
|
|
3129
1417
|
}
|
|
3130
|
-
|
|
3131
|
-
f2Poly.reset();
|
|
3132
|
-
f1Poly.reset();
|
|
3133
|
-
return isEqual;
|
|
1418
|
+
return this.exactDiv(this.mul(c, sd1), lcSd);
|
|
3134
1419
|
}
|
|
3135
1420
|
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
1421
|
+
private getOptimizedSe1(
|
|
1422
|
+
d: number,
|
|
1423
|
+
e: number,
|
|
1424
|
+
a: Polynomial,
|
|
1425
|
+
sd1: Polynomial,
|
|
1426
|
+
se: Polynomial,
|
|
1427
|
+
s: Polynomial,
|
|
1428
|
+
x: number,
|
|
1429
|
+
) {
|
|
1430
|
+
const cd1 = this.lc(sd1, x);
|
|
1431
|
+
const lcSe = this.lc(se, x);
|
|
1432
|
+
const getPower = (power: number) => (power === 0 ? this.one : this.mkVar(x, power));
|
|
1433
|
+
const h: Polynomial[] = [];
|
|
1434
|
+
for (let j = 0; j < e; j++) h.push(this.mul(lcSe, getPower(j)));
|
|
1435
|
+
h.push(this.sub(this.mul(lcSe, getPower(e)), se));
|
|
1436
|
+
const xPolynomial = this.mkVar(x);
|
|
1437
|
+
for (let j = e + 1; j < d; j++) {
|
|
1438
|
+
const xh = this.mul(xPolynomial, h[j - 1]);
|
|
1439
|
+
h.push(this.sub(xh, this.exactDiv(this.mul(this.coeff(xh, x, e), sd1), cd1)));
|
|
1440
|
+
}
|
|
1441
|
+
let sum = this.zero;
|
|
1442
|
+
for (let j = 0; j < d; j++) sum = this.add(sum, this.mul(this.coeff(a, x, j), h[j]));
|
|
1443
|
+
const dPolynomial = this.exactDiv(sum, this.lc(a, x));
|
|
1444
|
+
const xh = this.mul(xPolynomial, h[d - 1]);
|
|
1445
|
+
const result = this.exactDiv(
|
|
1446
|
+
this.sub(this.mul(cd1, this.add(xh, dPolynomial)), this.mul(this.coeff(xh, x, e), sd1)),
|
|
1447
|
+
s,
|
|
1448
|
+
);
|
|
1449
|
+
return (d - e + 1) % 2 === 1 ? this.neg(result) : result;
|
|
3140
1450
|
}
|
|
3141
1451
|
|
|
3142
|
-
|
|
3143
|
-
private evaluatePartially<T>(
|
|
3144
|
-
polynomial: Polynomial,
|
|
3145
|
-
ops: ValueOps<T>,
|
|
3146
|
-
getValue: (variable: number) => T | null,
|
|
3147
|
-
) {
|
|
1452
|
+
evaluateWith<T>(polynomial: Polynomial, ops: ValueOps<T>, getValue: (variable: number) => T) {
|
|
3148
1453
|
if (polynomial.size === 0) return ops.fromInteger(0n);
|
|
3149
1454
|
if (this.isConst(polynomial)) return ops.fromInteger(polynomial.as[0]);
|
|
3150
1455
|
polynomial.lexSort();
|
|
@@ -3161,38 +1466,34 @@ export class PolynomialManager {
|
|
|
3161
1466
|
private evaluateCore<T>(
|
|
3162
1467
|
polynomial: Polynomial,
|
|
3163
1468
|
ops: ValueOps<T>,
|
|
3164
|
-
getValue: (variable: number) => T
|
|
1469
|
+
getValue: (variable: number) => T,
|
|
3165
1470
|
start: number,
|
|
3166
1471
|
end: number,
|
|
3167
1472
|
x: number,
|
|
3168
|
-
): T
|
|
1473
|
+
): T {
|
|
3169
1474
|
if (end === start + 1) {
|
|
3170
1475
|
let result = ops.fromInteger(polynomial.as[start]);
|
|
3171
1476
|
const monomial = polynomial.ms[start];
|
|
3172
1477
|
for (let index = 0; index < monomial.size; index++) {
|
|
3173
1478
|
const variable = monomial.vars[index];
|
|
3174
1479
|
if (variable > x) break;
|
|
3175
|
-
|
|
3176
|
-
if (value === null) return null;
|
|
3177
|
-
result = ops.mul(result, ops.power(value, monomial.degrees[index]));
|
|
1480
|
+
result = ops.mul(result, ops.power(getValue(variable), monomial.degrees[index]));
|
|
3178
1481
|
}
|
|
3179
1482
|
return result;
|
|
3180
1483
|
}
|
|
3181
1484
|
const value = getValue(x);
|
|
3182
|
-
if (value === null) return null;
|
|
3183
1485
|
let result = ops.fromInteger(0n);
|
|
3184
1486
|
let index = start;
|
|
3185
1487
|
while (index < end) {
|
|
3186
|
-
this.limit.checkpoint();
|
|
3187
1488
|
const degree = polynomial.ms[index].degreeOf(x);
|
|
3188
1489
|
if (degree === 0) {
|
|
3189
1490
|
const below = this.maxSmallerThan(polynomial, index, end, x);
|
|
3190
|
-
|
|
1491
|
+
result = ops.add(
|
|
1492
|
+
result,
|
|
3191
1493
|
below === NULL_VAR
|
|
3192
1494
|
? ops.fromInteger(polynomial.as[index])
|
|
3193
|
-
: this.evaluateCore(polynomial, ops, getValue, index, end, below)
|
|
3194
|
-
|
|
3195
|
-
result = ops.add(result, rest);
|
|
1495
|
+
: this.evaluateCore(polynomial, ops, getValue, index, end, below),
|
|
1496
|
+
);
|
|
3196
1497
|
break;
|
|
3197
1498
|
}
|
|
3198
1499
|
let next = index + 1;
|
|
@@ -3205,12 +1506,13 @@ export class PolynomialManager {
|
|
|
3205
1506
|
}
|
|
3206
1507
|
}
|
|
3207
1508
|
const below = this.maxSmallerThan(polynomial, index, next, x);
|
|
3208
|
-
|
|
1509
|
+
result = ops.add(
|
|
1510
|
+
result,
|
|
3209
1511
|
below === NULL_VAR
|
|
3210
1512
|
? ops.fromInteger(polynomial.as[index])
|
|
3211
|
-
: this.evaluateCore(polynomial, ops, getValue, index, next, below)
|
|
3212
|
-
|
|
3213
|
-
result = ops.mul(
|
|
1513
|
+
: this.evaluateCore(polynomial, ops, getValue, index, next, below),
|
|
1514
|
+
);
|
|
1515
|
+
result = ops.mul(result, ops.power(value, degree - nextDegree));
|
|
3214
1516
|
index = next;
|
|
3215
1517
|
}
|
|
3216
1518
|
return result;
|
|
@@ -3223,88 +1525,66 @@ export class PolynomialManager {
|
|
|
3223
1525
|
return max;
|
|
3224
1526
|
}
|
|
3225
1527
|
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
);
|
|
3232
|
-
|
|
1528
|
+
evaluate(polynomial: Polynomial, values: readonly (Rational | undefined)[]) {
|
|
1529
|
+
return this.evaluateWith(polynomial, RATIONAL_OPS, (variable) => {
|
|
1530
|
+
const value = values[variable];
|
|
1531
|
+
if (value === undefined) throw new SmtInvariantError(`x${variable} is unassigned`);
|
|
1532
|
+
return value;
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
getSign(polynomial: Polynomial, values: readonly (Rational | undefined)[]) {
|
|
1537
|
+
const value = this.evaluate(polynomial, values);
|
|
1538
|
+
return value.isZero() ? 0 : value.isPositive() ? 1 : -1;
|
|
3233
1539
|
}
|
|
3234
1540
|
}
|
|
3235
1541
|
|
|
3236
1542
|
export class PolynomialCache {
|
|
3237
1543
|
private table = new Map<string, Polynomial>();
|
|
3238
|
-
private
|
|
3239
|
-
private inCache: boolean[] = [];
|
|
1544
|
+
private inCache = new Set<number>();
|
|
3240
1545
|
private pscChains = new Map<string, Polynomial[]>();
|
|
3241
1546
|
private factorizations = new Map<number, Polynomial[]>();
|
|
3242
1547
|
|
|
3243
|
-
constructor(readonly pm: PolynomialManager) {
|
|
3244
|
-
this.cachedPolys = new PolynomialRefVector(pm);
|
|
3245
|
-
}
|
|
1548
|
+
constructor(readonly pm: PolynomialManager) {}
|
|
3246
1549
|
|
|
3247
1550
|
reset() {
|
|
3248
1551
|
this.table.clear();
|
|
1552
|
+
this.inCache.clear();
|
|
3249
1553
|
this.pscChains.clear();
|
|
3250
1554
|
this.factorizations.clear();
|
|
3251
|
-
this.inCache = [];
|
|
3252
|
-
this.cachedPolys.reset();
|
|
3253
1555
|
}
|
|
3254
1556
|
|
|
3255
1557
|
mkUnique(polynomial: Polynomial) {
|
|
3256
|
-
if (this.inCache
|
|
1558
|
+
if (this.inCache.has(polynomial.id)) return polynomial;
|
|
3257
1559
|
const key = this.pm.getKey(polynomial);
|
|
3258
1560
|
const existing = this.table.get(key);
|
|
3259
1561
|
if (existing !== undefined) return existing;
|
|
3260
1562
|
this.table.set(key, polynomial);
|
|
3261
|
-
this.
|
|
3262
|
-
this.inCache[polynomial.id] = true;
|
|
1563
|
+
this.inCache.add(polynomial.id);
|
|
3263
1564
|
return polynomial;
|
|
3264
1565
|
}
|
|
3265
1566
|
|
|
3266
1567
|
contains(polynomial: Polynomial) {
|
|
3267
|
-
return this.inCache
|
|
1568
|
+
return this.inCache.has(polynomial.id);
|
|
3268
1569
|
}
|
|
3269
1570
|
|
|
3270
|
-
pscChain(first: Polynomial, second: Polynomial, variable: number
|
|
1571
|
+
pscChain(first: Polynomial, second: Polynomial, variable: number) {
|
|
3271
1572
|
const p = this.mkUnique(first);
|
|
3272
1573
|
const q = this.mkUnique(second);
|
|
3273
1574
|
const key = `${p.id},${q.id},${variable}`;
|
|
3274
1575
|
const cached = this.pscChains.get(key);
|
|
3275
|
-
if (cached !== undefined)
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
}
|
|
3289
|
-
|
|
3290
|
-
factor(polynomial: Polynomial, distinctFactors: PolynomialRefVector) {
|
|
3291
|
-
distinctFactors.reset();
|
|
3292
|
-
const p = this.mkUnique(polynomial);
|
|
3293
|
-
const cached = this.factorizations.get(p.id);
|
|
3294
|
-
if (cached !== undefined) {
|
|
3295
|
-
distinctFactors.reset();
|
|
3296
|
-
for (const factor of cached) distinctFactors.push(factor);
|
|
3297
|
-
return;
|
|
3298
|
-
}
|
|
3299
|
-
const fs = new Factors(this.pm);
|
|
3300
|
-
this.pm.factor(p, fs);
|
|
3301
|
-
const result: Polynomial[] = [];
|
|
3302
|
-
for (const factor of fs.factors) {
|
|
3303
|
-
const unique = this.mkUnique(factor);
|
|
3304
|
-
distinctFactors.push(unique);
|
|
3305
|
-
result.push(unique);
|
|
3306
|
-
}
|
|
3307
|
-
this.factorizations.set(p.id, result);
|
|
3308
|
-
fs.reset();
|
|
1576
|
+
if (cached !== undefined) return cached;
|
|
1577
|
+
const chain = this.pm.pscChain(p, q, variable).map((element) => this.mkUnique(element));
|
|
1578
|
+
this.pscChains.set(key, chain);
|
|
1579
|
+
return chain;
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
factor(polynomial: Polynomial) {
|
|
1583
|
+
const unique = this.mkUnique(polynomial);
|
|
1584
|
+
const cached = this.factorizations.get(unique.id);
|
|
1585
|
+
if (cached !== undefined) return cached;
|
|
1586
|
+
const factors = this.pm.factor(unique).factors.map((factor) => this.mkUnique(factor));
|
|
1587
|
+
this.factorizations.set(unique.id, factors);
|
|
1588
|
+
return factors;
|
|
3309
1589
|
}
|
|
3310
1590
|
}
|