polynomial.h 5.85 KB
Newer Older
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
1 2
#ifndef POLYNOMIAL_H_
#define POLYNOMIAL_H_
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
3 4

#include <list>
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
5 6 7
#include <utility>
#include "./param.h"
#include "./monomial.h"
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
8

9 10
namespace tinygb {

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
11
template<class T>
12
class Polynomial;
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
13 14

template<class T>
15
std::ostream& operator<<(std::ostream &o, const Polynomial<T> &p);
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
16 17

template<class T>
18
class Polynomial {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
19
  void combine_LM();
20
  std::list<std::pair<Monomial, T> > vp;
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
21

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
22
 public:
23 24
  Polynomial() {
      vp = std::list<std::pair<Monomial, T> >();
SPAENLEHAUER Pierre-Jean's avatar
doc  
SPAENLEHAUER Pierre-Jean committed
25
    }
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
26

27
  explicit Polynomial(const std::list<std::pair<Monomial, T> >& _vp)
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
28 29
    : vp(_vp) {}

30
  const std::list<std::pair<Monomial, T> >& get_vp() const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
31 32 33
    return vp;
  }

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
34 35 36
  void normalize();

  void clear() {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
37 38
    vp.clear();
  }
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
39

40 41
  void insert(Monomial m, T e) {
    vp.push_back(std::pair<Monomial, T>(m, e));
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
42
  }
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
43

44
  const Monomial& LM() const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
45 46 47 48
    assert(!vp.back().second.is_zero());
    return vp.back().first;
  }

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
49
  const T& LC() const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
50 51 52 53
    return vp.back().second;
  }

  void set_monic();
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
54 55

  unsigned size() const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
56 57
    return vp.size();
  }
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
58 59

  bool is_zero() const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
60 61 62
    return vp.empty();
  }

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
63 64
  unsigned degree() const {
    // TODO(pj): extends for non-degree orderings
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
65 66 67 68 69 70 71
    return LM().degree();
  }

#ifdef BILIN
  unsigned degree_bilin() const;
#endif

72 73 74
  bool topred(const Polynomial &g);
  bool istopred(const Polynomial &g) const;
  T coeff(const Monomial& m) const;
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
75

76 77 78
  Polynomial<T> operator*(const Monomial&) const;
  void operator+=(const Polynomial<T>&);
  void operator-=(const Polynomial<T>&);
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
79 80
  void operator*=(const T&);
  void operator/=(const T&);
SPAENLEHAUER Pierre-Jean's avatar
doc  
SPAENLEHAUER Pierre-Jean committed
81 82

  void print(std::ostream& out) const;
83
  inline bool operator<(const Polynomial<T>& P) const;
84

85
  friend std::ostream& operator<< <T>(std::ostream&, const Polynomial<T>&);
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
86
};
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
87 88 89

#ifdef BILIN
template<class T>
90
unsigned Polynomial<T>::degree_bilin() const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
91
  unsigned R = 0;
92
  for (typename std::list<std::pair<Monomial, T> >::const_iterator it = \
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
93 94 95 96 97 98 99 100 101
      vp.begin(); it != vp.end(); ++it)
    if (it->first.degree_bilin() > R)
      R = it->first.degree_bilin();
  return R;
}
#endif


template<class T>
102
void Polynomial<T>::combine_LM() {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
103 104
  if (vp.empty())
    return;
105 106
  typename std::list<std::pair<Monomial, T> >::iterator it2;
  for (typename std::list<std::pair<Monomial, T> >::iterator it = vp.begin();
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      it != vp.end();) {
    if (it->second == T(0)) {
      it = vp.erase(it);
      continue;
    }
    it2 = it;
    ++it;
    if (it == vp.end()) {
      break;
    } else if (it->first == it2->first) {
      it->second += it2->second;
      vp.erase(it2);
    }
  }
}

template<class T>
124 125
T Polynomial<T>::coeff(const Monomial& m) const {
  for (typename std::list<std::pair<Monomial, T> >::const_iterator it = \
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
126 127 128 129 130 131 132
      vp.begin(); it != vp.end(); ++it)
    if (it->first == m)
      return it->second;
  return T(0);
}

template<class T>
133
void Polynomial<T>::set_monic() {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
134 135 136 137 138 139 140 141 142
  combine_LM();
  T leading_coeff = vp.back().second;
  leading_coeff.inv();
  // TODO(pj):  test if 1
  for (auto& it : vp)
    it.second *= leading_coeff;
}

template<class T>
143
void Polynomial<T>::normalize() {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
144 145 146 147 148
  vp.sort();
  combine_LM();
}

template<class T>
149
bool Polynomial<T>::topred(const Polynomial &g) {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
150 151 152 153
  if (g.is_zero() || is_zero())
    return false;
  if (!LM().isDivisibleBy(g.LM()))
    return false;
154
  Polynomial reductor = g*(LM()/g.LM());
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
155 156 157 158 159 160 161
  reductor.set_monic();
  reductor *= LC();
  operator-=(reductor);
  return true;
}

template<class T>
162
bool Polynomial<T>::istopred(const Polynomial<T> &g) const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
163 164 165 166 167
  if (g.is_zero() || is_zero()) return false;
  return LM().isDivisibleBy(g.LM());
}

template<class T>
168 169
Polynomial<T> Polynomial<T>::operator*(const Monomial &m) const {
  Polynomial R;
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
170
  R.vp.insert(R.vp.begin(), this->vp.begin(), this->vp.end());
171
  for (typename std::list<std::pair<Monomial, T> >::iterator it = \
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
172 173 174 175 176 177
      R.vp.begin(); it != R.vp.end(); ++it)
    (it->first) *= m;
  return R;
}

template<class T>
178
void Polynomial<T>::operator+=(const Polynomial<T> &p2) {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
179 180 181 182 183
  vp.merge(p2.vp);
  combine_LM();
}

template<class T>
184 185
void Polynomial<T>::operator*=(const T &e) {
  for (typename std::list<std::pair<Monomial, T> >::iterator it = vp.begin(); \
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
186 187 188 189 190
      it != vp.end(); ++it)
    it->second *= e;
}

template<class T>
191 192
void Polynomial<T>::operator/=(const T& e) {
  for (typename std::list<std::pair<Monomial, T> >::iterator it = vp.begin(); \
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
193 194 195 196 197
      it != vp.end(); ++it)
    it->second /= e;
}

template <class T>
198 199 200
void Polynomial<T>::operator-=(const Polynomial<T>& p2) {
  typename std::list<std::pair<Monomial, T> >::iterator it = vp.begin();
  typename std::list<std::pair<Monomial, T> >::const_iterator it2 = \
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
201
       p2.vp.begin();
202
  typename std::list<std::pair<Monomial, T> >::const_iterator it2end = \
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
       p2.vp.end();

  while (it2 != p2.vp.end()) {
    if (it == vp.end()) {
      vp.insert(it, it2, it2end);
      while (it != vp.end()) {
        it->second.neg(); ++it;
      }
      break;
    } else if (it->first < it2->first) {
      ++it;
    } else if (it->first == it2->first) {
      if (it->second == it2->second) {
        it = vp.erase(it);
        ++it2;
      } else {
        it->second -= it2->second;
        ++it2;
      }
    } else {
      it = vp.insert(it, *it2);
      it->second.neg();
      ++it2;
    }
  }
}

template <class T>
231
void Polynomial<T>::print(std::ostream& o) const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
232 233 234 235 236 237 238 239 240
  for (auto& it : std::as_const(vp)) {
    o << it.second << " ";
    it.first.print(o);
    o << std::endl;
  }
  o << ";";
}

template <class T>
241
std::ostream& operator<<(std::ostream& o, const Polynomial<T>& p) {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
242 243 244 245
  if (p.is_zero()) {
    o << "0";
    return o;
  }
246
  typename std::list<std::pair<Monomial, T> >::const_iterator it = p.vp.begin();
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
247 248 249 250 251 252 253 254 255 256 257 258
  if (it == p.vp.end())
    return o;
  if (it->second != T(0))
    o << it->second << it->first;
  ++it;
  for (; it != p.vp.end() ; ++it)
    if (it->second != T(0))
      o << "+" << it->second << it->first;
  return o;
}

template <class T>
259
inline bool Polynomial<T>::operator<(const Polynomial<T>& P) const {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
260 261
  return LM() < P.LM();
}
262
}  // namespace tinygb
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
263

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
264
#endif  // POLYNOMIAL_H_