File: comp-res.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 (250 lines) | stat: -rw-r--r-- 7,266 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
// Copyright 2004 Michael E. Stillman.

#include "text-io.hpp"
#include "comp-res.hpp"
#include "res-a1.hpp"
#include "res-a0.hpp"
#include "res-a2.hpp"
#include "finalize.hpp"
#include "schreyer-resolution/res-f4-computation.hpp"
#include "NCResolutions/nc-res-computation.hpp"

#include <iostream>

ResolutionComputation::ResolutionComputation() {}
ResolutionComputation::~ResolutionComputation() {}
ResolutionComputation *ResolutionComputation::choose_res(
    const Matrix *m,
    M2_bool resolve_cokernel,
    int max_level,
    M2_bool use_max_slanted_degree,
    int max_slanted_degree,
    int algorithm,
    int strategy)
{
  // The following modification is because some algorithms do not work if
  // max_level is 0.
  // github issue (crash, #368).
  if (max_level <= 0) max_level = 1;

  const Ring *R = m->get_ring();
  ResolutionComputation *C = nullptr;
  int origsyz;
  // First, we need to check that m is homogeneous, and that
  // the heft values of the variables are all positive.
  // All of these algorithms also assume that R is a polynomial ring.

  const M2FreeAlgebraOrQuotient *NCP = R->cast_to_M2FreeAlgebraOrQuotient();
  if (NCP != nullptr)
    {
      if (M2_gbTrace > 0) emit_line("NC resolution");
      C = createNCRes(m, max_level, strategy);
      return C;
    }
  const PolynomialRing *P = R->cast_to_PolynomialRing();
  if (P == nullptr)
    {
      ERROR("engine resolution strategies all require a polynomial base ring");
      return nullptr;
    }
  const Ring* K = P->getCoefficientRing();
  if (K->get_precision() != 0)
    {
      ERROR("free resolutions over polynomial rings with RR or CC coefficients not yet implemented");
      return nullptr;
    }
  if (!P->getMonoid()->primary_degrees_of_vars_positive())
    {
      ERROR(
          "engine resolution strategies all require a Heft vector which is "
          "positive for all variables");
      return nullptr;
    }
  if (algorithm < 4 and !m->is_homogeneous())
    {
      ERROR("engine resolution strategies require a homogeneous module");
      return nullptr;
    }

  switch (algorithm)
    {
      case 1:
        if (!resolve_cokernel)
          {
            ERROR(
                "resolution Strategy=>1 cannot resolve a cokernel with a given "
                "presentation: use Strategy=>2 or Strategy=>3 instead");
            return nullptr;
          }
        if (!R->is_commutative_ring())
          {
            ERROR(
                "use resolution Strategy=>2 or Strategy=>3 for non commutative "
                "polynomial rings");
            return nullptr;
          }
        if (M2_gbTrace > 0) emit_line("resolution Strategy=>1");
        C = new res_comp(m, max_level, strategy);
        break;
      case 0:
        if (!resolve_cokernel)
          {
            ERROR(
                "resolution Strategy=>0 cannot resolve a cokernel with a given "
                "presentation: use Strategy=>2 or Strategy=>3 instead");
            return nullptr;
          }
        if (!R->is_commutative_ring())
          {
            ERROR(
                "use resolution Strategy=>2 or Strategy=>3 for non commutative "
                "polynomial rings");
            return nullptr;
          }
        if (M2_gbTrace > 0) emit_line("resolution Strategy=>0");
        C = new res2_comp(
            m, max_level, use_max_slanted_degree, max_slanted_degree, strategy);
        break;
      case 2:
        origsyz = m->n_cols();
        if (M2_gbTrace > 0) emit_line("resolution Strategy=>2");
        C = new gbres_comp(m, max_level + 1, origsyz, strategy);
        break;
      case 3:
        origsyz = m->n_cols();
        if (M2_gbTrace > 0) emit_line("resolution Strategy=>3");
        C = new gbres_comp(
            m, max_level + 1, origsyz, strategy | STRATEGY_USE_HILB);
        break;
      case 4:
      case 5:
        if (!resolve_cokernel)
          {
            ERROR(
                "resolution Strategy=>4 cannot resolve a cokernel with a given "
                "presentation: use Strategy=>2 or Strategy=>3 instead");
            return nullptr;
          }
        if (!P->is_skew_commutative() and !R->is_commutative_ring())
          {
            ERROR(
                "use resolution Strategy=>2 or Strategy=>3 for non commutative "
                "polynomial rings");
            return nullptr;
          }
        if (M2_gbTrace > 0) emit_line("resolution Strategy=>4 (res-f4)");
        C = createF4Res(m, max_level, strategy);
        if (C == nullptr) return nullptr;
        break;
    }
  if (C == nullptr)
    {
      ERROR("unknown resolution algorithm");
      return nullptr;
    }
  intern_res(C);
  return C;
}

void ResolutionComputation::betti_init(int lo, int hi, int len, int *&bettis)
{
  int z = (hi - lo + 1) * (len + 1);
  bettis = newarray_atomic_clear(int, z);
}

M2_arrayint ResolutionComputation::betti_make(int lo,
                                              int hi,
                                              int len,
                                              int *bettis)
{
  int d, lev;
  int hi1 = hi + 1;
  int len1 = len + 1;

  // Reset 'hi1' to reflect the top degree that occurs
  for (d = hi; d >= lo; d--)
    {
      for (lev = 0; lev <= len; lev++)
        if (bettis[lev + (len + 1) * (d - lo)] > 0)
          {
            hi1 = d;
            break;
          }
      if (hi1 <= hi) break;
    }
  if (hi1 > hi) hi1 = hi;

  // Reset 'len1' to reflect the top level that occurs
  for (lev = len; lev >= 0; lev--)
    {
      for (d = lo; d <= hi1; d++)
        if (bettis[lev + (len + 1) * (d - lo)] > 0)
          {
            len1 = lev;
            break;
          }
      if (len1 <= len) break;
    }
  if (len1 > len) len1 = len;

  int totallen = (hi1 - lo + 1) * (len1 + 1);
  M2_arrayint result = M2_makearrayint(3 + totallen);

  result->array[0] = lo;
  result->array[1] = hi1;
  result->array[2] = len1;

  int next = 3;
  for (d = lo; d <= hi1; d++)
    for (lev = 0; lev <= len1; lev++)
      result->array[next++] = bettis[lev + (len + 1) * (d - lo)];

  return result;
}

void ResolutionComputation::betti_display(buffer &o, M2_arrayint ar)
{
  int *a = ar->array;
  int total_sum = 0;
  int lo = a[0];
  int hi = a[1];
  int len = a[2] + 1;
  o << "total  ";
  for (int lev = 0; lev < len; lev++)
    {
      int sum = 0;
      for (int d = lo; d <= hi; d++) sum += a[len * (d - lo) + lev + 3];
      total_sum += sum;
      o.put(sum, 6);
      o << ' ';
    }
  o << " [" << total_sum << "]" << newline;
  for (int d = lo; d <= hi; d++)
    {
      o.put(d, 5);
      o << ": ";
      for (int lev = 0; lev < len; lev++)
        {
          int c = a[len * (d - lo) + lev + 3];
          if (c != 0)
            o.put(c, 6);
          else
            o << "     -";
          o << " ";
        }
      o << newline;
    }
}

MutableMatrix /* or null */ *ResolutionComputation::get_matrix(int level,
                                                               int degree)
{
  // the default version gives an error that it isn't defined
  ERROR("this function not defined for this resolution type");
  return 0;
}

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