Macaulay2 Engine
Loading...
Searching...
No Matches
M2FreeAlgebraQuotient.cpp
Go to the documentation of this file.
2
3#include <iostream>
4#include <memory>
5#include <utility>
6#include <vector>
7
9#include "matrix.hpp"
10#include "ring.hpp"
11
13 const Matrix* input)
14{
16 result.reserve(input->n_cols());
17 for (int i=0; i<input->n_cols(); i++)
18 {
19 ring_elem a = input->elem(0,i);
20 auto f = reinterpret_cast<const Poly*>(a.get_Poly());
21 auto g = new Poly;
22 F.freeAlgebra().copy(*g, *f);
23 result.push_back(g);
24 }
25 return result;
26}
27
29 const M2FreeAlgebra& F,
30 const Matrix* GB,
31 int maxdeg // TODO: need to handle use of 'maxdeg' in the class
32 )
33{
34 auto gbElements = copyMatrixToVector(F, GB);
35 auto A = std::unique_ptr<FreeAlgebraQuotient> (new FreeAlgebraQuotient(F.freeAlgebra(), gbElements, maxdeg));
37 result->initialize_ring(F.coefficientRing()->characteristic(), F.degreeRing(), {});
38 result->zeroV = result->from_long(0);
39 result->oneV = result->from_long(1);
40 result->minus_oneV = result->from_long(-1);
41
42 return result;
43}
44
46 std::unique_ptr<FreeAlgebraQuotient> A)
47 : mM2FreeAlgebra(F),
49{
50}
51
53{
54 o << "Quotient of ";
56}
57
59{
60 (void) a;
61 return 0; // TODO: change this to a more reasonable hash code.
62}
63
68
70{
71 auto result = new Poly;
73 return ring_elem(reinterpret_cast<void *>(result));
74}
75
80
85
86bool M2FreeAlgebraQuotient::from_rational(const mpq_srcptr q, ring_elem& result1) const
87{
88 ring_elem cq; // in coeff ring.
89 bool worked = coefficientRing()->from_rational(q, cq);
90 if (!worked) return false;
91 result1 = from_coefficient(cq);
92 return true;
93}
94
96{
97 auto result = new Poly;
99 return ring_elem(reinterpret_cast<void *>(result));
100}
101
103{
104 // std::cout << "called promote NC case" << std::endl;
105 // Currently the only case to handle is R = A --> this, and A is the coefficient ring of this.
106 if (R == coefficientRing())
107 {
109 return true;
110 }
111 if (R == &m2FreeAlgebra())
112 {
113 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
114 auto resultf = new Poly;
115 freeAlgebraQuotient().copy(*resultf, *f);
117 result = ring_elem(reinterpret_cast<void *>(resultf));
118 return true;
119 }
120 return false;
121}
122
124{
125 // R is the target ring
126 // f1 is an element of 'this'.
127 // set result to be the "lift" of f in the ring R, return true if this is possible.
128 // otherwise return false.
129
130 // case: R is the coefficient ring of 'this'.
131 if (R == coefficientRing())
132 {
133 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
134 if (f->numTerms() != 1) return false;
135 auto i = f->cbegin();
136 if (monoid().is_one(i.monom()))
137 {
138 result = coefficientRing()->copy(i.coeff());
139 return true;
140 }
141 return false;
142 }
143 if (R == &m2FreeAlgebra())
144 {
145 // just copy the element into result, considered in the free algebra.
146 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
147 auto resultf = new Poly;
148 freeAlgebra().copy(*resultf, *f);
149 result = ring_elem(reinterpret_cast<void *>(resultf));
150 return true;
151 }
152
153 // at this point, we can't lift it.
154 return false;
155}
156
158{
159 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
160 return freeAlgebraQuotient().is_unit(*f);
161}
162
164{
165 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
166 return freeAlgebraQuotient().n_terms(*f);
167}
168
170{
171 return n_terms(f1) == 0;
172}
173
175{
176 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
177 auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
178 return freeAlgebraQuotient().is_equal(*f,*g);
179}
180
182{
183 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
184 auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
185 return freeAlgebraQuotient().compare_elems(*f,*g);
186}
187
189{
190 // FRANK: is this what we want to do?
191 return f;
192}
193
195{
196 (void) f;
197}
198
200{
201 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
202 Poly* result = new Poly;
204 return ring_elem(reinterpret_cast<void *>(result));
205}
206
208{
209 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
210 auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
211 auto result = new Poly;
213 return ring_elem(reinterpret_cast<void *>(result));
214}
215
217{
218 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
219 auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
220 auto result = new Poly;
222 return ring_elem(reinterpret_cast<void *>(result));
223}
224
226{
227 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
228 auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
229 auto result = new Poly;
231 return ring_elem(reinterpret_cast<void *>(result));
232}
233
235{
236 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
237 auto result = new Poly;
239 return ring_elem(reinterpret_cast<void *>(result));
240}
241
243{
244 auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
245 auto result = new Poly;
247 return ring_elem(reinterpret_cast<void *>(result));
248}
249
251{
252 return m2FreeAlgebra().invert(f);
253}
254
256{
257 return m2FreeAlgebra().divide(f, g);
258}
259
261 ring_elem &x, ring_elem &y) const
262{
263 (void) a;
264 (void) b;
265 (void) x;
266 (void) y;
267 // TODO: In the commutative case, this function is to find x and y (as simple as possible)
268 // such that ax + by = 0. No such x and y may exist in the noncommutative case, however.
269 // In this case, the function should return x = y = 0.
270}
271
273{
274 std::cout << "coeffs: ";
275 for (auto i=f->cbeginCoeff(); i != f->cendCoeff(); ++i)
276 {
277 buffer o;
279 std::cout << o.str() << " ";
280 }
281 std::cout << std::endl << " monoms: ";
282 for (auto i=f->cbeginMonom(); i != f->cendMonom(); ++i)
283 {
284 std::cout << (*i) << " ";
285 }
286 std::cout << std::endl;
287}
288
290
291{
292 auto f = reinterpret_cast<const Poly*>(ff.get_Poly());
293 debug_display(f);
294}
295
297{
298 m2FreeAlgebra().makeTerm(result, a, monom);
300}
301
303{
304 Poly* f = new Poly;
305 makeTerm(*f, a, monom);
306 return ring_elem(reinterpret_cast<void*>(f));
307}
308
310 const ring_elem ff,
311 bool p_one,
312 bool p_plus,
313 bool p_parens) const
314{
315 auto f = reinterpret_cast<const Poly*>(ff.get_Poly());
316 freeAlgebraQuotient().elem_text_out(o,*f,p_one,p_plus,p_parens);
317}
318
319ring_elem M2FreeAlgebraQuotient::eval(const RingMap *map, const ring_elem ff, int first_var) const
320{
321 // map: R --> S, this = R.
322 // f is an ele ment in R
323 // return an element of S.
324
325 auto f = reinterpret_cast<const Poly*>(ff.get_Poly());
326 auto g = freeAlgebraQuotient().eval(map, *f, first_var);
327 return g;
328}
329
331{
332 // Either coeffR should be the actual coefficient ring (possible a "toField"ed ring)
333 // or a polynomial ring. If not, NULL is returned and an error given
334 // In the latter case, the last set of variables are part of
335 // the coefficients.
336 return m2FreeAlgebra().list_form(coeffR, ff);
337}
338
340{
341 return m2FreeAlgebra().lead_coefficient(coeffRing, f);
342}
343
345{
346 const Poly* f = reinterpret_cast<const Poly*>(f1.get_Poly());
347 return is_homogeneous(f);
348}
349
351{
352 if (f == nullptr) return true;
354}
355
357{
358 const Poly* f = reinterpret_cast<const Poly*>(g.get_Poly());
359 return multi_degree(f, d);
360}
361
363{
364 return freeAlgebraQuotient().multi_degree(*f, d);
365}
366
371
372// Local Variables:
373// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
374// indent-tabs-mode: nil
375// End:
varpower::ConstExponents const_varpower
Free associative algebra k<x_1,...,x_n> over an arbitrary coefficient ring.
ConstPolyList copyMatrixToVector(const M2FreeAlgebra &F, const Matrix *input)
Ring-shaped façade around a non-commutative quotient algebra.
gc_vector< const Poly * > ConstPolyList
Polynomial< CoefficientRingType > Poly
void copy(Poly &result, Poly::const_iterator fBegin, Poly::const_iterator fEnd) const
void from_coefficient(Poly &result, const ring_elem a) const
SumCollector * make_SumCollector() const
void mult(Poly &result, const Poly &f, const Poly &g) const
void elem_text_out(buffer &o, const Poly &f, bool p_one, bool p_plus, bool p_parens) const
ring_elem eval(const RingMap *map, const Poly &f, int first_var) const
int compare_elems(const Poly &f, const Poly &g) const
bool is_unit(const Poly &f) const
void power(Poly &result, const Poly &f, int n) const
bool is_homogeneous(const Poly &f) const
long n_terms(const Poly &f) const
void add(Poly &result, const Poly &f, const Poly &g) const
void var(Poly &result, int v) const
void subtract(Poly &result, const Poly &f, const Poly &g) const
bool multi_degree(const Poly &f, monomial already_allocated_degree_vector) const
bool is_equal(const Poly &f, const Poly &g) const
void copy(Poly &result, const Poly &f) const
void normalizeInPlace(Poly &f) const
void negate(Poly &result, const Poly &f) const
Quotient of a FreeAlgebra by a Groebner basis up to a fixed degree bound.
virtual ring_elem invert(const ring_elem f) const
ring_elem lead_coefficient(const Ring *coeffRing, const Poly *f) const
const FreeAlgebra & freeAlgebra() const
const Ring * coefficientRing() const
virtual void text_out(buffer &o) const
ring_elem makeTerm(const ring_elem a, const_varpower monom) const
virtual int index_of_var(const ring_elem a) const
virtual ring_elem divide(const ring_elem f, const ring_elem g) const
virtual engine_RawArrayPairOrNull list_form(const Ring *coeffR, const ring_elem f) const
const PolynomialRing * degreeRing() const
Concrete Ring wrapper around an owned FreeAlgebra (no quotient).
virtual bool lift(const Ring *R, const ring_elem f, ring_elem &result) const
virtual ring_elem divide(const ring_elem f, const ring_elem g) const
virtual ring_elem invert(const ring_elem f) const
virtual bool multi_degree(const ring_elem f, monomial d) const
static M2FreeAlgebraQuotient * create(const M2FreeAlgebra &F, const Matrix *GB, int maxdeg)
virtual SumCollector * make_SumCollector() const
virtual bool is_equal(const ring_elem f, const ring_elem g) const
virtual ring_elem from_int(mpz_srcptr n) const
virtual ring_elem eval(const RingMap *map, const ring_elem f, int first_var) const
virtual bool from_rational(const mpq_srcptr q, ring_elem &result) const
virtual ring_elem negate(const ring_elem f) const
virtual ring_elem subtract(const ring_elem f, const ring_elem g) const
virtual void remove(ring_elem &f) const
virtual ring_elem var(int v) const
const FreeAlgebra & freeAlgebra() const
const M2FreeAlgebra & mM2FreeAlgebra
virtual engine_RawArrayPairOrNull list_form(const Ring *coeffR, const ring_elem f) const
virtual void elem_text_out(buffer &o, const ring_elem f, bool p_one, bool p_plus, bool p_parens) const
const FreeAlgebraQuotient & freeAlgebraQuotient() const
void debug_display(const Poly *f) const
virtual ring_elem from_coefficient(const ring_elem a) const
virtual ring_elem from_long(long n) const
void makeTerm(Poly &result, const ring_elem a, const int *monom) const
virtual bool is_zero(const ring_elem f) const
virtual ring_elem mult(const ring_elem f, const ring_elem g) const
const Ring * coefficientRing() const
virtual void text_out(buffer &o) const
virtual ring_elem add(const ring_elem f, const ring_elem g) const
virtual int index_of_var(const ring_elem a) const
ring_elem lead_coefficient(const Ring *coeffRing, const Poly *f) const
virtual ring_elem power(const ring_elem f, mpz_srcptr n) const
Exponentiation. This is the default function, if a class doesn't define this.
virtual int compare_elems(const ring_elem f, const ring_elem g) const
virtual void syzygy(const ring_elem a, const ring_elem b, ring_elem &x, ring_elem &y) const
const M2FreeAlgebra & m2FreeAlgebra() const
long n_terms(const ring_elem f) const
virtual ring_elem copy(const ring_elem f) const
virtual bool is_homogeneous(const ring_elem f) const
M2FreeAlgebraQuotient(const M2FreeAlgebra &F, std::unique_ptr< FreeAlgebraQuotient > A)
virtual bool is_unit(const ring_elem f) const
virtual unsigned int computeHashValue(const ring_elem a) const
const FreeMonoid & monoid() const
const std::unique_ptr< FreeAlgebraQuotient > mFreeAlgebraQuotient
virtual bool promote(const Ring *R, const ring_elem f, ring_elem &result) const
ring_elem elem(int i, int j) const
Definition matrix.cpp:307
int n_cols() const
Definition matrix.hpp:147
virtual void elem_text_out(buffer &o, const ring_elem f, bool p_one=true, bool p_plus=false, bool p_parens=false) const =0
long characteristic() const
Definition ring.hpp:159
virtual ring_elem copy(const ring_elem f) const =0
virtual bool from_rational(const mpq_srcptr q, ring_elem &result) const =0
Ring()
Definition ring.hpp:136
Engine-side ring homomorphism: stores, for each source-ring variable, the target-ring element it maps...
Definition ringmap.hpp:60
Abstract incremental accumulator that builds a ring_elem from many add(f) calls.
Definition ring.hpp:669
char * str()
Definition buffer.hpp:72
#define Matrix
Definition factory.cpp:14
#define monomial
Definition gb-toric.cpp:11
VALGRIND_MAKE_MEM_DEFINED & result(result)
engine_RawArrayPair engine_RawArrayPairOrNull
Definition m2-types.h:184
Matrix — the engine's immutable homomorphism F -> G between free modules.
STL namespace.
volatile int x
Ring — the legacy abstract base class for every coefficient and polynomial ring.
const void * get_Poly() const
Definition ringelem.hpp:128