codac 1.5.6
Loading...
Searching...
No Matches
codac2_vec.h
1
9
10#pragma once
11
12#include "codac2_Interval.h"
14#include "codac2_AnalyticType.h"
16
17namespace codac2
18{
19 struct VectorOp
20 {
21 template<typename... X>
22 static inline std::string str(const X&... x)
23 {
24 std::string s = (("\t" + x->str() + ",\n") + ...);
25 s.pop_back(); s.pop_back(); // removes last separation
26 return "[\n" + s + "\n]";
27 }
28
29 template<typename... X>
30 static std::pair<Index,Index> output_shape([[maybe_unused]] const X&... x)
31 {
32 return { sizeof...(X), 1 };
33 }
34
35 template<typename... X>
36 requires (std::is_base_of_v<Interval,X> && ...)
37 static inline IntervalVector fwd(const X&... x)
38 {
39 return IntervalVector({x...});
40 }
41
42 template<typename... X>
43 requires (std::is_base_of_v<ScalarType,X> && ...)
44 static inline VectorType fwd_natural(const X&... x)
45 {
46 bool def_domain = true;
47 ((def_domain &= x.def_domain), ...);
48
49 return {
50 fwd(x.a...),
51 def_domain
52 };
53 }
54
55 template<typename... X>
56 requires (std::is_base_of_v<ScalarType,X> && ...)
57 static inline VectorType fwd_centered(const X&... x)
58 {
59 if(centered_form_not_available_for_args(x...))
60 return fwd_natural(x...);
61
62 IntervalMatrix d(sizeof...(X),std::get<0>(std::tie(x...)).da.cols());
63 Index i = 0;
64 ((d.row(i++) = x.da), ...);
65
66 bool def_domain = true;
67 ((def_domain &= x.def_domain), ...);
68
69 return {
70 fwd(x.m...),
71 fwd(x.a...),
72 d,
73 def_domain
74 };
75 }
76
77 template<typename... X>
78 requires (std::is_base_of_v<Interval,X> && ...)
79 static inline void bwd(const IntervalVector& y, X&... x)
80 {
81 Index i = 0;
82 ((x &= y[i++]), ...);
83 }
84 };
85
86 // Analytic operator
87 // The following functions can be used to build analytic expressions.
88
89 inline const ScalarExpr& _add_to_vec(const ScalarExpr& x)
90 {
91 return x;
92 }
93
94 inline ScalarExpr _add_to_vec(double x)
95 {
96 return const_value(x);
97 }
98
99 template<typename... X>
100 requires ((std::is_same_v<typename ExprType<X>::Type,ScalarType>) && ...)
101 inline VectorExpr
102 vec(const X&... x)
103 {
104 return { std::make_shared<AnalyticOperationExpr<VectorOp,VectorType,typename ExprType<X>::Type...>>(_add_to_vec(x)...) };
105 }
106}