tla-ts 0.0.4-dev.0be9567 → 0.0.4-dev.1a3acf6
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 +37 -126
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/deciders.cjs +366 -0
- package/dist/deciders.js +366 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +44 -12
- package/dist/index.d.ts +44 -12
- 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 +1006 -0
- package/dist/prove.js +1006 -0
- package/dist/run.cjs +45 -0
- package/dist/run.js +45 -0
- package/dist/tlapm.cjs +10 -0
- package/dist/tlapm.js +10 -0
- package/dist/transfer.cjs +76 -92
- package/dist/transfer.js +76 -92
- package/dist/worker.cjs +21 -0
- package/dist/worker.js +21 -0
- package/package.json +21 -12
- package/src/ast.ts +2 -2
- package/src/bags.ts +22 -23
- package/src/builtins.ts +16 -8
- package/src/check.ts +131 -1153
- package/src/{cli-options.ts → cli/options.ts} +54 -9
- package/src/cli/run.ts +63 -0
- package/src/cli/shared.ts +106 -0
- package/src/cli/tlapm.ts +374 -0
- package/src/{run-cli.ts → cli/tlc.ts} +115 -721
- package/src/cli/xml-exporter.ts +221 -0
- package/src/cli.ts +1 -1
- package/src/codegen.ts +17 -15
- package/src/config.ts +5 -5
- package/src/dependencies.ts +4 -6
- package/src/environment.ts +55 -60
- package/src/errors.ts +97 -35
- package/src/evaluate.ts +135 -120
- package/src/exit-codes.ts +1 -1
- package/src/fingerprints.ts +3 -3
- package/src/index.ts +10 -0
- package/src/instances.ts +56 -46
- package/src/java-overrides.ts +79 -41
- package/src/json.ts +31 -36
- package/src/levels.ts +10 -13
- package/src/{live-check.ts → liveness-checker.ts} +169 -142
- package/src/liveness.ts +38 -34
- package/src/{obligation-action.ts → obligations/action.ts} +248 -74
- package/src/{obligation-anon.ts → obligations/anonymize.ts} +55 -57
- package/src/obligations/equality.ts +260 -0
- package/src/obligations/expression.ts +618 -0
- package/src/{obligation-format.ts → obligations/format.ts} +7 -8
- package/src/obligations/generate.ts +457 -0
- package/src/{obligations.ts → obligations/index.ts} +46 -32
- package/src/{obligation-levels.ts → obligations/levels.ts} +44 -40
- package/src/obligations/module.ts +445 -0
- package/src/obligations/normalize.ts +554 -0
- package/src/obligations/operators.ts +176 -0
- package/src/obligations/pragma.ts +195 -0
- package/src/{obligation-printer.ts → obligations/printer.ts} +65 -230
- package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
- package/src/obligations/regroup.ts +100 -0
- package/src/{obligation-generate.ts → obligations/simplify.ts} +130 -536
- package/src/{obligation-source.ts → obligations/source.ts} +172 -216
- package/src/{obligation-deref.ts → obligations/subexpression.ts} +84 -115
- package/src/obligations/substitution.ts +261 -0
- package/src/obligations/temporal.ts +321 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
- package/src/parallel-worker.ts +39 -51
- package/src/parallel.ts +39 -66
- package/src/parser.ts +67 -74
- package/src/{pcal-ast.ts → pcal/ast.ts} +2 -17
- package/src/{pcal-parse.ts → pcal/parse.ts} +28 -64
- package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +10 -39
- package/src/{pcal-translate.ts → pcal/translate.ts} +30 -109
- package/src/{pcal-validate.ts → pcal/validate.ts} +10 -20
- package/src/prover/decide-pool.ts +95 -0
- package/src/prover/deciders.ts +33 -0
- package/src/prover/first-order.ts +175 -0
- package/src/prover/isabelle/LICENSE +36 -0
- package/src/prover/isabelle/LICENSE-tlapm +26 -0
- package/src/prover/isabelle/decide.ts +44 -0
- package/src/prover/isabelle/encode.ts +445 -0
- package/src/prover/isabelle/isar/inner-syntax.ts +579 -0
- package/src/prover/isabelle/isar/method.ts +367 -0
- package/src/prover/isabelle/isar/proof.ts +28 -0
- package/src/prover/isabelle/isar/statement.ts +57 -0
- package/src/prover/isabelle/isar/theory.ts +192 -0
- package/src/prover/isabelle/isar/type-check.ts +278 -0
- package/src/prover/isabelle/provers/blast/blast.ts +258 -0
- package/src/prover/isabelle/provers/blast/branch.ts +322 -0
- package/src/prover/isabelle/provers/blast/prove.ts +347 -0
- package/src/prover/isabelle/provers/blast/rules.ts +330 -0
- package/src/prover/isabelle/provers/blast/state.ts +180 -0
- package/src/prover/isabelle/provers/blast/term.ts +546 -0
- package/src/prover/isabelle/provers/clasimp.ts +150 -0
- package/src/prover/isabelle/provers/classical.ts +389 -0
- package/src/prover/isabelle/provers/hypsubst.ts +433 -0
- package/src/prover/isabelle/pure/bicompose.ts +174 -0
- package/src/prover/isabelle/pure/consts.ts +32 -0
- package/src/prover/isabelle/pure/conv.ts +79 -0
- package/src/prover/isabelle/pure/drule.ts +264 -0
- package/src/prover/isabelle/pure/envir.ts +331 -0
- package/src/prover/isabelle/pure/goal.ts +53 -0
- package/src/prover/isabelle/pure/interrupt.ts +16 -0
- package/src/prover/isabelle/pure/list.ts +12 -0
- package/src/prover/isabelle/pure/logic.ts +330 -0
- package/src/prover/isabelle/pure/name.ts +98 -0
- package/src/prover/isabelle/pure/net.ts +215 -0
- package/src/prover/isabelle/pure/object-logic.ts +41 -0
- package/src/prover/isabelle/pure/pattern.ts +631 -0
- package/src/prover/isabelle/pure/raw-simplifier.ts +338 -0
- package/src/prover/isabelle/pure/rewriter.ts +602 -0
- package/src/prover/isabelle/pure/search.ts +68 -0
- package/src/prover/isabelle/pure/seq.ts +133 -0
- package/src/prover/isabelle/pure/simplifier.ts +106 -0
- package/src/prover/isabelle/pure/table.ts +201 -0
- package/src/prover/isabelle/pure/tactic.ts +168 -0
- package/src/prover/isabelle/pure/tactical.ts +264 -0
- package/src/prover/isabelle/pure/term-ord.ts +105 -0
- package/src/prover/isabelle/pure/term.ts +688 -0
- package/src/prover/isabelle/pure/thm.ts +783 -0
- package/src/prover/isabelle/pure/type.ts +121 -0
- package/src/prover/isabelle/pure/unify.ts +753 -0
- package/src/prover/isabelle/pure/variable.ts +25 -0
- package/src/prover/isabelle/tla/classical-setup.ts +101 -0
- package/src/prover/isabelle/tla/context.ts +74 -0
- package/src/prover/isabelle/tla/logic-data.ts +5 -0
- package/src/prover/isabelle/tla/logic.ts +215 -0
- package/src/prover/isabelle/tla/simpdata.ts +241 -0
- package/src/prover/isabelle/tools/intuitionistic.ts +132 -0
- package/src/prover/ocaml.ts +22 -0
- package/src/prover/prep/action.ts +508 -0
- package/src/prover/prep/enabled-axioms.ts +189 -0
- package/src/prover/prep/fingerprint.ts +388 -0
- package/src/prover/prep/level-comparison.ts +529 -0
- package/src/prover/prep/md5.ts +60 -0
- package/src/prover/prep/methods.ts +221 -0
- package/src/prover/prep/normalize.ts +309 -0
- package/src/prover/prove-worker.ts +22 -0
- package/src/prover/prove.ts +492 -0
- package/src/prover/ptl/decide.ts +68 -0
- package/src/prover/ptl/encode.ts +244 -0
- package/src/prover/ptl/sat.ts +151 -0
- package/src/prover/ptl/tableau.ts +276 -0
- package/src/prover/smt/axioms.ts +209 -0
- package/src/prover/smt/context.ts +108 -0
- package/src/prover/smt/data.ts +636 -0
- package/src/prover/smt/encode.ts +275 -0
- package/src/prover/smt/extensionality.ts +65 -0
- package/src/prover/smt/flatten.ts +112 -0
- package/src/prover/smt/format.ts +154 -0
- package/src/prover/smt/instances.ts +181 -0
- package/src/prover/smt/rewrite.ts +766 -0
- package/src/prover/smt/solver/act-cache.ts +265 -0
- package/src/prover/smt/solver/arith-rewriter.ts +962 -0
- package/src/prover/smt/solver/array.ts +22 -0
- package/src/prover/smt/solver/asserted-formulas.ts +469 -0
- package/src/prover/smt/solver/ast-lt.ts +157 -0
- package/src/prover/smt/solver/ast-util.ts +91 -0
- package/src/prover/smt/solver/ast.ts +1315 -0
- package/src/prover/smt/solver/auf-solver.ts +572 -0
- package/src/prover/smt/solver/case-split-queue.ts +178 -0
- package/src/prover/smt/solver/cg-table.ts +175 -0
- package/src/prover/smt/solver/clause.ts +276 -0
- package/src/prover/smt/solver/conflict-resolution.ts +343 -0
- package/src/prover/smt/solver/context.ts +1292 -0
- package/src/prover/smt/solver/ctx-simplify.ts +334 -0
- package/src/prover/smt/solver/der.ts +224 -0
- package/src/prover/smt/solver/dyn-ack.ts +141 -0
- package/src/prover/smt/solver/e-matching.ts +659 -0
- package/src/prover/smt/solver/enode.ts +259 -0
- package/src/prover/smt/solver/expr-ref.ts +21 -0
- package/src/prover/smt/solver/expr-safe-replace.ts +172 -0
- package/src/prover/smt/solver/fingerprints.ts +92 -0
- package/src/prover/smt/solver/flat-literals.ts +51 -0
- package/src/prover/smt/solver/goal.ts +138 -0
- package/src/prover/smt/solver/hash.ts +173 -0
- package/src/prover/smt/solver/hashtable.ts +166 -0
- package/src/prover/smt/solver/internalizer.ts +1381 -0
- package/src/prover/smt/solver/lbool.ts +5 -0
- package/src/prover/smt/solver/literal.ts +24 -0
- package/src/prover/smt/solver/lp/bound-propagator.ts +391 -0
- package/src/prover/smt/solver/lp/constraints.ts +78 -0
- package/src/prover/smt/solver/lp/containers.ts +296 -0
- package/src/prover/smt/solver/lp/core-solver.ts +504 -0
- package/src/prover/smt/solver/lp/cross-nested.ts +224 -0
- package/src/prover/smt/solver/lp/dep-intervals.ts +642 -0
- package/src/prover/smt/solver/lp/dependency.ts +51 -0
- package/src/prover/smt/solver/lp/dioph-eq.ts +1181 -0
- package/src/prover/smt/solver/lp/explanation.ts +46 -0
- package/src/prover/smt/solver/lp/gomory.ts +255 -0
- package/src/prover/smt/solver/lp/hnf-cutter.ts +125 -0
- package/src/prover/smt/solver/lp/hnf.ts +281 -0
- package/src/prover/smt/solver/lp/int-cube.ts +295 -0
- package/src/prover/smt/solver/lp/int-solver.ts +514 -0
- package/src/prover/smt/solver/lp/lar-solver.ts +816 -0
- package/src/prover/smt/solver/lp/lar-term.ts +62 -0
- package/src/prover/smt/solver/lp/lp-settings.ts +91 -0
- package/src/prover/smt/solver/lp/maximize-term.ts +347 -0
- package/src/prover/smt/solver/lp/nex-creator.ts +381 -0
- package/src/prover/smt/solver/lp/nex.ts +120 -0
- package/src/prover/smt/solver/lp/nla-backoff.ts +43 -0
- package/src/prover/smt/solver/lp/nla-basics.ts +398 -0
- package/src/prover/smt/solver/lp/nla-core.ts +968 -0
- package/src/prover/smt/solver/lp/nla-divisions.ts +332 -0
- package/src/prover/smt/solver/lp/nla-emonics.ts +373 -0
- package/src/prover/smt/solver/lp/nla-factorization.ts +30 -0
- package/src/prover/smt/solver/lp/nla-grobner.ts +904 -0
- package/src/prover/smt/solver/lp/nla-horner.ts +126 -0
- package/src/prover/smt/solver/lp/nla-intervals.ts +265 -0
- package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +715 -0
- package/src/prover/smt/solver/lp/nla-monotone.ts +59 -0
- package/src/prover/smt/solver/lp/nla-order.ts +307 -0
- package/src/prover/smt/solver/lp/nla-patcher.ts +112 -0
- package/src/prover/smt/solver/lp/nla-tangents.ts +251 -0
- package/src/prover/smt/solver/lp/nla-throttle.ts +85 -0
- package/src/prover/smt/solver/lp/nla-types.ts +124 -0
- package/src/prover/smt/solver/lp/nla-var-eqs.ts +141 -0
- package/src/prover/smt/solver/lp/nra-solver.ts +440 -0
- package/src/prover/smt/solver/lp/numeric-pair.ts +65 -0
- package/src/prover/smt/solver/lp/pdd-interval.ts +31 -0
- package/src/prover/smt/solver/lp/pdd-simplifier.ts +184 -0
- package/src/prover/smt/solver/lp/pdd-solver.ts +408 -0
- package/src/prover/smt/solver/lp/pdd.ts +971 -0
- package/src/prover/smt/solver/lp/random-updater.ts +138 -0
- package/src/prover/smt/solver/lp/static-matrix.ts +246 -0
- package/src/prover/smt/solver/lp/union-find.ts +96 -0
- package/src/prover/smt/solver/mam-compiler.ts +847 -0
- package/src/prover/smt/solver/mam-interpreter.ts +400 -0
- package/src/prover/smt/solver/maximize-ac-sharing.ts +118 -0
- package/src/prover/smt/solver/model-checker.ts +347 -0
- package/src/prover/smt/solver/model-evaluator.ts +262 -0
- package/src/prover/smt/solver/model-finder.ts +155 -0
- package/src/prover/smt/solver/model-generator.ts +97 -0
- package/src/prover/smt/solver/model-macro-solver.ts +728 -0
- package/src/prover/smt/solver/nlsat/algebraic.ts +1161 -0
- package/src/prover/smt/solver/nlsat/atoms.ts +238 -0
- package/src/prover/smt/solver/nlsat/evaluator.ts +303 -0
- package/src/prover/smt/solver/nlsat/explain.ts +607 -0
- package/src/prover/smt/solver/nlsat/interval-set.ts +351 -0
- package/src/prover/smt/solver/nlsat/levelwise.ts +737 -0
- package/src/prover/smt/solver/nlsat/mpbq.ts +161 -0
- package/src/prover/smt/solver/nlsat/polynomial.ts +1722 -0
- package/src/prover/smt/solver/nlsat/solver.ts +911 -0
- package/src/prover/smt/solver/nlsat/types.ts +43 -0
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
- package/src/prover/smt/solver/nlsat/upolynomial.ts +996 -0
- package/src/prover/smt/solver/nnf.ts +469 -0
- package/src/prover/smt/solver/pattern-inference.ts +351 -0
- package/src/prover/smt/solver/poly-rewriter.ts +477 -0
- package/src/prover/smt/solver/propagate-values.ts +83 -0
- package/src/prover/smt/solver/proto-model.ts +551 -0
- package/src/prover/smt/solver/pull-quant.ts +139 -0
- package/src/prover/smt/solver/qe-lite-der.ts +526 -0
- package/src/prover/smt/solver/qe-lite-fm.ts +576 -0
- package/src/prover/smt/solver/qe-lite.ts +381 -0
- package/src/prover/smt/solver/qi-queue.ts +362 -0
- package/src/prover/smt/solver/quant-tactics.ts +117 -0
- package/src/prover/smt/solver/quantifier-analyzer.ts +562 -0
- package/src/prover/smt/solver/quantifier-manager.ts +186 -0
- package/src/prover/smt/solver/random-gen.ts +10 -0
- package/src/prover/smt/solver/rational.ts +165 -0
- package/src/prover/smt/solver/ref-vector.ts +62 -0
- package/src/prover/smt/solver/relevancy.ts +247 -0
- package/src/prover/smt/solver/rewriter-tpl.ts +1159 -0
- package/src/prover/smt/solver/rewriter.ts +843 -0
- package/src/prover/smt/solver/rlimit.ts +79 -0
- package/src/prover/smt/solver/smtlib-parser.ts +526 -0
- package/src/prover/smt/solver/solver.ts +71 -0
- package/src/prover/smt/solver/sort.ts +35 -0
- package/src/prover/smt/solver/sticky-generations.ts +69 -0
- package/src/prover/smt/solver/theory-lra-axioms.ts +340 -0
- package/src/prover/smt/solver/theory-lra-bounds.ts +226 -0
- package/src/prover/smt/solver/theory-lra-final-check.ts +252 -0
- package/src/prover/smt/solver/theory-lra-internalize.ts +476 -0
- package/src/prover/smt/solver/theory-lra-model.ts +243 -0
- package/src/prover/smt/solver/theory-lra-propagate.ts +405 -0
- package/src/prover/smt/solver/theory-lra-state.ts +43 -0
- package/src/prover/smt/solver/theory-lra.ts +56 -0
- package/src/prover/smt/solver/var-subst.ts +258 -0
- package/src/prover/smt/solver/z3-license.txt +10 -0
- package/src/prover/smt/table.ts +170 -0
- package/src/prover/tlapm/cache.ts +236 -0
- package/src/prover/tlapm/ocaml-marshal.ts +166 -0
- package/src/prover/tlapm/tlapm.ts +96 -0
- package/src/prover/tlaps-module.ts +510 -0
- package/src/prover/verdict.ts +13 -0
- package/src/prover/zenon/LICENSE +32 -0
- package/src/prover/zenon/check/check.ts +105 -0
- package/src/prover/zenon/check/definitions.ts +144 -0
- package/src/prover/zenon/check/equality.ts +182 -0
- package/src/prover/zenon/check/functions.ts +62 -0
- package/src/prover/zenon/check/keys.ts +125 -0
- package/src/prover/zenon/check/propositional.ts +104 -0
- package/src/prover/zenon/check/records.ts +188 -0
- package/src/prover/zenon/check/relations.ts +121 -0
- package/src/prover/zenon/check/rewrites.ts +224 -0
- package/src/prover/zenon/check/sets.ts +302 -0
- package/src/prover/zenon/check/terms.ts +127 -0
- package/src/prover/zenon/check/tla.ts +126 -0
- package/src/prover/zenon/decide.ts +107 -0
- package/src/prover/zenon/encode.ts +378 -0
- package/src/prover/zenon/eqrel.ts +300 -0
- package/src/prover/zenon/equality-rules.ts +435 -0
- package/src/prover/zenon/expr.ts +551 -0
- package/src/prover/zenon/index-table.ts +350 -0
- package/src/prover/zenon/isar-case.ts +513 -0
- package/src/prover/zenon/isar.ts +1249 -0
- package/src/prover/zenon/limits.ts +15 -0
- package/src/prover/zenon/llproof.ts +173 -0
- package/src/prover/zenon/mltoll.ts +1566 -0
- package/src/prover/zenon/node.ts +297 -0
- package/src/prover/zenon/ocaml.ts +169 -0
- package/src/prover/zenon/parse.ts +247 -0
- package/src/prover/zenon/phrase.ts +191 -0
- package/src/prover/zenon/proof.ts +198 -0
- package/src/prover/zenon/propositional-rules.ts +387 -0
- package/src/prover/zenon/rules.ts +94 -0
- package/src/prover/zenon/search.ts +288 -0
- package/src/prover/zenon/tla/functions.ts +151 -0
- package/src/prover/zenon/tla/llproof.ts +480 -0
- package/src/prover/zenon/tla/records.ts +225 -0
- package/src/prover/zenon/tla/rewrites.ts +447 -0
- package/src/prover/zenon/tla/sets.ts +304 -0
- package/src/prover/zenon/tla/terms.ts +162 -0
- package/src/prover/zenon/tla/tla.ts +506 -0
- package/src/randomization.ts +19 -24
- package/src/resolve.ts +15 -22
- package/src/runtime.ts +8 -8
- package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
- package/src/{sany-generator.ts → sany/generator.ts} +38 -55
- package/src/{sany-levels.ts → sany/levels.ts} +45 -48
- package/src/{sany-lexer.ts → sany/lexer.ts} +2 -3
- package/src/{sany-linter.ts → sany/linter.ts} +1 -1
- package/src/{sany-modules.ts → sany/modules.ts} +2 -2
- package/src/{sany-nodes.ts → sany/nodes.ts} +2 -2
- package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
- package/src/{sany-report.ts → sany/report.ts} +16 -17
- package/src/{sany-selectors.ts → sany/selectors.ts} +3 -5
- package/src/{sany-source.ts → sany/source.ts} +6 -7
- package/src/{sany-xml.ts → sany/xml.ts} +5 -6
- package/src/search.ts +969 -0
- package/src/spec-description.ts +26 -20
- package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
- package/src/standard-modules.ts +16 -20
- package/src/state-graph.ts +9 -8
- package/src/substitution-levels.ts +2 -2
- package/src/successors.ts +38 -22
- package/src/symmetry.ts +1 -1
- package/src/tableau.ts +6 -7
- package/src/tlc/fingerprint.ts +214 -0
- package/src/{tlc-messages.ts → tlc/messages.ts} +119 -7
- package/src/{tlc-order.ts → tlc/order.ts} +7 -7
- package/src/{tlc-random.ts → tlc/random.ts} +3 -190
- package/src/{tlc-replay.ts → tlc/replay.ts} +40 -36
- package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
- package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +30 -25
- package/src/{tlc-version.ts → tlc/version.ts} +8 -7
- package/src/transfer.ts +32 -32
- package/src/values.ts +119 -71
- package/src/worker.ts +62 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
- package/dist/run-cli.cjs +0 -55
- package/dist/run-cli.js +0 -55
- package/src/canonical.ts +0 -40
- package/src/obligation-expression.ts +0 -1060
- package/src/obligation-module.ts +0 -1042
- package/src/standard-ranges.ts +0 -116
- package/src/value-sources.ts +0 -29
- /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
- /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
|
@@ -0,0 +1,1161 @@
|
|
|
1
|
+
// Ported from Z3 (src/math/polynomial/algebraic_numbers.cpp, src/util/basic_interval.h,
|
|
2
|
+
// src/util/mpbq.cpp), MIT license, see z3-license.txt. Real algebraic numbers: a rational, or the
|
|
3
|
+
// root of a square-free integer polynomial in an open isolating interval of binary rationals whose
|
|
4
|
+
// ends the polynomial has opposite signs at. Comparing and evaluating refine these intervals in
|
|
5
|
+
// place, and may find a number rational, as Z3 does, so later operations see Z3's intervals.
|
|
6
|
+
import { SmtInvariantError } from "../../../../errors";
|
|
7
|
+
import { Rational } from "../rational";
|
|
8
|
+
import { getLog2, Mpbq } from "./mpbq";
|
|
9
|
+
import { PolynomialManager, type Polynomial, type ValueOps } from "./polynomial";
|
|
10
|
+
import { UpolynomialManager, type Upolynomial } from "./upolynomial";
|
|
11
|
+
import { factorUpolynomial, type FactorParams } from "./upolynomial-factorization";
|
|
12
|
+
|
|
13
|
+
// algebraic.factor_max_prime, factor_num_primes and factor_search_size.
|
|
14
|
+
const FACTOR_PARAMS: FactorParams = { maxPrime: 31, primeTrials: 1, maxSearchSize: 5000 };
|
|
15
|
+
// -algebraic.min_mag: a refined interval narrower than about 1/2^16 is not kept.
|
|
16
|
+
const MIN_MAGNITUDE = -16;
|
|
17
|
+
// DEFAULT_PRECISION: how far isolate_roots with signs refines each root first.
|
|
18
|
+
const SIGN_PRECISION = 2;
|
|
19
|
+
// The variables of the polynomials the binary operations build, m_x and m_y.
|
|
20
|
+
const X = 0;
|
|
21
|
+
const Y = 1;
|
|
22
|
+
|
|
23
|
+
class AlgebraicCell {
|
|
24
|
+
constructor(
|
|
25
|
+
public p: Upolynomial,
|
|
26
|
+
public lower: Mpbq,
|
|
27
|
+
public upper: Mpbq,
|
|
28
|
+
public isMinimal: boolean,
|
|
29
|
+
public isSignLowerNeg: boolean,
|
|
30
|
+
public isNotRational: boolean,
|
|
31
|
+
// m_i: which root of p the number is, from 1, or 0 when not yet computed.
|
|
32
|
+
public rootIndex: number,
|
|
33
|
+
) {}
|
|
34
|
+
|
|
35
|
+
get signLower() {
|
|
36
|
+
return this.isSignLowerNeg ? -1 : 1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
clone() {
|
|
40
|
+
return new AlgebraicCell(
|
|
41
|
+
this.p,
|
|
42
|
+
this.lower,
|
|
43
|
+
this.upper,
|
|
44
|
+
this.isMinimal,
|
|
45
|
+
this.isSignLowerNeg,
|
|
46
|
+
this.isNotRational,
|
|
47
|
+
this.rootIndex,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// anum: a mutable handle, since refining an algebraic number may turn it into a rational.
|
|
53
|
+
export class Anum {
|
|
54
|
+
constructor(public value: Rational | AlgebraicCell) {}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface BqInterval {
|
|
58
|
+
lower: Mpbq;
|
|
59
|
+
upper: Mpbq;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// One end of the interval select_small picks from: a binary rational end is closed, a rational one
|
|
63
|
+
// open.
|
|
64
|
+
interface SelectEnd {
|
|
65
|
+
value: Rational;
|
|
66
|
+
isOpen: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// save_intervals: an interval to restore when refining it made it too narrow to keep.
|
|
70
|
+
interface SavedInterval {
|
|
71
|
+
anum: Anum;
|
|
72
|
+
lower: Mpbq;
|
|
73
|
+
upper: Mpbq;
|
|
74
|
+
isRestored: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The factor whose Sturm sequence counts the one root in an interval.
|
|
78
|
+
interface IsolatingFactor {
|
|
79
|
+
factor: Upolynomial;
|
|
80
|
+
sequence: Upolynomial[];
|
|
81
|
+
lowerVariations: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interface ClosestAnums {
|
|
85
|
+
roots: Anum[];
|
|
86
|
+
indices: number[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface SignedRoots {
|
|
90
|
+
roots: Anum[];
|
|
91
|
+
signs: number[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const minBq = (first: Mpbq, second: Mpbq) => (second.compare(first) < 0 ? second : first);
|
|
95
|
+
const maxBq = (first: Mpbq, second: Mpbq) => (second.compare(first) > 0 ? second : first);
|
|
96
|
+
|
|
97
|
+
// mpbqi_manager over open intervals, as t_eval uses it.
|
|
98
|
+
const INTERVAL_OPS: ValueOps<BqInterval> = {
|
|
99
|
+
fromInteger: (value) => ({ lower: Mpbq.of(value), upper: Mpbq.of(value) }),
|
|
100
|
+
add: (first, second) => ({
|
|
101
|
+
lower: first.lower.add(second.lower),
|
|
102
|
+
upper: first.upper.add(second.upper),
|
|
103
|
+
}),
|
|
104
|
+
mul: (first, second) => {
|
|
105
|
+
const products = [
|
|
106
|
+
first.lower.mul(second.lower),
|
|
107
|
+
first.lower.mul(second.upper),
|
|
108
|
+
first.upper.mul(second.lower),
|
|
109
|
+
first.upper.mul(second.upper),
|
|
110
|
+
];
|
|
111
|
+
return { lower: products.reduce(minBq), upper: products.reduce(maxBq) };
|
|
112
|
+
},
|
|
113
|
+
power: (base, exponent) => getIntervalPower(base, exponent),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const getIntervalPower = (base: BqInterval, exponent: number): BqInterval => {
|
|
117
|
+
const lower = base.lower.power(exponent);
|
|
118
|
+
const upper = base.upper.power(exponent);
|
|
119
|
+
if (exponent % 2 === 1 || !base.lower.isNeg) return { lower, upper };
|
|
120
|
+
if (base.upper.isNeg) return { lower: upper, upper: lower };
|
|
121
|
+
return { lower: Mpbq.zero, upper: maxBq(lower, upper) };
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const containsZero = (interval: BqInterval) => interval.lower.isNeg && interval.upper.isPos;
|
|
125
|
+
|
|
126
|
+
// mpz log2 and mlog2: 0 outside the sign they expect.
|
|
127
|
+
const getPositiveLog2 = (value: bigint) => (value <= 0n ? 0 : getLog2(value));
|
|
128
|
+
const getNegativeLog2 = (value: bigint) => (value >= 0n ? 0 : getLog2(value));
|
|
129
|
+
|
|
130
|
+
// magnitude_ub
|
|
131
|
+
const getMagnitudeUpperBound = (value: Mpbq) =>
|
|
132
|
+
value.isZero
|
|
133
|
+
? 0
|
|
134
|
+
: value.isPos
|
|
135
|
+
? getLog2(value.num) - value.k + 1
|
|
136
|
+
: getNegativeLog2(value.num) - value.k;
|
|
137
|
+
|
|
138
|
+
// magnitude: about log2 of the width of (l, u).
|
|
139
|
+
const getMagnitude = (lower: Mpbq, upper: Mpbq) => {
|
|
140
|
+
if (lower.k === upper.k) return getMagnitudeUpperBound(lower);
|
|
141
|
+
const log2 = lower.isNeg ? getNegativeLog2 : getPositiveLog2;
|
|
142
|
+
return log2(upper.num) - log2(lower.num) - upper.k + lower.k - upper.k;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// select_integer: the integer select_small takes from scaled ends, preferring a closed end.
|
|
146
|
+
const selectInteger = (
|
|
147
|
+
lower: SelectEnd,
|
|
148
|
+
upper: SelectEnd,
|
|
149
|
+
lowerValue: Rational,
|
|
150
|
+
upperValue: Rational,
|
|
151
|
+
) => {
|
|
152
|
+
if (!lower.isOpen && lowerValue.isInt()) return lowerValue.num;
|
|
153
|
+
if (!upper.isOpen && upperValue.isInt()) return upperValue.num;
|
|
154
|
+
const ceilLower =
|
|
155
|
+
lower.isOpen && lowerValue.isInt() ? lowerValue.num + 1n : lowerValue.ceil().num;
|
|
156
|
+
const floorUpper =
|
|
157
|
+
upper.isOpen && upperValue.isInt() ? upperValue.num - 1n : upperValue.floor().num;
|
|
158
|
+
return ceilLower <= floorUpper ? ceilLower : null;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// select_small_core: the binary rational between two ends with the smallest power of two as its
|
|
162
|
+
// denominator.
|
|
163
|
+
const selectSmall = (lower: SelectEnd, upper: SelectEnd) => {
|
|
164
|
+
for (let k = 0; ; k++) {
|
|
165
|
+
const scale = Rational.of(1n << BigInt(k));
|
|
166
|
+
const integer = selectInteger(lower, upper, lower.value.mul(scale), upper.value.mul(scale));
|
|
167
|
+
if (integer !== null) return Mpbq.of(integer, k);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const getCell = (anum: Anum | undefined) => {
|
|
172
|
+
if (anum === undefined || anum.value instanceof Rational)
|
|
173
|
+
throw new SmtInvariantError("expected an irrational algebraic number");
|
|
174
|
+
return anum.value;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const getAssigned = (values: readonly (Anum | undefined)[], variable: number) => {
|
|
178
|
+
const value = values[variable];
|
|
179
|
+
if (value === undefined) throw new SmtInvariantError(`x${variable} is unassigned`);
|
|
180
|
+
return value;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const getRationalValues = (values: readonly (Anum | undefined)[]) =>
|
|
184
|
+
values.map((anum) => (anum?.value instanceof Rational ? anum.value : undefined));
|
|
185
|
+
|
|
186
|
+
const getExtended = (values: readonly (Anum | undefined)[], variable: number, anum: Anum) => {
|
|
187
|
+
const extended = [...values];
|
|
188
|
+
extended[variable] = anum;
|
|
189
|
+
return extended;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const sameCoeffs = (first: Upolynomial, second: Upolynomial) =>
|
|
193
|
+
first.length === second.length && first.every((coeff, index) => coeff === second[index]);
|
|
194
|
+
|
|
195
|
+
export class AlgebraicManager {
|
|
196
|
+
private um = new UpolynomialManager();
|
|
197
|
+
private pm = new PolynomialManager();
|
|
198
|
+
private anumOps: ValueOps<Anum> = {
|
|
199
|
+
fromInteger: (value) => new Anum(Rational.of(value)),
|
|
200
|
+
add: (first, second) => this.add(first, second),
|
|
201
|
+
mul: (first, second) => this.mul(first, second),
|
|
202
|
+
power: (base, exponent) => this.power(base, exponent),
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// Queries.
|
|
206
|
+
|
|
207
|
+
isZero(anum: Anum) {
|
|
208
|
+
return anum.value instanceof Rational && anum.value.isZero();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
degree(anum: Anum) {
|
|
212
|
+
if (this.isZero(anum)) return 0;
|
|
213
|
+
return anum.value instanceof Rational ? 1 : anum.value.p.length - 1;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
copy(anum: Anum) {
|
|
217
|
+
return new Anum(anum.value instanceof Rational ? anum.value : anum.value.clone());
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// is_int: refines the interval until it holds at most one integer, then tests it.
|
|
221
|
+
isInt(anum: Anum): boolean {
|
|
222
|
+
if (anum.value instanceof Rational) return anum.value.isInt();
|
|
223
|
+
const cell = anum.value;
|
|
224
|
+
if (cell.isNotRational) return false;
|
|
225
|
+
if (!this.refineUntilPrecision(anum, 1)) return this.isInt(anum);
|
|
226
|
+
const candidate = Rational.of(cell.upper.floor());
|
|
227
|
+
if (cell.lower.compareRational(candidate) < 0 && this.um.evalSignAtQ(cell.p, candidate) === 0) {
|
|
228
|
+
anum.value = candidate;
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// is_rational: by the rational root theorem, the only candidate in an interval narrower than
|
|
235
|
+
// 1/|a_n| is floor(u*|a_n|)/|a_n|.
|
|
236
|
+
isRational(anum: Anum) {
|
|
237
|
+
if (anum.value instanceof Rational) return true;
|
|
238
|
+
const cell = anum.value;
|
|
239
|
+
if (cell.isNotRational) return false;
|
|
240
|
+
const saved = this.saveInterval(anum);
|
|
241
|
+
const leading = cell.p[cell.p.length - 1];
|
|
242
|
+
const absLeading = leading < 0n ? -leading : leading;
|
|
243
|
+
if (!this.refineUntilPrecision(anum, getLog2(absLeading) + 1)) return true;
|
|
244
|
+
const candidate = Rational.of(cell.upper.mul(Mpbq.of(absLeading)).floor(), absLeading);
|
|
245
|
+
const isRoot =
|
|
246
|
+
cell.lower.compareRational(candidate) < 0 && this.um.evalSignAtQ(cell.p, candidate) === 0;
|
|
247
|
+
this.restoreIfTooSmall(saved);
|
|
248
|
+
if (isRoot) anum.value = candidate;
|
|
249
|
+
else cell.isNotRational = true;
|
|
250
|
+
return isRoot;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// get_i: which root of its polynomial the number is.
|
|
254
|
+
getRootIndex(anum: Anum) {
|
|
255
|
+
if (anum.value instanceof Rational) return this.isZero(anum) ? 0 : 1;
|
|
256
|
+
const cell = anum.value;
|
|
257
|
+
if (cell.rootIndex === 0) cell.rootIndex = this.um.getRootId(cell.p, cell.lower) + 1;
|
|
258
|
+
return cell.rootIndex;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Cells.
|
|
262
|
+
|
|
263
|
+
// mk_algebraic_cell: the coefficients made primitive with a positive leading coefficient.
|
|
264
|
+
private mkCell(p: Upolynomial, lower: Mpbq, upper: Mpbq, isMinimal: boolean) {
|
|
265
|
+
const cell = new AlgebraicCell(p, lower, upper, isMinimal, false, isMinimal, 0);
|
|
266
|
+
this.updateSignLower(cell);
|
|
267
|
+
cell.p = this.um.normalize(cell.p);
|
|
268
|
+
if (cell.p[cell.p.length - 1] < 0n) {
|
|
269
|
+
cell.p = this.um.neg(cell.p);
|
|
270
|
+
cell.isSignLowerNeg = !cell.isSignLowerNeg;
|
|
271
|
+
}
|
|
272
|
+
return cell;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private updateSignLower(cell: AlgebraicCell) {
|
|
276
|
+
cell.isSignLowerNeg = this.um.evalSignAtBq(cell.p, cell.lower) < 0;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// set(a, p, l, u, minimal): the root of p in (l, u), a rational when p is linear.
|
|
280
|
+
private mkAnum(p: Upolynomial, lower: Mpbq, upper: Mpbq, isMinimal: boolean) {
|
|
281
|
+
if (p.length === 2) return new Anum(Rational.of(p[0]).div(Rational.of(p[1])).neg());
|
|
282
|
+
return new Anum(this.mkCell(p, lower, upper, isMinimal));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// normalize: an interval around zero shrunk to the side of zero holding the root.
|
|
286
|
+
private normalize(anum: Anum) {
|
|
287
|
+
if (anum.value instanceof Rational) return;
|
|
288
|
+
const cell = anum.value;
|
|
289
|
+
const interval = this.um.normalizeInterval(cell.p, cell.lower, cell.upper, cell.signLower);
|
|
290
|
+
if (interval === null) {
|
|
291
|
+
anum.value = Rational.zero;
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
cell.lower = interval.lower;
|
|
295
|
+
cell.upper = interval.upper;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Refinement.
|
|
299
|
+
|
|
300
|
+
// refine: halves the interval, or finds the number rational and returns false.
|
|
301
|
+
private refine(anum: Anum) {
|
|
302
|
+
if (anum.value instanceof Rational) return false;
|
|
303
|
+
const cell = anum.value;
|
|
304
|
+
const refined = this.um.refineCore(cell.p, cell.signLower, cell.lower, cell.upper);
|
|
305
|
+
if (!refined.isInterval) {
|
|
306
|
+
anum.value = refined.lower.toRational();
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
cell.lower = refined.lower;
|
|
310
|
+
cell.upper = refined.upper;
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private refineTimes(anum: Anum, count: number) {
|
|
315
|
+
for (let index = 0; index < count; index++) if (!this.refine(anum)) return false;
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// refine_until_prec: refines until the interval is narrower than 1/2^precision.
|
|
320
|
+
private refineUntilPrecision(anum: Anum, precision: number) {
|
|
321
|
+
if (anum.value instanceof Rational) return true;
|
|
322
|
+
const cell = anum.value;
|
|
323
|
+
const refined = this.um.refineToPrecision(cell.p, cell.lower, cell.upper, precision);
|
|
324
|
+
if (!refined.isInterval) {
|
|
325
|
+
anum.value = refined.lower.toRational();
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
cell.lower = refined.lower;
|
|
329
|
+
cell.upper = refined.upper;
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private getCellMagnitude(cell: AlgebraicCell) {
|
|
334
|
+
return getMagnitude(cell.lower, cell.upper);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private saveInterval(anum: Anum): SavedInterval {
|
|
338
|
+
const cell = getCell(anum);
|
|
339
|
+
return { anum, lower: cell.lower, upper: cell.upper, isRestored: false };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// restore_if_too_small: swaps the saved interval back in when the current one is too narrow.
|
|
343
|
+
private restoreIfTooSmall(saved: SavedInterval) {
|
|
344
|
+
saved.isRestored = true;
|
|
345
|
+
if (saved.anum.value instanceof Rational) return;
|
|
346
|
+
const cell = saved.anum.value;
|
|
347
|
+
if (this.getCellMagnitude(cell) >= MIN_MAGNITUDE) return;
|
|
348
|
+
[cell.lower, cell.upper, saved.lower, saved.upper] = [
|
|
349
|
+
saved.lower,
|
|
350
|
+
saved.upper,
|
|
351
|
+
cell.lower,
|
|
352
|
+
cell.upper,
|
|
353
|
+
];
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ~save_intervals
|
|
357
|
+
private releaseInterval(saved: SavedInterval) {
|
|
358
|
+
if (!saved.isRestored) this.restoreIfTooSmall(saved);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Root isolation.
|
|
362
|
+
|
|
363
|
+
private factor(p: Upolynomial) {
|
|
364
|
+
return factorUpolynomial(this.um, p, FACTOR_PARAMS);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// isolate_roots(up): the real roots of a univariate polynomial, factor by factor, sorted.
|
|
368
|
+
private isolateUnivariateRoots(up: Upolynomial) {
|
|
369
|
+
const roots: Anum[] = [];
|
|
370
|
+
if (up.length === 0) return roots;
|
|
371
|
+
const hasZeroRoot = this.um.hasZeroRoots(up);
|
|
372
|
+
if (hasZeroRoot) roots.push(new Anum(Rational.zero));
|
|
373
|
+
const factored = this.factor(hasZeroRoot ? this.um.removeZeroRoots(up) : up);
|
|
374
|
+
for (const factor of factored.factors.factors) {
|
|
375
|
+
const degree = factor.length - 1;
|
|
376
|
+
if (degree <= 0) continue;
|
|
377
|
+
if (degree === 1) {
|
|
378
|
+
roots.push(new Anum(Rational.of(factor[0]).div(Rational.of(factor[1])).neg()));
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
const isolated = this.um.sqfIsolateRoots(factor);
|
|
382
|
+
for (const root of isolated.roots) roots.push(new Anum(root.toRational()));
|
|
383
|
+
for (let index = 0; index < isolated.lowers.length; index++) {
|
|
384
|
+
const refined = this.um.isolating2refinable(
|
|
385
|
+
factor,
|
|
386
|
+
isolated.lowers[index],
|
|
387
|
+
isolated.uppers[index],
|
|
388
|
+
);
|
|
389
|
+
roots.push(
|
|
390
|
+
new Anum(
|
|
391
|
+
refined.isInterval
|
|
392
|
+
? this.mkCell(factor, refined.lower, refined.upper, factored.isComplete)
|
|
393
|
+
: refined.lower.toRational(),
|
|
394
|
+
),
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
this.sortRoots(roots);
|
|
399
|
+
return roots;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// sort_roots: Z3's stable bottom-up merge sort, since each comparison may refine the roots.
|
|
403
|
+
private sortRoots(roots: Anum[]) {
|
|
404
|
+
const size = roots.length;
|
|
405
|
+
if (size < 2) return;
|
|
406
|
+
const isLess = (first: number, second: number) => {
|
|
407
|
+
const sign = this.compare(roots[first], roots[second]);
|
|
408
|
+
return sign !== 0 ? sign < 0 : first < second;
|
|
409
|
+
};
|
|
410
|
+
let source = roots.map((_root, index) => index);
|
|
411
|
+
let target = [...source];
|
|
412
|
+
for (let width = 1; width < size; width *= 2) {
|
|
413
|
+
for (let low = 0; low < size; low += 2 * width) {
|
|
414
|
+
const middle = Math.min(low + width, size);
|
|
415
|
+
const high = Math.min(low + 2 * width, size);
|
|
416
|
+
let left = low;
|
|
417
|
+
let right = middle;
|
|
418
|
+
let next = low;
|
|
419
|
+
while (left < middle && right < high)
|
|
420
|
+
target[next++] = isLess(source[right], source[left]) ? source[right++] : source[left++];
|
|
421
|
+
while (left < middle) target[next++] = source[left++];
|
|
422
|
+
while (right < high) target[next++] = source[right++];
|
|
423
|
+
}
|
|
424
|
+
[source, target] = [target, source];
|
|
425
|
+
}
|
|
426
|
+
const sorted = source.map((index) => roots[index]);
|
|
427
|
+
for (let index = 0; index < size; index++) roots[index] = sorted[index];
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private getSignVariationsAtQ(sequence: Upolynomial[], value: Rational) {
|
|
431
|
+
if (sequence.length <= 1) return 0;
|
|
432
|
+
let count = 0;
|
|
433
|
+
let previous = 0;
|
|
434
|
+
for (const coeffs of sequence) {
|
|
435
|
+
const sign = this.um.evalSignAtQ(coeffs, value);
|
|
436
|
+
if (sign === 0) continue;
|
|
437
|
+
if (previous !== 0 && sign !== previous) count++;
|
|
438
|
+
previous = sign;
|
|
439
|
+
}
|
|
440
|
+
return count;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// isolate_kth_root: the i-th real root of a square-free p, by bisecting with its Sturm sequence.
|
|
444
|
+
private isolateKthRoot(p: Upolynomial, sequence: Upolynomial[], rootIndex: number) {
|
|
445
|
+
if (p.length === 2) return new Anum(Rational.of(p[0]).div(Rational.of(p[1])).neg());
|
|
446
|
+
const minusInf = this.um.signVariationsAtMinusInf(sequence);
|
|
447
|
+
const atZero = this.um.signVariationsAtZero(sequence);
|
|
448
|
+
const nonPositiveCount = minusInf - atZero;
|
|
449
|
+
const isNonPositive = rootIndex <= nonPositiveCount;
|
|
450
|
+
let low = isNonPositive
|
|
451
|
+
? Mpbq.of(-(1n << BigInt(this.um.knuthNegativeRootUpperBound(p))))
|
|
452
|
+
: Mpbq.zero;
|
|
453
|
+
let high = isNonPositive
|
|
454
|
+
? Mpbq.zero
|
|
455
|
+
: Mpbq.of(1n << BigInt(this.um.knuthPositiveRootUpperBound(p)));
|
|
456
|
+
let lowVariations = isNonPositive ? minusInf : atZero;
|
|
457
|
+
let highVariations = isNonPositive ? atZero : this.um.signVariationsAtPlusInf(sequence);
|
|
458
|
+
let target = isNonPositive ? rootIndex : rootIndex - nonPositiveCount;
|
|
459
|
+
while (lowVariations > highVariations + 1) {
|
|
460
|
+
const middle = low.add(high).div2();
|
|
461
|
+
const middleVariations = this.um.signVariationsAt(sequence, middle);
|
|
462
|
+
const leftCount = lowVariations - middleVariations;
|
|
463
|
+
if (target <= leftCount) {
|
|
464
|
+
high = middle;
|
|
465
|
+
highVariations = middleVariations;
|
|
466
|
+
} else {
|
|
467
|
+
low = middle;
|
|
468
|
+
lowVariations = middleVariations;
|
|
469
|
+
target -= leftCount;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (this.um.evalSignAtBq(p, high) === 0) return new Anum(high.toRational());
|
|
473
|
+
const refined = this.um.isolating2refinable(p, low, high);
|
|
474
|
+
if (!refined.isInterval) return new Anum(refined.lower.toRational());
|
|
475
|
+
const root = this.getRationalRootIn(p, refined.lower, refined.upper);
|
|
476
|
+
return new Anum(
|
|
477
|
+
root instanceof Rational ? root : this.mkCell(p, root.lower, root.upper, false),
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// rational_root_in_interval: the root of p in (l, u) when it is rational, else the interval
|
|
482
|
+
// refined below 1/|a_n|.
|
|
483
|
+
private getRationalRootIn(p: Upolynomial, lower: Mpbq, upper: Mpbq): Rational | BqInterval {
|
|
484
|
+
const leading = p[p.length - 1];
|
|
485
|
+
const absLeading = leading < 0n ? -leading : leading;
|
|
486
|
+
const refined = this.um.refineToPrecision(p, lower, upper, getLog2(absLeading) + 1);
|
|
487
|
+
if (!refined.isInterval) return refined.lower.toRational();
|
|
488
|
+
const candidate = Rational.of(refined.upper.mul(Mpbq.of(absLeading)).floor(), absLeading);
|
|
489
|
+
if (refined.lower.compareRational(candidate) < 0 && this.um.evalSignAtQ(p, candidate) === 0)
|
|
490
|
+
return candidate;
|
|
491
|
+
return { lower: refined.lower, upper: refined.upper };
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// isolate_roots_closest_univariate: the roots next to s, s itself when it is a root.
|
|
495
|
+
private isolateUnivariateClosest(
|
|
496
|
+
pm: PolynomialManager,
|
|
497
|
+
polynomial: Polynomial,
|
|
498
|
+
sample: Rational,
|
|
499
|
+
): ClosestAnums {
|
|
500
|
+
const result: ClosestAnums = { roots: [], indices: [] };
|
|
501
|
+
if (pm.isConst(polynomial)) return result;
|
|
502
|
+
const up = pm.toCoeffs(polynomial);
|
|
503
|
+
if (up.length === 0) return result;
|
|
504
|
+
const p = this.um.squareFree(up);
|
|
505
|
+
if (p.length <= 1) return result;
|
|
506
|
+
const sequence = this.um.sturmSeq(p);
|
|
507
|
+
const minusInf = this.um.signVariationsAtMinusInf(sequence);
|
|
508
|
+
const plusInf = this.um.signVariationsAtPlusInf(sequence);
|
|
509
|
+
if (minusInf <= plusInf) return result;
|
|
510
|
+
const total = minusInf - plusInf;
|
|
511
|
+
const below = minusInf - this.getSignVariationsAtQ(sequence, sample);
|
|
512
|
+
if (this.um.evalSignAtQ(p, sample) === 0)
|
|
513
|
+
return { roots: [new Anum(sample)], indices: [below] };
|
|
514
|
+
if (below > 0) {
|
|
515
|
+
result.roots.push(this.isolateKthRoot(p, sequence, below));
|
|
516
|
+
result.indices.push(below);
|
|
517
|
+
}
|
|
518
|
+
if (below < total) {
|
|
519
|
+
result.roots.push(this.isolateKthRoot(p, sequence, below + 1));
|
|
520
|
+
result.indices.push(below + 1);
|
|
521
|
+
}
|
|
522
|
+
return result;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// isolate_roots(p, x2v): the roots of p in its one unassigned variable at the values of the
|
|
526
|
+
// others. Resultants eliminate the irrational values, and the roots of the result that are not
|
|
527
|
+
// roots of p are filtered out.
|
|
528
|
+
isolateRoots(
|
|
529
|
+
pm: PolynomialManager,
|
|
530
|
+
polynomial: Polynomial,
|
|
531
|
+
values: readonly (Anum | undefined)[],
|
|
532
|
+
isNested = false,
|
|
533
|
+
): Anum[] {
|
|
534
|
+
if (pm.isConst(polynomial)) return [];
|
|
535
|
+
if (pm.isUnivariate(polynomial)) return this.isolateUnivariateRoots(pm.toCoeffs(polynomial));
|
|
536
|
+
const reduced = pm.substituteRational(polynomial, getRationalValues(values));
|
|
537
|
+
if (pm.isConst(reduced)) return [];
|
|
538
|
+
if (pm.isUnivariate(reduced)) {
|
|
539
|
+
if (values[pm.maxVar(reduced)] !== undefined) return [];
|
|
540
|
+
return this.isolateUnivariateRoots(pm.toCoeffs(reduced));
|
|
541
|
+
}
|
|
542
|
+
const xs = this.sortByDegree(pm.getVars(reduced), values);
|
|
543
|
+
const x = xs[xs.length - 1];
|
|
544
|
+
if (values[x] !== undefined) return [];
|
|
545
|
+
let q = reduced;
|
|
546
|
+
for (const y of xs.slice(0, -1)) {
|
|
547
|
+
q = pm.resultant(q, pm.fromCoeffs(getCell(values[y]).p, y), y);
|
|
548
|
+
if (q.size === 0) break;
|
|
549
|
+
}
|
|
550
|
+
if (q.size > 0) {
|
|
551
|
+
if (pm.isConst(q)) return [];
|
|
552
|
+
return this.filterRoots(pm, reduced, values, x, this.isolateUnivariateRoots(pm.toCoeffs(q)));
|
|
553
|
+
}
|
|
554
|
+
if (isNested)
|
|
555
|
+
throw new SmtInvariantError("resultant vanished during nested isolate_roots call");
|
|
556
|
+
return this.isolateVanishedRoots(pm, reduced, values, xs, x);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// The resultant vanished, perhaps at another root of a value's polynomial: p's leading
|
|
560
|
+
// coefficient at the values decides.
|
|
561
|
+
private isolateVanishedRoots(
|
|
562
|
+
pm: PolynomialManager,
|
|
563
|
+
reduced: Polynomial,
|
|
564
|
+
values: readonly (Anum | undefined)[],
|
|
565
|
+
xs: number[],
|
|
566
|
+
x: number,
|
|
567
|
+
) {
|
|
568
|
+
const evaluate = (polynomial: Polynomial) =>
|
|
569
|
+
pm.evaluateWith(polynomial, this.anumOps, (variable) => getAssigned(values, variable));
|
|
570
|
+
const degree = pm.degree(reduced, x);
|
|
571
|
+
if (degree === 1) {
|
|
572
|
+
const constant = evaluate(pm.coeff(reduced, x, 0));
|
|
573
|
+
const leading = evaluate(pm.coeff(reduced, x, 1));
|
|
574
|
+
if (this.isZero(leading)) return [];
|
|
575
|
+
const root = this.div(constant, leading);
|
|
576
|
+
this.neg(root);
|
|
577
|
+
return [root];
|
|
578
|
+
}
|
|
579
|
+
let power = degree;
|
|
580
|
+
let leading = new Anum(Rational.zero);
|
|
581
|
+
for (; power >= 1; power--) {
|
|
582
|
+
leading = evaluate(pm.coeff(reduced, x, power));
|
|
583
|
+
if (!this.isZero(leading)) break;
|
|
584
|
+
}
|
|
585
|
+
if (power === 0) return [];
|
|
586
|
+
// z*x^i + c_{i-1}*x^{i-1} + ... + c_0, with z the value of the leading coefficient.
|
|
587
|
+
const z = Math.max(...xs) + 1;
|
|
588
|
+
const q = this.pm.add(
|
|
589
|
+
this.pm.mul(this.pm.mkVar(z), this.pm.mkVar(x, power)),
|
|
590
|
+
this.convert(pm, reduced, x, power - 1),
|
|
591
|
+
);
|
|
592
|
+
return this.isolateRoots(this.pm, q, getExtended(values, z, leading), true);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// convert: p in the local manager, without its monomials of degree above maxDegree in x.
|
|
596
|
+
private convert(pm: PolynomialManager, polynomial: Polynomial, x = -1, maxDegree = Infinity) {
|
|
597
|
+
if (pm === this.pm) return polynomial;
|
|
598
|
+
const as: bigint[] = [];
|
|
599
|
+
const ms = [];
|
|
600
|
+
for (let index = 0; index < polynomial.size; index++) {
|
|
601
|
+
const monomial = polynomial.ms[index];
|
|
602
|
+
if (x !== -1 && monomial.degreeOf(x) > maxDegree) continue;
|
|
603
|
+
as.push(polynomial.as[index]);
|
|
604
|
+
ms.push(this.pm.mkMonomial(monomial.vars, monomial.degrees));
|
|
605
|
+
}
|
|
606
|
+
return this.pm.mkPolynomial(as, ms);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// std::stable_sort with var_degree_lt: by the degree of each value, unassigned last.
|
|
610
|
+
private sortByDegree(xs: number[], values: readonly (Anum | undefined)[]) {
|
|
611
|
+
const getDegree = (variable: number) => {
|
|
612
|
+
const value = values[variable];
|
|
613
|
+
return value === undefined ? Infinity : this.degree(value);
|
|
614
|
+
};
|
|
615
|
+
return [...xs].sort((first, second) => getDegree(first) - getDegree(second));
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// filter_roots: the roots at which p vanishes.
|
|
619
|
+
private filterRoots(
|
|
620
|
+
pm: PolynomialManager,
|
|
621
|
+
polynomial: Polynomial,
|
|
622
|
+
values: readonly (Anum | undefined)[],
|
|
623
|
+
x: number,
|
|
624
|
+
roots: Anum[],
|
|
625
|
+
) {
|
|
626
|
+
return roots.filter(
|
|
627
|
+
(root) => this.evalSignAt(pm, polynomial, getExtended(values, x, root)) === 0,
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// isolate_roots_closest: the roots of p next to the rational s, with their indices from 1.
|
|
632
|
+
isolateRootsClosest(
|
|
633
|
+
pm: PolynomialManager,
|
|
634
|
+
polynomial: Polynomial,
|
|
635
|
+
values: readonly (Anum | undefined)[],
|
|
636
|
+
sample: Rational,
|
|
637
|
+
): ClosestAnums {
|
|
638
|
+
const result: ClosestAnums = { roots: [], indices: [] };
|
|
639
|
+
if (pm.isConst(polynomial)) return result;
|
|
640
|
+
if (pm.isUnivariate(polynomial)) return this.isolateUnivariateClosest(pm, polynomial, sample);
|
|
641
|
+
const reduced = pm.substituteRational(polynomial, getRationalValues(values));
|
|
642
|
+
if (pm.isConst(reduced)) return result;
|
|
643
|
+
if (pm.isUnivariate(reduced)) {
|
|
644
|
+
if (values[pm.maxVar(reduced)] !== undefined) return result;
|
|
645
|
+
return this.isolateUnivariateClosest(pm, reduced, sample);
|
|
646
|
+
}
|
|
647
|
+
const all = this.isolateRoots(pm, polynomial, values);
|
|
648
|
+
const sampleAnum = new Anum(sample);
|
|
649
|
+
let lower = -1;
|
|
650
|
+
let upper = -1;
|
|
651
|
+
for (let index = 0; index < all.length; index++) {
|
|
652
|
+
if (this.compare(all[index], sampleAnum) <= 0) lower = index;
|
|
653
|
+
else {
|
|
654
|
+
upper = index;
|
|
655
|
+
break;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
if (lower !== -1 && this.compare(all[lower], sampleAnum) === 0)
|
|
659
|
+
return { roots: [all[lower]], indices: [lower + 1] };
|
|
660
|
+
if (lower !== -1) {
|
|
661
|
+
result.roots.push(all[lower]);
|
|
662
|
+
result.indices.push(lower + 1);
|
|
663
|
+
}
|
|
664
|
+
if (upper !== -1) {
|
|
665
|
+
result.roots.push(all[upper]);
|
|
666
|
+
result.indices.push(upper + 1);
|
|
667
|
+
}
|
|
668
|
+
return result;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
// isolate_roots(p, x2v, roots, signs): the roots, and the sign of p below, between and above
|
|
672
|
+
// them, at a sample the unassigned variables share.
|
|
673
|
+
isolateRootsWithSigns(
|
|
674
|
+
pm: PolynomialManager,
|
|
675
|
+
polynomial: Polynomial,
|
|
676
|
+
values: readonly (Anum | undefined)[],
|
|
677
|
+
): SignedRoots {
|
|
678
|
+
const roots = this.isolateRoots(pm, polynomial, values);
|
|
679
|
+
const getSign = (sample: Anum) => {
|
|
680
|
+
const extended = [...values];
|
|
681
|
+
for (const variable of pm.getVars(polynomial))
|
|
682
|
+
if (extended[variable] === undefined) extended[variable] = sample;
|
|
683
|
+
return this.evalSignAt(pm, polynomial, extended);
|
|
684
|
+
};
|
|
685
|
+
if (roots.length === 0) return { roots, signs: [getSign(new Anum(Rational.zero))] };
|
|
686
|
+
for (const root of roots) this.refineUntilPrecision(root, SIGN_PRECISION);
|
|
687
|
+
const signs = [getSign(this.intLt(roots[0]))];
|
|
688
|
+
for (let index = 1; index < roots.length; index++)
|
|
689
|
+
signs.push(getSign(this.select(roots[index - 1], roots[index])));
|
|
690
|
+
signs.push(getSign(this.intGt(roots[roots.length - 1])));
|
|
691
|
+
return { roots, signs };
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// Signs.
|
|
695
|
+
|
|
696
|
+
// eval_sign_at: the sign of p at the values. Rational values are substituted, interval
|
|
697
|
+
// arithmetic on the irrational ones decides most signs, and otherwise a lower bound on the
|
|
698
|
+
// nonzero roots of the resultant with their polynomials tells zero apart.
|
|
699
|
+
evalSignAt(pm: PolynomialManager, polynomial: Polynomial, values: readonly (Anum | undefined)[]) {
|
|
700
|
+
while (true) {
|
|
701
|
+
const rationalValues = getRationalValues(values);
|
|
702
|
+
if (pm.getVars(polynomial).every((variable) => rationalValues[variable] !== undefined))
|
|
703
|
+
return pm.getSign(polynomial, rationalValues);
|
|
704
|
+
const reduced = pm.substituteRational(polynomial, rationalValues);
|
|
705
|
+
if (reduced.size === 0) return 0;
|
|
706
|
+
if (pm.isConst(reduced)) return reduced.as[0] > 0n ? 1 : -1;
|
|
707
|
+
const xs = pm.getVars(reduced);
|
|
708
|
+
const getInterval = (variable: number) => {
|
|
709
|
+
const cell = getCell(values[variable]);
|
|
710
|
+
return { lower: cell.lower, upper: cell.upper };
|
|
711
|
+
};
|
|
712
|
+
const evaluate = () => pm.evaluateWith(reduced, INTERVAL_OPS, getInterval);
|
|
713
|
+
let interval = evaluate();
|
|
714
|
+
let isRestarted = false;
|
|
715
|
+
while (true) {
|
|
716
|
+
if (!containsZero(interval)) return interval.lower.isNeg ? -1 : 1;
|
|
717
|
+
let isRefined = false;
|
|
718
|
+
for (const variable of xs) {
|
|
719
|
+
const value = getAssigned(values, variable);
|
|
720
|
+
if (this.getCellMagnitude(getCell(value)) <= MIN_MAGNITUDE) continue;
|
|
721
|
+
if (!this.refine(value)) {
|
|
722
|
+
isRestarted = true;
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
isRefined = true;
|
|
726
|
+
}
|
|
727
|
+
if (!isRefined || isRestarted) break;
|
|
728
|
+
interval = evaluate();
|
|
729
|
+
}
|
|
730
|
+
if (isRestarted) continue;
|
|
731
|
+
const sign = this.evalSignByResultant(pm, reduced, values, xs, interval, evaluate);
|
|
732
|
+
if (sign !== null) return sign;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// The resultant step of eval_sign_at, or null when a value turned rational and it restarts.
|
|
737
|
+
private evalSignByResultant(
|
|
738
|
+
pm: PolynomialManager,
|
|
739
|
+
reduced: Polynomial,
|
|
740
|
+
values: readonly (Anum | undefined)[],
|
|
741
|
+
xs: number[],
|
|
742
|
+
initial: BqInterval,
|
|
743
|
+
evaluate: () => BqInterval,
|
|
744
|
+
) {
|
|
745
|
+
const sorted = this.sortByDegree(xs, values);
|
|
746
|
+
let resultant = this.pm.sub(this.pm.mkVar(Math.max(...xs) + 1), this.convert(pm, reduced));
|
|
747
|
+
for (const variable of sorted)
|
|
748
|
+
resultant = this.pm.resultant(
|
|
749
|
+
resultant,
|
|
750
|
+
this.pm.fromCoeffs(getCell(values[variable]).p, variable),
|
|
751
|
+
variable,
|
|
752
|
+
);
|
|
753
|
+
const k = this.um.nonzeroRootLowerBound(this.pm.toCoeffs(resultant));
|
|
754
|
+
const bound = Mpbq.of(1n, k);
|
|
755
|
+
const isInsideBound = (interval: BqInterval) =>
|
|
756
|
+
bound.neg().compare(interval.lower) < 0 && interval.upper.compare(bound) < 0;
|
|
757
|
+
if (isInsideBound(initial)) return 0;
|
|
758
|
+
const saved = sorted.map((variable) => this.saveInterval(getAssigned(values, variable)));
|
|
759
|
+
let sign: number | null = null;
|
|
760
|
+
while (sign === null) {
|
|
761
|
+
const interval = evaluate();
|
|
762
|
+
if (!containsZero(interval)) sign = interval.lower.isNeg ? -1 : 1;
|
|
763
|
+
else if (isInsideBound(interval)) sign = 0;
|
|
764
|
+
else if (!sorted.every((variable) => this.refine(getAssigned(values, variable)))) break;
|
|
765
|
+
}
|
|
766
|
+
for (const entry of saved) this.releaseInterval(entry);
|
|
767
|
+
return sign;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// Arithmetic.
|
|
771
|
+
|
|
772
|
+
add(first: Anum, second: Anum): Anum {
|
|
773
|
+
if (this.isZero(first)) return this.copy(second);
|
|
774
|
+
if (this.isZero(second)) return this.copy(first);
|
|
775
|
+
if (first.value instanceof Rational)
|
|
776
|
+
return second.value instanceof Rational
|
|
777
|
+
? new Anum(first.value.add(second.value))
|
|
778
|
+
: this.addRational(second.value, first.value);
|
|
779
|
+
if (second.value instanceof Rational) return this.addRational(first.value, second.value);
|
|
780
|
+
return this.mkBinary(first, second, true);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// add(cell, basic): p(x - b), with the interval shifted by b.
|
|
784
|
+
private addRational(cell: AlgebraicCell, value: Rational) {
|
|
785
|
+
const p = this.um.translateQ(cell.p, value.neg());
|
|
786
|
+
const shift = Mpbq.fromRational(value);
|
|
787
|
+
const interval = shift.isExact
|
|
788
|
+
? { lower: cell.lower.add(shift.value), upper: cell.upper.add(shift.value) }
|
|
789
|
+
: this.um.convertQ2bqInterval(
|
|
790
|
+
p,
|
|
791
|
+
cell.lower.toRational().add(value),
|
|
792
|
+
cell.upper.toRational().add(value),
|
|
793
|
+
);
|
|
794
|
+
const result = this.mkAnum(p, interval.lower, interval.upper, cell.isMinimal);
|
|
795
|
+
this.normalize(result);
|
|
796
|
+
return result;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
mul(first: Anum, second: Anum): Anum {
|
|
800
|
+
if (this.isZero(first) || this.isZero(second)) return new Anum(Rational.zero);
|
|
801
|
+
if (first.value instanceof Rational)
|
|
802
|
+
return second.value instanceof Rational
|
|
803
|
+
? new Anum(first.value.mul(second.value))
|
|
804
|
+
: this.mulRational(second.value, first.value);
|
|
805
|
+
if (second.value instanceof Rational) return this.mulRational(first.value, second.value);
|
|
806
|
+
return this.mkBinary(first, second, false);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
// mul(cell, basic): b^n * p(x/b), with the interval scaled by b.
|
|
810
|
+
private mulRational(cell: AlgebraicCell, value: Rational) {
|
|
811
|
+
const p = this.um.composePQX(cell.p, Rational.one.div(value));
|
|
812
|
+
const scale = Mpbq.fromRational(value);
|
|
813
|
+
const isNegative = value.isNegative();
|
|
814
|
+
let lower: Mpbq;
|
|
815
|
+
let upper: Mpbq;
|
|
816
|
+
if (scale.isExact) {
|
|
817
|
+
upper = cell.upper.mul(scale.value);
|
|
818
|
+
lower = cell.lower.mulIntoOther(scale.value);
|
|
819
|
+
if (isNegative) [lower, upper] = [upper, lower];
|
|
820
|
+
} else {
|
|
821
|
+
let rationalLower = cell.lower.toRational().mul(value);
|
|
822
|
+
let rationalUpper = cell.upper.toRational().mul(value);
|
|
823
|
+
if (isNegative) [rationalLower, rationalUpper] = [rationalUpper, rationalLower];
|
|
824
|
+
const interval = this.um.convertQ2bqInterval(p, rationalLower, rationalUpper);
|
|
825
|
+
lower = interval.lower;
|
|
826
|
+
upper = interval.upper;
|
|
827
|
+
}
|
|
828
|
+
const result = this.mkAnum(p, lower, upper, cell.isMinimal);
|
|
829
|
+
this.normalize(result);
|
|
830
|
+
return result;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// neg: p(-x) with the interval mirrored, in place.
|
|
834
|
+
neg(anum: Anum) {
|
|
835
|
+
if (anum.value instanceof Rational) {
|
|
836
|
+
anum.value = anum.value.neg();
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
const cell = anum.value;
|
|
840
|
+
cell.p = this.um.pMinusX(cell.p);
|
|
841
|
+
[cell.lower, cell.upper] = [cell.upper.neg(), cell.lower.neg()];
|
|
842
|
+
if (cell.rootIndex !== 0) cell.rootIndex = this.um.getRootId(cell.p, cell.lower) + 1;
|
|
843
|
+
this.updateSignLower(cell);
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
// refine_nz_bound: moves an interval end off zero, halving it until its sign is right.
|
|
847
|
+
private refineNonzeroBound(anum: Anum) {
|
|
848
|
+
if (anum.value instanceof Rational) return;
|
|
849
|
+
const cell = anum.value;
|
|
850
|
+
if (!cell.lower.isZero && !cell.upper.isZero) return;
|
|
851
|
+
const isLower = cell.lower.isZero;
|
|
852
|
+
const targetSign = isLower ? cell.signLower : -cell.signLower;
|
|
853
|
+
let bound = isLower ? cell.upper : cell.lower;
|
|
854
|
+
while (true) {
|
|
855
|
+
bound = bound.div2();
|
|
856
|
+
if (isLower) cell.lower = bound;
|
|
857
|
+
else cell.upper = bound;
|
|
858
|
+
const sign = this.um.evalSignAtBq(cell.p, bound);
|
|
859
|
+
if (sign === 0) {
|
|
860
|
+
anum.value = bound.toRational();
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
if (sign === targetSign) return;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// inv: x^n * p(1/x), with the interval (1/u, 1/l), in place.
|
|
868
|
+
inv(anum: Anum) {
|
|
869
|
+
if (this.isZero(anum)) throw new SmtInvariantError("division by zero");
|
|
870
|
+
this.refineNonzeroBound(anum);
|
|
871
|
+
if (anum.value instanceof Rational) {
|
|
872
|
+
anum.value = Rational.one.div(anum.value);
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
const cell = anum.value;
|
|
876
|
+
cell.p = this.um.p1DivX(cell.p);
|
|
877
|
+
const interval = this.um.convertQ2bqInterval(
|
|
878
|
+
cell.p,
|
|
879
|
+
Rational.one.div(cell.upper.toRational()),
|
|
880
|
+
Rational.one.div(cell.lower.toRational()),
|
|
881
|
+
);
|
|
882
|
+
if (!interval.isInterval) throw new SmtInvariantError("inversion of algebraic number failed");
|
|
883
|
+
cell.lower = interval.lower;
|
|
884
|
+
cell.upper = interval.upper;
|
|
885
|
+
this.updateSignLower(cell);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
div(first: Anum, second: Anum) {
|
|
889
|
+
if (this.isZero(second)) throw new SmtInvariantError("division by zero");
|
|
890
|
+
const inverse = this.copy(second);
|
|
891
|
+
this.inv(inverse);
|
|
892
|
+
return this.mul(first, inverse);
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
power(anum: Anum, exponent: number): Anum {
|
|
896
|
+
if (exponent === 0) {
|
|
897
|
+
if (this.isZero(anum)) throw new SmtInvariantError("0^0 is indeterminate");
|
|
898
|
+
return new Anum(Rational.one);
|
|
899
|
+
}
|
|
900
|
+
if (exponent === 1) return this.copy(anum);
|
|
901
|
+
if (anum.value instanceof Rational) return new Anum(anum.value.pow(exponent));
|
|
902
|
+
return this.mkPower(anum, exponent);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// mk_add_polynomial: Res_y(pa(x - y), pb(y)), which has a + b as a root.
|
|
906
|
+
private getSumPolynomial(first: AlgebraicCell, second: AlgebraicCell) {
|
|
907
|
+
const pa = this.pm.composeXMinusY(this.pm.fromCoeffs(first.p, X), Y);
|
|
908
|
+
return this.pm.toCoeffs(this.pm.resultant(pa, this.pm.fromCoeffs(second.p, Y), Y));
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// mk_mul_polynomial: Res_y(y^n * pa(x/y), pb(y)), which has a*b as a root.
|
|
912
|
+
private getProductPolynomial(first: AlgebraicCell, second: AlgebraicCell) {
|
|
913
|
+
const pa = this.pm.composeXDivY(this.pm.fromCoeffs(first.p, X), Y);
|
|
914
|
+
return this.pm.toCoeffs(this.pm.resultant(pa, this.pm.fromCoeffs(second.p, Y), Y));
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
// The factors of p whose Sturm sequence counts exactly one root in an interval, until only one
|
|
918
|
+
// factor is left.
|
|
919
|
+
private findIsolatingFactor(
|
|
920
|
+
factors: bigint[][],
|
|
921
|
+
sequences: (Upolynomial[] | null)[],
|
|
922
|
+
interval: BqInterval,
|
|
923
|
+
): IsolatingFactor | null {
|
|
924
|
+
let remaining = 0;
|
|
925
|
+
let target = -1;
|
|
926
|
+
let targetLowerVariations = 0;
|
|
927
|
+
for (let index = 0; index < factors.length; index++) {
|
|
928
|
+
const sequence = sequences[index];
|
|
929
|
+
if (sequence === null) continue;
|
|
930
|
+
const lowerVariations = this.um.signVariationsAt(sequence, interval.lower);
|
|
931
|
+
const count = lowerVariations - this.um.signVariationsAt(sequence, interval.upper);
|
|
932
|
+
if (count <= 0) {
|
|
933
|
+
sequences[index] = null;
|
|
934
|
+
continue;
|
|
935
|
+
}
|
|
936
|
+
if (count === 1) {
|
|
937
|
+
target = index;
|
|
938
|
+
targetLowerVariations = lowerVariations;
|
|
939
|
+
}
|
|
940
|
+
remaining++;
|
|
941
|
+
}
|
|
942
|
+
const sequence = sequences[target];
|
|
943
|
+
return remaining === 1 && sequence !== null && sequence !== undefined
|
|
944
|
+
? { factor: factors[target], sequence, lowerVariations: targetLowerVariations }
|
|
945
|
+
: null;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// mk_binary: a + b or a*b for irrational a and b, refining both until the interval of the result
|
|
949
|
+
// isolates a root of one factor of the resultant.
|
|
950
|
+
private mkBinary(first: Anum, second: Anum, isAdd: boolean) {
|
|
951
|
+
const cellA = getCell(first);
|
|
952
|
+
const cellB = getCell(second);
|
|
953
|
+
const factored = this.factor(
|
|
954
|
+
isAdd ? this.getSumPolynomial(cellA, cellB) : this.getProductPolynomial(cellA, cellB),
|
|
955
|
+
);
|
|
956
|
+
const factors = factored.factors.factors;
|
|
957
|
+
const sequences: (Upolynomial[] | null)[] = factors.map((factor) => this.um.sturmSeq(factor));
|
|
958
|
+
const savedA = this.saveInterval(first);
|
|
959
|
+
const savedB = this.saveInterval(second);
|
|
960
|
+
while (true) {
|
|
961
|
+
const interval = isAdd ? INTERVAL_OPS.add(cellA, cellB) : INTERVAL_OPS.mul(cellA, cellB);
|
|
962
|
+
const found = this.findIsolatingFactor(factors, sequences, interval);
|
|
963
|
+
if (found !== null) {
|
|
964
|
+
this.restoreIfTooSmall(savedA);
|
|
965
|
+
this.restoreIfTooSmall(savedB);
|
|
966
|
+
return this.setCore(found, interval, factored.isComplete);
|
|
967
|
+
}
|
|
968
|
+
if (!this.refine(first) || !this.refine(second)) {
|
|
969
|
+
// Z3 restores a twice here, and b only once the operation is done.
|
|
970
|
+
this.restoreIfTooSmall(savedA);
|
|
971
|
+
this.restoreIfTooSmall(savedA);
|
|
972
|
+
const result = isAdd ? this.add(first, second) : this.mul(first, second);
|
|
973
|
+
this.releaseInterval(savedB);
|
|
974
|
+
return result;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
// mk_power_polynomial: Res_y(x - y^k, pa(y)), which has a^k as a root.
|
|
980
|
+
private getPowerPolynomial(cell: AlgebraicCell, exponent: number) {
|
|
981
|
+
const x = this.pm.sub(this.pm.mkVar(X), this.pm.mkVar(Y, exponent));
|
|
982
|
+
return this.pm.toCoeffs(this.pm.resultant(x, this.pm.fromCoeffs(cell.p, Y), Y));
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
// mk_unary with mk_power_polynomial: a^k for an irrational a.
|
|
986
|
+
private mkPower(anum: Anum, exponent: number): Anum {
|
|
987
|
+
const cell = getCell(anum);
|
|
988
|
+
const factored = this.factor(this.getPowerPolynomial(cell, exponent));
|
|
989
|
+
const factors = factored.factors.factors;
|
|
990
|
+
const sequences: (Upolynomial[] | null)[] = factors.map((factor) => this.um.sturmSeq(factor));
|
|
991
|
+
const saved = this.saveInterval(anum);
|
|
992
|
+
while (true) {
|
|
993
|
+
const interval = getIntervalPower(cell, exponent);
|
|
994
|
+
const found = this.findIsolatingFactor(factors, sequences, interval);
|
|
995
|
+
if (found !== null) {
|
|
996
|
+
this.restoreIfTooSmall(saved);
|
|
997
|
+
return this.setCore(found, interval, factored.isComplete);
|
|
998
|
+
}
|
|
999
|
+
if (!this.refine(anum)) {
|
|
1000
|
+
this.restoreIfTooSmall(saved);
|
|
1001
|
+
return this.power(anum, exponent);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// set_core: the root of p in an isolating interval that may contain zero.
|
|
1007
|
+
private setCore(found: IsolatingFactor, interval: BqInterval, isMinimal: boolean) {
|
|
1008
|
+
const p = found.factor;
|
|
1009
|
+
let lower = interval.lower;
|
|
1010
|
+
let upper = interval.upper;
|
|
1011
|
+
if (containsZero(interval)) {
|
|
1012
|
+
if (this.um.hasZeroRoots(p)) return new Anum(Rational.zero);
|
|
1013
|
+
if (found.lowerVariations === this.um.signVariationsAtZero(found.sequence)) lower = Mpbq.zero;
|
|
1014
|
+
else upper = Mpbq.zero;
|
|
1015
|
+
}
|
|
1016
|
+
const nonzero = this.um.hasZeroRoots(p) ? this.um.removeZeroRoots(p) : p;
|
|
1017
|
+
const refined = this.um.isolating2refinable(nonzero, lower, upper);
|
|
1018
|
+
if (!refined.isInterval) return new Anum(refined.lower.toRational());
|
|
1019
|
+
return this.mkAnum(nonzero, refined.lower, refined.upper, isMinimal);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
// Comparison.
|
|
1023
|
+
|
|
1024
|
+
// compare(cell, b): by the interval, or by the sign of p at b.
|
|
1025
|
+
private compareRational(cell: AlgebraicCell, value: Rational) {
|
|
1026
|
+
if (cell.upper.compareRational(value) <= 0) return -1;
|
|
1027
|
+
if (cell.lower.compareRational(value) >= 0) return 1;
|
|
1028
|
+
const sign = this.um.evalSignAtQ(cell.p, value);
|
|
1029
|
+
if (sign === 0) return 0;
|
|
1030
|
+
return sign === cell.signLower ? 1 : -1;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
compare(first: Anum, second: Anum): number {
|
|
1034
|
+
if (first.value instanceof Rational)
|
|
1035
|
+
return second.value instanceof Rational
|
|
1036
|
+
? first.value.compare(second.value)
|
|
1037
|
+
: -this.compareRational(second.value, first.value);
|
|
1038
|
+
if (second.value instanceof Rational) return this.compareRational(first.value, second.value);
|
|
1039
|
+
return this.compareCore(first, second);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
// compare_core: refines both intervals to a common magnitude, then tries a wider separation, and
|
|
1043
|
+
// last the sign of pb at a by a Sturm-Tarski sequence.
|
|
1044
|
+
private compareCore(first: Anum, second: Anum): number {
|
|
1045
|
+
const cellA = getCell(first);
|
|
1046
|
+
const cellB = getCell(second);
|
|
1047
|
+
const compareIntervals = () =>
|
|
1048
|
+
cellA.upper.compare(cellB.lower) <= 0 ? -1 : cellA.lower.compare(cellB.upper) >= 0 ? 1 : 0;
|
|
1049
|
+
let sign = compareIntervals();
|
|
1050
|
+
if (sign !== 0) return sign;
|
|
1051
|
+
if (sameCoeffs(cellA.p, cellB.p)) return 0;
|
|
1052
|
+
if (cellA.isMinimal && cellB.isMinimal)
|
|
1053
|
+
while (true) {
|
|
1054
|
+
this.refine(first);
|
|
1055
|
+
this.refine(second);
|
|
1056
|
+
sign = compareIntervals();
|
|
1057
|
+
if (sign !== 0) return sign;
|
|
1058
|
+
}
|
|
1059
|
+
const magnitudeA = this.getCellMagnitude(cellA);
|
|
1060
|
+
const magnitudeB = this.getCellMagnitude(cellB);
|
|
1061
|
+
const target = Math.max(MIN_MAGNITUDE, Math.min(magnitudeA, magnitudeB));
|
|
1062
|
+
if (magnitudeB > target) {
|
|
1063
|
+
if (!this.refineTimes(second, magnitudeB - target)) return this.compare(first, second);
|
|
1064
|
+
sign = compareIntervals();
|
|
1065
|
+
if (sign !== 0) return sign;
|
|
1066
|
+
}
|
|
1067
|
+
if (magnitudeA > target) {
|
|
1068
|
+
if (!this.refineTimes(first, magnitudeA - target)) return this.compare(first, second);
|
|
1069
|
+
sign = compareIntervals();
|
|
1070
|
+
if (sign !== 0) return sign;
|
|
1071
|
+
}
|
|
1072
|
+
for (let index = 0; index < target - MIN_MAGNITUDE; index++) {
|
|
1073
|
+
if (!this.refine(first) || !this.refine(second)) return this.compare(first, second);
|
|
1074
|
+
sign = compareIntervals();
|
|
1075
|
+
if (sign !== 0) return sign;
|
|
1076
|
+
}
|
|
1077
|
+
// get_interval at precision 10: copies of the intervals refined below 1/2^40.
|
|
1078
|
+
const intervalA = this.um.refineToPrecision(cellA.p, cellA.lower, cellA.upper, 40);
|
|
1079
|
+
const intervalB = this.um.refineToPrecision(cellB.p, cellB.lower, cellB.upper, 40);
|
|
1080
|
+
if (intervalA.isInterval && intervalB.isInterval) {
|
|
1081
|
+
if (intervalA.lower.compare(intervalB.upper) > 0) return 1;
|
|
1082
|
+
if (intervalA.upper.compare(intervalB.lower) < 0) return -1;
|
|
1083
|
+
}
|
|
1084
|
+
if (this.compareRational(cellA, intervalB.lower.toRational()) < 0) return -1;
|
|
1085
|
+
if (this.compareRational(cellA, intervalB.upper.toRational()) > 0) return 1;
|
|
1086
|
+
if (this.compareRational(cellB, intervalA.lower.toRational()) < 0) return 1;
|
|
1087
|
+
if (this.compareRational(cellB, intervalA.upper.toRational()) > 0) return -1;
|
|
1088
|
+
const sequence = this.um.sturmTarskiSeq(cellA.p, cellB.p);
|
|
1089
|
+
const variations =
|
|
1090
|
+
this.um.signVariationsAt(sequence, cellA.lower) -
|
|
1091
|
+
this.um.signVariationsAt(sequence, cellA.upper);
|
|
1092
|
+
if (variations === 0) return 0;
|
|
1093
|
+
return variations < 0 === cellB.signLower < 0 ? -1 : 1;
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
// Sample points.
|
|
1097
|
+
|
|
1098
|
+
// int_lt: an integer below a.
|
|
1099
|
+
intLt(anum: Anum) {
|
|
1100
|
+
this.refineUntilPrecision(anum, 1);
|
|
1101
|
+
return new Anum(
|
|
1102
|
+
Rational.of(
|
|
1103
|
+
anum.value instanceof Rational ? anum.value.floor().num - 1n : anum.value.lower.floor(),
|
|
1104
|
+
),
|
|
1105
|
+
);
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
// int_gt: an integer above a.
|
|
1109
|
+
intGt(anum: Anum) {
|
|
1110
|
+
this.refineUntilPrecision(anum, 1);
|
|
1111
|
+
return new Anum(
|
|
1112
|
+
Rational.of(
|
|
1113
|
+
anum.value instanceof Rational ? anum.value.ceil().num + 1n : anum.value.upper.ceil(),
|
|
1114
|
+
),
|
|
1115
|
+
);
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
// separate: refines until the intervals of prev and curr do not touch.
|
|
1119
|
+
private separate(previous: Anum, current: Anum) {
|
|
1120
|
+
if (previous.value instanceof Rational) {
|
|
1121
|
+
const value = previous.value;
|
|
1122
|
+
while (
|
|
1123
|
+
current.value instanceof AlgebraicCell &&
|
|
1124
|
+
current.value.lower.compareRational(value) <= 0
|
|
1125
|
+
)
|
|
1126
|
+
this.refine(current);
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
if (current.value instanceof Rational) {
|
|
1130
|
+
const value = current.value;
|
|
1131
|
+
while (
|
|
1132
|
+
previous.value instanceof AlgebraicCell &&
|
|
1133
|
+
previous.value.upper.compareRational(value) >= 0
|
|
1134
|
+
)
|
|
1135
|
+
this.refine(previous);
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
while (
|
|
1139
|
+
previous.value instanceof AlgebraicCell &&
|
|
1140
|
+
current.value instanceof AlgebraicCell &&
|
|
1141
|
+
previous.value.upper.compare(current.value.lower) >= 0
|
|
1142
|
+
) {
|
|
1143
|
+
this.refine(previous);
|
|
1144
|
+
this.refine(current);
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
// select: a binary rational between prev < curr with the smallest denominator.
|
|
1149
|
+
select(previous: Anum, current: Anum) {
|
|
1150
|
+
this.separate(previous, current);
|
|
1151
|
+
const lower: SelectEnd =
|
|
1152
|
+
previous.value instanceof Rational
|
|
1153
|
+
? { value: previous.value, isOpen: true }
|
|
1154
|
+
: { value: previous.value.upper.toRational(), isOpen: false };
|
|
1155
|
+
const upper: SelectEnd =
|
|
1156
|
+
current.value instanceof Rational
|
|
1157
|
+
? { value: current.value, isOpen: true }
|
|
1158
|
+
: { value: current.value.lower.toRational(), isOpen: false };
|
|
1159
|
+
return new Anum(selectSmall(lower, upper).toRational());
|
|
1160
|
+
}
|
|
1161
|
+
}
|