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
29 _varCount(varCount) {
30}
31
33 return _varCount;
34}
35
37 return _terms.size();
38}
39
40const mpz_class& Polynomial::getCoef(size_t index) const {
41 ASSERT(index < getTermCount());
42 return _terms[index].coef;
43}
44
45const Term& Polynomial::getTerm(size_t index) const {
46 ASSERT(index < getTermCount());
47 return _terms[index].term;
48}
49
54
55void 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
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
102 ASSERT(term.getVarCount() == coefTerm.term.getVarCount());
103 return reverseLexCompare(term, coefTerm.term, term.getVarCount()) < 0;
104}
105
107 _terms.clear();
108}
109
111 ostringstream str;
112 print(str);
113 fputs(str.str().c_str(), out);
114}
115
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}
void nameFactoryRegister(NameFactory< AbstractProduct > &factory)
Registers the string returned by ConcreteProduct::getStaticName() to a function that default-construc...
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)
size_t getTermCount() const
void print(FILE *out)
size_t getVarCount() const
const Term & getTerm(size_t index) const
vector< CoefTerm > _terms
Definition Polynomial.h:57
void sortTermsReverseLex(bool collect=true)
size_t _varCount
Definition Polynomial.h:58
void add(const mpz_class &coef, const Term &term)
const mpz_class & getCoef(size_t index) const
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