Macaulay2 Engine
Loading...
Searching...
No Matches
res-moninfo-dense.hpp
Go to the documentation of this file.
1// Copyright 2016 Michael E. Stillman
2
3#ifndef _res_moninfo_dense_hpp_
4#define _res_moninfo_dense_hpp_
5
53
54#include <iostream> // for ostream
55#include <memory> // for unique_ptr
56#include <vector> // for vector
57
59#include "skew.hpp" // for SkewMultiplication
60
76{
77 int nvars;
78 int nslots;
79 std::unique_ptr<res_monomial_word[]>
80 hashfcn; // array 0..nvars-1 of hash values for each variable
82 std::vector<int> mVarDegrees; // array 0..nvars-1 of primary (heft) degrees
83 // for each variable.
84
85 int firstvar; // = 2, if no weight vector, otherwise 2 + nweights
86 int nweights; // number of weight vector values placed. These should all be
87 // positive values?
88
89 // flattened array 0..nweights of array 0..nvars-1 of longs
90 std::vector<int> weight_vectors;
92
93 // monomial format: [hashvalue, component, pack1, pack2, ..., packr]
94 // other possible:
95 // [hashvalue, len, component, v1, v2, ..., vr] each vi is potentially
96 // packed too.
97 // [hashvalue, component, wt1, ..., wtr, pack1, ..., packr]
98
99 mutable unsigned long ncalls_hash_value;
100 mutable unsigned long ncalls_compare;
101 mutable unsigned long ncalls_mult;
102 mutable unsigned long ncalls_get_component;
103 mutable unsigned long ncalls_from_expvector;
104 mutable unsigned long ncalls_to_expvector;
105 mutable unsigned long ncalls_to_varpower;
106 mutable unsigned long ncalls_from_varpower;
107 mutable unsigned long ncalls_is_equal;
108 mutable unsigned long ncalls_is_equal_true;
109 mutable unsigned long ncalls_divide;
110 mutable unsigned long ncalls_weight;
111 mutable unsigned long ncalls_unneccesary;
112 mutable unsigned long ncalls_quotient_as_vp;
113
114 public:
118
120 const std::vector<int>& var_degrees,
121 const std::vector<int>& weightvecs,
122 MonomialOrderingType moType);
123
125
126 int n_vars() const { return nvars; }
127 int max_monomial_size() const { return nslots; }
129 {
130 (void) m;
131 return nslots;
132 }
133
134 void show() const;
135
141 // This hash value is an ADDITIVE hash (trick due to A. Steel)
142
144 {
145 for (int i = 0; i < nslots; i++) *target++ = *src++;
146 }
147
149 {
150 m[1] = component;
151 }
152
158
160 component_index comp,
162 {
163 // Pack the vector e[0]..e[nvars-1],comp. Create the hash value at the same
164 // time.
166 result[0] = 0;
167 result[1] = comp;
168
169 for (int i = 0; i < nvars; i++)
170 {
171 result[firstvar + i] = e[i];
172 if (e[i] > 0) result[0] += hashfcn[i] * e[i];
173 }
174
175 const int* wt = weight_vectors.data();
176 for (int j = 0; j < nweights; j++, wt += nvars)
177 {
178 res_monomial_word val = 0;
179 for (int i = 0; i < nvars; i++)
180 {
181 auto a = e[i];
182 if (a > 0) val += a * wt[i];
183 }
184 result[2 + j] = val;
185 }
186 return true;
187 }
188
191 int* skewvars) const
192 {
193 return skew->skew_vars(m + 2 + nweights, skewvars);
194 }
195
199 {
200 return skew->mult_sign(m + 2 + nweights, n + 2 + nweights);
201 }
202
204 {
205 // Pack the vector (0,...,0,comp) with nvars zeroes.
206 // Hash value = 0. ??? Should the hash-function take component into account
207 // ???
208 result[0] = 0;
209 result[1] = comp;
210 for (int i = 2; i < nslots; i++) result[i] = 0;
211 return true;
212 }
213
216 component_index& result_comp) const
217 {
218 // Unpack the monomial m.
220 result_comp = m[1];
221 m += 2 + nweights;
222 for (int i = 0; i < nvars; i++) *result++ = *m++;
223 return true;
224 }
225
228 {
229 // 'result' must have enough space allocated
231 res_varpower_word* t = result + 1;
233 int len = 0;
234 for (int i = nvars - 1; i >= 0; i--)
235 {
236 if (*--m1 > 0)
237 {
238 *t++ = i;
239 *t++ = *m1;
240 len++;
241 }
242 }
243 *result = len;
244 }
245
247 component_index comp,
249 {
250 // 'result' must have enough space allocated
252 result[0] = 0;
253 result[1] = comp;
254 for (int i = 2; i < nslots; i++)
255 {
256 result[i] = 0;
257 }
258 for (index_res_varpower_monomial j = m; j.valid(); ++j)
259 {
260 res_varpower_word v = j.var();
261 res_varpower_word e = j.exponent();
262 result[firstvar + v] = e;
263 if (e == 1)
264 result[0] += hashfcn[v];
265 else
266 result[0] += e * hashfcn[v];
267 }
268
269 const int* wt = weight_vectors.data();
270 for (int j = 0; j < nweights; j++, wt += nvars)
271 {
272 res_monomial_word val = 0;
273 for (index_res_varpower_monomial i = m; i.valid(); ++i)
274 {
275 auto v = i.var();
276 auto e = i.exponent();
277 auto w = wt[v];
278 if (e == 1)
279 val += w;
280 else
281 val += w * e;
282 result[2 + j] = val;
283 }
284 }
285 }
286
288 {
290 for (int j = nslots; j > 0; --j)
291 if (*m++ != *n++) return false;
293 return true;
294 }
295
298 {
300 if (*m++ != *n++) return false;
301 m++;
302 n++;
303 for (int j = nslots - 2; j > 0; --j)
304 if (*m++ != *n++) return false;
306 return true;
307 }
308
310 int lo,
311 int hi) const
312 {
313 monom += 2 + nweights;
314
315 for (int i = lo; i <= hi; i++)
316 if (monom[i] > 0) return false;
317 return true;
318 }
319
321 {
322 // Determine if m represents a well-formed monomial.
323 m++;
324 for (int j = nslots - 1; j > 0; --j)
325 if (mask & (*m++)) return false;
326 return true;
327 }
328
332 {
333 ncalls_mult++;
334 for (int j = nslots; j > 0; --j) *result++ = *m++ + *n++;
335 }
336
340 {
342 // First, divide monomials
343 // Then, if the division is OK, set the component, hash value and rest of
344 // the monomial
345 if (m[1] != n[1]) // components are not equal
346 return false;
350 for (int i = nslots - 2; i > 0; i--)
351 {
352 res_varpower_word cmp = *--m1 - *--n1;
353 if (cmp < 0) return false;
354 *--result1 = cmp;
355 }
356 result[1] = 0; // the component of a division is in the ring (comp 0).
357 result[0] = m[0] - n[0]; // subtract hash codes
358 return true;
359 }
360
368
369 void show(res_const_packed_monomial m) const;
370
372
373#if 0
374 int compare_grevlex(res_const_packed_monomial m, res_const_packed_monomial n) const {
378 for (int i=nslots-2; i>0; i--) {
379 res_varpower_word cmp = *--m1 - *--n1;
380 if (cmp < 0) return -1;
381 if (cmp > 0) return 1;
382 }
383 res_monomial_word cmp = m[1]-n[1];
384 if (cmp < 0) return 1;
385 if (cmp > 0) return -1;
386 return 0;
387 }
388#endif
389
394 component_index tie1,
395 component_index tie2) const
396 {
398#if 0
399 printf("compare_schreyer: ");
400 printf(" m=");
401 showAlpha(m);
402 printf(" n=");
403 showAlpha(n);
404 printf(" m0=");
405 showAlpha(m0);
406 printf(" n0=");
407 showAlpha(n0);
408 printf(" tiebreakers: %d %d\n", tie1, tie2);
409#endif
414 for (int i = nslots - 2; i > 0; i--)
415 {
416 res_varpower_word cmp = *--m1 - *--n1 + *--m2 - *--n2;
417 if (cmp < 0) return -1;
418 if (cmp > 0) return 1;
419 }
420 res_monomial_word cmp = tie1 - tie2;
421 if (cmp < 0) return 1;
422 if (cmp > 0) return -1;
423 return 0;
424 }
425
426#if 0
427 int compare_lex(res_const_packed_monomial m, res_const_packed_monomial n) const {
431 for (int i=nslots-2; i>0; i--) {
432 res_varpower_word cmp = *m1++ - *n1++;
433 if (cmp > 0) return -1;
434 if (cmp < 0) return 1;
435 }
436 res_monomial_word cmp = m[1]-n[1];
437 if (cmp < 0) return 1;
438 if (cmp > 0) return -1;
439 return 0;
440 }
441
442 int compare_weightvector(res_const_packed_monomial m, res_const_packed_monomial n) const {
446 for (int i=0; i<nweights; i++) {
447 res_varpower_word cmp = *m1++ - *n1++;
448 if (cmp > 0) return -1;
449 if (cmp < 0) return 1;
450 }
451 m1 = m+nslots;
452 n1 = n+nslots;
453 for (int i=nvars-1; i>0; i--) {
454 res_varpower_word cmp = *--m1 - *--n1;
455 if (cmp < 0) return -1;
456 if (cmp > 0) return 1;
457 }
458 res_monomial_word cmp = m[1]-n[1];
459 if (cmp < 0) return 1;
460 if (cmp > 0) return -1;
461 return 0;
462 }
463
465#endif
466
468 {
469 result[0] = 1;
470 result[1] = v;
471 result[2] = 1;
472 }
473
475 {
476 return static_cast<int>(res_varpower_monomials::weight(a, mVarDegrees));
477 }
478
482 {
483 // sets result
485 a += firstvar;
486 b += firstvar;
487 int len = 0;
488 res_varpower_word* r = result + 1;
489 for (int i = nvars - 1; i >= 0; --i)
490 {
491 res_varpower_word c = a[i] - b[i];
492 if (c > 0)
493 {
494 *r++ = i;
495 *r++ = c;
496 len++;
497 }
498 }
499 result[0] = len;
500 }
501
502 void dump(std::ostream& o, res_const_packed_monomial mon);
503};
504#endif
505
506// Local Variables:
507// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
508// indent-tabs-mode: nil
509// End:
static Exponent weight(ConstExponents m, const std::vector< T > &wts)
unsigned long ncalls_quotient_as_vp
bool monomial_part_is_equal(res_const_packed_monomial m, res_const_packed_monomial n) const
int max_monomial_size() const
unsigned long ncalls_hash_value
component_index get_component(res_const_packed_monomial m) const
bool from_expvector(res_const_ntuple_monomial e, component_index comp, res_packed_monomial result) const
unsigned long ncalls_to_varpower
bool mult(res_const_packed_monomial m, res_const_packed_monomial n, res_packed_monomial result) const
unsigned long ncalls_divide
std::unique_ptr< res_monomial_word[]> hashfcn
void copy(res_const_packed_monomial src, res_packed_monomial target) const
int compare_schreyer(res_const_packed_monomial m, res_const_packed_monomial n, res_const_packed_monomial m0, res_const_packed_monomial n0, component_index tie1, component_index tie2) const
res_packed_monomial monomial
void showAlpha(res_const_packed_monomial m) const
res_monomial_word mask
void to_varpower_monomial(res_const_packed_monomial m, res_varpower_monomial result) const
unsigned long ncalls_compare
unsigned long ncalls_is_equal
bool to_expvector(res_const_packed_monomial m, res_ntuple_monomial result, component_index &result_comp) const
unsigned long ncalls_unneccesary
void set_component(component_index component, res_packed_monomial m) const
bool is_divisible_by_var_in_range(res_const_packed_monomial monom, int lo, int hi) const
bool one(component_index comp, res_packed_monomial result) const
unsigned long ncalls_to_expvector
unsigned long ncalls_from_expvector
bool is_equal(res_const_packed_monomial m, res_const_packed_monomial n) const
res_monomial_word hash_value(res_const_packed_monomial m) const
ResMonoidDense(int nvars, const std::vector< int > &var_degrees, const std::vector< int > &weightvecs, MonomialOrderingType moType)
int skew_vars(const SkewMultiplication *skew, res_const_packed_monomial m, int *skewvars) const
int monomial_size(res_const_packed_monomial m) const
unsigned long ncalls_get_component
unsigned long ncalls_weight
res_const_packed_monomial const_monomial
void from_varpower_monomial(res_const_varpower_monomial m, component_index comp, res_packed_monomial result) const
std::vector< int > weight_vectors
bool divide(res_const_packed_monomial m, res_const_packed_monomial n, res_packed_monomial result) const
void unchecked_mult(res_const_packed_monomial m, res_const_packed_monomial n, res_packed_monomial result) const
std::vector< int > mVarDegrees
int skew_mult_sign(const SkewMultiplication *skew, res_const_packed_monomial m, res_const_packed_monomial n) const
unsigned long ncalls_is_equal_true
unsigned long ncalls_mult
void quotient_as_vp(res_const_packed_monomial a, res_const_packed_monomial b, res_varpower_monomial result) const
bool check_monomial(res_const_packed_monomial m) const
unsigned long ncalls_from_varpower
void variable_as_vp(int v, res_varpower_monomial result) const
int degree_of_vp(res_const_varpower_monomial a) const
void dump(std::ostream &o, res_const_packed_monomial mon)
int mult_sign(const int *exp1, const int *exp2) const
Definition skew.cpp:91
int skew_vars(const int *exp, int *result) const
Definition skew.cpp:61
Sign-rule helper used by every ring that has a skew-commutative subset of variables (exterior factor,...
Definition skew.hpp:54
static int compare(const vecterm *t, const vecterm *s)
Definition geovec.hpp:112
VALGRIND_MAKE_MEM_DEFINED & result(result)
myword component_index
ExponentListIterator< myword, false > index_res_varpower_monomial
const res_varpower_word * res_const_varpower_monomial
res_varpower_word * res_varpower_monomial
res_ntuple_word * res_ntuple_monomial
const res_monomial_word * res_const_packed_monomial
myword res_monomial_word
MonomialOrderingType
res_varpower_monomials::Exponent res_varpower_word
res_monomial_word * res_packed_monomial
const res_ntuple_word * res_const_ntuple_monomial
Typed-monomial vocabulary shared by ResMonoid, ResPolyRing, SchreyerFrame, and F4Res.
SkewMultiplication — configuration object naming the skew-commuting variables of a ring.