codac 2.0.0
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 double ir1 = ir.lb();
66 double ir2 = ir.ub();
67 Interval x1_1 = x1; Interval x1_2 = x1;
68 Interval x2_1 = x2; Interval x2_2 = x2;
69 SubOp::bwd(Interval(ir1*p),x1_1,x2_1);
70 SubOp::bwd(Interval(ir2*p),x1_2,x2_2);
71 x1 = x1_1 | x1_2;
72 x2 = x2_1 | x2_2;
73 }
74
75 else
76 {
77 assert_release(false && "Modulo diameter error.");
78 }
79 }
80 }
81 }
82
83 inline void ModOp::bwd(Interval& x1, Interval& x2, Interval& p) // x = y mod(p)
84 {
85 assert_release(p.is_degenerated() && "ModOp::bwd(y,x1,x2) (with x1 and x2 intervals) not implemented yet");
86 ModOp::bwd(x1, x2, p.mid());
87 }
88}
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