Skip to content

Lex Groebner bases by sparse and classical FGLM (option -x) - #401

Merged
vneiger merged 69 commits into
algebraic-solving:masterfrom
jerebertho:fglm
Nov 27, 2026
Merged

vneiger merged 69 commits into
algebraic-solving:masterfrom
jerebertho:fglm

Conversation

@jerebertho

@jerebertho jerebertho commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Lex Groebner bases by sparse and classical FGLM (option -x)

This PR adds the computation of reduced lexicographical Groebner bases of
zero-dimensional ideals by the FGLM algorithm, over prime fields and over the
rationals.

It first tries the sparse-FGLM algorithm and, if the ideal is in shape position,
it returns the elimination polynomial and the parametrizations as polynomials.
Otherwise, it switches to the classical FGLM algorithm to compute the whole basis.

Sparse-FGLM computes the scalar sequence s_i = <1, M_n^i r>, for r random, and runs
Berlekamp-Massey on it. Berlekamp-Massey is checked regularly for early termination:
as soon as the linear complexity L of the sequence is less than the degree D of the
ideal and the minimal generator predicts the last 32 terms, the ideal is very likely
not in shape position. The computation then stops after about 2L + 32 matrix-vector
products and switches to classical FGLM. This keeps the overhead of trying
sparse-FGLM first small for ideals not in shape position.

New option -x, --lex-groebner-basis:
-x 0 default output (default)
-x 1 leading ideal of the lex Groebner basis
-x 2 full reduced lex Groebner basis
-x cannot be combined with -g, -S, -C or -n. With -e, msolve falls back to
-g 1 / -g 2 (as for parametrizations).

If the ideal has positive dimension, -x 1 and -x 2 output [1, nvars, -1, []]:, as the
default call. If it is the unit ideal, they output the lex Groebner basis [1].

Over prime fields of characteristic less than 2^15, the default call now computes the
reduced lex Groebner basis (as -x 2), unless one of the options -P, -g, -e, -S,
-C, -n, --boolean or --boolean-zeros is given, or the field is GF(16) or GF(256).
It is printed in the default output format: [-1]: if there is no solution,
[1, nvars, -1, []]: in positive dimension, and in dimension 0 the header of a rational
parametrization w.r.t. the last variable (characteristic, number of variables, degree of
the ideal, variables, linear form), followed by the polynomials of the lex basis instead
of the parametrization. For instance, for cyclic5 modulo 7919:

[0, [7919, 
5, 
70, 
['x1', 'x2', 'x3', 'x4', 'x5'],
[0,0,0,0,1],
[1*x5^15+122*x5^10+7797*x5^5+7918,
...
1*x1^1+1*x2^1+1*x3^1+1*x4^1+1*x5^1]]]:

As for -P 0, -x 0 gives the default output. The description of the default output
in the help is updated accordingly:

Default output file format (changed by some command line options, see below):

  - When there is no solution in an algebraic closure of the base field:
      [-1]:
  - When there are infinitely many solutions in an algebraic closure of the
    base field:
      [1, nvars, -1, []]:
  - When there are finitely many solutions in an algebraic closure of the base
    field:
      - over prime fields of characteristic less than 2^15: the reduced
        Groebner basis w.r.t. the lexicographical ordering (in the format
        of a parametrization w.r.t. the last variable);
      - over prime fields of characteristic at least 2^15: a rational
        parametrization of the solutions;
      - over the rationals: real solutions to the input system (see the -P
        flag to recover a parametrization of the solutions).

The tests bug-68 (characteristic 257) and mq-2-1 (characteristic 251) call msolve
without option: their expected outputs are now lex Groebner bases in this format. The new
test one-8 checks the output [-1]: for the unit ideal modulo 251. Since the lex basis
does not depend on the random projection of sparse-FGLM (classical FGLM is used after an
unlucky one), diff_bug-68.sh no longer fails for some seeds, which was the problem of
issue #393; with -P 2, an unlucky projection can of course still happen.

Over QQ,

  • -x 1 behaves as -g 1: the leading ideal is computed modulo one prime;
  • the shape position test is done modulo the first prime; if it succeeds, sparse-FGLM
    is used for all the other primes and the polynomials of the lex Groebner basis are
    lifted; otherwise, a tracer records the needed rows of the multiplication matrices
    and the leading monomials of the lex Groebner basis, and classical FGLM is used for
    all the other primes;
  • option -L 1 to lift the multiplication matrices is available, at the cost of a
    higher memory usage;
  • with several threads, the computations modulo the first prime (F4, multiplication
    matrices, FGLM) are multi-threaded, then several primes are handled in parallel, each
    on one thread; the rational reconstruction is sequential.

Over a prime field whose characteristic is a 31-bit prime (timing for FGLM, memory overall)

Random-14 Degree Size of basis #terms in basis msolve (s) msolve -P 2 (s) msolve (MB) msolve -P 2 (MB)
shape 16384 14 229390 210 210 2700 2700
2-thick 16384 15 241679 570 200 4300 N/A
7-thick 16384 134 1536558 670 N/A 4300 N/A
12-thick 16384 4097 39902674 590 N/A 5800 N/A
14-thick 16384 16383 78390784 940 N/A 8700 N/A

Over QQ (timing and memory overall)

Random-7 Degree Size of basis #terms in basis max bitsize msolve (s) msolve -L 1 (s) msolve (MB) msolve -L 1 (MB)
shape 128 7 903 927996 300 290 370 390
2-thick 128 8 1000 381351 40 40 130 150
5-thick 128 33 3260 45764 11 12 42 73
7-thick 128 127 7413 2494 0.60 0.69 22 35
Random-8 Degree Size of basis #terms in basis max bitsize msolve (s) msolve -L 1 (s) msolve (MB) msolve -L 1 (MB)
shape 256 8 2056 1288970 860 790 1100 1100
2-thick 256 9 2249 482868 150 140 330 390
6-thick 256 65 12006 26023 15 16 80 180
8-thick 256 255 26884 895 1.0 1.1 29 54

The output follows that of -g 1 and -g 2, with
-x 1:

#Leading ideal data
#---
#field characteristic: 1073741827
#variable order:       z1, z2, z3
#monomial order:       lexicographical
#length of basis:      7 elements sorted by increasing leading monomials
#---
[z3^10,
z2^1*z3^6,
z2^2*z3^4,
z2^3,
z1^1*z3^4,
z1^1*z2^1,
z1^2]:

-x 2:

#Reduced Groebner basis data
#---
#field characteristic: 1073741827
#variable order:       z1, z2, z3
#monomial order:       lexicographical
#length of basis:      7 elements sorted by increasing leading monomials
#---
[1*z3^10+1073741822*z3^9+715827893*z3^8+357913939*z3^7+238609291*z3^6+715827887*z3^5+278377512*z3^4+994205395*z3^3+1007461467*z3^2+1060485755*z3^1+786526935,
1*z2^1*z3^6+1073741824*z2^1*z3^5+2*z2^1*z3^4+1*z2^1*z3^3+715827884*z2^1*z3^2+357913942*z2^1*z3^1+636291453*z2^1+1020054737*z3^9+161061267*z3^8+322122560*z3^7+107374180*z3^6+483183811*z3^5+483183829*z3^4+250539763*z3^3+202817899*z3^2+932564660*z3^1+65617556,
1*z2^2*z3^4+1073741825*z2^2*z3^3+715827885*z2^2*z3^2+357913943*z2^2*z3^1+238609295*z2^2+1073741825*z2^1*z3^5+8*z2^1*z3^4+715827876*z2^1*z3^3+954437182*z2^1*z3^1+954437180*z2^1+493921237*z3^9+450971586*z3^8+472446368*z3^7+944892832*z3^6+279172877*z3^5+780252395*z3^4+773094105*z3^3+973525924*z3^2+789796769*z3^1+692762320,
1*z2^3+214748360*z2^2*z3^3+9*z2^2*z3^2+429496726*z2^2*z3^1+644245101*z2^2+128849016*z2^1*z3^5+85899371*z2^1*z3^4+429496675*z2^1*z3^3+558345793*z2^1*z3^2+601295411*z2^1*z3^1+515396085*z2^1+355709174*z3^9+537472306*z3^8+487392707*z3^7+34188067*z3^6+160717675*z3^5+399517883*z3^4+5325663*z3^3+47588290*z3^2+107631877*z3^1+98011159,
1*z1^1*z3^4+1073741825*z1^1*z3^3+715827885*z1^1*z3^2+357913943*z1^1*z3^1+238609295*z1^1+944892808*z2^2*z3^3+314964269*z2^2*z3^2+930576250*z2^2*z3^1+653789468*z2^2+128849019*z2^1*z3^5+171798692*z2^1*z3^4+701511329*z2^1*z3^3+973525922*z2^1*z3^2+396091429*z2^1*z3^1+606067609*z2^1+353046315*z3^9+856416470*z3^8+3435992*z3^7+865865403*z3^6+851262512*z3^5+934298557*z3^4+373948493*z3^3+48294520*z3^2+240231837*z3^1+381679428,
1*z1^1*z2^1+429496729*z1^1*z3^3+3*z1^1*z3^2+858993460*z1^1*z3^1+214748367*z1^1+1005022350*z2^2*z3^3+463856469*z2^2*z3^2+107374183*z2^2*z3^1+90194313*z2^2+1030792155*z2^1*z3^5+747324309*z2^1*z3^4+584115556*z2^1*z3^3+970662610*z2^1*z3^2+712964576*z2^1*z3^1+695784702*z2^1+886893570*z3^9+343992517*z3^8+82222864*z3^7+958155659*z3^6+568550593*z3^5+646633096*z3^4+930599163*z3^3+405547988*z3^2+300510277*z3^1+274860726,
1*z1^2+858993458*z1^1*z3^3+6*z1^1*z3^2+644245097*z1^1*z3^1+429496730*z1^1+936302873*z2^2*z3^3+927712938*z2^2*z3^2+214748366*z2^2*z3^1+180388627*z2^2+987842483*z2^1*z3^5+420906791*z2^1*z3^4+94489285*z2^1*z3^3+867583393*z2^1*z3^2+352187321*z2^1*z3^1+317827581*z2^1+700045313*z3^9+687985034*z3^8+164445728*z3^7+842569491*z3^6+63359359*z3^5+219524365*z3^4+787456499*z3^3+811095980*z3^2+601020546*z3^1+549721456]:

Similar outputs are provided over QQ, with the same convention as for -g 1 and -g 2:
a coefficient 1 of a non-constant monomial and an exponent 1 are not printed.

Verbose mode prints information as follows:

fglm steps                                                 new data         time(rd) in sec (real|cpu)
------------------------------------------------------------------------------------------------------
multiplication matrix                                                             0.00 | 0.00
scalar sequence                                                                   0.00 | 0.00
elimination polynomial                                 1 new                      0.00 | 0.00
nf                       42 x 148        50.00%       14 new       0 zero         0.00 | 0.00
variable   5                                           5 new                      0.00 | 0.00
nf                       16 x 114        80.37%        1 new       0 zero         0.00 | 0.00
nf                       44 x 128        42.44%       15 new       0 zero         0.00 | 0.00
variable   4                                          10 new                      0.00 | 0.00
variable   3                                          10 new                      0.00 | 0.00
variable   2                                           5 new                      0.00 | 0.00
variable   1                                           1 new                      0.00 | 0.00
variable   0                                           1 new                      0.00 | 0.00
------------------------------------------------------------------------------------------------------

---------------- TIMINGS ----------------
overall(elapsed)        0.00 sec
overall(cpu)            0.00 sec
normal forms(elapsed)   0.00 sec
-----------------------------------------

---------- COMPUTATIONAL DATA -----------
degree of ideal                 128
#dense rows                     325
#normal forms                    30
deg. elim. pol.                  24
size of basis                    33
#terms in basis                3260
-----------------------------------------

where

  • multiplication matrix is the computation of the last multiplication matrix, as in the -P 2 case;
  • scalar sequence is the computation of the sequence for sparse-FGLM;
  • each nf row is the computation of the needed rows of the multiplication matrices;
  • elimination polynomial is the computation of the univariate polynomial in the last variable, as in the -P 2 case
    (first from the scalar sequence and, if the ideal is not in shape position, by classical FGLM);
  • variable k is the computation of all the polynomials involving variable k and no larger variable;
  • #dense rows is the number of dense rows in all the multiplication matrices together, without duplicates,
    i.e. the size of the staircase and its border;
  • #normal forms is the number of normal forms computed for the matrices;
  • deg. elim. pol. is the degree of the elimination polynomial, as in the -P 2 case;
  • size of basis and #terms in basis are the number of polynomials and the sum of the sizes of their supports.

As a consequence, the verbose output of the parametrization and normal form computations has been slightly
modified.

Parametrization:

  • before:
fglm steps                                               time(rd) in sec (real|cpu)
------------------------------------------------------------------------------------------------------
normal forms
      13 x 19         21.86%        5 new       0 zero         0.00 | 0.00
------------------------------------------------------------------------------------------------------
multiplication matrix                                          0.00 | 0.00
scalar sequence                                                0.00 | 0.00
elimination polynomial                                         0.00 | 0.00
parametrizations                                               0.00 | 0.00
------------------------------------------------------------------------------------------------------
  • with this PR:
fglm steps                                                 new data         time(rd) in sec (real|cpu)
------------------------------------------------------------------------------------------------------
nf                       13 x 19         21.86%        5 new       0 zero         0.00 | 0.00
multiplication matrix                                                             0.00 | 0.00
scalar sequence                                                                   0.00 | 0.00
elimination polynomial                                 1 new                      0.00 | 0.00
parametrizations                                       5 new                      0.00 | 0.00
------------------------------------------------------------------------------------------------------

Normal forms with option -n:

  • before:
       6 x 16         48.96%        2 new       0 zero         0.00 | 0.00
------------------------------------------------------------------------------------------------------
  • with this PR:
                           mat          density            new data         time(rd) in sec (real|cpu)
------------------------------------------------------------------------------------------------------
nf                        6 x 16         48.96%        2 new       0 zero         0.00 | 0.00
------------------------------------------------------------------------------------------------------

Although the option is -x 1 or -x 2, internally it uses print_gb with values 3 and 4 respectively.
Therefore, if you prefer these computations to be called with -g 3 and -g 4, this
is an easy change.

Use of generative AI
AI-assisted code writing, debugging and testing.

@jerebertho

Copy link
Copy Markdown
Contributor Author

There seems to be a bug with ideal [1] that makes the CI fail. I will check this on another computer.

@jerebertho
jerebertho marked this pull request as ready for review November 16, 2026 15:39
Comment thread src/msolve/msolve.c Outdated
/* create the lex Gb [1] */
FILE *of = (files->out_file != NULL) ?
fopen(files->out_file, "ab") : OUTSTREAM;
fglm_gb_t *one= malloc(sizeof(fglm_gb_t));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one is never freed, maybe add free_fglm_gb(one).

Comment thread src/msolve/msolve.c Outdated
}
fprintf(ERRSTREAM, "Computing Groebner basis\n");
print_gb=2;
print_gb -= 2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment running msolve -e k over QQ prints a basis, here we set a print_gb=0 to -2.
Maybe better do something like

print_gb = (print_gb == 3) ? 1 : 2;

like it is done in the finite field branch of the code?

Comment thread src/msolve/lifting-lexgb.c Outdated
Comment on lines +639 to +672
const int lifted = use_ml && ML.lifted;
const int K = lifted ? Kmax : nthr;
for(int i = 0; i < K; i++){
prime = lexgb_next_prime(prime, bs, p0);
bprimes[i] = prime;
lprimes[i] = prime;
}
if(lifted){
lexgb_matlift_residues(bmat, mstride, pbad, &ML, lprimes, K, nthr);
} else {
nf4 += K;
}
if(info_level && first_batch){
fprintf(VERBSTREAM, "New prime = %u\n", bprimes[0]);
}
#pragma omp parallel for num_threads(nthr) schedule(dynamic)
for(int i = 0; i < K; i++){
CF_t *ri = bres + (uint64_t)i * (tr.lex.ncoeffs + 1);
if(lifted){
/* lifted matrices: neither F4 nor core_nf */
int bad = 0;
if(pbad[i]){
bad = 3; /* p divides a denominator */
} else {
sp_multimatfglm_t *Mq =
lexgb_matrices_view(&tr, bmat + (uint64_t)i * mstride,
bprimes[i]);
if(fglm_classic_apply(ri, Mq, &tr)) bad = 1;
Mq->dense_mat = NULL;
free_sp_multimat_fglm(Mq);
}
bbad[i] = bad;
continue;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I understand the code completely, but this should be with -L1 option. Do here later primes get their matrices from the already lifted ones? Shouldn't it get them directly from f4?

@jerebertho jerebertho Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear, if the matrix has been lifted with -L 1, then the code takes this matrix for the new prime, reduces it and then does its FGLM stuff. Otherwise, it performs the code below (i.e. runs F4, builds the matrices), which, in the next commit will be in an else (with the continue removed) and properly commented.

/* progress display: {n} = good primes used, p0 included, as for the
* parametrizations (msolve_trace_qq) */
int32_t lstart = dl->lstart;
while(ok && !done && consec_bad < LEXGB_MAX_BAD){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if an accepted prime gives correct leading monomials but wrong coeffs? Don't we need a cap on the number of primes or something in order to prevent an infinite loop here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But how to cap this? Maybe the coefficient is very large, isn't it?
I don't think we have such limit in other multi-modular code. But I will check again.
If not, we might need discuss about this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This non-resolved comment made me curious: does this mean msolve can infinitely-loop in multimodular approaches (in other places maybe, at least in the code here apparently)? I thought it was a Monte Carlo algorithm?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an explicit example with an infinite loop, obtained with the help of Claude Opus 5.5. My usual viewpoint is that we do not merge something when termination is known to fail on some input, but if you confirm that this already happens in other places in msolve and is declared ok, then I will adapt.

There are two systems, the base one, and the modified one (just one constant coefficient changed, if you open the file you cannot miss this coefficient).
Called with ./msolve -f lexgb-loop-qq.ms -x 2 -L 1 -t 1 --random-seed 7 -v 2 -o out1.res , and
./msolve -f lexgb-loop-base-qq.ms -x 2 -L 1 -t 1 --random-seed 7 -v 2 -o out2.res,
we can see with diff that out1.res and out2.res give the same shape lex bases, which should not be.

Called with -t 2, lexgb-loop-qq.ms does not terminate (the base system takes a few seconds). Building a system with -t 1 should be feasible too, although more difficult.

systems.tar.gz

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last commits fix the infinite loop with -t 2.

With -t 1, or on the base system, the verbose output is

multi-modular steps
------------------------------------------------------------------------------------------------------
{2}{4}{8}{16}{32}{64}<33.33%>{128}(->2){256}[L](->4){512}(->8){1024}(->16){2048}(->32){4096}<100.00%>
------------------------------------------------------------------------------------------------------

(with -t 1, the modified system still returns the lex basis of the base system: with a fixed seed, all the primes used can divide the modified constant, and this cannot be detected).

With -t 2, the modified system now gives

multi-modular steps
------------------------------------------------------------------------------------------------------
{2}{4}{8}{16}{32}{64}<33.33%>{128}(->2){256}[L]<-L given up>(->4){512}(->8){1024}(->16){2048}(->32){4096}(->64){8192}(->128){16384}(->256){32768}^C

The prime in flight, which ran F4 on the actual input, disagrees with the lifted matrices, so -L is given up before any image is computed from them. From then on, the computation is exactly the one without -L. It terminates, but it is very long on this input: with a 15,000-bit constant, the lex basis has huge coefficients.

On a smaller example built the same way (3 variables, 302-bit constant), -x 2 -L 1 -t 2 used to loop and now terminates in 0.02 s, with the same output as -x 2 -t 2.

The same mixing exists on master in the lifting of the parametrization with -P 2 -L 1 (for -t 2 and more). It is fixed in a separate PR.

@ederc

ederc commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

One question about the shape position case:
Cannot we compute the elimination polynomial with the existing sparse fglm first, if it's degree is D then we should be able to get the lex GB directly from the parametrization, right? This should get O(D^3) down to O(D^2), or am I wrong here?

@jerebertho
jerebertho marked this pull request as draft November 17, 2026 09:57
Comment thread src/fglm/fglm_classic.c Outdated
static inline CF_t fglmc_negmod(CF_t a, CF_t p){ return a ? p - a : 0; }
static inline CF_t fglmc_mulmod(CF_t a, CF_t b, CF_t p){
return (CF_t)(((uint64_t)a * b) % p);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a slow way to do modular multiplication. But anyway it seems this is not used anywhere, is it? in that case it could be removed

Comment thread src/fglm/fglm_classic.c Resolved
Comment thread src/fglm/fglm_classic.c Resolved
Comment thread src/fglm/fglm_classic.c Resolved
Comment thread src/fglm/fglm_classic.c Resolved
Comment thread src/fglm/fglm_classic.c Resolved
@vneiger

vneiger commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

I put some comments on the basic linear algebra tools, after a quick reading of fglm_classic. One thing that could be very useful to both developers and users is a rough description of the algorithm (as a comment in the code I guess?), or a reference if it is a direct implementation of one algorithm in the literature (I think it is not? there is some blocking strategy with several monomials at a time, at least?).

Another interesting addition would be a performance comparison with the existing code for the shape case, and as mentioned before in this thread, a branching mechanism so that this goes the shape case route when applicable.

@jerebertho
jerebertho force-pushed the fglm branch 3 times, most recently from 1d33b1d to 3683e05 Compare November 19, 2026 23:08
@jerebertho jerebertho changed the title Classical FGLM for msolve Lex Groebner bases by sparse and classical FGLM (option -x) Nov 19, 2026
@jerebertho
jerebertho marked this pull request as ready for review November 20, 2026 00:02
@jerebertho
jerebertho marked this pull request as draft November 20, 2026 08:54
@jerebertho
jerebertho marked this pull request as ready for review November 20, 2026 13:36
@jerebertho
jerebertho force-pushed the fglm branch 2 times, most recently from abb4324 to f9cbff9 Compare November 22, 2026 07:34
@jerebertho
jerebertho marked this pull request as draft November 23, 2026 09:24
@jerebertho

Copy link
Copy Markdown
Contributor Author

The PR has been rebased and now uses the same machinery as #409: the output time is reported separately and integer coefficients are converted through mpz_out_str_10().

@jerebertho
jerebertho marked this pull request as ready for review November 23, 2026 13:29
jerebertho and others added 18 commits November 26, 2026 17:03
The dense rows of T_n are stored transposed once, so that the products by
x_n are D dot products of length nd (libfglm AVX2/AVX-512 kernels), instead
of a combination of rows. Used when all the rows of T_n are known and
p <= 1515531528; otherwise the previous product is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…/64 points

At most (2L + 32)/64 points more than the 2L + 32 needed, instead of
(2L + 32)/8; Berlekamp-Massey stays negligible (30 ms vs 12 ms on a
D = 4096 example, sequence 2.79 s -> 2.36 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For -x 1 and -x 2 over Q, sparse-FGLM is tried on the first prime. If the
minimal polynomial of x_n has degree D (radical or not), the lex Gb is in
shape position: every prime then uses sparse-FGLM instead of
fglm_classic_apply (after traced F4 or with the matrices lifted by -L),
and the lifting is unchanged. Otherwise classical FGLM is used as before.

The variables outside the staircase (leading monomials of linear DRL
polynomials) are passed as linvars; their coordinates are computed from
NF(x_i), the row of T_i at 1, since there is no DRL Gb with -L. No
radical check (squvars is zero).

Same outputs; dq7 (D = 128): 99.6 s -> 85 s (-t 1).

Co-Authored-By: Claude <noreply@anthropic.com>
The lex Groebner bases computed by FGLM (mod p and over Q), the unit
ideal and the positive-dimensional output of -x were written outside the
output timer, so their writing time was counted as computation time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
As for the DRL Groebner bases, the coefficients of lex Groebner bases
over Q, stored as mpz_t by the lifting, are written with mpz_out_str_10
(FLINT's fmpz_get_str) instead of being copied to an fmpz_t and written
with fmpz_fprint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Solve issue algebraic-solving#393 by calling classical FGLM by default on diff_bug-68.
The matrix-vector products of sparse-FGLM (linalg-fglm.c) accumulate 8
products and need p <= DOT2_ACC8_MAX_MODULUS = 1515531528, while
FGLM_SPARSE_MAX_PRIME was 1518500213: primes in between could give wrong
results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Over prime fields of characteristic less than 2^15, the default call
still computes the lex Groebner basis (as -x 2), but prints it in the
default output format: [-1] if there is no solution, [1, nvars, -1, []]
in positive dimension, and in dimension 0 the header of a rational
parametrization w.r.t. the last variable (characteristic, number of
variables, degree of the ideal, variables, linear form [0,...,0,1])
followed by the list of the polynomials of the lex basis.

print_lex_basis is split into print_lex_basis_header and
print_lex_basis_polys, the latter being used by print_lex_basis_default.
The internal value PRINT_LEX_DEFAULT of print_gb selects this output.
New test one-8 (unit ideal modulo 251); expected outputs of bug-68 and
mq-2-1 updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vneiger

vneiger commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

For -L 1 and prime limit, ok.

For shape over QQ: yes what I had in mind was recovering the lex basis from the parametrization (when we are sufficient conditions to do this). It can be kept as a future idea. If you feel like doing it, I think there should be a at least a note somewhere (tutorial?) warning the user so that they are aware that -P 2 can be much faster than -x 2 over the rationals, especially when the radical is in shape position.

For #393, I'm not sure how to interpret your answer, i.e. if it is "this issue is solved by this PR" or not. Calling msolve with -P 2 on the example still gives different output depending on the seed, and calling msolve with default options on the example I gave will give different output depending on the seed (and even wrong output in some other cases, as you mention). I find no mention of such possibilities in the documentation (the tutorial). So for the user, this is very much like a bug, still open. If you think this PR closes 393, feel free to merge the PR and close the issue when you merge the PR, I can open another issue that explains the one I have in mind in more clear terms.

@vneiger

vneiger commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

(minor cleaning, maybe: output_files/bug-68.P2.d0.res is a dead file now I think?)

@jerebertho

Copy link
Copy Markdown
Contributor Author

For -L 1 and prime limit, ok.

For shape over QQ: yes what I had in mind was recovering the lex basis from the parametrization (when we are sufficient conditions to do this). It can be kept as a future idea. If you feel like doing it, I think there should be a at least a note somewhere (tutorial?) warning the user so that they are aware that -P 2 can be much faster than -x 2 over the rationals, especially when the radical is in shape position.

For #393, I'm not sure how to interpret your answer, i.e. if it is "this issue is solved by this PR" or not. Calling msolve with -P 2 on the example still gives different output depending on the seed, and calling msolve with default options on the example I gave will give different output depending on the seed (and even wrong output in some other cases, as you mention). I find no mention of such possibilities in the documentation (the tutorial). So for the user, this is very much like a bug, still open. If you think this PR closes 393, feel free to merge the PR and close the issue when you merge the PR, I can open another issue that explains the one I have in mind in more clear terms.

I am committing an update on the doc/msolve-tutorial.tex about all of this. For #393, my answer is more like "it depends what we consider the issue to be". If it is about make check failing, then yes, it is resolved (and the dead file is going away in the same set of commits). If the issue is that -P 2 may compute a wrong parametrization of the radical, then we can keep the issue open.

@mohabsafey

Copy link
Copy Markdown
Contributor

When the ideal is radical and in shape position over QQ, do you compute the lex basis from the rational parametrization or do you compute the lex basis through a multi-modular process directly? I think it could be worse to compute the lex basis (for this radical and shape case) from the rational parametrization. These are reduced to extended euclidean division-like algorithms which you could delegate to FLINT. I wouldn't be surprised that this is more efficient than computing the lex basis from a complete multi-modular run

@vneiger

vneiger commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

When the ideal is radical and in shape position over QQ, do you compute the lex basis from the rational parametrization or do you compute the lex basis through a multi-modular process directly? I think it could be worse to compute the lex basis (for this radical and shape case) from the rational parametrization. These are reduced to extended euclidean division-like algorithms which you could delegate to FLINT. I wouldn't be surprised that this is more efficient than computing the lex basis from a complete multi-modular run

If I got it correctly: through multi-modular, and experiments show that it could be faster from the parametrization (using rational reconstruction / XGCD as you say), but this is kept for another improvement.

jerebertho and others added 2 commits November 27, 2026 14:05
The rows of the multiplication matrices are frozen one by one, and the
primes of the batch in which their lifting completes have already run F4:
their lex residues entered the CRT without any check. If the lifted rows
are wrong (e.g. all their primes divide some coefficient of the input,
which can be forced with a fixed seed), these residues are mixed with the
images computed from the wrong matrices and the lifting never terminates
(with -t 2 and more).

Now every prime that ran F4 is checked against the rows already frozen,
including the primes of that batch; on a mismatch, -L is given up (all
residues obtained so far come from F4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vneiger
vneiger merged commit aae439c into algebraic-solving:master Nov 27, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants