polynomial_in_matrix.h 2.75 KB
Newer Older
SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
1 2
#ifndef __POLYNOMIAL_IN_MATRIX_H__
#define __POLYNOMIAL_IN_MATRIX_H__
3 4 5 6 7

#include <map>
#include <set>
#include <utility>
#include <vector>
SPAENLEHAUER Pierre-Jean's avatar
update  
SPAENLEHAUER Pierre-Jean committed
8
#include "./common.h"
9
#include "finite_field_arithmetic/givaro_wrapper.h"
10 11
#include "./monomial.h"
#include "./polynomial.h"
12
#include "linalg/storage_matrix.h"
13 14 15

namespace tinygb {

SPAENLEHAUER Pierre-Jean's avatar
SPAENLEHAUER Pierre-Jean committed
16
class PolynomialInMatrix : public AbstractPolynomial<GFp_Givaro, PolynomialInMatrix> {
17
 public:
18
  PolynomialInMatrix() : pMatrix_(nullptr), row_(0), nbMonoms_(0) {}
19 20

  PolynomialInMatrix(const std::shared_ptr<const StorageMatrix>& pMatrix, std::size_t Row) 
21 22 23
    : pMatrix_(pMatrix), row_(Row) {
      nbMonoms_ = 1;
      for (std::size_t j = 0; j < pMatrix_->column_size(); ++j) {
24
        if (!(*pMatrix)(row_, j).is_zero())
25 26
          nbMonoms_++;
      }
27
    }
28

29
  PolynomialInMatrix(const PolynomialInMatrix &p) 
30
    : pMatrix_(p.pMatrix_), row_(p.row_), nbMonoms_(p.nbMonoms_) {}
31

32 33 34 35 36 37 38
  PolynomialInMatrix& operator=(const PolynomialInMatrix &p) {
    pMatrix_  = p.pMatrix_;
    row_      = p.row_;
    nbMonoms_ = p.nbMonoms_;
    return *this;
  }

39
  inline Monomial LeadingMonomial() const {
40
    return pMatrix_->LeadingMonomial(row_);
41 42
  }

43
  Polynomial to_polynomial() const;
44

45 46 47 48
  // FIXME(pj): this only works for degree orderings.
  inline unsigned Degree() const {
    return LeadingMonomial().Degree();
  } 
49

50 51
  // Returns true if the object is top reducible by one of the polynomials in
  // the argument.
52
  bool IsTopReducible(const std::vector<PolynomialInMatrix>&) const;
53

54
  // Converts a StorageMatrix to the vector of polynomials
55
  // corresponding to the list of Rows of the matrix
56
  static std::vector<PolynomialInMatrix>
57
    MakeSystemOfPolynomialsInMatrix(const std::shared_ptr<const StorageMatrix>&);
58

59 60 61 62
  // Merges the first argument with the set of monomials occuring with nonzero
  // coefficient in the polynomial (with the exception of the leading monomial)
  // multiplied by the second argument.
  void AddMonomialsMultipliedByCofactor(std::set<Monomial>&, const Monomial&) const;
63 64

  // Returns the number of monomials with nonzero coefficient in the polynomial.
65
  std::size_t NumberOfMonomials() const { return nbMonoms_; }
66

67 68 69 70
  // Returns the list of monomials which occur with nonzero coefficient in the
  // polynomial.
  std::list<Monomial> GetMonomials() const;

71 72
  std::shared_ptr<const StorageMatrix> pMatrix() const { return pMatrix_; }
  std::size_t row() const { return row_; }
73

74
 private:
75 76 77
  std::shared_ptr<const StorageMatrix> pMatrix_;
  std::size_t row_;
  std::size_t nbMonoms_;
78

79
  friend std::ostream& operator<<(std::ostream&, const PolynomialInMatrix&);
80

81 82
  friend bool operator<(const std::pair<Monomial, PolynomialInMatrix>&,
                        const std::pair<Monomial, PolynomialInMatrix>&);
83 84 85 86 87
};

}  // namespace tinygb

#endif  // POLYNOMIAL_IN_MATRIX_H_