codac4matlab 2.0.0.dev25__cp314-cp314-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. codac4matlab/__init__.py +4 -0
  2. codac4matlab/_core.cpython-314-aarch64-linux-gnu.so +0 -0
  3. codac4matlab/_graphics.cpython-314-aarch64-linux-gnu.so +0 -0
  4. codac4matlab/_unsupported.cpython-314-aarch64-linux-gnu.so +0 -0
  5. codac4matlab/core/__init__.py +588 -0
  6. codac4matlab/empty.cpython-314-aarch64-linux-gnu.so +0 -0
  7. codac4matlab/graphics/__init__.py +1 -0
  8. codac4matlab/tests/__init__.py +1 -0
  9. codac4matlab/tests/test_AnalyticFunction.py +442 -0
  10. codac4matlab/tests/test_AnalyticTraj.py +62 -0
  11. codac4matlab/tests/test_Approx.py +27 -0
  12. codac4matlab/tests/test_BoolInterval.py +96 -0
  13. codac4matlab/tests/test_Color.py +77 -0
  14. codac4matlab/tests/test_ConvexPolygon.py +300 -0
  15. codac4matlab/tests/test_CtcAction.py +30 -0
  16. codac4matlab/tests/test_CtcCartProd.py +39 -0
  17. codac4matlab/tests/test_CtcCtcBoundary.py +48 -0
  18. codac4matlab/tests/test_CtcDeriv.py +434 -0
  19. codac4matlab/tests/test_CtcEval.py +214 -0
  20. codac4matlab/tests/test_CtcFixpoint.py +65 -0
  21. codac4matlab/tests/test_CtcInter.py +39 -0
  22. codac4matlab/tests/test_CtcInverse.py +213 -0
  23. codac4matlab/tests/test_CtcInverseNotIn.py +129 -0
  24. codac4matlab/tests/test_CtcLazy.py +50 -0
  25. codac4matlab/tests/test_CtcPolygon.py +91 -0
  26. codac4matlab/tests/test_CtcSegment.py +104 -0
  27. codac4matlab/tests/test_Ellipsoid.py +20 -0
  28. codac4matlab/tests/test_GaussJordan.py +34 -0
  29. codac4matlab/tests/test_IntFullPivLU.py +84 -0
  30. codac4matlab/tests/test_Interval.py +254 -0
  31. codac4matlab/tests/test_IntervalMatrix.py +664 -0
  32. codac4matlab/tests/test_IntervalVector.py +657 -0
  33. codac4matlab/tests/test_Interval_operations.py +293 -0
  34. codac4matlab/tests/test_Matrix.py +81 -0
  35. codac4matlab/tests/test_OctaSym.py +41 -0
  36. codac4matlab/tests/test_Parallelepiped.py +35 -0
  37. codac4matlab/tests/test_Parallelepiped_eval.py +62 -0
  38. codac4matlab/tests/test_Polygon.py +120 -0
  39. codac4matlab/tests/test_SampledTraj.py +135 -0
  40. codac4matlab/tests/test_Segment.py +138 -0
  41. codac4matlab/tests/test_SepCartProd.py +34 -0
  42. codac4matlab/tests/test_SepCtcBoundary.py +52 -0
  43. codac4matlab/tests/test_SepInverse.py +104 -0
  44. codac4matlab/tests/test_SepPolygon.py +102 -0
  45. codac4matlab/tests/test_SepProj.py +39 -0
  46. codac4matlab/tests/test_SepTransform.py +41 -0
  47. codac4matlab/tests/test_Slice.py +78 -0
  48. codac4matlab/tests/test_Slice_polygon.py +208 -0
  49. codac4matlab/tests/test_SlicedTube.py +562 -0
  50. codac4matlab/tests/test_SlicedTube_integral.py +380 -0
  51. codac4matlab/tests/test_TDomain.py +115 -0
  52. codac4matlab/tests/test_Vector.py +41 -0
  53. codac4matlab/tests/test_arithmetic_add.py +54 -0
  54. codac4matlab/tests/test_arithmetic_div.py +46 -0
  55. codac4matlab/tests/test_arithmetic_mul.py +114 -0
  56. codac4matlab/tests/test_arithmetic_sub.py +54 -0
  57. codac4matlab/tests/test_capd.py +19 -0
  58. codac4matlab/tests/test_cart_prod.py +38 -0
  59. codac4matlab/tests/test_eigen.py +19 -0
  60. codac4matlab/tests/test_geometry.py +136 -0
  61. codac4matlab/tests/test_hull.py +35 -0
  62. codac4matlab/tests/test_ibex.py +19 -0
  63. codac4matlab/tests/test_inversion.py +57 -0
  64. codac4matlab/tests/test_linear_ctc.py +77 -0
  65. codac4matlab/tests/test_operators.py +377 -0
  66. codac4matlab/tests/test_peibos.py +83 -0
  67. codac4matlab/tests/test_predefined_tubes.py +193 -0
  68. codac4matlab/tests/test_serialization.py +30 -0
  69. codac4matlab/tests/test_transformations.py +61 -0
  70. codac4matlab/tests/test_trunc.py +95 -0
  71. codac4matlab/unsupported/__init__.py +1 -0
  72. codac4matlab/version.py +1 -0
  73. codac4matlab-2.0.0.dev25.dist-info/METADATA +23 -0
  74. codac4matlab-2.0.0.dev25.dist-info/RECORD +76 -0
  75. codac4matlab-2.0.0.dev25.dist-info/WHEEL +6 -0
  76. codac4matlab-2.0.0.dev25.dist-info/top_level.txt +1 -0
@@ -0,0 +1,562 @@
1
+ #!/usr/bin/env python
2
+
3
+ # Codac tests
4
+ # ----------------------------------------------------------------------------
5
+ # \date 2024
6
+ # \author Simon Rohou
7
+ # \copyright Copyright 2024 Codac Team
8
+ # \license GNU Lesser General Public License (LGPL)
9
+
10
+ import unittest
11
+ from codac import *
12
+ import sys
13
+ import math
14
+
15
+ import os
16
+ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '.')))
17
+ import codac2_tests_predefined_tubes as predef
18
+
19
+ def return_a_tube():
20
+ return SlicedTube(
21
+ create_tdomain(Interval(0,2),0.5,False),
22
+ IntervalVector.constant(3,Interval(-1.5,1)))
23
+
24
+ class TestSlicedTube(unittest.TestCase):
25
+
26
+ def test_tdomain_with_sampling_and_values(self):
27
+
28
+ tdomain = create_tdomain()
29
+ self.assertTrue(tdomain.nb_tslices() == 1)
30
+ self.assertTrue(tdomain.t0_tf() == Interval(-oo,oo))
31
+ x = SlicedTube(tdomain, IntervalVector(1))
32
+ x.set(IntervalVector([[1,5]]), [0,1])
33
+ x.set(IntervalVector([[2,8]]), [1,2])
34
+ x.set(IntervalVector([[6,9]]), [2,3])
35
+
36
+ # Checking structure
37
+ v = []
38
+ for s in x:
39
+ v.append(s)
40
+
41
+ self.assertTrue(v[0].t0_tf() == Interval(-oo,0))
42
+ self.assertTrue(v[0].codomain() == IntervalVector([[-oo,oo]]))
43
+ self.assertTrue(v[1].t0_tf() == Interval(0,1))
44
+ self.assertTrue(v[1].codomain() == IntervalVector([[1,5]]))
45
+ self.assertTrue(v[2].t0_tf() == Interval(1,2))
46
+ self.assertTrue(v[2].codomain() == IntervalVector([[2,8]]))
47
+ self.assertTrue(v[3].t0_tf() == Interval(2,3))
48
+ self.assertTrue(v[3].codomain() == IntervalVector([[6,9]]))
49
+ self.assertTrue(v[4].t0_tf() == Interval(3,oo))
50
+ self.assertTrue(v[4].codomain() == IntervalVector([[-oo,oo]]))
51
+
52
+ self.assertTrue(tdomain.tslice(-1.) == Interval(-oo,0))
53
+ self.assertTrue(tdomain.tslice(0.) == Interval(0,1))
54
+ self.assertTrue(tdomain.tslice(0.01) == Interval(0,1))
55
+ self.assertTrue(tdomain.tslice(1) == Interval(1,2))
56
+ self.assertTrue(tdomain.tslice(2) == Interval(2,3))
57
+ self.assertTrue(tdomain.tslice(prev_float(3.)) == Interval(2,3))
58
+ self.assertTrue(tdomain.tslice(3) == Interval(3,oo))
59
+ self.assertTrue(tdomain.tslice(next_float(3.)) == Interval(3,oo))
60
+
61
+ self.assertTrue(tdomain.nb_tslices() == 5) # with [-oo,0] and [3,oo]
62
+ self.assertTrue(x(Interval(0,3)) == IntervalVector([[1,9]]))
63
+ self.assertTrue(x(-1) == IntervalVector(1))
64
+ self.assertTrue(x(0.5) == IntervalVector([[1,5]]))
65
+ self.assertTrue(x(1.5) == IntervalVector([[2,8]]))
66
+ self.assertTrue(x(2.5) == IntervalVector([[6,9]]))
67
+ # No gates: testing values between slices
68
+ self.assertTrue(x(1.) == IntervalVector([[2,5]]))
69
+ self.assertTrue(x(2.) == IntervalVector([[6,8]]))
70
+ self.assertTrue(x(3.) == IntervalVector([[6,9]]))
71
+ self.assertTrue(x(999.) == IntervalVector(1))
72
+
73
+ s0 = x.first_slice()
74
+ self.assertTrue(s0.t0_tf() == Interval(-oo,0))
75
+ self.assertTrue(s0.codomain() == IntervalVector([[-oo,oo]]))
76
+ s1 = s0.next_slice()
77
+ self.assertTrue(s1.t0_tf() == Interval(0,1))
78
+ self.assertTrue(s1.codomain() == IntervalVector([[1,5]]))
79
+ s2 = s1.next_slice()
80
+ self.assertTrue(s2.t0_tf() == Interval(1,2))
81
+ self.assertTrue(s2.codomain() == IntervalVector([[2,8]]))
82
+ s3 = s2.next_slice()
83
+ self.assertTrue(s3.t0_tf() == Interval(2,3))
84
+ self.assertTrue(s3.codomain() == IntervalVector([[6,9]]))
85
+ s4 = s3.next_slice()
86
+ self.assertTrue(s4.t0_tf() == Interval(3,oo))
87
+ self.assertTrue(s4.codomain() == IntervalVector([[-oo,oo]]))
88
+
89
+ self.assertTrue(tdomain.nb_tslices() == 5)
90
+ tdomain.sample(1.3)
91
+ self.assertTrue(tdomain.nb_tslices() == 6)
92
+ self.assertTrue(s2.t0_tf() == Interval(1,1.3))
93
+ self.assertTrue(s2.codomain() == IntervalVector([[2,8]]))
94
+ s2bis = s2.next_slice()
95
+ self.assertTrue(s2bis.t0_tf() == Interval(1.3,2))
96
+ self.assertTrue(s2bis.codomain() == IntervalVector([[2,8]]))
97
+ self.assertTrue(s3.t0_tf() == Interval(2,3))
98
+ self.assertTrue(s3.codomain() == IntervalVector([[6,9]]))
99
+
100
+ def test_sampling_inside_tdomain(self):
101
+
102
+ tdomain = create_tdomain()
103
+ self.assertTrue(tdomain.t0_tf() == Interval(-oo,oo))
104
+ self.assertTrue(tdomain.nb_tslices() == 1)
105
+ tdomain.sample(1., False)
106
+ self.assertTrue(tdomain.t0_tf() == Interval(-oo,oo))
107
+ self.assertTrue(tdomain.nb_tslices() == 2)
108
+ tdomain.sample(1., False)
109
+ self.assertTrue(tdomain.t0_tf() == Interval(-oo,oo))
110
+ self.assertTrue(tdomain.nb_tslices() == 2)
111
+ tdomain.sample(1., True)
112
+ self.assertTrue(tdomain.t0_tf() == Interval(-oo,oo))
113
+ self.assertTrue(tdomain.nb_tslices() == 3)
114
+ it = tdomain.sample(10., True)
115
+ self.assertTrue(tdomain.t0_tf() == Interval(-oo,oo))
116
+ self.assertTrue(tdomain.nb_tslices() == 5)
117
+ self.assertTrue(it == Interval(10.))
118
+ it = tdomain.sample(15., False)
119
+ self.assertTrue(tdomain.t0_tf() == Interval(-oo,oo))
120
+ self.assertTrue(tdomain.nb_tslices() == 6)
121
+ self.assertTrue(it == Interval(15.,oo))
122
+
123
+ def test_sampling_outside_tdomain(self):
124
+
125
+ tdomain = create_tdomain(Interval(0,0.5))
126
+ self.assertTrue(tdomain.t0_tf() == Interval(0,0.5))
127
+ self.assertTrue(tdomain.nb_tslices() == 1)
128
+ tdomain.sample(1., False)
129
+ self.assertTrue(tdomain.t0_tf() == Interval(0,1))
130
+ self.assertTrue(tdomain.nb_tslices() == 2)
131
+ tdomain.sample(1., False)
132
+ self.assertTrue(tdomain.t0_tf() == Interval(0,1))
133
+ self.assertTrue(tdomain.nb_tslices() == 2)
134
+ tdomain.sample(1., False)
135
+ self.assertTrue(tdomain.t0_tf() == Interval(0,1))
136
+ self.assertTrue(tdomain.nb_tslices() == 2)
137
+ tdomain.sample(1., True)
138
+ self.assertTrue(tdomain.t0_tf() == Interval(0,1))
139
+ self.assertTrue(tdomain.nb_tslices() == 3)
140
+ it = tdomain.sample(10., True)
141
+ self.assertTrue(tdomain.t0_tf() == Interval(0,10))
142
+ self.assertTrue(tdomain.nb_tslices() == 5)
143
+ self.assertTrue(it == Interval(10.))
144
+ it = tdomain.sample(15., False)
145
+ self.assertTrue(tdomain.t0_tf() == Interval(0,15))
146
+ self.assertTrue(tdomain.nb_tslices() == 6)
147
+ self.assertTrue(it == Interval(10,15))
148
+
149
+ def test_basic_SlicedTube(self):
150
+
151
+ tdomain = create_tdomain(Interval(0,1), 0.1, False)
152
+ x = SlicedTube(tdomain, IntervalVector(3))
153
+
154
+ self.assertTrue(x.size() == 3)
155
+ self.assertTrue(x.tdomain() == tdomain)
156
+ self.assertTrue(x.t0_tf() == Interval(0,1))
157
+ self.assertTrue(x.nb_slices() == tdomain.nb_tslices())
158
+ self.assertTrue(x.nb_slices() == 10)
159
+ self.assertTrue(x.first_slice().t0_tf() == Interval(0,0.1))
160
+ self.assertTrue(Approx(x.last_slice().t0_tf()) == Interval(0.9,1))
161
+ self.assertTrue(x.codomain() == IntervalVector(3))
162
+ x.set(IntervalVector.constant(3, Interval(-10,10)))
163
+ self.assertTrue(x.codomain() == IntervalVector.constant(3, Interval(-10,10)))
164
+
165
+ # Eval
166
+ self.assertTrue(tdomain.nb_tubes() == 1)
167
+ self.assertTrue(x(Interval(-oo,oo)) == IntervalVector(3))
168
+ self.assertTrue(x(Interval(-1,1)) == IntervalVector(3))
169
+ self.assertTrue(x(tdomain.t0_tf()) == x.codomain())
170
+ self.assertTrue(x(-42.) == IntervalVector(3))
171
+
172
+ # Eval: affectation at scalar t
173
+ self.assertTrue(tdomain.nb_tslices() == 10)
174
+ x.set(IntervalVector.constant(3,Interval(2.,3.)), -42.)
175
+ self.assertTrue(tdomain.nb_tslices() == 12)
176
+
177
+ # Checking structure
178
+ v = []
179
+ for s in x:
180
+ v.append(s)
181
+ self.assertTrue(v[0].t0_tf() == Interval(-42.))
182
+ self.assertTrue(v[0].codomain() == IntervalVector.constant(3,Interval(2.,3.)))
183
+ self.assertTrue(v[1].t0_tf() == Interval(-42.,0.))
184
+ self.assertTrue(v[1].codomain() == IntervalVector(3))
185
+ self.assertTrue(v[2].t0_tf() == Interval(0.,0.1))
186
+ self.assertTrue(v[2].codomain() == IntervalVector.constant(3,Interval(-10,10)))
187
+ self.assertTrue(v[3].t0_tf() == Interval(0.1,0.2))
188
+ self.assertTrue(v[3].codomain() == IntervalVector.constant(3,Interval(-10,10)))
189
+
190
+ self.assertTrue(x(-42.) == IntervalVector.constant(3,Interval(2.,3.)))
191
+ self.assertTrue(x(prev_float(-42.)) == IntervalVector(3))
192
+ self.assertTrue(x(next_float(-42.)) == IntervalVector(3))
193
+
194
+ # Eval: affectation at interval t
195
+ self.assertTrue(x.codomain() == IntervalVector(3))
196
+ self.assertTrue(tdomain.nb_tslices() == 12)
197
+ x.set(IntervalVector.constant(3,Interval(9.,10.)), Interval(44,55))
198
+ self.assertTrue(tdomain.nb_tslices() == 14)
199
+
200
+ v.clear()
201
+ for s in x:
202
+ v.append(s)
203
+ self.assertTrue(Approx(v[11].t0_tf()) == Interval(0.9,1))
204
+ self.assertTrue(v[11].codomain() == IntervalVector.constant(3,Interval(-10,10)))
205
+ self.assertTrue(v[12].t0_tf() == Interval(1,44))
206
+ self.assertTrue(v[12].codomain() == IntervalVector(3))
207
+ self.assertTrue(v[13].t0_tf() == Interval(44,55))
208
+ self.assertTrue(v[13].codomain() == IntervalVector.constant(3,Interval(9.,10.)))
209
+
210
+ self.assertTrue(x(Interval(44,55)) == IntervalVector.constant(3,Interval(9.,10.)))
211
+ self.assertTrue(x(prev_float(44.)) == IntervalVector(3))
212
+ self.assertTrue(x(next_float(55.)) == IntervalVector(3))
213
+
214
+ def test_sliceT(self):
215
+
216
+ tdomain = create_tdomain(Interval(0,1), 0.1, False)
217
+ x = SlicedTube(tdomain, IntervalVector(2))
218
+ self.assertTrue(x.nb_slices() == 10)
219
+ # C++ like: self.assertTrue(tdomain.tslice(-oo) == tdomain.end())
220
+ # C++ like: self.assertTrue(tdomain.tslice(oo) == tdomain.end())
221
+
222
+ for s in x:
223
+ s.set(IntervalVector.constant(2,s.t0_tf()))
224
+
225
+ v = []
226
+ for s in x:
227
+ v.append(s)
228
+
229
+ self.assertTrue(v[0].t0_tf() == Interval(0,0.1))
230
+ self.assertTrue(v[0].input_gate() == IntervalVector.constant(2,Interval(0.,0.1))) # nothing before
231
+ self.assertTrue(v[0].codomain() == IntervalVector.constant(2,Interval(0,0.1)))
232
+ self.assertTrue(v[0].output_gate() == IntervalVector.constant(2,Interval(0.1)))
233
+
234
+ self.assertTrue(Approx(v[9].t0_tf()) == Interval(0.9,1.))
235
+ self.assertTrue(v[9].input_gate() == v[8].output_gate())
236
+ self.assertTrue(Approx(v[9].codomain()) == IntervalVector.constant(2,Interval(0.9,1.)))
237
+ self.assertTrue(Approx(v[9].input_gate()) == IntervalVector.constant(2,Interval(0.9)))
238
+ self.assertTrue(Approx(v[9].output_gate()) == IntervalVector.constant(2,Interval(0.9,1.))) # nothing after
239
+
240
+ def test_again_1(self):
241
+
242
+ tdomain = create_tdomain(Interval(1,10), 0.01, True) # last argument creates "gates" (degenerated slices at scalar timesteps)
243
+ t = ScalarVar()
244
+ x = SlicedTube(tdomain, AnalyticFunction(
245
+ [t],
246
+ [
247
+ sin(sqrt(t)+((t-5)^2)*Interval(-0.01,0.01)),
248
+ cos(t)+sin(t/0.2)*Interval(-0.1,0.1)
249
+ ]))
250
+ u = SlicedTube(tdomain, IntervalVector(2))
251
+ self.assertTrue(x.size() == 2)
252
+ self.assertTrue(Approx(x.codomain(),1e-4) == IntervalVector([[-0.267392,1],[-1.06721,1.06721]]))
253
+
254
+ def test_function_returning_a_tube(self):
255
+
256
+ x = return_a_tube()
257
+ self.assertTrue(x.tdomain().t0_tf() == Interval(0,2))
258
+ self.assertTrue(x.size() == 3)
259
+ self.assertTrue(x.codomain()[1] == Interval(-1.5,1))
260
+
261
+ def test_setting_values(self):
262
+
263
+ tdomain = create_tdomain(Interval(0,10),1.,True) # with gates
264
+ x = SlicedTube(tdomain, Interval(-10,10))
265
+ self.assertTrue(x.codomain() == Interval(-10,10))
266
+ self.assertTrue(x(0.) == Interval(-10,10))
267
+ self.assertTrue(x(Interval(0,1)) == Interval(-10,10))
268
+ self.assertTrue(x(Interval(1,1)) == Interval(-10,10))
269
+
270
+ def test_validity_copy_of_tubes(self):
271
+
272
+ tdomain = create_tdomain(Interval(0,5), 0.01, True)
273
+
274
+ x1 = SlicedTube(tdomain, Interval(-1,1))
275
+ x2 = SlicedTube(tdomain, Interval(1))
276
+ cx1 = SlicedTube(x1) # copy
277
+ cx2 = SlicedTube(x2) # copy
278
+
279
+ self.assertTrue(cx1 == x1)
280
+ self.assertTrue(cx2 == x2)
281
+ self.assertTrue(cx1 != x2)
282
+ self.assertTrue(cx2 != x1)
283
+
284
+ cx1.set(Interval(42))
285
+ cx2.set(Interval(-3))
286
+
287
+ self.assertTrue(cx1 != x1)
288
+ self.assertTrue(cx2 != x2)
289
+
290
+ def test_tube_not_empty_if_built_from_a_AnalyticFunction(self):
291
+
292
+ tdomain = create_tdomain(Interval(0,5), 0.01, True)
293
+ t = ScalarVar()
294
+ f = AnalyticFunction([t], 5*sin(2*t)+t)
295
+ aa1 = SlicedTube(tdomain, f)
296
+ self.assertTrue(not aa1.is_empty())
297
+
298
+ def test_tube_evaluation(self):
299
+
300
+ tdomain = create_tdomain(Interval(0,5), 0.1, True)
301
+ t = ScalarVar()
302
+ f = AnalyticFunction([t], 10*cos(t)+t)
303
+ a = SlicedTube(tdomain, f)
304
+ self.assertTrue(Approx(tdomain.tslice(2.)) == Interval(1.900000000000001, 2.000000000000002))
305
+ self.assertTrue(Approx(a(Interval(1,2)),1e-4) == Interval(-2.17496, 7.13757))
306
+
307
+ def test_specific_detected_bug_from_sampling(self):
308
+
309
+ tdomain = create_tdomain([0.,46.], 0.5, False)
310
+ x = SlicedTube(tdomain, Interval())
311
+ tdomain.sample(46, False)
312
+ x.set([-1,3], [30,31])
313
+ self.assertTrue(x([30,31]) == Interval(-1,3))
314
+ x.set([-1,3], [45,46])
315
+ self.assertTrue(x([45,46]) == Interval(-1,3))
316
+ self.assertTrue(x([45.5,46]) == Interval(-1,3))
317
+ self.assertTrue(x(45.8) == Interval(-1,3))
318
+ self.assertTrue(x(45.2) == Interval(-1,3))
319
+
320
+ def test_SlicedTube_as_operator_1dcase(self):
321
+
322
+ t = ScalarVar()
323
+ f = AnalyticFunction([t], cos(t))
324
+ analytic_traj = AnalyticTraj(f, [-PI,PI])
325
+ sampled_traj = analytic_traj.sampled(1e-2)
326
+ tdomain = create_tdomain([-PI,PI],1e-2,False)
327
+ tube = SlicedTube(tdomain, sampled_traj)
328
+ g = tube.as_function()
329
+
330
+ h = AnalyticFunction([t], g(t))
331
+
332
+ t_ = -math.pi
333
+ while t_ < math.pi:
334
+ self.assertTrue(Approx(h.real_eval(t_),1e-4) == math.cos(t_))
335
+ t_=t_+1e-2
336
+
337
+ def test_SlicedTube_as_operator_ndcase(self):
338
+
339
+ t = ScalarVar()
340
+ f = AnalyticFunction(
341
+ [t],
342
+ vec(2*cos(t),sin(2*t))
343
+ )
344
+
345
+ analytic_traj = AnalyticTraj(f, [0,5])
346
+ sampled_traj = analytic_traj.sampled(1e-2)
347
+ tdomain = create_tdomain([0,5],1e-3,False)
348
+ tube = SlicedTube(tdomain, sampled_traj)
349
+ g = tube.as_function()
350
+
351
+ h = AnalyticFunction(
352
+ [t],
353
+ g(t)
354
+ )
355
+
356
+ t_ = 0
357
+ while t_ < 5:
358
+ self.assertTrue(Approx(h.eval(t_),1e-2) == IntervalVector([2*math.cos(t_),math.sin(2*t_)]))
359
+ t_=t_+1e-2
360
+
361
+ h = AnalyticFunction(
362
+ [t],
363
+ [ g(t)[0],g(t)[1] ]
364
+ )
365
+
366
+ t_ = 0
367
+ while t_ < 5:
368
+ self.assertTrue(Approx(h.eval(t_),1e-2) == IntervalVector([2*math.cos(t_),math.sin(2*t_)]))
369
+ t_=t_+1e-2
370
+
371
+
372
+ def test_inversion_scalar_tube(self):
373
+
374
+ x = predef.tube_test_1()
375
+ x.set_ith_slice(Interval(-4,2), 14) # to test primitives pre-computation
376
+
377
+ self.assertTrue(x.invert([0], x.tdomain().t0_tf()) == Interval(3,46))
378
+ self.assertTrue(x.invert([-7], x.tdomain().t0_tf()) == Interval(4,12))
379
+ self.assertTrue(x.invert(Interval(), x.tdomain().t0_tf()) == Interval(0,46))
380
+ self.assertTrue(x.invert([-12,14], x.tdomain().t0_tf()) == Interval(0,46))
381
+ self.assertTrue(x.invert([-20,-18], x.tdomain().t0_tf()) == Interval.empty())
382
+ self.assertTrue(x.invert([-1,1], x.tdomain().t0_tf()) == Interval(2,46))
383
+ self.assertTrue(x.invert([-10.5], x.tdomain().t0_tf()) == Interval(7,8))
384
+ self.assertTrue(x.invert([-12,-7], x.tdomain().t0_tf()) == Interval(4,12))
385
+ self.assertTrue(x.invert([10,11], x.tdomain().t0_tf()) == Interval(20,27))
386
+ self.assertTrue(x.invert([6.01,7], x.tdomain().t0_tf()) == Interval(0,30))
387
+ self.assertTrue(x.invert([6,7], x.tdomain().t0_tf()) == Interval(0,43))
388
+ self.assertTrue(x.invert([5.9,7], x.tdomain().t0_tf()) == Interval(0,43))
389
+
390
+
391
+ def test_inversion_scalar_tube_subsets(self):
392
+
393
+ x = predef.tube_test_1()
394
+ x.set_ith_slice(Interval(-4,2), 14) # to test primitives pre-computation
395
+
396
+ v = []
397
+
398
+ x.invert(Interval(0), v, x.tdomain().t0_tf())
399
+ self.assertTrue(len(v) == 4)
400
+
401
+ if len(v) == 4:
402
+ self.assertTrue(v[0] == Interval(3,4))
403
+ self.assertTrue(v[1] == Interval(14,17))
404
+ self.assertTrue(v[2] == Interval(37,42))
405
+ self.assertTrue(v[3] == Interval(43,46))
406
+
407
+ # The same, with a custom domain:
408
+ x.invert(Interval(0), v, Interval(3.8,42.5))
409
+ self.assertTrue(len(v) == 3)
410
+
411
+ if len(v) == 3:
412
+ self.assertTrue(v[0] == Interval(3.8,4))
413
+ self.assertTrue(v[1] == Interval(14,17))
414
+ self.assertTrue(v[2] == Interval(37,42))
415
+
416
+ x.invert(Interval(-1,1), v, x.tdomain().t0_tf())
417
+ self.assertTrue(len(v) == 4)
418
+
419
+ if len(v) == 4:
420
+ self.assertTrue(v[0] == Interval(2,5))
421
+ self.assertTrue(v[1] == Interval(13,17))
422
+ self.assertTrue(v[2] == Interval(34,35))
423
+ self.assertTrue(v[3] == Interval(36,46))
424
+
425
+ # The same, with a custom domain (empty):
426
+ x.invert(Interval(-1,1), v, Interval.empty())
427
+ self.assertTrue(len(v) == 0)
428
+
429
+ x.invert(Interval(-6.9999), v, x.tdomain().t0_tf())
430
+ self.assertTrue(len(v) == 2)
431
+
432
+ if len(v) == 2:
433
+ self.assertTrue(v[0] == Interval(4,7))
434
+ self.assertTrue(v[1] == Interval(8,12))
435
+
436
+ x.invert(Interval(), v, x.tdomain().t0_tf())
437
+ self.assertTrue(len(v) == 1)
438
+
439
+ if len(v) == 1:
440
+ self.assertTrue(v[0] == Interval(0,46))
441
+
442
+ x.invert(Interval(-30,-29), v, x.tdomain().t0_tf())
443
+ self.assertTrue(len(v) == 0)
444
+
445
+ x.invert(Interval(3.5), v, x.tdomain().t0_tf())
446
+ self.assertTrue(len(v) == 5)
447
+
448
+ if len(v) == 5:
449
+ self.assertTrue(v[0] == Interval(1,4))
450
+ self.assertTrue(v[1] == Interval(15,18))
451
+ self.assertTrue(v[2] == Interval(26,27))
452
+ self.assertTrue(v[3] == Interval(30,38))
453
+ self.assertTrue(v[4] == Interval(40,45))
454
+
455
+ x.invert(Interval(9.5,30), v, x.tdomain().t0_tf())
456
+ self.assertTrue(len(v) == 1)
457
+
458
+ if len(v) == 1:
459
+ self.assertTrue(v[0] == Interval(20,27))
460
+
461
+ x.invert(Interval(12,13), v, x.tdomain().t0_tf())
462
+ self.assertTrue(len(v) == 1)
463
+
464
+ if len(v) == 1:
465
+ self.assertTrue(v[0] == Interval(22,25))
466
+
467
+ x.invert(Interval(-4,-3), v, x.tdomain().t0_tf())
468
+ self.assertTrue(len(v) == 3)
469
+
470
+ if len(v) == 3:
471
+ self.assertTrue(v[0] == Interval(3,5))
472
+ self.assertTrue(v[1] == Interval(9,10))
473
+ self.assertTrue(v[2] == Interval(11,15))
474
+
475
+
476
+ def test_inversion_derivative(self):
477
+
478
+ tdomain = create_tdomain([0,5],1.)
479
+ x = SlicedTube(tdomain, Interval())
480
+ v = SlicedTube(tdomain, Interval())
481
+
482
+ x.set([0], 0)
483
+ x.set([4], 5)
484
+
485
+ v.set_ith_slice([1,2], 0*2+1)
486
+ v.set_ith_slice((Interval(1)/2) | (Interval(3)/2), 1*2+1)
487
+ v.set_ith_slice(Interval(0) | (Interval(1)/2), 2*2+1)
488
+ v.set_ith_slice([0], 3*2+1)
489
+ v.set_ith_slice((Interval(-1)/2) | (Interval(1)/2), 4*2+1)
490
+
491
+ ctc = CtcDeriv(TimePropag.FWD_BWD)
492
+ ctc.contract(x, v)
493
+ ctc.contract(x, v)
494
+ ctc.contract(x, v)
495
+
496
+ self.assertTrue(x.invert(x.codomain(), x.tdomain().t0_tf()) == x.tdomain().t0_tf())
497
+ self.assertTrue(x.invert(Interval(), x.tdomain().t0_tf()) == x.tdomain().t0_tf())
498
+
499
+ # Using derivative
500
+ self.assertTrue(x.invert(x.codomain(), v, x.tdomain().t0_tf()) == x.tdomain().t0_tf())
501
+ self.assertTrue(x.invert(Interval(), v, x.tdomain().t0_tf()) == x.tdomain().t0_tf())
502
+ self.assertTrue(x.invert(Interval(0.), v, x.tdomain().t0_tf()) == Interval(0))
503
+ self.assertTrue(Approx(x.slice(tdomain.tslice(4.5)).invert(Interval(4.25), v.slice(tdomain.tslice(4.5)), tdomain.tslice(4.5)),1e-10) == Interval(9)/2)
504
+ self.assertTrue(x.invert(Interval(17)/4, v, x.tdomain().t0_tf()) == Interval(Interval(9)/2))
505
+ self.assertTrue(Approx(x.invert(Interval(4), v, x.tdomain().t0_tf()),1e-10) == Interval(3,5))
506
+
507
+ self.assertTrue(Approx(x.slice(tdomain.tslice(4.5)).invert(Interval(41)/10, v.slice(tdomain.tslice(4.5)), tdomain.tslice(4.5)),1e-10) == ((Interval(21)/5)|(Interval(24)/5)))
508
+
509
+ self.assertTrue(Approx(x.invert(Interval(41)/10, v, x.tdomain().t0_tf()),1e-10) == ((Interval(21)/5)|(Interval(24)/5)))
510
+ self.assertTrue(x.invert((Interval(7)/2), v, x.tdomain().t0_tf()) == Interval(2,4))
511
+ self.assertTrue(x.invert(Interval.empty(), v, x.tdomain().t0_tf()) == Interval.empty())
512
+ self.assertTrue(x.invert(Interval(10), v, x.tdomain().t0_tf()) == Interval.empty())
513
+ self.assertTrue(x.invert(Interval(2,3), v, x.tdomain().t0_tf()) == Interval(1,2))
514
+ self.assertTrue(x.invert(Interval(1), v, x.tdomain().t0_tf()) == Interval(0.5,0.75))
515
+ self.assertTrue(x.invert((Interval(7)/2) | Interval(4), v, x.tdomain().t0_tf()) == Interval(2,5))
516
+
517
+
518
+ def test_inversion_another_test(self):
519
+
520
+ tdomain = create_tdomain([-20,20],0.05)
521
+ t = ScalarVar()
522
+ f = AnalyticFunction([t], Interval(-1,1)*((t^2)+1))
523
+ x = SlicedTube(tdomain, f)
524
+ self.assertTrue(x.invert(0., x.tdomain().t0_tf()) == x.tdomain().t0_tf())
525
+
526
+
527
+ def test_inversion_vector_tube(self):
528
+
529
+ x0 = predef.tube_test_1()
530
+ x = SlicedTube(x0.tdomain(), IntervalVector(2))
531
+
532
+ for xi, x0i in zip(x, x0):
533
+ xi.set(cart_prod(x0i.codomain(), x0i.codomain() - 3), False)
534
+
535
+ inv_val = IntervalVector.constant(2,[0.5,0.5])
536
+ v_t = []
537
+ x.invert(inv_val, v_t, x.tdomain().t0_tf())
538
+
539
+ self.assertTrue(len(v_t) == 5)
540
+ self.assertTrue(v_t[0] == Interval(3,4))
541
+ self.assertTrue(v_t[1] == Interval(15,17))
542
+ self.assertTrue(v_t[2] == Interval(37,38))
543
+ self.assertTrue(v_t[3] == Interval(40,42))
544
+ self.assertTrue(v_t[4] == Interval(43,45))
545
+
546
+ # Union inversion:
547
+ inv = x.invert(inv_val, x.tdomain().t0_tf())
548
+ self.assertTrue(inv == Interval(3,45))
549
+
550
+ # Restricted domain
551
+ restricted = Interval(15.2,39)
552
+ x.invert(inv_val, v_t, restricted)
553
+ self.assertTrue(len(v_t) == 2)
554
+ self.assertTrue(v_t[0] == Interval(15.2,17))
555
+ self.assertTrue(v_t[1] == Interval(37,38))
556
+
557
+ inv = x.invert(inv_val, restricted)
558
+ self.assertTrue(inv == Interval(15.2,38))
559
+
560
+
561
+ if __name__ == '__main__':
562
+ unittest.main()