Skip to content

Commit 20bb9e5

Browse files
authored
Merge pull request #346 from SimonRohou/codac2_dev
Minor updates, graphics + manual
2 parents 6af9fc5 + d49a642 commit 20bb9e5

4 files changed

Lines changed: 214 additions & 130 deletions

File tree

doc/manual/manual/intervals/IntervalVector_class.rst

Lines changed: 61 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,21 @@ A box can be created from:
2525

2626
.. tabs::
2727

28-
.. group-tab:: Python
28+
.. group-tab:: Python
2929

30-
.. code-block:: py
30+
.. literalinclude:: src.py
31+
:language: py
32+
:start-after: [intervalvector-class-1-beg]
33+
:end-before: [intervalvector-class-1-end]
34+
:dedent: 4
3135

32-
# Default box: [-oo,oo]^n
33-
x = IntervalVector(3)
36+
.. group-tab:: C++
3437

35-
# Cube [-1,3]^2
36-
y = IntervalVector(2, Interval(-1,3))
37-
38-
# From a list of bounds (each entry is [lb,ub])
39-
z = IntervalVector([[3,4],[4,6]]) # [3,4]×[4,6]
40-
41-
# From a list of components (Intervals and/or bounds pairs)
42-
q = IntervalVector([y[1], z[0], [0,oo]]) # [-1,3]×[3,4]×[0,oo]
43-
44-
# From a point (degenerate intervals)
45-
p = Vector([0.42,0.42,0.42])
46-
bp = IntervalVector(p) # [0.42,0.42]^3
47-
48-
.. group-tab:: C++
49-
50-
.. code-block:: c++
51-
52-
// Default box: [-oo,oo]^n (Interval default constructor)
53-
IntervalVector x(3);
54-
55-
// Cube [-1,3]^2
56-
IntervalVector y(2, Interval(-1,3));
57-
58-
// From a list of bounds (initializer-list style)
59-
IntervalVector z{{3,4},{4,6}}; // [3,4]×[4,6]
60-
61-
// From a point (degenerate intervals)
62-
Vector p({0.42,0.42,0.42});
63-
IntervalVector bp(p); // [0.42,0.42]^3
38+
.. literalinclude:: src.cpp
39+
:language: c++
40+
:start-after: [intervalvector-class-1-beg]
41+
:end-before: [intervalvector-class-1-end]
42+
:dedent: 4
6443

6544
.. note::
6645

@@ -74,45 +53,22 @@ Components are intervals; indexing is 0-based in Python/C++ and 1-based in Matla
7453

7554
.. tabs::
7655

77-
.. group-tab:: Python
78-
79-
.. code-block:: py
80-
81-
x = IntervalVector(2, [-1,3]) # [-1,3]^2
82-
x[1] = Interval(0,10) # [-1,3]×[0,10]
83-
84-
# Iterating/accessing over components
85-
y = IntervalVector(2)
86-
for i, xi in enumerate(x):
87-
y[i] = xi
88-
89-
# Unpacking (Python convenience)
90-
a,b = x
91-
assert a == x[0] and b == x[1]
92-
93-
# Building a new box from existing components
94-
v = IntervalVector([*x, [3,6]]) # concatenation in Python
95-
# v == [[-1,3]×[0,10]×[3,6]]
96-
97-
# Resize: new components are default-initialized ([-oo,oo])
98-
v.resize(4) # v == [[-1,3]×[0,10]×[3,6]×[-oo,oo]]
99-
s = v.subvector(1,2) # [0,10]×[3,6]
56+
.. group-tab:: Python
10057

101-
.. group-tab:: C++
58+
.. literalinclude:: src.py
59+
:language: py
60+
:start-after: [intervalvector-class-2-beg]
61+
:end-before: [intervalvector-class-2-end]
62+
:dedent: 4
10263

103-
.. code-block:: c++
64+
.. group-tab:: C++
10465

105-
IntervalVector x(2, Interval(-1,3)); // [-1,3]^2
106-
x[1] = Interval(0, 10); // [-1,3]×[0,10]
66+
.. literalinclude:: src.cpp
67+
:language: c++
68+
:start-after: [intervalvector-class-2-beg]
69+
:end-before: [intervalvector-class-2-end]
70+
:dedent: 4
10771

108-
// Accessing components
109-
const Interval& x0 = x[0];
110-
111-
// Resize: new components are default-initialized ([-oo,oo])
112-
x.resize(4); // x == [-1,3]×[0,10]×[-oo,oo]×[-oo,oo]
113-
114-
// Subvector / segment extraction
115-
IntervalVector s = x.subvector(1,2); // [0,10]×[-oo,oo]
11672

11773
Box properties
11874
--------------
@@ -127,30 +83,21 @@ Typical accessors you will use in practice:
12783

12884
.. tabs::
12985

130-
.. group-tab:: Python
131-
132-
.. code-block:: py
133-
134-
x = IntervalVector([[0,2],[-1,3]])
86+
.. group-tab:: Python
13587

136-
n = x.size() # dimension
137-
# Common box information (component-wise):
138-
lo = x.lb() # Vector of lower bounds
139-
hi = x.ub() # Vector of upper bounds
140-
m = x.mid() # Vector of midpoints
141-
d = x.diam() # Vector of diameters
88+
.. literalinclude:: src.py
89+
:language: py
90+
:start-after: [intervalvector-class-3-beg]
91+
:end-before: [intervalvector-class-3-end]
92+
:dedent: 4
14293

143-
.. group-tab:: C++
94+
.. group-tab:: C++
14495

145-
.. code-block:: c++
146-
147-
IntervalVector x{{0,2},{-1,3}};
148-
149-
Index n = x.size();
150-
Vector lo = x.lb();
151-
Vector hi = x.ub();
152-
Vector m = x.mid();
153-
Vector d = x.diam();
96+
.. literalinclude:: src.cpp
97+
:language: c++
98+
:start-after: [intervalvector-class-3-beg]
99+
:end-before: [intervalvector-class-3-end]
100+
:dedent: 4
154101

155102
.. note::
156103

@@ -171,25 +118,21 @@ Common predicates include:
171118

172119
.. tabs::
173120

174-
.. group-tab:: Python
175-
176-
.. code-block:: py
177-
178-
x = IntervalVector([[0,1],[2,3]])
179-
y = IntervalVector([[0.5,2],[1,4]])
121+
.. group-tab:: Python
180122

181-
assert x.intersects(y)
182-
assert x.is_subset(y)
123+
.. literalinclude:: src.py
124+
:language: py
125+
:start-after: [intervalvector-class-4-beg]
126+
:end-before: [intervalvector-class-4-end]
127+
:dedent: 4
183128

184-
.. group-tab:: C++
129+
.. group-tab:: C++
185130

186-
.. code-block:: c++
187-
188-
IntervalVector x{{0,1},{2,3}};
189-
IntervalVector y{{0.5,2},{1,4}};
190-
191-
assert(x.intersects(y));
192-
assert(x.is_subset(y));
131+
.. literalinclude:: src.cpp
132+
:language: c++
133+
:start-after: [intervalvector-class-4-beg]
134+
:end-before: [intervalvector-class-4-end]
135+
:dedent: 4
193136

194137
Advanced operations
195138
-------------------
@@ -220,29 +163,23 @@ Because ``IntervalVector`` is a vector of ``Interval``, standard arithmetic is n
220163
* interactions with real vectors/matrices,
221164
* matrix–vector products involving :class:`~codac.IntervalMatrix`.
222165

223-
.. tabs::
224-
225-
.. group-tab:: Python
226-
227-
.. code-block:: py
228-
229-
x = IntervalVector([[0,1],[2,3]])
230-
y = IntervalVector([[1,2],[0,1]])
231-
232-
z1 = x+y
233-
z2 = 2*x
234-
z3 = x-1
166+
.. tabs::
235167

236-
.. group-tab:: C++
168+
.. group-tab:: Python
237169

238-
.. code-block:: c++
170+
.. literalinclude:: src.py
171+
:language: py
172+
:start-after: [intervalvector-class-5-beg]
173+
:end-before: [intervalvector-class-5-end]
174+
:dedent: 4
239175

240-
IntervalVector x{{0,1},{2,3}};
241-
IntervalVector y{{1,2},{0,1}};
176+
.. group-tab:: C++
242177

243-
IntervalVector z1 = x+y;
244-
IntervalVector z2 = 2.*x;
245-
IntervalVector z3 = x-1.;
178+
.. literalinclude:: src.cpp
179+
:language: c++
180+
:start-after: [intervalvector-class-5-beg]
181+
:end-before: [intervalvector-class-5-end]
182+
:dedent: 4
246183

247184

248185
.. admonition:: Technical documentation

doc/manual/manual/intervals/src.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,77 @@ TEST_CASE("Interval class - manual")
108108
CHECK(Approx<double>(x) == 0.9999999999999999);
109109
CHECK(x != 1.);
110110
}
111+
}
112+
113+
TEST_CASE("IntervalVector class - manual")
114+
{
115+
{
116+
// [intervalvector-class-1-beg]
117+
// Default box: [-oo,oo]^n (Interval default constructor)
118+
IntervalVector x(3);
119+
120+
// Cube [-1,3]^2
121+
IntervalVector y = IntervalVector::constant(2,{-1,3});
122+
123+
// From a list of bounds (initializer-list style)
124+
IntervalVector z{{3,4},{4,6}}; // [3,4]×[4,6]
125+
126+
// From a point (degenerate intervals)
127+
Vector p({0.42,0.42,0.42});
128+
IntervalVector bp(p); // [0.42,0.42]^3
129+
// [intervalvector-class-1-end]
130+
}
131+
132+
{
133+
// [intervalvector-class-2-beg]
134+
IntervalVector x = IntervalVector::constant(2,{-1,3}); // [-1,3]^2
135+
x[1] = Interval(0,10); // [-1,3]×[0,10]
136+
137+
// Accessing components
138+
const Interval& x0 = x[0];
139+
140+
// Resize: new components are default-initialized ([-oo,oo])
141+
x.resize(4); // x == [-1,3]×[0,10]×[-oo,oo]×[-oo,oo]
142+
143+
// Subvector / segment extraction
144+
IntervalVector s = x.subvector(1,2); // [0,10]×[-oo,oo]
145+
// [intervalvector-class-2-end]
146+
147+
(void)x0; // avoid warning on unused variable
148+
}
149+
150+
{
151+
// [intervalvector-class-3-beg]
152+
IntervalVector x{{0,2},{-1,3}};
153+
154+
Index n = x.size();
155+
Vector lo = x.lb();
156+
Vector hi = x.ub();
157+
Vector m = x.mid();
158+
Vector d = x.diam();
159+
// [intervalvector-class-3-end]
160+
161+
(void)n; // avoid warning on unused variable
162+
}
163+
164+
{
165+
// [intervalvector-class-4-beg]
166+
IntervalVector x{{0,1},{2,3}};
167+
IntervalVector y{{-0.5,2},{1,4}};
168+
169+
assert(x.intersects(y));
170+
assert(x.is_subset(y));
171+
// [intervalvector-class-4-end]
172+
}
173+
174+
{
175+
// [intervalvector-class-5-beg]
176+
IntervalVector x{{0,1},{2,3}};
177+
IntervalVector y{{1,2},{0,1}};
178+
179+
IntervalVector z1 = x+y;
180+
IntervalVector z2 = 2.*x;
181+
IntervalVector z3 = x/2.;
182+
// [intervalvector-class-5-end]
183+
}
111184
}

doc/manual/manual/intervals/src.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,80 @@ def tests_Interval_manual(test):
8383
# [interval-class-7-end]
8484
test.assertTrue(Approx(x) == 0.9999999999999999 and x != 1)
8585

86+
87+
def tests_IntervalVector_manual(test):
88+
89+
# [intervalvector-class-1-beg]
90+
# Default box: [-oo,oo]^n
91+
x = IntervalVector(3)
92+
93+
# Cube [-1,3]^2
94+
y = IntervalVector.constant(2,[-1,3])
95+
96+
# From a list of bounds (each entry is [lb,ub])
97+
z = IntervalVector([[3,4],[4,6]]) # [3,4]×[4,6]
98+
99+
# From a list of components (Intervals and/or bounds pairs)
100+
q = IntervalVector([y[1], z[0], [0,oo]]) # [-1,3]×[3,4]×[0,oo]
101+
102+
# From a point (degenerate intervals)
103+
p = Vector([0.42,0.42,0.42])
104+
bp = IntervalVector(p) # [0.42,0.42]^3
105+
# [intervalvector-class-1-end]
106+
107+
108+
# [intervalvector-class-2-beg]
109+
x = IntervalVector.constant(2,[-1,3]) # [-1,3]^2
110+
x[1] = Interval(0,10) # [-1,3]×[0,10]
111+
112+
# Iterating/accessing over components
113+
y = IntervalVector(2)
114+
for i, xi in enumerate(x):
115+
y[i] = xi
116+
117+
# Unpacking (Python convenience)
118+
a,b = x
119+
assert a == x[0] and b == x[1]
120+
121+
# Building a new box from existing components
122+
v = IntervalVector([*x, [3,6]]) # concatenation in Python
123+
# v == [[-1,3]×[0,10]×[3,6]]
124+
125+
# Resize: new components are default-initialized ([-oo,oo])
126+
v.resize(4) # v == [[-1,3]×[0,10]×[3,6]×[-oo,oo]]
127+
s = v.subvector(1,2) # [0,10]×[3,6]
128+
# [intervalvector-class-2-end]
129+
130+
131+
# [intervalvector-class-3-beg]
132+
x = IntervalVector([[0,2],[-1,3]])
133+
134+
n = x.size() # dimension
135+
# Common box information (component-wise):
136+
lo = x.lb() # Vector of lower bounds
137+
hi = x.ub() # Vector of upper bounds
138+
m = x.mid() # Vector of midpoints
139+
d = x.diam() # Vector of diameters
140+
# [intervalvector-class-3-end]
141+
142+
143+
# [intervalvector-class-4-beg]
144+
x = IntervalVector([[0,1],[2,3]])
145+
y = IntervalVector([[-0.5,2],[1,4]])
146+
147+
assert x.intersects(y)
148+
assert x.is_subset(y)
149+
# [intervalvector-class-4-end]
150+
151+
152+
# [intervalvector-class-5-beg]
153+
x = IntervalVector([[0,1],[2,3]])
154+
y = IntervalVector([[1,2],[0,1]])
155+
156+
z1 = x+y
157+
z2 = 2*x
158+
z3 = x/2
159+
# [intervalvector-class-5-end]
160+
86161
if __name__ == '__main__':
87162
unittest.main()

0 commit comments

Comments
 (0)