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
demo.cpp
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 #include "../include/Polynomial.hpp"
18 #include <iostream>
19 #include <string>
20 #include <stdexcept>
21 
22 void print_help() {
23  std::cout<< "Enter number of coefficients followed by the values for two polynomials. For example:" <<std::endl;
24  std::cout<< "if p1 = 2x^3 + x and p2 = x^2 - 10x + 1,\nthen enter 4 0 1 0 2 3 1 -10 1" <<std::endl;
25 }
26 
27 int main(int argc, char* argv[])
28 {
29 
30  /* use field as double for now */
31  using FieldType = double;
32  /* process the inputs */
35  if ( argc > 2) {
36  try {
37  size_t number_of_coeffs_p1 = std::stoul(argv[1]);
38  size_t idx = 1;
39  if ((size_t)argc < idx + number_of_coeffs_p1 +1) throw std::invalid_argument("Invalid number of inputs supplied");
40  for (size_t i= 0; i < number_of_coeffs_p1; ++i) {
41  p1.appendTerm(std::stod(argv[++idx]));
42  }
43 
44  size_t number_of_coeffs_p2 = std::stoul(argv[++idx]);
45  if ((size_t)argc < idx + number_of_coeffs_p2 + 1) throw std::invalid_argument("Invalid number of inputs supplied");
46  for (size_t i= 0; i < number_of_coeffs_p2; ++i) {
47  p2.appendTerm(std::stod(argv[++idx]));
48  }
49  }
50  catch (std::invalid_argument& e) {
51  std::cout << e.what() <<std::endl;
52  print_help();
53  return 1;
54  }
55  }
56  else {
57  print_help();
58  }
59 
60  /* add the polynomial */
61  Polynomial<FieldType> padd = p1+p2;
62  std::cout<<"Addition:p1+p2\n"<<padd<<std::endl;
63  /* subtract the polynomial */
64  Polynomial<FieldType> psub = p1-p2;
65  std::cout<<"Subtraction:p1-p2\n"<<psub<<std::endl;
66 
67  return 0;
68 }
void appendTerm(FieldT coeff)
Append new terms by pushing back coefficients.
void print_help()
This file demonstrates the top-level funtionalities of the Polynomial class.
Definition: demo.cpp:22
Contains the definition for the abstract base class which will be used by different multiplication al...
Definition: Polynomial.hpp:40
int main(int argc, char *argv[])
Definition: demo.cpp:27