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