OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
DifferentialOperator.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 <iostream>
29 #include <vector>
30 #include "DifferentialOperator.h"
31 
32 namespace polynomial {
33 
35  xDerivatives_m(0), sDerivatives_m(0){
36  std::vector<int> temp;
37  temp.push_back(1);
38  polynomials_m.resize(1);
39  polynomials_m[0].push_back(Polynomial(temp));
40 }
41 
42 DifferentialOperator::DifferentialOperator(const std::size_t &xDerivatives,
43  const std::size_t &sDerivatives):
44  xDerivatives_m(xDerivatives), sDerivatives_m(sDerivatives) {
45  polynomials_m.resize(xDerivatives_m + 1);
46  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
47  polynomials_m[i].resize(sDerivatives_m + 1);
48  }
49 }
50 
52  const DifferentialOperator &doperator):
53  polynomials_m(doperator.polynomials_m),
54  xDerivatives_m(doperator.xDerivatives_m),
55  sDerivatives_m(doperator.sDerivatives_m) {
56 }
57 
59 }
60 
62  const DifferentialOperator &doperator) {
63  polynomials_m = doperator.polynomials_m;
64  xDerivatives_m = doperator.xDerivatives_m;
65  sDerivatives_m = doperator.sDerivatives_m;
66  return *this;
67 }
68 
69 void DifferentialOperator::resizeX(const std::size_t &xDerivatives) {
70  std::size_t oldxDerivatives = xDerivatives_m;
71  xDerivatives_m = xDerivatives;
72  polynomials_m.resize(xDerivatives_m + 1);
73  if (xDerivatives_m > oldxDerivatives) {
74  for (std::size_t i = oldxDerivatives + 1; i <= xDerivatives_m; i++) {
75  polynomials_m[i].resize(sDerivatives_m + 1);
76  }
77  }
78 }
79 
80 void DifferentialOperator::resizeS(const std::size_t &sDerivatives) {
81  sDerivatives_m = sDerivatives;
82  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
83  polynomials_m[i].resize(sDerivatives_m + 1);
84  }
85 }
86 
89  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
90  std::size_t i = xDerivatives_m;
91  while (i != 0) {
92  i--;
93  polynomials_m[i + 1][j].addPolynomial(polynomials_m[i][j]);
94  polynomials_m[i][j].differentiatePolynomial();
95  }
96  }
97 }
98 
100  resizeS(sDerivatives_m + 1);
101  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
102  std::size_t j = sDerivatives_m;
103  while (j != 0) {
104  j--;
105  polynomials_m[i][j + 1].addPolynomial(polynomials_m[i][j]);
106  polynomials_m[i][j].setZero();
107  }
108  }
109 }
110 
112  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
113  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
114  polynomials_m[i][j].multiplyPolynomial(poly);
115  }
116  }
117 }
118 
119 void DifferentialOperator::setPolynomial(const std::vector<int> &poly,
120  const std::size_t &x,
121  const std::size_t &s) {
122  if (x > xDerivatives_m) {
123  resizeX(x);
124  }
125  if (s > sDerivatives_m) {
126  resizeS(s);
127  }
128  polynomials_m[x][s] = Polynomial(poly);
129 }
130 
132  polynomials_m[0][0].printPolynomial();
133  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
134  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
135  if ((i != 0 || j != 0) &&
136  (polynomials_m[i][j].getMaxXorder() != 0 ||
137  polynomials_m[i][j].getCoefficient(0) != 0)) {
138  std::cout << " + (";
139  polynomials_m[i][j].printPolynomial();
140  std::cout << ")" << "(d/dx)^" << i << "(d/ds)^" << j;
141  }
142  }
143  }
144  std::cout << std::endl;
145 }
146 
148  if (xDerivatives_m < doperator.xDerivatives_m) {
149  resizeX(doperator.xDerivatives_m);
150  }
151  if (sDerivatives_m < doperator.sDerivatives_m) {
152  resizeS(doperator.sDerivatives_m);
153  }
154  for (std::size_t i = 0; i <= doperator.xDerivatives_m; i++) {
155  for (std::size_t j = 0; j <= doperator.sDerivatives_m; j++) {
156  polynomials_m[i][j].addPolynomial(doperator.polynomials_m[i][j]);
157  }
158  }
159 }
160 
161 bool DifferentialOperator::isPolynomialZero(const std::size_t &x,
162  const std::size_t &s) const {
163  if (x > xDerivatives_m || s > sDerivatives_m) {
164  return true;
165  }
166  return (polynomials_m[x][s].getCoefficient(0) == 0 &&
167  polynomials_m[x][s].getMaxXorder() == 0);
168 }
169 
170 void DifferentialOperator::truncate(const std::size_t &truncateOrder) {
171  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
172  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
173  polynomials_m[i][j].truncate(truncateOrder);
174  }
175  }
176 }
177 
179  const double &x,
180  const std::size_t &xDerivative,
181  const std::size_t &sDerivative) const {
182  if (xDerivative > xDerivatives_m || sDerivative > sDerivatives_m) {
183  return 0.0;
184  }
185  return polynomials_m[xDerivative][sDerivative].evaluatePolynomial(x);
186 }
187 
188 }
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
DifferentialOperator & operator=(const DifferentialOperator &doperator)
bool isPolynomialZero(const std::size_t &x, const std::size_t &s) const
double evaluatePolynomial(const double &x, const std::size_t &xDerivative, const std::size_t &sDerivative) const
void truncate(const std::size_t &truncateOrder)
void multiplyPolynomial(const Polynomial &poly)
void resizeX(const std::size_t &xDerivatives)
std::vector< std::vector< Polynomial > > polynomials_m
void setPolynomial(const std::vector< int > &poly, const std::size_t &x, const std::size_t &s)
void addOperator(const DifferentialOperator &doperator)
void resizeS(const std::size_t &sDerivatives)