Macaulay2 Engine
Loading...
Searching...
No Matches
ExponentList.hpp
Go to the documentation of this file.
1/* Copyright 2006 by Michael E. Stillman */
2#pragma once
3
36
37#include <cstdio> // for FILE
38#include <vector> // for vector
39
40#include "engine-includes.hpp" // for M2_arrayint, M2_ArrayString
41
42#include "ExponentVector.hpp"
43#include "buffer.hpp"
44
45class buffer;
46
55// TODO: reimplement as a std::vector?
56template <class Exponent, bool legacy_length>
57class ExponentList;
58
59template <class Exponent, bool legacy_length>
61
62template <class E, bool L>
64{
66 friend class ExponentListIterator<E, L>;
67
68 public:
69 typedef E Exponent;
71 typedef const Exponent *ConstExponents;
72 typedef typename std::make_unsigned<Exponent>::type HashExponent;
74
76 {
77 HashExponent hashval = *vp;
78 for (Iterator i = vp; i.valid(); ++i)
79 hashval = 4624296 * hashval + 2341 * i.var() + i.exponent();
80 return hashval;
81 }
82
83 static void elem_text_out(buffer &o, ConstExponents m, bool p_one = true)
84 {
85 Iterator i = m;
86 if (!i.valid() and p_one) o << "1";
87 for (; i.valid(); ++i)
88 {
89 Exponent v = i.var();
90 Exponent e = i.exponent();
91 if (v < 26)
92 o << char('a' + v);
93 else if (v < 52)
94 o << char('A' + v - 26);
95 else
96 o << "x[" << v << "]";
97 if (e > 1)
98 o << e;
99 else if (e < 0)
100 o << "^(" << e << ")";
101 }
102 }
103
105 {
106 if constexpr (L)
107 return *m;
108 else
109 return 2 * *m + 1;
110 }
112 {
113 if constexpr (L)
114 return (*m - 1) / 2;
115 else
116 return *m;
117 }
118
119 static void one(Vector& result) { result = {1}; }
120 static bool is_one(ConstExponents a) { return length(a) == 1; }
122 {
123 return std::equal(a, a + length(a), b);
124 }
125
127 {
128 assert(length(a) > 1);
129 return a[1];
130 }
131 static void var(Exponent v, Exponent e, Vector& result)
132 {
133 // TODO: decide on 1 or 3
134 if (e == 0)
135 result = {1};
136 else
137 result = {3, v, e};
138 }
140 {
141 std::copy(vp, vp + length(vp), std::back_inserter(result));
142 }
143
144 // return EQ, LT, or GT for m1 == m2, m1 < m2, or m1 > m2.
146 {
147 Exponent alen = length(a++) - 1;
148 Exponent blen = length(b++) - 1;
149 // TODO: try using std::lexicographical_compare?
150 for (size_t i = 0; i < std::min(alen, blen); i++)
151 {
152 Exponent c = *a++ - *b++;
153 if (c == 0) continue;
154 return (c > 0 ? GT : LT);
155 }
156 if (alen == blen) return EQ;
157 return (alen > blen ? GT : LT);
158 }
159
162
163 // TODO: make this return by reference?
166
167 template <typename T>
168 static Exponent weight(ConstExponents m, const std::vector<T> &wts)
169 {
170 Exponent sum = 0;
171 auto num_wts = wts.size();
172 for (Iterator i = m; i.valid(); ++i)
173 sum += i.exponent() * (i.var() < num_wts ? wts[i.var()] : 1);
174 return sum;
175 }
177 {
178 Exponent deg = 0;
179 for (Iterator i = m; i.valid(); ++i) deg += i.exponent();
180 return deg;
181 }
182
184 // compute the quotient a:b
187
188 static void monsyz(ConstExponents a,
190 Vector& sa,
191 Vector& sb);
192
193 // whether a is divisible by b
197
198 // divide a by b^infinity
201 // if a=v^e, then set v and e appropriately, otherwise return false.
203 {
204 if (npairs(a) != 1) return false;
205 v = a[1];
206 e = a[2];
207 return true;
208 }
209
210 /* These should satisfy: lcm(p,q) == pq
211 returns 0 if the pair (p,q) should be REMOVED
212 Returns 1 iff either (a) m does not divide pq, or
213 (b) m does divide pq, and lcm(m,p) == lcm(m,q) */
218 {
219#ifdef DEVELOPMENT
220# warning "buchberger-moeller still to write"
221#endif
222 return 0;
223 }
224};
225
226// Legacy specialization
230
231// TODO: upgrade so you can use one of:
232// for (auto it = values.begin(); it != values.end(); ++it )
233// for (auto& value : values)
234template <class E, bool L>
236{
237 typedef E Exponent;
239 typedef const Exponent *ConstExponents;
240
241 const Exponent *loc;
242 const Exponent *hi;
243
244 public:
247 : loc(m + 1), hi(m + ExponentList<E, L>::length(m)) {}
248
250
251 bool valid() { return loc < hi; }
252 ExponentListIterator &operator++() { loc += 2; return *this; }
253 // ExponentListIterator &operator--() { loc -= 2; return *this; }
254
255 Exponent var() { return *loc; }
256 Exponent exponent() { return loc[1]; }
257};
258
259// Local Variables:
260// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
261// indent-tabs-mode: nil
262// End:
varpower::ConstExponents const_varpower
ExponentList< int, true > varpower
ExponentListIterator< int, true > index_varpower
Dense exponent-vector template [e_0, ..., e_{nvars-1}] for monomial operations.
Append-only GC-backed byte buffer used throughout the engine for text output.
static Exponent buchberger_moeller_keep(ConstExponents m, ConstExponents p, ConstExponents q, ConstExponents pq)
static bool is_one(ConstExponents a)
gc_vector< Exponent > Vector
static void radical(ConstExponents a, Vector &result)
static int compare(ConstExponents a, ConstExponents b)
std::make_unsigned< Exponent >::type HashExponent
static HashExponent computeHashValue(ConstExponents vp)
ExponentListIterator< E, L > Iterator
const Exponent * ConstExponents
static void one(Vector &result)
static Exponent weight(ConstExponents m, const std::vector< T > &wts)
static void lcm(ConstExponents a, ConstExponents b, Vector &result)
static void mult(ConstExponents a, ConstExponents b, Vector &result)
static void from_arrayint(M2_arrayint m, Vector &result)
static void quotient(ConstExponents a, ConstExponents b, Vector &result)
static Exponent topvar(ConstExponents a)
static void from_expvector(int n, exponents::ConstExponents a, Vector &result)
static void gcd(ConstExponents a, ConstExponents b, Vector &result)
static Exponent simple_degree(ConstExponents m)
static void elem_text_out(buffer &o, ConstExponents m, bool p_one=true)
static void monsyz(ConstExponents a, ConstExponents b, Vector &sa, Vector &sb)
static void erase(ConstExponents a, ConstExponents b, Vector &result)
static void copy(ConstExponents vp, Vector &result)
static bool divides(ConstExponents a, ConstExponents b)
static M2_arrayint to_arrayint(ConstExponents vp)
static const Exponent length(ConstExponents m)
static const Exponent npairs(ConstExponents m)
static bool is_pure_power(ConstExponents a, Exponent &v, Exponent &e)
static bool is_equal(ConstExponents a, ConstExponents b)
static void to_expvector(int n, ConstExponents a, exponents::Exponents result)
static void var(Exponent v, Exponent e, Vector &result)
static void power(ConstExponents a, Exponent n, Vector &result)
Exponent * Exponents
ExponentListIterator(const Exponent *m)
ExponentListIterator(const ExponentListIterator &i)
ExponentListIterator & operator++()
Engine-wide include prelude — a single point of truth for portability shims.
int p
VALGRIND_MAKE_MEM_DEFINED & result(result)
typename std::vector< T, gc_allocator< T > > gc_vector
a version of the STL vector, which allocates its backing memory with gc.
Definition newdelete.hpp:76
const int EQ
Definition style.hpp:40
const int GT
Definition style.hpp:41
const int LT
Definition style.hpp:39