Commit aed1ef30cf0cd5f2379b19cf0894099972320fb9

Authored by Olivier Mullier
1 parent 0c06be4a

version qui marche

src/integrale.cpp
1 1 #include "integrale.h"
2 2  
3   -integrale::integrale(const ibex::Interval & inf, const ibex::Interval & sup, ibex::Function* f, ibex::Variable* var) : _inf_bound(inf), _sup_bound(sup), _f(f)
  3 +integrale::integrale(const ibex::Interval & inf, const ibex::Interval & sup, ibex::Function* f) : _inf_bound(inf), _sup_bound(sup), _f(f)
4 4 {
5 5 _zeros_inf = nullptr;
6 6 _zeros_sup = nullptr;
7 7 _result = nullptr;
8 8 }
9 9  
  10 +integrale::~integrale()
  11 +{
  12 + if (_zeros_inf)
  13 + delete _zeros_inf;
  14 + if (_zeros_sup)
  15 + delete _zeros_sup;
  16 + if (_result)
  17 + delete _result;
  18 +
  19 +// if (_f)
  20 +// delete _f;
  21 +}
  22 +
10 23  
11 24 std::list<zero*> * integrale::construct_zeros_intern(const ibex::Interval & inf_bound)
12 25 {
13 26 #ifdef __DEBUG__
14 27 __SHOW_FCT_NAME__
15 28 #endif
16   - //ATTENTION pas de preuve d'unicité
  29 + //ATTENTION pas de cout << elem << endl;preuve d'unicité
17 30 std::list<zero*> * res = new std::list<zero*>();
18 31  
19 32 ibex::CtcNewton newton(*_f);
... ... @@ -104,36 +117,45 @@ std::pair&lt;integrale_sol, integrale_sol&gt; integrale::compute_integral_inf(const ib
104 117 if (!_zeros_inf){
105 118 construct_zeros_inf();
106 119 }
107   -
108   - std::list<zero*>::iterator it = _zeros_inf->begin();
109   - zero *bound_inf = new zero(ibex::Interval(range.lb()), 0);
110   - integrale_sol integ0 = compute_integral(bound_inf, *(it));
111   - res_tmp.push_back(integ0);
112   -
113   - while (it != _zeros_inf->end()){
114   - if (*it != _zeros_inf->back()){
115   - std::list<zero*>::iterator it_first = it;
116   - it++;
117   - std::list<zero*>::iterator it_second = it;
118   - integrale_sol integ = compute_integral(*it_first, *it_second);
119   - std::list<integrale_sol>::iterator it_tmp = res_tmp.begin();
120   - while (it_tmp != res_tmp.end()){
121   - *it_tmp = *it_tmp + integ;
122   - it_tmp++;
  120 + if (_zeros_inf->size()){
  121 + std::list<zero*>::iterator it = _zeros_inf->begin();
  122 + zero *bound_inf = new zero(ibex::Interval(range.lb()), 0);
  123 + integrale_sol integ0 = compute_integral(bound_inf, *(it));
  124 + res_tmp.push_back(integ0);
  125 +
  126 + while (it != _zeros_inf->end()){
  127 + if (*it != _zeros_inf->back()){
  128 + std::list<zero*>::iterator it_first = it;
  129 + it++;
  130 + std::list<zero*>::iterator it_second = it;
  131 + integrale_sol integ = compute_integral(*it_first, *it_second);
  132 + std::list<integrale_sol>::iterator it_tmp = res_tmp.begin();
  133 + while (it_tmp != res_tmp.end()){
  134 + *it_tmp = *it_tmp + integ;
  135 + it_tmp++;
  136 + }
  137 + res_tmp.push_back(integ);
  138 +
  139 + } else {
  140 + zero *bound = new zero(ibex::Interval(range.ub()), 0);
  141 + integrale_sol integ = compute_integral(*it, bound);
  142 + std::list<integrale_sol>::iterator it_tmp = res_tmp.begin();
  143 + while (it_tmp != res_tmp.end()){
  144 + (*it_tmp) = (*it_tmp) + integ;
  145 + it_tmp++;
  146 + }
  147 + it++;
  148 + res_tmp.push_back(integ);
123 149 }
124   - res_tmp.push_back(integ);
125   -
126   - } else {
127   - zero *bound = new zero(ibex::Interval(range.ub()), 0);
128   - integrale_sol integ = compute_integral(*it, bound);
129   - std::list<integrale_sol>::iterator it_tmp = res_tmp.begin();
130   - while (it_tmp != res_tmp.end()){
131   - (*it_tmp) = (*it_tmp) + integ;
132   - it_tmp++;
133   - }
134   - it++;
135   - res_tmp.push_back(integ);
136 150 }
  151 + } else {
  152 + zero * bound_sup = new zero(ibex::Interval(range.ub()), 0);
  153 + zero * bound_inf = new zero(ibex::Interval(range.lb()), 0);
  154 + integrale_sol integ = compute_integral(bound_inf, bound_sup);
  155 + res_tmp.push_back(integ);
  156 + ibex::Interval nul_inter(0.);
  157 + integrale_sol nul(bound_sup, bound_sup, nul_inter);
  158 + res_tmp.push_back(nul);
137 159 }
138 160 if (res_tmp.size() == 0){
139 161 cout << "Erreur, il devrait y avoir au moins une solution" << endl;
... ... @@ -149,6 +171,14 @@ std::pair&lt;integrale_sol, integrale_sol&gt; integrale::compute_integral_inf(const ib
149 171 max = elem;
150 172 }
151 173 }
  174 + for (auto elem : res_tmp){
  175 + if (elem != min || elem != max){
  176 + if (elem.get_inf_bound() && !elem.get_inf_bound()->is_non_zero())
  177 + delete elem.get_inf_bound();
  178 + if (elem.get_sup_bound() && !elem.get_sup_bound()->is_non_zero())
  179 + delete elem.get_sup_bound();
  180 + }
  181 + }
152 182 return pair<integrale_sol, integrale_sol>(min, max);
153 183 }
154 184  
... ... @@ -159,44 +189,45 @@ std::pair&lt;integrale_sol, integrale_sol&gt; integrale::compute_integral_sup(const ib
159 189 if (!_zeros_sup){
160 190 construct_zeros_sup();
161 191 }
162   -
163   - std::list<zero*>::iterator it = _zeros_sup->begin();
164   - zero * bound_sup = new zero(ibex::Interval(range.ub()), 0);
165   -// std::cout << "inf bound: " << *it << endl;
166   -// std::cout << "sup bound: " << bound_sup << endl;
167   - integrale_sol integ0 = compute_integral((*it), bound_sup);
168   - res_temp.push_back(integ0);
169   -
170   - while (it != _zeros_sup->end()){
171   - if (*it != _zeros_sup->back()){
172   - std::list<zero*>::iterator it_second = it;
173   - it++;
174   - std::list<zero*>::iterator it_first = it;
175   -// std::cout << "inf bound: " << *it_first << endl;
176   -// std::cout << "sup bound: " << *it_second << endl;
177   - integrale_sol integ = compute_integral(*it_first, *it_second);
178   - std::list<integrale_sol>::iterator it_tmp = res_temp.begin();
179   - while (it_tmp != res_temp.end()){
180   - *it_tmp = *it_tmp + integ;
181   - it_tmp++;
  192 + if (_zeros_sup->size() > 0){
  193 + std::list<zero*>::iterator it = _zeros_sup->begin();
  194 + zero * bound_sup = new zero(ibex::Interval(range.ub()), 0);
  195 + integrale_sol integ0 = compute_integral((*it), bound_sup);
  196 + res_temp.push_back(integ0);
  197 +
  198 + while (it != _zeros_sup->end()){
  199 + if (*it != _zeros_sup->back()){
  200 + std::list<zero*>::iterator it_second = it;
  201 + it++;
  202 + std::list<zero*>::iterator it_first = it;
  203 + integrale_sol integ = compute_integral(*it_first, *it_second);
  204 + std::list<integrale_sol>::iterator it_tmp = res_temp.begin();
  205 + while (it_tmp != res_temp.end()){
  206 + *it_tmp = *it_tmp + integ;
  207 + it_tmp++;
  208 + }
  209 + res_temp.push_back(integ);
  210 +
  211 + } else {
  212 + zero * bound = new zero(ibex::Interval(range.lb()), 0);
  213 + integrale_sol integ = compute_integral(bound, *it);
  214 + std::list<integrale_sol>::iterator it_tmp = res_temp.begin();
  215 + while (it_tmp != res_temp.end()){
  216 + *it_tmp = *it_tmp + integ;
  217 + it_tmp++;
  218 + }
  219 + it++;
  220 + res_temp.push_back(integ);
182 221 }
183   - res_temp.push_back(integ);
184   -
185   - } else {
186   -// cout << "on passe ici" << endl;
187   - zero * bound = new zero(ibex::Interval(range.lb()), 0);
188   -// std::cout << "inf bound: " << bound << endl;
189   -// std::cout << "sup bound: " << *it << endl;
190   - integrale_sol integ = compute_integral(bound, *it);
191   -// cout << integ << endl;
192   - std::list<integrale_sol>::iterator it_tmp = res_temp.begin();
193   - while (it_tmp != res_temp.end()){
194   - *it_tmp = *it_tmp + integ;
195   - it_tmp++;
196   - }
197   - it++;
198   - res_temp.push_back(integ);
199 222 }
  223 + } else {
  224 + zero * bound_sup = new zero(ibex::Interval(range.ub()), 0);
  225 + zero * bound_inf = new zero(ibex::Interval(range.lb()), 0);
  226 + integrale_sol integ = compute_integral(bound_inf, bound_sup);
  227 + res_temp.push_back(integ);
  228 + ibex::Interval nul_inter(0.);
  229 + integrale_sol nul(bound_inf, bound_inf, nul_inter);
  230 + res_temp.push_back(nul);
200 231 }
201 232 if (res_temp.size() == 0){
202 233 cout << "Erreur, il devrait y avoir au moins une solution" << endl;
... ... @@ -205,7 +236,6 @@ std::pair&lt;integrale_sol, integrale_sol&gt; integrale::compute_integral_sup(const ib
205 236 integrale_sol min = *(res_temp.begin());
206 237 integrale_sol max = *(res_temp.begin());
207 238 for (auto elem : res_temp){
208   -// cout << "\tcandidate : " << elem << endl;
209 239 if (elem.get_res_integrale().lb() < min.get_res_integrale().lb()){
210 240 min = elem;
211 241 }
... ... @@ -213,32 +243,29 @@ std::pair&lt;integrale_sol, integrale_sol&gt; integrale::compute_integral_sup(const ib
213 243 max = elem;
214 244 }
215 245 }
  246 + for (auto elem : res_temp){
  247 + if (elem != min || elem != max){
  248 + if (elem.get_inf_bound() && !elem.get_inf_bound()->is_non_zero())
  249 + delete elem.get_inf_bound();
  250 + if (elem.get_sup_bound() && !elem.get_sup_bound()->is_non_zero())
  251 + delete elem.get_sup_bound();
  252 + }
  253 + }
216 254 return pair<integrale_sol, integrale_sol>(min, max);
217 255 }
218 256  
219 257 std::pair<integrale_sol, integrale_sol> integrale::compute_integral()
220 258 {
221 259 std::pair<integrale_sol, integrale_sol> res = compute_integral_inf(_inf_bound);
222   -// std::cout << "in inf : " << endl;
223   -// std::cout << "\tmin : " << res.first << endl;
224   -// std::cout << "\tmax : " << res.second << endl;
225 260 zero * inf = res.first.get_sup_bound();
226   -
227 261 std::pair<integrale_sol, integrale_sol> res_sup = compute_integral_sup(_sup_bound);
228   -// std::cout << "in sup : " << endl;
229   -// std::cout << "\tmin : " << res_sup.first << endl;
230   -// std::cout << "\tmax : " << res_sup.second << endl;
231 262  
232 263 zero * sup = res_sup.first.get_inf_bound();
233   -
234 264 ibex::Interval mid = compute_integral(_inf_bound.ub(), _sup_bound.lb());
235   - cout << *inf << endl;
236   - cout << *sup << endl;
237 265 integrale_sol integ_mid(inf, sup, mid);
238   -// cout << "integrale mid : " << integ_mid << endl;
  266 +
239 267 res.first+= integ_mid;
240 268 res.second+= integ_mid;
241   -// std::cout << "mid : " << mid << endl;
242 269 res.first+= res_sup.first;
243 270 res.second+= res_sup.second;
244 271 if (_result == nullptr){
... ...
src/integrale.h
... ... @@ -9,7 +9,7 @@
9 9  
10 10  
11 11  
12   -#define __PREC__ (1e-3)
  12 +#define __PREC__ (1e-5)
13 13 #define __EPS__ (1e-1)
14 14 #define __SHOW_FCT_NAME__ {std::cout << __func__ << std::endl;}
15 15  
... ... @@ -22,7 +22,7 @@ private:
22 22 ibex::Interval _sup_bound;
23 23  
24 24 ibex::Function * _f;
25   - ibex::Variable * _x_var;
  25 +// ibex::Variable * _x_var;
26 26  
27 27 std::list<zero*> * _zeros_inf;
28 28 std::list<zero*> * _zeros_sup;
... ... @@ -43,13 +43,14 @@ private:
43 43  
44 44 public:
45 45 //CONSTRUCTORS
46   - integrale(const ibex::Interval& inf, const ibex::Interval& sup, ibex::Function * f, ibex::Variable * var);
  46 + integrale(const ibex::Interval& inf, const ibex::Interval& sup, ibex::Function * f);
  47 + ~integrale();
47 48  
48 49 //GETTERS
49 50 inline ibex::Interval get_inf_bound() const {return _inf_bound;};
50 51 inline ibex::Interval get_sup_bound() const {return _sup_bound;};
51 52 inline ibex::Function * get_f() const {return _f;};
52   - inline ibex::Variable * get_x_var() const {return _x_var;};
  53 +// inline ibex::Variable * get_x_var() const {return _x_var;};
53 54 inline std::list<zero*> * get_zeros_inf(){return _zeros_inf;};
54 55 inline std::list<zero*> * get_zeros_sup(){return _zeros_sup;};
55 56 inline std::pair<integrale_sol, integrale_sol> * get_result(){return _result;};
... ...
src/integrale_sol.cpp
... ... @@ -32,6 +32,15 @@ integrale_sol::integrale_sol(zero* inf, zero* sup, const ibex::Interval&amp; res) :
32 32 }
33 33 }
34 34  
  35 +integrale_sol::~integrale_sol()
  36 +{
  37 +// if (_inf)
  38 +// delete _inf;
  39 +// if (_sup)
  40 +// delete _sup;
  41 +}
  42 +
  43 +
35 44 integrale_sol & integrale_sol::operator+=(const integrale_sol &rhs){
36 45 *this = *this + rhs;
37 46 return *this;
... ... @@ -39,12 +48,11 @@ integrale_sol &amp; integrale_sol::operator+=(const integrale_sol &amp;rhs){
39 48  
40 49 integrale_sol operator+(const integrale_sol& lhs, const integrale_sol& rhs)
41 50 {
  51 +
42 52 if (lhs.get_sup_bound() == rhs.get_inf_bound()){
43   -// cout << "ici" << endl;
44   - return integrale_sol(&*lhs.get_inf_bound(), &*rhs.get_sup_bound(), (lhs.get_res_integrale() + rhs.get_res_integrale()));
  53 + return integrale_sol(lhs.get_inf_bound(), rhs.get_sup_bound(), (lhs.get_res_integrale() + rhs.get_res_integrale()));
45 54 } else {
46   -// cout << "la" << endl;
47   - return integrale_sol(&*rhs.get_inf_bound(), &*lhs.get_sup_bound(), (lhs.get_res_integrale() + rhs.get_res_integrale()));
  55 + return integrale_sol(rhs.get_inf_bound(), lhs.get_sup_bound(), (lhs.get_res_integrale() + rhs.get_res_integrale()));
48 56 }
49 57  
50 58 }
... ... @@ -54,3 +62,15 @@ std::ostream &amp; operator&lt;&lt;(std::ostream&amp; os, const integrale_sol&amp; inte)
54 62 os << "Integral from (" << *(inte.get_inf_bound()) << ") to (" << *inte.get_sup_bound() << ") : " << inte.get_res_integrale();
55 63 return os;
56 64 }
  65 +
  66 +bool operator==(const integrale_sol& lhs, const integrale_sol& rhs)
  67 +{
  68 + return (lhs.get_inf_bound() == rhs.get_inf_bound() && lhs.get_sup_bound() == rhs.get_sup_bound());
  69 +}
  70 +
  71 +bool operator!=(const integrale_sol& lhs, const integrale_sol& rhs)
  72 +{
  73 + return !(lhs == rhs);
  74 +}
  75 +
  76 +
... ...
src/integrale_sol.h
... ... @@ -35,6 +35,7 @@ private:
35 35  
36 36 public:
37 37 integrale_sol(zero *inf, zero *sup, const ibex::Interval & res);
  38 + ~integrale_sol();
38 39  
39 40 inline zero * get_inf_bound() const {return _inf;};
40 41 inline zero * get_sup_bound() const {return _sup;};
... ... @@ -49,6 +50,8 @@ public:
49 50 };
50 51  
51 52 integrale_sol operator+(const integrale_sol &, const integrale_sol &);
  53 +bool operator==(const integrale_sol &, const integrale_sol &);
  54 +bool operator!=(const integrale_sol &, const integrale_sol &);
52 55 std::ostream& operator<<(std::ostream& os, const integrale_sol& inte_sol);
53 56  
54 57 #endif // INTEGRALE_SOL_H
... ...
src/main.cpp
... ... @@ -8,16 +8,17 @@ using namespace std;
8 8 void example(){
9 9 cout << setprecision(15);
10 10 ibex::Variable x(1);
11   - ibex::Function f(x, ibex::sin(x));
  11 + ibex::Function f(x, -15. + x+5.*ibex::sin(x));
12 12 ibex::Interval inf(0., 5.5);
13 13 ibex::Interval sup(10, 15.);
14 14  
15   - integrale example(inf, sup, &f, &x);
  15 + integrale example(inf, sup, &f);
16 16  
17 17 cout << example << endl;
18 18 example.construct_zeros();
19 19 for (auto elem : *example.get_zeros_sup()){
20 20 cout << elem << endl;
  21 + cout << *elem << endl << endl;
21 22 }
22 23 example.compute_integral();
23 24 cout << "minimum: " << example.get_result()->first << endl;
... ...