All Classes Functions Friends
regularchain.h
1 #ifndef _REGULARCHAIN_H_
2 #define _REGULARCHAIN_H_
3 
4 #include "triangularset.h"
5 
6 template <class Ring>
7 class RegularChain : public BPASRegularChain, public TriangularSet<Ring> {
8  private:
12  public:
13  /**
14  * Default constructor
15  *
16  * @param
17  **/
18  RegularChain<Ring> () { n = 0; }
19  /**
20  * Construct with number of polynomials and variable names
21  *
22  * @param s: The number of polynomials
23  * @param xs; Variable names
24  **/
25  RegularChain<Ring> (int s, std::vector<std::string> xs) {
26  if (s < 1) { n = 0; }
27  else {
28  n = s;
30  if (xs.size() == s) {
31  names = new std::string[n];
32  for (int i = 0; i < n; ++i)
33  names[i] = xs[n-i-1];
34  }
35  else {
36  std::cout << "BPAS: error, RegularChain<Ring> must be defined with " << s << " variables." << std::endl;
37  exit(1);
38  }
39  }
40  }
41  /**
42  * Copy constructor
43  *
44  * @param a: A regular chain
45  **/
47  if (a.n) {
48  n = a.n;
50  names = new std::string[n];
51  std::copy(a.set, a.set+n, set);
52  std::copy(a.names, a.names+n, names);
53  }
54  }
55  /**
56  * Overload operator =
57  *
58  * Using TriangularSet<Ring>'s
59  *
60  * @param
61  **/
63  /**
64  * Overload operator +
65  *
66  * @param smp: A sparse multivariate polynomial
67  **/
69  RegularChain<Ring> r (*this);
70  return (r += smp);
71  }
72  /**
73  * Overload operator +=
74  *
75  * @param smp: A sparse multivariate polynomial
76  **/
78  if (smp.isConstant()) {
79  std::cout << "BPAS: error, cannot adding a constant to RegularChain<Ring>" << std::endl;
80  exit(1);
81  }
82  int v = smp.numberOfVariables();
83  if (v > n || !v) {
84  std::cout << "BPAS: error, cannot adding a polynomisl of " << v << " variables to a RegularChain<Ring> of " << n << " variables." << std::endl;
85  exit(1);
86  }
87 
88  int* pos = new int[v];
89  std::vector<std::string> xs = smp.variables();
90  for (int i = 0; i < v; ++i) {
91  pos[i] = -1;
92  for (int j = n-1; j > -1; --j) {
93  if (xs[i] == names[j]) {
94  pos[i] = j;
95  break;
96  }
97  }
98  }
99  bool isIt = 0;
100  int k = pos[0];
101  if (k < 0) { isIt = 1; }
102  for (int i = 1; i < v; ++i) {
103  if (pos[i] >= 0 && pos[i] < k) { k = pos[i]; }
104  else { isIt = 1; break; }
105  }
106  if (isIt) {
107  std::cout << "BPAS: error, RegularChain<Ring>[";
108  for (int i = n-1; i > -1; --i) {
109  std::cout << names[i];
110  if (i) { std::cout << ", "; }
111  }
112  std::cout << "] cannot add a polynomial in Ring[";
113  for (int i = 0; i < xs.size(); ++i) {
114  std::cout << xs[i];
115  if (i < xs.size()-1) { std::cout << ", "; }
116  }
117  std::cout << "]." << std::endl;
118  exit(1);
119  }
120  k = pos[0];
121  delete [] pos;
122 
123  if (!set[k].isZero()) {
124  std::cout << "BPAS: error, a polynomial with the leading variable " << xs[0] << " already exists in RegularChain<Ring>[";
125  for (int i = n-1; i > -1; --i) {
126  std::cout << names[i];
127  if (i) { std::cout << ", "; }
128  }
129  std::cout << "]." << std::endl;
130  exit(1);
131  }
132 
133  set[k] += smp;
134 
135  if (k < 2 && n > 1 && !set[0].isZero() && !set[1].isZero()) {
136  SparseMultivariatePolynomial<Ring> l = set[1].leadingCoefficientInVariable(names[1]);
137 
138  if (!l.isConstant()) {
140  SparseUnivariatePolynomial< SparseMultivariatePolynomial<Ring> > q = set[0].convertToSUP(names[0]);
141 
143 
144  if (r.isZero()) {
145  std::cout << "BPAS: error, it is not a regular chain after adding a polynomial in RegularChain<Ring>." << std::endl;
146  exit(1);
147  }
148  }
149  }
150 
151  return *this;
152  }
153 
154  /**
155  * Get the number of variables
156  *
157  * @param
158  **/
159  inline int numberOfVariables() {
160  return n;
161  }
162  /**
163  * Get variable names
164  *
165  * @param
166  **/
167  inline std::vector<std::string> variables() {
168  std::vector<std::string> xs;
169  for (int i = n-1; i > -1; --i)
170  xs.push_back(names[i]);
171  return xs;
172  }
173  /**
174  * Select a polynomial given the leading variable
175  *
176  * Using TriangularSet<Ring>'s
177  *
178  * @param
179  **/
181  /**
182  * The regular chain under a variable
183  *
184  * @param x: The variable name
185  **/
186  inline RegularChain<Ring> under (std::string x) {
187  int k = -1;
188  for (int i = 0; i < n; ++i) {
189  if (x == names[i]) {
190  k = i;
191  break;
192  }
193  }
194  if (k <= 0)
195  return RegularChain<Ring>();
196  else {
198  r.n = k - 1;
199  r.set = new SparseMultivariatePolynomial<Ring>[r.n];
200  r.names = new std::string[r.n];
201  std::copy(set, set+k-1, r.set);
202  std::copy(names, names+k-1, r.names);
203  return r;
204  }
205  }
206  /**
207  * The regular chain upper a variable
208  *
209  * @param x: The variable name
210  **/
211  inline RegularChain<Ring> upper (std::string x) {
212  int k = -1;
213  for (int i = 0; i < n; ++i) {
214  if (x == names[i]) {
215  k = i;
216  break;
217  }
218  }
219  if (k < 0 || k == n-1)
220  return RegularChain<Ring>();
221  else {
223  r.n = n - k - 1;
224  r.set = new SparseMultivariatePolynomial<Ring>[r.n];
225  r.names = new std::string[r.n];
226  std::copy(set+k+1, set+n, r.set);
227  std::copy(names+k+1, names+n, r.names);
228  return r;
229  }
230  }
231 
232  /**
233  * Pseudo division
234  * Return the remainder
235  *
236  * @param p: An input polynomial
237  * @param quo: The quotients
238  * @param c: The constant multiplied to the input polynomial
239  **/
241  /**
242  * Monic division
243  * Return the remainder
244  *
245  * @param p: An input polynomial
246  * @param quo: The quotients
247  **/
249  /**
250  * Overload stream operator <<
251  *
252  * @param out: Stream object
253  * @param a: A regular chain
254  **/
255  inline friend std::ostream& operator<< (std::ostream& out, RegularChain<Ring>& a) {
256  bool isFirst = 0;
257  out << "[";
258  for (int i = 0; i < a.n; ++i) {
259  if (isFirst) { out << ", "; }
260  out << a.set[i];
261  isFirst = 1;
262  }
263  out << "]";
264  return out;
265  }
266 };
267 
268 #endif
269 /* This file is part of the BPAS library http://www.bpaslib.org
270 
271  BPAS is free software: you can redistribute it and/or modify
272  it under the terms of the GNU General Public License as published by
273  the Free Software Foundation, either version 3 of the License, or
274  (at your option) any later version.
275 
276  BPAS is distributed in the hope that it will be useful,
277  but WITHOUT ANY WARRANTY; without even the implied warranty of
278  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
279  GNU General Public License for more details.
280 
281  You should have received a copy of the GNU General Public License
282  along with BPAS. If not, see <http://www.gnu.org/licenses/>.
283 
284  Copyright: Changbo Chen <changbo.chen@hotmail.com>
285  Farnam Mansouri <mansouri.farnam@gmail.com>
286  Marc Moreno Maza <moreno@csd.uwo.ca>
287  Ning Xie <nxie6@csd.uwo.ca>
288  Yuzhen Xie <yuzhenxie@yahoo.ca>
289 
290 */
291 
292 
Definition: triangularset.h:7
bool isZero()
Definition: upolynomial.h:469
Definition: upolynomial.h:10
std::vector< std::string > variables()
Definition: regularchain.h:167
int isConstant()
Definition: mpolynomial.h:621
SparseUnivariatePolynomial< SparseMultivariatePolynomial< Ring > > convertToSUP(std::string x)
Definition: mpolynomial.h:1399
std::vector< std::string > variables()
Definition: mpolynomial.h:1313
Definition: mpolynomial.h:12
RegularChain< Ring > operator+(SparseMultivariatePolynomial< Ring > &smp)
Definition: regularchain.h:68
RegularChain< Ring > & operator+=(SparseMultivariatePolynomial< Ring > smp)
Definition: regularchain.h:77
int numberOfVariables()
Definition: regularchain.h:159
SparseUnivariatePolynomial< Ring > resultant(SparseUnivariatePolynomial< Ring > &q)
Definition: upolynomial.h:1478
RegularChain< Ring > under(std::string x)
Definition: regularchain.h:186
Definition: regularchain.h:7
int numberOfVariables()
Definition: mpolynomial.h:414
Definition: polynomial.h:107
RegularChain< Ring > upper(std::string x)
Definition: regularchain.h:211