Macaulay2 Engine
Loading...
Searching...
No Matches
NewF4Test.cpp
Go to the documentation of this file.
1// In BUILD tree in Macaulay2/e:
2// ./M2-unit-tests --gtest_filter="*F4*"
3
35
36#include <iostream>
37#include <memory>
38#include <bitset>
39#include <gtest/gtest.h>
40
41#include "polyring.hpp"
45#include "VectorArithmetic.hpp"
48
49TEST(NewF4, hashstats)
50{
52
53 stats.dump();
54}
55
57{
58 newf4::HashInt hash = 0;
59 //for (auto a = m.cbegin(); a != m.cend(); ++a) { hash += (a.var() + a.power()); }
60 for (auto a : m) { hash += a.first + a.second; }
61 return hash;
62}
63
64TEST(NewF4, hashtable)
65{
66 newf4::MonomialHashTable hashtab(5);
67 // std::vector<int32_t> mdata{5, 1, 2, 2, 5};
68 // newf4::Monomial m(mdata);
69 // newf4::MonomialIndex m1 = hashtab.find(m, 7342643);
70 // newf4::MonomialIndex m2 = hashtab.find(m, 7342643);
71 // std::cout << "m1 = " << m1 << std::endl;
72 // EXPECT_EQ(m1, m2);
73
75 for (int i=0; i<100000; ++i)
76 {
77 newf4::MonomialView m({5, 1, 2, i, 3}, B);
78 /*newf4::MonomialIndex m1 = */ hashtab.find(m, hashFunction(m));
79 }
80 for (int i=0; i<10000; ++i)
81 {
82 newf4::MonomialView m({5, 1, 2, i, 3}, B);
83 /*newf4::MonomialIndex m1 = */ hashtab.find(m, hashFunction(m));
84 }
85 std::cout << std::endl;
86 hashtab.dump();
87
88}
89
90TEST(NewF4, matrixstream)
91{
92 const PolynomialRing* R = simplePolynomialRing(1235952427, {"x", "y", "z"});
93 const std::string polys = R"(1*x^1+2*y^1+2*z^1+1235952426
94y^1*z^1+494380972*z^2+370785728*y^1+247190485*z^1
95y^2+988761941*z^2+741571456*y^1+494380971*z^1
961*z^3+924021576*z^2+700373042*y^1+653289140*z^1
97)";
98
99
100 const Ring *K = R->getCoefficients();
101 auto VA = new VectorArithmetic(K);
102
103 BasicPolyList B = parseBasicPolyListFromString(polys, {"x", "y", "z"});
104 newf4::MonomialHashTable monHashTable;
105 newf4::PolynomialList L(*VA, monHashTable);
106 newf4::PolynomialListStreamCollector S(1235952427, 3, 1, L);
107 toStream(B, S);
108 std::cout << "Number of monomials: " << monHashTable.size() << std::endl;
109 monHashTable.dump();
110}
111
112TEST(NewF4MonomialLookupTable, mask_creation)
113{
114 MemoryBlock B;
115 for (int i=1; i<10; ++i)
116 {
117 newf4::MonomialView m({5, 0, 2, i, 3}, B);
119 std::cout << std::bitset<64>(mask) << std::endl;
120 }
121
122 newf4::MonomialView ab({5,0,1,1,1}, B);
123 newf4::MonomialView ac({5,0,1,2,1}, B);
124 newf4::MonomialView ab2({5,0,1,1,2}, B);
126 auto ab2Mask = newf4::MonomialLookupTable::createMask(ab2);
128 EXPECT_FALSE(newf4::MonomialLookupTable::maskDivides(abMask,acMask));
129 EXPECT_TRUE(newf4::MonomialLookupTable::maskDivides(abMask,ab2Mask));
131 std::cout << std::endl;
132}
133
134TEST(NewF4MonomialLookupTable, monomialDivides)
135{
136 MemoryBlock B;
137 newf4::MonomialView ab({5,0,1,1,1}, B);
138 newf4::MonomialView ac({5,0,1,2,1}, B);
139 newf4::MonomialView ab2({5,0,1,1,2}, B);
140 newf4::MonomialView unit({1},B);
141 newf4::MonomialView ab2f({7,0,1,1,2,5,1},B);
142 newf4::MonomialView bd({5,1,1,3,1},B);
143 newf4::MonomialView abdf({9,0,1,1,1,3,1,5,1},B);
144 EXPECT_FALSE(newf4::MonomialView::monomialDivides(ab,ac));
145 EXPECT_TRUE(newf4::MonomialView::monomialDivides(ab,ab2));
146 EXPECT_TRUE(newf4::MonomialView::monomialDivides(unit,unit));
147 EXPECT_TRUE(newf4::MonomialView::monomialDivides(unit,ab2));
149 EXPECT_FALSE(newf4::MonomialView::monomialDivides(ab2,abdf));
150}
151
152TEST(NewF4MonomialLookupTable, monomialOperations)
153{
154 MemoryBlock B;
155 newf4::MonomialView ab({5,0,1,1,1}, B);
156 newf4::MonomialView ac({5,0,1,2,1}, B);
157 newf4::MonomialView abc({7,0,1,1,1,2,1}, B);
158 newf4::MonomialView a2bc({7,0,2,1,1,2,1}, B);
159 newf4::MonomialView b({3,1,1}, B);
163
164 std::cout << lcm.size() << std::endl;
165 for (auto i = lcm.begin(); i != lcm.end(); ++i)
166 {
167 std::cout << i.var() << " ";
168 std::cout << i.power() << " ";
169 }
170 std::cout << std::endl;
171
172 std::cout << product.size() << std::endl;
173 for (auto i = product.begin(); i != product.end(); ++i)
174 {
175 std::cout << i.var() << " ";
176 std::cout << i.power() << " ";
177 }
178 std::cout << std::endl;
179
180 std::cout << quotient.size() << std::endl;
181 for (auto i = quotient.begin(); i != quotient.end(); ++i)
182 {
183 std::cout << i.var() << " ";
184 std::cout << i.power() << " ";
185 }
186 std::cout << std::endl;
187
188 EXPECT_TRUE(newf4::MonomialView::monomialDivides(lcm,abc));
189 EXPECT_TRUE(newf4::MonomialView::monomialDivides(abc,lcm));
190 EXPECT_TRUE(newf4::MonomialView::monomialDivides(product,a2bc));
191 EXPECT_TRUE(newf4::MonomialView::monomialDivides(a2bc,product));
192 EXPECT_TRUE(newf4::MonomialView::monomialDivides(quotient,b));
193 EXPECT_TRUE(newf4::MonomialView::monomialDivides(b,quotient));
194}
195
196 // TODO: need a function to get variable names from a PolynomialRing, Monoid (as a std::vector...)
197// TODO: need readMatrix(PolynomialRing, PolyList) -> const Matrix*
198// TODO: need a polynomialListFromStream function.
199// TODO: maybe even a (String, MatrixStream) -> PolyList, PolynomialList.
200// TODO: need a sort function for monomials in a polynomial (put a Polynomial into order)
201// this is more complicated: needs to take (component, MonomialView)'s. And allow Schreyer orders too...
202
203// Stream functions:
204// Stream objects: these are collectors. They often have other arguments needed to construct their objects?
205// MatrixStream
206// BasicPolyListStream
207// PolynomialListStream
208// Outputting to a string
209
210// PolynomialListStream -- a stream class generator?
211// fromStream(Stream) -> PolynomialList, PolyList.
212// typeAtoB(typeA. makeB) -> B // typeA generates stream calls for A, and makeB is a stream collector
213// PolyListGeneratorStream // function whose argument is a MatrixStream
214// PolyListFromStream // class which implements the MatrixStream interface.
215// PolyListToStream...
216//
217// MatrixStreamGenerator: this takes a Collector, and Matrix and sends the Matrix to the Collector.
218// MatrixStreamCollector: this implements the functions in the MatrixStream interface: ideal_done(), ...
219//
220// BasicPolyListCollector(some details about the ring, variables?) -> BasicPolyListStream
221// BasicPolyListGenerator(BasicPolyList, Stream)
222// BasicPolyListParser : (std::vector<std::string> varnames, String) -> BasicPolyList
223// BasicPolyListParserGenerator : (std::vector<std::string> varnames, String, Stream)
224
225
226// const Matrix* M = readMatrix(R, polys);
227
228 // Now let's make these into object in gb-f4...
229
230
231 // Start with a string of polynomials.
232 // Read it in with the parsing functions.
233 // Use PolynomialListStream to get it to this code.
234
235 // What tests to make here?
236
237 // Write it out to a Matrix.
238 // Read it from a Matrix.
std::vector< BasicPoly > BasicPolyList
void toStream(const BasicPolyList &Fs, S &str)
BasicPolyList parseBasicPolyListFromString(std::string contents, const IdentifierHash &idenMap)
Parsers from text (string or file) into a BasicPolyList, including the Msolve input format.
newf4::MonomialHashFunction and the new-F4 monomial-to-index hash table.
newf4::MonomialLookupTable — divisibility-aware leading-term index for the new F4.
TEST(NewF4, hashstats)
Definition NewF4Test.cpp:49
newf4::HashInt hashFunction(const newf4::MonomialView &m)
Definition NewF4Test.cpp:56
Hash-table-keyed polynomial storage for the new F4.
Coefficient-ring-erased arithmetic dispatcher used by F4, GB, and resolution code.
Thin RAII wrapper around memtailor::Arena providing bump-pointer array allocation with optional mutex...
virtual const Ring * getCoefficients() const
Definition polyring.hpp:277
Abstract base for the engine's polynomial-ring hierarchy.
Definition polyring.hpp:96
xxx xxx xxx
Definition ring.hpp:102
Runtime dispatcher that hides the concrete coefficient ring behind a std::variant of ConcreteVectorAr...
auto find(const MonomialView &m, HashInt mhash) -> MonomialIndex
Essentially the previous case when monomial(n) = monomial 1.
void dump() const
stats and debugging information.
static MonomialMask createMask(const MonomialView &monView)
static MonomialView quotient(const MonomialView &left, const MonomialView &right, MemoryBlock &block)
auto begin() const -> MonomialIterator< true >
auto end() const -> MonomialIterator< true >
static MonomialView lcm(const MonomialView &left, const MonomialView &right, MemoryBlock &block)
static MonomialView product(const MonomialView &left, const MonomialView &right, MemoryBlock &block)
size_t size() const
static bool monomialDivides(const MonomialView &divisor, const MonomialView &divisee)
implements the stream functions for creating a PolynomialList from a stream
uint64_t HashInt
PolynomialRing — abstract polynomial-ring base, the engine's most-reused class.
const PolynomialRing * simplePolynomialRing(const Ring *kk, const std::vector< std::string > &names, MonomialOrdering *monorder)
One-line helpers for building degree monoids and polynomial rings inside gtest cases.