polynomial.h 2.71 KB
Newer Older
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (C) 2016 tinygb
 * Written by <pierre-jean.spaenlehauer@inria.fr>
 *
 * ========LICENCE========
 * This file is part of tinygb.
 *
 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
20 21 22 23 24 25 26 27 28 29 30 31 32
 * ========LICENCE========
 *
 */

#ifndef POLYNOMIAL_H_
#define POLYNOMIAL_H_

#include <list>
#include "param.h"
#include "monomial.h"

//////////////////////////// class definitions //////////////////////////////

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
33 34
// polynomial is a class encoding polynomials in sparse representation, i.e. as
// a list of pairs <coeff, monomial>
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
35 36 37 38 39

template<class T>
class polynomial;

template<class T>
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
40
std::ostream& operator<<(std::ostream &o, const polynomial<T> &p);
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

template<class T>
class polynomial
{
  void combine_LM();
  std::list<std::pair<monomial,T> > vp;

public:

  polynomial() 
  {
    vp = std::list<std::pair<monomial,T> >();
  }
  
  polynomial(std::list<std::pair<monomial,T> > _vp) : vp(_vp){}

  const std::list<std::pair<monomial,T> >& get_vp() const 
  {
    return vp;
  }

  void normalize();  
  
  void clear() 
  {
    vp.clear();
  }
  
  void insert(monomial m, T e) 
  {
    vp.push_back(std::pair<monomial, T>(m,e));
  }
  
  const monomial& LM() const 
  {
    assert(!vp.back().second.is_zero());
    return vp.back().first;
  }

  const T& LC() const 
  {
    return vp.back().second;
  }

  void set_monic();
  unsigned size() const 
  {
    return vp.size();
  }
  
  bool is_zero() const 
  {
    return vp.empty();
  }

  unsigned degree() const 
  {
    return LM().degree();
  }

#ifdef BILIN
  unsigned degree_bilin() const;
#endif

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
105 106
  bool topred(const polynomial &g);
  bool istopred(const polynomial &g) const;
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
  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&);
  void print(std::ostream&) const;

  //human readable formatting
  friend std::ostream& operator<< <T>(std::ostream&, const polynomial<T>&);
};

#include "polynomial.tpp"
#endif