OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Polynomial.h
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 #ifndef POLYNOMIAL_H
29 #define POLYNOMIAL_H
30 
47 #include <vector>
48 
49 namespace polynomial {
50 
51 class Polynomial {
52 public:
54  Polynomial();
62  explicit Polynomial(const std::vector<int> &coefficients);
64  Polynomial(const Polynomial &poly);
66  ~Polynomial();
68  Polynomial& operator= (const Polynomial &poly);
76  void multiplyPolynomial(const Polynomial &poly);
82  void addPolynomial(const Polynomial &poly);
84  void printPolynomial() const;
88  int getCoefficient(const std::size_t &order) const;
95  void setCoefficient(const int &coefficient, const std::size_t &order);
97  int getMaxXorder() const;
101  void setMaxXorder(const std::size_t &maxXorder);
103  void setZero();
107  double evaluatePolynomial(const double &x) const;
113  void truncate(const std::size_t &truncateOrder);
114 private:
115  std::size_t maxXorder_m;
116  std::vector<int> coefficients_m;
117 };
118 
119 inline
120  int Polynomial::getCoefficient(const std::size_t &order) const {
121  if (order > maxXorder_m) {
122  return 0;
123  }
124  return coefficients_m[order];
125 }
126 inline
128  return maxXorder_m;
129 }
130 inline
131  void Polynomial::setMaxXorder(const std::size_t &maxXorder) {
132  coefficients_m.resize(maxXorder + 1, 0);
133  maxXorder_m = maxXorder;
134 }
135 inline
137  maxXorder_m = 0;
138  coefficients_m.resize(1);
139  coefficients_m[0] = 0;
140 }
141 inline
142  void Polynomial::truncate(const std::size_t &truncateOrder) {
143  if (truncateOrder < maxXorder_m) {
144  coefficients_m.resize(truncateOrder + 1);
145  maxXorder_m = truncateOrder;
146  }
147 }
148 
149 }
150 
151 #endif
void addPolynomial(const Polynomial &poly)
Definition: Polynomial.cpp:107
std::vector< int > coefficients_m
Definition: Polynomial.h:116
int getCoefficient(const std::size_t &order) const
Definition: Polynomial.h:120
void setCoefficient(const int &coefficient, const std::size_t &order)
Definition: Polynomial.cpp:117
int getMaxXorder() const
Definition: Polynomial.h:127
void multiplyPolynomial(const Polynomial &poly)
Definition: Polynomial.cpp:77
void printPolynomial() const
Definition: Polynomial.cpp:96
void setMaxXorder(const std::size_t &maxXorder)
Definition: Polynomial.h:131
Polynomial & operator=(const Polynomial &poly)
Definition: Polynomial.cpp:56
void truncate(const std::size_t &truncateOrder)
Definition: Polynomial.h:142
double evaluatePolynomial(const double &x) const
Definition: Polynomial.cpp:126
std::size_t maxXorder_m
Definition: Polynomial.h:115