Macaulay2 Engine
Loading...
Searching...
No Matches
FreeAlgebra.hpp
Go to the documentation of this file.
1#ifndef _free_algebra_hpp_
2#define _free_algebra_hpp_
3
35
36#include "Polynomial.hpp" // for Poly, Monom
37#include "NCAlgebras/FreeMonoid.hpp" // for FreeMonoid
38#include "NCAlgebras/Word.hpp" // for Word
39#include "newdelete.hpp" // for our_new_delete
40#include "ring.hpp" // for Ring (ptr only), SumCollector
41#include "ringelem.hpp" // for ring_elem
42#include "style.hpp" // for GEOHEAP_SIZE
43
44#include <iosfwd> // for ostream, string
45#include <utility> // for pair
46#include <vector> // for vector
47
48class Monoid;
49class PolynomialRing;
50class RingMap;
51class buffer;
52
69{
70private:
72 std::shared_ptr<FreeMonoid> mMonoid;
73
74
75private:
76 FreeAlgebra(const Ring* K, std::shared_ptr<FreeMonoid> M);
77
78public:
79 static FreeAlgebra* create(const Ring* K,
80 const std::vector<std::string>& names,
82 const std::vector<int>& degrees,
83 const std::vector<int>& wtvecs,
84 const std::vector<int>& heftVector
85 );
86
87 const Ring* coefficientRing() const { return &mCoefficientRing; }
88 const FreeMonoid& monoid() const { return *mMonoid; }
89 const Monoid& degreeMonoid() const { return monoid().degreeMonoid(); }
90 int numVars() const { return monoid().numVars(); }
91
92 unsigned int computeHashValue(const Poly& a) const; // TODO
93
94 void init(Poly& f) const { (void) f; }
95 void clear(Poly& f) const;
96 void setZero(Poly& f) const;
97
98 void copy(Poly& result, Poly::const_iterator fBegin, Poly::const_iterator fEnd) const;
99
100 void from_coefficient(Poly& result, const ring_elem a) const;
101 void from_long(Poly& result, long n) const;
102 void from_int(Poly& result, mpz_srcptr n) const;
103 bool from_rational(Poly& result, const mpq_srcptr q) const;
104 void copy(Poly& result, const Poly& f) const;
105 void swap(Poly& f, Poly& g) const;
106 void var(Poly& result, int v) const;
107
108 M2_arrayint support(const Poly& f) const;
109
110 void from_word(Poly& result, const Word& word) const;
111 void from_word(Poly& result, const std::vector<int>& word) const;
112 void from_word(Poly& result, ring_elem coeff, const Word& word) const;
113 void from_word(Poly& result, ring_elem coeff, const std::vector<int>& word) const;
114
115 long n_terms(const Poly& f) const { return f.numTerms(); }
116 bool is_unit(const Poly& f) const;
117 bool is_zero(const Poly& f) const { return n_terms(f) == 0; }
118 bool is_equal(const Poly& f, const Poly& g) const;
119 int compare_elems(const Poly& f, const Poly& g) const;
120
121 void add(Poly& result,
122 Poly::const_iterator fBegin,
123 Poly::const_iterator fEnd,
124 Poly::const_iterator gBegin,
125 Poly::const_iterator gEnd) const;
127 Poly::const_iterator fBegin,
128 Poly::const_iterator fEnd,
129 Poly::const_iterator gBegin,
130 Poly::const_iterator gEnd,
131 ring_elem coeff) const;
132
133 void negate(Poly& result, const Poly& f) const;
134 void add(Poly& result, const Poly& f, const Poly& g) const;
135
136 void subtract(Poly& result, const Poly& f, const Poly& g) const;
138 const Poly& f,
139 const Poly& g,
140 ring_elem coeff) const;
141 void mult(Poly& result, const Poly& f, const Poly& g) const;
142 void power(Poly& result, const Poly& f, int n) const;
143 void power(Poly& result, const Poly& f, mpz_srcptr n) const;
144
145 ring_elem eval(const RingMap *map, const Poly& f, int first_var) const;
146
147 void makeMonic(Poly& result, Poly& f) const;
148 void makeMonicInPlace(Poly& f) const;
149
150 void elem_text_out(buffer &o,
151 const Poly& f,
152 bool p_one,
153 bool p_plus,
154 bool p_parens) const;
155
156 bool is_homogeneous(const Poly& f) const;
157 // FIXME: copied from ring.hpp because this doesn't inherit from Ring
158 inline const_monomial degree(const Poly& f) const
159 {
160 auto d = degreeMonoid().make_one();
161 multi_degree(f, d);
162 return d;
163 }
164 // returns true if f is homogeneous, and sets already_allocated_degree_vector
165 // to be the LCM of the exponent vectors of the degrees of all terms in f.
166 bool multi_degree(const Poly& f, monomial already_allocated_degree_vector) const;
167
168 // Returns the pair (d, ishomog) where
169 // d is the largest heft of the degree of each monomial in 'f'.
170 // ishomog is true when all monomials have the same *heft* degree
171 std::pair<int, bool> heft_degree(const Poly& f) const;
172
174 const Poly& f,
175 const ring_elem c,
176 const Monom m) const;
178 const Poly& f,
179 const ring_elem c,
180 const Monom m) const;
182 const Poly& f,
183 const ring_elem c,
184 const Monom leftM,
185 const Monom rightM) const;
187 const Poly& f,
188 const Monom leftM,
189 const Monom rightM) const;
191 const Poly& f,
192 const ring_elem c,
193 const Word& w) const;
195 const Poly& f,
196 const ring_elem c,
197 const Word& w) const;
199 const Poly& f,
200 const ring_elem c,
201 const Word& leftW,
202 const Word& rightW) const;
204 const Poly& f,
205 const Word& leftW,
206 const Word& rightW) const;
207 void add_to_end(Poly& f, const Poly& g) const;
208 void add_to_end(Poly& f, ring_elem coeff, const Monom& monom) const;
209
210 void lead_term_as_poly(Poly& result, const Poly& f) const;
211 void mult_by_coeff(Poly& result, const Poly& f, const ring_elem c) const;
212
214
215 Word lead_word(const Poly& f) const;
216 Word lead_word_prefix(const Poly& f, int endIndex) const;
217 Word lead_word_suffix(const Poly& f, int beginIndex) const;
218
219 // this stuff has yet to be moved over from ../M2FreeAlgebra
220#if 0
221
222 int index_of_var(const Poly& a) const;
223
224 void text_out(buffer &o) const; // need?
225
226 // if not lift/promoteable, return nullptr
227 Poly* promote(const Ring *R, const ring_elem f) const;
228 Poly* lift(const Ring *R, const ring_elem f) const;
229
230
231 engine_RawArrayPairOrNull list_form(const Ring *coeffR,
232 const Poly* f) const;
233
234 // lead coefficient, monomials and terms.
235 ring_elem lead_coefficient(const Ring* coeffRing, const Poly* f) const;
236
237 // lead terms, or get contiguous terms
238 Poly* get_terms(const Poly* f, int lo, int hi) const;
239
240 void debug_display(const Poly* f) const;
241
242 Poly* makeTerm(const ring_elem a, const_varpower monom) const;
243
244 void setZero(Poly& f) const // resets f to zero
245 {
246 for (auto a : f.mCoefficients)
248
249 f.mCoefficients.clear();
250 f.mMonomials.clear();
251 }
252
253#endif
254};
255
268// For debugging purposes
270{
271public:
273 : mRing(F)
274 {
275 mRing->init(mPoly);
276 }
278 : mRing(F),
279 mPoly(f)
280 {
281 }
283 {
284 mRing->clear(mPoly);
285 }
286 const FreeAlgebra& ring() const
287 {
288 return *mRing;
289 }
291 {
292 return mPoly;
293 }
294 const Poly& operator*() const
295 {
296 return mPoly;
297 }
298 bool operator==(const FreeAlgebraElement& g) const
299 {
300 return this->mRing->is_equal(**this,*g);
301 }
303 {
305 mRing->add(*result, **this, *g);
306 return result; // this is a copy
307 }
309 {
311 mRing->subtract(*result, **this, *g);
312 return result; // this is a copy
313 }
315 {
317 mRing->negate(*result, **this);
318 return result;
319 }
321 {
323 mRing->mult(*result, **this, *g);
324 return result; // this is a copy
325 }
327 {
329 mRing->power(*result, **this, n);
330 return result; // this is a copy
331 }
332private:
335};
336
337std::ostream& operator<<(std::ostream& o, const FreeAlgebraElement& f);
338
339// FreeAlgebraHeap and the SumCollector below are used for eval and mult
340
356{
357 const FreeAlgebra& F; // Our elements will be vectors in here
360
361 public:
364
365 void add(const Poly& f);
366 void value(Poly& result); // Returns the linearized value, and resets the FreeAlgebraHeap.
367
368 const Poly& debug_list(int i) const
369 {
370 return heap[i];
371 } // DO NOT USE, except for debugging purposes!
372};
373
388{
390
391 public:
394 virtual void add(ring_elem f1)
395 {
396 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
397 H.add(*f);
398 }
400 {
401 Poly* result = new Poly;
402 H.value(*result);
403 return ring_elem(reinterpret_cast<void *>(result));
404 }
405};
406
407#endif
408
409// Local Variables:
410// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
411// indent-tabs-mode: nil
412// End:
413
varpower::ConstExponents const_varpower
std::ostream & operator<<(std::ostream &o, const FreeAlgebraElement &f)
FreeMonoid — monoid of length-prefixed non-commutative words with weight-vector prefix.
std::vector< int > word
Polynomial< CoefficientRingType > Poly
Modern Monom / Polynomial value types shared by NC algebras and the refactored F4.
Word and WordWithData — non-owning views over the flat-int encoding of a non-commutative word.
const Ring * coefficientRing() const
void add(Poly &result, Poly::const_iterator fBegin, Poly::const_iterator fEnd, Poly::const_iterator gBegin, Poly::const_iterator gEnd) const
void power(Poly &result, const Poly &f, int n) const
void negate(Poly &result, const Poly &f) const
bool is_unit(const Poly &f) const
void subtract(Poly &result, const Poly &f, const Poly &g) const
void copy(Poly &result, Poly::const_iterator fBegin, Poly::const_iterator fEnd) const
void mult_by_term_right(Poly &result, const Poly &f, const ring_elem c, const Monom m) const
void add_to_end(Poly &f, const Poly &g) const
void from_long(Poly &result, long n) const
void mult_by_term_left(Poly &result, const Poly &f, const ring_elem c, const Monom m) const
void init(Poly &f) const
void makeMonicInPlace(Poly &f) const
std::pair< int, bool > heft_degree(const Poly &f) const
void from_coefficient(Poly &result, const ring_elem a) const
bool is_homogeneous(const Poly &f) const
void subtractScalarMultipleOf(Poly &result, const Poly &f, const Poly &g, ring_elem coeff) const
void mult(Poly &result, const Poly &f, const Poly &g) const
bool from_rational(Poly &result, const mpq_srcptr q) const
M2_arrayint support(const Poly &f) const
bool multi_degree(const Poly &f, monomial already_allocated_degree_vector) const
void mult_by_coeff(Poly &result, const Poly &f, const ring_elem c) const
void clear(Poly &f) const
void var(Poly &result, int v) const
const_monomial degree(const Poly &f) const
const Monoid & degreeMonoid() const
int numVars() const
Word lead_word_prefix(const Poly &f, int endIndex) const
const Ring & mCoefficientRing
SumCollector * make_SumCollector() const
FreeAlgebra(const Ring *K, std::shared_ptr< FreeMonoid > M)
unsigned int computeHashValue(const Poly &a) const
Word lead_word(const Poly &f) const
const FreeMonoid & monoid() const
void lead_term_as_poly(Poly &result, const Poly &f) const
bool is_zero(const Poly &f) const
static FreeAlgebra * create(const Ring *K, const std::vector< std::string > &names, const PolynomialRing *degreeRing, const std::vector< int > &degrees, const std::vector< int > &wtvecs, const std::vector< int > &heftVector)
void makeMonic(Poly &result, Poly &f) const
void mult_by_term_left_and_right(Poly &result, const Poly &f, const ring_elem c, const Monom leftM, const Monom rightM) const
void from_word(Poly &result, const Word &word) const
void elem_text_out(buffer &o, const Poly &f, bool p_one, bool p_plus, bool p_parens) const
bool is_equal(const Poly &f, const Poly &g) const
long n_terms(const Poly &f) const
std::shared_ptr< FreeMonoid > mMonoid
ring_elem eval(const RingMap *map, const Poly &f, int first_var) const
void from_int(Poly &result, mpz_srcptr n) const
int compare_elems(const Poly &f, const Poly &g) const
Word lead_word_suffix(const Poly &f, int beginIndex) const
void addScalarMultipleOf(Poly &result, Poly::const_iterator fBegin, Poly::const_iterator fEnd, Poly::const_iterator gBegin, Poly::const_iterator gEnd, ring_elem coeff) const
void setZero(Poly &f) const
const Poly & operator*() const
FreeAlgebraElement operator+(const FreeAlgebraElement &g)
FreeAlgebraElement operator*(const FreeAlgebraElement &g)
FreeAlgebraElement(const FreeAlgebra *F, const Poly &f)
const FreeAlgebra * mRing
FreeAlgebraElement operator^(int n)
FreeAlgebraElement(const FreeAlgebra *F)
bool operator==(const FreeAlgebraElement &g) const
FreeAlgebraElement operator-(const FreeAlgebraElement &g)
FreeAlgebraElement operator-() const
const FreeAlgebra & ring() const
Owned Poly value paired with its FreeAlgebra*, providing natural operator-overloaded arithmetic.
const Poly & debug_list(int i) const
FreeAlgebraHeap(const FreeAlgebra &F)
void value(Poly &result)
Poly heap[GEOHEAP_SIZE]
const FreeAlgebra & F
void add(const Poly &f)
Geobucket-style accumulator for many Poly summands in the free associative algebra.
Free associative algebra over a coefficient ring: the non-commutative analogue of PolynomialRing.
const Monoid & degreeMonoid() const
unsigned int numVars() const
The free non-commutative monoid on a set of named variables, with monomial ordering and degree / weig...
monomial make_one() const
Definition monoid.cpp:455
Engine-side commutative monomial monoid: variable names, ordering, multidegree machinery,...
Definition monoid.hpp:89
Abstract base for the engine's polynomial-ring hierarchy.
Definition polyring.hpp:96
virtual void remove(ring_elem &f) const =0
xxx xxx xxx
Definition ring.hpp:102
Engine-side ring homomorphism: stores, for each source-ring variable, the target-ring element it maps...
Definition ringmap.hpp:60
virtual void add(ring_elem f1)
SumCollectorFreeAlgebraHeap(const FreeAlgebra &F)
virtual ring_elem getValue()
Abstract incremental accumulator that builds a ring_elem from many add(f) calls.
Definition ring.hpp:669
Non-owning view of a non-commutative word: [begin, end) of int variable indices.
Definition Word.hpp:56
#define monomial
Definition gb-toric.cpp:11
const int * const_monomial
Definition imonorder.hpp:45
VALGRIND_MAKE_MEM_DEFINED & result(result)
engine_RawArrayPair engine_RawArrayPairOrNull
Definition m2-types.h:184
#define swap(a, b, t)
Definition monsort.hpp:127
our_new_delete — per-class opt-in routing of new / delete through bdwgc.
Ring — the legacy abstract base class for every coefficient and polynomial ring.
ring_elem — the universal value type carried by every Ring* in the engine.
Non-owning view onto a [length, degree, v1, v2, ..., vn] packed monomial in some externally managed b...
#define GEOHEAP_SIZE
Definition style.hpp:46
Engine-wide stylistic constants: LT / EQ / GT codes, INTSIZE, GEOHEAP_SIZE.
const void * get_Poly() const
Definition ringelem.hpp:128
const PolynomialRing * degreeRing(const std::vector< std::string > &names)