File: res-a1-poly.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 (356 lines) | stat: -rw-r--r-- 8,983 bytes parent folder | download | duplicates (2)
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
// Copyright 1996 Michael E. Stillman

#include "res-a1-poly.hpp"
#include "text-io.hpp"
#include "polyring.hpp"
#include "freemod.hpp"
#include "geovec.hpp"

res_poly::res_poly(PolynomialRing *RR)
    : R(RR), M(R->getMonoid()), K(R->getCoefficientRing())
{
  element_size = sizeof(resterm *) + sizeof(res_pair *) + sizeof(ring_elem) +
                 sizeof(int) * M->monomial_size();
  resterm_stash = new stash("resterm", element_size);
}

res_poly::~res_poly() { delete resterm_stash; }
inline int res_poly::compare(const resterm *a, const resterm *b) const
{
  int cmp = M->compare(a->monom, b->monom);
  if (cmp != 0) return cmp;
  cmp = a->comp->compare_num - b->comp->compare_num;
  if (cmp > 0) return 1;
  if (cmp < 0) return -1;
  return 0;
}

resterm *res_poly::new_term() const
{
  resterm *result = reinterpret_cast<resterm *>(resterm_stash->new_elem());
  result->next = NULL;
  return result;
}
resterm *res_poly::new_term(ring_elem c, const int *m, res_pair *x) const
// Note: this does NOT copy 'c'
{
  resterm *result = new_term();
  result->coeff = c;
  M->copy(m, result->monom);
  result->comp = x;
  return result;
}

resterm *res_poly::mult_by_monomial(const resterm *f, const int *m) const
{
  resterm head;
  resterm *result = &head;
  for (const resterm *tm = f; tm != NULL; tm = tm->next)
    {
      result->next = new_term();
      result = result->next;
      result->comp = tm->comp;
      result->coeff = K->copy(tm->coeff);
      M->mult(tm->monom, m, result->monom);
    }
  result->next = NULL;
  return head.next;
}

resterm *res_poly::copy(const resterm *f) const
{
  resterm head;
  resterm *result = &head;
  for (const resterm *tm = f; tm != NULL; tm = tm->next)
    {
      result->next = new_term();
      result = result->next;
      result->comp = tm->comp;
      result->coeff = K->copy(tm->coeff);
      M->copy(tm->monom, result->monom);
    }
  result->next = NULL;
  return head.next;
}
void res_poly::remove(resterm *&f) const
{
  while (f != NULL)
    {
      resterm *tmp = f;
      f = f->next;
      K->remove(tmp->coeff);
      resterm_stash->delete_elem(tmp);
    }
}

resterm *res_poly::mult_by_term(const resterm *f,
                                ring_elem c,
                                const int *m) const
{
  resterm head;
  resterm *result = &head;
  for (const resterm *tm = f; tm != NULL; tm = tm->next)
    {
      result->next = new_term();
      result = result->next;
      result->comp = tm->comp;
      result->coeff = K->mult(c, tm->coeff);
      M->mult(tm->monom, m, result->monom);
    }
  result->next = NULL;
  return head.next;
}
resterm *res_poly::ring_mult_by_term(const ring_elem f,
                                     ring_elem c,
                                     const int *m,
                                     res_pair *x) const
{
  resterm head;
  resterm *result = &head;
  for (const Nterm *tm = f; tm != NULL; tm = tm->next)
    {
      result->next = new_term();
      result = result->next;
      result->comp = x;
      result->coeff = K->mult(c, tm->coeff);
      M->mult(tm->monom, m, result->monom);
    }
  result->next = NULL;
  return head.next;
}

void res_poly::make_monic(resterm *&f) const
{
  if (f == NULL) return;
  ring_elem c_inv = K->invert(f->coeff);

  for (resterm *tm = f; tm != NULL; tm = tm->next) K->mult_to(tm->coeff, c_inv);

  K->remove(c_inv);
}

void res_poly::add_to(resterm *&f, resterm *&g) const
{
  if (g == NULL) return;
  if (f == NULL)
    {
      f = g;
      g = NULL;
      return;
    }
  resterm head;
  resterm *result = &head;
  while (1) switch (compare(f, g))
      {
        case -1:
          result->next = g;
          result = result->next;
          g = g->next;
          if (g == NULL)
            {
              result->next = f;
              f = head.next;
              return;
            }
          break;
        case 1:
          result->next = f;
          result = result->next;
          f = f->next;
          if (f == NULL)
            {
              result->next = g;
              f = head.next;
              g = NULL;
              return;
            }
          break;
        case 0:
          resterm *tmf = f;
          resterm *tmg = g;
          f = f->next;
          g = g->next;
          K->add_to(tmf->coeff, tmg->coeff);
          if (K->is_zero(tmf->coeff))
            resterm_stash->delete_elem(tmf);
          else
            {
              result->next = tmf;
              result = result->next;
            }
          resterm_stash->delete_elem(tmg);
          if (g == NULL)
            {
              result->next = f;
              f = head.next;
              return;
            }
          if (f == NULL)
            {
              result->next = g;
              f = head.next;
              g = NULL;
              return;
            }
          break;
      }
}

void res_poly::subtract_multiple_to(resterm *&f,
                                    ring_elem c,
                                    const int *m,
                                    const resterm *g) const
// f := f - c * m * g
{
  ring_elem minus_c = K->negate(c);
  resterm *h = mult_by_term(g, minus_c, m);
  add_to(f, h);
  K->remove(minus_c);
}
void res_poly::ring_subtract_multiple_to(resterm *&f,
                                         ring_elem c,
                                         const int *m,
                                         res_pair *x,
                                         const ring_elem g) const
// f := f - c * m * g * x, where g is a ring element
{
  ring_elem minus_c = K->negate(c);
  resterm *h = ring_mult_by_term(g, minus_c, m, x);
  add_to(f, h);
  K->remove(minus_c);
}

int res_poly::n_terms(const resterm *f) const
{
  int result = 0;
  for (; f != NULL; f = f->next) result++;
  return result;
}

void res_poly::elem_text_out(const resterm *f) const
{
  buffer o;
  elem_text_out(o, f);
  emit(o.str());
}
void res_poly::elem_text_out(buffer &o, const resterm *f) const
{
  if (f == NULL)
    {
      o << "0";
      return;
    }

  bool p_one = false;
  bool p_parens = true;
  bool p_plus = false;
  for (const resterm *t = f; t != NULL; t = t->next)
    {
      int isone = M->is_one(t->monom);
      K->elem_text_out(o, t->coeff, p_one, p_parens, p_plus);
      if (!isone) M->elem_text_out(o, t->monom, p_one);
      o << "<" << t->comp->me << ">";
      p_plus = true;
    }
}

vec res_poly::to_vector(const resterm *f,
                        const FreeModule *F,
                        int /*to_minimal*/) const
{
  vecHeap H(F);
  int *mon = M->make_one();
  for (const resterm *tm = f; tm != NULL; tm = tm->next)
    {
      //    int x = (to_minimal ? tm->comp->minimal_me : tm->comp->me);
      int x =
          tm->comp
              ->minimal_me;  // MES: Currently used for non-minimal as well...
      M->divide(tm->monom, tm->comp->base_monom, mon);

      ring_elem a = R->make_flat_term(tm->coeff, mon);
      vec tmp = R->make_vec(x, a);
      H.add(tmp);
    }
  M->remove(mon);
  return H.value();
}

resterm *res_poly::from_vector(const VECTOR(res_pair *)& base, const vec v) const
{
  resterm head;
  resterm *result = &head;
  for (vecterm *w = v; w != NULL; w = w->next)
    for (Nterm *t = w->coeff; t != 0; t = t->next)
      {
        result->next = new_term();
        result = result->next;
        result->comp = base[w->comp];
        result->coeff = t->coeff;
        M->copy(t->monom, result->monom);
        M->mult(result->monom, result->comp->base_monom, result->monom);
      }
  result->next = NULL;
  // Now we must sort these
  sort(head.next);
  return head.next;
}

resterm *res_poly::strip(const resterm *f) const
{
  resterm head;
  resterm *result = &head;
  for (const resterm *tm = f; tm != NULL; tm = tm->next)
    if (tm->comp->syz_type != SYZ_NOT_MINIMAL)
      {
        result->next = new_term();
        result = result->next;
        result->comp = tm->comp;
        result->coeff = K->copy(tm->coeff);
        M->copy(tm->monom, result->monom);
      }
  result->next = NULL;
  return head.next;
}

const resterm *res_poly::component_occurs_in(const res_pair *x,
                                             const resterm *f) const
{
  for (const resterm *tm = f; tm != NULL; tm = tm->next)
    if (tm->comp == x) return tm;
  return NULL;
}

void res_poly::sort(resterm *&f) const
{
  // Divide f into two lists of equal length, sort each,
  // then add them together.  This allows the same monomial
  // to appear more than once in 'f'.

  if (f == NULL || f->next == NULL) return;
  resterm *f1 = NULL;
  resterm *f2 = NULL;
  while (f != NULL)
    {
      resterm *t = f;
      f = f->next;
      t->next = f1;
      f1 = t;

      if (f == NULL) break;
      t = f;
      f = f->next;
      t->next = f2;
      f2 = t;
    }

  sort(f1);
  sort(f2);
  add_to(f1, f2);
  f = f1;
}

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