Polynomial.cpp
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include "stdinc.h"
#include "Polynomial.h"
#include "TermPredicate.h"
#include <algorithm>
#include <sstream>
Polynomial::Polynomial():
_varCount(0) {
}
Polynomial::Polynomial(size_t varCount):
_varCount(varCount) {
}
size_t Polynomial::getVarCount() const {
return _varCount;
}
size_t Polynomial::getTermCount() const {
return _terms.size();
}
const mpz_class& Polynomial::getCoef(size_t index) const {
ASSERT(index < getTermCount());
return _terms[index].coef;
}
const Term& Polynomial::getTerm(size_t index) const {
ASSERT(index < getTermCount());
return _terms[index].term;
}
void Polynomial::clearAndSetVarCount(size_t varCount) {
clear();
_varCount = varCount;
}
void Polynomial::add(const mpz_class& coef, const Term& term) {
ASSERT(_varCount == term.getVarCount());
if (coef == 0)
return;
_terms.resize(_terms.size() + 1);
try {
_terms.back().coef = coef;
_terms.back().term = term;
} catch (const std::bad_alloc&) {
_terms.pop_back();
throw;
}
}
void Polynomial::sortTermsReverseLex(bool collect) {
if (_terms.empty())
return;
sort(_terms.begin(), _terms.end());
if (!collect)
return;
// TODO: improve collection. E.g. have it be its own method.
size_t last = 0;
for (size_t i = 1; i < _terms.size(); ++i) {
if (_terms[last].term == _terms[i].term)
_terms[last].coef += _terms[i].coef;
else {
if (_terms[last].coef == 0)
_terms[last] = _terms[i];
else {
++last;
if (last != i)
_terms[last] = _terms[i];
}
}
}
ASSERT(last < _terms.size());
_terms.erase(_terms.begin() + last + 1, _terms.end());
}
bool Polynomial::CoefTerm::operator<(const CoefTerm& coefTerm) const {
ASSERT(term.getVarCount() == coefTerm.term.getVarCount());
return reverseLexCompare(term, coefTerm.term, term.getVarCount()) < 0;
}
void Polynomial::clear() {
_terms.clear();
}
void Polynomial::print(FILE* out) {
ostringstream str;
print(str);
fputs(str.str().c_str(), out);
}
void Polynomial::print(ostream& out) {
out << "//------- Polynomial:\n";
for (size_t i = 0; i < _terms.size(); ++i)
out << getCoef(i) << "*" << getTerm(i) << '\n';
out << "----------\\\\\n";
}
Polynomial.h
reverseLexCompare
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 .
Definition TermPredicate.cpp:71
TermPredicate.h
Polynomial::clearAndSetVarCount
void clearAndSetVarCount(size_t varCount)
Definition Polynomial.cpp:50
Polynomial::getTermCount
size_t getTermCount() const
Definition Polynomial.cpp:36
Polynomial::print
void print(FILE *out)
Definition Polynomial.cpp:110
Polynomial::getVarCount
size_t getVarCount() const
Definition Polynomial.cpp:32
Polynomial::getTerm
const Term & getTerm(size_t index) const
Definition Polynomial.cpp:45
Polynomial::_terms
vector< CoefTerm > _terms
Definition Polynomial.h:57
Polynomial::Polynomial
Polynomial()
Definition Polynomial.cpp:24
Polynomial::sortTermsReverseLex
void sortTermsReverseLex(bool collect=true)
Definition Polynomial.cpp:71
Polynomial::clear
void clear()
Definition Polynomial.cpp:106
Polynomial::_varCount
size_t _varCount
Definition Polynomial.h:58
Polynomial::add
void add(const mpz_class &coef, const Term &term)
Definition Polynomial.cpp:55
Polynomial::getCoef
const mpz_class & getCoef(size_t index) const
Definition Polynomial.cpp:40
Term
Term represents a product of variables which does not include a coefficient.
Definition Term.h:49
Term::getVarCount
size_t getVarCount() const
Definition Term.h:85
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...
ASSERT
#define ASSERT(X)
Definition stdinc.h:86
Polynomial::CoefTerm
Definition Polynomial.h:50
Polynomial::CoefTerm::operator<
bool operator<(const CoefTerm &coefTerm) const
Definition Polynomial.cpp:101
Polynomial::CoefTerm::term
Term term
Definition Polynomial.h:54