integralii.cpp 6.44 KB
Newer Older
Olivier Mullier's avatar
Olivier Mullier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * <one line to give the library's name and an idea of what it does.>
 * Copyright (C) 2019  mullier <email>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "integralii.h"


std::list<ibex::Interval *>& IntegralII::compute_list_of_zeros(ibex::Interval searchdomain)
{
    //ATTENTION pas de preuve d'unicité
    std::list<ibex::Interval *>* res = new std::list<ibex::Interval *>();
    
    ibex::CtcNewton newton(*_f);
    
    ibex::CtcFwdBwd fb (*_f);
    ibex::RoundRobin bisector(0);
    
    ibex::CellStack buff;
    
    ibex::CtcCompo comp( fb, newton);
    
    ibex::CtcFixPoint fix(comp, 1e-7);
    
    ibex::Solver s(fix, bisector, buff);
    
    
    ibex::IntervalVector inf(1);
    inf[0] = searchdomain;
    std::vector<ibex::IntervalVector> sols = s.solve(inf);

    unsigned int i;
    for (i = 0; i < sols.size(); i++){
        //ATTENTION detection foireuse de la pente dans la cas ou [f(temp)] contient 0 
        ibex::Interval * current = new ibex::Interval(sols[i][0]);
        res->push_back(current);
    }
    return *res;    
}

std::list<ibex::Interval *>& IntegralII::compute_list_of_zeros_inf()
{
    std::list<ibex::Interval *>& res = compute_list_of_zeros(_inf_endpoint);
    res.sort();
    return res;
}

std::list<ibex::Interval *>& IntegralII::compute_list_of_zeros_sup()
{
    return compute_list_of_zeros(_sup_endpoint);
}


void IntegralII::compute_integral()
{
    
    std::list<IntegralRR> res_temp;
    std::list<IntegralRR>::iterator it_sols;
    ibex::Interval *current = new ibex::Interval(_inf_endpoint.lb());
    IntegralRR min, max;
    //=======================================================================
    //Computation of the integrals in the _inf_endpoint
    std::list<ibex::Interval *> list_of_zeros_inf = compute_list_of_zeros_inf();
    if (list_of_zeros_inf.size()>0){
        std::list<ibex::Interval *>::iterator it_zeros_inf = list_of_zeros_inf.begin();
        while (it_zeros_inf != list_of_zeros_inf.end()){//calcul des intégrales entre chaque zéro de f
            IntegralRR current_integral(current->mid(), (*it_zeros_inf)->mid(), _f);
            it_sols = res_temp.begin();
            while (it_sols != res_temp.end()){//On ajoute l'intégrale courante aux autres intégrales
                *it_sols += current_integral;
                it_sols++;
            }
            res_temp.push_back(current_integral);
            current = *it_zeros_inf;
            it_zeros_inf++;
        }
        //Dernière intégrale du dernier zéro vers la borne inf de l'intervalle
        IntegralRR current_integral(current->mid(), _inf_endpoint.ub(), _f);
        it_sols = res_temp.begin();
        while (it_sols != res_temp.end()){//On ajoute l'intégrale courante aux autres intégrales
            *it_sols += current_integral;
            it_sols++;
        }
        res_temp.push_back(current_integral);
    } else {
        IntegralRR whole_integral(_inf_endpoint.lb(), _inf_endpoint.ub(), _f);
        res_temp.push_back(whole_integral);
        res_temp.push_back(IntegralRR(_inf_endpoint.ub(), _inf_endpoint.ub(), _f, 0.));
    }
    it_sols = res_temp.begin();
    min = *it_sols;
    max = *it_sols;
    it_sols++;
    while (it_sols != res_temp.end()){//On recherche le min et le max
        if (max < *it_sols){
            max = *it_sols;
        }
        if (*it_sols < min){
            min = *it_sols;
        }
        it_sols ++;
    }
    delete current;
    //Calcul de l'intégrale entre les intervalles
    IntegralRR integrale_milieu(_inf_endpoint.ub(), _sup_endpoint.lb(), _f);
    _solution.first = min + integrale_milieu;
    _solution.second = max + integrale_milieu;
    //=======================================================================
    current = new ibex::Interval(_sup_endpoint.ub());
    res_temp.clear();
    std::list<ibex::Interval *> list_of_zeros_sup = compute_list_of_zeros_sup();
    if (list_of_zeros_sup.size()>0){
        std::list<ibex::Interval *>::iterator it_zeros_sup = list_of_zeros_sup.begin();
        while (it_zeros_sup != list_of_zeros_sup.end()){//calcul des intégrales entre chaque zéro de f
            IntegralRR current_integral((*it_zeros_sup)->mid(), current->mid(), _f);
            it_sols = res_temp.begin();
            while (it_sols != res_temp.end()){//On ajoute l'intégrale courante aux autres intégrales
                *it_sols += current_integral;
                it_sols++;
            }
            res_temp.push_back(current_integral);
            current = *it_zeros_sup;
            it_zeros_sup++;
        }
        //Dernière intégrale du dernier zéro vers la borne inf de l'intervalle
        IntegralRR current_integral(_sup_endpoint.lb(), current->mid(), _f);
        it_sols = res_temp.begin();
        while (it_sols != res_temp.end()){//On ajoute l'intégrale courante aux autres intégrales
            *it_sols += current_integral;
            it_sols++;
        }
        res_temp.push_back(current_integral);
    } else {
        IntegralRR whole_integral(_sup_endpoint.lb(), _sup_endpoint.ub(), _f);
        res_temp.push_back(whole_integral);
        res_temp.push_back(IntegralRR(_sup_endpoint.lb(), _sup_endpoint.lb(), _f, 0.));
    }
    it_sols = res_temp.begin();
    min = *it_sols;
    max = *it_sols;
    it_sols++;
    while (it_sols != res_temp.end()){//On recherche le min et le max
        if (max < *it_sols){
            max = *it_sols;
        }
        if (*it_sols < min){
            min = *it_sols;
        }
        it_sols ++;
    }
    
    _solution.first += min;
    _solution.second += max;
    
}

std::ostream& operator<<(std::ostream& os, const IntegralII& integral){
    os << "Integrale from " << integral.get_inf_endpoint() << " to " << integral.get_sup_endpoint();
    os << " of the function " << *integral.get_f() << " : " << endl << "minimum : " << integral.get_solution().first << endl << "maximum : " << integral.get_solution().second;
    return os;
}