Frobby 0.9.5
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
31public:
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
80private:
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
131void 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
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}
Definition: Action.h:25
BoolParameter _printActions
Definition: Action.h:68
virtual void obtainParameters(vector< Parameter * > &parameters)
Definition: Action.cpp:133
BoolParameter _printMaximumExponent
Definition: AnalyzeAction.h:48
BoolParameter _printLcm
Definition: AnalyzeAction.h:45
BoolParameter _printGeneratorCount
Definition: AnalyzeAction.h:47
BoolParameter _printMinimal
Definition: AnalyzeAction.h:49
virtual void obtainParameters(vector< Parameter * > &parameters)
bool requiresWholeIdeal() const
static const char * staticGetName()
void analyzeIdeal(BigIdeal &ideal) const
virtual void perform()
IntegerParameter _summaryLevel
Definition: AnalyzeAction.h:44
BoolParameter _printVarCount
Definition: AnalyzeAction.h:46
void analyzeStreaming(AnalyzeConsumer &consumer) const
IOParameters _io
Definition: AnalyzeAction.h:43
const VarNames & getNames() const
const mpz_class & getMaximumExponent() const
vector< mpz_class > _lcm
size_t getGeneratorCount() const
const vector< mpz_class > & getLcm() const
virtual void consume(const Term &term, const TermTranslator &translator)
virtual void consumeRing(const VarNames &names)
Tell the consumer which ring is being used.
virtual void beginConsuming()
Tell the consumer to begin consuming an ideal.
virtual void doneConsuming()
Must be called once after each time beginConsuming has been called.
virtual void consume(const vector< mpz_class > &term)
size_t getGeneratorCount() const
Definition: BigIdeal.h:144
virtual void consume(const vector< mpz_class > &term)=0
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition: DataType.h:29
A facade for input and output of mathematical objects.
Definition: IOFacade.h:39
void writeTerm(const vector< mpz_class > &term, const VarNames &names, IOHandler *handler, FILE *out)
Definition: IOFacade.cpp:218
void readIdeal(Scanner &in, BigTermConsumer &consumer)
Read an ideal from in and feed it to consumer.
Definition: IOFacade.cpp:81
void autoDetectInputFormat(Scanner &in)
If using the input format, this must be called before validating the ideals, since the auto detect fo...
auto_ptr< IOHandler > createOutputHandler() const
const string & getInputFormat() const
void validateFormats() const
A facade for performing operations on BigIdeal.
Definition: IdealFacade.h:34
void sortAllAndMinimize(BigIdeal &bigIdeal)
Remove redundant generators from ideal.
void printAnalysis(FILE *out, BigIdeal &ideal)
unsigned int getValue() const
void obtainParameters(vector< Parameter * > &parameters)
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition: Scanner.h:50
void expectEOF()
Require that there is no more input.
Definition: Scanner.cpp:77
TermTranslator handles translation between terms whose exponents are infinite precision integers and ...
Term represents a product of variables which does not include a coefficient.
Definition: Term.h:49
Defines the variables of a polynomial ring and facilities IO involving them.
Definition: VarNames.h:40
size_t getVarCount() const
Returns the current number of variables.
Definition: VarNames.h:113
#define ASSERT(X)
Definition: stdinc.h:86