tla-ts 0.0.10-dev.5ffc55ce5 → 0.0.10-dev.6232a1403

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,6 @@
1
+ // Deviation from Z3: ties between polynomials break by creation-order id, where Z3 recycles ids of
2
+ // freed polynomials (id_gen in polynomial.cpp), so a cell bound can be another polynomial with the
3
+ // same root.
1
4
  import { NlsatUnsupportedError } from "../../../../errors";
2
5
  import { Rational } from "../rational";
3
6
  import { type AlgebraicManager, type Anum, mergeSortIndices } from "./algebraic";
@@ -6,26 +9,14 @@ import {
6
9
  type Polynomial,
7
10
  type PolynomialCache,
8
11
  type PolynomialManager,
9
- PolynomialRef,
10
- PolynomialRefVector,
11
12
  } from "./polynomial";
12
13
 
13
- export class CellInterval {
14
- isSection = false;
15
- readonly lower: PolynomialRef;
16
- lowerIndex = 0;
17
- readonly upper: PolynomialRef;
18
- upperIndex = 0;
19
-
20
- constructor(pm: PolynomialManager) {
21
- this.lower = new PolynomialRef(pm);
22
- this.upper = new PolynomialRef(pm);
23
- }
24
-
25
- release() {
26
- this.upper.reset();
27
- this.lower.reset();
28
- }
14
+ interface CellInterval {
15
+ isSection: boolean;
16
+ lower: Polynomial | null;
17
+ lowerIndex: number;
18
+ upper: Polynomial | null;
19
+ upperIndex: number;
29
20
  }
30
21
 
31
22
  interface RootFunction {
@@ -41,123 +32,98 @@ const UNSET = -1;
41
32
  const BOTH_SIDES = 3;
42
33
  const SPANNING_TREE_THRESHOLD = 4;
43
34
 
44
- export const releaseIntervals = (intervals: readonly CellInterval[]) => {
45
- for (let index = intervals.length; index-- > 0;) intervals[index].release();
46
- };
47
-
48
35
  export class TodoSet {
49
- readonly set: PolynomialRefVector;
50
- private inSet: boolean[] = [];
36
+ polynomials: Polynomial[] = [];
37
+ private inSet = new Set<number>();
51
38
 
52
39
  constructor(
40
+ private pm: PolynomialManager,
53
41
  private cache: PolynomialCache,
54
- private isCanonicalizing: boolean,
55
- ) {
56
- this.set = new PolynomialRefVector(cache.pm);
57
- }
42
+ ) {}
58
43
 
59
44
  get isEmpty() {
60
- return this.set.size === 0;
45
+ return this.polynomials.length === 0;
61
46
  }
62
47
 
63
48
  reset() {
64
- for (const polynomial of this.set.items) this.inSet[polynomial.id] = false;
65
- this.set.reset();
49
+ this.polynomials = [];
50
+ this.inSet.clear();
66
51
  }
67
52
 
68
53
  insert(polynomial: Polynomial) {
69
- const pm = this.cache.pm;
70
- if (this.inSet[polynomial.id] === true) return polynomial;
54
+ if (this.inSet.has(polynomial.id)) return polynomial;
71
55
  if (this.cache.contains(polynomial)) {
72
- this.inSet[polynomial.id] = true;
73
- this.set.push(polynomial);
56
+ this.inSet.add(polynomial.id);
57
+ this.polynomials.push(polynomial);
74
58
  return polynomial;
75
59
  }
76
- const pinned = new PolynomialRef(pm);
77
- let p = polynomial;
78
- if (this.isCanonicalizing) {
79
- if (!pm.isZero(p) && !pm.isConst(p)) {
80
- pinned.move(pm.pp(p, pm.maxVar(p)));
81
- p = pinned.get();
82
- } else pinned.set(p);
83
- p = pm.flipSignIfLmNeg(p);
84
- pinned.set(p);
85
- }
86
- p = this.cache.mkUnique(p);
87
- if (this.inSet[p.id] !== true) {
88
- this.inSet[p.id] = true;
89
- this.set.push(p);
60
+ const primitive =
61
+ this.pm.isZero(polynomial) || this.pm.isConst(polynomial)
62
+ ? polynomial
63
+ : this.pm.pp(polynomial, this.pm.maxVar(polynomial));
64
+ const unique = this.cache.mkUnique(this.pm.flipSignIfLmNeg(primitive));
65
+ if (!this.inSet.has(unique.id)) {
66
+ this.inSet.add(unique.id);
67
+ this.polynomials.push(unique);
90
68
  }
91
- pinned.reset();
92
- return p;
93
- }
94
-
95
- contains(polynomial: Polynomial) {
96
- return this.inSet[polynomial.id] === true;
69
+ return unique;
97
70
  }
98
71
 
99
72
  getMaxVar() {
100
- return this.cache.pm.getMaxVarOfPolys(this.set.items);
73
+ return this.pm.getMaxVarOfPolys(this.polynomials);
101
74
  }
102
75
 
103
- extractMaxPolys(out: PolynomialRefVector) {
104
- out.reset();
76
+ extractMaxPolys(out: Polynomial[]) {
77
+ out.length = 0;
105
78
  const variable = this.getMaxVar();
106
79
  this.extractPolysAtLevel(variable, out);
107
80
  return variable;
108
81
  }
109
82
 
110
- extractPolysAtLevel(variable: number, out: PolynomialRefVector) {
111
- const pm = this.cache.pm;
112
- let kept = 0;
113
- for (let index = 0; index < this.set.size; index++) {
114
- const polynomial = this.set.get(index);
115
- if (pm.maxVar(polynomial) === variable) {
116
- out.push(polynomial);
117
- this.inSet[polynomial.id] = false;
118
- } else this.set.set(kept++, polynomial);
83
+ extractPolysAtLevel(variable: number, out: Polynomial[]) {
84
+ const kept: Polynomial[] = [];
85
+ for (const polynomial of this.polynomials) {
86
+ if (this.pm.maxVar(polynomial) !== variable) {
87
+ kept.push(polynomial);
88
+ continue;
89
+ }
90
+ out.push(polynomial);
91
+ this.inSet.delete(polynomial.id);
119
92
  }
120
- this.set.shrink(kept);
93
+ this.polynomials = kept;
121
94
  }
122
95
  }
123
96
 
124
- export class Levelwise {
125
- readonly intervals: CellInterval[] = [];
126
- private level = 0;
127
- private lowerRf = UNSET;
128
- private upperRf = UNSET;
97
+ class Levelwise {
98
+ private intervals: CellInterval[];
129
99
  private todo: TodoSet;
130
- private levelPs: PolynomialRefVector;
131
- private witnesses: PolynomialRef[] = [];
132
- private polyHasRoots: boolean[] = [];
133
- private pscTmp: PolynomialRefVector;
134
- private sideMask: number[] = [];
135
- private isLcOmitted: boolean[] = [];
136
- private degInOrderGraph: number[] = [];
100
+ private level = 0;
101
+ private levelPolys: Polynomial[] = [];
102
+ private witnesses: (Polynomial | null)[] = [];
103
+ private hasRoots: boolean[] = [];
137
104
  private rootFunctions: RootFunction[] = [];
138
105
  private pairs = new Map<string, [number, number]>();
106
+ private sideMask: number[] = [];
107
+ private isLcOmitted: boolean[] = [];
108
+ private lowerRf = UNSET;
109
+ private upperRf = UNSET;
139
110
 
140
111
  constructor(
141
112
  private pm: PolynomialManager,
142
113
  private am: AlgebraicManager,
143
114
  private cache: PolynomialCache,
144
- private polynomials: PolynomialRefVector,
145
- private maxVar: number,
146
115
  private sample: readonly (Anum | undefined)[],
116
+ private maxVar: number,
147
117
  private isLinear: boolean,
148
118
  ) {
149
- this.todo = new TodoSet(cache, true);
150
- this.levelPs = new PolynomialRefVector(pm);
151
- this.pscTmp = new PolynomialRefVector(pm);
152
- for (let index = 0; index < maxVar; index++) this.intervals.push(new CellInterval(pm));
153
- }
154
-
155
- release() {
156
- this.pscTmp.reset();
157
- this.clearWitnesses();
158
- this.levelPs.reset();
159
- this.todo.set.reset();
160
- releaseIntervals(this.intervals);
119
+ this.todo = new TodoSet(pm, cache);
120
+ this.intervals = Array.from({ length: maxVar }, () => ({
121
+ isSection: false,
122
+ lower: null,
123
+ lowerIndex: 0,
124
+ upper: null,
125
+ upperIndex: 0,
126
+ }));
161
127
  }
162
128
 
163
129
  private getSign(polynomial: Polynomial) {
@@ -168,200 +134,93 @@ export class Levelwise {
168
134
  return this.sample.map((value, index) => (index === variable ? undefined : value));
169
135
  }
170
136
 
171
- private clearWitnesses() {
172
- for (let index = this.witnesses.length; index-- > 0;) this.witnesses[index].reset();
173
- this.witnesses = [];
174
- }
175
-
176
- private handleNullifiedPoly(p: Polynomial) {
177
- const pm = this.pm;
178
- const degree = pm.degree(p, this.level);
179
- for (let power = 0; power <= degree; power++) {
180
- const coeff = new PolynomialRef(pm);
181
- coeff.set(pm.coeff(p, this.level, power));
182
- if (!pm.isZero(coeff.get()) && !pm.isConst(coeff.get())) this.requestFactorized(coeff.get());
183
- coeff.reset();
184
- }
185
- let curPolys = new PolynomialRefVector(pm);
186
- let curMinVar: number[] = [];
187
- curPolys.push(p);
188
- curMinVar.push(0);
189
- while (curPolys.size > 0) {
190
- const nextPolys = new PolynomialRefVector(pm);
191
- const nextMinVar: number[] = [];
192
- for (let index = 0; index < curPolys.size; index++) {
193
- const q = new PolynomialRef(pm);
194
- q.set(curPolys.get(index));
195
- const maxVar = pm.maxVar(q.get());
196
- if (maxVar === NULL_VAR) {
197
- q.reset();
198
- continue;
199
- }
200
- for (let x = curMinVar[index]; x <= maxVar; x++) {
201
- if (pm.degree(q.get(), x) === 0) continue;
202
- const dq = new PolynomialRef(pm);
203
- dq.set(pm.derivative(q.get(), x));
204
- if (pm.isZero(dq.get()) || pm.isConst(dq.get())) {
205
- dq.reset();
206
- continue;
207
- }
208
- if (this.getSign(dq.get()) !== 0) {
209
- this.requestFactorized(dq.get());
210
- dq.reset();
211
- q.reset();
212
- nextPolys.reset();
213
- curPolys.reset();
137
+ private requestFactorized(polynomial: Polynomial | null) {
138
+ if (polynomial === null || this.pm.isConst(polynomial)) return;
139
+ for (const factor of this.cache.factor(polynomial))
140
+ if (!this.pm.isConst(factor)) this.todo.insert(factor);
141
+ }
142
+
143
+ private getFirstPsc(first: Polynomial, second: Polynomial, variable: number) {
144
+ for (const element of this.cache.pscChain(first, second, variable)) {
145
+ if (this.pm.isZero(element)) continue;
146
+ return this.pm.isConst(element) ? null : element;
147
+ }
148
+ return null;
149
+ }
150
+
151
+ private getPscDiscriminant(polynomial: Polynomial, variable: number) {
152
+ if (this.pm.isConst(polynomial) || this.pm.degree(polynomial, variable) < 2) return null;
153
+ return this.getFirstPsc(polynomial, this.pm.derivative(polynomial, variable), variable);
154
+ }
155
+
156
+ private handleNullifiedPoly(polynomial: Polynomial) {
157
+ const degree = this.pm.degree(polynomial, this.level);
158
+ for (let power = 0; power <= degree; power++)
159
+ this.requestFactorized(this.pm.coeff(polynomial, this.level, power));
160
+ let current = [{ polynomial, minVar: 0 }];
161
+ while (current.length > 0) {
162
+ const next: { polynomial: Polynomial; minVar: number }[] = [];
163
+ for (const entry of current) {
164
+ const maxVar = this.pm.maxVar(entry.polynomial);
165
+ if (maxVar === NULL_VAR) continue;
166
+ for (let variable = entry.minVar; variable <= maxVar; variable++) {
167
+ if (this.pm.degree(entry.polynomial, variable) === 0) continue;
168
+ const derivative = this.pm.derivative(entry.polynomial, variable);
169
+ if (this.pm.isConst(derivative)) continue;
170
+ if (this.getSign(derivative) !== 0) {
171
+ this.requestFactorized(derivative);
214
172
  return;
215
173
  }
216
- nextPolys.push(dq.get());
217
- nextMinVar.push(x);
218
- dq.reset();
174
+ next.push({ polynomial: derivative, minVar: variable });
219
175
  }
220
- q.reset();
221
- }
222
- for (let index = 0; index < nextPolys.size; index++) {
223
- const dq = new PolynomialRef(pm);
224
- dq.set(nextPolys.get(index));
225
- this.requestFactorized(dq.get());
226
- dq.reset();
227
- }
228
- curPolys.reset();
229
- curPolys = nextPolys;
230
- curMinVar = nextMinVar;
231
- }
232
- }
233
-
234
- private resetInterval(interval: CellInterval) {
235
- interval.isSection = false;
236
- interval.lower.reset();
237
- interval.upper.reset();
238
- interval.lowerIndex = 0;
239
- interval.upperIndex = 0;
240
- }
241
-
242
- private getFirstPsc(chain: PolynomialRefVector) {
243
- const r = new PolynomialRef(this.pm);
244
- for (let index = 0; index < chain.size; index++) {
245
- r.set(chain.get(index));
246
- if (this.pm.isZero(r.get())) continue;
247
- if (this.pm.isConst(r.get())) {
248
- r.reset();
249
- return r;
250
176
  }
251
- return r;
177
+ for (const entry of next) this.requestFactorized(entry.polynomial);
178
+ current = next;
252
179
  }
253
- r.reset();
254
- return r;
255
- }
256
-
257
- private pscDiscriminant(pIn: Polynomial, x: number) {
258
- const pm = this.pm;
259
- if (pm.isZero(pIn) || pm.isConst(pIn) || pm.degree(pIn, x) < 2) return new PolynomialRef(pm);
260
- const p = new PolynomialRef(pm);
261
- p.set(pIn);
262
- const d = new PolynomialRef(pm);
263
- d.set(pm.derivative(p.get(), x));
264
- const chain = this.pscTmp;
265
- chain.reset();
266
- this.cache.pscChain(p.get(), d.get(), x, chain);
267
- const disc = this.getFirstPsc(chain);
268
- d.reset();
269
- p.reset();
270
- return disc;
271
- }
272
-
273
- private pscResultant(a: Polynomial, b: Polynomial, x: number) {
274
- const pa = new PolynomialRef(this.pm);
275
- pa.set(a);
276
- const pb = new PolynomialRef(this.pm);
277
- pb.set(b);
278
- const chain = this.pscTmp;
279
- chain.reset();
280
- this.cache.pscChain(pa.get(), pb.get(), x, chain);
281
- const r = this.getFirstPsc(chain);
282
- pb.reset();
283
- pa.reset();
284
- return r;
285
180
  }
286
181
 
287
- private requestFactorized(polynomial: Polynomial | null) {
288
- const pm = this.pm;
289
- if (polynomial === null || pm.isZero(polynomial) || pm.isConst(polynomial)) return;
290
- const factors = new PolynomialRefVector(pm);
291
- this.cache.factor(polynomial, factors);
292
- for (let index = 0; index < factors.size; index++) {
293
- const f = new PolynomialRef(pm);
294
- f.set(factors.get(index));
295
- if (!pm.isZero(f.get()) && !pm.isConst(f.get())) this.todo.insert(f.get());
296
- f.reset();
297
- }
298
- factors.reset();
299
- }
300
-
301
- private chooseNonzeroCoeff(p: Polynomial, x: number) {
302
- const pm = this.pm;
303
- const degree = pm.degree(p, x);
304
- const coeff = new PolynomialRef(pm);
182
+ private chooseNonzeroCoeff(polynomial: Polynomial, variable: number) {
183
+ const degree = this.pm.degree(polynomial, variable);
305
184
  for (let power = 0; power <= degree; power++) {
306
- coeff.set(pm.coeff(p, x, power));
307
- if (pm.isZero(coeff.get())) continue;
308
- if (pm.isConst(coeff.get())) return coeff;
185
+ const coeff = this.pm.coeff(polynomial, variable, power);
186
+ if (!this.pm.isZero(coeff) && this.pm.isConst(coeff)) return coeff;
309
187
  }
310
- const best = new PolynomialRef(pm);
311
- let bestTotalDegree = 0;
312
- let bestSize = 0;
313
- let bestMaxVar = NULL_VAR;
188
+ let best: Polynomial | null = null;
314
189
  for (let power = 0; power <= degree; power++) {
315
- coeff.set(pm.coeff(p, x, power));
316
- const current = coeff.get();
317
- if (pm.isZero(current) || this.getSign(current) === 0) continue;
318
- const totalDegree = pm.totalDegree(current);
319
- const size = current.size;
320
- const maxVar = pm.maxVar(current);
190
+ const coeff = this.pm.coeff(polynomial, variable, power);
191
+ if (this.pm.isZero(coeff) || this.getSign(coeff) === 0) continue;
192
+ const totalDegree = this.pm.totalDegree(coeff);
193
+ const bestDegree = best === null ? 0 : this.pm.totalDegree(best);
321
194
  if (
322
- best.getOrNull() === null ||
323
- totalDegree < bestTotalDegree ||
324
- (totalDegree === bestTotalDegree &&
325
- (size < bestSize ||
326
- (size === bestSize && (bestMaxVar === NULL_VAR || maxVar < bestMaxVar))))
327
- ) {
328
- best.copy(coeff);
329
- bestTotalDegree = totalDegree;
330
- bestSize = size;
331
- bestMaxVar = maxVar;
332
- }
195
+ best === null ||
196
+ totalDegree < bestDegree ||
197
+ (totalDegree === bestDegree &&
198
+ (coeff.size < best.size ||
199
+ (coeff.size === best.size && this.pm.maxVar(coeff) < this.pm.maxVar(best))))
200
+ )
201
+ best = coeff;
333
202
  }
334
- coeff.reset();
335
203
  return best;
336
204
  }
337
205
 
338
206
  private addProjectionForPoly(
339
- p: Polynomial,
340
- x: number,
341
- nonzeroCoeff: Polynomial | null,
207
+ polynomial: Polynomial,
208
+ variable: number,
209
+ witness: Polynomial | null,
342
210
  isLcAdded: boolean,
343
- isDiscAdded: boolean,
344
211
  ) {
345
- const pm = this.pm;
346
- let isNonzeroCoeffAdded = nonzeroCoeff !== null && !pm.isConst(nonzeroCoeff);
212
+ let isWitnessAdded = witness !== null && !this.pm.isConst(witness);
347
213
  if (isLcAdded) {
348
- const lc = new PolynomialRef(pm);
349
- lc.set(pm.coeff(p, x, pm.degree(p, x)));
350
- this.requestFactorized(lc.get());
351
- if (isNonzeroCoeffAdded && this.getSign(lc.get()) !== 0) isNonzeroCoeffAdded = false;
352
- lc.reset();
214
+ const leadingCoeff = this.pm.lc(polynomial, variable);
215
+ this.requestFactorized(leadingCoeff);
216
+ if (isWitnessAdded && this.getSign(leadingCoeff) !== 0) isWitnessAdded = false;
353
217
  }
354
- if (isDiscAdded) {
355
- const disc = new PolynomialRef(pm);
356
- disc.move(this.pscDiscriminant(p, x));
357
- const value = disc.getOrNull();
358
- if (value !== null) {
359
- this.requestFactorized(value);
360
- if (isNonzeroCoeffAdded && this.getSign(value) !== 0) isNonzeroCoeffAdded = false;
361
- }
362
- disc.reset();
218
+ const discriminant = this.getPscDiscriminant(polynomial, variable);
219
+ if (discriminant !== null) {
220
+ this.requestFactorized(discriminant);
221
+ if (isWitnessAdded && this.getSign(discriminant) !== 0) isWitnessAdded = false;
363
222
  }
364
- if (isNonzeroCoeffAdded) this.requestFactorized(nonzeroCoeff);
223
+ if (isWitnessAdded) this.requestFactorized(witness);
365
224
  }
366
225
 
367
226
  private addPair(first: number, second: number) {
@@ -383,7 +242,7 @@ export class Levelwise {
383
242
 
384
243
  private computeSideMask() {
385
244
  if (this.lowerRf === UNSET) return;
386
- while (this.sideMask.length < this.levelPs.size) this.sideMask.push(0);
245
+ while (this.sideMask.length < this.levelPolys.length) this.sideMask.push(0);
387
246
  this.rootFunctions.forEach((rootFunction, index) => {
388
247
  this.sideMask[rootFunction.psIndex] |= index <= this.lowerRf ? 1 : 2;
389
248
  });
@@ -393,24 +252,20 @@ export class Levelwise {
393
252
  return rootFunctionIndex === UNSET ? UNSET : this.rootFunctions[rootFunctionIndex].psIndex;
394
253
  }
395
254
 
396
- private computeResultantGraphDegree() {
397
- this.degInOrderGraph = Array.from({ length: this.levelPs.size }, () => 0);
398
- for (const pair of this.pairs.values()) {
399
- this.degInOrderGraph[pair[0]]++;
400
- this.degInOrderGraph[pair[1]]++;
401
- }
402
- }
403
-
404
255
  private computeOmitLcBothSides(isLeafRequired: boolean) {
405
- this.isLcOmitted = Array.from({ length: this.levelPs.size }, () => false);
256
+ this.isLcOmitted = this.levelPolys.map(() => false);
406
257
  if (this.rootFunctions.length === 0 || this.pairs.size === 0 || this.sideMask.length === 0)
407
258
  return;
408
- if (isLeafRequired) this.computeResultantGraphDegree();
259
+ const degrees = this.levelPolys.map(() => 0);
260
+ for (const pair of this.pairs.values()) {
261
+ degrees[pair[0]]++;
262
+ degrees[pair[1]]++;
263
+ }
409
264
  const lowerBound = this.getBoundIndex(this.lowerRf);
410
265
  const upperBound = this.getBoundIndex(this.upperRf);
411
- for (let index = 0; index < this.levelPs.size; index++) {
266
+ for (let index = 0; index < this.levelPolys.length; index++) {
412
267
  if (this.sideMask[index] !== BOTH_SIDES) continue;
413
- if (isLeafRequired && this.degInOrderGraph[index] !== 1) continue;
268
+ if (isLeafRequired && degrees[index] !== 1) continue;
414
269
  if (index === lowerBound || index === upperBound) continue;
415
270
  this.isLcOmitted[index] = true;
416
271
  }
@@ -426,24 +281,26 @@ export class Levelwise {
426
281
  }
427
282
 
428
283
  private fillRelationSection() {
429
- if (this.rootFunctions.length === 0) return;
430
284
  for (let index = 0; index < this.lowerRf; index++) this.addPair(index, this.lowerRf);
431
285
  for (let index = this.lowerRf + 1; index < this.rootFunctions.length; index++)
432
286
  this.addPair(this.lowerRf, index);
433
287
  }
434
288
 
435
289
  private buildBothSideSpanningTree() {
436
- const lowerRfs = Array.from({ length: this.levelPs.size }, () => UNSET);
437
- const upperRfs = Array.from({ length: this.levelPs.size }, () => UNSET);
290
+ const lowerRfs = this.levelPolys.map(() => UNSET);
291
+ const upperRfs = this.levelPolys.map(() => UNSET);
438
292
  for (let index = 0; index <= this.lowerRf; index++)
439
293
  lowerRfs[this.rootFunctions[index].psIndex] = index;
440
294
  for (let index = this.upperRf; index < this.rootFunctions.length; index++)
441
295
  upperRfs[this.rootFunctions[index].psIndex] = index;
442
- const both: { psIndex: number; lowerRf: number; upperRf: number }[] = [];
443
- for (let psIndex = 0; psIndex < this.levelPs.size; psIndex++)
444
- if (this.sideMask[psIndex] === BOTH_SIDES)
445
- both.push({ psIndex, lowerRf: lowerRfs[psIndex], upperRf: upperRfs[psIndex] });
446
- both.sort((first, second) => first.lowerRf - second.lowerRf);
296
+ const both = this.levelPolys
297
+ .map((_polynomial, psIndex) => ({
298
+ psIndex,
299
+ lowerRf: lowerRfs[psIndex],
300
+ upperRf: upperRfs[psIndex],
301
+ }))
302
+ .filter((info) => this.sideMask[info.psIndex] === BOTH_SIDES)
303
+ .sort((first, second) => first.lowerRf - second.lowerRf);
447
304
  for (let first = 0; first < both.length - 1; first++) {
448
305
  let closest = first + 1;
449
306
  for (let index = first + 2; index < both.length; index++)
@@ -474,21 +331,21 @@ export class Levelwise {
474
331
  }
475
332
 
476
333
  private isolateRootsAroundSample(
477
- p: Polynomial,
478
334
  psIndex: number,
479
335
  value: Anum,
480
336
  lower: RootFunction[],
481
337
  upper: RootFunction[],
482
338
  ) {
339
+ const polynomial = this.levelPolys[psIndex];
483
340
  const values = this.getValuesWithout(this.level);
484
341
  const mkRootFunction = (root: Anum, index: number) => ({
485
342
  value: this.am.copy(root),
486
- polynomial: p,
343
+ polynomial,
487
344
  index,
488
345
  psIndex,
489
346
  });
490
347
  if (value.value instanceof Rational) {
491
- const closest = this.am.isolateRootsClosest(this.pm, p, values, value.value);
348
+ const closest = this.am.isolateRootsClosest(this.pm, polynomial, values, value.value);
492
349
  closest.roots.forEach((root, index) => {
493
350
  (this.am.compare(root, value) <= 0 ? lower : upper).push(
494
351
  mkRootFunction(root, closest.indices[index]),
@@ -496,7 +353,7 @@ export class Levelwise {
496
353
  });
497
354
  return closest.roots.length > 0;
498
355
  }
499
- const roots = this.am.isolateRoots(this.pm, p, values);
356
+ const roots = this.am.isolateRoots(this.pm, polynomial, values);
500
357
  let lowerIndex = UNSET;
501
358
  let upperIndex = UNSET;
502
359
  let isSection = false;
@@ -543,115 +400,95 @@ export class Levelwise {
543
400
  if (middle === 0) {
544
401
  this.lowerRf = UNSET;
545
402
  this.upperRf = 0;
546
- interval.upper.set(rootFunctions[0].polynomial);
403
+ interval.upper = rootFunctions[0].polynomial;
547
404
  interval.upperIndex = rootFunctions[0].index;
548
405
  return;
549
406
  }
550
407
  this.lowerRf = middle - 1;
551
408
  const lower = rootFunctions[middle - 1];
409
+ interval.lower = lower.polynomial;
410
+ interval.lowerIndex = lower.index;
552
411
  if (this.am.compare(lower.value, value) === 0) {
553
412
  interval.isSection = true;
554
- interval.lower.set(lower.polynomial);
555
- interval.lowerIndex = lower.index;
556
413
  this.upperRf = this.lowerRf;
557
414
  return;
558
415
  }
559
- interval.lower.set(lower.polynomial);
560
- interval.lowerIndex = lower.index;
561
416
  if (middle < rootFunctions.length) {
562
417
  this.upperRf = this.lowerRf + 1;
563
- interval.upper.set(rootFunctions[middle].polynomial);
418
+ interval.upper = rootFunctions[middle].polynomial;
564
419
  interval.upperIndex = rootFunctions[middle].index;
565
420
  }
566
421
  }
567
422
 
568
- private addLinearPolyFromRoot(root: Anum, isLower: boolean, p: PolynomialRef) {
569
- const pm = this.pm;
570
- const value = this.am.toRational(root);
571
- p.set(pm.mkVar(this.level));
572
- const scaled = new PolynomialRef(pm);
573
- scaled.set(pm.mulConst(value.den, p.get()));
574
- const constant = new PolynomialRef(pm);
575
- constant.set(pm.mkConst(value.num));
576
- const difference = new PolynomialRef(pm);
577
- difference.set(pm.sub(scaled.get(), constant.get()));
578
- constant.reset();
579
- p.move(difference);
580
- scaled.reset();
423
+ private addLinearPolyFromRoot(root: Anum, isLower: boolean) {
424
+ const value = root.value as Rational;
425
+ const polynomial = this.pm.sub(
426
+ this.pm.mul(this.pm.mkConst(value.den), this.pm.mkVar(this.level)),
427
+ this.pm.mkConst(value.num),
428
+ );
581
429
  const interval = this.intervals[this.level];
582
430
  if (isLower) {
583
- interval.lower.copy(p);
431
+ interval.lower = polynomial;
584
432
  interval.lowerIndex = 1;
585
433
  } else {
586
- interval.upper.copy(p);
434
+ interval.upper = polynomial;
587
435
  interval.upperIndex = 1;
588
436
  }
589
- this.levelPs.push(p.get());
590
- this.polyHasRoots.push(true);
591
- const witness = this.chooseNonzeroCoeff(p.get(), this.level);
592
- const copy = new PolynomialRef(pm);
593
- copy.copy(witness);
594
- this.witnesses.push(copy);
595
- witness.reset();
437
+ this.levelPolys.push(polynomial);
438
+ this.hasRoots.push(true);
439
+ this.witnesses.push(this.chooseNonzeroCoeff(polynomial, this.level));
440
+ return polynomial;
596
441
  }
597
442
 
598
443
  private addLinearApproximations(value: Anum) {
599
- const pLower = new PolynomialRef(this.pm);
600
- const pUpper = new PolynomialRef(this.pm);
601
444
  const interval = this.intervals[this.level];
602
445
  const mkRootFunction = (polynomial: Polynomial, root: Anum) => ({
603
446
  value: this.am.copy(root),
604
447
  polynomial,
605
448
  index: 1,
606
- psIndex: this.levelPs.size - 1,
449
+ psIndex: this.levelPolys.length - 1,
607
450
  });
608
451
  if (interval.isSection) {
609
452
  if (!this.am.isRational(value))
610
453
  throw new NlsatUnsupportedError("a linear cell around an irrational section");
611
- if (this.pm.totalDegree(interval.lower.get()) > 1) {
612
- this.addLinearPolyFromRoot(value, true, pLower);
613
- this.rootFunctions.splice(this.lowerRf, 0, mkRootFunction(pLower.get(), value));
454
+ if (this.pm.totalDegree(interval.lower!) > 1) {
455
+ const polynomial = this.addLinearPolyFromRoot(value, true);
456
+ this.rootFunctions.splice(this.lowerRf, 0, mkRootFunction(polynomial, value));
614
457
  }
615
- pUpper.reset();
616
- pLower.reset();
617
458
  return;
618
459
  }
619
- const lower = interval.lower.getOrNull();
620
- if (lower !== null && this.pm.totalDegree(lower) > 1) {
460
+ if (interval.lower !== null && this.pm.totalDegree(interval.lower) > 1) {
621
461
  const between = this.am.select(this.rootFunctions[this.lowerRf].value, value);
622
- this.addLinearPolyFromRoot(between, true, pLower);
623
- this.rootFunctions.splice(this.lowerRf + 1, 0, mkRootFunction(pLower.get(), between));
462
+ const polynomial = this.addLinearPolyFromRoot(between, true);
463
+ this.rootFunctions.splice(this.lowerRf + 1, 0, mkRootFunction(polynomial, between));
624
464
  this.lowerRf++;
625
465
  if (this.upperRf !== UNSET) this.upperRf++;
626
466
  }
627
- const upper = interval.upper.getOrNull();
628
- if (upper !== null && this.pm.totalDegree(upper) > 1) {
467
+ if (interval.upper !== null && this.pm.totalDegree(interval.upper) > 1) {
629
468
  const between = this.am.select(value, this.rootFunctions[this.upperRf].value);
630
- this.addLinearPolyFromRoot(between, false, pUpper);
631
- this.rootFunctions.splice(this.upperRf, 0, mkRootFunction(pUpper.get(), between));
469
+ const polynomial = this.addLinearPolyFromRoot(between, false);
470
+ this.rootFunctions.splice(this.upperRf, 0, mkRootFunction(polynomial, between));
632
471
  }
633
- pUpper.reset();
634
- pLower.reset();
635
472
  }
636
473
 
637
474
  private buildInterval() {
638
475
  this.rootFunctions = [];
639
476
  this.pairs.clear();
640
- this.resetInterval(this.intervals[this.level]);
477
+ Object.assign(this.intervals[this.level], {
478
+ isSection: false,
479
+ lower: null,
480
+ lowerIndex: 0,
481
+ upper: null,
482
+ upperIndex: 0,
483
+ });
641
484
  this.lowerRf = UNSET;
642
485
  this.upperRf = UNSET;
643
486
  const value = this.sample[this.level]!;
644
- this.polyHasRoots = Array.from({ length: this.levelPs.size }, () => false);
645
487
  const lower: RootFunction[] = [];
646
488
  const upper: RootFunction[] = [];
647
- for (let index = 0; index < this.levelPs.size; index++)
648
- this.polyHasRoots[index] = this.isolateRootsAroundSample(
649
- this.levelPs.get(index),
650
- index,
651
- value,
652
- lower,
653
- upper,
654
- );
489
+ this.hasRoots = this.levelPolys.map((_polynomial, psIndex) =>
490
+ this.isolateRootsAroundSample(psIndex, value, lower, upper),
491
+ );
655
492
  if (lower.length === 0 && upper.length === 0) return false;
656
493
  this.rootFunctions = [
657
494
  ...this.sortRootFunctions(lower, true),
@@ -664,27 +501,20 @@ export class Levelwise {
664
501
  }
665
502
 
666
503
  private addRelationResultants() {
667
- for (const pair of this.getSortedPairs()) {
668
- const res = this.pscResultant(
669
- this.levelPs.get(pair[0]),
670
- this.levelPs.get(pair[1]),
671
- this.level,
504
+ for (const pair of this.getSortedPairs())
505
+ this.requestFactorized(
506
+ this.getFirstPsc(this.levelPolys[pair[0]], this.levelPolys[pair[1]], this.level),
672
507
  );
673
- this.requestFactorized(res.getOrNull());
674
- res.reset();
675
- }
676
508
  }
677
509
 
678
510
  private addAdjacentRootResultants() {
679
- this.polyHasRoots = Array.from({ length: this.levelPs.size }, () => false);
680
511
  const rootValues: { root: Anum; polynomial: Polynomial }[] = [];
681
512
  const values = this.getValuesWithout(this.maxVar);
682
- for (let index = 0; index < this.levelPs.size; index++) {
683
- const polynomial = this.levelPs.get(index);
513
+ this.hasRoots = this.levelPolys.map((polynomial) => {
684
514
  const roots = this.am.isolateRoots(this.pm, polynomial, values);
685
- this.polyHasRoots[index] = roots.length > 0;
686
515
  for (const root of roots) rootValues.push({ root: this.am.copy(root), polynomial });
687
- }
516
+ return roots.length > 0;
517
+ });
688
518
  if (rootValues.length < 2) return;
689
519
  const order = mergeSortIndices(
690
520
  rootValues.length,
@@ -699,51 +529,32 @@ export class Levelwise {
699
529
  const key = `${low.id},${high.id}`;
700
530
  if (added.has(key)) continue;
701
531
  added.add(key);
702
- const res = this.pscResultant(low, high, this.maxVar);
703
- this.requestFactorized(res.getOrNull());
704
- res.reset();
532
+ this.requestFactorized(this.getFirstPsc(low, high, this.maxVar));
705
533
  }
706
534
  }
707
535
 
708
536
  private addSectionProjections() {
709
- const sectionPolynomial = this.intervals[this.level].lower.get();
537
+ const sectionPolynomial = this.intervals[this.level].lower;
710
538
  const withRoots = new Set(this.rootFunctions.map((rootFunction) => rootFunction.psIndex));
711
- for (let index = 0; index < this.levelPs.size; index++) {
712
- const p = new PolynomialRef(this.pm);
713
- p.set(this.levelPs.get(index));
714
- const witness = new PolynomialRef(this.pm);
715
- witness.copy(this.witnesses[index]);
716
- const isFull = this.levelPs.get(index) === sectionPolynomial || !withRoots.has(index);
717
- this.addProjectionForPoly(p.get(), this.level, witness.getOrNull(), isFull, true);
718
- witness.reset();
719
- p.reset();
720
- }
539
+ this.levelPolys.forEach((polynomial, index) => {
540
+ const isFull = polynomial === sectionPolynomial || !withRoots.has(index);
541
+ this.addProjectionForPoly(polynomial, this.level, this.witnesses[index], isFull);
542
+ });
721
543
  }
722
544
 
723
545
  private addSectorProjections() {
724
- const pm = this.pm;
725
- for (let index = 0; index < this.levelPs.size; index++) {
726
- const p = new PolynomialRef(pm);
727
- p.set(this.levelPs.get(index));
728
- const lc = new PolynomialRef(pm);
729
- lc.set(pm.coeff(p.get(), this.level, pm.degree(p.get(), this.level)));
546
+ this.levelPolys.forEach((polynomial, index) => {
730
547
  const isLcAdded = !(this.isLcOmitted[index] ?? false);
731
- const witness = new PolynomialRef(pm);
732
- witness.copy(this.witnesses[index]);
733
- const value = witness.getOrNull();
548
+ let witness = this.witnesses[index];
734
549
  if (
735
550
  isLcAdded &&
736
- value !== null &&
737
- !pm.isConst(value) &&
738
- !pm.isZero(lc.get()) &&
739
- this.getSign(lc.get()) !== 0
551
+ witness !== null &&
552
+ !this.pm.isConst(witness) &&
553
+ this.getSign(this.pm.lc(polynomial, this.level)) !== 0
740
554
  )
741
- witness.reset();
742
- this.addProjectionForPoly(p.get(), this.level, witness.getOrNull(), isLcAdded, true);
743
- witness.reset();
744
- lc.reset();
745
- p.reset();
746
- }
555
+ witness = null;
556
+ this.addProjectionForPoly(polynomial, this.level, witness, isLcAdded);
557
+ });
747
558
  }
748
559
 
749
560
  private shouldUseSpanningTree() {
@@ -751,19 +562,12 @@ export class Levelwise {
751
562
  if (this.lowerRf === UNSET || this.upperRf === UNSET) return false;
752
563
  if (this.getBoundIndex(this.lowerRf) === this.getBoundIndex(this.upperRf)) return false;
753
564
  let bothCount = 0;
754
- for (let index = 0; index < this.levelPs.size; index++)
565
+ for (let index = 0; index < this.levelPolys.length; index++)
755
566
  if (this.sideMask[index] === BOTH_SIDES && ++bothCount >= SPANNING_TREE_THRESHOLD)
756
567
  return true;
757
568
  return false;
758
569
  }
759
570
 
760
- private clearLevelState() {
761
- this.isLcOmitted = [];
762
- this.pairs.clear();
763
- this.sideMask = [];
764
- this.degInOrderGraph = [];
765
- }
766
-
767
571
  private processLevelWithMode(mode: ProjectionMode, hasInterval: boolean) {
768
572
  if (hasInterval) {
769
573
  if (mode === "section") this.fillRelationSection();
@@ -780,23 +584,21 @@ export class Levelwise {
780
584
  }
781
585
 
782
586
  private collectNonNullWitnesses() {
783
- this.clearWitnesses();
784
- for (let index = 0; index < this.levelPs.size; index++) {
785
- const p = new PolynomialRef(this.pm);
786
- p.set(this.levelPs.get(index));
787
- const witness = this.chooseNonzeroCoeff(p.get(), this.level);
788
- if (witness.getOrNull() === null) this.handleNullifiedPoly(p.get());
789
- const copy = new PolynomialRef(this.pm);
790
- copy.copy(witness);
791
- this.witnesses.push(copy);
792
- if (index + 1 === this.levelPs.size) this.todo.extractPolysAtLevel(this.level, this.levelPs);
793
- witness.reset();
794
- p.reset();
587
+ this.witnesses = [];
588
+ for (let index = 0; index < this.levelPolys.length; index++) {
589
+ const polynomial = this.levelPolys[index];
590
+ const witness = this.chooseNonzeroCoeff(polynomial, this.level);
591
+ if (witness === null) this.handleNullifiedPoly(polynomial);
592
+ this.witnesses.push(witness);
593
+ if (index + 1 === this.levelPolys.length)
594
+ this.todo.extractPolysAtLevel(this.level, this.levelPolys);
795
595
  }
796
596
  }
797
597
 
798
598
  private processLevel() {
799
- this.clearLevelState();
599
+ this.isLcOmitted = [];
600
+ this.pairs.clear();
601
+ this.sideMask = [];
800
602
  this.collectNonNullWitnesses();
801
603
  const hasInterval = this.buildInterval();
802
604
  this.processLevelWithMode(
@@ -810,65 +612,39 @@ export class Levelwise {
810
612
  }
811
613
 
812
614
  private processTopLevel() {
813
- const pm = this.pm;
814
615
  this.collectNonNullWitnesses();
815
616
  this.addAdjacentRootResultants();
816
- for (let index = 0; index < this.levelPs.size; index++) {
817
- const p = new PolynomialRef(pm);
818
- p.set(this.levelPs.get(index));
819
- const lc = new PolynomialRef(pm);
820
- lc.set(pm.coeff(p.get(), this.maxVar, pm.degree(p.get(), this.maxVar)));
821
- let isLcAdded = true;
822
- if (
823
- !(this.polyHasRoots[index] ?? false) &&
824
- !pm.isZero(lc.get()) &&
825
- this.getSign(lc.get()) !== 0
826
- ) {
827
- isLcAdded = false;
828
- this.witnesses[index].reset();
829
- }
830
- this.addProjectionForPoly(
831
- p.get(),
832
- this.maxVar,
833
- this.witnesses[index].getOrNull(),
834
- isLcAdded,
835
- true,
836
- );
837
- lc.reset();
838
- p.reset();
839
- }
840
- }
841
-
842
- private copyIntervals() {
843
- return this.intervals.map((interval) => {
844
- const copy = new CellInterval(this.pm);
845
- copy.isSection = interval.isSection;
846
- copy.lower.copy(interval.lower);
847
- copy.lowerIndex = interval.lowerIndex;
848
- copy.upper.copy(interval.upper);
849
- copy.upperIndex = interval.upperIndex;
850
- return copy;
617
+ this.levelPolys.forEach((polynomial, index) => {
618
+ const leadingCoeff = this.pm.lc(polynomial, this.maxVar);
619
+ const isLcAdded =
620
+ this.hasRoots[index] || this.pm.isZero(leadingCoeff) || this.getSign(leadingCoeff) === 0;
621
+ if (!isLcAdded) this.witnesses[index] = null;
622
+ this.addProjectionForPoly(polynomial, this.maxVar, this.witnesses[index], isLcAdded);
851
623
  });
852
624
  }
853
625
 
854
- singleCell() {
855
- if (this.maxVar === 0) return this.copyIntervals();
856
- this.todo.reset();
857
- for (let index = 0; index < this.polynomials.size; index++) {
858
- const p = new PolynomialRef(this.pm);
859
- p.set(this.polynomials.get(index));
860
- this.requestFactorized(p.get());
861
- p.reset();
862
- }
863
- if (this.todo.isEmpty) return this.copyIntervals();
626
+ computeSingleCell(polynomials: readonly Polynomial[]) {
627
+ if (this.maxVar === 0) return this.intervals;
628
+ for (const polynomial of polynomials) this.requestFactorized(polynomial);
629
+ if (this.todo.isEmpty) return this.intervals;
864
630
  if (this.todo.getMaxVar() === this.maxVar) {
865
- this.level = this.todo.extractMaxPolys(this.levelPs);
631
+ this.level = this.todo.extractMaxPolys(this.levelPolys);
866
632
  this.processTopLevel();
867
633
  }
868
634
  while (!this.todo.isEmpty) {
869
- this.level = this.todo.extractMaxPolys(this.levelPs);
635
+ this.level = this.todo.extractMaxPolys(this.levelPolys);
870
636
  this.processLevel();
871
637
  }
872
- return this.copyIntervals();
638
+ return this.intervals;
873
639
  }
874
640
  }
641
+
642
+ export const getSingleCell = (
643
+ pm: PolynomialManager,
644
+ am: AlgebraicManager,
645
+ cache: PolynomialCache,
646
+ polynomials: readonly Polynomial[],
647
+ maxVar: number,
648
+ sample: readonly (Anum | undefined)[],
649
+ isLinear: boolean,
650
+ ) => new Levelwise(pm, am, cache, sample, maxVar, isLinear).computeSingleCell(polynomials);