codac 1.5.6
Loading...
Searching...
No Matches
codac2_arith_div.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
15#include "codac2_AnalyticType.h"
17#include "codac2_arith_mul.h"
18
19namespace codac2
20{
21 struct DivOp
22 {
23 static Interval fwd(const Interval& x1, const Interval& x2);
24 static ScalarType fwd_natural(const ScalarType& x1, const ScalarType& x2);
25 static ScalarType fwd_centered(const ScalarType& x1, const ScalarType& x2);
26 static void bwd(const Interval& y, Interval& x1, Interval& x2);
27
28 static IntervalVector fwd(const IntervalVector& x1, const Interval& x2);
29 static VectorType fwd_natural(const VectorType& x1, const ScalarType& x2);
30 static VectorType fwd_centered(const VectorType& x1, const ScalarType& x2);
31 static void bwd(const IntervalVector& y, IntervalVector& x1, Interval& x2);
32
33 static IntervalMatrix fwd(const IntervalMatrix& x1, const Interval& x2);
34 static MatrixType fwd_natural(const MatrixType& x1, const ScalarType& x2);
35 static MatrixType fwd_centered(const MatrixType& x1, const ScalarType& x2);
36 static void bwd(const IntervalMatrix& y, IntervalMatrix& x1, Interval& x2);
37 };
38
39 // operator/
40 // The following functions can be used to build analytic expressions.
41
42 inline ScalarExpr
43 operator/(const ScalarExpr& x1, const ScalarExpr& x2)
44 {
45 return { std::make_shared<AnalyticOperationExpr<DivOp,ScalarType,ScalarType,ScalarType>>(x1,x2) };
46 }
47
48 inline VectorExpr
49 operator/(const VectorExpr& x1, const ScalarExpr& x2)
50 {
51 return { std::make_shared<AnalyticOperationExpr<DivOp,VectorType,VectorType,ScalarType>>(x1,x2) };
52 }
53
54 inline MatrixExpr
55 operator/(const MatrixExpr& x1, const ScalarExpr& x2)
56 {
57 return { std::make_shared<AnalyticOperationExpr<DivOp,MatrixType,MatrixType,ScalarType>>(x1,x2) };
58 }
59
60 // Inline functions
61
62 inline Interval DivOp::fwd(const Interval& x1, const Interval& x2)
63 {
64 return x1 / x2;
65 }
66
67 inline ScalarType DivOp::fwd_natural(const ScalarType& x1, const ScalarType& x2)
68 {
69 return {
70 fwd(x1.a, x2.a),
71 x1.def_domain && x2.def_domain && x2.a != 0. // def domain of the derivative of div
72 };
73 }
74
75 inline ScalarType DivOp::fwd_centered(const ScalarType& x1, const ScalarType& x2)
76 {
77 if(centered_form_not_available_for_args(x1,x2))
78 return fwd_natural(x1,x2);
79
80 assert(x1.da.size() == x2.da.size());
81
82 IntervalMatrix d(1,x1.da.size());
83 for(Index i = 0 ; i < d.size() ; i++)
84 d(0,i) = (x1.da(0,i)*x2.a-x1.a*x2.da(0,i))/sqr(x2.a);
85
86 return {
87 fwd(x1.m, x2.m),
88 fwd(x1.a, x2.a),
89 d,
90 x1.def_domain && x2.def_domain && x2.a != 0. // def domain of the derivative of div
91 };
92 }
93
94 inline void DivOp::bwd(const Interval& y, Interval& x1, Interval& x2)
95 {
96 if((x1 &= y*x2).is_empty())
97 x2.set_empty();
98
99 else
100 {
101 Interval tmp = y;
102 MulOp::bwd(x1, tmp, x2);
103 if(x2.is_empty())
104 x1.set_empty();
105 }
106 }
107
108 inline IntervalVector DivOp::fwd(const IntervalVector& x1, const Interval& x2)
109 {
110 return x1 / x2;
111 }
112
113 inline VectorType DivOp::fwd_natural(const VectorType& x1, const ScalarType& x2)
114 {
115 return {
116 fwd(x1.a, x2.a),
117 x1.def_domain && x2.def_domain && x2.a != 0. // def domain of the derivative of div
118 };
119 }
120
121 inline VectorType DivOp::fwd_centered(const VectorType& x1, const ScalarType& x2)
122 {
123 if(centered_form_not_available_for_args(x1,x2))
124 return fwd_natural(x1,x2);
125
126 assert(x1.da.size() == x2.da.size());
127
128 IntervalMatrix d(1,x1.da.size());
129 assert_release(false && "not implemented yet");
130
131 return {
132 fwd(x1.m, x2.m),
133 fwd(x1.a, x2.a),
134 d,
135 x1.def_domain && x2.def_domain && x2.a != 0. // def domain of the derivative of div
136 };
137 }
138
139 inline void DivOp::bwd(const IntervalVector& y, IntervalVector& x1, Interval& x2)
140 {
141 assert(x1.size() == y.size());
142 for(Index i = 0 ; i < x1.size() ; i++)
143 DivOp::bwd(y[i], x1[i], x2);
144 }
145
146 inline IntervalMatrix DivOp::fwd(const IntervalMatrix& x1, const Interval& x2)
147 {
148 return x1 / x2;
149 }
150
151 inline MatrixType DivOp::fwd_natural(const MatrixType& x1, const ScalarType& x2)
152 {
153 return {
154 fwd(x1.a, x2.a),
155 x1.def_domain && x2.def_domain && x2.a != 0. // def domain of the derivative of div
156 };
157 }
158
159 inline MatrixType DivOp::fwd_centered(const MatrixType& x1, const ScalarType& x2)
160 {
161 if(centered_form_not_available_for_args(x1,x2))
162 return fwd_natural(x1,x2);
163
164 assert(x1.da.size() == x2.da.size());
165
166 return {
167 fwd(x1.m, x2.m),
168 fwd(x1.a, x2.a),
169 IntervalMatrix(0,0), // not available
170 x1.def_domain && x2.def_domain && x2.a != 0. // def domain of the derivative of div
171 };
172 }
173
174 inline void DivOp::bwd([[maybe_unused]] const IntervalMatrix& y, [[maybe_unused]] IntervalMatrix& x1, [[maybe_unused]] Interval& x2)
175 {
176 assert(x1.size() == y.size());
177 // todo
178 }
179}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval operator/(const Interval &x, double y)
Returns with .
Definition codac2_Interval_impl.h:435
Interval sqr(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:21