OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Polynomial.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Martin Duy Tat
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 1. Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  * 3. Neither the name of STFC nor the names of its contributors may be used to
12  * endorse or promote products derived from this software without specific
13  * prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <cmath>
29 #include <iostream>
30 #include "Polynomial.h"
31 
32 namespace polynomial {
33 
35  coefficients_m.push_back(0);
36  maxXorder_m = 0;
37 }
38 
39 Polynomial::Polynomial(const std::vector<int> &coefficients) {
40  if (coefficients.size() == 0) {
41  coefficients_m.push_back(0);
42  return;
43  }
44  coefficients_m = coefficients;
45  maxXorder_m = coefficients_m.size() - 1;
46 }
47 
50  maxXorder_m = coefficients_m.size() - 1;
51 }
52 
54 }
55 
57  maxXorder_m = poly.maxXorder_m;
59  return *this;
60 }
61 
63  if (maxXorder_m == 0 && coefficients_m[0] == 0) {
64  return;
65  }
66  if (maxXorder_m == 0 && coefficients_m[0] != 0) {
67  coefficients_m[0] = 0;
68  return;
69  }
70  for (std::size_t i = 0; i < maxXorder_m; i++) {
71  coefficients_m[i] = coefficients_m[i + 1] * (i + 1);
72  }
73  coefficients_m.pop_back();
74  maxXorder_m--;
75 }
76 
78  if (maxXorder_m == 0 && coefficients_m[0] == 0) {
79  return;
80  }
81  if (poly.maxXorder_m == 0 && poly.coefficients_m[0] == 0) {
82  setZero();
83  return;
84  }
85  std::vector<int> newPoly(maxXorder_m + poly.maxXorder_m + 1, 0);
86  for (std::size_t i = 0; i <= maxXorder_m; i++) {
87  for (std::size_t j = 0; j <= poly.maxXorder_m; j ++) {
88  newPoly[i + j] = newPoly[i + j] +
89  coefficients_m[i] * poly.coefficients_m[j];
90  }
91  }
92  coefficients_m = newPoly;
93  maxXorder_m = coefficients_m.size() - 1;
94 }
95 
97  std::cout << coefficients_m[0];
98  for (std::size_t i = 1; i <= maxXorder_m; i++) {
99  if (coefficients_m[i] >= 0) {
100  std::cout << " + " << coefficients_m[i] << "x^" << i;
101  } else {
102  std::cout << " - " << -coefficients_m[i] << "x^" << i;
103  }
104  }
105 }
106 
108  if (poly.maxXorder_m > maxXorder_m) {
109  maxXorder_m = poly.maxXorder_m;
110  coefficients_m.resize(maxXorder_m + 1, 0);
111  }
112  for (std::size_t i = 0; i <= poly.maxXorder_m; i++) {
113  coefficients_m[i] = coefficients_m[i] + poly.coefficients_m[i];
114  }
115 }
116 
117 void Polynomial::setCoefficient(const int &coefficient,
118  const std::size_t &order) {
119  if (order > maxXorder_m) {
120  coefficients_m.resize(order + 1, 0);
121  maxXorder_m = coefficients_m.size() - 1;
122  }
123  coefficients_m[order] = coefficient;
124 }
125 
126 double Polynomial::evaluatePolynomial(const double &x) const {
127  double result = 0.0;
128  std::size_t i = maxXorder_m + 1;
129  while (i != 0) {
130  i--;
131  result = result * x + coefficients_m[i];
132  }
133  return result;
134 }
135 
136 }
void addPolynomial(const Polynomial &poly)
Definition: Polynomial.cpp:107
std::vector< int > coefficients_m
Definition: Polynomial.h:116
void setCoefficient(const int &coefficient, const std::size_t &order)
Definition: Polynomial.cpp:117
void multiplyPolynomial(const Polynomial &poly)
Definition: Polynomial.cpp:77
void printPolynomial() const
Definition: Polynomial.cpp:96
Polynomial & operator=(const Polynomial &poly)
Definition: Polynomial.cpp:56
double evaluatePolynomial(const double &x) const
Definition: Polynomial.cpp:126
std::size_t maxXorder_m
Definition: Polynomial.h:115