codac 1.5.6
Loading...
Searching...
No Matches
codac2_mod.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
13#include "codac2_AnalyticType.h"
15#include "codac2_arith_sub.h"
16
17namespace codac2
18{
19 struct ModOp
20 {
21 template<typename X1,typename X2,typename P>
22 static std::string str(const X1& x1, const X2& x2, const P& p)
23 {
24 return "mod(" + x1->str() + "," + x2->str() + "," + p->str() + ")";
25 }
26
27 template<typename X1, typename X2>
28 static std::pair<Index,Index> output_shape([[maybe_unused]] const X1& s1, [[maybe_unused]] const X2& s2)
29 {
30 return {1,1};
31 }
32
33 static void bwd(Interval& x1, Interval& x2, double p);
34 static void bwd(Interval& x1, Interval& x2, Interval& p);
35 };
36
37 // Inline functions
38
39 inline void ModOp::bwd(Interval& x1, Interval& x2, double p) // x1 = x2 mod(p)
40 {
41 // The content of this function comes from the IBEX library.
42 // See ibex::Interval (IBEX lib, main author: Gilles Chabert)
43 // https://ibex-lib.readthedocs.io
44
45 assert_release(p > 0. && "Modulo needs a strictly positive period p.");
46
47 if(!(x2.diam() > p || x1.diam() > p))
48 {
49 Interval r = (x1-x2)/p;
50 Interval ir = integer(r);
51
52 if(ir.is_empty()) // additional protection because an empty interval is considered degenerated.
53 {
54 x1.set_empty();
55 x2.set_empty();
56 }
57
58 else
59 {
60 if(ir.is_degenerated())
61 SubOp::bwd(ir*p,x1,x2);
62
63 else if(ir.diam() == 1.)
64 {
65 Interval x1_1 = x1; Interval x1_2 = x1;
66 Interval x2_1 = x2; Interval x2_2 = x2;
67 SubOp::bwd(Interval(ir.lb()*p),x1_1,x2_1);
68 SubOp::bwd(Interval(ir.ub()*p),x1_2,x2_2);
69 x1 = x1_1 | x1_2;
70 x2 = x2_1 | x2_2;
71 }
72
73 else
74 {
75 assert_release_constexpr(false && "Modulo diameter error.");
76 }
77 }
78 }
79 }
80
81 inline void ModOp::bwd(Interval& x1, Interval& x2, Interval& p) // x = y mod(p)
82 {
83 assert_release(p.is_degenerated() && "ModOp::bwd(y,x1,x2) (with x1 and x2 intervals) not implemented yet");
84 ModOp::bwd(x1, x2, p.mid());
85 }
86}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval integer(const Interval &x)
Returns the largest integer interval included in .
Definition codac2_Interval_operations_impl.h:284