fglm_core.c 45.1 KB
Newer Older
Mohab Safey El Din's avatar
Mohab Safey El Din committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* This file is part of msolve.
 *
 * msolve is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * msolve is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with msolve.  If not, see <https://www.gnu.org/licenses/>
 *
 * Authors:
 * Jérémy Berthomieu
 * Christian Eder
 * Mohab Safey El Din */

Christian Eder's avatar
Christian Eder committed
21
22
23
24
#if HAVE_CONFIG_H
#include "../../config.h"
#endif

Mohab Safey El Din's avatar
Mohab Safey El Din committed
25
26
27
28
29
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<time.h>
30
31
32
/* for timing functions */
#include "../neogb/tools.h"

Christian Eder's avatar
Christian Eder committed
33
#ifdef _OPENMP
Mohab Safey El Din's avatar
Mohab Safey El Din committed
34
#include<omp.h>
35
#endif
Mohab Safey El Din's avatar
Mohab Safey El Din committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define DEBUGFGLM 0

#include "data_fglm.c"
#include "linalg-fglm.c"
#include "berlekamp_massey.c"


void display_nmod_poly(FILE *file, nmod_poly_t pol){
  fprintf(file, "[%ld,\n", pol->length-1);
  if(pol->length != 0){
    fprintf(file, "[");
    for(long i = 0; i < pol->length - 1 ; i++){
      fprintf(file, "%lu, ", pol->coeffs[i]);
    }
    fprintf(file, "%lu]", pol->coeffs[pol->length - 1]);
  }
  else{
    fprintf(file, "[0]");
  }
  fprintf(file, "]");
}


void display_fglm_param(FILE * file, param_t *param){
63
64
  fprintf(file, "%ld,\n", param->charac);
  fprintf(file, "%ld,\n", param->nvars);
Mohab Safey El Din's avatar
Mohab Safey El Din committed
65
66

  display_nmod_poly(file, param->elim);
67
  fprintf(file, ",\n");
Mohab Safey El Din's avatar
Mohab Safey El Din committed
68
  display_nmod_poly(file, param->denom);
69
70
  fprintf(file, ",\n");
  fprintf(file, "[");
Mohab Safey El Din's avatar
Mohab Safey El Din committed
71
72
73
74
  for(int c = param->nvars-2; c >= 0; c--){
    display_nmod_poly(file, param->coords[c]);
    fprintf(file, "\n");
  }
75
  fprintf(file, "]");
Mohab Safey El Din's avatar
Mohab Safey El Din committed
76
77
78
79
80
81
82
83
84
85
86
}

void display_fglm_param_maple(FILE * file, param_t *param){
  fprintf(file, "[%ld, \n", param->charac);
  fprintf(file, "%ld, \n", param->nvars);

  display_nmod_poly(file, param->elim);
  fprintf(file, ", \n");
  display_nmod_poly(file, param->denom);
  fprintf(file, ", \n");

87
  for(int c = param->nvars-2; c > 0; c--){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    display_nmod_poly(file, param->coords[c]);
    fprintf(file, ", \n");
  }
  display_nmod_poly(file, param->coords[0]);
  fprintf(file, "]:\n");
}

/* Points of B are u_0, .., u_l */
/* They become u_l, ..., u_0 */

static inline void mirror_points(nmod_berlekamp_massey_t B, szmat_t length){
  CF_t tmp;
  szmat_t mid = length / 2;
  for(szmat_t i = 0; i < mid; i++){
    tmp = B->points->coeffs[i];
    B->points->coeffs[i] = B->points->coeffs[length - i - 1];
    B->points->coeffs[length - i - 1] = tmp;
  }
}

Christian Eder's avatar
Christian Eder committed
108
#if 0
Mohab Safey El Din's avatar
Mohab Safey El Din committed
109
110
111
112
113
114
115
116
static inline void mirror_poly(nmod_poly_t out, nmod_poly_t in){
  szmat_t mid = in->length / 2;
  for(long i = 0; i <= mid; i++){
    out->coeffs[i] = in->coeffs[in->length - i - 1];
    out->coeffs[in->length - i - 1] = in->coeffs[i];
  }
  out->length=in->length;
}
Christian Eder's avatar
Christian Eder committed
117
#endif
Mohab Safey El Din's avatar
Mohab Safey El Din committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

static inline void mirror_poly_solve(nmod_poly_t out, nmod_poly_t in, szmat_t length){

  long i;
  long m = MIN(in->length, length);
  if(out->alloc < length){
    nmod_poly_fit_length(out, length);
  }
  out->length=length;
  for(i = 0; i < m; i++){
    out->coeffs[length - 1 - i] = in->coeffs[i];
  }
  for(;i<length; i++){
    out->coeffs[length-1-i] = 0;
  }
}



static inline void mirror_poly_inplace(nmod_poly_t in){
  CF_t tmp;
  szmat_t mid = in->length / 2;
  for(long i = 0; i < mid; i++){
    tmp = in->coeffs[i];
    in->coeffs[i] = in->coeffs[in->length - i - 1];
    in->coeffs[in->length - i - 1] = tmp;
  }
}



/*

  U is a Hankel matrix of size dim

  returns 0 when U is not invertible else it returns 1.
 */
static int invert_hankel_matrix(fglm_bms_data_t *data_bms, szmat_t deg){

  nmod_poly_one(data_bms->BMS->R0);
  nmod_poly_zero(data_bms->BMS->R1);
  nmod_poly_zero(data_bms->BMS->V0);
  nmod_poly_one(data_bms->BMS->V1);
  szmat_t dim = deg; //B->points->length / 2;
  data_bms->BMS->npoints = 0;

  data_bms->BMS->points->length = 2 * dim - 1;

  mirror_points(data_bms->BMS, data_bms->BMS->points->length);

  nmod_em_gcd(data_bms->BMS, 0);
169
  if(data_bms->BMS->R1->length-1 < dim-1 && dim > 1){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    fprintf(stderr, "Singular matrix\n");
    return 0;
  }

  if(data_bms->BMS->V1->coeffs[0]!=0){
    //Compute Z1 = LC(R1)^{-1} * V1

    mp_limb_t inv = n_invmod(data_bms->BMS->R1->coeffs[data_bms->BMS->R1->length-1],
                             (data_bms->BMS->R1->mod).n);
    nmod_poly_scalar_mul_nmod(data_bms->Z1, data_bms->BMS->V1, inv);

    mirror_points(data_bms->BMS, data_bms->BMS->points->length);
    nmod_poly_one(data_bms->BMS->R0); //x^(2dim -1)
    nmod_poly_zero(data_bms->BMS->R1);
    nmod_poly_zero(data_bms->BMS->V0);
    nmod_poly_one(data_bms->BMS->V1);
    data_bms->BMS->npoints = 0;

    //(R_i, R_{i+1}, V_i, V_{i+1}) = EMGCD(R_0, R_1)
    nmod_em_gcd(data_bms->BMS, 0);
    //Z2 = LC(R_{i+1})^{-1} x (V_{i+1})
    inv = n_invmod(data_bms->BMS->R1->coeffs[data_bms->BMS->R1->length-1],
                   (data_bms->BMS->R1->mod).n);
    nmod_poly_scalar_mul_nmod(data_bms->Z2, data_bms->BMS->V1, inv);

  }
  else{//V1(0) = 0
    fprintf(stderr, "Warning: this part of the code has not been tested intensively\n");
    nmod_poly_one(data_bms->BMS->R0);
    nmod_poly_zero(data_bms->BMS->R1);
    nmod_poly_zero(data_bms->BMS->V0);
    nmod_poly_one(data_bms->BMS->V1);
    data_bms->BMS->npoints = 0;

    data_bms->BMS->points->length = 2 * dim + 1;

    long queue_lo = (data_bms->BMS->npoints);
    long queue_len = (data_bms->BMS->points->length) - queue_lo;
    nmod_poly_zero(data_bms->BMS->rt);
    nmod_poly_set_coeff_ui(data_bms->BMS->rt, queue_len+1, 1);
    for (long i = 0; i < queue_len; i++)
      {
        nmod_poly_set_coeff_ui(data_bms->BMS->rt, queue_len - i,
                               data_bms->BMS->points->coeffs[queue_lo + i]);
      }
    nmod_poly_set_coeff_ui(data_bms->BMS->rt, 0, 1);

    nmod_em_gcd_preinstantiated(data_bms->BMS, 0);//B->rt is pre-instantied

    if(data_bms->BMS->R1->length-1 == dim){
      mp_limb_t inv = n_invmod(data_bms->BMS->R1->coeffs[data_bms->BMS->R1->length-1],
                               (data_bms->BMS->R1->mod).n);
      nmod_poly_scalar_mul_nmod(data_bms->Z1, data_bms->BMS->V1, inv);


      nmod_poly_set_coeff_ui(data_bms->BMS->rt, queue_len+1, 1);
      for (long i = 0; i < queue_len; i++)
        {
          nmod_poly_set_coeff_ui(data_bms->BMS->rt, i + 1,
                                 data_bms->BMS->points->coeffs[queue_lo + i]);
        }
      nmod_poly_set_coeff_ui(data_bms->BMS->rt, 0, 1);

      nmod_poly_one(data_bms->BMS->R0); //x^(2dim +1)
      nmod_poly_zero(data_bms->BMS->R1);
      nmod_poly_zero(data_bms->BMS->V0);
      nmod_poly_one(data_bms->BMS->V1);
      data_bms->BMS->npoints = 0;

      //(R_i, R_{i+1}, V_i, V_{i+1}) = EMGCD(R_0, R_1)
      nmod_em_gcd_preinstantiated(data_bms->BMS, 0);
      //Z2 = LC(R_{i+1})^{-1} x (V_{i+1})
      inv = n_invmod(data_bms->BMS->R1->coeffs[data_bms->BMS->R1->length-1],
                     (data_bms->BMS->R1->mod).n);
      nmod_poly_scalar_mul_nmod(data_bms->Z2, data_bms->BMS->V1, inv);
      fprintf(stderr, "Something should be checked\n");
      return 1;
    }
    else{
      nmod_poly_one(data_bms->BMS->R0);
      nmod_poly_zero(data_bms->BMS->R1);
      nmod_poly_zero(data_bms->BMS->V0);
      nmod_poly_one(data_bms->BMS->V1);
      data_bms->BMS->npoints = 0;

      data_bms->BMS->points->length = 2 * dim + 1;

      queue_lo = (data_bms->BMS->npoints);
      queue_len = (data_bms->BMS->points->length) - queue_lo;
      nmod_poly_zero(data_bms->BMS->rt);
      nmod_poly_set_coeff_ui(data_bms->BMS->rt, queue_len+1, data_bms->BMS->R1->mod.n - 1);
      for (long i = 0; i < queue_len; i++)
        {
          nmod_poly_set_coeff_ui(data_bms->BMS->rt, queue_len - i,
                                 data_bms->BMS->points->coeffs[queue_lo + i]);
        }
      nmod_poly_set_coeff_ui(data_bms->BMS->rt, 0, 1);

      nmod_em_gcd_preinstantiated(data_bms->BMS, 0);//B->rt is pre-instantied

      if(data_bms->BMS->R1->length-1 == dim){
        mp_limb_t inv = n_invmod(data_bms->BMS->R1->coeffs[data_bms->BMS->R1->length-1],
                                 (data_bms->BMS->R1->mod).n);
        nmod_poly_scalar_mul_nmod(data_bms->Z1, data_bms->BMS->V1, inv);


        nmod_poly_set_coeff_ui(data_bms->BMS->rt, queue_len+1, 1);
        for (long i = 0; i < queue_len; i++)
          {
            nmod_poly_set_coeff_ui(data_bms->BMS->rt, i + 1,
                                   data_bms->BMS->points->coeffs[queue_lo + i]);
          }
        nmod_poly_set_coeff_ui(data_bms->BMS->rt, 0, data_bms->BMS->R1->mod.n - 1);

        nmod_poly_one(data_bms->BMS->R0); //x^(2dim +1)
        nmod_poly_zero(data_bms->BMS->R1);
        nmod_poly_zero(data_bms->BMS->V0);
        nmod_poly_one(data_bms->BMS->V1);
        data_bms->BMS->npoints = 0;

        //(R_i, R_{i+1}, V_i, V_{i+1}) = EMGCD(R_0, R_1)
        nmod_em_gcd_preinstantiated(data_bms->BMS, 0);
        //Z2 = LC(R_{i+1})^{-1} x (V_{i+1})
        inv = n_invmod(data_bms->BMS->R1->coeffs[data_bms->BMS->R1->length-1],
                       (data_bms->BMS->R1->mod).n);
        nmod_poly_scalar_mul_nmod(data_bms->Z2, data_bms->BMS->V1, inv);
        fprintf(stderr, "Something should be checked\n");
        return 1;
      }
      else{
        fprintf(stderr, "There should be a bug here (invert_hankel)\n");
        return 0;
      }
    }
  }
  return 1;
}


/*

  Z1 and Z2 must be arrays of length d + 1
  Mirroring them will give an array of length d + 1

*/


Christian Eder's avatar
Christian Eder committed
317
#if 0
Mohab Safey El Din's avatar
Mohab Safey El Din committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
static inline void solveHankel(nmod_poly_t param,
                               nmod_poly_t Z1, nmod_poly_t Z2,
                               nmod_poly_t rZ1, nmod_poly_t rZ2,
                               nmod_poly_t A, nmod_poly_t B,
                               szmat_t dimquot,
                               szmat_t dim,
                               CF_t *res,
                               int ncoord,
                               nmod_poly_t V,
                               long disp){

  V->length = dim;

  for(long i = 0; i < dim; i++){
    V->coeffs[i] = res[ncoord-1+i*(dimquot)];
  }

  #if DEBUGFGLM > 0
  fprintf(stdout, "\n ncoord = %d\n", ncoord);
  fprintf(stdout, "V = "); nmod_poly_fprint_pretty(stdout, V, "x");fprintf(stdout, "\n\n");
  #endif
  mirror_poly_inplace(V);
  mirror_poly_solve(rZ1, Z1, dim + 1);

  mirror_poly_solve(rZ2, Z2, dim + 1);

  nmod_poly_mullow(A, rZ1, V, dim); // mod t^dim

  nmod_poly_mullow(B, Z2, V, dim); // mod t^dim

  mirror_poly_inplace(B);
  mirror_poly_inplace(A);
  nmod_poly_mullow(rZ1, Z1, B, dim);

  nmod_poly_mullow(rZ2, rZ2, A, dim);
  nmod_poly_neg(rZ2, rZ2);

  nmod_poly_add(param, rZ1, rZ2);


  mp_limb_t inv = n_invmod(Z1->coeffs[0], (Z1->mod).n);

  nmod_poly_scalar_mul_nmod(param, param, inv);

}
Christian Eder's avatar
Christian Eder committed
363
#endif
Mohab Safey El Din's avatar
Mohab Safey El Din committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

/*
 Z1 and Z2 must be arrays of length d + 1
 Mirroring them will give an array of length d + 1
 */

static inline void solve_hankel(fglm_bms_data_t *data_bms, 
                                szmat_t dimquot,
                                szmat_t dim,
                                szmat_t block_size,
                                CF_t *res,
                                int ncoord){
  data_bms->V->length = dim;

  for(long i = 0; i < dim; i++){
    data_bms->V->coeffs[i] = res[ncoord-1+i*(block_size)];
  }

  #if DEBUGFGLM > 0
  fprintf(stdout, "\n ncoord = %d\n", ncoord);
  fprintf(stdout, "V = ");
  nmod_poly_fprint_pretty(stdout, data_bms->V, "x");
  fprintf(stdout, "\n");
  #endif

  mirror_poly_inplace(data_bms->V);
  mirror_poly_solve(data_bms->rZ1, data_bms->Z1, dim + 1);
  mirror_poly_solve(data_bms->rZ2, data_bms->Z2, dim + 1);

  nmod_poly_mullow(data_bms->A, data_bms->rZ1, data_bms->V, dim); // mod t^dim
  nmod_poly_mullow(data_bms->B, data_bms->Z2, data_bms->V, dim); // mod t^dim

  mirror_poly_solve(data_bms->rZ1, data_bms->B, dim); 

  for(szmat_t i = 0; i < dim ; i++){
    data_bms->B->coeffs[i] = data_bms->rZ1->coeffs[i];
  }
  data_bms->B->length = data_bms->rZ1->length;
  mirror_poly_solve(data_bms->rZ1, data_bms->A, dim); 
  for(szmat_t i = 0; i < dim ; i++){
    data_bms->A->coeffs[i] = data_bms->rZ1->coeffs[i];
  }
  data_bms->A->length = data_bms->rZ1->length;

  nmod_poly_mullow(data_bms->rZ1, data_bms->Z1, data_bms->B, dim);
  nmod_poly_mullow(data_bms->rZ2, data_bms->rZ2, data_bms->A, dim);

  nmod_poly_neg(data_bms->rZ2, data_bms->rZ2);

  nmod_poly_add(data_bms->param, data_bms->rZ1, data_bms->rZ2);

  mp_limb_t inv = n_invmod(data_bms->Z1->coeffs[0], (data_bms->Z1->mod).n);

  nmod_poly_scalar_mul_nmod(data_bms->param, data_bms->param, inv);

}


/*

Matrix vector product.
Result is stored in res.
vres will contain the product of the dense part.

 */
static inline void sparse_mat_fglm_mult_vec(CF_t *res, sp_matfglm_t *mat,
                                            CF_t *vec,
                                            CF_t *vres,
                                            const mod_t prime,
                                            //cf_l_t *vec_cache, //obsolete
                                            const uint32_t RED_32,
                                            const uint64_t RED_64,
                                            const uint32_t preinv,
                                            const uint32_t pi1,
                                            const uint32_t pi2){

  szmat_t ncols = mat->ncols;
  szmat_t nrows = mat->nrows;
  szmat_t ntriv = ncols - nrows;
  for(szmat_t i = 0; i < ntriv; i++){
    res[mat->triv_idx[i]] = vec[mat->triv_pos[i]];
  }
  /* printf("ncols %u\n", ncols); */
447
#ifdef HAVE_AVX2
Mohab Safey El Din's avatar
Mohab Safey El Din committed
448
449
  /* matrix_vector_product(vres, mat->dense_mat, vec, ncols, nrows, prime, RED_32, RED_64); */
  _8mul_matrix_vector_product(vres, mat->dense_mat, vec, mat->dst,
Mohab Safey El Din's avatar
Mohab Safey El Din committed
450
                              ncols, nrows, prime, RED_32, RED_64, preinv);
Mohab Safey El Din's avatar
Mohab Safey El Din committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#else
  non_avx_matrix_vector_product(vres, mat->dense_mat, vec,
                              ncols, nrows, prime, RED_32, RED_64);
#endif
    for(szmat_t i = 0; i < nrows; i++){
      res[mat->dense_idx[i]] = vres[i];
    }
}




/**

   FGLM mod prime.
   All needed intermediate data are allocated inside the function.
   Useful only for first call.

 **/


Christian Eder's avatar
Christian Eder committed
472
#if DEBUGFGLM
Mohab Safey El Din's avatar
Mohab Safey El Din committed
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
static inline void print_vec(FILE *file, CF_t *vec, szmat_t len){
  fprintf(file, "[");
  for(szmat_t i = 0; i < len-1; ++i){
    fprintf(file, "%u, ", vec[i]);
  }
  fprintf(file, "%u]\n",vec[len-1]);
}

static inline void mynmod_berlekamp_massey_print(
                                                 const nmod_berlekamp_massey_t B)
{
  slong i;
  nmod_poly_fprint_pretty(stderr, B->V1, "x");
  fprintf(stderr, ", ");
  for (i = 0; i < B->points->length; i++)
    {
      fprintf(stderr, " %lu", B->points->coeffs[i]);
    }
}

static inline void mynmod_berlekamp_massey_print_poly(FILE *file,
                                               const nmod_berlekamp_massey_t B)
{
  slong i;
  fprintf(file, "%lu\n", B->V1->mod.n);
  for(i = 0; i < B->V1->length; ++i ){
    fprintf(file, "%lu ", (B->V1->coeffs)[i]);
  }
}
#endif

Christian Eder's avatar
Christian Eder committed
504
#if 0
Mohab Safey El Din's avatar
Mohab Safey El Din committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
static inline void generate_sequence(sp_matfglm_t *matrix, fglm_data_t * data,
                                     szmat_t block_size, long dimquot,
                                     mod_t prime){
  uint32_t RED_32 = ((uint64_t)2<<31) % prime;

#if DEBUGFGLM > 2
  fprintf(stderr, "RED_32 = %u\n", RED_32);
#endif

  uint32_t RED_64 = ((uint64_t)1<<63) % prime;
  RED_64 = (RED_64*2) % prime;

#if DEBUGFGLM > 2
  fprintf(stderr, "RED_64 = %u\n", RED_64);
#endif

Mohab Safey El Din's avatar
Mohab Safey El Din committed
521
  uint32_t preinv = 2^(62) / prime;
Mohab Safey El Din's avatar
Mohab Safey El Din committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
  uint32_t pi1 = ((uint64_t)pow(2, 32)) / RED_64;
  uint32_t pi2 = (uint64_t)pow(2, 32) / RED_32;

  for(szmat_t i = 1; i < matrix->ncols; i++){
    sparse_mat_fglm_mult_vec(data->vvec, matrix,
                             data->vecinit, data->vecmult,
                             prime, RED_32, RED_64, preinv, pi1, pi2);
#if DEBUGFGLM > 1
    print_vec(stderr, data->vvec, matrix->ncols);
#endif


    CF_t *tmp = data->vecinit;
    data->vecinit = data->vvec;
    data->vvec = tmp;
    data->res[i*block_size] = data->vecinit[0];

    for(szmat_t j = 1; j < block_size; j++){
      data->res[j+i*block_size] = data->vecinit[j+1];
    }

#if DEBUGFGLM > 1
    print_vec(stdout, data->res, 2*block_size * matrix->ncols);
#endif

#if DEBUGFGLM > 2
    fprintf(stderr, "res = ");
    print_vec(stdout, data->res+i*matrix->ncols, matrix->ncols);
#endif
  }
  for(szmat_t i = matrix->ncols; i < 2*matrix->ncols; i++){
    sparse_mat_fglm_mult_vec(data->vvec, matrix,
                             data->vecinit, data->vecmult,
                             prime, RED_32, RED_64, preinv, pi1, pi2);
#if DEBUGFGLM > 1
    print_vec(stderr, data->vvec, matrix->ncols);
#endif


    CF_t *tmp = data->vecinit;
    data->vecinit = data->vvec;
    data->vvec = tmp;
    data->res[i*block_size] = data->vecinit[0];

#if DEBUGFGLM > 1
    print_vec(stdout, data->res, 2*block_size * matrix->ncols);
#endif

#if DEBUGFGLM > 2
    fprintf(stderr, "res = ");
    print_vec(stdout, data->res+i*matrix->ncols, matrix->ncols);
#endif
  }

  /* now res contains our generating sequence */

  for(ulong i = 0; i < 2 * dimquot; i++){
    data->pts[i] = data->res[i*block_size];
  }

}
Christian Eder's avatar
Christian Eder committed
583
#endif
Mohab Safey El Din's avatar
Mohab Safey El Din committed
584

585
static void generate_sequence_verif(sp_matfglm_t *matrix, fglm_data_t * data,
Mohab Safey El Din's avatar
Mohab Safey El Din committed
586
587
588
589
590
591
592
593
594
595
596
					   szmat_t block_size, long dimquot,
					   uint64_t* squvars,
					   uint64_t* linvars,
					   long nvars,
					   mod_t prime){
  uint32_t RED_32 = ((uint64_t)2<<31) % prime;


  uint32_t RED_64 = ((uint64_t)1<<63) % prime;
  RED_64 = (RED_64*2) % prime;

Mohab Safey El Din's avatar
Mohab Safey El Din committed
597
  uint32_t preinv = 2^(62) / prime;
Mohab Safey El Din's avatar
Mohab Safey El Din committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  uint32_t pi1 = ((uint64_t)pow(2, 32)) / RED_64;
  uint32_t pi2 = (uint64_t)pow(2, 32) / RED_32;

  long dec= 0;
  for(szmat_t j = 1; j < block_size; j++){
    while (nvars-1-j-dec > 0 && linvars[nvars-1-j-dec] != 0) {
      dec++;
    }
    data->res[j+matrix->ncols*block_size]
      = data->vecinit[squvars[nvars-1-j-dec]];
  }
  for(szmat_t i = 1; i < matrix->ncols; i++){
    sparse_mat_fglm_mult_vec(data->vvec, matrix,
                             data->vecinit, data->vecmult,
                             prime, RED_32, RED_64, preinv, pi1, pi2);
#if DEBUGFGLM > 1
    print_vec(stderr, data->vvec, matrix->ncols);
#endif


    CF_t *tmp = data->vecinit;
    data->vecinit = data->vvec;
    data->vvec = tmp;
    data->res[i*block_size] = data->vecinit[0];

    dec= 0;
    for(szmat_t j = 1; j < block_size; j++){
      data->res[j+i*block_size] = data->vecinit[j+1];
      while (linvars[nvars-1-j-dec] != 0) {
	dec++;
      }
      data->res[j+(i+matrix->ncols)*block_size]
	= data->vecinit[squvars[nvars-1-j-dec]];
    }

#if DEBUGFGLM > 1
    print_vec(stdout, data->res, 2*block_size * matrix->ncols);
#endif

#if DEBUGFGLM > 2
    fprintf(stderr, "res = ");
    print_vec(stdout, data->res+i*matrix->ncols, matrix->ncols);
#endif
  }
  for(szmat_t i = matrix->ncols; i < 2*matrix->ncols; i++){
    sparse_mat_fglm_mult_vec(data->vvec, matrix,
                             data->vecinit, data->vecmult,
                             prime, RED_32, RED_64, preinv, pi1, pi2);
#if DEBUGFGLM > 1
    print_vec(stderr, data->vvec, matrix->ncols);
#endif


    CF_t *tmp = data->vecinit;
    data->vecinit = data->vvec;
    data->vvec = tmp;
    data->res[i*block_size] = data->vecinit[0];

#if DEBUGFGLM > 1
    print_vec(stdout, data->res, 2*block_size * matrix->ncols);
#endif

#if DEBUGFGLM > 2
    fprintf(stderr, "res = ");
    print_vec(stdout, data->res+i*matrix->ncols, matrix->ncols);
#endif
  }

  /* now res contains our generating sequence */

  for(ulong i = 0; i < 2 * dimquot; i++){
    data->pts[i] = data->res[i*block_size];
  }

}



static inline void compute_elim_poly(fglm_data_t *data,
                                     fglm_bms_data_t *data_bms,
                                     long dimquot){
    nmod_berlekamp_massey_add_points(data_bms->BMS, data->pts, 2*dimquot);

    nmod_berlekamp_massey_reduce(data_bms->BMS);
    nmod_poly_make_monic(data_bms->BMS->V1, data_bms->BMS->V1);
}

Mohab Safey El Din's avatar
Mohab Safey El Din committed
685

Mohab Safey El Din's avatar
Mohab Safey El Din committed
686
687
688
689
690
691
692
693
694
static inline long make_square_free_elim_poly(param_t *param,
                                              fglm_bms_data_t *data_bms,
                                              long dimquot,
                                              int info_level){
  long dim = data_bms->BMS->V1->length - 1;
  int boo = nmod_poly_is_squarefree(data_bms->BMS->V1);

  if(boo && dim == dimquot){
    nmod_poly_set(param->elim, data_bms->BMS->V1);
Mohab Safey El Din's avatar
Mohab Safey El Din committed
695
    nmod_poly_one(param->denom);
Mohab Safey El Din's avatar
Mohab Safey El Din committed
696
697
698
699
700
701
702
703
  }
  else{

    if(boo==0){
      if(info_level){
        fprintf(stderr, "Mininimal polynomial is not square-free\n");
      }
    }
Mohab Safey El Din's avatar
Mohab Safey El Din committed
704

Mohab Safey El Din's avatar
Mohab Safey El Din committed
705
706
    nmod_poly_factor_squarefree(data_bms->sqf, data_bms->BMS->V1);
    nmod_poly_one(param->elim);
Mohab Safey El Din's avatar
Mohab Safey El Din committed
707
    nmod_poly_one(param->denom);
Mohab Safey El Din's avatar
Mohab Safey El Din committed
708
709
710
711
    for(ulong i = 0; i < data_bms->sqf->num; i++){
      nmod_poly_mul(param->elim, param->elim, data_bms->sqf->p+i);
    }
    if(info_level){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
712
713
      fprintf(stderr, "Degree of the square-free part: %ld\n",
              param->elim->length-1);
Mohab Safey El Din's avatar
Mohab Safey El Din committed
714
715
716
717
718
      fprintf(stderr, "[%ld, %ld, %ld]\n", dimquot, dim, param->elim->length - 1);
    }
  }

  data_bms->sqf->num=0;
Mohab Safey El Din's avatar
Mohab Safey El Din committed
719
  return param->elim->length - 1;
Mohab Safey El Din's avatar
Mohab Safey El Din committed
720
721
722
723
724
725
726
727
728
729
730
731
732
}

static inline void compute_minpoly(param_t *param,
                                   fglm_data_t *data,
                                   fglm_bms_data_t *data_bms,
                                   long dimquot,
                                   uint64_t *linvars,
                                   uint32_t *lineqs,
                                   long nvars,
                                   long *dim,
                                   int info_level){
  compute_elim_poly(data, data_bms, dimquot);

Mohab Safey El Din's avatar
Mohab Safey El Din committed
733
  if(dimquot >= 1){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
734
735
736
737
738
739
740
741
742
743
744
    *dim = make_square_free_elim_poly(param, data_bms, dimquot, info_level);
  }
  else{
    nmod_poly_fit_length(param->elim, 2);
    param->elim->length = 2;
    param->elim->coeffs[1] = 1;
    param->elim->coeffs[0] = lineqs[nvars*(nvars+1)-1];
    *dim = 1;
  }

}
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
static void set_param_linear_vars(param_t *param,
                                  long nlins,
                                  uint64_t *linvars,
                                  uint32_t *lineqs,
                                  long nvars){

  int cnt = 1;
  const uint32_t fc = param->charac;

  int nr = 0;
  if(nlins==nvars){
    nr = nvars - 1;
    param->elim->length = 2;
    param->elim->coeffs[1] = 1;
    param->elim->coeffs[0] = lineqs[nlins*(nvars + 1) - 1];
  }
  else{
    nr = nlins;
  }
Mohab Safey El Din's avatar
Mohab Safey El Din committed
764

765
  for(int nc = nvars - 2; nc >= 0; nc--){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
766

Mohab Safey El Din's avatar
Mohab Safey El Din committed
767
768
    int ind = nc;

769
    if(linvars[nc] != 0){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
770
      /* int64_t lc = lineqs[nc +(nvars+1)*(nr-1 - (cnt-1))]; */
771
772
773
774
775
776
      nmod_poly_fit_length(param->coords[ind], param->elim->length);
      param->coords[ind]->coeffs[param->coords[ind]->length-1] = 0;
      param->coords[ind]->length = param->elim->length;
      for(long i = 0; i < param->coords[ind]->length; i++){
        param->coords[ind]->coeffs[i] = 0;
      }
Mohab Safey El Din's avatar
Mohab Safey El Din committed
777
778
779
780
781
782
783
784

      int64_t lc = lineqs[nr - 1 - (cnt-1) +(nvars+1)*(nr-1 - (cnt-1))];

      /* if(lc != 1){ */
      /*   fprintf(stderr, "LC is not 1. There should be a bug\n"); */
      /*   exit(1); */
      /* } */
      /* for(int k = nc + 1; k < nvars - 1 ; k++){ */
785
      for(int k = 1; k < nvars - 1 ; k++){
786
        int32_t c = lineqs[k+(nvars+1)*(nr-(cnt-1)-1)];
Mohab Safey El Din's avatar
Mohab Safey El Din committed
787

Mohab Safey El Din's avatar
Mohab Safey El Din committed
788
789
790
791
        if(c){
          /* one multiplies param->coords[k] by fc -c */
          /* and adds this to param->coords[ind] */
          uint32_t cc = (fc - c);
792

793
          for(int i = 0; i < param->coords[k]->length; i++){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
794
            int64_t tmp = cc * param->coords[k]->coeffs[i];
795
796
797
798
799
            tmp = tmp % fc;
            tmp += param->coords[ind]->coeffs[i];
            tmp = tmp % fc;
            param->coords[ind]->coeffs[i] = tmp;
          }
Mohab Safey El Din's avatar
Mohab Safey El Din committed
800

801
802
        }
      }
Mohab Safey El Din's avatar
Mohab Safey El Din committed
803

804
      int32_t c1 = lineqs[nvars - 1 +(nvars+1)*(nr-(cnt-1)-1)];
805
806
      param->coords[ind]->coeffs[1] = ((int64_t)(param->coords[ind]->coeffs[1] + c1)) % fc;

807
808

      int32_t c0 = lineqs[nvars +(nvars+1)*(nr-(cnt-1)-1)];
809
      param->coords[ind]->coeffs[0] = ((int64_t)(param->coords[ind]->coeffs[0] + c0)) % fc;
810

811
812
813
814
815
816
817
818
819
820
821
822
      for(long k = param->coords[ind]->length - 1; k >= 0; k--){
        if(param->coords[ind]->coeffs[k] == 0){
          param->coords[ind]->length--;
        }
        else{
          break;
        }
      }

      nmod_poly_rem(param->coords[ind], param->coords[ind],
                    param->elim);

Mohab Safey El Din's avatar
Mohab Safey El Din committed
823
824
825
826
827
828
829
830
831
      for(long k = param->coords[ind]->length - 1; k >= 0; k--){
        if(param->coords[ind]->coeffs[k] == 0){
          param->coords[ind]->length--;
        }
        else{
          break;
        }
      }

832
833
834
835
836
837
838
839
      cnt++;
    }
  }

#if DEBUGFGLM >0
  display_fglm_param(stderr, param);
#endif
}
Mohab Safey El Din's avatar
Mohab Safey El Din committed
840

841
842
843
844
845
846
847
848
849
850
static int compute_parametrizations(param_t *param,
                                    fglm_data_t *data,
                                    fglm_bms_data_t *data_bms,
                                    long dim,
                                    long dimquot,
                                    long block_size,
                                    long nlins,
                                    uint64_t *linvars,
                                    uint32_t *lineqs,
                                    long nvars){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
851

Mohab Safey El Din's avatar
Mohab Safey El Din committed
852
853
854
855
856
857
858
859
860
  nmod_poly_one(param->denom);

  if(invert_hankel_matrix(data_bms, dim)){

#if DEBUGFGLM > 0
    fprintf(stdout, "Z1 = "); nmod_poly_fprint_pretty(stdout, data_bms->Z1, "x");fprintf(stdout, "\n");
    fprintf(stdout, "Z2 = "); nmod_poly_fprint_pretty(stdout, data_bms->Z2, "x");fprintf(stdout, "\n");
#endif
    long dec = 0;
Mohab Safey El Din's avatar
Mohab Safey El Din committed
861

Mohab Safey El Din's avatar
Mohab Safey El Din committed
862
    for(long nc = 0; nc < nvars - 1 ; nc++){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
863

Mohab Safey El Din's avatar
Mohab Safey El Din committed
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
      if(linvars[nvars - 2- nc] == 0){
        solve_hankel(data_bms, dimquot, dim, block_size, data->res,
                     nc + 2 - dec);

        nmod_poly_neg(data_bms->param, data_bms->param);
        nmod_poly_reverse(param->coords[nvars-2-nc], data_bms->param, dim);
        nmod_poly_rem(param->coords[nvars-2-nc], param->coords[nvars-2-nc],
                      param->elim);

#if DEBUGFGLM > 0
        nmod_poly_fprint_pretty(stdout, param->coords[nvars-2-nc], "X");
        fprintf(stdout, "\n");
#endif
      }
      else{
Mohab Safey El Din's avatar
Mohab Safey El Din committed
879

Mohab Safey El Din's avatar
Mohab Safey El Din committed
880
        if(param->coords[nvars-2-nc]->alloc <  param->elim->alloc - 1){
881
          nmod_poly_fit_length(param->coords[nvars-2-nc],
Mohab Safey El Din's avatar
Mohab Safey El Din committed
882
                               param->elim->length-1 );
Mohab Safey El Din's avatar
Mohab Safey El Din committed
883
        }
Mohab Safey El Din's avatar
Mohab Safey El Din committed
884

885
        param->coords[nvars-2-nc]->length = param->elim->length-1 ;
Mohab Safey El Din's avatar
Mohab Safey El Din committed
886

887
        for(long i = 0; i < param->elim->length-1 ; i++){
Mohab Safey El Din's avatar
Mohab Safey El Din committed
888
889
          param->coords[nvars-2-nc]->coeffs[i] = 0;
        }
890
        dec++;
Mohab Safey El Din's avatar
Mohab Safey El Din committed
891
892
      }
    }
893
894
895
896
897
898
899
900

    set_param_linear_vars(param, nlins, linvars, lineqs, nvars);

#if DEBUGFGLM > 0
    fprintf(stderr, "PARAM = ");
    display_fglm_param_maple(stderr, param);
    fprintf(stderr, "\n");
#endif
Mohab Safey El Din's avatar
Mohab Safey El Din committed
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
    return 1;
  }
  else{
    return 0;
  }
}

static inline int invert_table_polynomial (param_t *param,
					   fglm_data_t *data,
					   fglm_bms_data_t *data_bms,
					   ulong dimquot,
					   szmat_t block_size,
					   mod_t prime,
					   int ncoord,
					   uint64_t lambda) {

  szmat_t length= data_bms->BMS->V1->length-1;
  nmod_poly_zero(data_bms->BMS->R0);
  nmod_poly_zero(data_bms->Z1);
  nmod_poly_zero(data_bms->Z2);
  nmod_poly_fit_length(data_bms->BMS->R0, length);
  nmod_poly_fit_length(data_bms->BMS->R0, length);

  for (long i = 0; i < length; i++){

    if (lambda == 0) {
      nmod_poly_set_coeff_ui (data_bms->BMS->R0,i,
			      data->res[(length-i-1)*block_size+ncoord]);

    }
    else {
      uint64_t coeff= (lambda*data->res[(length-i-1)*block_size]) % prime;
      coeff= (data->res[(length-i-1)*block_size+ncoord] + coeff) % prime;
      nmod_poly_set_coeff_ui (data_bms->BMS->R0,i,
			      coeff);
    }

  }

  nmod_poly_mul (data_bms->Z1,data_bms->BMS->R0,data_bms->BMS->V1);
  nmod_poly_shift_right (data_bms->Z1,data_bms->Z1,length);
  nmod_poly_xgcd (data_bms->BMS->R0,data_bms->BMS->R1,data_bms->Z2,param->elim,
		  data_bms->Z1);

  return (nmod_poly_degree (data_bms->BMS->R0) == 0);
}


static inline void divide_table_polynomials (param_t *param,
					     fglm_data_t *data,
					     fglm_bms_data_t *data_bms,
					     ulong dimquot,
					     szmat_t block_size,
					     mod_t prime,
					     int ncoord,
					     uint64_t lambda) {

  szmat_t length= data_bms->BMS->V1->length-1;
  nmod_poly_zero (data_bms->BMS->R0);
  nmod_poly_fit_length(data_bms->BMS->R0, length);

  for (long i = 0; i < length; i++){
    if (lambda == 0) {
      nmod_poly_set_coeff_ui (data_bms->BMS->R0,i,
			      data->res[(length-i-1)*block_size+ncoord]);

    }
    else {
      uint64_t coeff= (lambda*data->res[(length-i-1)*block_size+ncoord]) % prime;
      coeff= (data->res[(dimquot+length-i-1)*block_size+ncoord] + coeff) % prime;
      nmod_poly_set_coeff_ui (data_bms->BMS->R0,i,
			      coeff);
    }
  }

  nmod_poly_mul (data_bms->BMS->R1,data_bms->BMS->R0,data_bms->BMS->V1);
  nmod_poly_shift_right (data_bms->BMS->R1,data_bms->BMS->R1,length);
  nmod_poly_mul (data_bms->BMS->R1,data_bms->BMS->R1,data_bms->Z2);
  nmod_poly_rem (data_bms->BMS->R1,data_bms->BMS->R1,param->elim);

}


984
985

static
Mohab Safey El Din's avatar
Mohab Safey El Din committed
986
int compute_parametrizations_non_shape_position_case(param_t *param,
987
988
989
990
991
992
993
994
995
996
997
                                                     fglm_data_t *data,
                                                     fglm_bms_data_t *data_bms,
                                                     ulong dimquot,
                                                     szmat_t block_size,
                                                     long nlins,
                                                     uint64_t *linvars,
                                                     uint32_t *lineqs,
                                                     uint64_t *squvars,
                                                     long nvars,
                                                     mod_t prime,
                                                     int verif){
998

Mohab Safey El Din's avatar
Mohab Safey El Din committed
999
1000
  int nr_fail_param=-1;
  if (invert_table_polynomial (param, data, data_bms, dimquot, block_size,
For faster browsing, not all history is shown. View entire blame