diff --git a/doc/manual/index.rst b/doc/manual/index.rst index b31797914..1d2f31f56 100644 --- a/doc/manual/index.rst +++ b/doc/manual/index.rst @@ -263,6 +263,7 @@ User manual * Analytic contractors * :ref:`sec-ctc-analytic-ctcinverse` * CtcInverseNotIn + * :ref:`sec-ctc-analytic-sepimage` * Dynamic contractors * :ref:`sec-ctc-dynamic-ctclohner` * Geometric contractors @@ -282,10 +283,10 @@ User manual * Temporal contractors * Using static contractors on tubes * CtcDeriv - * CtcEval + * :ref:`sec-ctc-dynamic-ctceval` * CtcDelay * CtcLinobs - * CtcLohner + * :ref:`sec-ctc-dynamic-ctclohner` * CtcPicard * CtcChain * CtcDiffInclusion diff --git a/doc/manual/manual/contractors/dynamic/ctceval.rst b/doc/manual/manual/contractors/dynamic/ctceval.rst new file mode 100644 index 000000000..3b92c3f26 --- /dev/null +++ b/doc/manual/manual/contractors/dynamic/ctceval.rst @@ -0,0 +1,394 @@ +.. _sec-ctc-dynamic-ctceval: + +The CtcEval contractor +====================== + + Main author: `Simon Rohou `_ + + +.. doxygenclass:: codac2::CtcEval + :project: codac + +The evaluation constraint is particularly useful in state estimation when both +an observation and its acquisition time are uncertain. A position measurement, +for example, may be available only within a time window due to synchronization +errors. + +Basic usage +----------- + +Consider a trajectory :math:`x(\cdot)` enclosed in a tube :math:`[x](\cdot)`, +its derivative :math:`v(\cdot)` enclosed in :math:`[v](\cdot)`, and an observation +:math:`[t_i]\times[z_i]`. The contractor implements the constraints + +.. math:: + + \left.\begin{array}{r} + z_i=x(t_i)\\ + \dot{x}(\cdot)=v(\cdot) + \end{array}\right\} + \longrightarrow + \mathcal{C}_{\mathrm{eval}}\big([t_i],[z_i],[x](\cdot),[v](\cdot)\big). + +The observation specifies that the trajectory must visit the set :math:`[z_i]` +at **some** instant in :math:`[t_i]`, not at every instant in this interval. +The derivative bounds constrain the evolution of the trajectory before and after +that instant. + +The contractor updates ``t``, ``z`` and ``x`` in place. The derivative tube +``v`` is not contracted, although its shared temporal partition may be refined +by the insertion of gates. + +Scalar observation +------------------ + +The following example models the uncertain derivative of a sinusoidal trajectory +on :math:`[t_0,t_f]=[-\pi,\pi/2]`: + +.. math:: + + [v](t)=\cos(t)+[-0.1,0.1],\qquad [x](t_0)=[-0.1,0.1]. + +An initial state tube is built with ``v.primitive()`` using the prescribed +initial interval. A measurement is then supplied in the time-value plane: + +.. math:: + + [t_i]\times[z_i]=[-0.5,0.3]\times[0.3,1.1]. + +.. tabs:: + + .. group-tab:: Python + + .. literalinclude:: src.py + :language: py + :start-after: [ctceval-scalar-beg] + :end-before: [ctceval-scalar-end] + :dedent: 4 + + .. group-tab:: C++ + + .. literalinclude:: src.cpp + :language: c++ + :start-after: [ctceval-scalar-beg] + :end-before: [ctceval-scalar-end] + :dedent: 2 + + .. group-tab:: Matlab + + .. literalinclude:: src.m + :language: matlab + :start-after: [ctceval-scalar-beg] + :end-before: [ctceval-scalar-end] + +The contraction reduces the possible observation time and value. To understand +why the tube can also be contracted, consider any time :math:`\tau` and a +candidate measurement instant :math:`s\in[t_i]`. A feasible trajectory satisfies + +.. math:: + + x(\tau)=x(s)+\int_s^{\tau}v(u)\,du. + +An enclosure of its possible values at :math:`\tau` can therefore be expressed as + +.. math:: + + [x](\tau)\ \cap\ + \left[\bigcup_{s\in[t_i]} + \left(\big([x](s)\cap[z_i]\big)+\int_s^{\tau}[v](u)\,du\right) + \right], + +where the outer square brackets denote an interval hull. This is the continuous +relation underlying the transport of feasible observation values. The sliced +implementation works with the temporal partition and uses derivative-aware +inversion and evaluation, followed by propagation between gates. It is not in +general an exact computation of the continuous feasible set. + +.. figure:: img/ceval_x.png + :alt: Local contraction of a scalar tube and an uncertain observation + :width: 70% + + Local contraction of the scalar tube by ``CtcEval``. The light gray region + shows the part of the tube removed by contraction, and the dark gray region + shows the remaining tube. The blue and red boxes represent the initial and + contracted observation, respectively. The curve and dot show a feasible + trajectory and observation. + +Temporal propagation +-------------------- + +``CtcEval`` first narrows the temporal interval using tube inversion and then +narrows the observed value using tube evaluation. It performs local temporal +propagation within the contracted observation interval. When the time is a +singleton, it can directly update the corresponding gate. + +To propagate this information to the rest of the trajectory, apply ``CtcDeriv`` +without a temporal restriction: + +.. tabs:: + + .. group-tab:: Python + + .. literalinclude:: src.py + :language: py + :start-after: [ctceval-propagation-beg] + :end-before: [ctceval-propagation-end] + :dedent: 4 + + .. group-tab:: C++ + + .. literalinclude:: src.cpp + :language: c++ + :start-after: [ctceval-propagation-beg] + :end-before: [ctceval-propagation-end] + :dedent: 2 + + .. group-tab:: Matlab + + .. literalinclude:: src.m + :language: matlab + :start-after: [ctceval-propagation-beg] + :end-before: [ctceval-propagation-end] + +.. figure:: img/ceval_x_sweep.png + :alt: Scalar tube after global temporal propagation + :width: 70% + + Global temporal propagation by ``CtcDeriv`` following the local contraction + by ``CtcEval``. The light gray region shows the part of the initial tube + removed by the two contractors, and the dark gray region shows the remaining + tube. + +This separation is useful when several measurements constrain the same state +trajectory. Local updates can be collected first, and a single forward/backward +sweep can then propagate their information throughout the tube. + +Several observations and partial primitives +------------------------------------------- + +Suppose that, in addition to the first scalar measurement, there are two more +observations. Each has its own uncertain time and measured value. All of them +constrain the same ``x`` and ``v``. + +For repeated calls, ``CtcEval`` accepts a pair of partial primitives computed +outside the contractor. For :math:`[v](s)=[v^-(s),v^+(s)]`, these tubes enclose + +.. math:: + + p^-(t)=\int_{t_0}^{t}v^-(s)\,ds,\qquad + p^+(t)=\int_{t_0}^{t}v^+(s)\,ds. + +For certain dates :math:`a\leq b`, the integral of the derivative tube is +bounded by the differences of these primitives: + +.. math:: + + \int_a^b[v](s)\,ds + =\big[p^-(b)-p^-(a),\ p^+(b)-p^+(a)\big]. + +The equality above refers to the exact lower and upper primitives. The +precomputed tubes provide enclosures of these quantities. For backward +integration, the bounds are exchanged and negated. This avoids traversing all +intermediate slices for every transport. See :ref:`sec-tubes-partial-primitives` +for details on this optimization. + +In the code below, ``x`` and ``v`` are initialized as in the scalar example, +before any observation is injected. The primitives are computed once and reused +through two sweeps of the observation constraints and the derivative constraint. + +.. tabs:: + + .. group-tab:: Python + + .. literalinclude:: src.py + :language: py + :start-after: [ctceval-multi-beg] + :end-before: [ctceval-multi-end] + :dedent: 4 + + .. group-tab:: C++ + + .. literalinclude:: src.cpp + :language: c++ + :start-after: [ctceval-multi-beg] + :end-before: [ctceval-multi-end] + :dedent: 2 + + .. group-tab:: Matlab + + .. literalinclude:: src.m + :language: matlab + :start-after: [ctceval-multi-beg] + :end-before: [ctceval-multi-end] + +Precomputation trades memory and initialization cost for faster integration +queries. For short or infrequent observation windows, precomputation is not +necessarily beneficial. + +.. note:: + + The partial primitives must be recomputed if the derivative envelopes or + the temporal extent change. Refinement of the shared ``TDomain`` by slice + duplication preserves their validity but can make the new gate enclosures + less precise. + +.. figure:: img/ceval_multi.gif + :alt: Local contractions followed by propagation along a scalar tube + :width: 70% + + Contraction of a scalar tube using three uncertain observations. The animation + alternates between local contractions by ``CtcEval`` and the result of global + temporal propagation by ``CtcDeriv``. The light gray regions show the parts + of the tube removed by contraction, and the dark gray region shows the + remaining tube. Blue and red boxes represent the initial and contracted + observations, respectively. + +Two sweeps are used here for illustration. In general, repeat the contractions +until a stopping criterion is met, for instance a negligible reduction in tube +volume and observation widths, or a prescribed iteration budget. A single +sweep does not imply that a fixpoint has been reached. + +2d localization example +----------------------- + +Contracting the position tube +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Consider a robot following a Lissajous-type trajectory over :math:`[0,5]`. +Its initial position is unknown, but its velocity is enclosed by + +.. math:: + + [\mathbf{v}](t)= + \begin{pmatrix}-2\sin(t)\\2\cos(2t)\end{pmatrix} + +[-0.01,0.01]^2. + +A position observation gives + +.. math:: + + [t_i]=[4.3,4.4],\qquad + [\mathbf{z}_i]=[-0.73,-0.69]\times[0.64,0.68]. + +The position tube is initially unbounded. The evaluation contractor anchors it +locally, and the derivative contractor propagates this anchor in both time +directions. + +.. tabs:: + + .. group-tab:: Python + + .. literalinclude:: src.py + :language: py + :start-after: [ctceval-localization-beg] + :end-before: [ctceval-localization-end] + :dedent: 4 + + .. group-tab:: C++ + + .. literalinclude:: src.cpp + :language: c++ + :start-after: [ctceval-localization-beg] + :end-before: [ctceval-localization-end] + :dedent: 2 + + .. group-tab:: Matlab + + .. literalinclude:: src.m + :language: matlab + :start-after: [ctceval-localization-beg] + :end-before: [ctceval-localization-end] + +In this example, the observation alone does not distinguish any time or +position within the original observation box. Its main role is to bound the +position trajectory using the velocity information. The nominal curve +:math:`(2\cos(t),\sin(2t))^\mathsf{T}` is one feasible trajectory. + +.. figure:: img/ceval_lissajous_tube.png + :alt: Planar Lissajous position tube bounded by an uncertain observation + :width: 70% + + Position tube obtained from an initially unbounded tube by combining an uncertain + position observation with velocity information. The blue gradient represents + time along the tube. The white curve is a feasible trajectory and the yellow + robot indicates a feasible pose at the observation time. + +Contracting the observation box +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Conversely, suppose that a position tube is already available: + +.. math:: + + [\mathbf{x}](t)= + \begin{pmatrix}2\cos(t)\\\sin(2t)\end{pmatrix} + +[-0.05,0.05]^2. + +The robot is detected somewhere in :math:`[-1,0]\times[0.4,1.2]`, with no +information about its acquisition time except the temporal domain of the tube. +Using the same derivative tube as before, one can estimate when this detection +could have occurred and refine its spatial enclosure: + +.. tabs:: + + .. group-tab:: Python + + .. literalinclude:: src.py + :language: py + :start-after: [ctceval-inversion-beg] + :end-before: [ctceval-inversion-end] + :dedent: 4 + + .. group-tab:: C++ + + .. literalinclude:: src.cpp + :language: c++ + :start-after: [ctceval-inversion-beg] + :end-before: [ctceval-inversion-end] + :dedent: 2 + + .. group-tab:: Matlab + + .. literalinclude:: src.m + :language: matlab + :start-after: [ctceval-inversion-beg] + :end-before: [ctceval-inversion-end] + +.. figure:: img/ceval_lissajous_box.png + :alt: Planar Lissajous tube with a contracted observation box + :width: 70% + + Contraction of an uncertain position observation using a known position tube. + The blue and red boxes represent the initial and contracted position domains, + respectively. The blue gradient represents time along the tube, the white curve + is a feasible trajectory and the yellow robot indicates a feasible pose at the + observation time. + +When the derivative is unavailable and only the observation domains need to be +refined, use tube inversion and evaluation directly: +``t &= x.invert(z,t)`` and, provided ``t`` is non-empty, ``z &= x(t)``. +These operations do not impose an observation constraint on the tube itself. + +Known observation time +---------------------- + +A known acquisition time :math:`t_i` is represented by a degenerate +``Interval``. If the observation is feasible, the contractor contracts the +observation value and updates the gate of :math:`[x](\cdot)` at :math:`t_i`, +creating it if necessary. No temporal propagation is performed in this case. +Use ``CtcDeriv`` to propagate the information to the rest of the tube. + +Related content +--------------- + +.. admonition:: Related publication + + | **Reliable non-linear state estimation involving time uncertainties** + | S. Rohou, L. Jaulin, L. Mihaylova, F. Le Bars, S. M. Veres + | *Automatica*, 93:379--388, July 2018. + | `DOI: 10.1016/j.automatica.2018.03.074 `_ + | `Author manuscript `_ + +.. admonition:: Technical documentation + + See the `C++ API documentation of this class <../../../api/html/classcodac2_1_1_ctc_eval.html>`_. diff --git a/doc/manual/manual/contractors/dynamic/img/ceval_lissajous_box.png b/doc/manual/manual/contractors/dynamic/img/ceval_lissajous_box.png new file mode 100644 index 000000000..6416ab3b8 Binary files /dev/null and b/doc/manual/manual/contractors/dynamic/img/ceval_lissajous_box.png differ diff --git a/doc/manual/manual/contractors/dynamic/img/ceval_lissajous_tube.png b/doc/manual/manual/contractors/dynamic/img/ceval_lissajous_tube.png new file mode 100644 index 000000000..f5e246c38 Binary files /dev/null and b/doc/manual/manual/contractors/dynamic/img/ceval_lissajous_tube.png differ diff --git a/doc/manual/manual/contractors/dynamic/img/ceval_multi.gif b/doc/manual/manual/contractors/dynamic/img/ceval_multi.gif new file mode 100644 index 000000000..a97ff4ffb Binary files /dev/null and b/doc/manual/manual/contractors/dynamic/img/ceval_multi.gif differ diff --git a/doc/manual/manual/contractors/dynamic/img/ceval_multi.png b/doc/manual/manual/contractors/dynamic/img/ceval_multi.png new file mode 100644 index 000000000..67bfefd46 Binary files /dev/null and b/doc/manual/manual/contractors/dynamic/img/ceval_multi.png differ diff --git a/doc/manual/manual/contractors/dynamic/img/ceval_multi_sweep.png b/doc/manual/manual/contractors/dynamic/img/ceval_multi_sweep.png new file mode 100644 index 000000000..4c9de7b22 Binary files /dev/null and b/doc/manual/manual/contractors/dynamic/img/ceval_multi_sweep.png differ diff --git a/doc/manual/manual/contractors/dynamic/img/ceval_x.png b/doc/manual/manual/contractors/dynamic/img/ceval_x.png new file mode 100644 index 000000000..83e0a27af Binary files /dev/null and b/doc/manual/manual/contractors/dynamic/img/ceval_x.png differ diff --git a/doc/manual/manual/contractors/dynamic/img/ceval_x_sweep.png b/doc/manual/manual/contractors/dynamic/img/ceval_x_sweep.png new file mode 100644 index 000000000..ad0e875b6 Binary files /dev/null and b/doc/manual/manual/contractors/dynamic/img/ceval_x_sweep.png differ diff --git a/doc/manual/manual/contractors/dynamic/img/src/ceval_lissajous_box.py b/doc/manual/manual/contractors/dynamic/img/src/ceval_lissajous_box.py new file mode 100644 index 000000000..3f0b0330e --- /dev/null +++ b/doc/manual/manual/contractors/dynamic/img/src/ceval_lissajous_box.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import math +from codac import * + +td = create_tdomain([0,5],0.01,True) +s = ScalarVar() +v = SlicedTube(td,AnalyticFunction([s],[-2*sin(s),2*cos(2*s)])) +v.inflate(0.01) + +# Reproduce the preceding example to obtain the same shared temporal partition +x = SlicedTube(td,IntervalVector(2)) +t = Interval(4.3,4.4) +z = IntervalVector([[-0.73,-0.69],[0.64,0.68]]) +ctc_eval = CtcEval() +ctc_eval.contract(t,z,x,v) +CtcDeriv().contract(x,v) + +y = SlicedTube(td,AnalyticFunction([s],[2*cos(s),sin(2*s)])) +y.inflate(0.05) +t = td.t0_tf() +z = IntervalVector([[-1,0],[0.4,1.2]]) + +z_initial = IntervalVector(z) +ctc_eval.contract(t,z,y,v) +assert not y.is_empty() and not y.is_unbounded() +print('Contracted tube:',y) +print('Contracted observation:',t,z) + +fig = Figure2D('ceval_lissajous_box',GraphicOutput.VIBES | GraphicOutput.IPE) +fig.set_window_properties([50,50],[750,480]) +fig.set_axes([[-2.5,2.5],[-1.6,1.6]]) + +# Draw the contracted tube and both observation boxes +fig.draw_tube(y,StyleGradientProperties(ColorMap.blue_tube(),'z:0')) +fig.draw_box(z_initial,StyleProperties(Color('#148EB5'),'z:2')) +fig.draw_box(z,StyleProperties(Color('#E02020'),'z:3')) + +# Reference trajectory and feasible measurement +truth_color = Color.white() +truth = [Vector([2*math.cos(k/120),math.sin(2*k/120)]) for k in range(601)] +fig.draw_polyline(truth,StyleProperties(truth_color,'z:1')) +tr = 4.35 +zr = Vector([2*math.cos(tr),math.sin(2*tr)]) +assert t.contains(tr) and z.contains(zr) and y(tr).contains(zr) +theta = math.atan2(2*math.cos(2*tr),-2*math.sin(tr)) +fig.draw_tank(Vector([zr[0],zr[1],theta]),0.25, + StyleProperties([Color.black(),Color.yellow()],'z:4')) + diff --git a/doc/manual/manual/contractors/dynamic/img/src/ceval_lissajous_tube.py b/doc/manual/manual/contractors/dynamic/img/src/ceval_lissajous_tube.py new file mode 100644 index 000000000..2f6014917 --- /dev/null +++ b/doc/manual/manual/contractors/dynamic/img/src/ceval_lissajous_tube.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import math +from codac import * + +td = create_tdomain([0,5],0.01,True) +s = ScalarVar() +v = SlicedTube(td,AnalyticFunction([s],[-2*sin(s),2*cos(2*s)])) +v.inflate(0.01) + +x = SlicedTube(td,IntervalVector(2)) +t = Interval(4.3,4.4) +z = IntervalVector([[-0.73,-0.69],[0.64,0.68]]) + +CtcEval().contract(t,z,x,v) +CtcDeriv().contract(x,v) +assert not x.is_empty() and not x.is_unbounded() + +fig = Figure2D('ceval_lissajous_tube',GraphicOutput.VIBES | GraphicOutput.IPE) +fig.set_window_properties([50,50],[750,480]) +fig.set_axes([[-2.5,2.5],[-1.6,1.6]]) + +# Position tube after temporal propagation +fig.draw_tube(x,StyleGradientProperties(ColorMap.blue_tube(),'position_tube','z:-2')) + +# Reference trajectory and observation +truth_color = Color.white() +truth = [Vector([2*math.cos(k/120),math.sin(2*k/120)]) for k in range(601)] +fig.draw_polyline(truth,StyleProperties(truth_color,'reference_trajectory','z:1')) +fig.draw_box(z,StyleProperties(Color('#148EB5'),'observation','z:2')) +tr = 4.35 +zr = Vector([2*math.cos(tr),math.sin(2*tr)]) +assert t.contains(tr) and z.contains(zr) and x(tr).contains(zr) +theta = math.atan2(2*math.cos(2*tr),-2*math.sin(tr)) +fig.draw_tank(Vector([zr[0],zr[1],theta]),0.25, + StyleProperties([Color.black(),Color.yellow()],'robot','z:4')) + +print('Contracted observation:',t,z) diff --git a/doc/manual/manual/contractors/dynamic/img/src/ceval_multi.py b/doc/manual/manual/contractors/dynamic/img/src/ceval_multi.py new file mode 100644 index 000000000..463705350 --- /dev/null +++ b/doc/manual/manual/contractors/dynamic/img/src/ceval_multi.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import math +from codac import * + + +def draw_boxes(fig, boxes, color, layer, z): + style = StyleProperties([color,color],layer,f'z:{z}') + for b in boxes: + fig.draw_box(b,style) + + +def tube_boxes(x): + return [IntervalVector([s.t0_tf(),s.codomain()]) for s in x if not s.is_gate()] + + +def draw_frame(name, x, t, z, contracted): + fig = Figure2D(name,GraphicOutput.VIBES | GraphicOutput.IPE) + fig.set_window_properties([50,50],[800,500]) + fig.set_axes([[-PI-0.1,PI/2+0.1],[-1.6,1.7]]) + draw_boxes(fig,initial_tube,Color('#D6D6D6'),'initial_tube',-3) + if contracted: + draw_boxes(fig,tube_boxes(x),Color('#888888'),'contracted_tube',-2) + + for b in initial_observations: + fig.draw_box(b,StyleProperties(blue,'initial_observations','z:1')) + if contracted: + for ti, zi in zip(t,z): + fig.draw_box(IntervalVector([ti,zi]),StyleProperties(red,'contracted_observations','z:2')) + + fig.draw_polyline(truth,StyleProperties(truth_color,'reference_trajectory','z:3')) + for tr in ref_times: + zr = math.sin(tr)+0.1+0.03*tr + fig.draw_circle(Vector([tr,zr]),0.025,StyleProperties([truth_color,truth_color],'measurements','z:4')) + + return fig + + +td = create_tdomain([-PI,PI/2],0.01,True) +s = ScalarVar() +v = SlicedTube(td,AnalyticFunction([s],cos(s)+Interval(-0.1,0.1))) +x = v.primitive(Interval(-0.1,0.1)) + +t = [Interval(-0.5,0.3),Interval(-0.6,0.8),Interval(-2.3,-2.2)] +z = [Interval(0.3,1.1),Interval(-0.5,-0.4),Interval(-0.8,-0.7)] +initial_tube = tube_boxes(x) +initial_observations = [IntervalVector([ti,zi]) for ti, zi in zip(t,z)] +blue = Color('#148EB5') +red = Color('#E02020') +truth_color = Color('#003E5F') +truth = [Vector([ti,math.sin(ti)+0.1+0.03*ti]) + for ti in [-PI+1.5*PI*k/600 for k in range(601)]] +ref_times = [0.25,-0.55,-2.25] + +# Keep every stage in a separate VIBes window and IPE file +figures = [draw_frame('ceval_multi_00_initial',x,t,z,False)] +ctc_eval = CtcEval() +ctc_deriv = CtcDeriv() +primitives = v.partial_primitives() + +for i in range(2): + for j in range(len(t)): + ctc_eval.contract(t[j],z[j],x,v,primitives) # Optional primitives argument for faster integration + + assert not x.is_empty() + figures.append(draw_frame(f'ceval_multi_{2*i+1:02d}_local',x,t,z,True)) + ctc_deriv.contract(x,v) + assert not x.is_empty() + figures.append(draw_frame(f'ceval_multi_{2*i+2:02d}_propagated',x,t,z,True)) + +for i, tr in enumerate(ref_times): + zr = math.sin(tr)+0.1+0.03*tr + assert t[i].contains(tr) and z[i].contains(zr) and x(tr).contains(zr) + print('Contracted observation:',t[i],z[i]) diff --git a/doc/manual/manual/contractors/dynamic/img/src/ceval_x.py b/doc/manual/manual/contractors/dynamic/img/src/ceval_x.py new file mode 100644 index 000000000..522be3698 --- /dev/null +++ b/doc/manual/manual/contractors/dynamic/img/src/ceval_x.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import math +from codac import * + + +def draw_tube(fig, x, color, layer, z): + style = StyleProperties([color,color],layer,f'z:{z}') + for s in x: + if not s.is_gate(): + fig.draw_box(IntervalVector([s.t0_tf(),s.codomain()]),style) + + +td = create_tdomain([-PI,PI/2],0.01,True) +s = ScalarVar() +v = SlicedTube(td,AnalyticFunction([s],cos(s)+Interval(-0.1,0.1))) +x = v.primitive(Interval(-0.1,0.1)) +t, z = Interval(-0.5,0.3), Interval(0.3,1.1) + +fig = Figure2D('ceval_x',GraphicOutput.VIBES | GraphicOutput.IPE) +fig.set_window_properties([50,50],[800,500]) +fig.set_axes([[-PI-0.1,PI/2+0.1],[-1.6,1.7]]) + +# Initial tube and observation +blue = Color('#148EB5') +red = Color('#E02020') +truth_color = Color('#003E5F') +draw_tube(fig,x,Color('#D6D6D6'),'initial_tube',-3) +fig.draw_box(IntervalVector([t,z]),StyleProperties(blue,'initial_observation','z:1')) + +# Local contraction followed by global temporal propagation +CtcEval().contract(t,z,x,v) +#CtcDeriv().contract(x,v) +assert not x.is_empty() +draw_tube(fig,x,Color('#888888'),'contracted_tube',-2) +fig.draw_box(IntervalVector([t,z]),StyleProperties(red,'contracted_observation','z:2')) + +# Reference trajectory and a feasible measurement +truth = [Vector([ti,math.sin(ti)+0.1+0.03*ti]) + for ti in [-PI+1.5*PI*k/600 for k in range(601)]] +fig.draw_polyline(truth,StyleProperties(truth_color,'reference_trajectory','z:3')) +tr = 0.25 +zr = math.sin(tr)+0.1+0.03*tr +assert t.contains(tr) and z.contains(zr) and x(tr).contains(zr) +fig.draw_circle(Vector([tr,zr]),0.025,StyleProperties([truth_color,truth_color],'measurement','z:4')) + +print('Contracted observation:',t,z) diff --git a/doc/manual/manual/contractors/dynamic/src.cpp b/doc/manual/manual/contractors/dynamic/src.cpp new file mode 100644 index 000000000..c6ae21b73 --- /dev/null +++ b/doc/manual/manual/contractors/dynamic/src.cpp @@ -0,0 +1,140 @@ +/** + * Codac tests + * ---------------------------------------------------------------------------- + * \date 2026 + * \author Simon Rohou + * \copyright Copyright 2026 Codac Team + * \license GNU Lesser General Public License (LGPL) + */ + +#include +#include +#include +#include +#include + +using namespace std; +using namespace codac2; + +TEST_CASE("CtcEval - manual, scalar observation") +{ + // [ctceval-scalar-beg] + auto td = create_tdomain({-PI,PI/2.},0.01,true); + ScalarVar s; + SlicedTube v(td, AnalyticFunction({s},cos(s)+Interval(-0.1,0.1))); + auto x = v.primitive(Interval(-0.1,0.1)); + + Interval t(-0.5,0.3), z(0.3,1.1); + CtcEval ctc_eval; + ctc_eval.contract(t,z,x,v); + // [ctceval-scalar-end] + + CHECK(!x.is_empty()); + CHECK(t.is_subset(Interval(-0.5,0.3))); + CHECK(z.is_subset(Interval(0.3,1.1))); + CHECK(t.lb() > -0.5); + CHECK(z.ub() < 1.1); + double ref = std::sin(0.25)+0.1+0.03*0.25; + CHECK(t.contains(0.25)); + CHECK(z.contains(ref)); + + // [ctceval-propagation-beg] + CtcDeriv ctc_deriv; + ctc_deriv.contract(x,v); + // [ctceval-propagation-end] + + CHECK(!x.is_empty()); + CHECK(x(0.25).contains(ref)); +} + +TEST_CASE("CtcEval - manual, several observations") +{ + auto td = create_tdomain({-PI,PI/2.},0.01,true); + ScalarVar s; + SlicedTube v(td, AnalyticFunction({s},cos(s)+Interval(-0.1,0.1))); + auto x = v.primitive(Interval(-0.1,0.1)); + + // [ctceval-multi-beg] + vector t = {{-0.5,0.3},{-0.6,0.8},{-2.3,-2.2}}; + vector z = {{0.3,1.1},{-0.5,-0.4},{-0.8,-0.7}}; + + CtcEval ctc_eval; + CtcDeriv ctc_deriv; + auto primitives = v.partial_primitives(); + + for(int iter = 0 ; iter < 2 ; iter++) + { + for(size_t i = 0 ; i < t.size() ; i++) + ctc_eval.contract(t[i],z[i],x,v,primitives); // Optional primitives argument for faster integration + ctc_deriv.contract(x,v); + } + // [ctceval-multi-end] + + REQUIRE(!x.is_empty()); + vector ref_times = {0.25,-0.55,-2.25}; + for(size_t i = 0 ; i < t.size() ; i++) + { + double ti = ref_times[i]; + double zi = std::sin(ti)+0.1+0.03*ti; + CHECK(t[i].contains(ti)); + CHECK(z[i].contains(zi)); + CHECK(x(ti).contains(zi)); + } +} + +TEST_CASE("CtcEval - manual, 2d localization") +{ + // [ctceval-localization-beg] + auto td = create_tdomain({0,5},0.01,true); + ScalarVar s; + SlicedTube v(td, AnalyticFunction({s},{-2.*sin(s),2.*cos(2.*s)})); + v.inflate(0.01); + + SlicedTube x(td, IntervalVector(2)); + Interval t(4.3,4.4); + IntervalVector z({{-0.73,-0.69},{0.64,0.68}}); + + CtcEval ctc_eval; + ctc_eval.contract(t,z,x,v); + CtcDeriv().contract(x,v); + // [ctceval-localization-end] + + REQUIRE(!x.is_empty()); + CHECK(!x.is_unbounded()); + CHECK(t == Interval(4.3,4.4)); + CHECK(z == IntervalVector({{-0.73,-0.69},{0.64,0.68}})); + CHECK(x(0.).contains(Vector({2.,0.}))); + CHECK(x(4.35).contains(Vector({2.*std::cos(4.35),std::sin(8.7)}))); + + // [ctceval-inversion-beg] + SlicedTube y(td, AnalyticFunction({s},{2.*cos(s),sin(2.*s)})); + y.inflate(0.05); + t = td->t0_tf(); + z = IntervalVector({{-1.,0.},{0.4,1.2}}); + + ctc_eval.contract(t,z,y,v); + // [ctceval-inversion-end] + + REQUIRE(!y.is_empty()); + CHECK(t.is_subset(Interval(4.,4.7))); + CHECK(t.contains(4.35)); + CHECK(z.is_subset(IntervalVector({{-1.,0.},{0.4,1.2}}))); + CHECK(z[0].ub() < 0.); + CHECK(z[1].ub() < 1.2); + CHECK(z.contains(Vector({2.*std::cos(4.35),std::sin(8.7)}))); +} + +TEST_CASE("CtcEval - manual, known observation time") +{ + auto td = create_tdomain({0,3},1.,true); + SlicedTube x(td, Interval()), v(td, Interval(1.)); + Interval t(1.5), z(2.5); + CtcEval().contract(t,z,x,v); + CHECK(x(1.5) == Interval(2.5)); + CtcDeriv().contract(x,v); + + CHECK(t == Interval(1.5)); + CHECK(z == Interval(2.5)); + CHECK(x(0.) == Interval(1.)); + CHECK(x(3.) == Interval(4.)); +} diff --git a/doc/manual/manual/contractors/dynamic/src.m b/doc/manual/manual/contractors/dynamic/src.m new file mode 100644 index 000000000..9f87624ec --- /dev/null +++ b/doc/manual/manual/contractors/dynamic/src.m @@ -0,0 +1,116 @@ +% Codac tests +% ---------------------------------------------------------------------------- +% \date 2026 +% \author Simon Rohou +% \copyright Copyright 2026 Codac Team +% \license GNU Lesser General Public License (LGPL) + +import py.codac4matlab.* + +% [ctceval-scalar-beg] +td = create_tdomain(Interval(-PI,PI/2),0.01,true); +s = ScalarVar(); +v = SlicedTube(td, AnalyticFunction({s},cos(s)+Interval(-0.1,0.1))); +x = v.primitive(Interval(-0.1,0.1)); + +t = Interval(-0.5,0.3); +z = Interval(0.3,1.1); +ctc_eval = CtcEval(); +ctc_eval.contract(t,z,x,v); +% [ctceval-scalar-end] + +assert(~logical(x.is_empty())); +assert(logical(t.is_subset(Interval(-0.5,0.3)))); +assert(logical(z.is_subset(Interval(0.3,1.1)))); +assert(double(t.lb()) > -0.5); +assert(double(z.ub()) < 1.1); +ref = sin(0.25)+0.1+0.03*0.25; +assert(logical(t.contains(0.25))); +assert(logical(z.contains(ref))); + +% [ctceval-propagation-beg] +ctc_deriv = CtcDeriv(); +ctc_deriv.contract(x,v); +% [ctceval-propagation-end] + +assert(~logical(x.is_empty())); +xt = x(0.25); +assert(logical(xt.contains(ref))); + + +td = create_tdomain(Interval(-PI,PI/2),0.01,true); +s = ScalarVar(); +v = SlicedTube(td, AnalyticFunction({s},cos(s)+Interval(-0.1,0.1))); +x = v.primitive(Interval(-0.1,0.1)); + +% [ctceval-multi-beg] +t = {Interval(-0.5,0.3),Interval(-0.6,0.8),Interval(-2.3,-2.2)}; +z = {Interval(0.3,1.1),Interval(-0.5,-0.4),Interval(-0.8,-0.7)}; + +ctc_eval = CtcEval(); +ctc_deriv = CtcDeriv(); +primitives = v.partial_primitives(); + +for iter = 1:2 + for i = 1:numel(t) + ctc_eval.contract(t{i},z{i},x,v,primitives); % Optional primitives argument for faster integration + end + ctc_deriv.contract(x,v); +end +% [ctceval-multi-end] + +assert(~logical(x.is_empty())); +ref_times = [0.25,-0.55,-2.25]; +for i = 1:numel(t) + ti = ref_times(i); + zi = sin(ti)+0.1+0.03*ti; + assert(logical(t{i}.contains(ti))); + assert(logical(z{i}.contains(zi))); + xt = x(ti); + assert(logical(xt.contains(zi))); +end + + +% [ctceval-localization-beg] +td = create_tdomain(Interval(0,5),0.01,true); +s = ScalarVar(); +v = SlicedTube(td, AnalyticFunction({s},vec(-2*sin(s),2*cos(2*s)))); +v.inflate(0.01); + +x = SlicedTube(td, IntervalVector(2)); +t = Interval(4.3,4.4); +z = IntervalVector({{-0.73,-0.69},{0.64,0.68}}); + +ctc_eval = CtcEval(); +ctc_eval.contract(t,z,x,v); +ctc_deriv = CtcDeriv(); +ctc_deriv.contract(x,v); +% [ctceval-localization-end] + +assert(~logical(x.is_empty())); +assert(~logical(x.is_unbounded())); +assert(t == Interval(4.3,4.4)); +assert(z == IntervalVector({{-0.73,-0.69},{0.64,0.68}})); +xt = x(0.); +assert(logical(xt.contains(Vector({2.,0.})))); +xt = x(4.35); +assert(logical(xt.contains(Vector({2*cos(4.35),sin(8.7)})))); + +% [ctceval-inversion-beg] +y = SlicedTube(td, AnalyticFunction({s},vec(2*cos(s),sin(2*s)))); +y.inflate(0.05); +t = td.t0_tf(); +z = IntervalVector({{-1,0},{0.4,1.2}}); + +ctc_eval.contract(t,z,y,v); +% [ctceval-inversion-end] + +assert(~logical(y.is_empty())); +assert(logical(t.is_subset(Interval(4.,4.7)))); +assert(logical(t.contains(4.35))); +assert(logical(z.is_subset(IntervalVector({{-1,0},{0.4,1.2}})))); +z1 = z(1); +z2 = z(2); +assert(double(z1.ub()) < 0.); +assert(double(z2.ub()) < 1.2); +assert(logical(z.contains(Vector({2*cos(4.35),sin(8.7)})))); diff --git a/doc/manual/manual/contractors/dynamic/src.py b/doc/manual/manual/contractors/dynamic/src.py new file mode 100644 index 000000000..6b8d2aa24 --- /dev/null +++ b/doc/manual/manual/contractors/dynamic/src.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python +# Codac tests +# ---------------------------------------------------------------------------- +# \date 2026 +# \author Simon Rohou +# \copyright Copyright 2026 Codac Team +# \license GNU Lesser General Public License (LGPL) + +import math +import unittest +from codac import * + +class TestCtcDynamicManual(unittest.TestCase): + + def test_ctceval_scalar(self): + + # [ctceval-scalar-beg] + td = create_tdomain([-PI,PI/2],0.01,True) + s = ScalarVar() + v = SlicedTube(td, AnalyticFunction([s],cos(s)+Interval(-0.1,0.1))) + x = v.primitive(Interval(-0.1,0.1)) + + t, z = Interval(-0.5,0.3), Interval(0.3,1.1) + ctc_eval = CtcEval() + ctc_eval.contract(t,z,x,v) + # [ctceval-scalar-end] + + self.assertFalse(x.is_empty()) + self.assertTrue(t.is_subset(Interval(-0.5,0.3))) + self.assertTrue(z.is_subset(Interval(0.3,1.1))) + self.assertGreater(t.lb(),-0.5) + self.assertLess(z.ub(),1.1) + ref = math.sin(0.25)+0.1+0.03*0.25 + self.assertTrue(t.contains(0.25)) + self.assertTrue(z.contains(ref)) + + # [ctceval-propagation-beg] + ctc_deriv = CtcDeriv() + ctc_deriv.contract(x,v) + # [ctceval-propagation-end] + + self.assertFalse(x.is_empty()) + self.assertTrue(x(0.25).contains(ref)) + + def test_ctceval_multi(self): + + td = create_tdomain([-PI,PI/2],0.01,True) + s = ScalarVar() + v = SlicedTube(td, AnalyticFunction([s],cos(s)+Interval(-0.1,0.1))) + x = v.primitive(Interval(-0.1,0.1)) + + # [ctceval-multi-beg] + t = [Interval(-0.5,0.3),Interval(-0.6,0.8),Interval(-2.3,-2.2)] + z = [Interval(0.3,1.1),Interval(-0.5,-0.4),Interval(-0.8,-0.7)] + + ctc_eval = CtcEval() + ctc_deriv = CtcDeriv() + primitives = v.partial_primitives() + + for iter in range(2): + for i in range(len(t)): + ctc_eval.contract(t[i],z[i],x,v,primitives) # Optional primitives argument for faster integration + ctc_deriv.contract(x,v) + # [ctceval-multi-end] + + self.assertFalse(x.is_empty()) + ref_times = [0.25,-0.55,-2.25] + for i, ti in enumerate(ref_times): + zi = math.sin(ti)+0.1+0.03*ti + self.assertTrue(t[i].contains(ti)) + self.assertTrue(z[i].contains(zi)) + self.assertTrue(x(ti).contains(zi)) + + def test_ctceval_localization(self): + + # [ctceval-localization-beg] + td = create_tdomain([0,5],0.01,True) + s = ScalarVar() + v = SlicedTube(td, AnalyticFunction([s],[-2*sin(s),2*cos(2*s)])) + v.inflate(0.01) + + x = SlicedTube(td, IntervalVector(2)) + t = Interval(4.3,4.4) + z = IntervalVector([[-0.73,-0.69],[0.64,0.68]]) + + ctc_eval = CtcEval() + ctc_eval.contract(t,z,x,v) + CtcDeriv().contract(x,v) + # [ctceval-localization-end] + + self.assertFalse(x.is_empty()) + self.assertFalse(x.is_unbounded()) + self.assertEqual(t,Interval(4.3,4.4)) + self.assertEqual(z,IntervalVector([[-0.73,-0.69],[0.64,0.68]])) + self.assertTrue(x(0.).contains(Vector([2.,0.]))) + self.assertTrue(x(4.35).contains(Vector([2*math.cos(4.35),math.sin(8.7)]))) + + # [ctceval-inversion-beg] + y = SlicedTube(td, AnalyticFunction([s],[2*cos(s),sin(2*s)])) + y.inflate(0.05) + t = td.t0_tf() + z = IntervalVector([[-1,0],[0.4,1.2]]) + + ctc_eval.contract(t,z,y,v) + # [ctceval-inversion-end] + + self.assertFalse(y.is_empty()) + self.assertTrue(t.is_subset(Interval(4.,4.7))) + self.assertTrue(t.contains(4.35)) + self.assertTrue(z.is_subset(IntervalVector([[-1,0],[0.4,1.2]]))) + self.assertLess(z[0].ub(),0.) + self.assertLess(z[1].ub(),1.2) + self.assertTrue(z.contains(Vector([2*math.cos(4.35),math.sin(8.7)]))) + + def test_ctceval_known_time(self): + + td = create_tdomain([0,3],1.,True) + x = SlicedTube(td, Interval()) + v = SlicedTube(td, Interval(1.)) + t, z = Interval(1.5), Interval(2.5) + CtcEval().contract(t,z,x,v) + self.assertEqual(x(1.5),Interval(2.5)) + CtcDeriv().contract(x,v) + + self.assertEqual(t,Interval(1.5)) + self.assertEqual(z,Interval(2.5)) + self.assertEqual(x(0.),Interval(1.)) + self.assertEqual(x(3.),Interval(4.)) + +if __name__ == '__main__': + unittest.main() diff --git a/doc/manual/manual/contractors/index.rst b/doc/manual/manual/contractors/index.rst index 67dab8f9e..037f1f641 100644 --- a/doc/manual/manual/contractors/index.rst +++ b/doc/manual/manual/contractors/index.rst @@ -6,13 +6,14 @@ Contractors, separators .. toctree:: CtcInter - SepImage CtcInverse - CtcLohner CtcDist CtcPolar - CtcVisible CtcWrapper + CtcEval + CtcLohner + CtcVisible + SepImage .. What are contractors? .. The Ctc class @@ -127,7 +128,7 @@ Overview of contractors and separators * - ``CtcDeriv`` - — - * - ``CtcEval`` + * - :ref:`CtcEval ` - — * - ``CtcDelay`` diff --git a/doc/manual/manual/tubes/slicedtube.rst b/doc/manual/manual/tubes/slicedtube.rst index 784a44bfc..f031e0604 100644 --- a/doc/manual/manual/tubes/slicedtube.rst +++ b/doc/manual/manual/tubes/slicedtube.rst @@ -363,6 +363,8 @@ The computation is reliable even for tubes defined from analytic functions, beca :project: codac +.. _sec-tubes-partial-primitives: + Precomputing partial primitives ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -424,7 +426,7 @@ For a tube made of :math:`N` slices, precomputation requires :math:`O(N)` time a .. warning:: - The precomputed primitives are not automatically updated. Recompute them after any modification of the source tube or its temporal partition, including refinements induced by other tubes sharing the same ``TDomain``. Do not modify the primitive tubes while using them for integration. + The precomputed primitives are not automatically updated. Recompute them if the source envelopes or the temporal extent change. Refinement by duplication of slices on the shared ``TDomain`` preserves the validity of the primitives but may reduce their accuracy at new gates. Do not modify the primitive tubes while using them for integration. Inflation, extraction, and algebraic helpers diff --git a/python/src/core/contractors/codac2_py_CtcEval.cpp b/python/src/core/contractors/codac2_py_CtcEval.cpp index cd3cdb294..4dd2988f3 100644 --- a/python/src/core/contractors/codac2_py_CtcEval.cpp +++ b/python/src/core/contractors/codac2_py_CtcEval.cpp @@ -16,6 +16,9 @@ #include "codac2_py_Ctc.h" #include "codac2_py_CtcEval_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py): +PYBIND11_MAKE_OPAQUE(std::pair,codac2::SlicedTube>); +PYBIND11_MAKE_OPAQUE(std::pair,codac2::SlicedTube>); + using namespace std; using namespace codac2; namespace py = pybind11; @@ -39,6 +42,23 @@ void export_contract(py::class_& exported) }, VOID_CTCEVAL_CONTRACT_INTERVAL_REF_T_REF_SLICEDTUBE_T_REF_CONST_SLICEDTUBE_T_REF_CONST, "t"_a, "y"_a, "x"_a, "v"_a); + + exported.def("contract", + [](const CtcEval& ctc, Interval& t, T& y, SlicedTube& x, const SlicedTube& v, + const std::pair,SlicedTube>& primitives) + -> py::tuple + { + ctc.contract(t,y,x,v,primitives); + + return py::make_tuple( + py::cast(t, py::return_value_policy::reference), + py::cast(y, py::return_value_policy::reference), + py::cast(x, py::return_value_policy::reference), + py::cast(v, py::return_value_policy::reference) + ); + }, + VOID_CTCEVAL_CONTRACT_INTERVAL_REF_T_REF_SLICEDTUBE_T_REF_CONST_SLICEDTUBE_T_REF_CONST_PAIR_SLICEDTUBE_TSLICEDTUBE_T_REF_CONST, + "t"_a, "y"_a, "x"_a, "v"_a, "primitives"_a); } void export_CtcEval(py::module& m) diff --git a/python/src/core/functions/analytic/codac2_py_AnalyticFunction.h b/python/src/core/functions/analytic/codac2_py_AnalyticFunction.h index 9fdc91376..c3d602309 100644 --- a/python/src/core/functions/analytic/codac2_py_AnalyticFunction.h +++ b/python/src/core/functions/analytic/codac2_py_AnalyticFunction.h @@ -404,6 +404,70 @@ void export_AnalyticFunction(py::module& m, const std::string& export_name) ) const) &AnalyticFunction::tube_eval, AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, "x1"_a, "x2"_a, "x3"_a, "x4"_a, "x5"_a) + + // All scalar inputs with an explicit evaluation mode + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a) + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a, "x2"_a) + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube&, const SlicedTube&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a, "x2"_a, "x3"_a) + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube&, const SlicedTube&, const SlicedTube&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a, "x2"_a, "x3"_a, "x4"_a) + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube&, const SlicedTube&, const SlicedTube&, const SlicedTube&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a, "x2"_a, "x3"_a, "x4"_a, "x5"_a) + + // All vector inputs with an explicit evaluation mode + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a) + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a, "x2"_a) + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube&, const SlicedTube&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a, "x2"_a, "x3"_a) + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube&, const SlicedTube&, const SlicedTube&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a, "x2"_a, "x3"_a, "x4"_a) + + .def("tube_eval", (SlicedTube (AnalyticFunction::*) ( + const EvalMode&, const SlicedTube&, const SlicedTube&, const SlicedTube&, const SlicedTube&, const SlicedTube& + ) const) &AnalyticFunction::tube_eval, + AUTO_ANALYTICFUNCTION_T_TUBE_EVAL_CONST_EVALMODE_REF_CONST_SLICEDTUBE_ARGS_REF_VARIADIC_CONST, + "m"_a, "x1"_a, "x2"_a, "x3"_a, "x4"_a, "x5"_a) ; } diff --git a/src/core/contractors/codac2_CtcEval.h b/src/core/contractors/codac2_CtcEval.h index fa7834d4a..eecd72929 100644 --- a/src/core/contractors/codac2_CtcEval.h +++ b/src/core/contractors/codac2_CtcEval.h @@ -9,21 +9,92 @@ #pragma once +#include #include "codac2_Ctc.h" #include "codac2_SlicedTube.h" namespace codac2 { + /** + * \class CtcEval + * \brief Evaluation contractor \f$\mathcal{C}_\textrm{eval}\f$ for scalar or vector tubes. + * + * Given a tube \f$[x](\cdot)\f$, a derivative tube \f$[v](\cdot)\f$ + * and an observation \f$[t]\times[z]\f$, the contractor reduces + * \f$[t]\f$, \f$[z]\f$ and \f$[x](\cdot)\f$ subject to + * \f[ + * z=x(t),\qquad \dot{x}(\cdot)=v(\cdot) + * \f] + * The derivative tube is not contracted. + */ class CtcEval { public: + /** + * \brief Creates the evaluation contractor \f$\mathcal{C}_\textrm{eval}\f$. + */ CtcEval() { } + /** + * \brief \f$\mathcal{C}_\textrm{eval}\big([t],[z],[x](\cdot),[v](\cdot)\big)\f$: + * contracts the observation \f$[t]\times[z]\f$ and the tube \f$[x](\cdot)\f$ + * subject to \f$z=x(t)\f$ and \f$\dot{x}(\cdot)=v(\cdot)\f$. + * + * \tparam T scalar or vector interval type + * \param t temporal interval \f$[t]\f$ of the evaluation + * \param z interval or box \f$[z]\f$ enclosing the evaluation + * \param x scalar or vector tube \f$[x](\cdot)\f$ + * \param v derivative tube \f$[v](\cdot)\f$ + * \note The tubes must share the same TDomain, which may be refined by adding gates + */ template void contract(Interval& t, T& z, SlicedTube& x, const SlicedTube& v) const requires std::is_same_v || std::is_same_v + { + contract_impl(t,z,x,v,nullptr); + } + + /** + * \brief \f$\mathcal{C}_\textrm{eval}\big([t],[z],[x](\cdot),[v](\cdot)\big)\f$: + * contracts the observation \f$[t]\times[z]\f$ and the tube \f$[x](\cdot)\f$ + * subject to \f$z=x(t)\f$ and \f$\dot{x}(\cdot)=v(\cdot)\f$, + * using precomputed partial primitives of the derivative tube. + * + * For \f$[v](\cdot)=[v^-(\cdot),v^+(\cdot)]\f$, the provided tubes enclose + * the partial primitives + * \f[ + * p^-(t)=\int_{t_0}^{t}v^-(s)\,ds,\qquad + * p^+(t)=\int_{t_0}^{t}v^+(s)\,ds + * \f] + * + * \tparam T scalar or vector interval type + * \param t temporal interval \f$[t]\f$ of the evaluation + * \param z interval or box \f$[z]\f$ enclosing the evaluation + * \param x scalar or vector tube \f$[x](\cdot)\f$ + * \param v derivative tube \f$[v](\cdot)\f$ + * \param primitives pair of tubes returned by ``v.partial_primitives()`` + * \note All tubes must share the same TDomain, which may be refined by adding gates + * \note The primitives can be reused after temporal refinement by duplication of slices, + * provided the derivative envelopes and temporal extent remain unchanged + */ + template + void contract(Interval& t, T& z, SlicedTube& x, const SlicedTube& v, + const std::pair,SlicedTube>& primitives) const + requires std::is_same_v || std::is_same_v + { + assert_release(x.tdomain() == v.tdomain()); + assert_release(primitives.first.tdomain() == v.tdomain()); + assert_release(primitives.second.tdomain() == v.tdomain()); + contract_impl(t,z,x,v,&primitives); + } + + private: + + template + void contract_impl(Interval& t, T& z, SlicedTube& x, const SlicedTube& v, + const std::pair,SlicedTube>* primitives) const { assert_release(TDomain::are_same(x.tdomain(), v.tdomain())); assert_release(x.size() == v.size()); @@ -63,7 +134,7 @@ namespace codac2 T slices_union = x.empty_value(); // For each t1 in [t] - auto it_t1 = x.tdomain()->sample(t.lb(),true); + auto it_t1 = it_t_beg; auto it_t1_end = it_t_end; while(it_t1 != it_t1_end) { @@ -74,24 +145,12 @@ namespace codac2 if(!y1.is_empty()) { // Transporting y1 to t - auto it_t1_t = it_t1; - auto it_t1_t_end = it_t; - while(it_t1_t != it_t1_t_end) + if(it_t1 != it_t) { - if(!it_t1_t->is_gate()) - { - // /!\ .diam() method is not reliable (floating point result) - // -> We need to compute the diameter with intervals - Interval d = Interval(it_t1_t->ub())-Interval(it_t1_t->lb()); - if(it_t1->lb() > it_t->lb()) - d *= -1; // going backward - y1 += d*v.slice(it_t1_t)->codomain(); - } - - if(it_t1->lb() < it_t->lb()) - it_t1_t++; + if(primitives) + y1 += v.integral(Interval(it_t1->lb()),Interval(it_t->lb()),*primitives); else - it_t1_t--; + y1 += v.integral(Interval(it_t1->lb()),Interval(it_t->lb())); } slices_union |= y1; diff --git a/src/core/domains/tube/codac2_SlicedTube.h b/src/core/domains/tube/codac2_SlicedTube.h index a052b8c1e..8e8e2ea9d 100644 --- a/src/core/domains/tube/codac2_SlicedTube.h +++ b/src/core/domains/tube/codac2_SlicedTube.h @@ -1006,8 +1006,10 @@ namespace codac2 * \;\middle|\; \tau_1\in[t_1],\ \tau_2\in[t_2] \right\}. * \f] * - * The result is obtained by subtracting the partial integral enclosures at - * ``t1`` and ``t2``. + * For degenerate time intervals, the integral is evaluated directly on the + * non-gate slices between the two dates, with the appropriate sign for + * backward integration. Otherwise, partial integral enclosures are + * subtracted and combined according to the possible integration directions. * * \param t1 first temporal interval \f$[t_1]\f$ * \param t2 second temporal interval \f$[t_2]\f$ @@ -1089,7 +1091,10 @@ namespace codac2 * \pre The temporal domain must be non-empty and bounded, and the codomains * of all non-gate slices must be non-empty and bounded. * \note The source codomains, temporal partition and returned primitives - * must remain unchanged while the primitives are used for integration. + * must remain unchanged while the primitives are used for integration, + * except for temporal refinement by duplication of slices on the shared + * TDomain. Such refinement preserves the enclosures of the partial + * primitives but may reduce their accuracy at the new gates * * \return pair of tubes enclosing the lower and upper partial primitives */ diff --git a/src/core/domains/tube/codac2_SlicedTube_integral_impl.h b/src/core/domains/tube/codac2_SlicedTube_integral_impl.h index e7690f080..ea18e2f5f 100644 --- a/src/core/domains/tube/codac2_SlicedTube_integral_impl.h +++ b/src/core/domains/tube/codac2_SlicedTube_integral_impl.h @@ -9,6 +9,7 @@ #pragma once +#include #include #include #include "codac2_Interval.h" @@ -33,89 +34,98 @@ namespace codac2 template T SlicedTube::integral(const Interval& t1, const Interval& t2) const { - auto integ_t1 = partial_integral(t1); - auto integ_t2 = partial_integral(t2); + if(t1.is_empty() || t2.is_empty()) + return this->empty_value(); + if(!t1.is_subset(this->t0_tf()) || !t2.is_subset(this->t0_tf())) + return this->all_reals_value(); - if(integ_t1.first.is_empty() || integ_t1.second.is_empty() || - integ_t2.first.is_empty() || integ_t2.second.is_empty()) + if(t1.is_degenerated() && t2.is_degenerated()) { - return this->empty_value(); + auto p = this->all_reals_value(); + p.init(Interval(0.)); + if(t1 == t2) + return p; + + // Integrate only the slices between the two dates + Interval t = t1 | t2; + for(auto it = _tdomain->tslice(t.lb()) ; it != _tdomain->end() && it->lb() < t.ub() ; ++it) + { + if(it->is_gate()) + continue; + Interval ti = t & it->t0_tf(); + if(ti.is_empty() || ti.is_degenerated()) + continue; + const auto& x = this->slice(it)->codomain(); + if(x.is_empty()) + return this->empty_value(); + Interval dt = Interval(ti.ub()) - Interval(ti.lb()); + p += dt * x; + } + + return t1.lb() < t2.lb() ? p : T(-p); } - else if(integ_t1.first.is_unbounded() || integ_t1.second.is_unbounded() || - integ_t2.first.is_unbounded() || integ_t2.second.is_unbounded()) - { + auto p = partial_integral(t1,t2); + if(p.first.is_empty() || p.second.is_empty()) + return this->empty_value(); + if(p.first.is_unbounded() || p.second.is_unbounded()) return this->all_reals_value(); - } - else - { - auto lb = (integ_t2.first - integ_t1.first).lb(); - auto ub = (integ_t2.second - integ_t1.second).ub(); - return T(lb) | ub; - } + if(t1.ub() <= t2.lb()) + return T(p.first.lb()) | p.second.ub(); + if(t2.ub() <= t1.lb()) + return T(p.second.lb()) | p.first.ub(); + + return p.first | p.second; } template std::pair SlicedTube::partial_integral(const Interval& t) const { - //if(!t.is_subset(tdomain()->t0_tf())) - // return { Interval(), Interval() }; + if(t.is_empty()) + return { this->empty_value(), this->empty_value() }; + if(!t.is_subset(this->t0_tf()) || t.is_unbounded() || !std::isfinite(this->t0_tf().lb())) + return { this->all_reals_value(), this->all_reals_value() }; auto zero = this->all_reals_value(); zero.init(Interval(0.)); + if(t == Interval(this->t0_tf().lb())) + return { zero, zero }; - std::pair p_integ { zero, zero }; - auto p_integ_uncertain = p_integ; + std::pair p { zero, zero }; + std::pair result { this->empty_value(), this->empty_value() }; - for(const auto& si : *this) + for(const auto& s : *this) { - if(si.t0_tf().lb() >= t.ub()) + if(s.t0_tf().lb() >= t.ub()) break; + if(s.is_gate()) + continue; - if(si.codomain().is_empty()) - { - auto e = this->empty_value(); - return { e, e }; - } + const auto& x = s.codomain(); + if(x.is_empty()) + return { this->empty_value(), this->empty_value() }; + if(x.is_unbounded()) + return { this->all_reals_value(), this->all_reals_value() }; - if(si.codomain().is_unbounded()) + // Evaluate the affine partial primitives on the queried part of the slice + Interval ti = s.t0_tf() & t; + if(!ti.is_empty()) { - auto u = this->all_reals_value(); - return { u, u }; + Interval dt = ti - Interval(s.t0_tf().lb()); + result.first |= p.first + dt * T(x.lb()); + result.second |= p.second + dt * T(x.ub()); } - // From t0 to tlb - - Interval intv_t = si.t0_tf() & Interval(tdomain()->t0_tf().lb(), t.lb()); - if(!intv_t.is_empty()) - { - p_integ.first += intv_t.diam() * si.codomain().lb(); - p_integ.second += intv_t.diam() * si.codomain().ub(); - p_integ_uncertain = p_integ; - - if(intv_t.ub() == t.ub()) - return p_integ; // end of the integral evaluation - } - - // From tlb to tub + if(s.t0_tf().ub() >= t.ub()) + break; - intv_t = si.t0_tf() & t; - if(!intv_t.is_empty()) - { - auto p_integ_temp = p_integ_uncertain; - p_integ_uncertain.first += Interval(0., intv_t.diam()) * si.codomain().lb(); - p_integ_uncertain.second += Interval(0., intv_t.diam()) * si.codomain().ub(); - - p_integ.first |= p_integ_uncertain.first; - p_integ.second |= p_integ_uncertain.second; - - p_integ_uncertain.first = p_integ_temp.first + intv_t.diam() * si.codomain().lb(); - p_integ_uncertain.second = p_integ_temp.second + intv_t.diam() * si.codomain().ub(); - } + Interval dt = Interval(s.t0_tf().ub()) - Interval(s.t0_tf().lb()); + p.first += dt * T(x.lb()); + p.second += dt * T(x.ub()); } - return p_integ; + return result; } template diff --git a/src/core/functions/analytic/codac2_AnalyticFunction.h b/src/core/functions/analytic/codac2_AnalyticFunction.h index 7d569e124..af1506ae2 100644 --- a/src/core/functions/analytic/codac2_AnalyticFunction.h +++ b/src/core/functions/analytic/codac2_AnalyticFunction.h @@ -196,6 +196,12 @@ namespace codac2 template auto tube_eval(const SlicedTube&... x) const + { + return tube_eval(EvalMode::DEFAULT,x...); + } + + template + auto tube_eval(const EvalMode& m, const SlicedTube&... x) const { auto tdomain = std::get<0>(std::tie(x...)).tdomain(); @@ -204,7 +210,7 @@ namespace codac2 ); for(auto it = tdomain->begin() ; it != tdomain->end() ; it++) - y.slice(it)->codomain() = this->eval(x.slice(it)->codomain()...); + y.slice(it)->codomain() = this->eval(m,x.slice(it)->codomain()...); return y; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d933ab001..e69a435b1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -48,6 +48,7 @@ list(APPEND SRC_TESTS # listing files without extension ../doc/manual/manual/contractors/geometric/src ../doc/manual/manual/contractors/analytic/src ../doc/manual/manual/contractors/set/src + ../doc/manual/manual/contractors/dynamic/src core/domains/codac2_tests_BoolInterval core/domains/ellipsoid/codac2_tests_Ellipsoid diff --git a/tests/core/domains/tube/codac2_tests_SlicedTube_integral.cpp b/tests/core/domains/tube/codac2_tests_SlicedTube_integral.cpp index e44e8c3bd..8c511f41e 100644 --- a/tests/core/domains/tube/codac2_tests_SlicedTube_integral.cpp +++ b/tests/core/domains/tube/codac2_tests_SlicedTube_integral.cpp @@ -344,6 +344,9 @@ TEST_CASE("Computing integration, two interval bounds") CHECK(Approx(integral(Interval(28.), Interval(33.))) == Interval(17,31)); CHECK(Approx(integral(Interval(20.), Interval(28.))) == Interval(51,88)); CHECK(Approx(integral(Interval(17.), Interval(33.))) == Interval(80,143)); + CHECK(Approx(integral(Interval(33.), Interval(17.))) == Interval(-143,-80)); + CHECK(Approx(integral(Interval(17,20), Interval(28,33))) == Interval(51,143)); + CHECK(Approx(integral(Interval(28,33), Interval(17,20))) == Interval(-143,-51)); CHECK(Approx(integral(Interval(22.5), Interval(23))) == Interval(4.5,6)); CHECK(Approx(integral(Interval(8.5), Interval(10.5))) == Interval(-18,-9.5)); CHECK(Approx(integral(Interval(0), Interval(7))) == Interval(-23,13)); @@ -376,6 +379,9 @@ TEST_CASE("Computing integration, two interval bounds") CHECK(Approx(integral(Interval(28.), Interval(33.))) == Interval(17,31)); CHECK(Approx(integral(Interval(20.), Interval(28.))) == Interval(51,88)); CHECK(Approx(integral(Interval(17.), Interval(33.))) == Interval(80,143)); + CHECK(Approx(integral(Interval(33.), Interval(17.))) == Interval(-143,-80)); + CHECK(Approx(integral(Interval(17,20), Interval(28,33))) == Interval(51,143)); + CHECK(Approx(integral(Interval(28,33), Interval(17,20))) == Interval(-143,-51)); CHECK(Approx(integral(Interval(22.5), Interval(23))) == Interval(4.5,6)); CHECK(Approx(integral(Interval(8.5), Interval(10.5))) == Interval(-18,-9.5)); CHECK(Approx(integral(Interval(0), Interval(7))) == Interval(-23,13)); @@ -431,6 +437,7 @@ TEST_CASE("Computing partial integration, two interval bounds") CHECK(Approx(partial_integral(Interval(20), Interval(20))) == make_pair(Interval(0.), Interval(0.))); CHECK(Approx(partial_integral(Interval(22,23), Interval(24,25))) == make_pair(Interval(8,24), Interval(13,37))); CHECK(Approx(partial_integral(Interval(17,20), Interval(28,33))) == make_pair(Interval(51,80), Interval(88,143))); + CHECK(Approx(partial_integral(Interval(28,33), Interval(17,20))) == make_pair(Interval(-80,-51), Interval(-143,-88))); CHECK(Approx(partial_integral(Interval(0.), Interval(7.))) == make_pair(Interval(-23), Interval(13))); CHECK(Approx(partial_integral(Interval(2.), Interval(6.))) == make_pair(Interval(-19), Interval(4))); CHECK(Approx(partial_integral(Interval(0,2), Interval(6,7))) == make_pair(Interval(-29,-13), Interval(-2,19))); @@ -449,6 +456,7 @@ TEST_CASE("Computing partial integration, two interval bounds") CHECK(Approx(partial_integral(Interval(20), Interval(20))) == make_pair(Interval(0.), Interval(0.))); CHECK(Approx(partial_integral(Interval(22,23), Interval(24,25))) == make_pair(Interval(8,24), Interval(13,37))); CHECK(Approx(partial_integral(Interval(17,20), Interval(28,33))) == make_pair(Interval(51,80), Interval(88,143))); + CHECK(Approx(partial_integral(Interval(28,33), Interval(17,20))) == make_pair(Interval(-80,-51), Interval(-143,-88))); CHECK(Approx(partial_integral(Interval(0.), Interval(7.))) == make_pair(Interval(-23), Interval(13))); CHECK(Approx(partial_integral(Interval(2.), Interval(6.))) == make_pair(Interval(-19), Interval(4))); CHECK(Approx(partial_integral(Interval(0,2), Interval(6,7))) == make_pair(Interval(-29,-13), Interval(-2,19))); @@ -483,4 +491,34 @@ TEST_CASE("Computing partial integration, two interval bounds") == make_pair(Interval(-3.35,-2.3), Interval(1.95,4.7))); }); } -} \ No newline at end of file +} + +TEST_CASE("Computing integration, overlapping interval bounds") +{ + SlicedTube x(create_tdomain({0,4},1.,true),Interval(1,3)); + + check_integrals_in_both_modes(x,[&](auto integral, auto partial_integral) + { + CHECK(Approx(integral(Interval(1,3), Interval(2,4))) == Interval(-3,9)); + CHECK(Approx(integral(Interval(2,4), Interval(1,3))) == Interval(-9,3)); + CHECK(Approx(integral(Interval(1,3), Interval(1,3))) == Interval(-6,6)); + CHECK(Approx(integral(Interval(1,2), Interval(2,3))) == Interval(0,6)); + CHECK(Approx(integral(Interval(2,3), Interval(1,2))) == Interval(-6,0)); + CHECK(Approx(partial_integral(Interval(1,3), Interval(2,4))) == make_pair(Interval(-1,3), Interval(-3,9))); + CHECK(Approx(partial_integral(Interval(2,4), Interval(1,3))) == make_pair(Interval(-3,1), Interval(-9,3))); + }); +} + +TEST_CASE("Computing local integration of an unbounded tube") +{ + SlicedTube x(create_tdomain({0,3},1.,false),Interval()); + x.set_ith_slice(Interval(2,3),1); + x.set_ith_slice(Interval(4,5),2); + + CHECK(x.integral(Interval(1.), Interval(3.)) == Interval(6,8)); + CHECK(x.integral(Interval(3.), Interval(1.)) == Interval(-8,-6)); + CHECK(x.integral(Interval(2.), Interval(2.)) == Interval(0.)); + CHECK(x.integral(Interval(0.), Interval(0.)) == Interval(0.)); + CHECK(x.integral(Interval(0.), Interval(2.)).is_unbounded()); + CHECK(x.integral(Interval(2.), Interval(0.)).is_unbounded()); +} diff --git a/tests/core/domains/tube/codac2_tests_SlicedTube_integral.py b/tests/core/domains/tube/codac2_tests_SlicedTube_integral.py index 348689eef..15a2adcd2 100644 --- a/tests/core/domains/tube/codac2_tests_SlicedTube_integral.py +++ b/tests/core/domains/tube/codac2_tests_SlicedTube_integral.py @@ -301,6 +301,9 @@ def test_computing_integration_two_interval_bounds__tube1(self): self.assertTrue(Approx(integral(Interval(28.), Interval(33.))) == Interval(17,31)) self.assertTrue(Approx(integral(Interval(20.), Interval(28.))) == Interval(51,88)) self.assertTrue(Approx(integral(Interval(17.), Interval(33.))) == Interval(80,143)) + self.assertTrue(Approx(integral(Interval(33.), Interval(17.))) == Interval(-143,-80)) + self.assertTrue(Approx(integral(Interval(17,20), Interval(28,33))) == Interval(51,143)) + self.assertTrue(Approx(integral(Interval(28,33), Interval(17,20))) == Interval(-143,-51)) self.assertTrue(Approx(integral(Interval(22.5), Interval(23))) == Interval(4.5,6)) self.assertTrue(Approx(integral(Interval(8.5), Interval(10.5))) == Interval(-18,-9.5)) self.assertTrue(Approx(integral(Interval(0), Interval(7))) == Interval(-23,13)) @@ -331,6 +334,9 @@ def test_computing_integration_two_interval_bounds__tube1_01(self): self.assertTrue(Approx(integral(Interval(28.), Interval(33.))) == Interval(17,31)) self.assertTrue(Approx(integral(Interval(20.), Interval(28.))) == Interval(51,88)) self.assertTrue(Approx(integral(Interval(17.), Interval(33.))) == Interval(80,143)) + self.assertTrue(Approx(integral(Interval(33.), Interval(17.))) == Interval(-143,-80)) + self.assertTrue(Approx(integral(Interval(17,20), Interval(28,33))) == Interval(51,143)) + self.assertTrue(Approx(integral(Interval(28,33), Interval(17,20))) == Interval(-143,-51)) self.assertTrue(Approx(integral(Interval(22.5), Interval(23))) == Interval(4.5,6)) self.assertTrue(Approx(integral(Interval(8.5), Interval(10.5))) == Interval(-18,-9.5)) self.assertTrue(Approx(integral(Interval(0), Interval(7))) == Interval(-23,13)) @@ -377,6 +383,7 @@ def test_computing_partial_integration_two_interval_bounds__tube1(self): self.assertTrue(Approx(partial_integral(Interval(20), Interval(20))) == (Interval(0.), Interval(0.))) self.assertTrue(Approx(partial_integral(Interval(22,23), Interval(24,25))) == (Interval(8,24), Interval(13,37))) self.assertTrue(Approx(partial_integral(Interval(17,20), Interval(28,33))) == (Interval(51,80), Interval(88,143))) + self.assertTrue(Approx(partial_integral(Interval(28,33), Interval(17,20))) == (Interval(-80,-51), Interval(-143,-88))) self.assertTrue(Approx(partial_integral(Interval(0.), Interval(7.))) == (Interval(-23), Interval(13))) self.assertTrue(Approx(partial_integral(Interval(2.), Interval(6.))) == (Interval(-19), Interval(4))) self.assertTrue(Approx(partial_integral(Interval(0,2), Interval(6,7))) == (Interval(-29,-13), Interval(-2,19))) @@ -393,6 +400,7 @@ def test_computing_partial_integration_two_interval_bounds__tube1_01(self): self.assertTrue(Approx(partial_integral(Interval(20), Interval(20))) == (Interval(0.), Interval(0.))) self.assertTrue(Approx(partial_integral(Interval(22,23), Interval(24,25))) == (Interval(8,24), Interval(13,37))) self.assertTrue(Approx(partial_integral(Interval(17,20), Interval(28,33))) == (Interval(51,80), Interval(88,143))) + self.assertTrue(Approx(partial_integral(Interval(28,33), Interval(17,20))) == (Interval(-80,-51), Interval(-143,-88))) self.assertTrue(Approx(partial_integral(Interval(0.), Interval(7.))) == (Interval(-23), Interval(13))) self.assertTrue(Approx(partial_integral(Interval(2.), Interval(6.))) == (Interval(-19), Interval(4))) self.assertTrue(Approx(partial_integral(Interval(0,2), Interval(6,7))) == (Interval(-29,-13), Interval(-2,19))) @@ -423,9 +431,38 @@ def test_computing_partial_integration_two_interval_bounds__tube4_05(self): == (Interval(-3.35,-2.3), Interval(1.95,4.7))) + def test_computing_integration_overlapping_interval_bounds(self): + + x = SlicedTube(create_tdomain(Interval(0,4),1.,True),Interval(1,3)) + integral, partial_integral = self.integration_functions(x) + + self.assertTrue(Approx(integral(Interval(1,3), Interval(2,4))) == Interval(-3,9)) + self.assertTrue(Approx(integral(Interval(2,4), Interval(1,3))) == Interval(-9,3)) + self.assertTrue(Approx(integral(Interval(1,3), Interval(1,3))) == Interval(-6,6)) + self.assertTrue(Approx(integral(Interval(1,2), Interval(2,3))) == Interval(0,6)) + self.assertTrue(Approx(integral(Interval(2,3), Interval(1,2))) == Interval(-6,0)) + self.assertTrue(Approx(partial_integral(Interval(1,3), Interval(2,4))) == (Interval(-1,3), Interval(-3,9))) + self.assertTrue(Approx(partial_integral(Interval(2,4), Interval(1,3))) == (Interval(-3,1), Interval(-9,3))) + class TestSlicedTube_integral_precomputed(TestSlicedTube_integral): precomputed = True +class TestSlicedTube_integral_unbounded(unittest.TestCase): + + def test_computing_local_integration_of_an_unbounded_tube(self): + + x = SlicedTube(create_tdomain(Interval(0,3),1.,False),Interval()) + x.set_ith_slice(Interval(2,3),1) + x.set_ith_slice(Interval(4,5),2) + + self.assertEqual(x.integral(Interval(1.), Interval(3.)),Interval(6,8)) + self.assertEqual(x.integral(Interval(3.), Interval(1.)),Interval(-8,-6)) + self.assertEqual(x.integral(Interval(2.), Interval(2.)),Interval(0.)) + self.assertEqual(x.integral(Interval(0.), Interval(0.)),Interval(0.)) + self.assertTrue(x.integral(Interval(0.), Interval(2.)).is_unbounded()) + self.assertTrue(x.integral(Interval(2.), Interval(0.)).is_unbounded()) + + if __name__ == '__main__': unittest.main() \ No newline at end of file