Frobby  0.9.5
TransformAction.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 "TransformAction.h"
19 
20 #include "BigIdeal.h"
21 #include "IOFacade.h"
22 #include "IdealFacade.h"
23 #include "Scanner.h"
24 #include "ElementDeleter.h"
25 #include "VarSorter.h"
26 #include "DataType.h"
27 
28 #include <algorithm>
29 
31  Action
32 (staticGetName(),
33  "Change the representation of the input ideal.",
34  "By default, transform simply writes the input ideals to output. A "
35  "number of parameters allow one to transform the input ideal in various ways.",
36  false),
37 
38  _io(DataType::getMonomialIdealType(), DataType::getMonomialIdealType()),
39 
40  _canonicalize
41  ("canon",
42  "Sort variables, generators and ideals to get canonical representation.",
43  false),
44 
45  _sort
46  ("sort",
47  "Sort generators according to the reverse lexicographic order.",
48  false),
49 
50  _unique
51  ("unique",
52  "Remove duplicate generators.",
53  false),
54 
55  _minimize
56  ("minimize",
57  "Remove non-minimial generators.",
58  false),
59 
60  _deform
61  ("deform",
62  "Apply a generic deformation to the input ideal.",
63  false),
64 
65  _radical
66  ("radical",
67  "Take the radical of the generators. Combine this with -minimize to "
68  "get rid of any non-minimal ones.",
69  false),
70 
71  _product
72  ("product",
73  "Replace each ideal with the product of its generators.",
74  false),
75 
76  _addPurePowers
77  ("addPurePowers",
78  "Adds a pure power for each variable that does not already have a pure "
79  "power in the ideal. Each exponent is chosen to be one larger than the "
80  "maximal exponent of that variable that appears in the ideal.",
81  false),
82 
83  _trimVariables
84  ("trimVariables",
85  "Remove variables that divide none of the generators.",
86  false),
87 
88  _transpose
89  ("transpose",
90  "Exchange variables and minimal generators. Let M be a matrix whose "
91  "rows are labeled by minimal generators and whose columns are labeled "
92  "by variables. The entry at row g and column x is the number of times "
93  "that x divides g. This options transposes that matrix.",
94  false),
95 
96  _swap01
97  ("swap01",
98  "Change all 0 exponents to 1 and vice versa.",
99  false),
100 
101  _projectVar
102  ("projectVar",
103  "Project away the i'th variable counting from 1. No action is taken "
104  "for a value of 0 or more than the number of variables in the ring.",
105  0) {
106 }
107 
108 void TransformAction::obtainParameters(vector<Parameter*>& parameters) {
109  _io.obtainParameters(parameters);
110  parameters.push_back(&_canonicalize);
111  parameters.push_back(&_minimize);
112  parameters.push_back(&_sort);
113  parameters.push_back(&_unique);
114  parameters.push_back(&_deform);
115  parameters.push_back(&_radical);
116  parameters.push_back(&_product);
117  parameters.push_back(&_addPurePowers);
118  parameters.push_back(&_trimVariables);
119  parameters.push_back(&_swap01);
120  parameters.push_back(&_projectVar);
121  parameters.push_back(&_transpose);
122  Action::obtainParameters(parameters);
123 }
124 
126  Scanner in(_io.getInputFormat(), stdin);
129 
130  IOFacade facade(_printActions);
131 
132  vector<BigIdeal*> ideals;
133  ElementDeleter<vector<BigIdeal*> > idealsDeleter(ideals);
134  VarNames names;
135 
136  facade.readIdeals(in, ideals, names);
137  in.expectEOF();
138 
139  IdealFacade idealFacade(_printActions);
140 
141  if (_transpose) {
142  names.clear();
143  for (size_t i = 0; i < ideals.size(); ++i) {
144  const BigIdeal& ideal = *(ideals[i]);
145  BigIdeal trans(VarNames(ideal.getGeneratorCount()));
146  trans.reserve(ideal.getVarCount());
147  for (size_t var = 0; var < ideal.getVarCount(); ++var) {
148  trans.newLastTerm();
149  for (size_t gen = 0; gen < ideal.getGeneratorCount(); ++gen)
150  trans.getLastTermRef()[gen] = ideal[gen][var];
151  }
152  (*ideals[i]) = trans;
153  if (i == ideals.size() - 1)
154  names = ideal.getNames();
155  }
156  }
157 
158  if (0 < _projectVar && _projectVar <= names.getVarCount()) {
159  size_t var = _projectVar - 1;
160  names.projectVar(var);
161 
162  for (size_t i = 0; i < ideals.size(); ++i) {
163  BigIdeal& ideal = *(ideals[i]);
164  idealFacade.projectVar(ideal, var);
165  }
166  }
167 
168  if (_product) {
169  auto_ptr<BigIdeal> ideal;
170  ideal.reset(new BigIdeal(names));
171 
172  idealFacade.takeProducts(ideals, *ideal);
173 
174  idealsDeleter.deleteElements();
175  exceptionSafePushBack(ideals, ideal);
176  }
177 
178  for (size_t i = 0; i < ideals.size(); ++i) {
179  BigIdeal& ideal = *(ideals[i]);
180 
181  if (_radical)
182  idealFacade.takeRadical(ideal);
183 
184  if (_swap01)
185  idealFacade.swap01(ideal);
186 
187  if (_minimize)
188  idealFacade.sortAllAndMinimize(ideal);
189 
190  if (_deform)
191  idealFacade.deform(ideal);
192  }
193 
194  if (_trimVariables)
195  idealFacade.trimVariables(ideals, names);
196 
197  for (size_t i = 0; i < ideals.size(); ++i) {
198  BigIdeal& ideal = *(ideals[i]);
199 
200  if (_addPurePowers)
201  idealFacade.addPurePowers(ideal);
202 
203  if (_canonicalize)
204  idealFacade.sortVariables(ideal);
205 
206  if (_unique)
207  idealFacade.sortGeneratorsUnique(ideal);
208  else if (_sort || _canonicalize)
209  idealFacade.sortGenerators(ideal);
210  }
211 
212  if (_canonicalize) {
213  VarSorter sorter(names);
214  sorter.getOrderedNames(names);
215 
216  sort(ideals.begin(), ideals.end(), compareIdeals);
217  }
218 
219  auto_ptr<IOHandler> output(_io.createOutputHandler());
220  facade.writeIdeals(ideals, names, output.get(), stdout);
221 }
222 
224  return "transform";
225 }
226 
228  return *a < *b;
229 }
void exceptionSafePushBack(Container &container, auto_ptr< Element > pointer)
Definition: Action.h:25
BoolParameter _printActions
Definition: Action.h:68
virtual void obtainParameters(vector< Parameter * > &parameters)
Definition: Action.cpp:133
void reserve(size_t capacity)
Definition: BigIdeal.cpp:112
void newLastTerm()
Definition: BigIdeal.cpp:104
size_t getVarCount() const
Definition: BigIdeal.h:148
const VarNames & getNames() const
Definition: BigIdeal.cpp:253
size_t getGeneratorCount() const
Definition: BigIdeal.h:144
vector< mpz_class > & getLastTermRef()
Definition: BigIdeal.h:133
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition: DataType.h:29
void deleteElements()
A facade for input and output of mathematical objects.
Definition: IOFacade.h:39
void readIdeals(Scanner &in, vector< BigIdeal * > &ideals, VarNames &names)
Insert the ideals that are read into the parameter ideals.
Definition: IOFacade.cpp:132
void writeIdeals(const vector< BigIdeal * > &ideals, const VarNames &names, IOHandler *handler, FILE *out)
Definition: IOFacade.cpp:168
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 trimVariables(const vector< BigIdeal * > &ideals, VarNames &names)
Remove those variables that do not appear in any generator.
void addPurePowers(BigIdeal &bigIdeal)
Adds x_i^(l_i+1) to the ideal for each i where that will be a minimal generator, where x^l is the lcm...
void swap01(BigIdeal &ideal)
Change all 0 exponents to 1 and vice versa.
Definition: IdealFacade.cpp:65
void sortGenerators(BigIdeal &ideal)
Sorts the generators of ideal.
void deform(BigIdeal &ideal)
Applies some generic deformation to the ideal.
Definition: IdealFacade.cpp:37
void takeRadical(BigIdeal &ideal)
Takes the radical of the generators of ideal.
Definition: IdealFacade.cpp:51
void sortVariables(BigIdeal &ideal)
Sorts the variables of ideal.
void projectVar(BigIdeal &bigIdeal, size_t var)
Remove the variable var from the ideal and ring by substituting it by 1.
void sortGeneratorsUnique(BigIdeal &ideal)
Sorts the generators of ideal and removes duplicates.
void takeProducts(const vector< BigIdeal * > &ideals, BigIdeal &ideal)
Take the product of the minimal generators of each ideal, and add the resulting monomials as generato...
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
static bool compareIdeals(const BigIdeal *a, const BigIdeal *b)
BoolParameter _deform
BoolParameter _canonicalize
BoolParameter _addPurePowers
BoolParameter _transpose
IOParameters _io
BoolParameter _swap01
virtual void obtainParameters(vector< Parameter * > &parameters)
BoolParameter _minimize
static const char * staticGetName()
BoolParameter _product
BoolParameter _unique
BoolParameter _sort
BoolParameter _radical
virtual void perform()
BoolParameter _trimVariables
IntegerParameter _projectVar
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
void projectVar(size_t index)
Definition: VarNames.cpp:161
void clear()
Resets the number of variables to zero.
Definition: VarNames.cpp:106
void getOrderedNames(VarNames &names)
Definition: VarSorter.cpp:50