codac 2.0.0
Loading...
Searching...
No Matches
codac2_AnalyticExpr.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <map>
13#include <memory>
14#include <utility>
15#include "codac2_ExprBase.h"
16#include "codac2_Domain.h"
18#include "codac2_AnalyticType.h"
19
20namespace codac2
21{
22 using ValuesMap = std::map<ExprID,std::shared_ptr<AnalyticTypeBase>>;
23
24 template<typename T>
25 class AnalyticExpr : public ExprBase
26 {
27 public:
28
29 virtual const T& fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const = 0;
30 virtual void bwd_eval(ValuesMap& v) const = 0;
31 virtual std::pair<Index,Index> output_shape() const = 0;
32
33 const T& init_value(ValuesMap& v, const T& x) const
34 {
35 auto& p = v[unique_id()];
36
37 if(!p)
38 {
39 p = std::make_shared<T>(x);
40 return *static_cast<T*>(p.get());
41 }
42
43 else
44 {
45 auto pt = std::dynamic_pointer_cast<T>(p);
46 assert(pt && "Type mismatch in ValuesMap for this ExprID");
47 *pt = x;
48 return *pt;
49 }
50 }
51
52
53 const T& init_value(ValuesMap& v, T&& x) const
54 {
55 auto& p = v[unique_id()];
56
57 if(!p)
58 {
59 p = std::make_shared<T>(std::move(x));
60 return *static_cast<T*>(p.get());
61 }
62
63 else
64 {
65 auto pt = std::dynamic_pointer_cast<T>(p);
66 assert(pt && "Type mismatch in ValuesMap for this ExprID");
67 *pt = std::move(x);
68 return *pt;
69 }
70 }
71
72 T& value(ValuesMap& v) const
73 {
74 auto it = v.find(unique_id());
75 assert(it != v.end() && "argument cannot be found");
76 auto p = std::dynamic_pointer_cast<T>(it->second);
77 assert(p && "Type mismatch in ValuesMap for this ExprID");
78 return *p;
79 }
80
81 virtual bool belongs_to_args_list(const FunctionArgsList& args) const = 0;
82 virtual std::string str(bool in_parentheses = false) const = 0;
83 virtual bool is_str_leaf() const = 0;
84 };
85
86 template<typename C,typename Y,typename... X>
87 class AnalyticOperationExpr : public AnalyticExpr<Y>, public OperationExprBase<AnalyticExpr<X>...>
88 {
89 public:
90
91 AnalyticOperationExpr(std::shared_ptr<AnalyticExpr<X>>... x)
92 : OperationExprBase<AnalyticExpr<X>...>(x...)
93 { }
94
95 AnalyticOperationExpr(const AnalyticOperationExpr<C,Y,X...>& e)
96 : OperationExprBase<AnalyticExpr<X>...>(e)
97 { }
98
99 std::shared_ptr<ExprBase> copy() const override
100 {
101 return std::make_shared<AnalyticOperationExpr<C,Y,X...>>(*this);
102 }
103
104 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr) override
105 {
106 return OperationExprBase<AnalyticExpr<X>...>::replace_arg(old_arg_id, new_expr);
107 }
108
109 const Y& fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const override
110 {
111 return std::apply(
112 [this,&v,total_input_size,natural_eval](auto &&... x) -> const Y&
113 {
114 if(natural_eval)
115 return AnalyticExpr<Y>::init_value(v,
116 C::fwd_natural(x->fwd_eval(v, total_input_size, natural_eval)...));
117
118 else
119 return AnalyticExpr<Y>::init_value(v,
120 C::fwd_centered(x->fwd_eval(v, total_input_size, natural_eval)...));
121 },
122 this->_x);
123 }
124
125 void bwd_eval(ValuesMap& v) const override
126 {
127 const auto& y = AnalyticExpr<Y>::value(v);
128
129 std::apply([&v,&y](auto &&... x)
130 {
131 C::bwd(y.a, x->value(v).a...);
132 }, this->_x);
133
134 std::apply([&v](auto &&... x)
135 {
136 (x->bwd_eval(v), ...);
137 }, this->_x);
138 }
139
140 virtual std::string str(bool in_parentheses = false) const override
141 {
142 std::string s = std::apply([](auto &&... x) {
143 return C::str(x...);
144 }, this->_x);
145 return in_parentheses ? "(" + s + ")" : s;
146 }
147
148 virtual bool is_str_leaf() const override
149 {
150 return false;
151 }
152
153 std::pair<Index,Index> output_shape() const override
154 {
155 std::pair<Index,Index> s;
156 std::apply([&s](auto &&... x)
157 {
158 s = C::output_shape(x...);
159 }, this->_x);
160 return s;
161 }
162
163 virtual bool belongs_to_args_list(const FunctionArgsList& args) const override
164 {
165 bool b = true;
166
167 std::apply([&b,&args](auto &&... x)
168 {
169 ((b &= x->belongs_to_args_list(args)), ...);
170 }, this->_x);
171
172 return b;
173 }
174
175 std::vector<std::shared_ptr<ExprBase>> children_expr_base() const override
176 {
178 }
179 };
180}
Abstract base class for representing an expression.
Definition codac2_ExprBase.h:86
const ExprID & unique_id() const
Returns the unique identifier of the expression.
virtual std::shared_ptr< ExprBase > copy() const =0
Creates a copy of the current expression.
A base class for expressions representing operations with multiple operands.
Definition codac2_ExprBase.h:178
std::vector< std::shared_ptr< ExprBase > > children_expr_base() const
Definition codac2_ExprBase.h:240
OperationExprBase(std::shared_ptr< X >... x)
Definition codac2_ExprBase.h:190
std::tuple< std::shared_ptr< X >... > _x
Definition codac2_ExprBase.h:292
Definition codac2_OctaSym.h:21