File: det.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 (495 lines) | stat: -rw-r--r-- 14,068 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright 1996 Michael E. Stillman.

#include "det.hpp"
#include "text-io.hpp"
#include "interrupted.hpp"
#include "comb.hpp"

DetComputation::DetComputation(const Matrix *M0,
                               int p0,
                               bool do_exterior0,
                               int strategy0)
    : R(M0->get_ring()),
      M(M0),
      done(false),
      p(p0),
      do_exterior(do_exterior0),
      strategy(strategy0),
      row_set(NULL),
      col_set(NULL),
      this_row(0),
      this_col(0),
      D(0),
      dynamic_cache(0)
{
  if (do_exterior)
    {
      F = M->rows()->exterior(p);
      FreeModule *G = M->cols()->exterior(p);
      int *deg = R->degree_monoid()->make_new(M->degree_shift());
      R->degree_monoid()->power(deg, p, deg);
      result = MatrixConstructor(F, G, deg);
      R->degree_monoid()->remove(deg);
    }
  else
    {
      F = R->make_FreeModule(1);
      result = MatrixConstructor(F, 0);
    }

  // Handle trivial cases
  if (p < 0)
    {
      // In either case, want a zero matrix
      done = true;
      return;
    }
  if (p == 0)
    {
      // We want a single element which is '1'
      if (do_exterior)
        result.set_entry(0, 0, R->one());
      else
        result.append(R->make_vec(0, R->one()));
      done = true;
      return;
    }
  if (p > M->n_rows() || p > M->n_cols())
    {
      // Zero matrix in either case
      done = true;
      return;
    }
  done = false;

  row_set = newarray_atomic(size_t, p);
  col_set = newarray_atomic(size_t, p);

  for (size_t i = 0; i < p; i++)
    {
      row_set[i] = i;
      col_set[i] = i;
    }

  D = newarray(ring_elem *, p);
  for (size_t i = 0; i < p; i++)
    {
      D[i] = newarray(ring_elem, p);
      for (size_t j = 0; j < p; j++) D[i][j] = ZERO_RINGELEM;
    }
}

DetComputation::~DetComputation()
{
  freemem(row_set);
  freemem(col_set);

  if (D)
    {
      for (size_t i = 0; i < p; i++) freemem(D[i]);
      freemem(D);
    }
}

int DetComputation::make_dynamic_cache()
{
  // Traverse through matrix entries, find nonzero entries
  int nonzero = -1;
  for (int i = 0; i < M->n_rows(); ++i)
    {
      bool flag = false;
      for (int j = 0; j < M->n_cols(); ++j)
        {
          if (!R->is_zero(M->elem(i, j)))
            {
              if (!flag)
                {
                  flag = true;
                  dynamic_cache[0][++nonzero] = {};
                  row_lookup[i] = nonzero;
                }
              dynamic_cache[0][nonzero][{{i}, {j}}] = M->elem(i, j);
            }
        }
    }
  int n_nonzero_rows = dynamic_cache[0].size();
  for (int minor_size = 1; minor_size < p; ++minor_size)
    {
      for (int top_row = p - (minor_size + 1);
           top_row <= n_nonzero_rows - minor_size;
           ++top_row)
        {
          dynamic_cache[minor_size].insert({top_row, {}});
          for (const auto &[pp, map] : dynamic_cache[minor_size - 1])
            {
              if (system_interrupted())
                return COMP_INTERRUPTED;  // Allow interruption
              if (pp <= top_row)
                {
                  continue;
                }  // top_row wouldn't be the top row, so skip
              for (auto x : dynamic_cache[0][top_row])
                {
                  for (const auto &[Didx, Dval] : map)
                    {
                      // Check if x and D live on distinct columns
                      const std::vector<int> &Dcols = Didx.second;
                      int xcol = x.first.second[0];
                      auto col_find =
                          std::find(Dcols.begin(), Dcols.end(), xcol);
                      if (col_find != Dcols.end())
                        {
                          continue;
                        }  // xcol found in Dcols, so skip
                      // if no skip, compute a term in the cofactor
                      ColRowIndices newKey = Didx;
                      newKey.first.insert(newKey.first.begin(),
                                          x.first.first[0]);
                      // Get iterator to future location of xcol
                      auto xcol_position = std::upper_bound(
                          newKey.second.begin(), newKey.second.end(), xcol);
                      newKey.second.insert(xcol_position, xcol);
                      bool negate =
                          ((xcol_position - newKey.second.begin()) % 2 != 0);
                      // Insert, add or negate cofactor term
                      auto search =
                          dynamic_cache[minor_size][top_row].find(newKey);
                      if (search == dynamic_cache[minor_size][top_row].end())
                        {  // not found
                          dynamic_cache[minor_size][top_row].insert(
                              {newKey, R->mult(x.second, Dval)});
                          if (negate)
                            {
                              R->negate_to(
                                  dynamic_cache[minor_size][top_row][newKey]);
                            }
                        }
                      else
                        {  // found
                          if (!negate)
                            {
                              R->add_to(
                                  dynamic_cache[minor_size][top_row][newKey],
                                  R->mult(x.second, Dval));
                            }
                          else
                            {
                              R->subtract_to(
                                  dynamic_cache[minor_size][top_row][newKey],
                                  R->mult(x.second, Dval));
                            }
                        }
                    }
                }
            }
        }
    }
  return COMP_DONE;
}

int DetComputation::step()
// Compute one more determinant of size p.
// increments I and/or J and updates 'dets', 'table'.
{
  if (done) return COMP_DONE;

  ring_elem r;

  if (strategy == DET_BAREISS)
    {
      get_minor(row_set, col_set, p, D);
      r = bareiss_det();
    }
  else if (strategy == DET_DYNAMIC)
    {
      if (dynamic_cache.empty())
        {  // Cache minors if needed
          dynamic_cache.resize(p);
          if (make_dynamic_cache() == COMP_INTERRUPTED) return COMP_INTERRUPTED;
        }
      std::vector<int> row_vec(p), col_vec(p);
      for (int i = 0; i < p; ++i)
        {
          row_vec[i] = static_cast<int>(row_set[i]);
          col_vec[i] = static_cast<int>(col_set[i]);
        }
      // Find row number
      const Subdeterminant &map = dynamic_cache[p - 1][row_lookup[row_vec[0]]];
      auto it = map.find({row_vec, col_vec});
      if (it != map.end()) { r = it->second; }
      else { r = ZERO_RINGELEM; }
    }
  else
    r = calc_det(row_set, col_set, p);

  if (!R->is_zero(r))
    {
      if (do_exterior)
        result.set_entry(this_row, this_col, r);
      else
        result.append(R->make_vec(0, r));
    }
  else
    R->remove(r);

  this_row++;
  if (!Subsets::increment(M->n_rows(), p, row_set))
    {
      // Now we increment column
      if (!Subsets::increment(M->n_cols(), p, col_set))
        {
          done = true;
          return COMP_DONE;
        }
      // Now set the row set back to initial value
      this_col++;
      this_row = 0;
      for (size_t i = 0; i < p; i++) row_set[i] = i;
    }
  return COMP_COMPUTING;
}

void DetComputation::clear()
{
  if (do_exterior) return;
  result = MatrixConstructor(F, 0);
}

void DetComputation::set_next_minor(const int *rows, const int *cols)
{
  if (do_exterior) return;
  if (rows != NULL && Subsets::isValid(M->n_rows(), p, rows))
    for (size_t i = 0; i < p; i++) row_set[i] = rows[i];
  else
    for (size_t i = 0; i < p; i++) row_set[i] = i;

  if (cols != NULL && Subsets::isValid(M->n_cols(), p, cols))
    for (size_t i = 0; i < p; i++) col_set[i] = cols[i];
  else
    for (size_t i = 0; i < p; i++) col_set[i] = i;
}

int DetComputation::calc(int nsteps)
{
  for (;;)
    {
      int r = step();
      if (M2_gbTrace >= 3) emit_wrapped(".");
      if (r == COMP_DONE) return COMP_DONE;
      if (--nsteps == 0) return COMP_DONE_STEPS;
      if (system_interrupted()) return COMP_INTERRUPTED;
    }
}

void DetComputation::get_minor(size_t *r, size_t *c, int p0, ring_elem **D0)
{
  for (size_t i = 0; i < p0; i++)
    for (size_t j = 0; j < p0; j++)
      D0[i][j] = M->elem(static_cast<int>(r[i]), static_cast<int>(c[j]));
}

bool DetComputation::get_pivot(ring_elem **D0,
                               size_t r,
                               ring_elem &pivot,
                               size_t &pivot_col)
// Get a non-zero column 0..r in the r th row.
{
  // MES: it would be worthwhile to find a good pivot.
  for (size_t c = 0; c <= r; c++)
    if (!R->is_zero(D0[r][c]))
      {
        pivot_col = c;
        pivot = D0[r][c];
        return true;
      }
  return false;
}

ring_elem DetComputation::detmult(ring_elem f1,
                                  ring_elem g1,
                                  ring_elem f2,
                                  ring_elem g2,
                                  ring_elem d)
{
  ring_elem a = R->mult(f1, g1);
  ring_elem b = R->mult(f2, g2);
  R->subtract_to(a, b);
  if (!R->is_zero(d))
    {
      ring_elem tmp = R->divide(a, d);  // exact division
      R->remove(a);
      a = tmp;
    }
  R->remove(g1);
  return a;
}

void DetComputation::gauss(ring_elem **D0,
                           size_t i,
                           size_t r,
                           size_t pivot_col,
                           ring_elem lastpivot)
{
  ring_elem f = D0[i][pivot_col];
  ring_elem pivot = D0[r][pivot_col];

  for (size_t c = 0; c < pivot_col; c++)
    D0[i][c] = detmult(pivot, D0[i][c], f, D0[r][c], lastpivot);

  for (size_t c = pivot_col + 1; c <= r; c++)
    D0[i][c - 1] = detmult(pivot, D0[i][c], f, D0[r][c], lastpivot);

  R->remove(f);
}

ring_elem DetComputation::bareiss_det()
{
  // Computes the determinant of the p by p matrix D. (dense form).
  int sign = 1;
  size_t pivot_col;

  ring_elem pivot = R->from_long(0);
  ring_elem lastpivot = R->from_long(0);

  for (size_t r = p - 1; r >= 1; --r)
    {
      R->remove(lastpivot);
      lastpivot = pivot;
      if (!get_pivot(D, r, pivot, pivot_col))  // sets pivot_col and pivot
        {
          // Remove the rest of D.
          for (size_t i = 0; i <= r; i++)
            for (size_t j = 0; j <= r; j++) R->remove(D[i][j]);
          R->remove(lastpivot);
          return R->from_long(0);
        }
      for (size_t i = 0; i < r; i++) gauss(D, i, r, pivot_col, lastpivot);

      if (((r + pivot_col) % 2) == 1)
        sign = -sign;  // MES: do I need to rethink this logic?

      for (size_t c = 0; c <= r; c++)
        if (c != pivot_col)
          R->remove(D[r][c]);
        else
          D[r][c] = ZERO_RINGELEM;
    }

  R->remove(pivot);
  R->remove(lastpivot);
  ring_elem r = D[0][0];
  D[0][0] = ZERO_RINGELEM;

  if (sign < 0) R->negate_to(r);

  return r;
}
ring_elem DetComputation::calc_det(size_t *r, size_t *c, int p0)
// Compute the determinant of the minor with rows r[0]..r[p0-1]
// and columns c[0]..c[p0-1].
{
  if (p0 == 1) return M->elem(static_cast<int>(r[0]), static_cast<int>(c[0]));
  ring_elem answer = R->from_long(0);

  int negate = 1;
  for (int i = p0 - 1; i >= 0; i--)
    {
      std::swap(c[i], c[p0 - 1]);
      negate = !negate;
      ring_elem g =
          M->elem(static_cast<int>(r[p0 - 1]), static_cast<int>(c[p0 - 1]));
      if (R->is_zero(g))
        {
          R->remove(g);
          continue;
        }
      ring_elem h = calc_det(r, c, p0 - 1);
      ring_elem gh = R->mult(g, h);
      R->remove(g);
      R->remove(h);
      if (negate)
        R->subtract_to(answer, gh);
      else
        R->add_to(answer, gh);
    }

  // pulling out the columns has disordered c. Fix it.

  size_t temp = c[p0 - 1];
  for (size_t i = p0 - 1; i > 0; i--) c[i] = c[i - 1];
  c[0] = temp;

  return answer;
}

Matrix /* or null */ *Matrix::exterior(int p, int strategy) const
{
  if (strategy == DET_BAREISS && get_ring()->get_precision() > 0)
    {
      ERROR(
          "determinant computations over RR or CC requires Strategy=>Cofactor");
      return 0;
    }
  DetComputation *d = new DetComputation(this, p, 1, strategy);
  d->calc(-1);
  Matrix *result = d->determinants();
  freemem(d);
  return result;
}

Matrix /* or null */ *Matrix::minors(int p, int strategy) const
{
  if (strategy == DET_BAREISS && get_ring()->get_precision() > 0)
    {
      ERROR(
          "determinant computations over RR or CC requires Strategy=>Cofactor");
      return 0;
    }
  DetComputation *d = new DetComputation(this, p, 0, strategy);
  d->calc(-1);
  Matrix *result = d->determinants();
  freemem(d);
  return result;
}

Matrix /* or null */ *Matrix::minors(
    int p,
    int strategy,
    int n_to_compute,             // -1 means all
    M2_arrayintOrNull first_row,  // possibly NULL
    M2_arrayintOrNull first_col   // possibly NULL
) const
{
  if (strategy == DET_BAREISS && get_ring()->get_precision() > 0)
    {
      ERROR(
          "determinant computations over RR or CC requires Strategy=>Cofactor");
      return 0;
    }
  if (first_row != 0 || first_col != 0)
    {
      // Make sure these are the correct size, and both are given
      if (first_row == 0 || first_row->len != p)
        {
          ERROR("row index set inappropriate");
          return 0;
        }
      if (first_col == 0 || first_col->len != p)
        {
          ERROR("column index set inappropriate");
          return 0;
        }
    }
  DetComputation *d = new DetComputation(this, p, 0, strategy);
  if (first_row != 0 && first_col != 0)
    d->set_next_minor(first_row->array, first_col->array);
  d->calc(n_to_compute);
  Matrix *result = d->determinants();
  freemem(d);
  return result;
}

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