Frobby  0.9.5
Polynomial.cpp
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2007 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 "Polynomial.h"
19 
20 #include "TermPredicate.h"
21 #include <algorithm>
22 #include <sstream>
23 
25  _varCount(0) {
26 }
27 
28 Polynomial::Polynomial(size_t varCount):
29  _varCount(varCount) {
30 }
31 
32 size_t Polynomial::getVarCount() const {
33  return _varCount;
34 }
35 
36 size_t Polynomial::getTermCount() const {
37  return _terms.size();
38 }
39 
40 const mpz_class& Polynomial::getCoef(size_t index) const {
41  ASSERT(index < getTermCount());
42  return _terms[index].coef;
43 }
44 
45 const Term& Polynomial::getTerm(size_t index) const {
46  ASSERT(index < getTermCount());
47  return _terms[index].term;
48 }
49 
50 void Polynomial::clearAndSetVarCount(size_t varCount) {
51  clear();
52  _varCount = varCount;
53 }
54 
55 void Polynomial::add(const mpz_class& coef, const Term& term) {
56  ASSERT(_varCount == term.getVarCount());
57 
58  if (coef == 0)
59  return;
60 
61  _terms.resize(_terms.size() + 1);
62  try {
63  _terms.back().coef = coef;
64  _terms.back().term = term;
65  } catch (const std::bad_alloc&) {
66  _terms.pop_back();
67  throw;
68  }
69 }
70 
71 void Polynomial::sortTermsReverseLex(bool collect) {
72  if (_terms.empty())
73  return;
74 
75  sort(_terms.begin(), _terms.end());
76 
77  if (!collect)
78  return;
79 
80  // TODO: improve collection. E.g. have it be its own method.
81 
82  size_t last = 0;
83  for (size_t i = 1; i < _terms.size(); ++i) {
84  if (_terms[last].term == _terms[i].term)
85  _terms[last].coef += _terms[i].coef;
86  else {
87  if (_terms[last].coef == 0)
88  _terms[last] = _terms[i];
89  else {
90  ++last;
91  if (last != i)
92  _terms[last] = _terms[i];
93  }
94  }
95  }
96 
97  ASSERT(last < _terms.size());
98  _terms.erase(_terms.begin() + last + 1, _terms.end());
99 }
100 
101 bool Polynomial::CoefTerm::operator<(const CoefTerm& coefTerm) const {
102  ASSERT(term.getVarCount() == coefTerm.term.getVarCount());
103  return reverseLexCompare(term, coefTerm.term, term.getVarCount()) < 0;
104 }
105 
107  _terms.clear();
108 }
109 
110 void Polynomial::print(FILE* out) {
111  ostringstream str;
112  print(str);
113  fputs(str.str().c_str(), out);
114 }
115 
116 void Polynomial::print(ostream& out) {
117  out << "//------- Polynomial:\n";
118  for (size_t i = 0; i < _terms.size(); ++i)
119  out << getCoef(i) << "*" << getTerm(i) << '\n';
120  out << "----------\\\\\n";
121 }
int reverseLexCompare(const Exponent *a, const Exponent *b, size_t varCount)
Indicates how a relates to b according to the reverse lexicographic term order where .
void clearAndSetVarCount(size_t varCount)
Definition: Polynomial.cpp:50
size_t getTermCount() const
Definition: Polynomial.cpp:36
void print(FILE *out)
Definition: Polynomial.cpp:110
size_t getVarCount() const
Definition: Polynomial.cpp:32
const Term & getTerm(size_t index) const
Definition: Polynomial.cpp:45
vector< CoefTerm > _terms
Definition: Polynomial.h:57
void sortTermsReverseLex(bool collect=true)
Definition: Polynomial.cpp:71
void clear()
Definition: Polynomial.cpp:106
size_t _varCount
Definition: Polynomial.h:58
void add(const mpz_class &coef, const Term &term)
Definition: Polynomial.cpp:55
const mpz_class & getCoef(size_t index) const
Definition: Polynomial.cpp:40
Term represents a product of variables which does not include a coefficient.
Definition: Term.h:49
size_t getVarCount() const
Definition: Term.h:85
#define ASSERT(X)
Definition: stdinc.h:86
bool operator<(const CoefTerm &coefTerm) const
Definition: Polynomial.cpp:101