1 /* Frobby: Software for monomial ideal computations.
2    Copyright (C) 2009 Bjarke Hammersholt Roune (www.broune.com)
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see http://www.gnu.org/licenses/.
16 */
17 #include "stdinc.h"
18 #include "LibTest.h"
19 
20 #include "tests.h"
21 
22 #include <algorithm>
23 
TEST_SUITE(LibraryInterface)24 TEST_SUITE(LibraryInterface)
25 
26 Frobby::Ideal toLibIdeal(const BigIdeal& ideal) {
27   size_t varCount = ideal.getVarCount();
28 
29   Frobby::Ideal libIdeal(varCount);
30   for (size_t generator = 0; generator < ideal.getGeneratorCount(); ++generator) {
31     if (varCount == 0)
32       libIdeal.addExponent(0);
33     else
34       for (size_t var = 0; var < varCount; ++var)
35         libIdeal.addExponent(ideal[generator][var].get_mpz_t());
36   }
37 
38   return libIdeal;
39 }
40 
castLibArray(const vector<mpz_class> & vect)41 const mpz_t* castLibArray(const vector<mpz_class>& vect) {
42   // The following cast depends on knowing that mpz_class just
43   // consists of a single mpz_t, so (nearly) assert that that is true.
44   ASSERT(sizeof(mpz_class) == sizeof(mpz_t));
45   return reinterpret_cast<const mpz_t*>(&(vect[0]));
46 }
47 
LibIdealConsumer(const VarNames & names)48 LibIdealConsumer::LibIdealConsumer(const VarNames& names):
49   _hasAnyOutput(false),
50   _ideal(names) {
51 }
52 
idealBegin(size_t varCount)53 void LibIdealConsumer::idealBegin(size_t varCount) {
54   ASSERT(varCount == _ideal.getVarCount());
55 
56   _ideal.clear();
57   _hasAnyOutput = true;
58 }
59 
consume(mpz_ptr * exponentVector)60 void LibIdealConsumer::consume(mpz_ptr* exponentVector) {
61   _ideal.newLastTerm();
62   for (size_t var = 0; var < _ideal.getVarCount(); ++var)
63     _ideal.getLastTermRef()[var] = mpz_class(exponentVector[var]);
64 }
65 
getIdeal() const66 const BigIdeal& LibIdealConsumer::getIdeal() const {
67   _ideal.sortGenerators();
68   return _ideal;
69 }
70 
hasAnyOutput() const71 bool LibIdealConsumer::hasAnyOutput() const {
72   return _hasAnyOutput;
73 }
74 
75 
LibIdealsConsumer(const VarNames & names)76 LibIdealsConsumer::LibIdealsConsumer(const VarNames& names):
77   _names(names) {
78 }
79 
idealBegin(size_t varCount)80 void LibIdealsConsumer::idealBegin(size_t varCount) {
81   ASSERT(_names.getVarCount() == varCount);
82   _ideals.push_back(BigIdeal(_names));
83 }
84 
consume(mpz_ptr * exponentVector)85 void LibIdealsConsumer::consume(mpz_ptr* exponentVector) {
86   _ideals.back().newLastTerm();
87   for (size_t var = 0; var < _names.getVarCount(); ++var)
88     _ideals.back().getLastTermRef()[var] = mpz_class(exponentVector[var]);
89 }
90 
getIdeals() const91 const vector<BigIdeal>& LibIdealsConsumer::getIdeals() const {
92   for (size_t i = 0; i < _ideals.size(); ++i)
93     _ideals[i].sortGenerators();
94   sort(_ideals.begin(), _ideals.end());
95   return _ideals;
96 }
97 
LibPolynomialConsumer(const VarNames & names)98 LibPolynomialConsumer::LibPolynomialConsumer(const VarNames& names):
99   _polynomial(names) {
100 }
101 
polynomialBegin(size_t varCount)102 void LibPolynomialConsumer::polynomialBegin(size_t varCount) {
103   ASSERT(varCount == _polynomial.getVarCount());
104 }
105 
consume(const mpz_t coef,mpz_ptr * exponentVector)106 void LibPolynomialConsumer::consume(const mpz_t coef, mpz_ptr* exponentVector) {
107   _polynomial.newLastTerm();
108   _polynomial.getLastCoef() = mpz_class(coef);
109   for (size_t var = 0; var < _polynomial.getVarCount(); ++var)
110     _polynomial.getLastTerm()[var] = mpz_class(exponentVector[var]);
111 }
112 
getPolynomial() const113 const BigPolynomial& LibPolynomialConsumer::getPolynomial() const {
114   _polynomial.sortTermsReverseLex();
115   return _polynomial;
116 }
117