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 12 13 14
template<class T>
class polynomial;

template<class T>
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
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>
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
18
class polynomial {
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
19
  void combine_LM();
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
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 23 24
 public:
  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

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

  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

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
72 73
  bool topred(const polynomial &g);
  bool istopred(const polynomial &g) const;
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
74 75 76 77 78 79 80
  T coeff(const monomial& m) const;

  polynomial<T> operator*(const monomial&) const;
  void operator+=(const polynomial<T>&);
  void operator-=(const polynomial<T>&);
  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 84
  inline bool operator<(const polynomial<T>& P) const;

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
85 86
  friend std::ostream& operator<< <T>(std::ostream&, const polynomial<T>&);
};
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

#ifdef BILIN
template<class T>
unsigned polynomial<T>::degree_bilin() const {
  unsigned R = 0;
  for (typename std::list<std::pair<monomial, T> >::const_iterator it = \
      vp.begin(); it != vp.end(); ++it)
    if (it->first.degree_bilin() > R)
      R = it->first.degree_bilin();
  return R;
}
#endif


template<class T>
void polynomial<T>::combine_LM() {
  if (vp.empty())
    return;
  typename std::list<std::pair<monomial, T> >::iterator it2;
  for (typename std::list<std::pair<monomial, T> >::iterator it = vp.begin();
      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>
T polynomial<T>::coeff(const monomial& m) const {
  for (typename std::list<std::pair<monomial, T> >::const_iterator it = \
      vp.begin(); it != vp.end(); ++it)
    if (it->first == m)
      return it->second;
  return T(0);
}

template<class T>
void polynomial<T>::set_monic() {
  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>
void polynomial<T>::normalize() {
  vp.sort();
  combine_LM();
}

template<class T>
bool polynomial<T>::topred(const polynomial &g) {
  if (g.is_zero() || is_zero())
    return false;
  if (!LM().isDivisibleBy(g.LM()))
    return false;
  polynomial reductor = g*(LM()/g.LM());
  reductor.set_monic();
  reductor *= LC();
  operator-=(reductor);
  return true;
}

template<class T>
bool polynomial<T>::istopred(const polynomial<T> &g) const {
  if (g.is_zero() || is_zero()) return false;
  return LM().isDivisibleBy(g.LM());
}

template<class T>
polynomial<T> polynomial<T>::operator*(const monomial &m) const {
  polynomial R;
  R.vp.insert(R.vp.begin(), this->vp.begin(), this->vp.end());
  for (typename std::list<std::pair<monomial, T> >::iterator it = \
      R.vp.begin(); it != R.vp.end(); ++it)
    (it->first) *= m;
  return R;
}

template<class T>
void polynomial<T>::operator+=(const polynomial<T> &p2) {
  vp.merge(p2.vp);
  combine_LM();
}

template<class T>
void polynomial<T>::operator*=(const T &e) {
  for (typename std::list<std::pair<monomial, T> >::iterator it = vp.begin(); \
      it != vp.end(); ++it)
    it->second *= e;
}

template<class T>
void polynomial<T>::operator/=(const T& e) {
  for (typename std::list<std::pair<monomial, T> >::iterator it = vp.begin(); \
      it != vp.end(); ++it)
    it->second /= e;
}

template <class T>
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 = \
       p2.vp.begin();
  typename std::list<std::pair<monomial, T> >::const_iterator it2end = \
       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>
void polynomial<T>::print(std::ostream& o) const {
  for (auto& it : std::as_const(vp)) {
    o << it.second << " ";
    it.first.print(o);
    o << std::endl;
  }
  o << ";";
}


template <class T>
std::ostream& operator<<(std::ostream& o, const polynomial<T>& p) {
  if (p.is_zero()) {
    o << "0";
    return o;
  }
  typename std::list<std::pair<monomial, T> >::const_iterator it = p.vp.begin();
  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>
inline bool polynomial<T>::operator<(const polynomial<T>& P) const {
  return LM() < P.LM();
}
263
}  // namespace tinygb
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
264

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