tla-ts 0.0.10-dev.09e53c2a6 → 0.0.10-dev.1ebc153bd
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 +54 -54
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- 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 +43 -43
- package/dist/prove.js +78 -78
- package/dist/run.cjs +33 -35
- package/dist/run.js +44 -46
- package/dist/tlapm.cjs +1 -1
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +43 -43
- package/dist/transfer.js +38 -38
- package/dist/worker.cjs +6 -6
- package/dist/worker.js +6 -6
- package/package.json +1 -1
- package/src/ast.ts +1 -0
- package/src/bags.ts +46 -28
- package/src/check-trace.ts +2 -16
- package/src/check.ts +152 -64
- package/src/cli/runtime-invariants.ts +20 -56
- package/src/cli/tlc-arguments.ts +2 -2
- package/src/cli/tlc-output.ts +1 -2
- package/src/cli/tlc.ts +13 -8
- package/src/cli/xml-exporter.ts +1 -1
- package/src/codegen.ts +9 -5
- package/src/dependencies.ts +49 -9
- package/src/dfid.ts +9 -2
- package/src/environment.ts +57 -33
- package/src/errors.ts +7 -0
- package/src/evaluate.ts +92 -32
- package/src/exit-codes.ts +4 -2
- package/src/instances.ts +5 -5
- package/src/java-overrides.ts +2 -2
- package/src/levels.ts +138 -90
- package/src/liveness-checker.ts +43 -4
- package/src/liveness.ts +1 -1
- package/src/obligations/index.ts +1 -1
- package/src/obligations/normalize.ts +1 -4
- package/src/obligations/printer.ts +5 -5
- package/src/parallel-worker.ts +8 -2
- package/src/parallel.ts +5 -0
- package/src/parser.ts +106 -144
- 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/asserted-formulas.ts +66 -1
- package/src/prover/smt/solver/ast-lt.ts +1 -1
- package/src/prover/smt/solver/ast.ts +58 -11
- package/src/prover/smt/solver/blast-term-ite.ts +47 -0
- package/src/prover/smt/solver/bound-simplifier.ts +2 -3
- 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 +3 -5
- package/src/prover/smt/solver/cofactor-elim-term-ite.ts +2 -3
- package/src/prover/smt/solver/context.ts +24 -3
- 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 +9 -4
- package/src/prover/smt/solver/diff-neq.ts +1 -2
- package/src/prover/smt/solver/elim-term-ite-tactic.ts +48 -0
- package/src/prover/smt/solver/elim-uncnstr.ts +4 -7
- 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 +2 -0
- 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/logic-tactics.ts +9 -2
- package/src/prover/smt/solver/lp/containers.ts +7 -1
- package/src/prover/smt/solver/lp/cross-nested.ts +1 -3
- package/src/prover/smt/solver/lp/lp-settings.ts +1 -1
- 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-core.ts +1 -1
- package/src/prover/smt/solver/lp/nla-grobner.ts +158 -80
- package/src/prover/smt/solver/lp/nra-solver.ts +45 -39
- 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/model-based-opt.ts +1 -1
- package/src/prover/smt/solver/model-generator.ts +3 -1
- 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 +10 -4
- package/src/prover/smt/solver/muz/dl-mk-slice.ts +3 -3
- package/src/prover/smt/solver/muz/dl-rule.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 +1 -1
- 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/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 -64
- 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 +9 -3
- package/src/prover/smt/solver/opt-nlsat.ts +29 -18
- package/src/prover/smt/solver/opt-preprocess.ts +1 -1
- package/src/prover/smt/solver/opt-solver.ts +6 -3
- package/src/prover/smt/solver/optsmt.ts +1 -1
- package/src/prover/smt/solver/pb-rewriter.ts +1 -4
- package/src/prover/smt/solver/poly-rewriter.ts +74 -18
- package/src/prover/smt/solver/probes.ts +88 -23
- package/src/prover/smt/solver/proto-model.ts +4 -6
- 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/quantifier-manager.ts +14 -9
- package/src/prover/smt/solver/recfun-decl-plugin.ts +2 -1
- package/src/prover/smt/solver/rlimit.ts +4 -0
- package/src/prover/smt/solver/sat/config.ts +1 -1
- package/src/prover/smt/solver/sat/inc-sat-solver.ts +26 -1
- package/src/prover/smt/solver/sat/model-converter.ts +1 -1
- package/src/prover/smt/solver/sat/pb-solver.ts +1 -1
- package/src/prover/smt/solver/sat/sat-tactic.ts +4 -3
- package/src/prover/smt/solver/sat/simplifier.ts +1 -1
- package/src/prover/smt/solver/sat/solver.ts +25 -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-kernel.ts +9 -3
- package/src/prover/smt/solver/smt-setup.ts +68 -6
- package/src/prover/smt/solver/smtlib-parser.ts +558 -220
- package/src/prover/smt/solver/smtlib-script.ts +495 -153
- package/src/prover/smt/solver/solve-eqs.ts +13 -21
- package/src/prover/smt/solver/tactic.ts +219 -130
- package/src/prover/smt/solver/theory-bv.ts +2 -2
- package/src/prover/smt/solver/theory-lra-final-check.ts +1 -1
- package/src/prover/smt/solver/theory-lra-internalize.ts +1 -0
- package/src/prover/smt/solver/theory-lra-propagate.ts +6 -4
- package/src/prover/smt/solver/theory-lra.ts +0 -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/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 +624 -187
- package/src/runtime.ts +3 -1
- package/src/sany/builtins.ts +10 -10
- package/src/sany/generation.ts +2 -13
- package/src/sany/generator-definitions.ts +3 -1
- package/src/sany/generator-expressions.ts +36 -30
- package/src/sany/generator-proofs.ts +5 -3
- package/src/sany/generator.ts +2 -1
- package/src/sany/lexer.ts +3 -3
- 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 +51 -22
- package/src/spec-description.ts +2 -2
- package/src/subexpressions.ts +265 -94
- package/src/successors.ts +1 -0
- package/src/symmetry.ts +1 -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/tlc-operators.ts +3 -1
- package/src/tlc/tlcext-operators.ts +18 -5
- package/src/tlc/trace-spec.ts +5 -5
- package/src/typed-tier.ts +29 -23
- package/src/typescript/types.ts +2 -2
- package/src/values.ts +46 -27
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// Deviation from Z3: nodes are never freed, so node indices match Z3 only until its first
|
|
2
|
-
// collection.
|
|
3
1
|
import { PddMemoryOutError } from "../../../../errors";
|
|
4
2
|
import { getGcd, Rational } from "../rational";
|
|
5
3
|
import { shrink } from "../array";
|
|
@@ -12,7 +10,7 @@ const MINUS_OP = 4;
|
|
|
12
10
|
const MUL_OP = 5;
|
|
13
11
|
const REDUCE_OP = 6;
|
|
14
12
|
const NO_OP = 10;
|
|
15
|
-
const
|
|
13
|
+
const MAX_RC = (1 << 10) - 1;
|
|
16
14
|
|
|
17
15
|
interface PddMonomial {
|
|
18
16
|
coeff: Rational;
|
|
@@ -29,9 +27,9 @@ interface PddPower {
|
|
|
29
27
|
degree: number;
|
|
30
28
|
}
|
|
31
29
|
|
|
32
|
-
interface
|
|
33
|
-
lc:
|
|
34
|
-
rest:
|
|
30
|
+
interface FactorEntry {
|
|
31
|
+
lc: number;
|
|
32
|
+
rest: number;
|
|
35
33
|
}
|
|
36
34
|
|
|
37
35
|
interface PddVarFactors {
|
|
@@ -51,68 +49,91 @@ interface CommonFactors {
|
|
|
51
49
|
secondCoeff: Rational;
|
|
52
50
|
}
|
|
53
51
|
|
|
52
|
+
const getNodeKey = (level: number, lo: number, hi: number) => `${level},${lo},${hi}`;
|
|
53
|
+
|
|
54
54
|
export class PddManager {
|
|
55
55
|
private levels: number[] = [];
|
|
56
56
|
private los: number[] = [];
|
|
57
57
|
private his: number[] = [];
|
|
58
|
+
private refcounts: number[] = [];
|
|
58
59
|
private values: Rational[] = [];
|
|
60
|
+
private opCache = new Map<number, number>();
|
|
61
|
+
private factorCache = new Map<string, FactorEntry>();
|
|
59
62
|
private nodeTable = new Map<string, number>();
|
|
60
63
|
private valueTable = new Map<string, number>();
|
|
61
|
-
private
|
|
62
|
-
private
|
|
63
|
-
private degrees: number[] = [];
|
|
64
|
-
private numAllocated = 0;
|
|
65
|
-
private maxNumNodes = 1 << 24;
|
|
66
|
-
private isApplying = false;
|
|
64
|
+
private pddStack: number[] = [];
|
|
65
|
+
private pddStackSize = 0;
|
|
67
66
|
private var2pdd: number[] = [];
|
|
68
67
|
var2level: number[] = [];
|
|
69
68
|
level2var: number[] = [];
|
|
69
|
+
private freeNodes: number[] = [];
|
|
70
|
+
private dmarks: number[] = [];
|
|
71
|
+
private dmarkLevel = 0;
|
|
72
|
+
private degrees: number[] = [];
|
|
73
|
+
private degreeTodo: number[] = [];
|
|
74
|
+
private maxNumNodes = 1 << 24;
|
|
75
|
+
private freeValues: number[] = [];
|
|
76
|
+
private freezeValue = Rational.zero;
|
|
70
77
|
|
|
71
78
|
constructor(numVars: number) {
|
|
72
|
-
this.initNodes(Array.from({ length: numVars }, (
|
|
79
|
+
this.initNodes(Array.from({ length: numVars }, (_value, index) => index));
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
reset(level2var: number[]) {
|
|
83
|
+
this.opCache.clear();
|
|
84
|
+
this.factorCache.clear();
|
|
85
|
+
this.nodeTable.clear();
|
|
76
86
|
this.levels = [];
|
|
77
87
|
this.los = [];
|
|
78
88
|
this.his = [];
|
|
89
|
+
this.refcounts = [];
|
|
90
|
+
this.dmarks = [];
|
|
91
|
+
this.degrees = [];
|
|
92
|
+
this.freeNodes = [];
|
|
93
|
+
this.pddStackSize = 0;
|
|
79
94
|
this.values = [];
|
|
80
|
-
this.
|
|
95
|
+
this.freeValues = [];
|
|
81
96
|
this.valueTable.clear();
|
|
82
|
-
this.opCache.clear();
|
|
83
|
-
this.factorCache.clear();
|
|
84
|
-
this.degrees = [];
|
|
85
97
|
this.initNodes(level2var);
|
|
86
98
|
}
|
|
87
99
|
|
|
100
|
+
setMaxNumNodes(limit: number) {
|
|
101
|
+
this.maxNumNodes = limit + this.level2var.length;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
numNodes() {
|
|
105
|
+
return this.levels.length - this.freeNodes.length;
|
|
106
|
+
}
|
|
107
|
+
|
|
88
108
|
private initNodes(level2var: number[]) {
|
|
89
109
|
for (let index = 0; index < NO_OP; index++) {
|
|
90
110
|
this.levels.push(0);
|
|
91
111
|
this.los.push(0);
|
|
92
112
|
this.his.push(0);
|
|
113
|
+
this.refcounts.push(MAX_RC);
|
|
114
|
+
this.dmarks.push(0);
|
|
115
|
+
this.degrees.push(0);
|
|
93
116
|
}
|
|
94
117
|
this.los[ONE_PDD] = 1;
|
|
95
118
|
this.values.push(Rational.zero, Rational.one);
|
|
96
|
-
this.
|
|
97
|
-
this.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
119
|
+
this.allocFreeNodes(1024 + level2var.length);
|
|
120
|
+
this.initVars(level2var);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private initVars(level2var: number[]) {
|
|
124
|
+
const count = level2var.length;
|
|
125
|
+
this.level2var = Array.from({ length: count }, () => 0);
|
|
126
|
+
this.var2level = Array.from({ length: count }, () => 0);
|
|
127
|
+
this.var2pdd = Array.from({ length: count }, () => 0);
|
|
128
|
+
for (let level = 0; level < count; level++) {
|
|
101
129
|
const variable = level2var[level];
|
|
102
130
|
this.var2pdd[variable] = this.makeNode(level, ZERO_PDD, ONE_PDD);
|
|
131
|
+
this.refcounts[this.var2pdd[variable]] = MAX_RC;
|
|
103
132
|
this.var2level[variable] = level;
|
|
104
133
|
this.level2var[level] = variable;
|
|
105
134
|
}
|
|
106
135
|
}
|
|
107
136
|
|
|
108
|
-
setMaxNumNodes(limit: number) {
|
|
109
|
-
this.maxNumNodes = limit + this.level2var.length;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
numNodes() {
|
|
113
|
-
return this.levels.length;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
137
|
isVal(node: number) {
|
|
117
138
|
return this.his[node] === 0;
|
|
118
139
|
}
|
|
@@ -137,13 +158,26 @@ export class PddManager {
|
|
|
137
158
|
return this.values[this.los[node]];
|
|
138
159
|
}
|
|
139
160
|
|
|
140
|
-
|
|
161
|
+
incRef(node: number) {
|
|
162
|
+
if (this.refcounts[node] !== MAX_RC) this.refcounts[node]++;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
decRef(node: number) {
|
|
166
|
+
if (this.refcounts[node] !== MAX_RC) this.refcounts[node]--;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private reserveVar(variable: number) {
|
|
141
170
|
while (this.var2level.length <= variable) {
|
|
142
171
|
const next = this.var2level.length;
|
|
143
172
|
this.var2pdd.push(this.makeNode(next, ZERO_PDD, ONE_PDD));
|
|
173
|
+
this.refcounts[this.var2pdd[next]] = MAX_RC;
|
|
144
174
|
this.var2level.push(next);
|
|
145
175
|
this.level2var.push(next);
|
|
146
176
|
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
mkVar(variable: number) {
|
|
180
|
+
this.reserveVar(variable);
|
|
147
181
|
return new Pdd(this, this.var2pdd[variable]);
|
|
148
182
|
}
|
|
149
183
|
|
|
@@ -175,151 +209,227 @@ export class PddManager {
|
|
|
175
209
|
return new Pdd(this, this.apply(first.root, second.root, REDUCE_OP));
|
|
176
210
|
}
|
|
177
211
|
|
|
212
|
+
mulValue(value: Rational, poly: Pdd) {
|
|
213
|
+
const constant = this.mkVal(value);
|
|
214
|
+
const result = new Pdd(this, this.apply(constant.root, poly.root, MUL_OP));
|
|
215
|
+
constant.release();
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
addValue(value: Rational, poly: Pdd) {
|
|
220
|
+
const constant = this.mkVal(value);
|
|
221
|
+
const result = new Pdd(this, this.apply(constant.root, poly.root, ADD_OP));
|
|
222
|
+
constant.release();
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
225
|
+
|
|
178
226
|
minus(poly: Pdd) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
227
|
+
const size = this.pddStackSize;
|
|
228
|
+
try {
|
|
229
|
+
for (let count = 0; ; count++) {
|
|
230
|
+
try {
|
|
231
|
+
return new Pdd(this, this.minusRec(poly.root));
|
|
232
|
+
} catch (error) {
|
|
233
|
+
if (!(error instanceof PddMemoryOutError)) throw error;
|
|
234
|
+
this.tryGc();
|
|
235
|
+
if (count > 0) this.maxNumNodes *= 2;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
} finally {
|
|
239
|
+
this.pddStackSize = size;
|
|
240
|
+
}
|
|
183
241
|
}
|
|
184
242
|
|
|
185
|
-
pow(poly: Pdd, exponent: number)
|
|
186
|
-
|
|
187
|
-
if (exponent === 1 || poly.isZero() || poly.isOne()) return poly;
|
|
188
|
-
if (poly.isVal()) return this.mkVal(poly.val().pow(exponent));
|
|
189
|
-
const half = this.pow(this.mul(poly, poly), Math.floor(exponent / 2));
|
|
190
|
-
return exponent % 2 === 1 ? this.mul(half, poly) : half;
|
|
243
|
+
pow(poly: Pdd, exponent: number) {
|
|
244
|
+
return new Pdd(this, this.powNode(poly.root, exponent));
|
|
191
245
|
}
|
|
192
246
|
|
|
193
|
-
private
|
|
194
|
-
|
|
247
|
+
private powNode(node: number, exponent: number) {
|
|
248
|
+
if (exponent === 0) return ONE_PDD;
|
|
249
|
+
if (exponent === 1) return node;
|
|
250
|
+
if (node === ZERO_PDD) return ZERO_PDD;
|
|
251
|
+
if (node === ONE_PDD) return ONE_PDD;
|
|
252
|
+
if (this.isVal(node)) return this.imkVal(this.val(node).pow(exponent));
|
|
253
|
+
return this.powRec(node, exponent);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private powRec(node: number, exponent: number): number {
|
|
257
|
+
if (exponent === 1) return node;
|
|
258
|
+
const half = this.powRec(this.apply(node, node, MUL_OP), Math.floor(exponent / 2));
|
|
259
|
+
return exponent % 2 === 1 ? this.apply(half, node, MUL_OP) : half;
|
|
195
260
|
}
|
|
196
261
|
|
|
197
|
-
private
|
|
198
|
-
|
|
262
|
+
private apply(first: number, second: number, op: number) {
|
|
263
|
+
const size = this.pddStackSize;
|
|
199
264
|
try {
|
|
200
|
-
|
|
265
|
+
for (let count = 0; ; count++) {
|
|
266
|
+
try {
|
|
267
|
+
return this.applyRec(first, second, op);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (!(error instanceof PddMemoryOutError)) throw error;
|
|
270
|
+
this.tryGc();
|
|
271
|
+
if (count > 0) this.maxNumNodes *= 2;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
201
274
|
} finally {
|
|
202
|
-
this.
|
|
275
|
+
this.pddStackSize = size;
|
|
203
276
|
}
|
|
204
277
|
}
|
|
205
278
|
|
|
206
279
|
private applyRec(first: number, second: number, op: number): number {
|
|
280
|
+
let p = first;
|
|
281
|
+
let q = second;
|
|
207
282
|
switch (op) {
|
|
208
283
|
case SUB_OP:
|
|
209
|
-
if (
|
|
210
|
-
if (this.isVal(
|
|
211
|
-
return this.imkVal(this.val(first).sub(this.val(second)));
|
|
212
|
-
}
|
|
284
|
+
if (q === ZERO_PDD) return p;
|
|
285
|
+
if (this.isVal(p) && this.isVal(q)) return this.imkVal(this.val(p).sub(this.val(q)));
|
|
213
286
|
break;
|
|
214
287
|
case ADD_OP:
|
|
215
|
-
if (
|
|
216
|
-
if (
|
|
217
|
-
if (this.isVal(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return this.applyRec(second, first, op);
|
|
288
|
+
if (p === ZERO_PDD) return q;
|
|
289
|
+
if (q === ZERO_PDD) return p;
|
|
290
|
+
if (this.isVal(p) && this.isVal(q)) return this.imkVal(this.val(p).add(this.val(q)));
|
|
291
|
+
if (this.isVal(p) || (!this.isVal(q) && this.level(p) < this.level(q))) {
|
|
292
|
+
p = second;
|
|
293
|
+
q = first;
|
|
222
294
|
}
|
|
223
295
|
break;
|
|
224
296
|
case MUL_OP:
|
|
225
|
-
if (
|
|
226
|
-
if (
|
|
227
|
-
if (
|
|
228
|
-
if (this.isVal(
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
return this.applyRec(second, first, op);
|
|
297
|
+
if (p === ZERO_PDD || q === ZERO_PDD) return ZERO_PDD;
|
|
298
|
+
if (p === ONE_PDD) return q;
|
|
299
|
+
if (q === ONE_PDD) return p;
|
|
300
|
+
if (this.isVal(p) && this.isVal(q)) return this.imkVal(this.val(p).mul(this.val(q)));
|
|
301
|
+
if (this.isVal(p) || (!this.isVal(q) && this.level(p) < this.level(q))) {
|
|
302
|
+
p = second;
|
|
303
|
+
q = first;
|
|
233
304
|
}
|
|
234
305
|
break;
|
|
235
|
-
|
|
236
|
-
if (
|
|
237
|
-
if (this.degree(
|
|
238
|
-
if (this.level(this.firstLeading(
|
|
306
|
+
default:
|
|
307
|
+
if (q === ZERO_PDD || this.isVal(p)) return p;
|
|
308
|
+
if (this.degree(p) < this.degree(q)) return p;
|
|
309
|
+
if (this.level(this.firstLeading(q)) > this.level(p)) return p;
|
|
239
310
|
break;
|
|
240
311
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
const firstLo = this.lo(first);
|
|
253
|
-
const firstHi = this.hi(first);
|
|
312
|
+
// Z3's op_entry equality compares the cached first operand with the new second one, so only equal operands hit.
|
|
313
|
+
// Deviation from Z3: on a hash collision Z3 returns that result for unequal operands too.
|
|
314
|
+
const isCached = p === q;
|
|
315
|
+
if (isCached) {
|
|
316
|
+
const cached = this.opCache.get(q * NO_OP + op);
|
|
317
|
+
if (cached !== undefined) return cached;
|
|
318
|
+
}
|
|
319
|
+
let result: number;
|
|
320
|
+
const levelP = this.level(p);
|
|
321
|
+
const levelQ = this.level(q);
|
|
322
|
+
let npop = 2;
|
|
254
323
|
switch (op) {
|
|
255
|
-
case ADD_OP:
|
|
256
|
-
if (this.isVal(
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return this.makeNode(firstLevel, this.applyRec(firstLo, second, op), firstHi);
|
|
269
|
-
}
|
|
270
|
-
const lo = this.applyRec(firstLo, this.lo(second), op);
|
|
271
|
-
return this.makeNode(firstLevel, lo, this.applyRec(firstHi, this.hi(second), op));
|
|
272
|
-
}
|
|
273
|
-
case MUL_OP: {
|
|
274
|
-
if (this.isVal(second) || firstLevel !== secondLevel) {
|
|
275
|
-
const lo = this.applyRec(firstLo, second, op);
|
|
276
|
-
return this.makeNode(firstLevel, lo, this.applyRec(firstHi, second, op));
|
|
324
|
+
case ADD_OP:
|
|
325
|
+
if (this.isVal(q)) {
|
|
326
|
+
this.push(this.applyRec(this.lo(p), q, op));
|
|
327
|
+
result = this.makeNode(levelP, this.read(1), this.hi(p));
|
|
328
|
+
npop = 1;
|
|
329
|
+
} else if (levelP === levelQ) {
|
|
330
|
+
this.push(this.applyRec(this.lo(p), this.lo(q), op));
|
|
331
|
+
this.push(this.applyRec(this.hi(p), this.hi(q), op));
|
|
332
|
+
result = this.makeNode(levelP, this.read(2), this.read(1));
|
|
333
|
+
} else {
|
|
334
|
+
this.push(this.applyRec(this.lo(p), q, op));
|
|
335
|
+
result = this.makeNode(levelP, this.read(1), this.hi(p));
|
|
336
|
+
npop = 1;
|
|
277
337
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
338
|
+
break;
|
|
339
|
+
case SUB_OP:
|
|
340
|
+
if (this.isVal(p) || (!this.isVal(q) && levelP < levelQ)) {
|
|
341
|
+
this.push(this.applyRec(p, this.lo(q), op));
|
|
342
|
+
this.push(this.minusRec(this.hi(q)));
|
|
343
|
+
result = this.makeNode(levelQ, this.read(2), this.read(1));
|
|
344
|
+
} else if (this.isVal(q) || levelP > levelQ) {
|
|
345
|
+
this.push(this.applyRec(this.lo(p), q, op));
|
|
346
|
+
result = this.makeNode(levelP, this.read(1), this.hi(p));
|
|
347
|
+
npop = 1;
|
|
348
|
+
} else {
|
|
349
|
+
this.push(this.applyRec(this.lo(p), this.lo(q), op));
|
|
350
|
+
this.push(this.applyRec(this.hi(p), this.hi(q), op));
|
|
351
|
+
result = this.makeNode(levelP, this.read(2), this.read(1));
|
|
286
352
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
353
|
+
break;
|
|
354
|
+
case MUL_OP:
|
|
355
|
+
if (this.isVal(q) || levelP !== levelQ) {
|
|
356
|
+
this.push(this.applyRec(this.lo(p), q, op));
|
|
357
|
+
this.push(this.applyRec(this.hi(p), q, op));
|
|
358
|
+
result = this.makeNode(levelP, this.read(2), this.read(1));
|
|
359
|
+
} else {
|
|
360
|
+
this.push(this.applyRec(this.hi(p), this.hi(q), op));
|
|
361
|
+
this.push(this.applyRec(this.hi(p), this.lo(q), op));
|
|
362
|
+
this.push(this.applyRec(this.lo(p), this.hi(q), op));
|
|
363
|
+
this.push(this.applyRec(this.lo(p), this.lo(q), op));
|
|
364
|
+
const ac = this.read(4);
|
|
365
|
+
const ad = this.read(3);
|
|
366
|
+
const bc = this.read(2);
|
|
367
|
+
const bd = this.read(1);
|
|
368
|
+
this.push(this.applyRec(ad, bc, ADD_OP));
|
|
369
|
+
const sum = this.read(1);
|
|
370
|
+
if (!this.isVal(sum) && this.level(sum) === levelP) {
|
|
371
|
+
this.push(this.applyRec(ac, this.hi(sum), ADD_OP));
|
|
372
|
+
this.push(this.makeNode(levelP, this.lo(sum), this.read(1)));
|
|
373
|
+
result = this.makeNode(levelP, bd, this.read(1));
|
|
374
|
+
npop = 7;
|
|
375
|
+
} else {
|
|
376
|
+
this.push(this.makeNode(levelP, sum, ac));
|
|
377
|
+
result = this.makeNode(levelP, bd, this.read(1));
|
|
378
|
+
npop = 6;
|
|
379
|
+
}
|
|
292
380
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
if (
|
|
296
|
-
|
|
297
|
-
|
|
381
|
+
break;
|
|
382
|
+
default:
|
|
383
|
+
if (this.level(this.firstLeading(q)) < levelP) {
|
|
384
|
+
this.push(this.applyRec(this.lo(p), q, op));
|
|
385
|
+
this.push(this.applyRec(this.hi(p), q, op));
|
|
386
|
+
const lo = this.read(2);
|
|
387
|
+
const hi = this.read(1);
|
|
388
|
+
if (lo === this.lo(p) && hi === this.hi(p)) {
|
|
389
|
+
result = p;
|
|
390
|
+
} else if (this.level(lo) < levelP && this.level(hi) <= levelP) {
|
|
391
|
+
result = this.makeNode(levelP, lo, hi);
|
|
392
|
+
} else {
|
|
393
|
+
this.push(this.applyRec(hi, this.var2pdd[this.var(p)], MUL_OP));
|
|
394
|
+
this.push(this.applyRec(this.read(1), lo, ADD_OP));
|
|
395
|
+
result = this.read(1);
|
|
396
|
+
npop = 4;
|
|
397
|
+
}
|
|
398
|
+
} else {
|
|
399
|
+
result = this.reduceOnMatch(p, q);
|
|
400
|
+
npop = 0;
|
|
298
401
|
}
|
|
299
|
-
const product = this.applyRec(hi, this.var2pdd[this.var(first)], MUL_OP);
|
|
300
|
-
return this.applyRec(product, lo, ADD_OP);
|
|
301
|
-
}
|
|
302
402
|
}
|
|
403
|
+
this.pop(npop);
|
|
404
|
+
if (isCached) this.opCache.set(q * NO_OP + op, result);
|
|
405
|
+
return result;
|
|
303
406
|
}
|
|
304
407
|
|
|
305
408
|
private minusRec(node: number): number {
|
|
306
409
|
if (node === ZERO_PDD) return ZERO_PDD;
|
|
307
410
|
if (this.isVal(node)) return this.imkVal(this.val(node).neg());
|
|
308
|
-
const
|
|
309
|
-
const cached = this.opCache.get(key);
|
|
411
|
+
const cached = this.opCache.get(node * NO_OP + MINUS_OP);
|
|
310
412
|
if (cached !== undefined) return cached;
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
this.
|
|
413
|
+
this.push(this.minusRec(this.lo(node)));
|
|
414
|
+
this.push(this.minusRec(this.hi(node)));
|
|
415
|
+
const result = this.makeNode(this.level(node), this.read(2), this.read(1));
|
|
416
|
+
this.pop(2);
|
|
417
|
+
this.opCache.set(node * NO_OP + MINUS_OP, result);
|
|
314
418
|
return result;
|
|
315
419
|
}
|
|
316
420
|
|
|
317
421
|
private reduceOnMatch(first: number, second: number) {
|
|
318
422
|
let current = first;
|
|
423
|
+
this.push(current);
|
|
319
424
|
while (this.lmOccurs(second, current)) {
|
|
320
|
-
|
|
321
|
-
|
|
425
|
+
this.push(this.ltQuotient(second, current));
|
|
426
|
+
this.push(this.applyRec(this.read(1), second, MUL_OP));
|
|
427
|
+
this.push(this.applyRec(current, this.read(1), ADD_OP));
|
|
428
|
+
current = this.read(1);
|
|
429
|
+
this.pop(4);
|
|
430
|
+
this.push(current);
|
|
322
431
|
}
|
|
432
|
+
this.pop(1);
|
|
323
433
|
return current;
|
|
324
434
|
}
|
|
325
435
|
|
|
@@ -359,22 +469,45 @@ export class PddManager {
|
|
|
359
469
|
}
|
|
360
470
|
|
|
361
471
|
private ltQuotientHi(first: number, second: number) {
|
|
362
|
-
|
|
363
|
-
|
|
472
|
+
this.push(this.ltQuotient(first, this.hi(second)));
|
|
473
|
+
const result = this.applyRec(this.var2pdd[this.var(second)], this.read(1), MUL_OP);
|
|
474
|
+
this.pop(1);
|
|
475
|
+
return result;
|
|
364
476
|
}
|
|
365
477
|
|
|
366
|
-
trySpoly(first: Pdd, second: Pdd) {
|
|
478
|
+
trySpoly(first: Pdd, second: Pdd, result: Pdd) {
|
|
367
479
|
const factors = this.commonFactors(first, second);
|
|
368
|
-
if (factors === null) return
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
480
|
+
if (factors === null) return false;
|
|
481
|
+
const spoly = this.spoly(first, second, factors);
|
|
482
|
+
result.assign(spoly);
|
|
483
|
+
spoly.release();
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private spoly(first: Pdd, second: Pdd, factors: CommonFactors) {
|
|
488
|
+
const firstMultiplier = this.mkVal(factors.secondCoeff);
|
|
489
|
+
try {
|
|
490
|
+
for (let index = factors.second.length; index-- > 0;) {
|
|
491
|
+
const variable = this.mkVar(factors.second[index]);
|
|
492
|
+
firstMultiplier.mulAssign(variable);
|
|
493
|
+
variable.release();
|
|
494
|
+
}
|
|
495
|
+
const secondMultiplier = this.mkVal(factors.firstCoeff.neg());
|
|
496
|
+
for (let index = factors.first.length; index-- > 0;) {
|
|
497
|
+
const variable = this.mkVar(factors.first[index]);
|
|
498
|
+
secondMultiplier.mulAssign(variable);
|
|
499
|
+
variable.release();
|
|
500
|
+
}
|
|
501
|
+
const firstProduct = firstMultiplier.mul(first);
|
|
502
|
+
const secondProduct = secondMultiplier.mul(second);
|
|
503
|
+
const result = firstProduct.add(secondProduct);
|
|
504
|
+
secondProduct.release();
|
|
505
|
+
firstProduct.release();
|
|
506
|
+
secondMultiplier.release();
|
|
507
|
+
return result;
|
|
508
|
+
} finally {
|
|
509
|
+
firstMultiplier.release();
|
|
376
510
|
}
|
|
377
|
-
return firstMultiplier.mul(first).add(secondMultiplier.mul(second));
|
|
378
511
|
}
|
|
379
512
|
|
|
380
513
|
private commonFactors(first: Pdd, second: Pdd): CommonFactors | null {
|
|
@@ -478,56 +611,109 @@ export class PddManager {
|
|
|
478
611
|
return this.firstLeading(this.hi(node));
|
|
479
612
|
}
|
|
480
613
|
|
|
481
|
-
degree(node: number)
|
|
614
|
+
degree(node: number) {
|
|
482
615
|
if (node === ZERO_PDD || node === ONE_PDD) return 0;
|
|
483
|
-
const
|
|
484
|
-
if (
|
|
485
|
-
const
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
616
|
+
const dmarkLevel = this.dmarkLevel;
|
|
617
|
+
if (this.dmarks[node] === dmarkLevel) return this.degrees[node];
|
|
618
|
+
const todo = this.degreeTodo;
|
|
619
|
+
todo.push(node);
|
|
620
|
+
while (todo.length > 0) {
|
|
621
|
+
const current = todo[todo.length - 1];
|
|
622
|
+
if (this.dmarks[current] === dmarkLevel) {
|
|
623
|
+
todo.pop();
|
|
624
|
+
} else if (this.isVal(current)) {
|
|
625
|
+
this.degrees[current] = 0;
|
|
626
|
+
this.dmarks[current] = dmarkLevel;
|
|
627
|
+
} else {
|
|
628
|
+
const lo = this.lo(current);
|
|
629
|
+
const hi = this.hi(current);
|
|
630
|
+
if (this.dmarks[lo] !== dmarkLevel || this.dmarks[hi] !== dmarkLevel) {
|
|
631
|
+
todo.push(lo, hi);
|
|
632
|
+
} else {
|
|
633
|
+
this.degrees[current] = Math.max(this.degrees[lo], this.degrees[hi] + 1);
|
|
634
|
+
this.dmarks[current] = dmarkLevel;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return this.degrees[node];
|
|
490
639
|
}
|
|
491
640
|
|
|
492
|
-
factor(poly: Pdd, variable: number, degree: number
|
|
641
|
+
factor(poly: Pdd, variable: number, degree: number, lc: Pdd, rest: Pdd) {
|
|
493
642
|
const variableLevel = this.var2level[variable];
|
|
494
643
|
if (degree === 0 || this.level(poly.root) < variableLevel) {
|
|
495
|
-
|
|
644
|
+
const zero = this.zero();
|
|
645
|
+
lc.assign(zero);
|
|
646
|
+
zero.release();
|
|
647
|
+
rest.assign(poly);
|
|
648
|
+
return;
|
|
496
649
|
}
|
|
497
650
|
const key = `${poly.root},${variable},${degree}`;
|
|
498
651
|
const cached = this.factorCache.get(key);
|
|
499
|
-
if (cached !== undefined)
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
652
|
+
if (cached !== undefined) {
|
|
653
|
+
const cachedLc = new Pdd(this, cached.lc);
|
|
654
|
+
lc.assign(cachedLc);
|
|
655
|
+
cachedLc.release();
|
|
656
|
+
const cachedRest = new Pdd(this, cached.rest);
|
|
657
|
+
rest.assign(cachedRest);
|
|
658
|
+
cachedRest.release();
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
507
661
|
if (this.level(poly.root) > variableLevel) {
|
|
662
|
+
const lc1 = this.zero();
|
|
663
|
+
const rest1 = this.zero();
|
|
508
664
|
const node = this.mkVar(poly.var());
|
|
509
|
-
const
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
const
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
665
|
+
const hi = poly.hi();
|
|
666
|
+
this.factor(hi, variable, degree, lc, rest);
|
|
667
|
+
hi.release();
|
|
668
|
+
const lo = poly.lo();
|
|
669
|
+
this.factor(lo, variable, degree, lc1, rest1);
|
|
670
|
+
lo.release();
|
|
671
|
+
lc.mulAssign(node);
|
|
672
|
+
rest.mulAssign(node);
|
|
673
|
+
lc.addAssign(lc1);
|
|
674
|
+
rest.addAssign(rest1);
|
|
675
|
+
node.release();
|
|
676
|
+
rest1.release();
|
|
677
|
+
lc1.release();
|
|
678
|
+
} else {
|
|
679
|
+
let found = 0;
|
|
680
|
+
const current = poly.copy();
|
|
681
|
+
while (found < degree && !current.isVal() && this.level(current.root) === variableLevel) {
|
|
682
|
+
found++;
|
|
683
|
+
const hi = current.hi();
|
|
684
|
+
current.assign(hi);
|
|
685
|
+
hi.release();
|
|
686
|
+
}
|
|
687
|
+
if (found === degree) {
|
|
688
|
+
lc.assign(current);
|
|
689
|
+
current.assign(poly);
|
|
690
|
+
const zero = this.zero();
|
|
691
|
+
rest.assign(zero);
|
|
692
|
+
zero.release();
|
|
693
|
+
const power = this.one();
|
|
694
|
+
for (let index = 0; index < degree; index++) {
|
|
695
|
+
const lo = current.lo();
|
|
696
|
+
const term = power.mul(lo);
|
|
697
|
+
rest.addAssign(term);
|
|
698
|
+
term.release();
|
|
699
|
+
lo.release();
|
|
700
|
+
const hi = current.hi();
|
|
701
|
+
current.assign(hi);
|
|
702
|
+
const variablePdd = this.mkVar(variable);
|
|
703
|
+
power.mulAssign(variablePdd);
|
|
704
|
+
variablePdd.release();
|
|
705
|
+
hi.release();
|
|
706
|
+
}
|
|
707
|
+
power.release();
|
|
708
|
+
} else {
|
|
709
|
+
const zero = this.zero();
|
|
710
|
+
lc.assign(zero);
|
|
711
|
+
zero.release();
|
|
712
|
+
rest.assign(poly);
|
|
713
|
+
}
|
|
714
|
+
current.release();
|
|
715
|
+
}
|
|
716
|
+
this.factorCache.set(key, { lc: lc.root, rest: rest.root });
|
|
531
717
|
}
|
|
532
718
|
|
|
533
719
|
isBinary(node: number) {
|
|
@@ -605,14 +791,36 @@ export class PddManager {
|
|
|
605
791
|
return vars;
|
|
606
792
|
}
|
|
607
793
|
|
|
794
|
+
private push(node: number) {
|
|
795
|
+
this.pddStack[this.pddStackSize++] = node;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
private pop(count: number) {
|
|
799
|
+
this.pddStackSize -= count;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
private read(index: number) {
|
|
803
|
+
return this.pddStack[this.pddStackSize - index];
|
|
804
|
+
}
|
|
805
|
+
|
|
608
806
|
private imkVal(value: Rational) {
|
|
609
807
|
if (value.isZero()) return ZERO_PDD;
|
|
610
808
|
if (value.equals(Rational.one)) return ONE_PDD;
|
|
611
809
|
const key = value.toString();
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
810
|
+
return this.valueTable.get(key) ?? this.initValue(value, key);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
private initValue(value: Rational, key: string) {
|
|
814
|
+
let valueIndex = this.values.length;
|
|
815
|
+
if (this.freeValues.length === 0) {
|
|
816
|
+
this.values.push(value);
|
|
817
|
+
} else {
|
|
818
|
+
valueIndex = this.freeValues[this.freeValues.length - 1];
|
|
819
|
+
this.freeValues.pop();
|
|
820
|
+
this.values[valueIndex] = value;
|
|
821
|
+
}
|
|
822
|
+
this.freezeValue = value;
|
|
823
|
+
const node = this.insertNode(0, valueIndex, 0);
|
|
616
824
|
this.valueTable.set(key, node);
|
|
617
825
|
return node;
|
|
618
826
|
}
|
|
@@ -622,28 +830,158 @@ export class PddManager {
|
|
|
622
830
|
}
|
|
623
831
|
|
|
624
832
|
private insertNode(level: number, lo: number, hi: number) {
|
|
625
|
-
const key =
|
|
833
|
+
const key = getNodeKey(level, lo, hi);
|
|
626
834
|
const existing = this.nodeTable.get(key);
|
|
627
835
|
if (existing !== undefined) return existing;
|
|
628
|
-
if (this.
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
this.
|
|
632
|
-
}
|
|
633
|
-
const node = this.
|
|
634
|
-
this.
|
|
635
|
-
this.
|
|
636
|
-
this.
|
|
836
|
+
if (this.freeNodes.length === 0) {
|
|
837
|
+
this.gc();
|
|
838
|
+
if (this.levels.length > this.maxNumNodes) throw new PddMemoryOutError();
|
|
839
|
+
this.allocFreeNodes(Math.floor(this.levels.length / 2));
|
|
840
|
+
}
|
|
841
|
+
const node = this.freeNodes[this.freeNodes.length - 1];
|
|
842
|
+
this.freeNodes.pop();
|
|
843
|
+
this.levels[node] = level;
|
|
844
|
+
this.los[node] = lo;
|
|
845
|
+
this.his[node] = hi;
|
|
846
|
+
this.refcounts[node] = 0;
|
|
637
847
|
this.nodeTable.set(key, node);
|
|
638
848
|
return node;
|
|
639
849
|
}
|
|
850
|
+
|
|
851
|
+
private tryGc() {
|
|
852
|
+
this.gc();
|
|
853
|
+
this.opCache.clear();
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
private allocFreeNodes(count: number) {
|
|
857
|
+
for (let index = 0; index < count; index++) {
|
|
858
|
+
this.freeNodes.push(this.levels.length);
|
|
859
|
+
this.levels.push(0);
|
|
860
|
+
this.los.push(0);
|
|
861
|
+
this.his.push(0);
|
|
862
|
+
this.refcounts.push(0);
|
|
863
|
+
this.dmarks.push(0);
|
|
864
|
+
this.degrees.push(0);
|
|
865
|
+
}
|
|
866
|
+
this.freeNodes.sort((first, second) => second - first);
|
|
867
|
+
this.initDmark();
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
private computeReachable(reachable: Uint8Array) {
|
|
871
|
+
const todo: number[] = [];
|
|
872
|
+
for (let index = this.pddStackSize; index-- > 0;) {
|
|
873
|
+
reachable[this.pddStack[index]] = 1;
|
|
874
|
+
todo.push(this.pddStack[index]);
|
|
875
|
+
}
|
|
876
|
+
for (let node = NO_OP; node-- > 0;) reachable[node] = 1;
|
|
877
|
+
for (let node = this.levels.length; node-- > NO_OP;) {
|
|
878
|
+
if (this.refcounts[node] > 0) {
|
|
879
|
+
reachable[node] = 1;
|
|
880
|
+
todo.push(node);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
for (let node = todo.pop(); node !== undefined; node = todo.pop()) {
|
|
884
|
+
if (this.isVal(node)) continue;
|
|
885
|
+
const lo = this.lo(node);
|
|
886
|
+
const hi = this.hi(node);
|
|
887
|
+
if (reachable[lo] === 0) {
|
|
888
|
+
reachable[lo] = 1;
|
|
889
|
+
todo.push(lo);
|
|
890
|
+
}
|
|
891
|
+
if (reachable[hi] === 0) {
|
|
892
|
+
reachable[hi] = 1;
|
|
893
|
+
todo.push(hi);
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
private gc() {
|
|
899
|
+
this.initDmark();
|
|
900
|
+
this.freeNodes = [];
|
|
901
|
+
const size = this.levels.length;
|
|
902
|
+
const reachable = new Uint8Array(size);
|
|
903
|
+
this.computeReachable(reachable);
|
|
904
|
+
for (let node = size; node-- > NO_OP;) {
|
|
905
|
+
if (reachable[node] === 1) continue;
|
|
906
|
+
if (this.his[node] === 0 && this.los[node] !== 0) {
|
|
907
|
+
const value = this.values[this.los[node]];
|
|
908
|
+
if (this.freezeValue.equals(value)) continue;
|
|
909
|
+
this.freeValues.push(this.los[node]);
|
|
910
|
+
this.valueTable.delete(value.toString());
|
|
911
|
+
}
|
|
912
|
+
this.los[node] = 0;
|
|
913
|
+
this.his[node] = 0;
|
|
914
|
+
this.freeNodes.push(node);
|
|
915
|
+
}
|
|
916
|
+
this.opCache.clear();
|
|
917
|
+
this.factorCache.clear();
|
|
918
|
+
this.nodeTable.clear();
|
|
919
|
+
for (let node = size; node-- > 2;) {
|
|
920
|
+
if (reachable[node] === 0) continue;
|
|
921
|
+
this.nodeTable.set(getNodeKey(this.levels[node], this.los[node], this.his[node]), node);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
private initDmark() {
|
|
926
|
+
this.dmarkLevel++;
|
|
927
|
+
}
|
|
640
928
|
}
|
|
641
929
|
|
|
930
|
+
const getMonomials = (poly: Pdd): PddMonomial[] => {
|
|
931
|
+
if (poly.isVal()) return poly.val().isZero() ? [] : [{ coeff: poly.val(), vars: [] }];
|
|
932
|
+
const hi = poly.hi();
|
|
933
|
+
const monomials = getMonomials(hi);
|
|
934
|
+
hi.release();
|
|
935
|
+
for (const monomial of monomials) monomial.vars.push(poly.var());
|
|
936
|
+
const lo = poly.lo();
|
|
937
|
+
monomials.push(...getMonomials(lo));
|
|
938
|
+
lo.release();
|
|
939
|
+
return monomials;
|
|
940
|
+
};
|
|
941
|
+
|
|
642
942
|
export class Pdd {
|
|
643
943
|
constructor(
|
|
644
944
|
private readonly manager: PddManager,
|
|
645
|
-
|
|
646
|
-
) {
|
|
945
|
+
public root: number,
|
|
946
|
+
) {
|
|
947
|
+
manager.incRef(root);
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
copy() {
|
|
951
|
+
const result = new Pdd(this.manager, this.root);
|
|
952
|
+
// Like Z3, whose copy constructor delegates to a counting constructor and then counts again.
|
|
953
|
+
this.manager.incRef(this.root);
|
|
954
|
+
return result;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
release() {
|
|
958
|
+
this.manager.decRef(this.root);
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
assign(other: Pdd) {
|
|
962
|
+
const previous = this.root;
|
|
963
|
+
this.root = other.root;
|
|
964
|
+
this.manager.incRef(this.root);
|
|
965
|
+
this.manager.decRef(previous);
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
addAssign(other: Pdd) {
|
|
969
|
+
const sum = this.add(other);
|
|
970
|
+
this.assign(sum);
|
|
971
|
+
sum.release();
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
subAssign(value: Rational) {
|
|
975
|
+
const difference = this.sub(value);
|
|
976
|
+
this.assign(difference);
|
|
977
|
+
difference.release();
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
mulAssign(other: Pdd | Rational) {
|
|
981
|
+
const product = this.mul(other);
|
|
982
|
+
this.assign(product);
|
|
983
|
+
product.release();
|
|
984
|
+
}
|
|
647
985
|
|
|
648
986
|
lo() {
|
|
649
987
|
return new Pdd(this.manager, this.manager.lo(this.root));
|
|
@@ -678,11 +1016,29 @@ export class Pdd {
|
|
|
678
1016
|
}
|
|
679
1017
|
|
|
680
1018
|
isUnary() {
|
|
681
|
-
|
|
1019
|
+
if (this.isVal()) return false;
|
|
1020
|
+
const lo = this.lo();
|
|
1021
|
+
let isUnary = lo.isZero();
|
|
1022
|
+
if (isUnary) {
|
|
1023
|
+
const hi = this.hi();
|
|
1024
|
+
isUnary = hi.isVal();
|
|
1025
|
+
hi.release();
|
|
1026
|
+
}
|
|
1027
|
+
lo.release();
|
|
1028
|
+
return isUnary;
|
|
682
1029
|
}
|
|
683
1030
|
|
|
684
1031
|
isOffset() {
|
|
685
|
-
|
|
1032
|
+
if (this.isVal()) return false;
|
|
1033
|
+
const lo = this.lo();
|
|
1034
|
+
let isOffset = lo.isVal();
|
|
1035
|
+
if (isOffset) {
|
|
1036
|
+
const hi = this.hi();
|
|
1037
|
+
isOffset = hi.isOne();
|
|
1038
|
+
hi.release();
|
|
1039
|
+
}
|
|
1040
|
+
lo.release();
|
|
1041
|
+
return isOffset;
|
|
686
1042
|
}
|
|
687
1043
|
|
|
688
1044
|
isBinary() {
|
|
@@ -699,17 +1055,19 @@ export class Pdd {
|
|
|
699
1055
|
|
|
700
1056
|
add(other: Pdd | Rational) {
|
|
701
1057
|
return other instanceof Rational
|
|
702
|
-
? this.manager.
|
|
1058
|
+
? this.manager.addValue(other, this)
|
|
703
1059
|
: this.manager.add(this, other);
|
|
704
1060
|
}
|
|
705
1061
|
|
|
706
1062
|
sub(other: Pdd | Rational) {
|
|
707
|
-
return other instanceof Rational
|
|
1063
|
+
return other instanceof Rational
|
|
1064
|
+
? this.manager.addValue(other.neg(), this)
|
|
1065
|
+
: this.manager.sub(this, other);
|
|
708
1066
|
}
|
|
709
1067
|
|
|
710
1068
|
mul(other: Pdd | Rational) {
|
|
711
1069
|
return other instanceof Rational
|
|
712
|
-
? this.manager.
|
|
1070
|
+
? this.manager.mulValue(other, this)
|
|
713
1071
|
: this.manager.mul(this, other);
|
|
714
1072
|
}
|
|
715
1073
|
|
|
@@ -721,8 +1079,8 @@ export class Pdd {
|
|
|
721
1079
|
return this.manager.reduce(this, other);
|
|
722
1080
|
}
|
|
723
1081
|
|
|
724
|
-
factor(variable: number, degree: number) {
|
|
725
|
-
|
|
1082
|
+
factor(variable: number, degree: number, lc: Pdd, rest: Pdd) {
|
|
1083
|
+
this.manager.factor(this, variable, degree, lc, rest);
|
|
726
1084
|
}
|
|
727
1085
|
|
|
728
1086
|
differentLeadingTerm(other: Pdd) {
|
|
@@ -750,18 +1108,34 @@ export class Pdd {
|
|
|
750
1108
|
}
|
|
751
1109
|
|
|
752
1110
|
varFactors(): PddVarFactors {
|
|
753
|
-
if (this.isVal()) return { vars: [], poly: this };
|
|
1111
|
+
if (this.isVal()) return { vars: [], poly: this.copy() };
|
|
754
1112
|
const manager = this.manager;
|
|
755
1113
|
const variable = this.var();
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
1114
|
+
const firstLo = this.lo();
|
|
1115
|
+
const isLoVal = firstLo.isVal();
|
|
1116
|
+
firstLo.release();
|
|
1117
|
+
if (isLoVal) {
|
|
1118
|
+
const secondLo = this.lo();
|
|
1119
|
+
const isLoZero = secondLo.isZero();
|
|
1120
|
+
secondLo.release();
|
|
1121
|
+
if (!isLoZero) return { vars: [], poly: this.copy() };
|
|
1122
|
+
const hi = this.hi();
|
|
1123
|
+
const factors = hi.varFactors();
|
|
1124
|
+
hi.release();
|
|
759
1125
|
factors.vars.push(variable);
|
|
760
|
-
|
|
1126
|
+
const result = { vars: factors.vars, poly: factors.poly.copy() };
|
|
1127
|
+
factors.poly.release();
|
|
1128
|
+
return result;
|
|
761
1129
|
}
|
|
762
|
-
const
|
|
1130
|
+
const lo = this.lo();
|
|
1131
|
+
const loFactors = lo.varFactors();
|
|
1132
|
+
lo.release();
|
|
763
1133
|
const loVars = loFactors.vars;
|
|
764
|
-
if (loVars.length === 0)
|
|
1134
|
+
if (loVars.length === 0) {
|
|
1135
|
+
const result = { vars: [], poly: this.copy() };
|
|
1136
|
+
loFactors.poly.release();
|
|
1137
|
+
return result;
|
|
1138
|
+
}
|
|
765
1139
|
const loAndHi: number[] = [];
|
|
766
1140
|
const merge = (hiVars: number[]) => {
|
|
767
1141
|
let loKept = 0;
|
|
@@ -786,104 +1160,161 @@ export class Pdd {
|
|
|
786
1160
|
shrink(loVars, loKept);
|
|
787
1161
|
shrink(hiVars, hiKept);
|
|
788
1162
|
};
|
|
789
|
-
const multiply = (vars: number[], poly: Pdd) =>
|
|
790
|
-
|
|
791
|
-
|
|
1163
|
+
const multiply = (vars: number[], poly: Pdd) => {
|
|
1164
|
+
const product = poly.copy();
|
|
1165
|
+
for (const innerVar of vars) {
|
|
1166
|
+
const variablePdd = manager.mkVar(innerVar);
|
|
1167
|
+
product.mulAssign(variablePdd);
|
|
1168
|
+
variablePdd.release();
|
|
1169
|
+
}
|
|
1170
|
+
return product;
|
|
1171
|
+
};
|
|
1172
|
+
const hi = this.hi();
|
|
1173
|
+
const hiFactors = hi.varFactors();
|
|
1174
|
+
hi.release();
|
|
792
1175
|
const hiVars = hiFactors.vars;
|
|
793
1176
|
if (loVars[loVars.length - 1] === variable) {
|
|
794
1177
|
loVars.pop();
|
|
795
1178
|
merge(hiVars);
|
|
796
1179
|
loAndHi.push(variable);
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
if (
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
vars: loAndHi,
|
|
808
|
-
|
|
809
|
-
|
|
1180
|
+
} else if (hiVars.length > 0) {
|
|
1181
|
+
merge(hiVars);
|
|
1182
|
+
hiVars.push(variable);
|
|
1183
|
+
}
|
|
1184
|
+
let result: PddVarFactors;
|
|
1185
|
+
if (loAndHi.length === 0) {
|
|
1186
|
+
result = { vars: [], poly: this.copy() };
|
|
1187
|
+
} else {
|
|
1188
|
+
const loProduct = multiply(loVars, loFactors.poly);
|
|
1189
|
+
const hiProduct = multiply(hiVars, hiFactors.poly);
|
|
1190
|
+
result = { vars: loAndHi, poly: loProduct.add(hiProduct) };
|
|
1191
|
+
hiProduct.release();
|
|
1192
|
+
loProduct.release();
|
|
1193
|
+
}
|
|
1194
|
+
hiFactors.poly.release();
|
|
1195
|
+
loFactors.poly.release();
|
|
1196
|
+
return result;
|
|
810
1197
|
}
|
|
811
1198
|
|
|
812
1199
|
substPdd(variable: number, replacement: Pdd): Pdd {
|
|
813
1200
|
const manager = this.manager;
|
|
814
|
-
if (this.isVal() || manager.var2level[this.var()] < manager.var2level[variable])
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
1201
|
+
if (this.isVal() || manager.var2level[this.var()] < manager.var2level[variable]) {
|
|
1202
|
+
return this.copy();
|
|
1203
|
+
}
|
|
1204
|
+
const lo = this.lo();
|
|
1205
|
+
const substitutedLo = lo.substPdd(variable, replacement);
|
|
1206
|
+
lo.release();
|
|
1207
|
+
const hi = this.hi();
|
|
1208
|
+
const substitutedHi = hi.substPdd(variable, replacement);
|
|
1209
|
+
hi.release();
|
|
1210
|
+
let result: Pdd;
|
|
1211
|
+
if (this.var() === variable) {
|
|
1212
|
+
const product = replacement.mul(substitutedHi);
|
|
1213
|
+
result = product.add(substitutedLo);
|
|
1214
|
+
product.release();
|
|
1215
|
+
} else {
|
|
1216
|
+
const thisLo = this.lo();
|
|
1217
|
+
let isUnchanged = substitutedLo.equals(thisLo);
|
|
1218
|
+
if (isUnchanged) {
|
|
1219
|
+
const thisHi = this.hi();
|
|
1220
|
+
isUnchanged = substitutedHi.equals(thisHi);
|
|
1221
|
+
thisHi.release();
|
|
1222
|
+
}
|
|
1223
|
+
thisLo.release();
|
|
1224
|
+
if (isUnchanged) {
|
|
1225
|
+
result = this.copy();
|
|
1226
|
+
} else {
|
|
1227
|
+
const variablePdd = manager.mkVar(this.var());
|
|
1228
|
+
const product = variablePdd.mul(substitutedHi);
|
|
1229
|
+
result = product.add(substitutedLo);
|
|
1230
|
+
product.release();
|
|
1231
|
+
variablePdd.release();
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
substitutedHi.release();
|
|
1235
|
+
substitutedLo.release();
|
|
1236
|
+
return result;
|
|
820
1237
|
}
|
|
821
1238
|
|
|
822
1239
|
*[Symbol.iterator](): Generator<PddMonomial> {
|
|
823
|
-
const
|
|
824
|
-
const
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
return;
|
|
835
|
-
}
|
|
836
|
-
yield { coeff: manager.val(node), vars: [...vars] };
|
|
837
|
-
while (nodes.length > 0) {
|
|
838
|
-
const top = nodes[nodes.length - 1];
|
|
839
|
-
if (!top.isFirst) {
|
|
840
|
-
nodes.pop();
|
|
841
|
-
continue;
|
|
1240
|
+
const begin = this.copy();
|
|
1241
|
+
const end = this.copy();
|
|
1242
|
+
try {
|
|
1243
|
+
const manager = this.manager;
|
|
1244
|
+
const nodes: IteratorNode[] = [];
|
|
1245
|
+
const vars: number[] = [];
|
|
1246
|
+
let node = begin.root;
|
|
1247
|
+
while (!manager.isVal(node)) {
|
|
1248
|
+
nodes.push({ isFirst: true, node });
|
|
1249
|
+
vars.push(manager.var(node));
|
|
1250
|
+
node = manager.hi(node);
|
|
842
1251
|
}
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
if (next === ZERO_PDD) {
|
|
847
|
-
nodes.pop();
|
|
848
|
-
continue;
|
|
1252
|
+
if (nodes.length === 0) {
|
|
1253
|
+
if (!manager.val(node).isZero()) yield { coeff: manager.val(node), vars: [] };
|
|
1254
|
+
return;
|
|
849
1255
|
}
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1256
|
+
yield { coeff: manager.val(node), vars: [...vars] };
|
|
1257
|
+
while (nodes.length > 0) {
|
|
1258
|
+
const top = nodes[nodes.length - 1];
|
|
1259
|
+
if (!top.isFirst) {
|
|
1260
|
+
nodes.pop();
|
|
1261
|
+
continue;
|
|
1262
|
+
}
|
|
1263
|
+
top.isFirst = false;
|
|
1264
|
+
vars.pop();
|
|
1265
|
+
let next = manager.lo(top.node);
|
|
1266
|
+
if (next === ZERO_PDD) {
|
|
1267
|
+
nodes.pop();
|
|
1268
|
+
continue;
|
|
1269
|
+
}
|
|
1270
|
+
while (!manager.isVal(next)) {
|
|
1271
|
+
nodes.push({ isFirst: true, node: next });
|
|
1272
|
+
vars.push(manager.var(next));
|
|
1273
|
+
next = manager.hi(next);
|
|
1274
|
+
}
|
|
1275
|
+
yield { coeff: manager.val(next), vars: [...vars] };
|
|
854
1276
|
}
|
|
855
|
-
|
|
1277
|
+
} finally {
|
|
1278
|
+
end.release();
|
|
1279
|
+
begin.release();
|
|
856
1280
|
}
|
|
857
1281
|
}
|
|
858
1282
|
|
|
859
1283
|
*coefficients(): Generator<PddCoefficient> {
|
|
860
|
-
const
|
|
861
|
-
const
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
nodes
|
|
865
|
-
node =
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
let next = manager.lo(top);
|
|
870
|
-
if (next === ZERO_PDD) continue;
|
|
871
|
-
while (!manager.isVal(next)) {
|
|
872
|
-
nodes.push(next);
|
|
873
|
-
next = manager.hi(next);
|
|
1284
|
+
const begin = this.copy();
|
|
1285
|
+
const end = this.copy();
|
|
1286
|
+
try {
|
|
1287
|
+
const manager = this.manager;
|
|
1288
|
+
const nodes: number[] = [];
|
|
1289
|
+
let node = begin.root;
|
|
1290
|
+
while (!manager.isVal(node)) {
|
|
1291
|
+
nodes.push(node);
|
|
1292
|
+
node = manager.hi(node);
|
|
874
1293
|
}
|
|
875
|
-
yield { coeff: manager.val(
|
|
1294
|
+
yield { coeff: manager.val(node), isConstant: nodes.length === 0 };
|
|
1295
|
+
for (let top = nodes.pop(); top !== undefined; top = nodes.pop()) {
|
|
1296
|
+
let next = manager.lo(top);
|
|
1297
|
+
if (next === ZERO_PDD) continue;
|
|
1298
|
+
while (!manager.isVal(next)) {
|
|
1299
|
+
nodes.push(next);
|
|
1300
|
+
next = manager.hi(next);
|
|
1301
|
+
}
|
|
1302
|
+
yield { coeff: manager.val(next), isConstant: nodes.length === 0 };
|
|
1303
|
+
}
|
|
1304
|
+
} finally {
|
|
1305
|
+
end.release();
|
|
1306
|
+
begin.release();
|
|
876
1307
|
}
|
|
877
1308
|
}
|
|
878
1309
|
|
|
879
1310
|
toString() {
|
|
880
1311
|
let text = "";
|
|
881
|
-
for (const monomial of this) {
|
|
1312
|
+
for (const monomial of getMonomials(this)) {
|
|
882
1313
|
if (text !== "") text += " ";
|
|
883
1314
|
if (monomial.coeff.isNegative()) text += "- ";
|
|
884
1315
|
else if (text !== "") text += "+ ";
|
|
885
1316
|
const coeff = monomial.coeff.abs();
|
|
886
|
-
const vars = monomial.vars;
|
|
1317
|
+
const vars = monomial.vars.reverse();
|
|
887
1318
|
if (!coeff.equals(Rational.one) || vars.length === 0) {
|
|
888
1319
|
text += vars.length === 0 ? `${coeff}` : `${coeff}*`;
|
|
889
1320
|
}
|
|
@@ -900,9 +1331,13 @@ export class Pdd {
|
|
|
900
1331
|
}
|
|
901
1332
|
}
|
|
902
1333
|
|
|
903
|
-
export const evaluatePdd = (poly: Pdd, getValue: (variable: number) => Rational): Rational =>
|
|
904
|
-
poly.isVal()
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
1334
|
+
export const evaluatePdd = (poly: Pdd, getValue: (variable: number) => Rational): Rational => {
|
|
1335
|
+
if (poly.isVal()) return poly.val();
|
|
1336
|
+
const hi = poly.hi();
|
|
1337
|
+
const product = evaluatePdd(hi, getValue).mul(getValue(poly.var()));
|
|
1338
|
+
const lo = poly.lo();
|
|
1339
|
+
const result = product.add(evaluatePdd(lo, getValue));
|
|
1340
|
+
lo.release();
|
|
1341
|
+
hi.release();
|
|
1342
|
+
return result;
|
|
1343
|
+
};
|