Skip to content

Commit 4bb78bc

Browse files
committed
Merge branch 'codac2' into codac2_codac4matlab
2 parents df855f7 + 9bb7122 commit 4bb78bc

12 files changed

Lines changed: 167 additions & 74 deletions

File tree

doc/manual/manual/contractors/analytic/ctcinverse.rst

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ To represent the vectors :math:`\mathbf{x}\in\mathbb{R}^n` consistent with the c
9292
:end-before: [ctcinv-1-end]
9393
:dedent: 0
9494

95-
The contractor can be used as an operator to contract a 2d box :math:`[\mathbf{x}]`. It can also be involved in a paver in order to reveal the constraint:
95+
The contractor can be used as an operator to contract a :math:`n`-d box :math:`[\mathbf{x}]`.
96+
97+
Note that the contraction may be fast but not minimal, depending on your analytic expression. Therefore, you can also combine a ``CtcInverse`` with a ``CtcFixpoint`` to apply the contraction procedure repeatedly until a fixpoint is reached on the same box. The following code corresponds to a contraction revealed in the next figure, which shows contraction cases in blue and their fixpoint counterparts in red.
9698

9799
.. tabs::
98100

@@ -120,14 +122,10 @@ The contractor can be used as an operator to contract a 2d box :math:`[\mathbf{x
120122
:end-before: [ctcinv-2-end]
121123
:dedent: 0
122124

123-
Which produces the following output:
124-
125-
.. figure:: ./himmelblau_50.png
125+
.. figure:: ./himmelblau_boxes.png
126126
:width: 400px
127127

128-
Outer approximation of the solution set for :math:`f(\mathbf{x})\in[50,50]`. This paving result reveals three connected subsets approximated with outer boxes. Blue parts are guaranteed not to contain solutions.
129-
130-
We recall that for thick solution sets, one should prefer the use of the ``SepInverse`` separator in order to get both outer and inner approximations. For instance, the solution set associated with :math:`f(\mathbf{x})\leqslant 50` corresponds to:
128+
As any other contractor, a ``CtcInverse`` can also be involved in a paver in order to reveal the constraint set:
131129

132130
.. tabs::
133131

@@ -155,6 +153,41 @@ We recall that for thick solution sets, one should prefer the use of the ``SepIn
155153
:end-before: [ctcinv-3-end]
156154
:dedent: 0
157155

156+
Which produces the following output:
157+
158+
.. figure:: ./himmelblau_50.png
159+
:width: 400px
160+
161+
Outer approximation of the solution set for :math:`f(\mathbf{x})\in[50,50]`. This paving result reveals three connected subsets approximated with outer boxes. Blue parts are guaranteed not to contain solutions.
162+
163+
We recall that for thick solution sets, one should prefer the use of the ``SepInverse`` separator in order to get both outer and inner approximations. For instance, the solution set associated with :math:`f(\mathbf{x})\leqslant 50` corresponds to:
164+
165+
.. tabs::
166+
167+
.. group-tab:: Python
168+
169+
.. literalinclude:: src.py
170+
:language: py
171+
:start-after: [ctcinv-4-beg]
172+
:end-before: [ctcinv-4-end]
173+
:dedent: 4
174+
175+
.. group-tab:: C++
176+
177+
.. literalinclude:: src.cpp
178+
:language: c++
179+
:start-after: [ctcinv-4-beg]
180+
:end-before: [ctcinv-4-end]
181+
:dedent: 4
182+
183+
.. group-tab:: Matlab
184+
185+
.. literalinclude:: src.m
186+
:language: matlab
187+
:start-after: [ctcinv-4-beg]
188+
:end-before: [ctcinv-4-end]
189+
:dedent: 0
190+
158191

159192
.. figure:: ./himmelblau_50_inner.png
160193
:width: 400px
@@ -176,24 +209,24 @@ can be easily approximated by the following union of contractors:
176209

177210
.. literalinclude:: src.py
178211
:language: py
179-
:start-after: [ctcinv-4-beg]
180-
:end-before: [ctcinv-4-end]
212+
:start-after: [ctcinv-5-beg]
213+
:end-before: [ctcinv-5-end]
181214
:dedent: 4
182215

183216
.. group-tab:: C++
184217

185218
.. literalinclude:: src.cpp
186219
:language: c++
187-
:start-after: [ctcinv-4-beg]
188-
:end-before: [ctcinv-4-end]
220+
:start-after: [ctcinv-5-beg]
221+
:end-before: [ctcinv-5-end]
189222
:dedent: 4
190223

191224
.. group-tab:: Matlab
192225

193226
.. literalinclude:: src.m
194227
:language: matlab
195-
:start-after: [ctcinv-4-beg]
196-
:end-before: [ctcinv-4-end]
228+
:start-after: [ctcinv-5-beg]
229+
:end-before: [ctcinv-5-end]
197230
:dedent: 0
198231

199232
.. figure:: ./himmelblau_50_150_250.png
@@ -323,24 +356,24 @@ When the constraint is a complement constraint :math:`\mathbf{f}(\mathbf{x})\not
323356

324357
.. literalinclude:: src.py
325358
:language: py
326-
:start-after: [ctcinv-5-beg]
327-
:end-before: [ctcinv-5-end]
359+
:start-after: [ctcinv-6-beg]
360+
:end-before: [ctcinv-6-end]
328361
:dedent: 4
329362

330363
.. group-tab:: C++
331364

332365
.. literalinclude:: src.cpp
333366
:language: c++
334-
:start-after: [ctcinv-5-beg]
335-
:end-before: [ctcinv-5-end]
367+
:start-after: [ctcinv-6-beg]
368+
:end-before: [ctcinv-6-end]
336369
:dedent: 4
337370

338371
.. group-tab:: Matlab
339372

340373
.. literalinclude:: src.m
341374
:language: matlab
342-
:start-after: [ctcinv-5-beg]
343-
:end-before: [ctcinv-5-end]
375+
:start-after: [ctcinv-6-beg]
376+
:end-before: [ctcinv-6-end]
344377
:dedent: 0
345378

346379

@@ -358,24 +391,24 @@ The underlying analytic function can be accessed through ``.fnc()`` (useful for
358391

359392
.. literalinclude:: src.py
360393
:language: py
361-
:start-after: [ctcinv-6-beg]
362-
:end-before: [ctcinv-6-end]
394+
:start-after: [ctcinv-7-beg]
395+
:end-before: [ctcinv-7-end]
363396
:dedent: 4
364397

365398
.. group-tab:: C++
366399

367400
.. literalinclude:: src.cpp
368401
:language: c++
369-
:start-after: [ctcinv-6-beg]
370-
:end-before: [ctcinv-6-end]
402+
:start-after: [ctcinv-7-beg]
403+
:end-before: [ctcinv-7-end]
371404
:dedent: 4
372405

373406
.. group-tab:: Matlab
374407

375408
.. literalinclude:: src.m
376409
:language: matlab
377-
:start-after: [ctcinv-6-beg]
378-
:end-before: [ctcinv-6-end]
410+
:start-after: [ctcinv-7-beg]
411+
:end-before: [ctcinv-7-end]
379412
:dedent: 0
380413

381414
Centered form option
4.02 KB
Loading
-2.38 KB
Loading
29.3 KB
Loading

doc/manual/manual/contractors/analytic/src.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <catch2/catch_test_macros.hpp>
1111
#include <codac2_CtcInverse.h>
12+
#include <codac2_CtcFixpoint.h>
1213
#include <codac2_SepInverse.h>
1314
#include <codac2_CtcInverseNotIn.h>
1415
#include <codac2_Approx.h>
@@ -29,22 +30,36 @@ TEST_CASE("CtcInverse - manual")
2930
// [ctcinv-1-end]
3031

3132
// [ctcinv-2-beg]
32-
DefaultFigure::pave({{-6,6},{-6,6}}, c, 1e-2);
33+
IntervalVector z({{0,3.5},{0,1}});
34+
DefaultFigure::draw_box(z, {Color::blue(),Color::blue(.1)}); // prior to contraction
35+
c.contract(z);
36+
DefaultFigure::draw_box(z, {Color::blue(),Color::white()}); // after one CtcInverse contraction
37+
// z == [ [1.84, 3.5] ; [0, 1] ]
38+
39+
// Combining CtcInverse with a CtcFixpoint:
40+
CtcFixpoint cfix(c);
41+
c.contract(z);
42+
DefaultFigure::draw_box(z, Color::red()); // after a fixed point contraction
43+
// z == [ [1.84, 2.483] ; [0, 1] ]
3344
// [ctcinv-2-end]
3445

3546
// [ctcinv-3-beg]
36-
SepInverse s(f, {0,50});
37-
DefaultFigure::pave({{-6,6},{-6,6}}, s, 1e-2);
47+
DefaultFigure::pave({{-6,6},{-6,6}}, c, 1e-2);
3848
// [ctcinv-3-end]
3949

4050
// [ctcinv-4-beg]
51+
SepInverse s(f, {0,50});
52+
DefaultFigure::pave({{-6,6},{-6,6}}, s, 1e-2);
53+
// [ctcinv-4-end]
54+
55+
// [ctcinv-5-beg]
4156
auto cu = CtcInverse(f,50) | CtcInverse(f,150) | CtcInverse(f,250);
4257
DefaultFigure::pave({{-6,6},{-6,6}}, cu, 1e-2);
43-
// [ctcinv-4-end]
58+
// [ctcinv-5-end]
4459
}
4560

4661
{
47-
// [ctcinv-5-beg]
62+
// [ctcinv-6-beg]
4863
VectorVar x(2);
4964
AnalyticFunction f({x}, x[0]);
5065

@@ -54,19 +69,19 @@ TEST_CASE("CtcInverse - manual")
5469
IntervalVector y({{0.5,3},{-1,1}});
5570
c.contract(y); // {{1,3},{-1,1}}
5671
// Only the first component is constrained by the not-in condition
57-
// [ctcinv-5-end]
72+
// [ctcinv-6-end]
5873

5974
CHECK(y == IntervalVector({{1,3},{-1,1}}));
6075
}
6176

6277
{
63-
// [ctcinv-6-beg]
78+
// [ctcinv-7-beg]
6479
VectorVar x(2);
6580
AnalyticFunction f({x}, x[0]-x[1]);
6681
CtcInverse c(f, 0);
6782
// c.fnc().input_size() == 2
6883
// c.fnc().output_size() == 1
69-
// [ctcinv-6-end]
84+
// [ctcinv-7-end]
7085

7186
CHECK(c.fnc().input_size() == 2);
7287
CHECK(c.fnc().output_size() == 1);

doc/manual/manual/contractors/analytic/src.m

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,34 @@
1717
% [ctcinv-1-end]
1818

1919
% [ctcinv-2-beg]
20-
DefaultFigure().pave(IntervalVector({{-6,6},{-6,6}}), c, 1e-2);
20+
z = IntervalVector({{0.0,3.5},{0.0,1.0}});
21+
DefaultFigure().draw_box(z, StyleProperties({Color().blue(),Color().blue(.1)})); % prior to contraction
22+
z = c.contract(z);
23+
DefaultFigure().draw_box(z, StyleProperties({Color().blue(),Color().white()})); % after one CtcInverse contraction
24+
% z == [ [1.84, 3.5] ; [0, 1] ]
25+
26+
% Combining CtcInverse with a CtcFixpoint:
27+
cfix = CtcFixpoint(c);
28+
z = c.contract(z);
29+
DefaultFigure().draw_box(z, Color().red()); % after a fixed point contraction
30+
% z == [ [1.84, 2.483] ; [0, 1] ]
2131
% [ctcinv-2-end]
2232

2333
% [ctcinv-3-beg]
24-
s = SepInverse(f, Interval(0,50));
25-
DefaultFigure().pave(IntervalVector({{-6,6},{-6,6}}), s, 1e-2);
34+
DefaultFigure().pave(IntervalVector({{-6,6},{-6,6}}), c, 1e-2);
2635
% [ctcinv-3-end]
2736

2837
% [ctcinv-4-beg]
29-
cu = CtcUnion(CtcUnion(CtcInverse(f,50), CtcInverse(f,150)), CtcInverse(f,250));
30-
DefaultFigure().pave(IntervalVector({{-6,6},{-6,6}}), cu, 1e-2)
38+
s = SepInverse(f, Interval(0,50));
39+
DefaultFigure().pave(IntervalVector({{-6,6},{-6,6}}), s, 1e-2);
3140
% [ctcinv-4-end]
3241

3342
% [ctcinv-5-beg]
43+
cu = CtcUnion(CtcUnion(CtcInverse(f,50), CtcInverse(f,150)), CtcInverse(f,250));
44+
DefaultFigure().pave(IntervalVector({{-6,6},{-6,6}}), cu, 1e-2)
45+
% [ctcinv-5-end]
46+
47+
% [ctcinv-6-beg]
3448
x = VectorVar(2);
3549
f = AnalyticFunction({x}, x(1));
3650

@@ -40,14 +54,14 @@
4054
y = IntervalVector({{0.5,3},{-1,1}});
4155
c.contract(y); % [[1,3],[-1,1]]
4256
% Only the first component is constrained by the not-in condition
43-
% [ctcinv-5-end]
57+
% [ctcinv-6-end]
4458

4559
assert(y==IntervalVector({{1,3},{-1,1}}));
4660

47-
% [ctcinv-6-beg]
61+
% [ctcinv-7-beg]
4862
x = VectorVar(2);
4963
f = AnalyticFunction({x}, x(1)-x(2));
5064
c = CtcInverse(f, 0);
5165
assert(c.fnc().input_size() == 2);
5266
assert(c.fnc().output_size() == 1);
53-
% [ctcinv-6-end]
67+
% [ctcinv-7-end]

doc/manual/manual/contractors/analytic/src.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,67 @@ def tests_CtcInverse_manual(test):
2424
c = CtcInverse(f, 50)
2525
# [ctcinv-1-end]
2626

27+
28+
# # Generates the documentation figure:
29+
30+
# sty = PavingStyle(
31+
# [Color.black(),Color.black()],
32+
# [Color.none(),Color.none()],
33+
# [Color.none(),Color.none()])
34+
35+
# l = [
36+
# IntervalVector([[0,3],[2,4]]),
37+
# IntervalVector([[0,3.5],[0,1]]),
38+
# IntervalVector([[-2,-1],[0,3]]),
39+
# IntervalVector([[0,2],[-3,-2]]),
40+
# IntervalVector([[-5,-1],[-4.5,-1]]),
41+
# IntervalVector([[3,4],[-3.5,-0.5]])
42+
# ]
43+
44+
# fig = Figure2D("My figure", GraphicOutput.VIBES | GraphicOutput.IPE)
45+
# fig.set_axes(axis(0,[-6,6]), axis(1,[-6,6]))
46+
47+
# for xi in l:
48+
# fig.draw_box(xi, [Color.blue(),Color.blue(0.1)])
49+
# c1 = CtcFixpoint(c)
50+
# xi = c.contract(xi)
51+
# fig.draw_box(xi, [Color.blue(),Color.white()])
52+
# c1 = CtcFixpoint(c,0)
53+
# xi = c1.contract(xi)
54+
# fig.draw_box(xi, [Color.red(),Color.none()])
55+
56+
# fig.pave([[-6,6],[-6,6]], c, 5e-3, sty)
57+
58+
2759
# [ctcinv-2-beg]
28-
DefaultFigure.pave([[-6,6],[-6,6]], c, 1e-2)
60+
z = IntervalVector([[0,3.5],[0,1]])
61+
DefaultFigure.draw_box(z, [Color.blue(),Color.blue(.1)]) # prior to contraction
62+
z = c.contract(z)
63+
DefaultFigure.draw_box(z, [Color.blue(),Color.white()]) # after one CtcInverse contraction
64+
# z == [ [1.84, 3.5] ; [0, 1] ]
65+
66+
# Combining CtcInverse with a CtcFixpoint:
67+
cfix = CtcFixpoint(c)
68+
z = c.contract(z)
69+
DefaultFigure.draw_box(z, Color.red()) # after a fixed point contraction
70+
# z == [ [1.84, 2.483] ; [0, 1] ]
2971
# [ctcinv-2-end]
3072

3173
# [ctcinv-3-beg]
32-
s = SepInverse(f, [0,50])
33-
DefaultFigure.pave([[-6,6],[-6,6]], s, 1e-2)
74+
DefaultFigure.pave([[-6,6],[-6,6]], c, 1e-2)
3475
# [ctcinv-3-end]
3576

3677
# [ctcinv-4-beg]
37-
cu = CtcInverse(f,50) | CtcInverse(f,150) | CtcInverse(f,250)
38-
DefaultFigure.pave([[-6,6],[-6,6]], cu, 1e-2)
78+
s = SepInverse(f, [0,50])
79+
DefaultFigure.pave([[-6,6],[-6,6]], s, 1e-2)
3980
# [ctcinv-4-end]
4081

4182
# [ctcinv-5-beg]
83+
cu = CtcInverse(f,50) | CtcInverse(f,150) | CtcInverse(f,250)
84+
DefaultFigure.pave([[-6,6],[-6,6]], cu, 1e-2)
85+
# [ctcinv-5-end]
86+
87+
# [ctcinv-6-beg]
4288
x = VectorVar(2)
4389
f = AnalyticFunction([x], x[0])
4490

@@ -48,17 +94,17 @@ def tests_CtcInverse_manual(test):
4894
y = IntervalVector([[0.5,3],[-1,1]])
4995
c.contract(y) # [[1,3],[-1,1]]
5096
# Only the first component is constrained by the not-in condition
51-
# [ctcinv-5-end]
97+
# [ctcinv-6-end]
5298

5399
test.assertTrue(y == IntervalVector([[1,3],[-1,1]]))
54100

55-
# [ctcinv-6-beg]
101+
# [ctcinv-7-beg]
56102
x = VectorVar(2)
57103
f = AnalyticFunction([x], x[0]-x[1])
58104
c = CtcInverse(f, 0)
59105
assert c.fnc().input_size() == 2
60106
assert c.fnc().output_size() == 1
61-
# [ctcinv-6-end]
107+
# [ctcinv-7-end]
62108

63109
if __name__ == '__main__':
64110
unittest.main()

0 commit comments

Comments
 (0)