Symmetric Polynomials  V3.1
A C++ library for generating symmetric polynomials with relations
Polynomials.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "General.hpp"
3 #include "impl/Details.ipp"
4 #include <map>
5 #include <unordered_map>
6 #include <string>
7 #include <iostream>
8 
11 
12 namespace symmp
13 {
27  template <class exp_t>
28  class BaseContainer {
29  typedef typename exp_t::deg_t deg_t;
30  protected:
31 
34  BaseContainer(const deg_t* dimensions = nullptr);
35 
39  deg_t compute_degree(const exp_t& exponent) const;
40 
41  const deg_t* dimensions;
42  };
43 
60  template <class _scl, class _exp, template<class...> class _cnt, bool _ord, class ... _arg>
61  class DefaultContainer : public BaseContainer<_exp>, implementation_details::container_wrapper<_scl, _exp, _cnt, _ord, _arg...> {
62  private:
63  typedef typename implementation_details::container_wrapper<_scl, _exp, _cnt, _ord, _arg...> data_t;
64  protected:
65  typedef _scl scl_t;
66  typedef _exp exp_t;
67  typedef typename _exp::deg_t deg_t;
68  public:
70  size_t number_of_monomials() const;
71 
73  bool operator==(const DefaultContainer&) const;
74 
76  bool operator!=(const DefaultContainer& b) const;
77 
81  void reserve(size_t n);
82 
88  void insert(const exp_t& exp, scl_t coeff);
89 
91  class ConstIterator : public data_t::const_iterator {
92  public:
93  scl_t coeff() const;
94  const exp_t& exponent() const;
95  deg_t degree() const;
96  private:
97  ConstIterator(typename data_t::const_iterator);
98  friend class DefaultContainer;
99  };
101  ConstIterator end() const;
102 
107 
108  protected:
111  class Iterator : public data_t::iterator {
112  public:
114  private:
115  Iterator(typename data_t::iterator);
116  friend class DefaultContainer;
117  };
118 
121 
124  void add(const std::pair<const std::pair<deg_t, exp_t>, scl_t>& kvp);
125 
128  void subtract(const std::pair<const std::pair<deg_t, exp_t>, scl_t>& kvp);
129 
133  void multiply_add(const std::pair<const std::pair<deg_t, exp_t>, scl_t >& kvp1, const std::pair<const std::pair <deg_t, exp_t>, scl_t >& kvp2);
134  private:
135  void add(const std::pair<deg_t, exp_t> key, scl_t value);
136  };
137 
138 
147  template <typename container_t>
148  class Polynomial : public container_t
149  {
150  public:
151  typedef typename container_t::scl_t scl_t;
152  typedef typename container_t::exp_t exp_t;
153  typedef typename container_t::deg_t deg_t;
154 
158  Polynomial(const deg_t* dim_var = nullptr, const std::string* name_var = nullptr);
159 
166  Polynomial(const exp_t& exp, scl_t coeff, const deg_t* dim_var = nullptr, const std::string* name_var = nullptr);
167 
174  Polynomial(int num_var, scl_t coeff, const deg_t* dim_var = nullptr, const std::string* name_var = nullptr);
175 
179  size_t number_of_variables() const;
180 
186 
192 
198 
204 
208  Polynomial operator+(const Polynomial& other) const;
209 
213  Polynomial operator-(const Polynomial& other) const;
214 
218  Polynomial operator*(const Polynomial& other) const;
219 
227  template <class T = int>
228  Polynomial operator^(T p) const;
229 
230  private:
231  const std::string* variable_names; //if exp_t does not have the appropriate method
232  template <class fun>
233  void print(scl_t, const exp_t&, std::ostream&, const fun&) const; //Print monomial using given variable names
234  template <class fun>
235  void print(std::ostream& os, const fun&) const; //Print polynomial using given variable names
236  template<class cont>
237  friend std::ostream& operator<< (std::ostream&, const Polynomial<cont>&);
238  };
239 
245  template <class container_t>
246  std::ostream& operator<<(std::ostream& os, const Polynomial<container_t>& a);
247 
252  template <class _scl, class _exp, bool _ord = 1>
254 
255 }
256 #include "impl/Polynomials.ipp"
Contains general operations on vectors: hashing, computing degrees.
The base of all monomial containers.
Definition: Polynomials.hpp:28
BaseContainer(const deg_t *dimensions=nullptr)
Constructor given pointer to dimensions of the variables.
const deg_t * dimensions
The pointer to the dimensions of the variables.
Definition: Polynomials.hpp:41
deg_t compute_degree(const exp_t &exponent) const
Computes degree of given exponent.
Constant iterator traversing the monomials of a polynomial.
Definition: Polynomials.hpp:91
scl_t coeff() const
The coefficient of the monomial.
const exp_t & exponent() const
The exponent of the monomial.
deg_t degree() const
The degree of the monomial.
Non const iterator traversing the monomials of a polynomial.
Definition: Polynomials.hpp:111
scl_t & coeff()
Reference to the coefficient of the monomial.
The default ordered/unordered monomial container.
Definition: Polynomials.hpp:61
ConstIterator highest_term() const
ConstIterator to the highest term monomial.
void insert(const exp_t &exp, scl_t coeff)
Inserts monomial in polynomial.
Iterator begin()
Iterator to the first monomial.
void subtract(const std::pair< const std::pair< deg_t, exp_t >, scl_t > &kvp)
Subtracts given monomial from polynomial.
size_t number_of_monomials() const
Returns number of monomials in the polynomial.
_exp::deg_t deg_t
The degree type eg size_t.
Definition: Polynomials.hpp:67
ConstIterator end() const
ConstIterator to just after the final monomial.
void add(const std::pair< const std::pair< deg_t, exp_t >, scl_t > &kvp)
Adds given monomial to polynomial.
_scl scl_t
The scalar (coefficient) type eg int64_t.
Definition: Polynomials.hpp:65
bool operator!=(const DefaultContainer &b) const
Inequality of polynomials.
bool operator==(const DefaultContainer &) const
Equality of polynomials.
void multiply_add(const std::pair< const std::pair< deg_t, exp_t >, scl_t > &kvp1, const std::pair< const std::pair< deg_t, exp_t >, scl_t > &kvp2)
Multiplies the two given monomials and then adds the product to polynomial.
Iterator end()
Iterator to just after the final monomial.
ConstIterator begin() const
ConstIterator to the first monomial.
_exp exp_t
The exponent (variable) type eg StandardVariables or HalfIdempotentVariables.
Definition: Polynomials.hpp:66
void reserve(size_t n)
Reserve number of monomials in polynomial.
Class for polynomials in multiple variables with relations.
Definition: Polynomials.hpp:149
Polynomial(const deg_t *dim_var=nullptr, const std::string *name_var=nullptr)
Constructs zero polynomial.
Polynomial(const exp_t &exp, scl_t coeff, const deg_t *dim_var=nullptr, const std::string *name_var=nullptr)
Constructs polynomial with a single nonzero monomial term.
Polynomial & operator+=(const Polynomial &other)
Addition assignment.
Polynomial & operator*=(const Polynomial &other)
Multiplication assignment.
container_t::exp_t exp_t
The exponent (variable) type eg StandardVariables or HalfIdempotentVariables.
Definition: Polynomials.hpp:152
Polynomial & operator-=(const Polynomial &other)
Subtraction assignment.
size_t number_of_variables() const
Returns the number of variables of the polynomial.
Polynomial operator^(T p) const
Raises polynomial to integer power.
container_t::scl_t scl_t
The scalar (coefficient) type eg int64_t.
Definition: Polynomials.hpp:151
Polynomial & operator*=(scl_t scalar)
Scalar multiplication assignment.
friend std::ostream & operator<<(std::ostream &, const Polynomial< cont > &)
Befriending the print to ostream function.
Polynomial operator+(const Polynomial &other) const
Addition of polynomials.
Polynomial operator-(const Polynomial &other) const
Subtraction of polynomials.
Polynomial operator*(const Polynomial &other) const
Multiplication of polynomials.
Polynomial(int num_var, scl_t coeff, const deg_t *dim_var=nullptr, const std::string *name_var=nullptr)
Constructs constant nonzero polynomial.
container_t::deg_t deg_t
The degree type eg size_t.
Definition: Polynomials.hpp:153
The namespace which contains every method and class in the library.
Definition: General.hpp:16
std::ostream & operator<<(std::ostream &os, const Polynomial< container_t > &a)
Prints polynomial to output stream.