BigPolynomial.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 "BigPolynomial.h"
#include "Term.h"
#include "TermTranslator.h"
#include "VarSorter.h"
#include <algorithm>
#include <sstream>
BigPolynomial::BigPolynomial() {
}
BigPolynomial::BigPolynomial(const VarNames& names):
_names(names) {
}
size_t BigPolynomial::getTermCount() const {
return _coefTerms.size();
}
size_t BigPolynomial::getVarCount() const {
return _names.getVarCount();
}
const VarNames& BigPolynomial::getNames() const {
return _names;
}
void BigPolynomial::sortTermsReverseLex() {
sort(_coefTerms.begin(), _coefTerms.end(), compareCoefTermsReverseLex);
}
void BigPolynomial::sortVariables() {
VarSorter sorter(_names);
sorter.getOrderedNames(_names);
for (size_t i = 0; i < _coefTerms.size(); ++i)
sorter.permute(_coefTerms[i].term);
}
void BigPolynomial::clear() {
_coefTerms.clear();
}
void BigPolynomial::clearAndSetNames(const VarNames& names) {
clear();
_names = names;
}
const mpz_class& BigPolynomial::getCoef(size_t index) const {
ASSERT(index < getTermCount());
return _coefTerms[index].coef;
}
const vector<mpz_class>& BigPolynomial::getTerm(size_t index) const {
ASSERT(index < getTermCount());
return _coefTerms[index].term;
}
void BigPolynomial::newLastTerm() {
_coefTerms.resize(_coefTerms.size() + 1);
_coefTerms.back().term.resize(getVarCount());
}
vector<mpz_class>& BigPolynomial::getLastTerm() {
ASSERT(getTermCount() > 0);
return _coefTerms.back().term;
}
mpz_class& BigPolynomial::getLastCoef() {
ASSERT(getTermCount() > 0);
return _coefTerms.back().coef;
}
void BigPolynomial::renameVars(const VarNames& names) {
ASSERT(names.getVarCount() == _names.getVarCount());
_names = names;
}
void BigPolynomial::add(const mpz_class& coef,
const vector<mpz_class> term) {
ASSERT(term.size() == getVarCount());
_coefTerms.resize(_coefTerms.size() + 1);
_coefTerms.back().coef = coef;
_coefTerms.back().term = term;
}
void BigPolynomial::add(const mpz_class& coef,
const Term& term,
const TermTranslator& translator) {
ASSERT(term.getVarCount() == getVarCount());
ASSERT(translator.getVarCount() == getVarCount());
_coefTerms.resize(_coefTerms.size() + 1);
_coefTerms.back().coef = coef;
vector<mpz_class>& bigTerm = _coefTerms.back().term;
bigTerm.reserve(term.getVarCount());
for (size_t var = 0; var < term.getVarCount(); ++var)
bigTerm.push_back(translator.getExponent(var, term));
}
bool BigPolynomial::operator==(const BigPolynomial& poly) const {
return _names == poly._names && _coefTerms == poly._coefTerms;
}
bool BigPolynomial::BigCoefTerm::operator==(const BigCoefTerm& coefTerm) const {
return coef == coefTerm.coef && term == coefTerm.term;
}
void BigPolynomial::print(FILE* file) const {
ostringstream out;
out << *this;
fputs(out.str().c_str(), file);
}
void BigPolynomial::print(ostream& out) const {
out << "/---- BigPolynomial of " << _coefTerms.size() << " terms:\n";
for (vector<BigCoefTerm>::const_iterator it = _coefTerms.begin();
it != _coefTerms.end(); ++it) {
out << ' ' << it->coef << " ";
for (vector<mpz_class>::const_iterator entry = it->term.begin();
entry != it->term.end(); ++entry)
out << ' ' << *entry;
out << '\n';
}
out << "----/ End of list.\n";
}
bool BigPolynomial::compareCoefTermsReverseLex(const BigCoefTerm& a,
const BigCoefTerm& b) {
for (size_t var = 0; var < a.term.size(); ++var)
if (a.term[var] != b.term[var])
return a.term[var] > b.term[var];
return a.coef < b.coef;
}
ostream& operator<<(ostream& out, const BigPolynomial& poly) {
poly.print(out);
return out;
}
BigPolynomial.h
operator<<
ostream & operator<<(ostream &out, const BigPolynomial &ideal)
Definition BigPolynomial.cpp:159
TermTranslator.h
Term.h
VarSorter.h
BigPolynomial
Definition BigPolynomial.h:27
BigPolynomial::getLastCoef
mpz_class & getLastCoef()
Definition BigPolynomial.cpp:89
BigPolynomial::getTerm
const vector< mpz_class > & getTerm(size_t index) const
Definition BigPolynomial.cpp:72
BigPolynomial::getTermCount
size_t getTermCount() const
Definition BigPolynomial.cpp:34
BigPolynomial::_coefTerms
vector< BigCoefTerm > _coefTerms
Definition BigPolynomial.h:75
BigPolynomial::add
void add(const mpz_class &coef, const vector< mpz_class > term)
Definition BigPolynomial.cpp:100
BigPolynomial::_names
VarNames _names
Definition BigPolynomial.h:74
BigPolynomial::getVarCount
size_t getVarCount() const
Definition BigPolynomial.cpp:38
BigPolynomial::newLastTerm
void newLastTerm()
Definition BigPolynomial.cpp:78
BigPolynomial::sortVariables
void sortVariables()
Definition BigPolynomial.cpp:50
BigPolynomial::BigPolynomial
BigPolynomial()
Definition BigPolynomial.cpp:27
BigPolynomial::clearAndSetNames
void clearAndSetNames(const VarNames &names)
Definition BigPolynomial.cpp:61
BigPolynomial::getCoef
const mpz_class & getCoef(size_t index) const
Definition BigPolynomial.cpp:66
BigPolynomial::renameVars
void renameVars(const VarNames &names)
Definition BigPolynomial.cpp:95
BigPolynomial::clear
void clear()
Definition BigPolynomial.cpp:57
BigPolynomial::print
void print(FILE *file) const
Definition BigPolynomial.cpp:132
BigPolynomial::getLastTerm
vector< mpz_class > & getLastTerm()
Definition BigPolynomial.cpp:83
BigPolynomial::getNames
const VarNames & getNames() const
Definition BigPolynomial.cpp:42
BigPolynomial::operator==
bool operator==(const BigPolynomial &poly) const
Definition BigPolynomial.cpp:124
BigPolynomial::sortTermsReverseLex
void sortTermsReverseLex()
Definition BigPolynomial.cpp:46
BigPolynomial::compareCoefTermsReverseLex
static bool compareCoefTermsReverseLex(const BigCoefTerm &a, const BigCoefTerm &b)
Definition BigPolynomial.cpp:151
TermTranslator
TermTranslator handles translation between terms whose exponents are infinite precision integers and ...
Definition TermTranslator.h:41
TermTranslator::getExponent
const mpz_class & getExponent(size_t variable, Exponent exponent) const
This method translates from IDs to arbitrary precision integers.
Definition TermTranslator.cpp:392
TermTranslator::getVarCount
size_t getVarCount() const
Definition TermTranslator.cpp:447
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
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
VarNames::getVarCount
size_t getVarCount() const
Returns the current number of variables.
Definition VarNames.h:113
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
BigPolynomial::BigCoefTerm
Definition BigPolynomial.h:62
BigPolynomial::BigCoefTerm::term
vector< mpz_class > term
Definition BigPolynomial.h:64
BigPolynomial::BigCoefTerm::operator==
bool operator==(const BigCoefTerm &coefTerm) const
Definition BigPolynomial.cpp:128
BigPolynomial::BigCoefTerm::coef
mpz_class coef
Definition BigPolynomial.h:63
VarSorter
Definition VarSorter.h:27
VarSorter::getOrderedNames
void getOrderedNames(VarNames &names)
Definition VarSorter.cpp:50
VarSorter::permute
void permute(vector< mpz_class > &term)
Definition VarSorter.cpp:56