File: gauss.cpp

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (432 lines) | stat: -rw-r--r-- 10,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
169
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
317
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
363
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
// Copyright 1997-2005  Michael E. Stillman

#include "style.hpp"
#include "gauss.hpp"
#include "text-io.hpp"
#include "matrix-con.hpp"
#include "newdelete.hpp"
#include "interrupted.hpp"

#include <iostream>
extern RingZZ *globalZZ;

int GaussElimComputation::complete_thru_degree() const
// The computation is complete up through this degree.
{
  if (status() == COMP_DONE) return 0;
  return -1;
}

gm_elem *GaussElimComputation::new_gen(int i)
{
  gm_elem *result = new gm_elem;
  result->f = R->copy_vec(gens->elem(i));

  if (i < n_comps_per_syz)
    result->fsyz = R->make_vec(i, R->one());
  else
    result->fsyz = NULL;
  result->next = NULL;
  return result;
}
void GaussElimComputation::insert(gm_elem *p)
{
  if (p->f == NULL)
    {
      if (p->fsyz != NULL && collect_syz)
        {
          syz_list.push_back(p->fsyz);
          n_syz++;
        }
      freemem(p);
    }
  else
    {
      ring_elem leadinv = R->invert(p->f->coeff);
      R->mult_vec_to(p->f, leadinv, false);
      R->mult_vec_to(p->fsyz, leadinv, false);
      p->nterms = R->n_nonzero_terms(p->f);
      int i = p->f->comp;
      if (gb_list[i] == NULL)
        {
          gb_list[i] = p;
          n_gb++;
        }
      else
        {
          if (p->nterms < gb_list[i]->nterms)
            {
              gm_elem *q = p;
              p = gb_list[i];
              gb_list[i] = q;
            }
          p->next = reduce_list[i];
          reduce_list[i] = p;
        }
    }
}

GaussElimComputation::GaussElimComputation(const Matrix *m,
                                           int collsyz,
                                           int nsyz)
    : row(m->n_rows() - 1),
      R(m->get_ring()),
      gens(m),
      n_gb(0),
      n_pairs(0),
      n_syz(0),
      collect_syz(collsyz)
{
  int i;

  typedef struct gm_elem *gm_elem_ptr;
  reduce_list = newarray(gm_elem_ptr, m->n_rows());
  gb_list = newarray(gm_elem_ptr, m->n_rows());

  for (i = 0; i < m->n_rows(); i++)
    {
      reduce_list[i] = NULL;
      gb_list[i] = NULL;
    }

  if (nsyz < 0 || nsyz > m->n_cols()) nsyz = m->n_cols();
  n_comps_per_syz = nsyz;
  Fsyz = m->cols()->sub_space(nsyz);

  for (i = 0; i < m->n_cols(); i++)
    {
      gm_elem *p = new_gen(i);
      insert(p);
    }
}

void GaussElimComputation::remove_gm_elem(gm_elem *&p)
{
  if (p != NULL)
    {
      R->remove_vec(p->f);
      R->remove_vec(p->fsyz);
      freemem(p);
      p = NULL;
    }
}

GaussElimComputation::~GaussElimComputation()
{
  for (int i = 0; i < gens->n_rows(); i++)
    {
      // remove the gm_elem list
      gm_elem *p = reduce_list[i];
      while (p != NULL)
        {
          gm_elem *tmp = p;
          p = p->next;
          remove_gm_elem(tmp);
        }
      remove_gm_elem(gb_list[i]);
    }
}

void GaussElimComputation::reduce(gm_elem *&p, gm_elem *q)
{
  // MES: rewrite.
  // Reduce q by p.  This is a single step reduction.
  // The element is then inserted further down.

  ring_elem c1 = p->f->coeff;
  ring_elem c2 = q->f->coeff;
  ring_elem d2 = R->negate(c2);
  vec v1 = R->mult_vec(c1, q->f);
  vec v2 = R->mult_vec(d2, p->f);
  vec s1 = R->mult_vec(c1, q->fsyz);
  vec s2 = R->mult_vec(d2, p->fsyz);
  R->remove_vec(q->f);
  R->remove_vec(q->fsyz);
  R->remove(d2);
  R->add_vec_to(v1, v2);
  R->add_vec_to(s1, s2);
  q->f = v1;
  q->fsyz = s1;
}

void GaussElimComputation::reduce(vec &f)
{
  vecterm head;
  vecterm *result = &head;

  while (f != NULL)
    {
      int r = f->comp;
      if (gb_list[r] != NULL)
        {
          // Reduce w.r.t. this term
          ring_elem c = f->coeff;
          c = R->negate(c);
          vec g = R->mult_vec(c, gb_list[r]->f);
          R->add_vec_to(f, g);
        }
      else
        {
          result->next = f;
          f = f->next;
          result = result->next;
        }
    }

  result->next = NULL;
  f = head.next;
}
void GaussElimComputation::reduce(vec &f, vec &fsyz, bool tail_only)
{
  if (f == 0) return;
  vecterm head;
  vecterm *result = &head;

  if (tail_only)
    {
      // Don't reduce the head term.
      result->next = f;
      f = f->next;
      result = result->next;
    }
  while (f != NULL)
    {
      int r = f->comp;
      if (gb_list[r] != NULL)
        {
          // Reduce w.r.t. this term
          ring_elem c = f->coeff;
          c = R->negate(c);
          vec gsyz = R->mult_vec(c, gb_list[r]->fsyz);
          vec g = R->mult_vec(c, gb_list[r]->f);
          R->add_vec_to(f, g);
          R->add_vec_to(fsyz, gsyz);
        }
      else
        {
          result->next = f;
          f = f->next;
          result = result->next;
        }
    }

  result->next = NULL;
  f = head.next;
}
void GaussElimComputation::start_computation()
{
  if (status() == COMP_DONE) return;
  for (; row >= 0; row--)
    {
      if (gb_list[row] == NULL) continue;
      while (reduce_list[row] != NULL)
        {
          gm_elem *p = reduce_list[row];
          reduce_list[row] = p->next;
          p->next = NULL;
          reduce(gb_list[row], p);  // replaces p
          if (M2_gbTrace >= 3)
            {
              if (p->f == NULL)
                {
                  if (p->fsyz == NULL)
                    emit_wrapped("o");
                  else
                    emit_wrapped("z");
                }
              else
                emit_wrapped("r");
            }
          else
            {
            }
          insert(p);
          n_pairs++;
          if (system_interrupted())
            {
              set_status(COMP_INTERRUPTED);
              return;
            }
          if (n_pairs == stop_.pair_limit)
            {
              set_status(COMP_DONE_PAIR_LIMIT);
              return;
            }
          if (n_syz == stop_.syzygy_limit)
            {
              set_status(COMP_DONE_SYZYGY_LIMIT);
              return;
            }
        }
    }
  // Now auto reduce these
  for (int r = 1; r < gens->n_rows(); r++)
    {
      if (gb_list[r] == 0) continue;
      reduce(gb_list[r]->f, gb_list[r]->fsyz, true);
    }
  if (M2_gbTrace >= 10)
    {
      buffer o;
      text_out(o);
      emit(o.str());
    }
  set_status(COMP_DONE);
}

const Matrix *GaussElimComputation::get_mingens() { return get_gb(); }
const Matrix *GaussElimComputation::get_initial(int nparts)
{
  MatrixConstructor mat(gens->rows(), 0);
  for (int i = 0; i < gens->n_rows(); i++)
    if (gb_list[i] != NULL)
      {
        vec v = gb_list[i]->f;
        mat.append(R->make_vec(v->comp, v->coeff));
      }
  return mat.to_matrix();
}

const Matrix *GaussElimComputation::get_gb()
{
  MatrixConstructor mat(gens->rows(), 0);
  for (int i = 0; i < gens->n_rows(); i++)
    if (gb_list[i] != NULL) mat.append(R->copy_vec(gb_list[i]->f));
  return mat.to_matrix();
}

const Matrix *GaussElimComputation::get_change()
{
  MatrixConstructor mat(Fsyz, 0);
  for (int i = 0; i < gens->n_rows(); i++)
    if (gb_list[i] != NULL) mat.append(R->copy_vec(gb_list[i]->fsyz));
  return mat.to_matrix();
}

const Matrix *GaussElimComputation::get_syzygies()
{
  MatrixConstructor mat(Fsyz, 0);
  for (int i = 0; i < syz_list.size(); i++)
    mat.append(R->copy_vec(syz_list[i]));
  return mat.to_matrix();
}

void GaussElimComputation::text_out(buffer &o) const
{
  o << newline;
  for (int i = 0; i < gens->n_rows(); i++)
    {
      if (gb_list[i] != NULL)
        {
          o << "--- component " << i << " -----" << newline;
          o << "gb elem = ";
          R->vec_text_out(o, gb_list[i]->f);
          o << newline;
        }
      else if (reduce_list[i] != NULL)
        o << "--- component " << i << " -----" << newline;
      for (gm_elem *p = reduce_list[i]; p != NULL; p = p->next)
        {
          o << p->nterms;
          o << " ## ";
          R->vec_text_out(o, p->f);
          o << " ## ";
          R->vec_text_out(o, p->fsyz);
          o << newline;
        }
    }
  o << newline;
  for (int i = 0; i < syz_list.size(); i++) R->vec_text_out(o, syz_list[i]);
  o << newline;
}

const Matrix /* or null */ *GaussElimComputation::matrix_remainder(
    const Matrix *m)
{
  if (m->get_ring() != R)
    {
      ERROR("encountered different rings");
      return 0;
    }
  if (m->n_rows() != gens->rows()->rank())
    {
      ERROR("expected matrices to have same number of rows");
      return 0;
    }
  MatrixConstructor mat_remainder(m->rows(), m->cols(), m->degree_shift());
  for (int i = 0; i < m->n_cols(); i++)
    {
      vec f = R->copy_vec(m->elem(i));

      reduce(f);
      mat_remainder.set_column(i, f);
    }
  return mat_remainder.to_matrix();
}

M2_bool GaussElimComputation::matrix_lift(
    const Matrix *m,
    const Matrix /* or null */ **result_remainder,
    const Matrix /* or null */ **result_quotient)
{
  if (m->get_ring() != R)
    {
      ERROR("encountered different rings");
      *result_remainder = 0;
      *result_quotient = 0;
      return false;
    }
  if (m->n_rows() != gens->rows()->rank())
    {
      ERROR("expected matrices to have same number of rows");
      *result_remainder = 0;
      *result_quotient = 0;
      return false;
    }
  MatrixConstructor mat_remainder(m->rows(), m->cols(), m->degree_shift());
  MatrixConstructor mat_quotient(Fsyz, m->cols(), 0);
  bool all_zeroes = true;
  for (int i = 0; i < m->n_cols(); i++)
    {
      vec f = R->copy_vec(m->elem(i));
      vec fsyz = NULL;

      reduce(f, fsyz);
      R->negate_vec_to(fsyz);
      if (f != 0) all_zeroes = false;
      mat_remainder.set_column(i, f);
      mat_quotient.set_column(i, fsyz);
    }
  *result_remainder = mat_remainder.to_matrix();
  *result_quotient = mat_quotient.to_matrix();
  return all_zeroes;
}

int GaussElimComputation::contains(const Matrix *m)
// Return -1 if every column of 'm' reduces to zero.
// Otherwise return the index of the first column that
// does not reduce to zero.
{
  if (m->get_ring() != R)
    {
      ERROR("encountered different ring");
      return -1;
    }
  // Reduce each column of m one by one.
  for (int i = 0; i < m->n_cols(); i++)
    {
      vec f = R->copy_vec(m->elem(i));
      reduce(f);
      if (f != NULL)
        {
          R->remove_vec(f);
          return i;
        }
    }
  return -1;
}

// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
// indent-tabs-mode: nil
// End: