Macaulay2 Engine
Loading...
Searching...
No Matches
f4-computation.cpp
Go to the documentation of this file.
1// Copyright 2005 Michael E. Stillman.
2
4
5#include "buffer.hpp" // for buffer
6#include "error.h" // for ERROR
7#include "f4/f4-m2-interface.hpp" // for F4toM2Interface
8#include "f4/f4-types.hpp" // for gb_array, gbelem
9#include "f4/f4.hpp" // for F4GB
10#include "f4/moninfo.hpp" // for MonomialInfo
11#include "matrix-con.hpp" // for MatrixConstructor
12#include "matrix.hpp" // for Matrix
13#include "mem.hpp" // for stash
14#include "monoid.hpp" // for Monoid
15#include "polyring.hpp" // for PolynomialRing
16#include "ring.hpp" // for Ring
17#include "ringelem.hpp" // for vec
18#include "text-io.hpp" // for emit
19#include "util.hpp" // for M2_arrayint_to_stdvector
20#include "VectorArithmetic.hpp" // for VectorArithmetic, ElementArray
21
22class Computation;
23class RingElement;
24
26 M2_bool collect_syz,
27 int n_rows_to_keep,
28 M2_arrayint gb_weights,
29 int strategy,
30 M2_bool use_max_degree,
31 int max_degree,
32 int numThreads)
33{
35 if (R == nullptr)
36 {
37 ERROR("internal error: expected a polynomial ring for `Algorithm => LinearAlgebra` groebner basis");
38 return nullptr;
39 }
40 const Ring *K = R->getCoefficients();
41
42 // TODO: code here used to detect whether R, K is a valid ring here
43 if (not R->is_commutative_ring())
44 {
45 ERROR("expected commutative polynomial ring for `Algorithm => LinearAlgebra` groebner basis");
46 return nullptr;
47 }
48 if (R->is_quotient_ring())
49 {
50 ERROR("can't use quotient polynomial rings with `Algorithm => LinearAlgebra` groebner basis");
51 return nullptr;
52 }
53 if (not m->is_homogeneous())
54 {
55 ERROR("expected homogeneous input for `Algorithm => LinearAlgebra` groebner basis");
56 return nullptr;
57 }
58 if (not K->isFinitePrimeField() and not K->isGaloisField())
59 {
60 ERROR("expected coefficient ring to be a finite field for `Algorithm => LinearAlgebra` groebner basis");
61 return nullptr;
62 }
63 auto vectorArithmetic = new VectorArithmetic(K);
64
65 return new F4Computation(vectorArithmetic,
66 m,
67 collect_syz,
68 n_rows_to_keep,
69 gb_weights,
70 strategy,
71 use_max_degree,
72 max_degree,
73 numThreads);
74}
75
77 const Matrix *m,
78 M2_bool collect_syz,
79 int n_rows_to_keep,
80 M2_arrayint gb_weights,
81 int strategy,
82 M2_bool use_max_degree,
83 int max_degree,
84 int numThreads)
85 : mFreeModule(m->rows()),
87{
88 mOriginalRing = m->get_ring()->cast_to_PolynomialRing();
89
90 std::vector<int> heftDegrees = M2_arrayint_to_stdvector<int> (gb_weights);
91 std::vector<int> moduleHeftDegrees;
92 for (int j = 0; j < mFreeModule->rank(); ++j)
93 {
94 moduleHeftDegrees.push_back(mFreeModule->primary_degree(j));
95
96 }
97 mMonoid = new MonomialInfo(mOriginalRing->n_vars(),
98 mOriginalRing->getMonoid()->getMonomialOrdering(),
99 heftDegrees,
100 moduleHeftDegrees);
101
102 if (m->n_rows() >= 2)
103 {
104 if (mMonoid->componentLocation() != -1 or
105 mMonoid->positionUp() != 1)
106 {
107 delete mMonoid;
108 throw exc::engine_error("must use Position=>Up at end of monomial order for Algorithm=>LinearAlgebra groebner basis");
109 }
110 }
111
113 mMonoid,
114 m->rows(),
115 collect_syz,
116 n_rows_to_keep,
117 gb_weights,
118 strategy,
119 use_max_degree,
120 max_degree,
121 numThreads);
122
124 mF4GB->new_generators(0, m->n_cols() - 1);
125}
126
128/*************************
129 ** Top level interface **
130 *************************/
131
132void F4Computation::start_computation() { mF4GB->start_computation(stop_); }
133
135 const RingElement *hf)
136{
137 mF4GB->set_hilbert_function(hf);
138 return this;
139}
140
141const Matrix /* or null */ *F4Computation::get_gb()
142{
143 const gb_array &gb = mF4GB->get_gb();
144 GBSorter C(mMonoid, gb);
145 auto gbsize = gb.size();
146 int *gb_order = new int[gbsize];
147 for (int i = 0; i < gbsize; i++)
148 {
149 gb_order[i] = i;
150 }
151
152 std::stable_sort(gb_order, gb_order + gbsize, C);
153
154 // Order: increasing in monomial order
156 for (int i = 0; i < gbsize; i++)
157 {
158 int which = gb_order[i];
161 result.append(v);
162 }
163 return result.to_matrix();
164}
165
166const Matrix /* or null */ *F4Computation::get_mingens()
167{
168 ERROR("Minimal generators for all `Algorithm => LinearAlgebra` Groebner bases not computed");
169 return nullptr;
170}
171
172const Matrix /* or null */ *F4Computation::get_change()
173{
174 ERROR("Change of basis matrix for all `Algorithm => LinearAlgebra` Groebner bases not computed");
175 return nullptr;
176}
177
179{
180 ERROR("Syzygies for `Algorithm => LinearAlgebra` Groebner bases not computed");
181 return nullptr;
182}
183
184const Matrix /* or null */ *F4Computation::get_initial(int nparts)
185{
186 (void) nparts;
187 ERROR("Lead terms for `Algorithm => LinearAlgebra`: use `leadTerm gens gb` instead of `leadTerm gb`");
188 return nullptr;
189}
190
191const Matrix /* or null */ *F4Computation::matrix_remainder(const Matrix *m)
192{
193 (void) m;
194 ERROR("No special algorithm for computing matrix remainder for `Algorithm => LinearAlgebra");
195 return nullptr;
196}
197
199 const Matrix *m,
200 const Matrix /* or null */ **result_remainder,
201 const Matrix /* or null */ **result_quotient)
202{
203 (void) m;
204 *result_remainder = nullptr;
205 *result_quotient = nullptr;
206 ERROR("No special algorithm for computing matrix remainder and lift for `Algorithm => LinearAlgebra");
207 return false;
208}
209
211// Return -1 if every column of 'm' reduces to zero.
212// Otherwise return the index of the first column that
213// does not reduce to zero.
214{
215 (void) m;
216 ERROR("No special algorithm for computing containment for `Algorithm => LinearAlgebra");
217 return 0;
218}
219
221// The computation is complete up through this degree.
222{
223 // TODO!
224 return 0;
225}
226
228/* This displays statistical information, and depends on the
229 M2_gbTrace value */
230{
231 (void) o;
232 // TODO: what to display?
233}
234
235void F4Computation::show() const // debug display
236{
237 buffer o;
238 stash::stats(o);
239 emit(o.str());
240
241 // f4->show();
242}
243
244// Local Variables:
245// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
246// indent-tabs-mode: nil
247// End:
Coefficient-ring-erased arithmetic dispatcher used by F4, GB, and resolution code.
Append-only GC-backed byte buffer used throughout the engine for text output.
StopConditions stop_
Definition comp.hpp:75
Computation()
Definition comp.cpp:40
Abstract base for long-running, resumable engine computations (GBComputation, ResolutionComputation,...
Definition comp.hpp:70
const PolynomialRing * mOriginalRing
void start_computation() override
const VectorArithmetic * mVectorArithmetic
int contains(const Matrix *m) override
const FreeModule * mFreeModule
M2_bool matrix_lift(const Matrix *m, const Matrix **result_remainder, const Matrix **result_quotient) override
int complete_thru_degree() const override
MonomialInfo * mMonoid
const Matrix * get_gb() override
const Matrix * get_change() override
void show() const override
const Matrix * matrix_remainder(const Matrix *m) override
const Matrix * get_initial(int nparts) override
Computation * set_hilbert_function(const RingElement *h) override
~F4Computation() override
F4Computation(const VectorArithmetic *VA, const Matrix *m, M2_bool collect_syz, int n_rows_to_keep, M2_arrayint gb_weights, int strategy, M2_bool use_max_degree, int max_degree, int numThreads)
const Matrix * get_mingens() override
const Matrix * get_syzygies() override
void text_out(buffer &o) const override
GBComputation subclass that drives an F4GB engine instance from the engine-side computation API.
Commutative F4 Groebner-basis driver: degree-by-degree Macaulay matrix construction plus row-reductio...
Definition f4.hpp:154
static void from_M2_matrix(const VectorArithmetic *VA, const MonomialInfo *MI, const Matrix *m, gb_array &result_polys)
static vec to_M2_vec(const VectorArithmetic *VA, const MonomialInfo *MI, const GBF4Polynomial &f, const FreeModule *F)
base class for Groebner basis computations.
Definition comp-gb.hpp:69
Comparator that orders indices into the current GB array (gb_array) by each gbelem's leading monomial...
Definition f4-types.hpp:249
const Ring * get_ring() const
Definition matrix.hpp:134
int n_cols() const
Definition matrix.hpp:147
int n_rows() const
Definition matrix.hpp:146
bool is_homogeneous() const
Definition matrix.cpp:332
const FreeModule * rows() const
Definition matrix.hpp:144
Mutable builder used to assemble an immutable Matrix one column (or one term) at a time.
Per-ring monomial layout / encoding helper used by F4GB.
Definition moninfo.hpp:108
Abstract base for the engine's polynomial-ring hierarchy.
Definition polyring.hpp:96
virtual bool is_quotient_ring() const
Definition ring.hpp:192
virtual bool isGaloisField() const
Definition ring.hpp:170
virtual bool is_commutative_ring() const
Definition ring.hpp:189
virtual const PolynomialRing * cast_to_PolynomialRing() const
Definition ring.hpp:243
virtual bool isFinitePrimeField() const
Definition ring.hpp:169
const Ring * R
Definition relem.hpp:68
Front-end-visible "ring element" value: an engine ring_elem paired with the Ring* that gives it meani...
Definition relem.hpp:67
xxx xxx xxx
Definition ring.hpp:102
Runtime dispatcher that hides the concrete coefficient ring behind a std::variant of ConcreteVectorAr...
char * str()
Definition buffer.hpp:72
static void stats(buffer &o)
Definition mem.cpp:93
Engine error-reporting primitives: ERROR, INTERNAL_ERROR, error, error_message.
GBComputation * createF4GB(const Matrix *m, M2_bool collect_syz, int n_rows_to_keep, M2_arrayint gb_weights, int strategy, M2_bool use_max_degree, int max_degree, int numThreads)
F4Computation — GBComputation adapter around the F4 inner-loop engine.
F4toM2Interface — static translators between engine vec / Matrix and F4's GBF4Polynomial.
std::vector< gbelem * > gb_array
Definition f4-types.hpp:145
Shared type vocabulary used across the F4 engine.
F4GB — the inner-loop Faugère F4 Groebner-basis algorithm.
#define Matrix
Definition factory.cpp:14
void gb(IntermediateBasis &F, int n)
const int ERROR
Definition m2-mem.cpp:55
VALGRIND_MAKE_MEM_DEFINED & result(result)
char M2_bool
Definition m2-types.h:82
MatrixConstructor — the mutable builder that produces an immutable Matrix.
Matrix — the engine's immutable homomorphism F -> G between free modules.
stash and doubling_stash — legacy size-class allocator interfaces, now stubbed to plain GC allocation...
MonomialInfo — F4's packed_monomial encoding plus operations.
Monoid — variable count, naming, grading, and monomial order of a polynomial ring.
PolynomialRing — abstract polynomial-ring base, the engine's most-reused class.
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.
void emit(const char *s)
Definition text-io.cpp:41
Text-formatting helpers layered on buffer: bignum print, line wrapping, M2_gbTrace-gated emit.
std::vector< T > M2_arrayint_to_stdvector(M2_arrayint arr)
Definition util.hpp:96
Conversion helpers between M2 boundary types and standard C++ containers.