carl  24.04
Computer ARithmetic Library
HornerTest.cpp
Go to the documentation of this file.
4 #include <carl-io/StringParser.h>
6 
7 #ifdef USE_GINAC
8 #include <cln/cln.h>
9 typedef cln::cl_RA Rational;
10 #else
11 #include <gmpxx.h>
12 typedef mpq_class Rational;
13 #endif
14 
15 using namespace carl;
16 
18 
19 int main (int argc, char** argv)
20 {
21 
23  sp.setVariables({"x", "y", "z"});
24  //Pol p1 = sp.parseMultivariatePolynomial<Rational>("x^2*y^4*z^5*3 + x^3*10*y^4 + 20*z^6*y^2 + 21*x^9*z^2 + 4*x*y");
25  Pol p1 = sp.parseMultivariatePolynomial<Rational>("x*y + x*z + 2*x");
26  //Pol p1 = sp.parseMultivariatePolynomial<Rational>("12*x +13*x^2+3*x ");
27  //Pol p1 = sp.parseMultivariatePolynomial<Rational>("2184*x^17+15708*z+(-126672)*x^2+643384*z^3+(-2306444)*z^4+4162512*x^13+(-10186920)*z^12+18820800*x^11+(-27118448)*x^10+31123477*x^9+6199788*x^5+(-12956461)*x^6+21524503*x^7+(-28784511)*x^8+(-1226048)*x^14+245224*y^15+(-31192)*y^16+(-924)");
28 
29 
30  std::set<Variable> allVarInPolynome = carl::variables(p1).as_set();
31  std::map<Variable, Interval<double>> map;
32 
33  for(auto i : allVarInPolynome) {
34  if (i.id() == 1)
35  {
36  Interval<double> ix (-2, 2);
37  map[i] = ix;
38  }
39  if (i.id() == 2)
40  {
41  Interval<double> iy (0, 1);
42  map[i] = iy;
43  }
44  if (i.id() == 3)
45  {
46  Interval<double> iz (0, 1);
47  map[i] = iz;
48  }
49  }
50 
51  std::cout << "\n Polynom :" << p1 << std::endl;
52 
53  MultivariateHorner< Pol, strategy > peterPolynom (std::move(p1));
54  //MultivariateHorner< Pol, GREEDY_IIs > peterPolynom2 (p1,map);
55 
56  std::cout << "\n GREEDY_Is :" << peterPolynom << std::endl;
57  //std::cout << " GREEDY_IIs :" << peterPolynom2 << std::endl;
58 
59  Interval<double> testInterval = carl::evaluate(peterPolynom ,map);
60  //Interval<Rational> testInterval2 = evaluate(peterPolynom2 , map);
61 
62  std::cout << "\n Evaluate Horner :" << testInterval << std::endl;
63  //std::cout << " Evaluate Horner2:" << testInterval2 << std::endl;
64 
65 
66  /*
67  Rational u_a;
68  Rational l_a;
69  Rational u_b;
70  Rational l_b;
71  Rational u_c;
72  Rational l_c;
73 
74  std::map<Variable, Interval<Rational>> map;
75  VariablePool& vpool = VariablePool::getInstance();
76  Variable a = vpool.get_fresh_variable();
77  vpool.set_name(a, "a");
78  Variable b = vpool.get_fresh_variable();
79  vpool.set_name(b, "b");
80  Variable c = vpool.get_fresh_variable();
81  vpool.set_name(c, "c");
82 
83  int l = 0;
84  int n = 0;
85 
86  for (int i = 1; i <= 6; i++)
87  {
88 
89  switch (i)
90  {
91  case 1 : l_a = -200; u_a = -100;
92  break;
93  case 2 : l_a = -100; u_a = 0;
94  break;
95  case 3 : l_a = -100; u_a = 100;
96  break;
97  case 4 : l_a = 0; u_a = 0;
98  break;
99  case 5 : l_a = 0; u_a = 100;
100  break;
101  case 6 : l_a = 100; u_a = 200;
102  break;
103  }
104  for (int j = 1; j <= 6; j++)
105  {
106  switch (j)
107  {
108  case 1 : l_b = -201; u_b = -101;
109  break;
110  case 2 : l_b = -101; u_b = 0;
111  break;
112  case 3 : l_b = -101; u_b = 101;
113  break;
114  case 4 : l_b = 0; u_b = 0;
115  break;
116  case 5 : l_b = 0; u_b = 101;
117  break;
118  case 6 : l_b = 101; u_b = 201;
119  break;
120  }
121  for (int k = 1; k <= 6; k++)
122  {
123  switch (k)
124  {
125  case 1 : l_c = -202; u_c = -102;
126  break;
127  case 2 : l_c = -102; u_c = 0;
128  break;
129  case 3 : l_c = -102; u_c = 102;
130  break;
131  case 4 : l_c = 0; u_c = 0;
132  break;
133  case 5 : l_c = 0; u_c = 102;
134  break;
135  case 6 : l_c = 102; u_c = 202;
136  break;
137  }
138 
139  Interval<Rational> ia( l_a, u_a );
140  Interval<Rational> ib( l_b, u_b );
141  Interval<Rational> ic( l_c, u_c );
142 
143  Interval<Rational> iR1 = ib + ic;
144  iR1 *= ia;
145 
146  Interval<Rational> iR2 = ia * ib;
147  Interval<Rational> iR3 = ia * ic;
148  iR2 += iR3;
149 
150  //std::cout << "[" << i << j << k << "] : "<< iR1 << " - " << iR2;
151  n++;
152  //std::cout << n;
153  if (iR1.diameter() < iR2.diameter()) {
154  l++;
155  //std::cout << "[" << i << j << k << "] : "<< iR1 << " - " << iR2;
156 
157  std::cout << l << " & $";
158 
159  switch (i)
160  {
161  case 1 : std::cout << "a_1 < a_2 < 0";
162  break;
163  case 2 : std::cout << "a_1 < a_2 = 0";
164  break;
165  case 3 : std::cout << "a_1 < 0 < a_2";
166  break;
167  case 4 : std::cout << "a_1 = a_2 = 0";
168  break;
169  case 5 : std::cout << "a_1 = 0 < a_2";
170  break;
171  case 6 : std::cout << "0 < a_1 < a_2";
172  break;
173  }
174 
175  std::cout << " $ & $ ";
176 
177  switch (j)
178  {
179  case 1 : std::cout << "b_1 < b_2 < 0";
180  break;
181  case 2 : std::cout << "b_1 < b_2 = 0";
182  break;
183  case 3 : std::cout << "b_1 < 0 < b_2";
184  break;
185  case 4 : std::cout << "b_1 = b_2 = 0";
186  break;
187  case 5 : std::cout << "b_1 = 0 < b_2";
188  break;
189  case 6 : std::cout << "0 < b_1 < b_2";
190  break;
191  }
192 
193  std::cout << "$ & $";
194 
195  switch (k)
196  {
197  case 1 : std::cout << "c_1 < c_2 < 0";
198  break;
199  case 2 : std::cout << "c_1 < c_2 = 0";
200  break;
201  case 3 : std::cout << "c_1 < 0 < c_2";
202  break;
203  case 4 : std::cout << "c_1 = c_2 = 0";
204  break;
205  case 5 : std::cout << "c_1 = 0 < c_2";
206  break;
207  case 6 : std::cout << "0 < c_1 < c_2";
208  break;
209  }
210 
211 
212  std::cout << " $ \\\\ " << std::endl;
213  }
214  if (iR1.diameter() == iR2.diameter()) {
215  //std::cout << "& SAME" << std::endl;
216  }
217  if (iR1.diameter() > iR2.diameter()) {
218  // std::cout << "& GREATER" << std::endl;
219  }
220 
221 
222  assert (iR1 == iR2);
223 
224 
225  }
226  }
227  }
228 */
229 }
int main(int argc, char **argv)
Definition: HornerTest.cpp:19
mpq_class Rational
Definition: HornerTest.cpp:12
MultivariatePolynomial< Rational > Pol
Definition: HornerTest.cpp:17
carl is the main namespace for the library.
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
Definition: Evaluation.h:10
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
void setVariables(std::list< std::string > variables)
Definition: StringParser.h:90
MultivariatePolynomial< C, O, P > parseMultivariatePolynomial(const std::string &inputString) const
Definition: StringParser.h:160