Macaulay2 Engine
Loading...
Searching...
No Matches
MatrixIOTest.cpp
Go to the documentation of this file.
1
29
30#include <iostream>
31#include <memory>
32#include <gtest/gtest.h>
33
35#include "matrix.hpp"
36#include "BasicPolyList.hpp"
40#include "VectorArithmetic.hpp"
41// These are more benchmark examples, and the files to be read are quite large
42// So we can't run these by default.
43
44#define EXAMPLE_DIR "/Users/mike/src/git-from-others/msolve/MES-examples/"
45//#define EXAMPLE_DIR "/Users/moorewf/Dropbox/NCEngine/GB examples/"
46
47// MES: just in process of adding this.
48// TODO: add in function f.ToString(varnames); // f is a BasicPoly
49
50TEST(MatrixIO, readPolynomial)
51{
52 std::string polyStr { "13*x^2*y-x*y-2" };
53 std::vector<std::string> varnames = {"x", "y", "z"};
54 auto result = parseBasicPoly(polyStr, varnames);
55 EXPECT_TRUE(result.termCount() == 3);
56 std::cout << "poly: ";
57 std::cout << result.toString(varnames);
58 std::cout << '\n';
59 EXPECT_TRUE(polyStr == result.toString(varnames));
60
61 polyStr = "-x+y^2-13*x*y*z+1";
62 result = parseBasicPoly(polyStr, varnames);
63 std::cout << "poly: " << result.toString(varnames) << '\n';
64 EXPECT_TRUE(result.toString(varnames) == polyStr);
65 std::cout << "bytes used: " << result.bytesUsed() << '\n';
66 // #bytes:
67
68 polyStr = "2*x+y^2-13*x*y*z-1";
69 result = parseBasicPoly(polyStr, varnames);
70 std::cout << "poly: " << result.toString(varnames) << '\n';
71 EXPECT_TRUE(result.toString(varnames) == polyStr);
72}
73
74TEST(MatrixIO, readPolynomialErrors)
75{
76 std::vector<std::string> varnames = {"x", "y", "z"};
77 EXPECT_THROW(parseBasicPoly("3*w-2", varnames), parsing_error);
78 try {
79 parseBasicPoly("3*w-2", varnames);
80 } catch (parsing_error& e) {
81 std::cout << "expected parse error: " << e.what() << std::endl;
82 }
83
84 EXPECT_THROW(parseBasicPoly("3*-2", varnames), parsing_error);
85 try {
86 parseBasicPoly("3*-2", varnames);
87 } catch (parsing_error& e) {
88 std::cout << "expected parse error: " << e.what() << std::endl;
89 }
90
91 EXPECT_THROW(parseBasicPoly("3*x^*y-2", varnames), parsing_error);
92 try {
93 parseBasicPoly("3*x^*y-2", varnames);
94 } catch (parsing_error& e) {
95 std::cout << "expected parse error: " << e.what() << std::endl;
96 }
97}
98
99
100TEST(MatrixIO, readMsolve)
101{
102 std::string filename { EXAMPLE_DIR"eg2-gb.ms" };
103 std::string contents = R"(#Reduced Groebner basis data
104#---
105#field characteristic: 1235952427
106#variable order: x, y, z
107#monomial order: graded reverse lexicographical
108#length of basis: 4 elements sorted by increasing leading monomials
109#---
110[1*x^1+2*y^1+2*z^1+1235952426,
1111*y^1*z^1+494380972*z^2+370785728*y^1+247190485*z^1,
1121*y^2+988761941*z^2+741571456*y^1+494380971*z^1,
1131*z^3+924021576*z^2+700373042*y^1+653289140*z^1]:
114)";
115
116 auto result = parseMsolveFromString(contents);
117 std::cout << "bytes used for poly list: " << bytesUsed(result) << '\n';
118 EXPECT_TRUE(result.size() == 4);
119
120 const PolynomialRing* R = simplePolynomialRing(1235952427, {"x", "y", "z"});
121 const Matrix* M = toMatrix(R->make_FreeModule(1), result);
122
123 EXPECT_TRUE(M->n_rows() == 1);
124 EXPECT_TRUE(M->n_cols() == 4);
125}
126
127#if 0
128TEST(MatrixIO, readMsolveBig1)
129{
130 std::string filename { EXAMPLE_DIR"6pts-a-gb.ms" };
131 auto B = parseMsolveFile(filename);
132 EXPECT_TRUE(B.size() == 1019);
133 std::cout << "bytes used for poly list: " << bytesUsed(B) << '\n';
134
135 // TODO: parseMsolveFile should also return: modulus, varnames, monorder.
136 std::vector<std::string> varnames {"t12", "t13", "t14", "t15", "t16",
137 "t23", "t24", "t25", "t26", "t34", "t35", "t36", "t45", "t46", "t56", "z1", "z2"};
138 const PolynomialRing* R = simplePolynomialRing(65537, varnames);
139
140 const Ring *K = R->getCoefficients();
141 auto VA = new VectorArithmetic(K);
142 newf4::MonomialHashTable monHashTable;
143 newf4::PolynomialList L(*VA, monHashTable);
144 newf4::PolynomialListStreamCollector S(65537, 17, 1, L);
145 toStream(B, S);
146 std::cout << "Number of monomials: " << monHashTable.size() << std::endl;
147 monHashTable.dump();
148
149 // const Matrix* M = toMatrix(R->make_FreeModule(1), result);
150 // EXPECT_TRUE(M->n_rows() == 1);
151 // EXPECT_TRUE(M->n_cols() == 1019);
152}
153
154TEST(MatrixIO, readMsolveBig2)
155{
156 std::string filename { EXAMPLE_DIR"6pts-b-gb.ms" };
157 auto B = parseMsolveFile(filename);
158 EXPECT_TRUE(B.size() == 1391);
159 std::cout << "bytes used: " << bytesUsed(B) << '\n';
160
161 std::vector<std::string> varnames {"t12", "t13", "t14", "t15", "t16",
162 "t23", "t24", "t25", "t26", "t34", "t35", "t36", "t45", "t46", "t56", "z1", "z2"};
163 const PolynomialRing* R = simplePolynomialRing(65537, varnames);
164
165 const Ring *K = R->getCoefficients();
166 auto VA = new VectorArithmetic(K);
167 newf4::MonomialHashTable monHashTable;
168 newf4::PolynomialList L(*VA, monHashTable);
169 newf4::PolynomialListStreamCollector S(65537, 17, 1, L);
170 toStream(B, S);
171 std::cout << "Number of monomials: " << monHashTable.size() << std::endl;
172 monHashTable.dump();
173
174
175 // const Matrix* M = toMatrix(R->make_FreeModule(1), B);
176 // EXPECT_TRUE(M->n_rows() == 1);
177 // EXPECT_TRUE(M->n_cols() == 1391);
178}
179
180TEST(MatrixIO, readMsolveBig3)
181{
182 std::string filename { EXAMPLE_DIR"eg2-gb.ms" };
183 auto B = parseMsolveFile(filename);
184 EXPECT_TRUE(B.size() == 4761);
185 std::cout << "bytes used: " << bytesUsed(B) << '\n';
186
187 // TODO: parseMsolveFile should also return: modulus, varnames, monorder.
188 std::vector<std::string> varnames {
189 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
190 "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
191 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
192 "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V"
193 };
194 const PolynomialRing* R = simplePolynomialRing(101, varnames);
195
196 // Matrix version
197 //const Matrix* M = toMatrix(R->make_FreeModule(1), B);
198 //newf4::GBF4Interface gbInterface(R,
199 // M,
200 // {},
201 // newf4::Strategy::Normal);
202
203 // BasicPolyList version
204 newf4::GBF4Interface gbInterface(R,
205 R->make_FreeModule(1),
206 B,
207 {},
209
210 gbInterface.computation().dumpBasisMonomials();
211
212 //const Ring *K = R->getCoefficients();
213 //auto VA = new VectorArithmetic(K);
214 //newf4::MonomialHashTable monHashTable;
215 //newf4::PolynomialList L(*VA, monHashTable);
216 //newf4::PolynomialListStreamCollector S(101, 48, 1, L);
217 //toStream(B, S);
218 //std::cout << "Number of monomials: " << monHashTable.size() << std::endl;
219 //monHashTable.dump();
220
221 // const Matrix* M = toMatrix(R->make_FreeModule(1), result);
222 // EXPECT_TRUE(M->n_rows() == 1);
223 // EXPECT_TRUE(M->n_cols() == 1019);
224}
225
226#endif
227
228TEST(MatrixIO, readPolys)
229{
230 std::string contents = R"(1*x^1+2*y^1+2*z^1+1235952426
2311*y^1*z^1+494380972*z^2+370785728*y^1+247190485*z^1
2321*y^2+988761941*z^2+741571456*y^1+494380971*z^1
2331*z^3+924021576*z^2+700373042*y^1+653289140*z^1
234)";
235
236 std::vector<std::string> varnames = {"x", "y", "z"};
237 auto result = parseBasicPolyListFromString(contents, varnames);
238 EXPECT_TRUE(result.size() == 4);
239
240 const PolynomialRing* R = simplePolynomialRing(1235952427, varnames);
241 const Matrix* M = toMatrix(R->make_FreeModule(1), result);
242
243 EXPECT_TRUE(M->n_rows() == 1);
244 EXPECT_TRUE(M->n_cols() == 4);
245
246 buffer o;
247 M->text_out(o);
248 std::cout << o.str() << std::endl;
249}
250
251#if 0
252restart
253dot = (e) -> (sum for i from 0 to 19 list ((e#i * vals#i) % 2^64)) % 2^20
254 dot = (e) -> ((sum for i from 0 to 19 list ((e#i * vals#i) % 2^64)) >> 25) % 2^20
255vals = {12550986463692465404, 3911555212215091238, 15090669942851505316, 16174113364685515424, 18172793978567602378, 4970727551569665824, 15244287395755336378, 3641586221293608170, 5697307520845005385, 17982501052917221133, 4205210476184990958, 3995014217224167515, 10391875845945764299, 17483720614571824287, 1115562083531405255, 7842315096810324507, 673864007402015535, 15878473700446701422, 15632675738063166334, 17700395182034373329}
256 R = ZZ/101[t_0..t_19]
257 exps = (flatten entries basis(0,6,R))/exponents/first;
258 allhashes = for e in exps list dot e;
259(#allhashes, #unique allhashes)
260allhashes
261max values tally allhashes
262#endif
BasicPoly parseBasicPoly(std::string poly, std::vector< std::string > varnames)
long bytesUsed(const BasicPolyList &F)
const Matrix * toMatrix(const FreeModule *target, const BasicPolyList &Fs)
void toStream(const BasicPolyList &Fs, S &str)
Ring-agnostic polynomial-list transport type plus its streaming collector and emitter.
BasicPolyList parseMsolveFile(std::string filename)
BasicPolyList parseMsolveFromString(std::string contents)
BasicPolyList parseBasicPolyListFromString(std::string contents, const IdentifierHash &idenMap)
Parsers from text (string or file) into a BasicPolyList, including the Msolve input format.
ExponentVector< int, true > exponents
Legacy-to-new-F4 adapter exposing GBF4Computation through the engine's GBComputation API.
#define EXAMPLE_DIR
TEST(MatrixIO, readPolynomial)
Hash-table-keyed polynomial storage for the new F4.
Coefficient-ring-erased arithmetic dispatcher used by F4, GB, and resolution code.
int n_cols() const
Definition matrix.hpp:147
int n_rows() const
Definition matrix.hpp:146
void text_out(buffer &o) const
Definition matrix.cpp:1316
virtual const Ring * getCoefficients() const
Definition polyring.hpp:277
Abstract base for the engine's polynomial-ring hierarchy.
Definition polyring.hpp:96
virtual FreeModule * make_FreeModule() const
Definition ring.cpp:53
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
void dump() const
stats and debugging information.
auto size() const -> size_t
The actual number of monomials in the table.
implements the stream functions for creating a PolynomialList from a stream
#define Matrix
Definition factory.cpp:14
VALGRIND_MAKE_MEM_DEFINED & result(result)
Matrix — the engine's immutable homomorphism F -> G between free modules.
#define max(a, b)
Definition polyroots.cpp:52
mpz_srcptr ZZ
Definition ringelem.hpp:44
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.