Polymetic  1.1
A c++ library for polynomial and matrix arithmetic, focused on applications in Kinematics.
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Polynomial.hpp
Go to the documentation of this file.
1 // Copyright 2018 Dhruvesh Nikhilkumar Patel
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
17 #ifndef _POLYNOMIAL_HPP_
18 #define _POLYNOMIAL_HPP_
19 
20 #include<list> // for doubly linked list
21 #include <initializer_list> // for init lists arguments
22 #ifdef INTRUSIVE_TESTS
23 #include "gtest/gtest.h"
24 #endif
25 #include <iostream>
26 #include <algorithm>
27 
28 template<typename FieldT>
30 
39 template <typename FieldT>
41 {
42 
43 public:
44  /*********** Constructors **************/
50  Polynomial(std::initializer_list<FieldT>);
56  template <typename InputIt>
57  Polynomial(InputIt first, InputIt last);
58 
59  Polynomial(FieldT c);
62  template <typename convertibleToFieldT>
63  Polynomial (convertibleToFieldT v);
64  Polynomial(const Polynomial& rhs)
65  :m_coefs{rhs.m_coefs},m_size{rhs.m_size}
66  {
67  #ifdef VERBOSE
68  std::cout<<"copy constructor Polynomial(const Polynomial&)"<<std::endl;
69  #endif
70  }
74  {
75  #ifdef VERBOSE
76  std::cout<<"move constructor Polynomial(Polynomial&&)"<<std::endl;
77  #endif
78  swap(*this,rhs);
79  }
81  void appendTerm(FieldT coeff);
82 
83  /************** Operators *************/
88  const FieldT& operator[](size_t i) const;
89  FieldT& operator[](size_t i) ;
90 
91 
97  friend void swap(Polynomial& lhs, Polynomial& rhs) noexcept
98  {
99  std::swap(lhs.m_coefs,rhs.m_coefs);
100  std::swap(lhs.m_size,rhs.m_size);
101  }
102 
103 
107  Polynomial const& operator=(const Polynomial& rhs);
111  Polynomial const& operator=(Polynomial&& rhs);
112 
114  friend Polynomial operator-(Polynomial const& rhs) {
115  #ifdef VERBOSE
116  std::cout<<"friend -(const &)"<<std::endl;
117  #endif
118  Polynomial<FieldT> temp (rhs);
119  temp.minus();
120  return temp;
121  }
122  friend Polynomial operator-(Polynomial&& temp) {
123  #ifdef VERBOSE
124  std::cout<<"friend -(&&)"<<std::endl;
125  #endif
126  temp.minus();
127  return std::move(temp);
128  }
131  const Polynomial& operator+=(Polynomial const& p2) ;
134  const Polynomial& operator+=(Polynomial&& p2) ;
137  const Polynomial& operator-=(Polynomial const& p2) ;
140  const Polynomial& operator-=(Polynomial&& p2) ;
141 
144  Polynomial operator+(const Polynomial& p2);
145  Polynomial operator+(Polynomial p2) const;
146 
149  Polynomial operator-(const Polynomial& p2);
150  Polynomial operator-(Polynomial p2) const;
151 
152 
153 public:
154  /********** getters **********/
155  size_t size() {return m_size;}
156  size_t size() const {return m_size;}
157  friend std::ostream& operator<< (std::ostream& os, const Polynomial& p)
158  {
159  size_t pow=0;
160  os<<"(";
161  std::for_each(p.m_coefs.begin(),p.m_coefs.end(), [&](const FieldT& coef) {os<< "+ "<<coef <<"x^"<<pow <<" ";++pow; });
162  os<<")";
163  return os;
164  }
165 private:
169  std::list<FieldT> m_coefs;
170  size_t m_size;
172  /*********** Helpers ********/
175  void trim();
176 
177 
179  Polynomial& minus();
180 
181 
182  /************ Friend classes ************/
183  friend class PolynomialMultiplicationInterface<FieldT>;
184 
185 #ifdef INTRUSIVE_TESTS
186 public:
187  friend class PolynomialTest;
188  FRIEND_TEST(PolynomialTest,helper_trim);
189  FRIEND_TEST(PolynomialTest,helper_minus);
190  friend class PolynomialTestMPZ;
191  FRIEND_TEST(PolynomialMPZTest,helper_trim);
192  FRIEND_TEST(PolynomialMPZTest,helper_minus);
193 #endif
194 
195 };
196 //namespace PolynomialNS {
197 // /*! \brief Brief function description here
198 // * \param lhs lhs+=rhs
199 // * \param rhs lhs+=rhs
200 // */
201 // template <typename FieldT>
202 // FieldT& add_assign(FieldT& lhs,const FieldT& rhs) {
203 // return lhs+=rhs;
204 // }
205 // /*! \brief Brief function description here
206 // * \param lhs lhs-=rhs
207 // * \param rhs lhs-=rhs
208 // */
209 // template <typename FieldT>
210 // FieldT& subtract_assign(FieldT& lhs,const FieldT& rhs) {
211 // return lhs-=rhs;
212 // }
213 //}
214 template <typename T>
215 T minus(T v) {
216  return -v;
217 }
218 
221 template<typename FieldT>
223 
224 
225 
226 #endif //_POLYNOMIAL_HPP_
T minus(T v)
Brief function description here.
Definition: Polynomial.hpp:215
Polynomial< FieldT > operator*(const Polynomial< FieldT > &p1, const Polynomial< FieldT > &p2)
The multiplicaion operator. It picks the algorithm with which the Polynomial class is instantiated...
void appendTerm(FieldT coeff)
Append new terms by pushing back coefficients.
std::list< FieldT > m_coefs
Definition: Polynomial.hpp:169
Polynomial()
The default constructor to make the class default constructible.
Definition: Polynomial.hpp:47
friend std::ostream & operator<<(std::ostream &os, const Polynomial &p)
Definition: Polynomial.hpp:157
Polynomial operator+(const Polynomial &p2)
operator
friend Polynomial operator-(Polynomial const &rhs)
The unary (-) operator.
Definition: Polynomial.hpp:114
size_t size()
Definition: Polynomial.hpp:155
const FieldT & operator[](size_t i) const
Operator to fetch coefficients. Checks range.
friend Polynomial operator-(Polynomial &&temp)
Definition: Polynomial.hpp:122
Polynomial(Polynomial &&rhs)
Move constructor implemented using swap.
Definition: Polynomial.hpp:73
friend void swap(Polynomial &lhs, Polynomial &rhs) noexcept
A non-template friend swap to aid in the implementation of copy-and-swap.
Definition: Polynomial.hpp:97
Polynomial and related classes.
Definition: Polynomial.hpp:29
const Polynomial & operator+=(Polynomial const &p2)
The += operator for polynomials.
size_t m_size
Definition: Polynomial.hpp:170
size_t size() const
Definition: Polynomial.hpp:156
void trim()
Use to maintain the invariant 1.
Polynomial(const Polynomial &rhs)
Definition: Polynomial.hpp:64
Polynomial & minus()
Flip the sign of all coefs.
Contains the definition for the abstract base class which will be used by different multiplication al...
Definition: Polynomial.hpp:40
const Polynomial & operator-=(Polynomial const &p2)
The -= operator for polynomials.
Polynomial const & operator=(const Polynomial &rhs)
The assignemnt operator.