Gröbner basis project
Codebase for research into Gröbner basis computation
dense_univariate_rational_poly.hpp
1 #ifndef __DENSE_UNIVARIATE_RATIONAL_POLY_HPP_
2 #define __DENSE_UNIVARIATE_RATIONAL_POLY_HPP_
3 
4 /*****************************************************************************\
5 * This file is part of DynGB. *
6 * *
7 * DynGB is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Foobar is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
19 \*****************************************************************************/
20 
21 #include <iostream>
22 
23 #include "system_constants.hpp"
24 
25 using std::ostream; using std::cout; using std::endl;
26 
38 public:
40 
46  );
48  Dense_Univariate_Rational_Polynomial(DEG_TYPE, int64_t *, uint64_t *);
50 
54 
55 
61  void expand_poly(DEG_TYPE);
63  void set_coefficient(DEG_TYPE k, long a, unsigned long b) {
64  expand_poly(k);
65  MPZCOEF_TYPE az = a;
66  MPZCOEF_TYPE bz = b;
67  coeffs[k] = az;
68  coeffs[k] /= bz;
69  //coeffs[k] = a;
70  //coeffs[k] /= b;
71  if (k > deg and a != 0) { deg = k; }
72  else if (k == deg and a == 0) {
73  while (deg > 0 and coeffs[deg] == 0) { --deg; }
74  }
75  }
79  void scale_by(COEF_TYPE a);
85  void scale_by(COEF_TYPE a, UCOEF_TYPE b);
89  void scale_by(MPQCOEF_TYPE);
95  void multiply_by_monomial_of_degree(DEG_TYPE);
101  void negate();
105  void operator +=(const Dense_Univariate_Rational_Polynomial & other) { add(other); }
113 
114 
119 
120 
122  bool is_zero() const {
123  bool nonzero = true;
124  for (DEG_TYPE i = 0; nonzero and i <= deg; ++i)
125  nonzero = (coeffs[i] == 0);
126  return nonzero;
127  }
129  MPZCOEF_TYPE numerator(DEG_TYPE k) const { return coeffs[k].get_num(); }
131  MPZCOEF_TYPE denominator(DEG_TYPE k) const { return coeffs[k].get_den(); }
133  DEG_TYPE degree() const { return deg; }
135 
136  friend ostream & operator<<(
138  ostream & os, const Dense_Univariate_Rational_Polynomial & p
139  ) {
140  if (p.coeffs[p.deg] < 0) { os << '-'; }
141  for (DEG_TYPE i = p.deg; i > 0; --i) {
142  if (p.coeffs[i].get_num() != 0) {
143  if (p.coeffs[i].get_num() == 1) {
144  if (p.coeffs[i].get_den() != 1) {
145  os << "1 / " << p.coeffs[i].get_den();
146  }
147  } else if (p.coeffs[i].get_num() == -1) {
148  if (p.coeffs[i].get_den() != 1) {
149  os << "1 / " << p.coeffs[i].get_den();
150  }
151  } else {
152  if (p.coeffs[i].get_num() > 0) { os << p.coeffs[i].get_num(); }
153  else { os << -p.coeffs[i].get_num(); }
154  if (p.coeffs[i].get_den() != 1) { os << " / " << p.coeffs[i].get_den(); }
155  }
156  if (i != 1) { os << "t^" << i; }
157  else { os << "t"; }
158  }
159  if (p.coeffs[i - 1].get_num() < 0) { os << " - "; }
160  else if (p.coeffs[i - 1].get_num() > 0) { os << " + "; }
161  }
162  if (p.coeffs[0].get_num() != 0) {
163  if (p.coeffs[0].get_num() > 0) { os << p.coeffs[0].get_num(); }
164  else if (p.coeffs[0].get_num() < 0) { os << -p.coeffs[0].get_num(); }
165  if (p.coeffs[0].get_den() != 1) { os << " / " << p.coeffs[0].get_den(); }
166  }
167  return os;
168  }
170 protected:
172  MPQCOEF_TYPE * coeffs;
174  DEG_TYPE deg;
176  DEG_TYPE size;
177 };
178 
179 #endif
MPZCOEF_TYPE numerator(DEG_TYPE k) const
returns the th numerator
void set_coefficient(DEG_TYPE k, long a, unsigned long b)
set the coefficient of to
MPQCOEF_TYPE * coeffs
list of coefficients; index 0 is the constant’s
DEG_TYPE size
number of slots for coefficients
bool is_zero() const
indicates whether the polynomial is zero
void multiply_by(const Dense_Univariate_Rational_Polynomial &)
highly inefficient polynomial multiplication ( )
void operator-=(const Dense_Univariate_Rational_Polynomial &other)
alias for subtract()
void add(const Dense_Univariate_Rational_Polynomial &)
adds other to this
void expand_poly(DEG_TYPE)
expand to allow for higher-degree monomials
void subtract(const Dense_Univariate_Rational_Polynomial &)
subtracts other from this
void multiply_by_monomial_of_degree(DEG_TYPE)
a hopefully efficient multiplication algorithm
void operator+=(const Dense_Univariate_Rational_Polynomial &other)
alias for add()
DEG_TYPE deg
degree of the polynomial (largest nonzero exponent)
void scale_by(COEF_TYPE a)
multiplies every monomial by a constant integer
quick-’n-dirty Dense_Univariate rational polynomial class
MPZCOEF_TYPE denominator(DEG_TYPE k) const
returns the th denominator
DEG_TYPE degree() const
returns the polynomial’s degree
Dense_Univariate_Rational_Polynomial(DEG_TYPE)
construct with the number of expected terms
Dense_Univariate_Rational_Polynomial operator-(const Dense_Univariate_Rational_Polynomial &) const
returns the difference between this and the other