tla-ts 0.0.4-dev.a0009b7 → 0.0.4-dev.a11a30a
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/README.md +32 -143
- package/dist/deciders.cjs +366 -0
- package/dist/deciders.js +366 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +35 -1
- package/dist/index.d.ts +35 -1
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -4
- package/dist/parallel-worker.js +1 -4
- package/dist/prove-worker.cjs +9 -0
- package/dist/prove-worker.d.cts +9 -0
- package/dist/prove-worker.d.ts +9 -0
- package/dist/prove-worker.js +9 -0
- package/dist/prove.cjs +996 -0
- package/dist/prove.js +996 -0
- package/dist/run-cli.cjs +37 -47
- package/dist/run-cli.js +37 -47
- package/dist/tlapm.cjs +10 -0
- package/dist/tlapm.js +10 -0
- package/dist/transfer.cjs +38 -44
- package/dist/transfer.js +38 -44
- package/dist/worker.cjs +21 -0
- package/dist/worker.js +21 -0
- package/package.json +21 -12
- package/src/check.ts +1 -0
- package/src/codegen.ts +26 -7
- package/src/errors.ts +86 -0
- package/src/evaluate.ts +63 -20
- package/src/index.ts +10 -0
- package/src/{obligation-action.ts → obligations/action.ts} +247 -69
- package/src/{obligation-anon.ts → obligations/anon.ts} +49 -47
- package/src/obligations/ctx.ts +64 -0
- package/src/{obligation-deref.ts → obligations/deref.ts} +51 -92
- package/src/{obligation-expression.ts → obligations/expression.ts} +117 -57
- package/src/{obligation-format.ts → obligations/format.ts} +7 -8
- package/src/{obligation-generate.ts → obligations/generate.ts} +170 -192
- package/src/{obligations.ts → obligations/index.ts} +32 -16
- package/src/{obligation-levels.ts → obligations/levels.ts} +26 -28
- package/src/{obligation-module.ts → obligations/module.ts} +113 -121
- package/src/obligations/pickle.ts +98 -0
- package/src/obligations/pragma.ts +189 -0
- package/src/{obligation-printer.ts → obligations/printer.ts} +22 -80
- package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
- package/src/{obligation-source.ts → obligations/source.ts} +154 -99
- package/src/obligations/temporal.ts +321 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
- package/src/ocaml-marshal.ts +166 -0
- package/src/parallel-worker.ts +2 -18
- package/src/parallel.ts +10 -43
- package/src/prove/decide-pool.ts +95 -0
- package/src/prove/deciders.ts +33 -0
- package/src/prove/first-order.ts +78 -0
- package/src/prove/isabelle/LICENSE +36 -0
- package/src/prove/isabelle/LICENSE-tlapm +26 -0
- package/src/prove/isabelle/decide.ts +47 -0
- package/src/prove/isabelle/encode.ts +446 -0
- package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
- package/src/prove/isabelle/isar/method.ts +367 -0
- package/src/prove/isabelle/isar/proof.ts +28 -0
- package/src/prove/isabelle/isar/statement.ts +57 -0
- package/src/prove/isabelle/isar/theory.ts +192 -0
- package/src/prove/isabelle/isar/type-check.ts +278 -0
- package/src/prove/isabelle/provers/blast/blast.ts +258 -0
- package/src/prove/isabelle/provers/blast/branch.ts +322 -0
- package/src/prove/isabelle/provers/blast/prove.ts +347 -0
- package/src/prove/isabelle/provers/blast/rules.ts +330 -0
- package/src/prove/isabelle/provers/blast/state.ts +180 -0
- package/src/prove/isabelle/provers/blast/term.ts +547 -0
- package/src/prove/isabelle/provers/clasimp.ts +150 -0
- package/src/prove/isabelle/provers/classical.ts +389 -0
- package/src/prove/isabelle/provers/hypsubst.ts +441 -0
- package/src/prove/isabelle/pure/bicompose.ts +174 -0
- package/src/prove/isabelle/pure/consts.ts +32 -0
- package/src/prove/isabelle/pure/conv.ts +79 -0
- package/src/prove/isabelle/pure/drule.ts +264 -0
- package/src/prove/isabelle/pure/envir.ts +363 -0
- package/src/prove/isabelle/pure/goal.ts +53 -0
- package/src/prove/isabelle/pure/interrupt.ts +16 -0
- package/src/prove/isabelle/pure/list.ts +12 -0
- package/src/prove/isabelle/pure/logic.ts +402 -0
- package/src/prove/isabelle/pure/name.ts +110 -0
- package/src/prove/isabelle/pure/net.ts +262 -0
- package/src/prove/isabelle/pure/object-logic.ts +41 -0
- package/src/prove/isabelle/pure/pattern.ts +650 -0
- package/src/prove/isabelle/pure/raw-simplifier.ts +353 -0
- package/src/prove/isabelle/pure/rewriter.ts +602 -0
- package/src/prove/isabelle/pure/search.ts +68 -0
- package/src/prove/isabelle/pure/seq.ts +156 -0
- package/src/prove/isabelle/pure/simplifier.ts +112 -0
- package/src/prove/isabelle/pure/table.ts +208 -0
- package/src/prove/isabelle/pure/tactic.ts +276 -0
- package/src/prove/isabelle/pure/tactical.ts +347 -0
- package/src/prove/isabelle/pure/term-ord.ts +159 -0
- package/src/prove/isabelle/pure/term.ts +808 -0
- package/src/prove/isabelle/pure/thm.ts +806 -0
- package/src/prove/isabelle/pure/type.ts +133 -0
- package/src/prove/isabelle/pure/unify.ts +753 -0
- package/src/prove/isabelle/pure/variable.ts +25 -0
- package/src/prove/isabelle/tla/classical-setup.ts +101 -0
- package/src/prove/isabelle/tla/context.ts +74 -0
- package/src/prove/isabelle/tla/logic-data.ts +5 -0
- package/src/prove/isabelle/tla/logic.ts +215 -0
- package/src/prove/isabelle/tla/simpdata.ts +241 -0
- package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
- package/src/prove/ocaml.ts +22 -0
- package/src/prove/prep/action.ts +508 -0
- package/src/prove/prep/defs.ts +33 -0
- package/src/prove/prep/elab.ts +156 -0
- package/src/prove/prep/enabled-axioms.ts +189 -0
- package/src/prove/prep/enabled.ts +43 -0
- package/src/prove/prep/fingerprint.ts +388 -0
- package/src/prove/prep/flatten.ts +70 -0
- package/src/prove/prep/level-comparison.ts +529 -0
- package/src/prove/prep/md5.ts +67 -0
- package/src/prove/prep/pragma.ts +221 -0
- package/src/prove/prep/trivial.ts +22 -0
- package/src/prove/prove-worker.ts +22 -0
- package/src/prove/prove.ts +498 -0
- package/src/prove/ptl/decide.ts +68 -0
- package/src/prove/ptl/encode.ts +255 -0
- package/src/prove/ptl/sat.ts +151 -0
- package/src/prove/ptl/tableau.ts +276 -0
- package/src/prove/smt/axiomatize.ts +69 -0
- package/src/prove/smt/axioms.ts +209 -0
- package/src/prove/smt/context.ts +108 -0
- package/src/prove/smt/data.ts +636 -0
- package/src/prove/smt/encode.ts +164 -0
- package/src/prove/smt/extensionality.ts +65 -0
- package/src/prove/smt/flatten.ts +112 -0
- package/src/prove/smt/format.ts +154 -0
- package/src/prove/smt/instances.ts +181 -0
- package/src/prove/smt/rewrite.ts +772 -0
- package/src/prove/smt/simplify.ts +57 -0
- package/src/prove/smt/solver/act-cache.ts +265 -0
- package/src/prove/smt/solver/arith-rewriter.ts +962 -0
- package/src/prove/smt/solver/array.ts +22 -0
- package/src/prove/smt/solver/asserted-formulas.ts +469 -0
- package/src/prove/smt/solver/ast-lt.ts +157 -0
- package/src/prove/smt/solver/ast-util.ts +147 -0
- package/src/prove/smt/solver/ast.ts +1315 -0
- package/src/prove/smt/solver/auf-solver.ts +572 -0
- package/src/prove/smt/solver/case-split-queue.ts +178 -0
- package/src/prove/smt/solver/cg-table.ts +175 -0
- package/src/prove/smt/solver/clause.ts +276 -0
- package/src/prove/smt/solver/conflict-resolution.ts +343 -0
- package/src/prove/smt/solver/context.ts +1292 -0
- package/src/prove/smt/solver/ctx-simplify.ts +334 -0
- package/src/prove/smt/solver/der.ts +224 -0
- package/src/prove/smt/solver/dyn-ack.ts +141 -0
- package/src/prove/smt/solver/e-matching.ts +659 -0
- package/src/prove/smt/solver/enode.ts +259 -0
- package/src/prove/smt/solver/expr-ref.ts +21 -0
- package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
- package/src/prove/smt/solver/fingerprints.ts +92 -0
- package/src/prove/smt/solver/flat-literals.ts +51 -0
- package/src/prove/smt/solver/goal.ts +138 -0
- package/src/prove/smt/solver/hash.ts +173 -0
- package/src/prove/smt/solver/hashtable.ts +166 -0
- package/src/prove/smt/solver/internalizer.ts +1381 -0
- package/src/prove/smt/solver/lbool.ts +5 -0
- package/src/prove/smt/solver/literal.ts +24 -0
- package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
- package/src/prove/smt/solver/lp/constraints.ts +78 -0
- package/src/prove/smt/solver/lp/containers.ts +296 -0
- package/src/prove/smt/solver/lp/core-solver.ts +504 -0
- package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
- package/src/prove/smt/solver/lp/dep-intervals.ts +656 -0
- package/src/prove/smt/solver/lp/dependency.ts +51 -0
- package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
- package/src/prove/smt/solver/lp/explanation.ts +46 -0
- package/src/prove/smt/solver/lp/gomory.ts +255 -0
- package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
- package/src/prove/smt/solver/lp/hnf.ts +281 -0
- package/src/prove/smt/solver/lp/int-cube.ts +295 -0
- package/src/prove/smt/solver/lp/int-solver.ts +514 -0
- package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
- package/src/prove/smt/solver/lp/lar-term.ts +62 -0
- package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
- package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
- package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
- package/src/prove/smt/solver/lp/nex.ts +120 -0
- package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
- package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
- package/src/prove/smt/solver/lp/nla-core.ts +968 -0
- package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
- package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
- package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
- package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
- package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
- package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
- package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
- package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
- package/src/prove/smt/solver/lp/nla-order.ts +307 -0
- package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
- package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
- package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
- package/src/prove/smt/solver/lp/nla-types.ts +124 -0
- package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
- package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
- package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
- package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
- package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
- package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
- package/src/prove/smt/solver/lp/pdd.ts +971 -0
- package/src/prove/smt/solver/lp/random-updater.ts +138 -0
- package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
- package/src/prove/smt/solver/lp/union-find.ts +96 -0
- package/src/prove/smt/solver/mam-compiler.ts +847 -0
- package/src/prove/smt/solver/mam-interpreter.ts +400 -0
- package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
- package/src/prove/smt/solver/model-checker.ts +347 -0
- package/src/prove/smt/solver/model-evaluator.ts +262 -0
- package/src/prove/smt/solver/model-finder.ts +155 -0
- package/src/prove/smt/solver/model-generator.ts +97 -0
- package/src/prove/smt/solver/model-macro-solver.ts +728 -0
- package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
- package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
- package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
- package/src/prove/smt/solver/nlsat/explain.ts +607 -0
- package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
- package/src/prove/smt/solver/nlsat/levelwise.ts +740 -0
- package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
- package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
- package/src/prove/smt/solver/nlsat/solver.ts +911 -0
- package/src/prove/smt/solver/nlsat/types.ts +43 -0
- package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
- package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
- package/src/prove/smt/solver/nnf.ts +469 -0
- package/src/prove/smt/solver/pattern-inference.ts +351 -0
- package/src/prove/smt/solver/poly-rewriter.ts +477 -0
- package/src/prove/smt/solver/propagate-values.ts +83 -0
- package/src/prove/smt/solver/proto-model.ts +551 -0
- package/src/prove/smt/solver/pull-quant.ts +139 -0
- package/src/prove/smt/solver/qe-lite-der.ts +526 -0
- package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
- package/src/prove/smt/solver/qe-lite.ts +381 -0
- package/src/prove/smt/solver/qi-queue.ts +362 -0
- package/src/prove/smt/solver/quant-tactics.ts +117 -0
- package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
- package/src/prove/smt/solver/quantifier-manager.ts +186 -0
- package/src/prove/smt/solver/random-gen.ts +10 -0
- package/src/prove/smt/solver/rational.ts +165 -0
- package/src/prove/smt/solver/ref-vector.ts +62 -0
- package/src/prove/smt/solver/relevancy.ts +247 -0
- package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
- package/src/prove/smt/solver/rewriter.ts +843 -0
- package/src/prove/smt/solver/rlimit.ts +79 -0
- package/src/prove/smt/solver/smtlib-parser.ts +526 -0
- package/src/prove/smt/solver/solver.ts +71 -0
- package/src/prove/smt/solver/sort.ts +35 -0
- package/src/prove/smt/solver/sticky-generations.ts +69 -0
- package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
- package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
- package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
- package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
- package/src/prove/smt/solver/theory-lra-model.ts +243 -0
- package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
- package/src/prove/smt/solver/theory-lra-state.ts +43 -0
- package/src/prove/smt/solver/theory-lra.ts +56 -0
- package/src/prove/smt/solver/var-subst.ts +258 -0
- package/src/prove/smt/solver/z3-license.txt +10 -0
- package/src/prove/smt/table.ts +170 -0
- package/src/prove/tlapm/tlapm.ts +96 -0
- package/src/prove/tlaps-module.ts +510 -0
- package/src/prove/verdict.ts +18 -0
- package/src/prove/zenon/LICENSE +32 -0
- package/src/prove/zenon/check/check.ts +105 -0
- package/src/prove/zenon/check/definitions.ts +144 -0
- package/src/prove/zenon/check/equality.ts +182 -0
- package/src/prove/zenon/check/functions.ts +62 -0
- package/src/prove/zenon/check/keys.ts +125 -0
- package/src/prove/zenon/check/propositional.ts +104 -0
- package/src/prove/zenon/check/records.ts +188 -0
- package/src/prove/zenon/check/relations.ts +121 -0
- package/src/prove/zenon/check/rewrites.ts +224 -0
- package/src/prove/zenon/check/sets.ts +302 -0
- package/src/prove/zenon/check/terms.ts +127 -0
- package/src/prove/zenon/check/tla.ts +126 -0
- package/src/prove/zenon/decide.ts +113 -0
- package/src/prove/zenon/encode.ts +379 -0
- package/src/prove/zenon/eqrel.ts +300 -0
- package/src/prove/zenon/equality-rules.ts +435 -0
- package/src/prove/zenon/expr.ts +551 -0
- package/src/prove/zenon/index-table.ts +350 -0
- package/src/prove/zenon/isar-case.ts +513 -0
- package/src/prove/zenon/isar.ts +1249 -0
- package/src/prove/zenon/limits.ts +15 -0
- package/src/prove/zenon/llproof.ts +173 -0
- package/src/prove/zenon/mltoll.ts +1566 -0
- package/src/prove/zenon/node.ts +297 -0
- package/src/prove/zenon/ocaml.ts +169 -0
- package/src/prove/zenon/parse.ts +247 -0
- package/src/prove/zenon/phrase.ts +191 -0
- package/src/prove/zenon/proof.ts +198 -0
- package/src/prove/zenon/propositional-rules.ts +387 -0
- package/src/prove/zenon/rules.ts +94 -0
- package/src/prove/zenon/search.ts +288 -0
- package/src/prove/zenon/tla/functions.ts +151 -0
- package/src/prove/zenon/tla/llproof.ts +480 -0
- package/src/prove/zenon/tla/records.ts +225 -0
- package/src/prove/zenon/tla/rewrites.ts +447 -0
- package/src/prove/zenon/tla/sets.ts +304 -0
- package/src/prove/zenon/tla/terms.ts +181 -0
- package/src/prove/zenon/tla/tla.ts +506 -0
- package/src/run-cli.ts +283 -81
- package/src/standard-modules.ts +5 -2
- package/src/successors.ts +24 -5
- package/src/tlapm-cache.ts +236 -0
- package/src/tlc-random.ts +55 -32
- package/src/tlc-version.ts +8 -7
- package/src/values.ts +97 -47
- package/src/worker.ts +65 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
|
@@ -0,0 +1,971 @@
|
|
|
1
|
+
// Ported from Z3 (src/math/dd/dd_pdd.h, src/math/dd/dd_pdd.cpp, src/math/dd/pdd_eval.h), MIT
|
|
2
|
+
// license, see z3-license.txt. Polynomial decision diagrams over the rationals, which nla's Grobner
|
|
3
|
+
// solver works on: a node is x*hi + lo for the variable x at its level, lo has no x, and the leaves
|
|
4
|
+
// are values. Only the free semantics is ported, the one nla and dd::solver use: not mod2, mod2N or
|
|
5
|
+
// zero_one_vars, nor what only those or polysat use (the boolean operations, substitution of
|
|
6
|
+
// values, resolve, quot_rem, and division without the inverse). Nodes are never freed, so there is
|
|
7
|
+
// no garbage collection: node indices match Z3's until its first collection, and the node limit
|
|
8
|
+
// counts every node made. The op cache keys on both operands, where Z3's eq_entry compares the
|
|
9
|
+
// first operand of one entry with the second of the other.
|
|
10
|
+
import { PddMemoryOutError } from "../../../../errors";
|
|
11
|
+
import { getGcd, Rational } from "../rational";
|
|
12
|
+
import { shrink } from "../array";
|
|
13
|
+
|
|
14
|
+
const ZERO_PDD = 0;
|
|
15
|
+
const ONE_PDD = 1;
|
|
16
|
+
const ADD_OP = 2;
|
|
17
|
+
const SUB_OP = 3;
|
|
18
|
+
const MINUS_OP = 4;
|
|
19
|
+
const MUL_OP = 5;
|
|
20
|
+
const REDUCE_OP = 6;
|
|
21
|
+
// Nodes below this index stand for the operations in the op cache, besides 0 and 1.
|
|
22
|
+
const NO_OP = 10;
|
|
23
|
+
const INITIAL_FREE_NODES = 1024;
|
|
24
|
+
|
|
25
|
+
interface PddMonomial {
|
|
26
|
+
coeff: Rational;
|
|
27
|
+
vars: number[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface PddCoefficient {
|
|
31
|
+
coeff: Rational;
|
|
32
|
+
isConstant: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface PddPower {
|
|
36
|
+
var: number;
|
|
37
|
+
degree: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface PddFactor {
|
|
41
|
+
lc: Pdd;
|
|
42
|
+
rest: Pdd;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface PddVarFactors {
|
|
46
|
+
vars: number[];
|
|
47
|
+
poly: Pdd;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface IteratorNode {
|
|
51
|
+
isFirst: boolean;
|
|
52
|
+
node: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface CommonFactors {
|
|
56
|
+
first: number[];
|
|
57
|
+
second: number[];
|
|
58
|
+
firstCoeff: Rational;
|
|
59
|
+
secondCoeff: Rational;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class PddManager {
|
|
63
|
+
private levels: number[] = [];
|
|
64
|
+
private los: number[] = [];
|
|
65
|
+
private his: number[] = [];
|
|
66
|
+
private values: Rational[] = [];
|
|
67
|
+
private nodeTable = new Map<string, number>();
|
|
68
|
+
private valueTable = new Map<string, number>();
|
|
69
|
+
private opCache = new Map<string, number>();
|
|
70
|
+
private factorCache = new Map<string, PddFactor>();
|
|
71
|
+
private degrees: number[] = [];
|
|
72
|
+
private numAllocated = 0;
|
|
73
|
+
private maxNumNodes = 1 << 24;
|
|
74
|
+
private isApplying = false;
|
|
75
|
+
var2pdd: number[] = [];
|
|
76
|
+
var2level: number[] = [];
|
|
77
|
+
level2var: number[] = [];
|
|
78
|
+
|
|
79
|
+
constructor(numVars: number) {
|
|
80
|
+
this.initNodes(Array.from({ length: numVars }, (_, index) => index));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
reset(level2var: number[]) {
|
|
84
|
+
this.levels = [];
|
|
85
|
+
this.los = [];
|
|
86
|
+
this.his = [];
|
|
87
|
+
this.values = [];
|
|
88
|
+
this.nodeTable.clear();
|
|
89
|
+
this.valueTable.clear();
|
|
90
|
+
this.opCache.clear();
|
|
91
|
+
this.factorCache.clear();
|
|
92
|
+
this.degrees = [];
|
|
93
|
+
this.initNodes(level2var);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private initNodes(level2var: number[]) {
|
|
97
|
+
for (let index = 0; index < NO_OP; index++) {
|
|
98
|
+
this.levels.push(0);
|
|
99
|
+
this.los.push(0);
|
|
100
|
+
this.his.push(0);
|
|
101
|
+
}
|
|
102
|
+
this.los[ONE_PDD] = 1;
|
|
103
|
+
this.values.push(Rational.zero, Rational.one);
|
|
104
|
+
this.numAllocated = NO_OP + INITIAL_FREE_NODES + level2var.length;
|
|
105
|
+
this.var2pdd = [];
|
|
106
|
+
this.var2level = [];
|
|
107
|
+
this.level2var = [];
|
|
108
|
+
for (let level = 0; level < level2var.length; level++) {
|
|
109
|
+
const variable = level2var[level];
|
|
110
|
+
this.var2pdd[variable] = this.makeNode(level, ZERO_PDD, ONE_PDD);
|
|
111
|
+
this.var2level[variable] = level;
|
|
112
|
+
this.level2var[level] = variable;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setMaxNumNodes(limit: number) {
|
|
117
|
+
this.maxNumNodes = limit + this.level2var.length;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
numNodes() {
|
|
121
|
+
return this.levels.length;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
numVars() {
|
|
125
|
+
return this.var2pdd.length;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
isVal(node: number) {
|
|
129
|
+
return this.his[node] === 0;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
level(node: number) {
|
|
133
|
+
return this.levels[node];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// A value node is at level 0, so its var is the lowest variable, as in Z3.
|
|
137
|
+
var(node: number) {
|
|
138
|
+
return this.level2var[this.levels[node]];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
lo(node: number) {
|
|
142
|
+
return this.los[node];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
hi(node: number) {
|
|
146
|
+
return this.his[node];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
val(node: number) {
|
|
150
|
+
return this.values[this.los[node]];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
offset(node: number) {
|
|
154
|
+
let current = node;
|
|
155
|
+
while (!this.isVal(current)) current = this.lo(current);
|
|
156
|
+
return this.val(current);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
mkVar(variable: number) {
|
|
160
|
+
while (this.var2level.length <= variable) {
|
|
161
|
+
const next = this.var2level.length;
|
|
162
|
+
this.var2pdd.push(this.makeNode(next, ZERO_PDD, ONE_PDD));
|
|
163
|
+
this.var2level.push(next);
|
|
164
|
+
this.level2var.push(next);
|
|
165
|
+
}
|
|
166
|
+
return new Pdd(this, this.var2pdd[variable]);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
mkVal(value: Rational) {
|
|
170
|
+
return new Pdd(this, this.imkVal(value));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
zero() {
|
|
174
|
+
return new Pdd(this, ZERO_PDD);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
one() {
|
|
178
|
+
return new Pdd(this, ONE_PDD);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
add(first: Pdd, second: Pdd) {
|
|
182
|
+
return new Pdd(this, this.apply(first.root, second.root, ADD_OP));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
sub(first: Pdd, second: Pdd) {
|
|
186
|
+
return new Pdd(this, this.apply(first.root, second.root, SUB_OP));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
mul(first: Pdd, second: Pdd) {
|
|
190
|
+
return new Pdd(this, this.apply(first.root, second.root, MUL_OP));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
reduce(first: Pdd, second: Pdd) {
|
|
194
|
+
return new Pdd(this, this.apply(first.root, second.root, REDUCE_OP));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
minus(poly: Pdd) {
|
|
198
|
+
return new Pdd(
|
|
199
|
+
this,
|
|
200
|
+
this.whileApplying(() => this.minusRec(poly.root)),
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
pow(poly: Pdd, exponent: number): Pdd {
|
|
205
|
+
if (exponent === 0) return this.one();
|
|
206
|
+
if (exponent === 1 || poly.isZero() || poly.isOne()) return poly;
|
|
207
|
+
if (poly.isVal()) return this.mkVal(poly.val().pow(exponent));
|
|
208
|
+
// j even: pow(p,2*j') = pow(p*p,j'); j odd: pow(p,2*j'+1) = p*pow(p*p,j')
|
|
209
|
+
const half = this.pow(this.mul(poly, poly), Math.floor(exponent / 2));
|
|
210
|
+
return exponent % 2 === 1 ? this.mul(half, poly) : half;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// apply: operations grow the node limit where Z3 catches mem_out, collects garbage, retries and
|
|
214
|
+
// then doubles the limit. Without garbage to collect, only the doubling matters.
|
|
215
|
+
private apply(first: number, second: number, op: number) {
|
|
216
|
+
return this.whileApplying(() => this.applyRec(first, second, op));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private whileApplying(compute: () => number) {
|
|
220
|
+
this.isApplying = true;
|
|
221
|
+
try {
|
|
222
|
+
return compute();
|
|
223
|
+
} finally {
|
|
224
|
+
this.isApplying = false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private applyRec(first: number, second: number, op: number): number {
|
|
229
|
+
switch (op) {
|
|
230
|
+
case SUB_OP:
|
|
231
|
+
if (second === ZERO_PDD) return first;
|
|
232
|
+
if (this.isVal(first) && this.isVal(second)) {
|
|
233
|
+
return this.imkVal(this.val(first).sub(this.val(second)));
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
case ADD_OP:
|
|
237
|
+
if (first === ZERO_PDD) return second;
|
|
238
|
+
if (second === ZERO_PDD) return first;
|
|
239
|
+
if (this.isVal(first) && this.isVal(second)) {
|
|
240
|
+
return this.imkVal(this.val(first).add(this.val(second)));
|
|
241
|
+
}
|
|
242
|
+
if (this.isVal(first) || (!this.isVal(second) && this.level(first) < this.level(second))) {
|
|
243
|
+
return this.applyRec(second, first, op);
|
|
244
|
+
}
|
|
245
|
+
break;
|
|
246
|
+
case MUL_OP:
|
|
247
|
+
if (first === ZERO_PDD || second === ZERO_PDD) return ZERO_PDD;
|
|
248
|
+
if (first === ONE_PDD) return second;
|
|
249
|
+
if (second === ONE_PDD) return first;
|
|
250
|
+
if (this.isVal(first) && this.isVal(second)) {
|
|
251
|
+
return this.imkVal(this.val(first).mul(this.val(second)));
|
|
252
|
+
}
|
|
253
|
+
if (this.isVal(first) || (!this.isVal(second) && this.level(first) < this.level(second))) {
|
|
254
|
+
return this.applyRec(second, first, op);
|
|
255
|
+
}
|
|
256
|
+
break;
|
|
257
|
+
case REDUCE_OP:
|
|
258
|
+
if (second === ZERO_PDD || this.isVal(first)) return first;
|
|
259
|
+
if (this.degree(first) < this.degree(second)) return first;
|
|
260
|
+
if (this.level(this.firstLeading(second)) > this.level(first)) return first;
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
const key = `${op},${first},${second}`;
|
|
264
|
+
const cached = this.opCache.get(key);
|
|
265
|
+
if (cached !== undefined) return cached;
|
|
266
|
+
const result = this.computeApply(first, second, op);
|
|
267
|
+
this.opCache.set(key, result);
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private computeApply(first: number, second: number, op: number) {
|
|
272
|
+
const firstLevel = this.level(first);
|
|
273
|
+
const secondLevel = this.level(second);
|
|
274
|
+
const firstLo = this.lo(first);
|
|
275
|
+
const firstHi = this.hi(first);
|
|
276
|
+
switch (op) {
|
|
277
|
+
case ADD_OP: {
|
|
278
|
+
if (this.isVal(second) || firstLevel !== secondLevel) {
|
|
279
|
+
return this.makeNode(firstLevel, this.applyRec(firstLo, second, op), firstHi);
|
|
280
|
+
}
|
|
281
|
+
const lo = this.applyRec(firstLo, this.lo(second), op);
|
|
282
|
+
return this.makeNode(firstLevel, lo, this.applyRec(firstHi, this.hi(second), op));
|
|
283
|
+
}
|
|
284
|
+
case SUB_OP: {
|
|
285
|
+
if (this.isVal(first) || (!this.isVal(second) && firstLevel < secondLevel)) {
|
|
286
|
+
// p - (ax + b) = -ax + (p - b)
|
|
287
|
+
const lo = this.applyRec(first, this.lo(second), op);
|
|
288
|
+
return this.makeNode(secondLevel, lo, this.minusRec(this.hi(second)));
|
|
289
|
+
}
|
|
290
|
+
if (this.isVal(second) || firstLevel > secondLevel) {
|
|
291
|
+
// (ax + b) - k = ax + (b - k)
|
|
292
|
+
return this.makeNode(firstLevel, this.applyRec(firstLo, second, op), firstHi);
|
|
293
|
+
}
|
|
294
|
+
// (ax + b) - (cx + d) = (a - c)x + (b - d)
|
|
295
|
+
const lo = this.applyRec(firstLo, this.lo(second), op);
|
|
296
|
+
return this.makeNode(firstLevel, lo, this.applyRec(firstHi, this.hi(second), op));
|
|
297
|
+
}
|
|
298
|
+
case MUL_OP: {
|
|
299
|
+
if (this.isVal(second) || firstLevel !== secondLevel) {
|
|
300
|
+
// (x*hi(p)+lo(p))*b = x*hi(p)*b + lo(p)*b
|
|
301
|
+
const lo = this.applyRec(firstLo, second, op);
|
|
302
|
+
return this.makeNode(firstLevel, lo, this.applyRec(firstHi, second, op));
|
|
303
|
+
}
|
|
304
|
+
// (xa + b)(xc + d) = x(x*ac + ad + bc) + bd, where ad + bc may be x*e + f, so that the
|
|
305
|
+
// hi child is x*(ac + e) + f.
|
|
306
|
+
const ac = this.applyRec(firstHi, this.hi(second), op);
|
|
307
|
+
const ad = this.applyRec(firstHi, this.lo(second), op);
|
|
308
|
+
const bc = this.applyRec(firstLo, this.hi(second), op);
|
|
309
|
+
const bd = this.applyRec(firstLo, this.lo(second), op);
|
|
310
|
+
const sum = this.applyRec(ad, bc, ADD_OP);
|
|
311
|
+
if (!this.isVal(sum) && this.level(sum) === firstLevel) {
|
|
312
|
+
const hi = this.applyRec(ac, this.hi(sum), ADD_OP);
|
|
313
|
+
return this.makeNode(firstLevel, bd, this.makeNode(firstLevel, this.lo(sum), hi));
|
|
314
|
+
}
|
|
315
|
+
return this.makeNode(firstLevel, bd, this.makeNode(firstLevel, sum, ac));
|
|
316
|
+
}
|
|
317
|
+
default: {
|
|
318
|
+
if (this.level(this.firstLeading(second)) >= firstLevel) {
|
|
319
|
+
return this.reduceOnMatch(first, second);
|
|
320
|
+
}
|
|
321
|
+
const lo = this.applyRec(firstLo, second, op);
|
|
322
|
+
const hi = this.applyRec(firstHi, second, op);
|
|
323
|
+
if (lo === firstLo && hi === firstHi) return first;
|
|
324
|
+
if (this.level(lo) < firstLevel && this.level(hi) <= firstLevel) {
|
|
325
|
+
return this.makeNode(firstLevel, lo, hi);
|
|
326
|
+
}
|
|
327
|
+
const product = this.applyRec(hi, this.var2pdd[this.var(first)], MUL_OP);
|
|
328
|
+
return this.applyRec(product, lo, ADD_OP);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private minusRec(node: number): number {
|
|
334
|
+
if (node === ZERO_PDD) return ZERO_PDD;
|
|
335
|
+
if (this.isVal(node)) return this.imkVal(this.val(node).neg());
|
|
336
|
+
const key = `${MINUS_OP},${node},${node}`;
|
|
337
|
+
const cached = this.opCache.get(key);
|
|
338
|
+
if (cached !== undefined) return cached;
|
|
339
|
+
const lo = this.minusRec(this.lo(node));
|
|
340
|
+
const result = this.makeNode(this.level(node), lo, this.minusRec(this.hi(node)));
|
|
341
|
+
this.opCache.set(key, result);
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// reduce_on_match: the monomials of a that lm(b) divides become (b - lt(b))/lm(b).
|
|
346
|
+
private reduceOnMatch(first: number, second: number) {
|
|
347
|
+
let current = first;
|
|
348
|
+
while (this.lmOccurs(second, current)) {
|
|
349
|
+
const product = this.applyRec(this.ltQuotient(second, current), second, MUL_OP);
|
|
350
|
+
current = this.applyRec(current, product, ADD_OP);
|
|
351
|
+
}
|
|
352
|
+
return current;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// lm_occurs: whether the leading monomial of p divides a monomial of q.
|
|
356
|
+
private lmOccurs(first: number, second: number): boolean {
|
|
357
|
+
let divisor = this.firstLeading(first);
|
|
358
|
+
let target = second;
|
|
359
|
+
for (;;) {
|
|
360
|
+
if (this.isVal(divisor)) return true;
|
|
361
|
+
if (this.isVal(target)) return false;
|
|
362
|
+
if (this.level(divisor) > this.level(target)) return false;
|
|
363
|
+
if (this.level(divisor) === this.level(target)) {
|
|
364
|
+
divisor = this.nextLeading(divisor);
|
|
365
|
+
target = this.hi(target);
|
|
366
|
+
} else if (this.lmOccurs(divisor, this.hi(target))) {
|
|
367
|
+
return true;
|
|
368
|
+
} else {
|
|
369
|
+
target = this.lo(target);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// lt_quotient: minus the r such that lt(p)*r is a monomial of q, given lm_occurs(p, q).
|
|
375
|
+
private ltQuotient(first: number, second: number): number {
|
|
376
|
+
let divisor = this.firstLeading(first);
|
|
377
|
+
let target = second;
|
|
378
|
+
while (!this.isVal(divisor)) {
|
|
379
|
+
if (this.level(divisor) === this.level(target)) {
|
|
380
|
+
divisor = this.nextLeading(divisor);
|
|
381
|
+
target = this.lmOccurs(divisor, this.hi(target)) ? this.hi(target) : this.lo(target);
|
|
382
|
+
} else if (this.lmOccurs(divisor, this.hi(target))) {
|
|
383
|
+
return this.ltQuotientHi(divisor, target);
|
|
384
|
+
} else {
|
|
385
|
+
target = this.lo(target);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (this.isVal(target)) return this.imkVal(this.val(target).neg().div(this.val(divisor)));
|
|
389
|
+
return this.ltQuotientHi(divisor, target);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private ltQuotientHi(first: number, second: number) {
|
|
393
|
+
const quotient = this.ltQuotient(first, this.hi(second));
|
|
394
|
+
return this.applyRec(this.var2pdd[this.var(second)], quotient, MUL_OP);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// try_spoly: a*q*qc - b*p*pc, where p = lcm(lm(a),lm(b))/lm(a), q = lcm(lm(a),lm(b))/lm(b) and pc
|
|
398
|
+
// and qc are the leading coefficients of a and b, when the leading monomials share a variable.
|
|
399
|
+
trySpoly(first: Pdd, second: Pdd) {
|
|
400
|
+
const factors = this.commonFactors(first, second);
|
|
401
|
+
if (factors === null) return null;
|
|
402
|
+
let firstMultiplier = this.mkVal(factors.secondCoeff);
|
|
403
|
+
for (let index = factors.second.length; index-- > 0;) {
|
|
404
|
+
firstMultiplier = firstMultiplier.mul(this.mkVar(factors.second[index]));
|
|
405
|
+
}
|
|
406
|
+
let secondMultiplier = this.mkVal(factors.firstCoeff.neg());
|
|
407
|
+
for (let index = factors.first.length; index-- > 0;) {
|
|
408
|
+
secondMultiplier = secondMultiplier.mul(this.mkVar(factors.first[index]));
|
|
409
|
+
}
|
|
410
|
+
return firstMultiplier.mul(first).add(secondMultiplier.mul(second));
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
private commonFactors(first: Pdd, second: Pdd): CommonFactors | null {
|
|
414
|
+
const firstVars: number[] = [];
|
|
415
|
+
const secondVars: number[] = [];
|
|
416
|
+
let firstNode = this.firstLeading(first.root);
|
|
417
|
+
let secondNode = this.firstLeading(second.root);
|
|
418
|
+
let hasCommon = false;
|
|
419
|
+
for (;;) {
|
|
420
|
+
if (this.isVal(firstNode) || this.isVal(secondNode)) {
|
|
421
|
+
if (!hasCommon) return null;
|
|
422
|
+
while (!this.isVal(secondNode)) {
|
|
423
|
+
secondVars.push(this.var(secondNode));
|
|
424
|
+
secondNode = this.nextLeading(secondNode);
|
|
425
|
+
}
|
|
426
|
+
while (!this.isVal(firstNode)) {
|
|
427
|
+
firstVars.push(this.var(firstNode));
|
|
428
|
+
firstNode = this.nextLeading(firstNode);
|
|
429
|
+
}
|
|
430
|
+
const firstCoeff = this.val(firstNode);
|
|
431
|
+
const secondCoeff = this.val(secondNode);
|
|
432
|
+
if (!firstCoeff.isInt() || !secondCoeff.isInt()) {
|
|
433
|
+
return { first: firstVars, second: secondVars, firstCoeff, secondCoeff };
|
|
434
|
+
}
|
|
435
|
+
const gcd = Rational.of(getGcd(firstCoeff.num, secondCoeff.num));
|
|
436
|
+
return {
|
|
437
|
+
first: firstVars,
|
|
438
|
+
second: secondVars,
|
|
439
|
+
firstCoeff: firstCoeff.div(gcd),
|
|
440
|
+
secondCoeff: secondCoeff.div(gcd),
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
if (this.level(firstNode) === this.level(secondNode)) {
|
|
444
|
+
hasCommon = true;
|
|
445
|
+
firstNode = this.nextLeading(firstNode);
|
|
446
|
+
secondNode = this.nextLeading(secondNode);
|
|
447
|
+
} else if (this.level(firstNode) > this.level(secondNode)) {
|
|
448
|
+
firstVars.push(this.var(firstNode));
|
|
449
|
+
firstNode = this.nextLeading(firstNode);
|
|
450
|
+
} else {
|
|
451
|
+
secondVars.push(this.var(secondNode));
|
|
452
|
+
secondNode = this.nextLeading(secondNode);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// lm_lt: compares the leading monomials, then the monomials in degree-lex order.
|
|
458
|
+
lmLt(first: Pdd, second: Pdd) {
|
|
459
|
+
let firstNode = this.firstLeading(first.root);
|
|
460
|
+
let secondNode = this.firstLeading(second.root);
|
|
461
|
+
while (firstNode !== secondNode && !(this.isVal(firstNode) && this.isVal(secondNode))) {
|
|
462
|
+
if (this.isVal(firstNode)) return true;
|
|
463
|
+
if (this.isVal(secondNode)) return false;
|
|
464
|
+
if (this.level(firstNode) !== this.level(secondNode)) {
|
|
465
|
+
return this.level(firstNode) < this.level(secondNode);
|
|
466
|
+
}
|
|
467
|
+
firstNode = this.nextLeading(firstNode);
|
|
468
|
+
secondNode = this.nextLeading(secondNode);
|
|
469
|
+
}
|
|
470
|
+
const isDegreeLexGt = (left: number[], right: number[]) => {
|
|
471
|
+
if (left.length !== right.length) return left.length > right.length;
|
|
472
|
+
let index = 0;
|
|
473
|
+
while (index < left.length && left[index] === right[index]) index++;
|
|
474
|
+
return index < left.length && this.var2level[left[index]] > this.var2level[right[index]];
|
|
475
|
+
};
|
|
476
|
+
const compare = (left: number[], right: number[]) =>
|
|
477
|
+
isDegreeLexGt(left, right) ? -1 : isDegreeLexGt(right, left) ? 1 : 0;
|
|
478
|
+
const firstMonomials = [...first].map((monomial) => monomial.vars).sort(compare);
|
|
479
|
+
const secondMonomials = [...second].map((monomial) => monomial.vars).sort(compare);
|
|
480
|
+
const length = Math.min(firstMonomials.length, secondMonomials.length);
|
|
481
|
+
for (let index = 0; index < length; index++) {
|
|
482
|
+
if (isDegreeLexGt(secondMonomials[index], firstMonomials[index])) return true;
|
|
483
|
+
if (isDegreeLexGt(firstMonomials[index], secondMonomials[index])) return false;
|
|
484
|
+
}
|
|
485
|
+
return firstMonomials.length === length && secondMonomials.length > length;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
differentLeadingTerm(first: Pdd, second: Pdd) {
|
|
489
|
+
let firstNode = this.firstLeading(first.root);
|
|
490
|
+
let secondNode = this.firstLeading(second.root);
|
|
491
|
+
for (;;) {
|
|
492
|
+
if (firstNode === secondNode) return false;
|
|
493
|
+
if (this.isVal(firstNode) || this.isVal(secondNode)) return true;
|
|
494
|
+
if (this.level(firstNode) !== this.level(secondNode)) return true;
|
|
495
|
+
firstNode = this.nextLeading(firstNode);
|
|
496
|
+
secondNode = this.nextLeading(secondNode);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// first_leading: the first node with a term of the leading monomial, the monomial of highest
|
|
501
|
+
// degree and then highest variables.
|
|
502
|
+
private firstLeading(node: number) {
|
|
503
|
+
let current = node;
|
|
504
|
+
while (
|
|
505
|
+
!this.isVal(current) &&
|
|
506
|
+
this.degree(this.hi(current)) + 1 < this.degree(this.lo(current))
|
|
507
|
+
) {
|
|
508
|
+
current = this.lo(current);
|
|
509
|
+
}
|
|
510
|
+
return current;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
private nextLeading(node: number) {
|
|
514
|
+
return this.firstLeading(this.hi(node));
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
degree(node: number): number {
|
|
518
|
+
if (node === ZERO_PDD || node === ONE_PDD) return 0;
|
|
519
|
+
const cached = this.degrees[node];
|
|
520
|
+
if (cached !== undefined) return cached;
|
|
521
|
+
const degree = this.isVal(node)
|
|
522
|
+
? 0
|
|
523
|
+
: Math.max(this.degree(this.lo(node)), this.degree(this.hi(node)) + 1);
|
|
524
|
+
this.degrees[node] = degree;
|
|
525
|
+
return degree;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// factor: p = lc*v^degree + rest, where the degree of v in rest is below degree.
|
|
529
|
+
factor(poly: Pdd, variable: number, degree: number): PddFactor {
|
|
530
|
+
const variableLevel = this.var2level[variable];
|
|
531
|
+
if (degree === 0 || this.level(poly.root) < variableLevel) {
|
|
532
|
+
return { lc: this.zero(), rest: poly };
|
|
533
|
+
}
|
|
534
|
+
const key = `${poly.root},${variable},${degree}`;
|
|
535
|
+
const cached = this.factorCache.get(key);
|
|
536
|
+
if (cached !== undefined) return cached;
|
|
537
|
+
const result = this.computeFactor(poly, variable, degree);
|
|
538
|
+
this.factorCache.set(key, result);
|
|
539
|
+
return result;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
private computeFactor(poly: Pdd, variable: number, degree: number): PddFactor {
|
|
543
|
+
const variableLevel = this.var2level[variable];
|
|
544
|
+
if (this.level(poly.root) > variableLevel) {
|
|
545
|
+
const node = this.mkVar(poly.var());
|
|
546
|
+
const hiFactor = this.factor(poly.hi(), variable, degree);
|
|
547
|
+
const loFactor = this.factor(poly.lo(), variable, degree);
|
|
548
|
+
const lc = hiFactor.lc.mul(node);
|
|
549
|
+
const rest = hiFactor.rest.mul(node);
|
|
550
|
+
return { lc: lc.add(loFactor.lc), rest: rest.add(loFactor.rest) };
|
|
551
|
+
}
|
|
552
|
+
let found = 0;
|
|
553
|
+
let lc = poly;
|
|
554
|
+
while (found < degree && !lc.isVal() && this.level(lc.root) === variableLevel) {
|
|
555
|
+
found++;
|
|
556
|
+
lc = lc.hi();
|
|
557
|
+
}
|
|
558
|
+
if (found < degree) return { lc: this.zero(), rest: poly };
|
|
559
|
+
let rest = this.zero();
|
|
560
|
+
let power = this.one();
|
|
561
|
+
let current = poly;
|
|
562
|
+
for (let index = 0; index < degree; index++) {
|
|
563
|
+
rest = rest.add(power.mul(current.lo()));
|
|
564
|
+
current = current.hi();
|
|
565
|
+
power = power.mul(this.mkVar(variable));
|
|
566
|
+
}
|
|
567
|
+
return { lc, rest };
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// is_binary: v1, x*v1 + v2 or x*v1 + y*v2 + v3, for values v1, v2 and v3.
|
|
571
|
+
isBinary(node: number) {
|
|
572
|
+
const lo = this.lo(node);
|
|
573
|
+
return (
|
|
574
|
+
this.isVal(node) ||
|
|
575
|
+
(this.isVal(this.hi(node)) &&
|
|
576
|
+
(this.isVal(lo) || (this.isVal(this.hi(lo)) && this.isVal(this.lo(lo)))))
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// get_powers: the variables with a power above 1. The var of a value node is the lowest
|
|
581
|
+
// variable, so Z3 counts a value under that variable as one more power of it.
|
|
582
|
+
getPowers(poly: Pdd) {
|
|
583
|
+
const powers: PddPower[] = [];
|
|
584
|
+
const marked = new Set<number>();
|
|
585
|
+
const todo = [poly.root];
|
|
586
|
+
for (let node = todo.pop(); node !== undefined; node = todo.pop()) {
|
|
587
|
+
if (this.isVal(node) || marked.has(node)) continue;
|
|
588
|
+
marked.add(node);
|
|
589
|
+
const variable = this.var(node);
|
|
590
|
+
let current = node;
|
|
591
|
+
let degree = 1;
|
|
592
|
+
while (!this.isVal(current) && this.var(this.hi(current)) === variable) {
|
|
593
|
+
degree++;
|
|
594
|
+
todo.push(this.lo(current));
|
|
595
|
+
current = this.hi(current);
|
|
596
|
+
}
|
|
597
|
+
if (degree > 1) powers.push({ var: variable, degree });
|
|
598
|
+
if (!this.isVal(current)) todo.push(this.hi(current), this.lo(current));
|
|
599
|
+
}
|
|
600
|
+
return powers;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// var_is_leaf: whether every node of v has values as children.
|
|
604
|
+
varIsLeaf(poly: Pdd, variable: number) {
|
|
605
|
+
const marked = new Set<number>();
|
|
606
|
+
const todo = [poly.root];
|
|
607
|
+
for (let node = todo.pop(); node !== undefined; node = todo.pop()) {
|
|
608
|
+
if (this.isVal(node) || marked.has(node)) continue;
|
|
609
|
+
marked.add(node);
|
|
610
|
+
if (this.var(node) === variable) {
|
|
611
|
+
if (!this.isVal(this.lo(node)) || !this.isVal(this.hi(node))) return false;
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
if (!marked.has(this.lo(node))) todo.push(this.lo(node));
|
|
615
|
+
if (!marked.has(this.hi(node))) todo.push(this.hi(node));
|
|
616
|
+
}
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
treeSize(poly: Pdd) {
|
|
621
|
+
const sizes = new Map<number, number>();
|
|
622
|
+
const getSize = (node: number): number => {
|
|
623
|
+
const cached = sizes.get(node);
|
|
624
|
+
if (cached !== undefined) return cached;
|
|
625
|
+
const size = this.isVal(node) ? 1 : 1 + getSize(this.lo(node)) + getSize(this.hi(node));
|
|
626
|
+
sizes.set(node, size);
|
|
627
|
+
return size;
|
|
628
|
+
};
|
|
629
|
+
return getSize(poly.root);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
freeVars(poly: Pdd) {
|
|
633
|
+
const vars: number[] = [];
|
|
634
|
+
const marked = new Set<number>();
|
|
635
|
+
const todo = [poly.root];
|
|
636
|
+
for (let node = todo.pop(); node !== undefined; node = todo.pop()) {
|
|
637
|
+
if (this.isVal(node) || marked.has(node)) continue;
|
|
638
|
+
const variableNode = this.var2pdd[this.var(node)];
|
|
639
|
+
if (!marked.has(variableNode)) vars.push(this.var(node));
|
|
640
|
+
marked.add(node);
|
|
641
|
+
marked.add(variableNode);
|
|
642
|
+
if (!marked.has(this.lo(node))) todo.push(this.lo(node));
|
|
643
|
+
if (!marked.has(this.hi(node))) todo.push(this.hi(node));
|
|
644
|
+
}
|
|
645
|
+
return vars;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
private imkVal(value: Rational) {
|
|
649
|
+
if (value.isZero()) return ZERO_PDD;
|
|
650
|
+
if (value.equals(Rational.one)) return ONE_PDD;
|
|
651
|
+
const key = value.toString();
|
|
652
|
+
const existing = this.valueTable.get(key);
|
|
653
|
+
if (existing !== undefined) return existing;
|
|
654
|
+
const node = this.insertNode(0, this.values.length, 0);
|
|
655
|
+
this.values.push(value);
|
|
656
|
+
this.valueTable.set(key, node);
|
|
657
|
+
return node;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
private makeNode(level: number, lo: number, hi: number) {
|
|
661
|
+
return hi === ZERO_PDD ? lo : this.insertNode(level, lo, hi);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
// insert_node: Z3 adds half as many free nodes again when they run out, and is out of memory
|
|
665
|
+
// when the nodes already number more than the limit.
|
|
666
|
+
private insertNode(level: number, lo: number, hi: number) {
|
|
667
|
+
const key = `${level},${lo},${hi}`;
|
|
668
|
+
const existing = this.nodeTable.get(key);
|
|
669
|
+
if (existing !== undefined) return existing;
|
|
670
|
+
if (this.levels.length === this.numAllocated) {
|
|
671
|
+
if (this.numAllocated > this.maxNumNodes && !this.isApplying) throw new PddMemoryOutError();
|
|
672
|
+
while (this.numAllocated > this.maxNumNodes) this.maxNumNodes *= 2;
|
|
673
|
+
this.numAllocated += Math.floor(this.numAllocated / 2);
|
|
674
|
+
}
|
|
675
|
+
const node = this.levels.length;
|
|
676
|
+
this.levels.push(level);
|
|
677
|
+
this.los.push(lo);
|
|
678
|
+
this.his.push(hi);
|
|
679
|
+
this.nodeTable.set(key, node);
|
|
680
|
+
return node;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
export class Pdd {
|
|
685
|
+
constructor(
|
|
686
|
+
readonly manager: PddManager,
|
|
687
|
+
readonly root: number,
|
|
688
|
+
) {}
|
|
689
|
+
|
|
690
|
+
lo() {
|
|
691
|
+
return new Pdd(this.manager, this.manager.lo(this.root));
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
hi() {
|
|
695
|
+
return new Pdd(this.manager, this.manager.hi(this.root));
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
var() {
|
|
699
|
+
return this.manager.var(this.root);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
val() {
|
|
703
|
+
return this.manager.val(this.root);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
offset() {
|
|
707
|
+
return this.manager.offset(this.root);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
isVal() {
|
|
711
|
+
return this.manager.isVal(this.root);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
isZero() {
|
|
715
|
+
return this.root === ZERO_PDD;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
isOne() {
|
|
719
|
+
return this.root === ONE_PDD;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
isLinear() {
|
|
723
|
+
return this.manager.degree(this.root) === 1;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
isVar() {
|
|
727
|
+
return !this.isVal() && this.lo().isZero() && this.hi().isOne();
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// a*x for a value a.
|
|
731
|
+
isUnary() {
|
|
732
|
+
return !this.isVal() && this.lo().isZero() && this.hi().isVal();
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// x + b for a value b.
|
|
736
|
+
isOffset() {
|
|
737
|
+
return !this.isVal() && this.lo().isVal() && this.hi().isOne();
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
isBinary() {
|
|
741
|
+
return this.manager.isBinary(this.root);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
equals(other: Pdd) {
|
|
745
|
+
return this.root === other.root;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
neg() {
|
|
749
|
+
return this.manager.minus(this);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
add(other: Pdd | Rational) {
|
|
753
|
+
return other instanceof Rational
|
|
754
|
+
? this.manager.add(this.manager.mkVal(other), this)
|
|
755
|
+
: this.manager.add(this, other);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
sub(other: Pdd | Rational) {
|
|
759
|
+
return other instanceof Rational ? this.add(other.neg()) : this.manager.sub(this, other);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
mul(other: Pdd | Rational) {
|
|
763
|
+
return other instanceof Rational
|
|
764
|
+
? this.manager.mul(this.manager.mkVal(other), this)
|
|
765
|
+
: this.manager.mul(this, other);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// div: in the free semantics, multiplication by the inverse, which shares the op cache.
|
|
769
|
+
div(divisor: Rational) {
|
|
770
|
+
return this.mul(Rational.one.div(divisor));
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
pow(exponent: number) {
|
|
774
|
+
return this.manager.pow(this, exponent);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
reduce(other: Pdd) {
|
|
778
|
+
return this.manager.reduce(this, other);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
factor(variable: number, degree: number) {
|
|
782
|
+
return this.manager.factor(this, variable, degree);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
differentLeadingTerm(other: Pdd) {
|
|
786
|
+
return this.manager.differentLeadingTerm(this, other);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
degree() {
|
|
790
|
+
return this.manager.degree(this.root);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
treeSize() {
|
|
794
|
+
return this.manager.treeSize(this);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
freeVars() {
|
|
798
|
+
return this.manager.freeVars(this);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
varIsLeaf(variable: number) {
|
|
802
|
+
return this.manager.varIsLeaf(this, variable);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
getPowers() {
|
|
806
|
+
return this.manager.getPowers(this);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
// var_factors: the variables that divide every monomial, with their product divided out.
|
|
810
|
+
varFactors(): PddVarFactors {
|
|
811
|
+
if (this.isVal()) return { vars: [], poly: this };
|
|
812
|
+
const manager = this.manager;
|
|
813
|
+
const variable = this.var();
|
|
814
|
+
if (this.lo().isVal()) {
|
|
815
|
+
if (!this.lo().isZero()) return { vars: [], poly: this };
|
|
816
|
+
const factors = this.hi().varFactors();
|
|
817
|
+
factors.vars.push(variable);
|
|
818
|
+
return factors;
|
|
819
|
+
}
|
|
820
|
+
const loFactors = this.lo().varFactors();
|
|
821
|
+
const loVars = loFactors.vars;
|
|
822
|
+
if (loVars.length === 0) return { vars: [], poly: this };
|
|
823
|
+
const loAndHi: number[] = [];
|
|
824
|
+
// Moves the variables in both lists to loAndHi and keeps the rest in place, by level.
|
|
825
|
+
const merge = (hiVars: number[]) => {
|
|
826
|
+
let loKept = 0;
|
|
827
|
+
let hiKept = 0;
|
|
828
|
+
let loIndex = 0;
|
|
829
|
+
let hiIndex = 0;
|
|
830
|
+
while (loIndex < loVars.length || hiIndex < hiVars.length) {
|
|
831
|
+
if (loIndex === loVars.length) {
|
|
832
|
+
hiVars[hiKept++] = hiVars[hiIndex++];
|
|
833
|
+
} else if (hiIndex === hiVars.length) {
|
|
834
|
+
loVars[loKept++] = loVars[loIndex++];
|
|
835
|
+
} else if (loVars[loIndex] === hiVars[hiIndex]) {
|
|
836
|
+
loAndHi.push(loVars[loIndex]);
|
|
837
|
+
loIndex++;
|
|
838
|
+
hiIndex++;
|
|
839
|
+
} else if (manager.var2level[loVars[loIndex]] > manager.var2level[hiVars[hiIndex]]) {
|
|
840
|
+
hiVars[hiKept++] = hiVars[hiIndex++];
|
|
841
|
+
} else {
|
|
842
|
+
loVars[loKept++] = loVars[loIndex++];
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
shrink(loVars, loKept);
|
|
846
|
+
shrink(hiVars, hiKept);
|
|
847
|
+
};
|
|
848
|
+
const multiply = (vars: number[], poly: Pdd) =>
|
|
849
|
+
vars.reduce((product, innerVar) => product.mul(manager.mkVar(innerVar)), poly);
|
|
850
|
+
const hiFactors = this.hi().varFactors();
|
|
851
|
+
const hiVars = hiFactors.vars;
|
|
852
|
+
if (loVars[loVars.length - 1] === variable) {
|
|
853
|
+
loVars.pop();
|
|
854
|
+
merge(hiVars);
|
|
855
|
+
loAndHi.push(variable);
|
|
856
|
+
return {
|
|
857
|
+
vars: loAndHi,
|
|
858
|
+
poly: multiply(loVars, loFactors.poly).add(multiply(hiVars, hiFactors.poly)),
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
if (hiVars.length === 0) return { vars: [], poly: this };
|
|
862
|
+
merge(hiVars);
|
|
863
|
+
hiVars.push(variable);
|
|
864
|
+
if (loAndHi.length === 0) return { vars: [], poly: this };
|
|
865
|
+
return {
|
|
866
|
+
vars: loAndHi,
|
|
867
|
+
poly: multiply(loVars, loFactors.poly).add(multiply(hiVars, hiFactors.poly)),
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// subst_pdd: replaces the variable by the polynomial, without the op cache.
|
|
872
|
+
substPdd(variable: number, replacement: Pdd): Pdd {
|
|
873
|
+
const manager = this.manager;
|
|
874
|
+
if (this.isVal() || manager.var2level[this.var()] < manager.var2level[variable]) return this;
|
|
875
|
+
const lo = this.lo().substPdd(variable, replacement);
|
|
876
|
+
const hi = this.hi().substPdd(variable, replacement);
|
|
877
|
+
if (this.var() === variable) return replacement.mul(hi).add(lo);
|
|
878
|
+
if (lo.equals(this.lo()) && hi.equals(this.hi())) return this;
|
|
879
|
+
return manager.mkVar(this.var()).mul(hi).add(lo);
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// The monomials, hi before lo, each with its variables from the highest level down.
|
|
883
|
+
*[Symbol.iterator](): Generator<PddMonomial> {
|
|
884
|
+
const manager = this.manager;
|
|
885
|
+
const nodes: IteratorNode[] = [];
|
|
886
|
+
const vars: number[] = [];
|
|
887
|
+
let node = this.root;
|
|
888
|
+
while (!manager.isVal(node)) {
|
|
889
|
+
nodes.push({ isFirst: true, node });
|
|
890
|
+
vars.push(manager.var(node));
|
|
891
|
+
node = manager.hi(node);
|
|
892
|
+
}
|
|
893
|
+
if (nodes.length === 0) {
|
|
894
|
+
if (!manager.val(node).isZero()) yield { coeff: manager.val(node), vars: [] };
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
yield { coeff: manager.val(node), vars: [...vars] };
|
|
898
|
+
while (nodes.length > 0) {
|
|
899
|
+
const top = nodes[nodes.length - 1];
|
|
900
|
+
if (!top.isFirst) {
|
|
901
|
+
nodes.pop();
|
|
902
|
+
continue;
|
|
903
|
+
}
|
|
904
|
+
top.isFirst = false;
|
|
905
|
+
vars.pop();
|
|
906
|
+
let next = manager.lo(top.node);
|
|
907
|
+
if (next === ZERO_PDD) {
|
|
908
|
+
nodes.pop();
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
while (!manager.isVal(next)) {
|
|
912
|
+
nodes.push({ isFirst: true, node: next });
|
|
913
|
+
vars.push(manager.var(next));
|
|
914
|
+
next = manager.hi(next);
|
|
915
|
+
}
|
|
916
|
+
yield { coeff: manager.val(next), vars: [...vars] };
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// The coefficients, each marked as the constant term or not. Zero yields one constant 0.
|
|
921
|
+
*coefficients(): Generator<PddCoefficient> {
|
|
922
|
+
const manager = this.manager;
|
|
923
|
+
const nodes: number[] = [];
|
|
924
|
+
let node = this.root;
|
|
925
|
+
while (!manager.isVal(node)) {
|
|
926
|
+
nodes.push(node);
|
|
927
|
+
node = manager.hi(node);
|
|
928
|
+
}
|
|
929
|
+
yield { coeff: manager.val(node), isConstant: nodes.length === 0 };
|
|
930
|
+
for (let top = nodes.pop(); top !== undefined; top = nodes.pop()) {
|
|
931
|
+
let next = manager.lo(top);
|
|
932
|
+
if (next === ZERO_PDD) continue;
|
|
933
|
+
while (!manager.isVal(next)) {
|
|
934
|
+
nodes.push(next);
|
|
935
|
+
next = manager.hi(next);
|
|
936
|
+
}
|
|
937
|
+
yield { coeff: manager.val(next), isConstant: nodes.length === 0 };
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
toString() {
|
|
942
|
+
let text = "";
|
|
943
|
+
for (const monomial of this) {
|
|
944
|
+
if (text !== "") text += " ";
|
|
945
|
+
if (monomial.coeff.isNegative()) text += "- ";
|
|
946
|
+
else if (text !== "") text += "+ ";
|
|
947
|
+
const coeff = monomial.coeff.abs();
|
|
948
|
+
const vars = monomial.vars;
|
|
949
|
+
if (!coeff.equals(Rational.one) || vars.length === 0) {
|
|
950
|
+
text += vars.length === 0 ? `${coeff}` : `${coeff}*`;
|
|
951
|
+
}
|
|
952
|
+
const factors: string[] = [];
|
|
953
|
+
for (let start = 0; start < vars.length;) {
|
|
954
|
+
let end = start;
|
|
955
|
+
while (end < vars.length && vars[end] === vars[start]) end++;
|
|
956
|
+
factors.push(end - start > 1 ? `v${vars[start]}^${end - start}` : `v${vars[start]}`);
|
|
957
|
+
start = end;
|
|
958
|
+
}
|
|
959
|
+
text += factors.join("*");
|
|
960
|
+
}
|
|
961
|
+
return text === "" ? "0" : text;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
// pdd_eval: the value of the polynomial under the values of its variables.
|
|
966
|
+
export const evaluatePdd = (poly: Pdd, getValue: (variable: number) => Rational): Rational =>
|
|
967
|
+
poly.isVal()
|
|
968
|
+
? poly.val()
|
|
969
|
+
: evaluatePdd(poly.hi(), getValue)
|
|
970
|
+
.mul(getValue(poly.var()))
|
|
971
|
+
.add(evaluatePdd(poly.lo(), getValue));
|