MINT2
symPolyTerm.cpp
Go to the documentation of this file.
1 #include "Mint/symPolyTerm.h"
2 #include "Mint/Utils.h"
3 
4 #include <iostream>
5 #include <algorithm>
6 
7 using namespace std;
8 using namespace MINT;
9 
10 symPolyTerm::symPolyTerm(const std::vector<int>& pos)
11  : _powers(pos)
12 {
13  init();
14 }
16  : _powers(1)
17 {
18  _powers[0] = a;
19  init();
20 }
22  : _powers(2)
23 {
24  _powers[0] = a;
25  _powers[1] = b;
26  init();
27 }
28 symPolyTerm::symPolyTerm(int a, int b, int c)
29  : _powers(3)
30 {
31  _powers[0] = a;
32  _powers[1] = b;
33  _powers[2] = c;
34  init();
35 }
36 symPolyTerm::symPolyTerm(int a, int b, int c, int d)
37  : _powers(4)
38 {
39  _powers[0] = a;
40  _powers[1] = b;
41  _powers[2] = c;
42  _powers[3] = d;
43  init();
44 }
45 symPolyTerm::symPolyTerm(int a, int b, int c, int d, int e)
46  : _powers(4)
47 {
48  _powers[0] = a;
49  _powers[1] = b;
50  _powers[2] = c;
51  _powers[3] = d;
52  _powers[4] = e;
53  init();
54 }
55 symPolyTerm::symPolyTerm(int a, int b, int c, int d, int e, int f)
56  : _powers(4)
57 {
58  _powers[0] = a;
59  _powers[1] = b;
60  _powers[2] = c;
61  _powers[3] = d;
62  _powers[4] = e;
63  _powers[5] = f;
64  init();
65 }
66 
68  : _powers(other._powers)
69  , _allPermutations(other._allPermutations)
70  , _name(other._name)
71 {
72 }
73 
75  makeName();
77  return true;
78 }
79 
81  _name = "";
82  for(unsigned int i=0; i < _powers.size(); i++){
84  }
85 }
86 
88 
89  _allPermutations.clear();
90  _allPermutations.push_back(_powers);
91 
92  for(unsigned int i=0; i < _powers.size()-1; i++){
93  for(unsigned int j=i+1; j < _powers.size(); j++){
94  unsigned int n = _allPermutations.size();
95  for(unsigned int k = 0; k < n; k++){
96  std::vector<int> ptemp( _allPermutations[k] );
97  swap(ptemp[i],ptemp[j]);
98  _allPermutations.push_back(ptemp);
99  }
100  }
101  }
102 
103  sort(_allPermutations.begin(), _allPermutations.end());
104 
105  std::vector< std::vector<int> >::iterator
106  lastUnique = unique(_allPermutations.begin(), _allPermutations.end());
107 
108  _allPermutations.erase(lastUnique, _allPermutations.end());
109 
110  return;
111 }
112 
113 
114 void symPolyTerm::print(std::ostream& os)const{
115  bool firstTerm(true);
116 
117  // os << " P(x_j; " << name() << " ) = ";
118  for(unsigned int i = 0; i < _allPermutations.size(); i++){
119  if(! firstTerm ) os << " + ";
120  const std::vector<int>& thisPerm(_allPermutations[i]);
121  double thisSum=0;
122  for(unsigned int j = 0; j < thisPerm.size(); j++){
123  thisSum += thisPerm[j];
124  if(0 == j) os << " ";
125  if(0 != thisPerm[j]){
126  os << "x_" << j;
127  if(1 != thisPerm[j]) os << "^" << thisPerm[j];
128  os << " ";
129  firstTerm = false;
130  }
131  }
132  if(0 == thisSum) os << "1 ";
133  }
134 }
135 
136 double symPolyTerm::eval(double x, double y)const{
137  static std::vector<const double* > ptrs(2);
138  ptrs[0] = &x;
139  ptrs[1] = &y;
140  return eval(ptrs);
141 }
142 double symPolyTerm::eval(double x, double y, double z)const{
143  std::vector<const double* > ptrs(3);
144  ptrs[0] = &x;
145  ptrs[1] = &y;
146  ptrs[2] = &z;
147  return eval(ptrs);
148 }
149 double symPolyTerm::eval(double x, double y, double z
150  , double u)const{
151  static std::vector<const double* > ptrs(4);
152  ptrs[0] = &x;
153  ptrs[1] = &y;
154  ptrs[2] = &z;
155  ptrs[3] = &u;
156  return eval(ptrs);
157 }
158 double symPolyTerm::eval(double x, double y, double z
159  , double u, double v)const{
160  std::vector<const double* > ptrs(5);
161  ptrs[0] = &x;
162  ptrs[1] = &y;
163  ptrs[2] = &z;
164  ptrs[3] = &u;
165  ptrs[4] = &v;
166  return eval(ptrs);
167 }
168 double symPolyTerm::eval(double x, double y, double z
169  , double u, double v, double w)const{
170  static std::vector<const double* > ptrs(6);
171  ptrs[0] = &x;
172  ptrs[1] = &y;
173  ptrs[2] = &z;
174  ptrs[3] = &u;
175  ptrs[4] = &v;
176  ptrs[5] = &w;
177  return eval(ptrs);
178 }
179 
180 double symPolyTerm::eval(const std::vector<double>& x)const{
181  if(x.size() != _powers.size()){
182  cout << "symPolyTerm::eval: size mismatch|!!!"
183  << " me: " << _powers.size() << " vs x: " << x.size()
184  << "\n _powers: " << _powers
185  << "\n x: " << x
186  << endl;
187  throw "can't deal with this";
188  }
189 
190  double sum(0);
191  for(unsigned int i = 0; i < _allPermutations.size(); i++){
192  const std::vector<int>& thisPerm(_allPermutations[i]);
193  double prod(1);
194  for(unsigned int j = 0; j < thisPerm.size(); j++){
195  prod *= pow((x[j]), thisPerm[j]);
196  }
197  sum += prod;
198  }
199  return sum;
200 }
201 
202 double symPolyTerm::eval(const std::vector<double* >& x)const{
203  if(x.size() != _powers.size()){
204  cout << "symPolyTerm::eval: size mismatch|!!!"
205  << " me: " << _powers.size() << " vs x: " << x.size()
206  << "\n _powers: " << _powers
207  << "\n x: " << x
208  << endl;
209  throw "can't deal with this";
210  }
211 
212  double sum(0);
213  for(unsigned int i = 0; i < _allPermutations.size(); i++){
214  const std::vector<int>& thisPerm(_allPermutations[i]);
215  double prod(1);
216  for(unsigned int j = 0; j < thisPerm.size(); j++){
217  prod *= pow((*(x[j])), thisPerm[j]);
218  }
219  sum += prod;
220  }
221  return sum;
222 }
223 
224 double symPolyTerm::eval(const std::vector<const double* >& x)const{
225  if(x.size() != _powers.size()){
226  cout << "symPolyTerm::eval: size mismatch|!!!"
227  << " me: " << _powers.size() << " vs x: " << x.size()
228  << "\n _powers: " << _powers
229  << "\n x: " << x
230  << endl;
231  throw "can't deal with this";
232  }
233 
234  double sum(0);
235  for(unsigned int i = 0; i < _allPermutations.size(); i++){
236  const std::vector<int>& thisPerm(_allPermutations[i]);
237  double prod(1);
238  for(unsigned int j = 0; j < thisPerm.size(); j++){
239  prod *= pow((*(x[j])), thisPerm[j]);
240  }
241  sum += prod;
242  }
243  return sum;
244 }
245 
246 
247 
248 std::ostream& operator<<(std::ostream& os, const symPolyTerm& spt){
249  spt.print(os);
250  return os;
251 }
252 
253 //
254 
255 
256 
257 
std::vector< int > _powers
Definition: symPolyTerm.h:8
std::string _name
Definition: symPolyTerm.h:12
void makeAllPermutations()
Definition: symPolyTerm.cpp:87
void makeName()
Definition: symPolyTerm.cpp:80
std::ostream & operator<<(std::ostream &os, const symPolyTerm &spt)
void print(std::ostream &os=std::cout) const
std::string anythingToString(const T &anything)
Definition: Utils.h:62
double eval(double x, double y) const
std::vector< std::vector< int > > _allPermutations
Definition: symPolyTerm.h:10
symPolyTerm(const std::vector< int > &pos)
Definition: symPolyTerm.cpp:10