AnalyzeAction.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 "AnalyzeAction.h"
#include "BigIdeal.h"
#include "IOFacade.h"
#include "IdealFacade.h"
#include "Scanner.h"
#include "IOHandler.h"
#include "BigTermConsumer.h"
#include "DataType.h"
#include <algorithm>
class AnalyzeConsumer : public BigTermConsumer {
public:
AnalyzeConsumer():
_generatorCount(0) {
}
virtual void consumeRing(const VarNames& names) {
_names = names;
_lcm.clear();
_lcm.resize(_names.getVarCount());
}
virtual void beginConsuming() {
}
using BigTermConsumer::consume;
virtual void consume(const Term& term, const TermTranslator& translator) {
BigTermConsumer::consume(term, translator);
}
virtual void consume(const vector<mpz_class>& term) {
ASSERT(term.size() == _names.getVarCount());
++_generatorCount;
for (size_t var = 0; var < term.size(); ++var)
if (_lcm[var] < term[var])
_lcm[var] = term[var];
}
virtual void doneConsuming() {
}
size_t getGeneratorCount() const {
return _generatorCount;
}
const VarNames& getNames() const {
return _names;
}
const vector<mpz_class>& getLcm() const {
return _lcm;
}
const mpz_class& getMaximumExponent() const {
ASSERT(_lcm.size() > 0);
return *max_element(_lcm.begin(), _lcm.end());
}
private:
VarNames _names;
size_t _generatorCount;
vector<mpz_class> _lcm;
};
AnalyzeAction::AnalyzeAction():
Action
(staticGetName(),
"Display information about the input ideal.",
"Display information about input ideal. This is useful for getting a quick\n"
"impression of how the ideal looks like, and it can be used in scripts\n"
"that need information about the ideal.",
false),
_io(DataType::getMonomialIdealType(), DataType::getMonomialIdealType()),
_summaryLevel
("summaryLevel",
"If non-zero, then print a summary of the ideal to the error output\n"
"stream. A higher summary level results in more expensive analysis in\n"
"order to provide more information. Currently levels 0, 1 and 2 are\n"
"available.",
1),
_printLcm
("lcm",
"Print the least common multiple of the generators.",
false),
_printVarCount
("varCount",
"Print the number of variables.",
false),
_printGeneratorCount
("genCount",
"Print the number of generators.",
false),
_printMaximumExponent
("maxExp",
"Print the largest exponent that appears in the input file",
false),
_printMinimal
("minimal",
"Print 1 if the ideal has no non-minimal generators. Print 0 otherwise.",
false) {
}
void AnalyzeAction::obtainParameters(vector<Parameter*>& parameters) {
parameters.push_back(&_summaryLevel);
parameters.push_back(&_printLcm);
parameters.push_back(&_printVarCount);
parameters.push_back(&_printGeneratorCount);
parameters.push_back(&_printMaximumExponent);
parameters.push_back(&_printMinimal);
_io.obtainParameters(parameters);
Action::obtainParameters(parameters);
}
void AnalyzeAction::perform() {
Scanner in(_io.getInputFormat(), stdin);
_io.autoDetectInputFormat(in);
_io.validateFormats();
AnalyzeConsumer consumer;
// We only read the entire ideal into memory at once if we have to.
IOFacade ioFacade(_printActions);
if (!requiresWholeIdeal()) {
ioFacade.readIdeal(in, consumer);
in.expectEOF();
analyzeStreaming(consumer);
} else {
BigIdeal ideal;
ioFacade.readIdeal(in, ideal);
in.expectEOF();
consumer.consume(ideal);
analyzeStreaming(consumer);
analyzeIdeal(ideal);
}
}
bool AnalyzeAction::requiresWholeIdeal() const {
return _printMinimal || _summaryLevel > 1;
}
void AnalyzeAction::analyzeIdeal(BigIdeal& ideal) const {
IdealFacade idealFacade(_printActions);
if (_printMinimal) {
size_t generatorCount = ideal.getGeneratorCount();
idealFacade.sortAllAndMinimize(ideal);
if (generatorCount == ideal.getGeneratorCount())
fputs("1\n", stdout);
else
fputs("0\n", stdout);
}
if (_summaryLevel >= 2) {
idealFacade.printAnalysis(stdout, ideal);
}
}
void AnalyzeAction::analyzeStreaming(AnalyzeConsumer& consumer) const {
IOFacade ioFacade(_printActions);
if (_printLcm) {
unique_ptr<IOHandler> output = _io.createOutputHandler();
ioFacade.writeTerm(consumer.getLcm(), consumer.getNames(),
output.get(), stdout);
fputc('\n', stdout);
}
if (_printVarCount)
fprintf(stdout, "%lu\n", (unsigned long)consumer.getNames().getVarCount());
if (_printGeneratorCount)
fprintf(stdout, "%lu\n", (unsigned long)consumer.getGeneratorCount());
if (_printMaximumExponent) {
if (consumer.getNames().getVarCount() == 0)
fputs("0\n", stdout);
else
gmp_fprintf(stdout, "%Zd\n", consumer.getMaximumExponent().get_mpz_t());
}
if (_summaryLevel.getValue() == 1) {
fprintf(stdout, "%lu generators\n",
(unsigned long)consumer.getGeneratorCount());
fprintf(stdout, "%lu variables\n",
(unsigned long)consumer.getNames().getVarCount());
}
}
const char* AnalyzeAction::staticGetName() {
return "analyze";
}
AnalyzeAction.h
BigIdeal.h
BigTermConsumer.h
DataType.h
IOFacade.h
IOHandler.h
IdealFacade.h
Scanner.h
Action::_printActions
BoolParameter _printActions
Definition Action.h:68
Action::Action
Action(const char *name, const char *shortDescription, const char *description, bool acceptsNonParameter)
Definition Action.cpp:46
Action::obtainParameters
virtual void obtainParameters(vector< Parameter * > &parameters)
Definition Action.cpp:133
AnalyzeAction::_printMaximumExponent
BoolParameter _printMaximumExponent
Definition AnalyzeAction.h:48
AnalyzeAction::_printLcm
BoolParameter _printLcm
Definition AnalyzeAction.h:45
AnalyzeAction::_printGeneratorCount
BoolParameter _printGeneratorCount
Definition AnalyzeAction.h:47
AnalyzeAction::_printMinimal
BoolParameter _printMinimal
Definition AnalyzeAction.h:49
AnalyzeAction::obtainParameters
virtual void obtainParameters(vector< Parameter * > &parameters)
Definition AnalyzeAction.cpp:131
AnalyzeAction::requiresWholeIdeal
bool requiresWholeIdeal() const
Definition AnalyzeAction.cpp:169
AnalyzeAction::staticGetName
static const char * staticGetName()
Definition AnalyzeAction.cpp:220
AnalyzeAction::analyzeIdeal
void analyzeIdeal(BigIdeal &ideal) const
Definition AnalyzeAction.cpp:173
AnalyzeAction::perform
virtual void perform()
Definition AnalyzeAction.cpp:143
AnalyzeAction::_summaryLevel
IntegerParameter _summaryLevel
Definition AnalyzeAction.h:44
AnalyzeAction::_printVarCount
BoolParameter _printVarCount
Definition AnalyzeAction.h:46
AnalyzeAction::analyzeStreaming
void analyzeStreaming(AnalyzeConsumer &consumer) const
Definition AnalyzeAction.cpp:190
AnalyzeAction::AnalyzeAction
AnalyzeAction()
Definition AnalyzeAction.cpp:86
AnalyzeAction::_io
IOParameters _io
Definition AnalyzeAction.h:43
AnalyzeConsumer
Definition AnalyzeAction.cpp:30
AnalyzeConsumer::_names
VarNames _names
Definition AnalyzeAction.cpp:81
AnalyzeConsumer::getNames
const VarNames & getNames() const
Definition AnalyzeAction.cpp:67
AnalyzeConsumer::getMaximumExponent
const mpz_class & getMaximumExponent() const
Definition AnalyzeAction.cpp:75
AnalyzeConsumer::_generatorCount
size_t _generatorCount
Definition AnalyzeAction.cpp:82
AnalyzeConsumer::_lcm
vector< mpz_class > _lcm
Definition AnalyzeAction.cpp:83
AnalyzeConsumer::getGeneratorCount
size_t getGeneratorCount() const
Definition AnalyzeAction.cpp:63
AnalyzeConsumer::getLcm
const vector< mpz_class > & getLcm() const
Definition AnalyzeAction.cpp:71
AnalyzeConsumer::consume
virtual void consume(const Term &term, const TermTranslator &translator)
Definition AnalyzeAction.cpp:47
AnalyzeConsumer::consumeRing
virtual void consumeRing(const VarNames &names)
Tell the consumer which ring is being used.
Definition AnalyzeAction.cpp:36
AnalyzeConsumer::beginConsuming
virtual void beginConsuming()
Tell the consumer to begin consuming an ideal.
Definition AnalyzeAction.cpp:42
AnalyzeConsumer::doneConsuming
virtual void doneConsuming()
Must be called once after each time beginConsuming has been called.
Definition AnalyzeAction.cpp:60
AnalyzeConsumer::AnalyzeConsumer
AnalyzeConsumer()
Definition AnalyzeAction.cpp:32
AnalyzeConsumer::consume
virtual void consume(const vector< mpz_class > &term)
Definition AnalyzeAction.cpp:51
BigIdeal
Definition BigIdeal.h:27
BigIdeal::getGeneratorCount
size_t getGeneratorCount() const
Definition BigIdeal.h:144
BigTermConsumer
Definition BigTermConsumer.h:29
BigTermConsumer::consume
virtual void consume(const vector< mpz_class > &term)=0
DataType
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition DataType.h:29
IOFacade
A facade for input and output of mathematical objects.
Definition IOFacade.h:39
IOFacade::writeTerm
void writeTerm(const vector< mpz_class > &term, const VarNames &names, IOHandler *handler, FILE *out)
Definition IOFacade.cpp:218
IOFacade::readIdeal
void readIdeal(Scanner &in, BigTermConsumer &consumer)
Read an ideal from in and feed it to consumer.
Definition IOFacade.cpp:81
IdealFacade
A facade for performing operations on BigIdeal.
Definition IdealFacade.h:34
IdealFacade::sortAllAndMinimize
void sortAllAndMinimize(BigIdeal &bigIdeal)
Remove redundant generators from ideal.
Definition IdealFacade.cpp:156
IdealFacade::printAnalysis
void printAnalysis(FILE *out, BigIdeal &ideal)
Definition IdealFacade.cpp:253
Scanner
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition Scanner.h:50
Scanner::expectEOF
void expectEOF()
Require that there is no more input.
Definition Scanner.cpp:77
TermTranslator
TermTranslator handles translation between terms whose exponents are infinite precision integers and ...
Definition TermTranslator.h:41
Term
Term represents a product of variables which does not include a coefficient.
Definition Term.h:49
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