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
PolynomialMultiplicationSimple.ipp
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 //
18 #ifndef _POLYNOMIALMULTIPLICATIONSIMPLE_IPP_
19 #define _POLYNOMIALMULTIPLICATIONSIMPLE_IPP_
20 #include "../include/PolynomialMultiplicationSimple.hpp"
21 #include "../include/Polynomial.hpp"
22 #include <algorithm>
23 #include <iterator>
24 
25 template <typename FieldT>
27 {
28  Polynomial<FieldT> result;
29  // result_deg = deg_p1 + deg_p2 = size_p1-1 + size_p2-1;
30  // result_size = result_deg +1 = size_p1 + size_p2 - 1;
31  // return if size_p1 or size_p2 is zero, ie, input is empty.
32  if ((p1.size() ==0) || (p2.size() == 0)) {
33  return result;
34  }
35  size_t result_size = p1.size() + p2.size() -1;
36  // r_j = p_i * q_j-i ; i : 0 to j for j <= size_q
37  // r_j = p_i * q_
38  // ex: p = p0 p1 p2 p3 ; q= q0 q1 q2
39  // r0 = p0*q0
40  // r1 = p0*q1 + p1*q0
41  // r2 = p0*q2 + p1*q1 + p2*q0
42  // r3 = p0*q3 (ooops!) q3 does not exit
43  //
44  // r0 = q0*p0
45  // r1 = q0*p1 + q1*p0
46  // r2 = q0*p2 + q1*p1 + q2*p0
47  // r3 = q0*p3
48  const std::list<FieldT>* pptr = &(this->getPolynomialCoefficients(p2));
49  const std::list<FieldT>* qptr = &(this->getPolynomialCoefficients(p1));
50 
51  if (p1.size() >= p2.size()) {
52  pptr = &(this->getPolynomialCoefficients(p1));
53  qptr = &(this->getPolynomialCoefficients(p2));
54  }
55  const std::list<FieldT>& p = *pptr;
56  const std::list<FieldT>& q = *qptr;
57  size_t deg_p = p.size() -1;
58  size_t deg_q = q.size() -1;
59  auto convolve = [&](typename std::list<FieldT>::const_iterator q_begin, typename std::list<FieldT>::const_iterator q_end, typename std::list<FieldT>::const_iterator p_begin)
60  {
61  result.appendTerm(0);
62  std::list<FieldT>& result_coeffs = this->getPolynomialCoefficients(result);
63  auto rj_it = (result_coeffs).rbegin();
64  for(;q_begin!=q_end;) {
65  *rj_it += (*q_begin)*(*p_begin);
66  p_begin--;
67  q_begin++;
68  }
69  };
70 
71  typename std::list<FieldT>::const_iterator p_it_begin = p.begin();
72  for(size_t j = 0; j < result_size; ++j) {
73  if (j<=deg_p) {
74  typename std::list<FieldT>::const_iterator q_it_begin = q.begin();
75  typename std::list<FieldT>::const_iterator q_it_end = std::next(q.begin(), std::min(j,deg_q)+1); // next to last
76  convolve(q_it_begin,q_it_end, p_it_begin);
77  p_it_begin++;
78  }
79  else { //j > deg_p
80  typename std::list<FieldT>::const_iterator q_it_begin = std::next(q.begin(),j-deg_p);
81  typename std::list<FieldT>::const_iterator q_it_end = q.end(); // next to last
82  p_it_begin = std::prev(p.end(),1);
83  convolve(q_it_begin,q_it_end, p_it_begin);
84  }
85  }
86  return result;
87 
88 }
89 
90 
91 #endif // _POLYNOMIALMULTIPLICATIONSIMPLE_IPP_
92 
93 
virtual Polynomial< FieldT > multiply(const Polynomial< FieldT > &p1, const Polynomial< FieldT > &p2) override
The method which should be overloaded by the derived class to implement its own multiplication algori...
void appendTerm(FieldT coeff)
Append new terms by pushing back coefficients.
size_t size()
Definition: Polynomial.hpp:155
Contains the definition for the abstract base class which will be used by different multiplication al...
Definition: Polynomial.hpp:40