Frobby  0.9.1
AnalyzeAction.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 "AnalyzeAction.h"
19 
20 #include "BigIdeal.h"
21 #include "IOFacade.h"
22 #include "IdealFacade.h"
23 #include "Scanner.h"
24 #include "IOHandler.h"
25 #include "BigTermConsumer.h"
26 #include "DataType.h"
27 
28 #include <algorithm>
29 
31 public:
33  _generatorCount(0) {
34  }
35 
36  virtual void consumeRing(const VarNames& names) {
37  _names = names;
38  _lcm.clear();
39  _lcm.resize(_names.getVarCount());
40  }
41 
42  virtual void beginConsuming() {
43  }
44 
46 
47  virtual void consume(const Term& term, const TermTranslator& translator) {
48  BigTermConsumer::consume(term, translator);
49  }
50 
51  virtual void consume(const vector<mpz_class>& term) {
52  ASSERT(term.size() == _names.getVarCount());
53 
55  for (size_t var = 0; var < term.size(); ++var)
56  if (_lcm[var] < term[var])
57  _lcm[var] = term[var];
58  }
59 
60  virtual void doneConsuming() {
61  }
62 
63  size_t getGeneratorCount() const {
64  return _generatorCount;
65  }
66 
67  const VarNames& getNames() const {
68  return _names;
69  }
70 
71  const vector<mpz_class>& getLcm() const {
72  return _lcm;
73  }
74 
75  const mpz_class& getMaximumExponent() const {
76  ASSERT(_lcm.size() > 0);
77  return *max_element(_lcm.begin(), _lcm.end());
78  }
79 
80 private:
83  vector<mpz_class> _lcm;
84 };
85 
87  Action
88 (staticGetName(),
89  "Display information about the input ideal.",
90  "Display information about input ideal. This is useful for getting a quick\n"
91  "impression of how the ideal looks like, and it can be used in scripts\n"
92  "that need information about the ideal.",
93  false),
94 
95  _io(DataType::getMonomialIdealType(), DataType::getMonomialIdealType()),
96 
97  _summaryLevel
98  ("summaryLevel",
99  "If non-zero, then print a summary of the ideal to the error output\n"
100  "stream. A higher summary level results in more expensive analysis in\n"
101  "order to provide more information. Currently levels 0, 1 and 2 are\n"
102  "available.",
103  1),
104 
105  _printLcm
106  ("lcm",
107  "Print the least common multiple of the generators.",
108  false),
109 
110  _printVarCount
111  ("varCount",
112  "Print the number of variables.",
113  false),
114 
115  _printGeneratorCount
116  ("genCount",
117  "Print the number of generators.",
118  false),
119 
120  _printMaximumExponent
121  ("maxExp",
122  "Print the largest exponent that appears in the input file",
123  false),
124 
125  _printMinimal
126  ("minimal",
127  "Print 1 if the ideal has no non-minimal generators. Print 0 otherwise.",
128  false) {
129 }
130 
131 void AnalyzeAction::obtainParameters(vector<Parameter*>& parameters) {
132  parameters.push_back(&_summaryLevel);
133  parameters.push_back(&_printLcm);
134  parameters.push_back(&_printVarCount);
135  parameters.push_back(&_printGeneratorCount);
136  parameters.push_back(&_printMaximumExponent);
137  parameters.push_back(&_printMinimal);
138 
139  _io.obtainParameters(parameters);
140  Action::obtainParameters(parameters);
141 }
142 
144  Scanner in(_io.getInputFormat(), stdin);
147 
148  AnalyzeConsumer consumer;
149 
150  // We only read the entire ideal into memory at once if we have to.
151  IOFacade ioFacade(_printActions);
152  if (!requiresWholeIdeal()) {
153  ioFacade.readIdeal(in, consumer);
154  in.expectEOF();
155 
156  analyzeStreaming(consumer);
157  } else {
158  BigIdeal ideal;
159  ioFacade.readIdeal(in, ideal);
160  in.expectEOF();
161 
162  consumer.consume(ideal);
163 
164  analyzeStreaming(consumer);
165  analyzeIdeal(ideal);
166  }
167 }
168 
170  return _printMinimal || _summaryLevel > 1;
171 }
172 
174  IdealFacade idealFacade(_printActions);
175 
176  if (_printMinimal) {
177  size_t generatorCount = ideal.getGeneratorCount();
178  idealFacade.sortAllAndMinimize(ideal);
179  if (generatorCount == ideal.getGeneratorCount())
180  fputs("1\n", stdout);
181  else
182  fputs("0\n", stdout);
183  }
184 
185  if (_summaryLevel >= 2) {
186  idealFacade.printAnalysis(stdout, ideal);
187  }
188 }
189 
191  IOFacade ioFacade(_printActions);
192 
193  if (_printLcm) {
194  auto_ptr<IOHandler> output = _io.createOutputHandler();
195  ioFacade.writeTerm(consumer.getLcm(), consumer.getNames(),
196  output.get(), stdout);
197  fputc('\n', stdout);
198  }
199 
200  if (_printVarCount)
201  fprintf(stdout, "%lu\n", (unsigned long)consumer.getNames().getVarCount());
203  fprintf(stdout, "%lu\n", (unsigned long)consumer.getGeneratorCount());
204 
205  if (_printMaximumExponent) {
206  if (consumer.getNames().getVarCount() == 0)
207  fputs("0\n", stdout);
208  else
209  gmp_fprintf(stdout, "%Zd\n", consumer.getMaximumExponent().get_mpz_t());
210  }
211 
212  if (_summaryLevel.getValue() == 1) {
213  fprintf(stdout, "%lu generators\n",
214  (unsigned long)consumer.getGeneratorCount());
215  fprintf(stdout, "%lu variables\n",
216  (unsigned long)consumer.getNames().getVarCount());
217  }
218 }
219 
221  return "analyze";
222 }
BigIdeal
Definition: BigIdeal.h:27
DataType.h
AnalyzeConsumer::_generatorCount
size_t _generatorCount
Definition: AnalyzeAction.cpp:82
stdinc.h
AnalyzeConsumer::AnalyzeConsumer
AnalyzeConsumer()
Definition: AnalyzeAction.cpp:32
IOHandler.h
Scanner::expectEOF
void expectEOF()
Require that there is no more input.
Definition: Scanner.cpp:77
BigTermConsumer.h
AnalyzeAction::_printGeneratorCount
BoolParameter _printGeneratorCount
Definition: AnalyzeAction.h:47
Scanner
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition: Scanner.h:50
AnalyzeAction::_io
IOParameters _io
Definition: AnalyzeAction.h:43
AnalyzeConsumer::_names
VarNames _names
Definition: AnalyzeAction.cpp:81
AnalyzeConsumer::getGeneratorCount
size_t getGeneratorCount() const
Definition: AnalyzeAction.cpp:63
AnalyzeConsumer::consumeRing
virtual void consumeRing(const VarNames &names)
Tell the consumer which ring is being used.
Definition: AnalyzeAction.cpp:36
AnalyzeAction::_printMinimal
BoolParameter _printMinimal
Definition: AnalyzeAction.h:49
AnalyzeConsumer::getNames
const VarNames & getNames() const
Definition: AnalyzeAction.cpp:67
IOParameters::validateFormats
void validateFormats() const
Definition: IOParameters.cpp:154
AnalyzeConsumer::consume
virtual void consume(const vector< mpz_class > &term)
Definition: AnalyzeAction.cpp:51
IOFacade::writeTerm
void writeTerm(const vector< mpz_class > &term, const VarNames &names, IOHandler *handler, FILE *out)
Definition: IOFacade.cpp:218
AnalyzeAction::analyzeStreaming
void analyzeStreaming(AnalyzeConsumer &consumer) const
Definition: AnalyzeAction.cpp:190
AnalyzeAction.h
Action::obtainParameters
virtual void obtainParameters(vector< Parameter * > &parameters)
Definition: Action.cpp:133
BigTermConsumer::consume
virtual void consume(const vector< mpz_class > &term)=0
AnalyzeConsumer::_lcm
vector< mpz_class > _lcm
Definition: AnalyzeAction.cpp:83
TermTranslator
TermTranslator handles translation between terms whose exponents are infinite precision integers and ...
Definition: TermTranslator.h:41
Action
Definition: Action.h:25
AnalyzeConsumer::getLcm
const vector< mpz_class > & getLcm() const
Definition: AnalyzeAction.cpp:71
VarNames::getVarCount
size_t getVarCount() const
Returns the current number of variables.
Definition: VarNames.h:113
IOParameters::autoDetectInputFormat
void autoDetectInputFormat(Scanner &in)
If using the input format, this must be called before validating the ideals, since the auto detect fo...
Definition: IOParameters.cpp:141
Scanner.h
AnalyzeAction::AnalyzeAction
AnalyzeAction()
Definition: AnalyzeAction.cpp:86
Term
Term represents a product of variables which does not include a coefficient.
Definition: Term.h:49
IdealFacade.h
BigTermConsumer
Definition: BigTermConsumer.h:29
IdealFacade::printAnalysis
void printAnalysis(FILE *out, BigIdeal &ideal)
Definition: IdealFacade.cpp:253
AnalyzeAction::_printVarCount
BoolParameter _printVarCount
Definition: AnalyzeAction.h:46
AnalyzeConsumer::doneConsuming
virtual void doneConsuming()
Must be called once after each time beginConsuming has been called.
Definition: AnalyzeAction.cpp:60
AnalyzeAction::obtainParameters
virtual void obtainParameters(vector< Parameter * > &parameters)
Definition: AnalyzeAction.cpp:131
IdealFacade::sortAllAndMinimize
void sortAllAndMinimize(BigIdeal &bigIdeal)
Remove redundant generators from ideal.
Definition: IdealFacade.cpp:156
AnalyzeAction::staticGetName
static const char * staticGetName()
Definition: AnalyzeAction.cpp:220
IntegerParameter::getValue
unsigned int getValue() const
Definition: IntegerParameter.h:30
IOFacade.h
AnalyzeConsumer::getMaximumExponent
const mpz_class & getMaximumExponent() const
Definition: AnalyzeAction.cpp:75
AnalyzeAction::perform
virtual void perform()
Definition: AnalyzeAction.cpp:143
AnalyzeAction::analyzeIdeal
void analyzeIdeal(BigIdeal &ideal) const
Definition: AnalyzeAction.cpp:173
AnalyzeConsumer
Definition: AnalyzeAction.cpp:30
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
Action::_printActions
BoolParameter _printActions
Definition: Action.h:68
IOParameters::getInputFormat
const string & getInputFormat() const
Definition: IOParameters.cpp:108
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition: VarNames.h:40
AnalyzeAction::_printLcm
BoolParameter _printLcm
Definition: AnalyzeAction.h:45
AnalyzeAction::requiresWholeIdeal
bool requiresWholeIdeal() const
Definition: AnalyzeAction.cpp:169
ASSERT
#define ASSERT(X)
Definition: stdinc.h:86
DataType
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition: DataType.h:29
IOParameters::createOutputHandler
auto_ptr< IOHandler > createOutputHandler() const
Definition: IOParameters.cpp:135
BigIdeal::getGeneratorCount
size_t getGeneratorCount() const
Definition: BigIdeal.h:144
AnalyzeAction::_printMaximumExponent
BoolParameter _printMaximumExponent
Definition: AnalyzeAction.h:48
BigIdeal.h
AnalyzeConsumer::beginConsuming
virtual void beginConsuming()
Tell the consumer to begin consuming an ideal.
Definition: AnalyzeAction.cpp:42
AnalyzeAction::_summaryLevel
IntegerParameter _summaryLevel
Definition: AnalyzeAction.h:44
ParameterGroup::obtainParameters
void obtainParameters(vector< Parameter * > &parameters)
Definition: ParameterGroup.cpp:37
AnalyzeConsumer::consume
virtual void consume(const Term &term, const TermTranslator &translator)
Definition: AnalyzeAction.cpp:47
IOFacade
A facade for input and output of mathematical objects.
Definition: IOFacade.h:39