22 using ValuesMap = std::map<ExprID,std::shared_ptr<AnalyticTypeBase>>;
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;
33 const T& init_value(ValuesMap& v,
const T& x)
const
39 p = std::make_shared<T>(x);
40 return *
static_cast<T*
>(p.get());
45 auto pt = std::dynamic_pointer_cast<T>(p);
46 assert(pt &&
"Type mismatch in ValuesMap for this ExprID");
53 const T& init_value(ValuesMap& v, T&& x)
const
59 p = std::make_shared<T>(std::move(x));
60 return *
static_cast<T*
>(p.get());
65 auto pt = std::dynamic_pointer_cast<T>(p);
66 assert(pt &&
"Type mismatch in ValuesMap for this ExprID");
72 T& value(ValuesMap& v)
const
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");
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;
86 template<
typename C,
typename Y,
typename... X>
87 class AnalyticOperationExpr :
public AnalyticExpr<Y>,
public OperationExprBase<AnalyticExpr<X>...>
91 AnalyticOperationExpr(std::shared_ptr<AnalyticExpr<X>>... x)
95 AnalyticOperationExpr(
const AnalyticOperationExpr<C,Y,X...>& e)
99 std::shared_ptr<ExprBase>
copy()
const override
101 return std::make_shared<AnalyticOperationExpr<C,Y,X...>>(*this);
104 void replace_arg(
const ExprID& old_arg_id,
const std::shared_ptr<ExprBase>& new_expr)
override
109 const Y& fwd_eval(ValuesMap& v, Index total_input_size,
bool natural_eval)
const override
112 [
this,&v,total_input_size,natural_eval](
auto &&... x) ->
const Y&
115 return AnalyticExpr<Y>::init_value(v,
116 C::fwd_natural(x->fwd_eval(v, total_input_size, natural_eval)...));
119 return AnalyticExpr<Y>::init_value(v,
120 C::fwd_centered(x->fwd_eval(v, total_input_size, natural_eval)...));
125 void bwd_eval(ValuesMap& v)
const override
127 const auto& y = AnalyticExpr<Y>::value(v);
129 std::apply([&v,&y](
auto &&... x)
131 C::bwd(y.a, x->value(v).a...);
134 std::apply([&v](
auto &&... x)
136 (x->bwd_eval(v), ...);
140 virtual std::string str(
bool in_parentheses =
false)
const override
142 std::string s = std::apply([](
auto &&... x) {
145 return in_parentheses ?
"(" + s +
")" : s;
148 virtual bool is_str_leaf()
const override
153 std::pair<Index,Index> output_shape()
const override
155 std::pair<Index,Index> s;
156 std::apply([&s](
auto &&... x)
158 s = C::output_shape(x...);
163 virtual bool belongs_to_args_list(
const FunctionArgsList& args)
const override
167 std::apply([&b,&args](
auto &&... x)
169 ((b &= x->belongs_to_args_list(args)), ...);
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