OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
DifferentialOperatorTwo.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>
31 #include "PolynomialSum.h"
32 #include "TwoPolynomial.h"
33 
34 namespace polynomial {
35 
37  xDerivatives_m(0), sDerivatives_m(0){
38  std::vector<int> dummy1;
39  dummy1.push_back(1);
40  std::vector<std::vector<int>> dummy2;
41  dummy2.push_back(dummy1);
42  polynomials_m.resize(1);
43  polynomials_m[0].push_back(PolynomialSum(TwoPolynomial(dummy2)));
44 }
45 
47  const std::size_t &xDerivatives,
48  const std::size_t &sDerivatives):
49  xDerivatives_m(xDerivatives), sDerivatives_m(sDerivatives) {
50  polynomials_m.resize(xDerivatives_m + 1);
51  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
52  polynomials_m[i].resize(sDerivatives_m + 1);
53  }
54 }
55 
57  const DifferentialOperatorTwo &doperator):
58  polynomials_m(doperator.polynomials_m),
59  xDerivatives_m(doperator.xDerivatives_m),
60  sDerivatives_m(doperator.sDerivatives_m) {
61 }
62 
64 }
65 
67  const DifferentialOperatorTwo &doperator) {
68  polynomials_m = doperator.polynomials_m;
69  xDerivatives_m = doperator.xDerivatives_m;
70  sDerivatives_m = doperator.sDerivatives_m;
71  return *this;
72 }
73 
74 void DifferentialOperatorTwo::resizeX(const std::size_t &xDerivatives) {
75  std::size_t oldxDerivatives = xDerivatives_m;
76  xDerivatives_m = xDerivatives;
77  polynomials_m.resize(xDerivatives_m + 1);
78  if (xDerivatives_m > oldxDerivatives) {
79  for (std::size_t i = oldxDerivatives + 1; i <= xDerivatives_m; i++) {
80  polynomials_m[i].resize(sDerivatives_m + 1);
81  }
82  }
83 }
84 
85 void DifferentialOperatorTwo::resizeS(const std::size_t &sDerivatives) {
86  sDerivatives_m = sDerivatives;
87  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
88  polynomials_m[i].resize(sDerivatives_m + 1);
89  }
90 }
91 
94  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
95  std::size_t i = xDerivatives_m;
96  while (i != 0) {
97  i--;
98  polynomials_m[i + 1][j].addPolynomial(polynomials_m[i][j]);
99  polynomials_m[i][j].differentiateX();
100  }
101  }
102 }
103 
105  resizeS(sDerivatives_m + 1);
106  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
107  std::size_t j = sDerivatives_m;
108  while (j != 0) {
109  j--;
110  polynomials_m[i][j + 1].addPolynomial(polynomials_m[i][j]);
111  polynomials_m[i][j].differentiateS();
112  }
113  }
114 }
115 
116 void DifferentialOperatorTwo::multiplyPolynomial(const TwoPolynomial &poly) {
117  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
118  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
119  polynomials_m[i][j].multiplyPolynomial(poly);
120  }
121  }
122 }
123 
124 void DifferentialOperatorTwo::setPolynomial(const TwoPolynomial &poly,
125  const std::size_t &x,
126  const std::size_t &s) {
127  if (x > xDerivatives_m) {
128  resizeX(x);
129  }
130  if (s > sDerivatives_m) {
131  resizeS(s);
132  }
133  polynomials_m[x][s] = PolynomialSum(poly);
134 }
135 
137  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
138  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
139  std::size_t zeros = 0;
140  std::cout << "(";
141  for (std::size_t k = 0;
142  k < polynomials_m[i][j].numberOfTerms(); k++) {
143  if (polynomials_m[i][j].isPolynomialZero(k)) zeros++;
144  }
145  if (zeros != polynomials_m[i][j].numberOfTerms()) {
146  polynomials_m[i][j].printPolynomial();
147  std::cout << "(d/dx)^" << i << "(d/ds)^" << j << ")";
148  }
149  }
150  }
151  std::cout << std::endl;
152 }
153 
155  const DifferentialOperatorTwo &doperator) {
156  if (xDerivatives_m < doperator.xDerivatives_m) {
157  resizeX(doperator.xDerivatives_m);
158  }
159  if (sDerivatives_m < doperator.sDerivatives_m) {
160  resizeS(doperator.sDerivatives_m);
161  }
162  for (std::size_t i = 0; i <= doperator.xDerivatives_m; i++) {
163  for (std::size_t j = 0; j <= doperator.sDerivatives_m; j++) {
164  polynomials_m[i][j].addPolynomial(doperator.polynomials_m[i][j]);
165  }
166  }
167 }
168 
170  const std::size_t &s,
171  const std::size_t &term) const {
172  if (x > xDerivatives_m || s > sDerivatives_m) {
173  return true;
174  }
175  return polynomials_m[x][s].isPolynomialZero(term);
176 }
177 
179  const std::size_t &truncateOrder) {
180  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
181  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
182  polynomials_m[i][j].truncate(truncateOrder);
183  }
184  }
185 }
186 
188  const double &x,
189  const double &s,
190  const std::size_t &xDerivative,
191  const std::size_t &sDerivative,
192  const std::vector<double> &dSvalues) const {
193  if (xDerivative > xDerivatives_m || sDerivative > sDerivatives_m) {
194  return 0.0;
195  }
196  return polynomials_m[xDerivative][sDerivative]
197  .evaluatePolynomial2(x, s, dSvalues);
198 }
199 
201  const std::size_t &xDerivatives,
202  const std::size_t &sDerivatives) const {
203  if (xDerivatives > xDerivatives_m || sDerivatives > sDerivatives_m) {
204  return 0;
205  }
206  return polynomials_m[xDerivatives][sDerivatives].numberOfTerms();
207 }
208 
209 std::vector<std::size_t> DifferentialOperatorTwo::getdSFactors(
210  const std::size_t &xDerivatives,
211  const std::size_t &sDerivatives,
212  const std::size_t &p) const{
213  if (xDerivatives > xDerivatives_m || sDerivatives > sDerivatives_m) {
214  std::vector<std::size_t> dummy;
215  return dummy;
216  }
217  return polynomials_m[xDerivatives][sDerivatives].getdSfactors(p);
218 }
219 
221  for (std::size_t i = 0; i <= xDerivatives_m; i++) {
222  for (std::size_t j = 0; j <= sDerivatives_m; j++) {
223  polynomials_m[i][j].sortTerms();
224  }
225  }
226 }
227 
228 }
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
void setPolynomial(const TwoPolynomial &poly, const std::size_t &x, const std::size_t &s)
DifferentialOperatorTwo & operator=(const DifferentialOperatorTwo &doperator)
bool isPolynomialZero(const std::size_t &x, const std::size_t &s, const std::size_t &term) const
void multiplyPolynomial(const TwoPolynomial &poly)
double evaluatePolynomial(const double &x, const double &s, const std::size_t &xDerivative, const std::size_t &sDerivative, const std::vector< double > &dSvalues) const
void resizeX(const std::size_t &xDerivatives)
void truncate(const std::size_t &truncateOrder)
std::size_t numberOfTerms(const std::size_t &xDerivatives, const std::size_t &sDerivatives) const
std::vector< std::vector< PolynomialSum > > polynomials_m
std::vector< std::size_t > getdSFactors(const std::size_t &xDerivatives, const std::size_t &sDerivatives, const std::size_t &p) const
void addOperator(const DifferentialOperatorTwo &doperator)
void resizeS(const std::size_t &sDerivatives)