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_testutils.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 _POLYNOMIAL_TESTUTILS_IPP
19 #define _POLYNOMIAL_TESTUTILS_IPP
20 
21 #include "../include/Polynomial.hpp"
22 #include "../include/Matrix.ipp"
23 #include <limits>
24 #include <string>
25 #include <utility>
26 #include <regex>
27 #include <fstream>
28 #include <stdexcept>
29 template <typename FieldT>
30  bool operator==(const Polynomial<FieldT>& lhs, const Polynomial<FieldT>& rhs)
31 {
32  if (lhs.size() != rhs.size()) {
33  return false;
34  }
35  for(size_t i = 0; i < lhs.size(); ++i){
36  if (lhs[i] != rhs[i]) {
37  return false;
38  }
39  }
40  return true;
41 }
42 
43 
44 
45 bool operator==(const Polynomial<double>& lhs, const Polynomial<double>& rhs)
46 {
47  if (lhs.size() != rhs.size()) {
48  return false;
49  }
50  auto areSame = [](double a, double b)->bool {
51  //return std::fabs(a-b) < std::numeric_limits<double>::epsilon();
52  return std::fabs(a-b) < double(1e-6);
53  };
54  for(size_t i = 0; i < lhs.size(); ++i){
55  if (!areSame(lhs[i],rhs[i])) {
56  return false;
57  }
58  }
59  return true;
60 
61 }
62 
65 std::pair<size_t,size_t> readInputSize(const std::string filename)
66 {
67  /* Matrix dimension */
68  std::regex matrixDimensionRegex ("MatDim(\\d+)");
69  std::smatch matrixDimensionMatch;
70  if(!std::regex_search(filename,matrixDimensionMatch,matrixDimensionRegex))
71  {
72  throw std::invalid_argument("readInputSize: The filename does not have pattern \"MatDim#\" ");
73  }
74  std::regex polyDegreeRegex("PolyDegree(\\d+)");
75  std::smatch polyDegreeMatch;
76  if(!std::regex_search(filename,polyDegreeMatch,polyDegreeRegex)) {
77  throw std::invalid_argument("readInputSize: The filename doesn not have pattern \"PolyDegree#\" ");
78  }
79  return std::make_pair(std::stoul(matrixDimensionMatch[1]),std::stoul(polyDegreeMatch[1]));
80 
81 }
82 
85 template <typename FieldT>
86 FieldT stringToField(const std::string& s);
89 template<>
90 double stringToField(const std::string& s)
91 {
92  return std::stod(s);
93 }
94 
95 
98 template<typename FieldT>
99  void createMatrixWithPolynomialEntries(const std::string& inputfile,Matrix<Polynomial<FieldT>>* pmat) {
100  if(pmat==nullptr) throw std::invalid_argument("createMatrixWithPolynomialEnties: Pointer to matrix is null");
101  std::pair<size_t,size_t> matrixDim_PolyDegree_pair = readInputSize(inputfile);
102  size_t matrixDim=matrixDim_PolyDegree_pair.first;
103  size_t PolyDegree=matrixDim_PolyDegree_pair.second;
104  using EntryT = Polynomial<FieldT>;
105 
106  /* entries in the file are linearly indexed in row major order */
107  std::ifstream file (inputfile);
108  if(!file.is_open()) throw std::invalid_argument("createMatrixWithPolynomialEntries: Failed to open inputfile");
109  size_t rowNumber=0;
110  for(size_t entryNumber =0; entryNumber < matrixDim*matrixDim ; ) {
111  for (size_t columnNumber=0;columnNumber<matrixDim;++columnNumber) {
112  std::string line;
113  getline(file,line); // read one line, ie coeffs of one polynomial
114  std::stringstream linestream(line);
115 
116  /* read all entries of a line */
117  EntryT ijth_polynomial;
118  for (size_t i_coef =0; i_coef < PolyDegree+1 ;++i_coef) {
119  std::string coef;
120  getline(linestream,coef,',');
121  ijth_polynomial.appendTerm(stringToField<FieldT>(coef));
122  }
123  /* Place the polynomial in the matrix */
124  (*pmat)(rowNumber,columnNumber)=ijth_polynomial;
125  ++entryNumber;
126  }
127  rowNumber++;
128  entryNumber++;
129  }
130 }
131 
132 template <typename FieldT>
133 Polynomial<FieldT> readPolynomialFromFile(const std::string& resultfile)
134 {
135  std::ifstream file (resultfile);
136  if(!file.is_open()) throw std::invalid_argument("readPolynomialFromFile: Failed to open resultfile");
137  std::string line;
139  while(getline(file,line)) {
140  p.appendTerm(stringToField<FieldT>(line));
141  }
142  return std::move(p);
143 }
144 #endif // _POLYNOMIAL_TESTUTILS_H
145 
146 
147 
148 
149 
void appendTerm(FieldT coeff)
Append new terms by pushing back coefficients.
Polynomial< FieldT > readPolynomialFromFile(const std::string &resultfile)
boost::numeric::ublas::matrix< EntryT > Matrix
Typedef for the main matrix class.
Definition: Matrix.hpp:24
FieldT stringToField(const std::string &s)
Function to convert string to various number types or coef types. Specialize for every supported type...
size_t size()
Definition: Polynomial.hpp:155
std::pair< size_t, size_t > readInputSize(const std::string filename)
Function to read the size of matrix and polynomial entries from filename of the input file...
void createMatrixWithPolynomialEntries(const std::string &inputfile, Matrix< Polynomial< FieldT >> *pmat)
Read the input and output files and create the matrix of polynomials.
Contains the definition for the abstract base class which will be used by different multiplication al...
Definition: Polynomial.hpp:40
bool operator==(const Polynomial< FieldT > &lhs, const Polynomial< FieldT > &rhs)